test_core.py 8.1 KB
Newer Older
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
1
2
import numpy as np
from tempfile import NamedTemporaryFile
3
import pickle
4
5
import unittest
import tfields
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
6

7
ATOL = 1e-8
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
8

9

dboe's avatar
dboe committed
10
class Base_Check(object):
dboe's avatar
dboe committed
11
12
13
14
15
16
17
18
19
20
21
22
23
    def demand_equal(self, other):
        raise NotImplementedError(self.__class__)

    def test_pickle(self):
        with NamedTemporaryFile(suffix='.pickle') as out_file:
            pickle.dump(self._inst,
                        out_file)
            out_file.flush()
            out_file.seek(0)
            reloaded = pickle.load(out_file)

        self.demand_equal(reloaded)

dboe's avatar
dboe committed
24
25
26
27
28
    def test_copy(self):
        copy = type(self._inst)(self._inst)
        self.demand_equal(copy)
        self.assertIsNot(self._inst, copy)

dboe's avatar
dboe committed
29
30
31
32
    def test_save_npz(self):
        out_file = NamedTemporaryFile(suffix='.npz')
        self._inst.save(out_file.name)
        _ = out_file.seek(0)  # this is only necessary in the test
33
        load_inst = type(self._inst).load(out_file.name)
dboe's avatar
dboe committed
34
35
36
37
        # allow_pickle=True)  ?

        self.demand_equal(load_inst)

38
39
40
41
42
    def test_dict(self):
        d = self._inst._as_dict()
        other = type(self._inst)._from_dict(**d)
        self.demand_equal(other)

dboe's avatar
dboe committed
43
44
45
46
    def tearDown(self):
        del self._inst


dboe's avatar
dboe committed
47
48
49
50
class AbstractNdarray_Check(Base_Check):
    pass


dboe's avatar
dboe committed
51
class Tensors_Check(AbstractNdarray_Check):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
52
    """
53
    Testing derivatives of Tensors
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
54
55
56
    """
    _inst = None

dboe's avatar
dboe committed
57
58
59
60
61
62
63
64
65
    def demand_equal(self, other, atol=False, transformed=False):
        if atol:
            self.assertTrue(self._inst.equal(other, atol=ATOL))
        else:
            self.assertTrue(self._inst.equal(other))
        if not transformed:
            self.assertEqual(self._inst.coord_sys, other.coord_sys)
        self.assertEqual(self._inst.name, other.name)

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
66
    def test_self_equality(self):
67
        # Test equality
dboe's avatar
dboe committed
68
69
70
71
        self.demand_equal(self._inst)
        transformer = self._inst.copy()
        transformer.transform(tfields.bases.CYLINDER)
        self.demand_equal(transformer, atol=True, transformed=True)
dboe's avatar
dboe committed
72
        self.assertIs(self._inst, np.asarray(self._inst))
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
73
74

    def test_cylinderTrafo(self):
75
        # Test coordinate transformations in circle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
76
77
78
79
80
        transformer = self._inst.copy()
        transformer.transform(tfields.bases.CYLINDER)
        if len(self._inst) > 0:
            self.assertFalse(np.array_equal(self._inst, transformer))
        transformer.transform(tfields.bases.CARTESIAN)
dboe's avatar
dboe committed
81
        self.demand_equal(transformer, atol=True, transformed=True)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
82
83

    def test_spericalTrafo(self):
84
        # Test coordinate transformations in circle
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
85
86
87
        transformer = self._inst.copy()
        transformer.transform(tfields.bases.SPHERICAL)
        transformer.transform(tfields.bases.CARTESIAN)
dboe's avatar
dboe committed
88
        self.demand_equal(transformer, atol=True, transformed=True)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
89
90

    def test_basic_merge(self):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
91
        # create 3 copies with different coord_sys
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
92
93
94
95
        merge_list = [self._inst.copy() for i in range(3)]
        merge_list[0].transform(tfields.bases.CARTESIAN)
        merge_list[1].transform(tfields.bases.CYLINDER)
        merge_list[2].transform(tfields.bases.SPHERICAL)
96

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
97
        # merge them and check that the first coord_sys is taken
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
98
        obj = type(self._inst).merged(*merge_list)
99
        self.assertTrue(obj.coord_sys == tfields.bases.CARTESIAN)
100

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
101
        # check that all copies are the same also with new coord_sys
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
102
103
104
105
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[0],
                                obj[i * len(self._inst): (i + 1) *
                                    len(self._inst)],
106
                                atol=ATOL)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
107
108
            self.assertTrue(value)

109
110
        obj_cs = type(self._inst).merged(*merge_list,
                                         coord_sys=tfields.bases.CYLINDER)
111
112
113
114
115
116
        for i in range(len(merge_list)):
            value = np.allclose(merge_list[1],
                                obj_cs[i * len(self._inst): (i + 1) *
                                       len(self._inst)],
                                atol=ATOL)
            self.assertTrue(value)
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
117

118

