File: data_reader_test_base.py

package info (click to toggle)
mayavi2 4.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (95 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Author: Suyog Dutt Jain <suyog.jain@aero.iitb.ac.in>
# Copyright (c) 2009,  Enthought, Inc.
# License: BSD Style.

# Standard library imports.
from os.path import abspath
from io import BytesIO
import copy
import unittest
import numpy
from mayavi.core.null_engine import NullEngine
from mayavi.modules.outline import Outline

class DataReaderTestBase(unittest.TestCase):

    def setup_reader(self):

        """"Setup the reader in here.  This is called after the engine
        has been created and started.  The engine is available as
        self.e.  This method is called by setUp().
        """
        raise NotImplementedError()

    def setup_viz(self):
        """Setup the visualization."""
        # Create an outline for the data.
        o = Outline()
        self.e.add_module(o)

    def setUp(self):

        e = NullEngine()
        # Uncomment to see visualization for debugging etc.
        #e = Engine()
        e.start()
        self.e = e
        s = e.new_scene()
        self.scene = e.current_scene

        self.setup_reader()
        self.setup_viz()
        return

    def tearDown(self):
        """For necessary clean up, automatically called by TestCase after the test methods have been invoked"""
        self.e.stop()
        return

    def check(self, scene, bounds, error = 1.01e-02):
        """Do the actual testing."""

        src = scene.children[0]
        ot = src.children[0].children[0]
        ot.render()  # Flush the pipeline.

        # Check the outline bounds
        got = ot.get_output_dataset().bounds
        msg = "Got: %s; expected: %s"%(got, bounds)
        self.assertEqual(numpy.allclose(got, bounds, atol=error), True, msg)

    def check_saving(self, engine, scene, bounds, error = 1.01e-02):

        # Save visualization.
        f = BytesIO()
        f.name = abspath('test.mv2') # We simulate a file.
        engine.save_visualization(f)
        f.seek(0) # So we can read this saved data.

        # Remove existing scene.
        engine.close_scene(scene)

        # Load visualization
        engine.load_visualization(f)
        self.scene = engine.current_scene
        self.check(scene,bounds,error)

    def check_deepcopying(self, scene, bounds, error = 1.01e-02):
        """Test if the MayaVi2 visualization can be deep-copied."""
        ############################################################

        # Test if the MayaVi2 visualization can be deep-copied.

        # Pop the source object.
        source = scene.children.pop()
        # Add it back to see if that works without error.
        scene.children.append(source)
        self.check(scene, bounds, error)

        # Now deepcopy the source and replace the existing one with
        # the copy.  This basically simulates cutting/copying the
        # object from the UI via the right-click menu on the tree
        # view, and pasting the copy back.
        source1 = copy.deepcopy(source)
        scene.children[0] = source1
        self.check(scene, bounds, error)