File: test_array_source.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 (298 lines) | stat: -rw-r--r-- 9,769 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Tests for the ArraySource class.
"""
# Author: Prabhu Ramachandran <prabhu@aero.iitb.ac.in>
# Copyright (c) 2008, Prabhu Ramachandran Enthought, Inc.
# License: BSD Style.

import unittest
import pickle
import numpy

# Enthought library imports.
from traits.api import TraitError
from mayavi.sources.array_source import ArraySource
from mayavi.modules.outline import Outline
from mayavi.modules.surface import Surface


class TestArraySource(unittest.TestCase):
    def setUp(self):
        d = ArraySource()
        self.data = d

    def make_2d_data(self):
        s = numpy.array([[0, 1], [2, 3]], 'd')
        v = numpy.array([[[1, 1, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1]]], 'd')
        tps = numpy.transpose
        s, v = tps(s), tps(v, (1, 0, 2))
        return s, v

    def make_3d_data(self):
        s = numpy.array([[[0, 1], [2, 3]],
                         [[4, 5], [6, 7]]], 'd')
        v = numpy.array([[[[0, 0, 0],
                           [1, 0, 0]],
                          [[0, 1, 0],
                           [1, 1, 0]]],
                         [[[0, 0, 1],
                           [1, 0, 1]],
                          [[0, 1, 1],
                           [1, 1, 1]]]], 'd')
        tps = numpy.transpose
        s, v = tps(s), tps(v, (2, 1, 0, 3))
        return s, v

    def test_input_validation(self):
        """Tests if only the correct forms of input arrays are supported."""
        obj = self.data
        # These should work.
        obj.scalar_data = numpy.zeros((2, 2), 'd')
        obj.scalar_data = numpy.zeros((2, 2, 2), 'd')
        obj.scalar_data = None
        obj.vector_data = numpy.zeros((2, 2, 3), 'd')
        obj.vector_data = numpy.zeros((2, 2, 2, 3), 'd')
        obj.vector_data = None

        # These should not.
        self.assertRaises(TraitError, setattr, obj, 'scalar_data', [1, 2, 3])
        self.assertRaises(TraitError, setattr, obj, 'scalar_data',
                          numpy.zeros((2, 2, 2, 3), 'd'))
        obj.scalar_data = None
        self.assertRaises(TraitError, setattr, obj, 'vector_data', [[1, 2, 3]])
        self.assertRaises(TraitError, setattr, obj, 'vector_data',
                          numpy.zeros((2, 2, 2, 1), 'd'))
        obj.vector_data = None

        obj.scalar_data = numpy.zeros((2, 2), 'd')
        self.assertRaises(TraitError, setattr, obj, 'vector_data',
                          numpy.zeros((4, 4, 3), 'd'))
        obj.vector_data = numpy.zeros((2, 2, 3), 'd')
        self.assertRaises(TraitError, setattr, obj, 'scalar_data',
                          numpy.zeros((4, 3), 'i'))
        self.assertRaises(TraitError, setattr, obj, 'scalar_data',
                          numpy.zeros((2, 2, 2), 'i'))
        obj.scalar_data = numpy.zeros((2, 2), 'f')

        # Clean up the object so it can be used for further testing.
        obj.scalar_data = obj.vector_data = None

    def test_2d_data(self):
        """Generic tests for 2D data arrays."""
        d = self.data
        sc, vec = self.make_2d_data()
        d.origin = (-1, -1, 0)
        d.scalar_data = sc
        d.vector_data = vec
        d.start()  # Start the object so it flushes the pipeline etc.

        # Create an outline for the data.
        o = Outline()
        d.add_child(o)
        o.start()
        self.assertEqual(tuple(o.actor.actor.bounds),
                         (-1., 0., -1., 0., 0., 0.))
        # Create a surface module.
        surf = Surface()
        d.add_child(surf)
        self.assertEqual(surf.running, True)

        tps = numpy.transpose
        expect = [tps(sc), tps(vec, (1, 0, 2))]
        sc1 = surf.actor.mapper.input.point_data.scalars.to_array()
        self.assertEqual(numpy.allclose(sc1.flatten(),
                         expect[0].flatten()), True)
        vec1 = surf.actor.mapper.input.point_data.vectors.to_array()
        self.assertEqual(numpy.allclose(vec1.flatten(),
                         expect[1].flatten()), True)

    def test_3d_data(self):
        "Test for 3D data arrays."
        # Add a 3D data source
        d = self.data
        sc, vec = self.make_3d_data()
        d.scalar_data = sc
        d.vector_data = vec
        d.start()  # Start the object so it flushes the pipeline etc.

        # Create an outline for the data.
        o = Outline()
        d.add_child(o)
        o.start()
        self.assertEqual(tuple(o.actor.actor.bounds),
                         (0, 1., 0., 1., 0., 1.))
        # Create a surface module.
        surf = Surface()
        d.add_child(surf)
        self.assertEqual(surf.running, True)

        tps = numpy.transpose
        expect = [tps(sc),  tps(vec, (2, 1, 0, 3))]
        sc2 = surf.actor.mapper.input.point_data.scalars.to_array()
        self.assertEqual(numpy.allclose(sc2.flatten(),
                         expect[0].flatten()), True)
        vec2 = surf.actor.mapper.input.point_data.vectors.to_array()
        self.assertEqual(numpy.allclose(vec2.flatten(),
                         expect[1].flatten()), True)

    def test_pickle(self):
        "Test if pickling works."

        # Test if saving a visualization and restoring it works.
        d = self.data
        sc, vec = self.make_3d_data()
        d.scalar_data = sc
        d.vector_data = vec
        d.spacing = [1, 2, 3]
        d.origin = [4, 5, 6]
        d.start()  # Start the object so it flushes the pipeline etc.

        # Create an outline for the data.
        o = Outline()
        d.add_child(o)
        o.start()
        # Create a surface module.
        surf = Surface()
        d.add_child(surf)

        data = pickle.dumps(d)
        del d, surf, o
        d = pickle.loads(data)
        # We must explicitly start the object.
        d.start()
        mm = d.children[0]
        o, surf = mm.children

        # Test the unpciked state.
        self.assertEqual(tuple(o.actor.actor.bounds),
                         (4., 5., 5., 7., 6., 9.))
        self.assertEqual(surf.running, True)
        self.assertEqual(o.running, True)
        self.assertEqual(d.running, True)
        self.assertEqual(numpy.allclose(d.spacing, [1, 2, 3]), True)
        self.assertEqual(numpy.allclose(d.origin,  [4, 5, 6]), True)

        tps = numpy.transpose
        expect = [tps(sc),  tps(vec, (2, 1, 0, 3))]
        sc2 = surf.actor.mapper.input.point_data.scalars.to_array()
        self.assertEqual(numpy.allclose(sc2.flatten(),
                         expect[0].flatten()), True)
        vec2 = surf.actor.mapper.input.point_data.vectors.to_array()
        self.assertEqual(numpy.allclose(vec2.flatten(),
                         expect[1].flatten()), True)


class TestArraySourceAttributes(unittest.TestCase):
    def setUp(self):
        s1 = numpy.ones((2, 2))
        src = ArraySource(scalar_data=s1, scalar_name='s1')
        self.src = src

    def test_add_attribute_works_for_point_data(self):
        # Given
        src = self.src
        s1 = src.scalar_data

        # When
        s2 = s1.ravel() + 1.0
        src.add_attribute(s2, 's2')
        v1 = numpy.ones((4, 3))
        src.add_attribute(v1, 'v1')
        t1 = numpy.ones((4, 9))
        src.add_attribute(t1, 't1')

        # Then
        self.assertTrue(
            numpy.allclose(
                src.image_data.point_data.get_array('s2').to_array(), s2
            )
        )
        self.assertTrue(
            numpy.allclose(
                src.image_data.point_data.get_array('v1').to_array(), v1
            )
        )
        self.assertTrue(
            numpy.allclose(
                src.image_data.point_data.get_array('t1').to_array(), t1)
        )

    def test_add_attribute_works_for_cell_data(self):
        # Given
        src = self.src
        s1 = src.scalar_data

        # When
        s2 = s1.ravel() + 1.0
        src.add_attribute(s2, 's2', category='cell')
        v1 = numpy.ones((4, 3))
        src.add_attribute(v1, 'v1', category='cell')
        t1 = numpy.ones((4, 9))
        src.add_attribute(t1, 't1', category='cell')

        # Then
        self.assertTrue(
            numpy.allclose(
                src.image_data.cell_data.get_array('s2').to_array(), s2
            )
        )
        self.assertTrue(
            numpy.allclose(
                src.image_data.cell_data.get_array('v1').to_array(), v1
            )
        )
        self.assertTrue(
            numpy.allclose(
                src.image_data.cell_data.get_array('t1').to_array(), t1
            )
        )

    def test_add_attribute_raises_errors(self):
        # Given
        src = self.src

        # When/Then
        data = numpy.ones((4, 3, 3))
        self.assertRaises(AssertionError, src.add_attribute, data, 's2')

        data = numpy.ones((4, 5))
        self.assertRaises(AssertionError, src.add_attribute, data, 's2')

    def test_remove_attribute(self):
        # Given
        src = self.src
        s1 = src.scalar_data

        # When
        s2 = s1.ravel() + 1.0
        src.add_attribute(s2, 's2')
        src.remove_attribute('s2')

        # Then
        self.assertEqual(src.image_data.point_data.get_array('s2'), None)
        self.assertTrue(
            numpy.allclose(
                src.image_data.point_data.get_array('s1').to_array(),
                s1.ravel()
            )
        )

    def test_rename_attribute(self):
        # Given
        src = self.src
        s1 = src.scalar_data
        s2 = s1.ravel() + 1.0
        src.add_attribute(s2, 's2')

        # When
        src.rename_attribute('s2', 's3')

        # Then
        self.assertTrue(
            numpy.all(src.image_data.point_data.get_array('s3') == s2)
        )
        self.assertEqual(src.image_data.point_data.get_array('s2'), None)


if __name__ == '__main__':
    unittest.main()