dboe's avatar
dboe committed
119
class TensorFields_Check(Tensors_Check):
Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
120
121
122
123
124
125
126
127
128
129
    def test_fields(self):
        # field is of type list
        self.assertTrue(isinstance(self._inst.fields, list))
        self.assertTrue(len(self._inst.fields) == len(self._fields))

        for field, target_field in zip(self._inst.fields, self._fields):
            self.assertTrue(np.array_equal(field, target_field))
            # fields are copied not reffered by a pointer
            self.assertFalse(field is target_field)

130

dboe's avatar
dboe committed
131
132
133
134
135
class TensorMaps_Check(TensorFields_Check):
    def test_maps(self):
        self.assertIsNotNone(self._inst.maps)


Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
136
137
138
139
140
"""
EMPTY TESTS
"""


dboe's avatar
dboe committed
141
class Tensors_Empty_Test(Tensors_Check, unittest.TestCase):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
142
143
144
145
    def setUp(self):
        self._inst = tfields.Tensors([], dim=3)


dboe's avatar
dboe committed
146
class TensorFields_Empty_Test(TensorFields_Check, unittest.TestCase):
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
147
148
149
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorFields([], dim=3)
150
151


dboe's avatar
dboe committed
152
class TensorMaps_Empty_Test(TensorMaps_Check, unittest.TestCase):
153
154
155
156
157
    def setUp(self):
        self._fields = []
        self._inst = tfields.TensorMaps([], dim=3)
        self._maps = []
        self._maps_fields = []
158

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
159
160
161
162

class TensorFields_Copy_Test(TensorFields_Empty_Test):
    def setUp(self):
        base = [(-5, 5, 11)] * 3
163
        self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
164
165
166
167
                        tfields.Tensors(range(11**3))]
        tensors = tfields.Tensors.grid(*base)
        self._inst = tfields.TensorFields(tensors, *self._fields)

Daniel Boeckenhoff's avatar
Daniel Boeckenhoff committed
168
169
170
        self.assertTrue(self._fields[0].coord_sys, 'cylinder')
        self.assertTrue(self._fields[1].coord_sys, 'cartesian')

Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
171
172
173
174
175

class TensorMaps_Copy_Test(TensorMaps_Empty_Test):
    def setUp(self):
        base = [(-1, 1, 3)] * 3
        tensors = tfields.Tensors.grid(*base)
176
        self._fields = [tfields.Tensors.grid(*base, coord_sys='cylinder'),
Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
                        tfields.Tensors(range(len(tensors)))]
        self._maps_tensors = [[[0, 0, 0],
                               [1, 2, 3],
                               [1, 5, 9]],
                              [[0, 4],
                               [1, 3]],
                              [[42]]]
        self._maps_fields = [[[42., 21., 11]],
                             [[3, 25]],
                             [[111]]]
        self._maps = [tfields.TensorFields(map_tensors,
                                           *map_fields) for map_tensors,
                      map_fields in zip(self._maps_tensors, self._maps_fields)]
        self._inst = tfields.TensorMaps(tensors, *self._fields,
                                        maps=self._maps)


dboe's avatar
dboe committed
194
195
196
197
198
199
200
201
class Maps_Test(Base_Check, unittest.TestCase):
    def demand_equal(self, other):
        self._inst.equal(other)

    def setUp(self):
        self._inst = tfields.Maps([[[1,2,3]]])


dboe's avatar
dboe committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class Container_Check(AbstractNdarray_Check):
    def demand_equal(self, other):
        raise NotImplementedError(self.__class__)

    def test_item(self):
        if len(self._inst.items) > 0:
            self.assertEqual(len(self._inst), len(self._inst))
            self.assertEqual(type(self._inst), type(self._inst))


class Container_Test(Container_Check, unittest.TestCase):
    def setUp(self):
        sphere = tfields.Mesh3D.grid(
            (1, 1, 1),
            (-np.pi, np.pi, 3),
            (-np.pi / 2, np.pi / 2, 3),
            coord_sys='spherical')
        sphere2 = sphere.copy() * 3
        self._inst = tfields.Container([sphere, sphere2])


dboe's avatar
dboe committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# class ContainerNoList_Test(Container_Check, unittest.TestCase):
#     def demand_equal_tensors(self, one, other, atol=False, transformed=False):
#         if atol:
#             self.assertTrue(one.equal(other, atol=ATOL))
#         else:
#             self.assertTrue(one.equal(other))
#         if not transformed:
#             self.assertEqual(one.coord_sys, other.coord_sys)
#         self.assertEqual(one.name, other.name)
#
#     def demand_equal(self, other):
#         self.demand_equal_tensors(self._inst.items.items[0], other)
#         self.demand_equal_tensors(self._inst.items.items[1], other)
#
#     def setUp(self):
#         t = tfields.TensorFields([[1, 2, 3]])
#         self._inst = tfields.Container(t)
#         print(self._inst.items)
dboe's avatar
dboe committed
241
242


Daniel Böckenhoff (Laptop)'s avatar
Daniel Böckenhoff (Laptop) committed
243
244
if __name__ == '__main__':
    unittest.main()