-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_numtraits.py
367 lines (269 loc) · 12.3 KB
/
test_numtraits.py
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import pytest
import numpy as np
from numtraits import NumericalTrait
from traitlets import HasTraits, TraitError
class ScalarProperties(HasTraits):
a = NumericalTrait(ndim=0)
b = NumericalTrait(ndim=0, domain='positive')
c = NumericalTrait(ndim=0, domain='strictly-positive')
d = NumericalTrait(ndim=0, domain='negative')
e = NumericalTrait(ndim=0, domain='strictly-negative')
f = NumericalTrait(ndim=0, domain=(3, 4))
class TestScalar(object):
def setup_method(self, method):
# Make sure we have an instance of ScalarProperties each time
# a testing method is called.
self.sp = ScalarProperties()
def test_simple(self):
self.sp.a = 1.
assert self.sp.a == 1.
def test_scalar(self):
with pytest.raises(TraitError) as exc:
self.sp.a = [1, 2]
assert exc.value.args[0] == "a should be a scalar value"
def test_numerical(self):
with pytest.raises(TraitError) as exc:
self.sp.a = 'a'
assert exc.value.args[0] == "a should be a numerical value"
def test_positive(self):
self.sp.b = 3.
self.sp.b = 0.
with pytest.raises(TraitError) as exc:
self.sp.b = -2
assert exc.value.args[0] == "b should be positive"
def test_strictly_positive(self):
self.sp.c = 3.
with pytest.raises(TraitError) as exc:
self.sp.c = 0.
assert exc.value.args[0] == "c should be strictly positive"
with pytest.raises(TraitError) as exc:
self.sp.c = -2
assert exc.value.args[0] == "c should be strictly positive"
def test_negative(self):
self.sp.d = -3.
self.sp.d = 0.
with pytest.raises(TraitError) as exc:
self.sp.d = 2
assert exc.value.args[0] == "d should be negative"
def test_strictly_negative(self):
self.sp.e = -2
with pytest.raises(TraitError) as exc:
self.sp.e = 0.
assert exc.value.args[0] == "e should be strictly negative"
with pytest.raises(TraitError) as exc:
self.sp.e = 2
assert exc.value.args[0] == "e should be strictly negative"
def test_range(self):
self.sp.f = 3.
self.sp.f = 3.5
self.sp.f = 4.
with pytest.raises(TraitError) as exc:
self.sp.f = 0.
assert exc.value.args[0] == "f should be in the range [3:4]"
with pytest.raises(TraitError) as exc:
self.sp.f = 7
assert exc.value.args[0] == "f should be in the range [3:4]"
class ArrayProperties(HasTraits):
a = NumericalTrait(shape=(3,))
b = NumericalTrait(domain='positive', ndim=1)
c = NumericalTrait(domain='strictly-positive', ndim=1)
d = NumericalTrait(domain='negative', ndim=1)
e = NumericalTrait(domain='strictly-negative', ndim=1)
f = NumericalTrait(domain=(3, 4), ndim=1)
g = NumericalTrait(shape=(3, 4))
class TestArray(object):
def setup_method(self, method):
self.ap = ArrayProperties()
def test_simple(self):
self.ap.a = (1, 2, 3)
self.ap.b = (1, 2, 3, 4)
np.testing.assert_allclose(self.ap.a, (1, 2, 3))
np.testing.assert_allclose(self.ap.b, (1, 2, 3, 4))
assert isinstance(self.ap.a, np.ndarray)
assert isinstance(self.ap.b, np.ndarray)
def test_shape(self):
with pytest.raises(TraitError) as exc:
self.ap.a = (1, 2, 3, 4)
assert exc.value.args[0] == "a has incorrect length (expected 3 but found 4)"
def test_ndim(self):
with pytest.raises(TraitError) as exc:
self.ap.a = np.ones((3, 3))
assert exc.value.args[0] == "a should be a 1-d sequence"
def test_positive(self):
self.ap.b = (0., 2., 3.)
with pytest.raises(TraitError) as exc:
self.ap.b = (0., -1., 3.)
assert exc.value.args[0] == "All values of b should be positive"
def test_strictly_positive(self):
self.ap.c = (1., 2., 3.)
with pytest.raises(TraitError) as exc:
self.ap.c = (0., 2., 3.)
assert exc.value.args[0] == "All values of c should be strictly positive"
with pytest.raises(TraitError) as exc:
self.ap.c = (0., -1., 3.)
assert exc.value.args[0] == "All values of c should be strictly positive"
def test_negative(self):
self.ap.d = (-1., -2., 0.)
with pytest.raises(TraitError) as exc:
self.ap.d = (-1., -2., 1.)
assert exc.value.args[0] == "All values of d should be negative"
def test_strictly_negative(self):
self.ap.e = (-1., -2., -3.)
with pytest.raises(TraitError) as exc:
self.ap.e = (-1., -2., 0.)
assert exc.value.args[0] == "All values of e should be strictly negative"
with pytest.raises(TraitError) as exc:
self.ap.e = (-1., -2., 1.)
assert exc.value.args[0] == "All values of e should be strictly negative"
def test_range(self):
self.ap.f = (3., 3.5, 4.)
with pytest.raises(TraitError) as exc:
self.ap.f = (0., 3.5, 4.)
assert exc.value.args[0] == "All values of f should be in the range [3:4]"
with pytest.raises(TraitError) as exc:
self.ap.f = (0., 3.5, 7.)
assert exc.value.args[0] == "All values of f should be in the range [3:4]"
def test_shape_2d(self):
self.ap.g = np.ones((3, 4))
with pytest.raises(TraitError) as exc:
self.ap.g = np.ones((3, 6))
assert exc.value.args[0] == "g has incorrect shape (expected (3, 4) but found (3, 6))"
def test_ndim_2d(self):
with pytest.raises(TraitError) as exc:
self.ap.g = np.ones((3, 6, 3))
assert exc.value.args[0] == "g should be a 2-d array"
def test_invalid(self):
with pytest.raises(TraitError) as exc:
self.ap.a = [[1.], [1., 2.]]
assert exc.value.args[0] == "Could not convert value of a to a Numpy array (Exception: setting an array element with a sequence.)"
# Need to decide on behavior if passing a unit-ed quantity to a trait
# with no convertible_to argument.
try:
from astropy import units as u
from pint import UnitRegistry
import quantities as pq
except ImportError:
pass
else:
ureg = UnitRegistry()
class AstropyUnitsProperties(HasTraits):
a = NumericalTrait(convertible_to=u.m)
b = NumericalTrait(convertible_to=u.cm / u.s)
class TestAstropyUnits(object):
def setup_method(self, method):
self.aup = AstropyUnitsProperties()
def test_valid(self):
self.aup.a = 3 * u.m
self.aup.a = [1, 2, 3] * u.cm
self.aup.a = np.ones((2, 2)) * u.pc
self.aup.b = 3 * u.m / u.yr
self.aup.b = [1, 2, 3] * u.cm / u.s
self.aup.b = np.ones((2, 2)) * u.pc / u.Myr
def test_invalid_type(self):
with pytest.raises(TraitError) as exc:
self.aup.a = 5
assert exc.value.args[0] == 'a should be given as an Astropy Quantity instance'
with pytest.raises(TraitError) as exc:
self.aup.b = np.ones((2, 5))
assert exc.value.args[0] == 'b should be given as an Astropy Quantity instance'
def test_invalid_framework(self):
# pint
with pytest.raises(TraitError) as exc:
self.aup.a = 5 * ureg.m
assert exc.value.args[0] == 'a should be given as an Astropy Quantity instance'
# quantities
with pytest.raises(TraitError) as exc:
self.aup.b = 3 * pq.cm
assert exc.value.args[0] == 'b should be given as an Astropy Quantity instance'
def test_invalid_units(self):
with pytest.raises(TraitError) as exc:
self.aup.a = 5 * u.s
assert exc.value.args[0] == 'a should be in units convertible to m'
with pytest.raises(TraitError) as exc:
self.aup.b = np.ones((2, 5)) * u.s
assert exc.value.args[0] == 'b should be in units convertible to cm / s'
class PintUnitsProperties(HasTraits):
a = NumericalTrait(convertible_to=ureg.m)
b = NumericalTrait(convertible_to=ureg.cm / ureg.s)
class TestPintUnits(object):
def setup_method(self, method):
self.pup = PintUnitsProperties()
def test_valid(self):
from astropy import units as u
self.pup.a = 3 * ureg.m
self.pup.a = [1, 2, 3] * ureg.cm
self.pup.a = np.ones((2, 2)) * ureg.pc
self.pup.b = 3 * ureg.m / ureg.year
self.pup.b = [1, 2, 3] * ureg.cm / ureg.s
self.pup.b = np.ones((2, 2)) * ureg.pc / ureg.megayear
def test_invalid_type(self):
with pytest.raises(TraitError) as exc:
self.pup.a = 5
assert exc.value.args[0] == 'a should be given as a Pint Quantity instance'
with pytest.raises(TraitError) as exc:
self.pup.b = np.ones((2, 5))
assert exc.value.args[0] == 'b should be given as a Pint Quantity instance'
def test_invalid_framework(self):
# astropy.units
with pytest.raises(TraitError) as exc:
self.pup.b = 5 * u.m
assert exc.value.args[0] == 'b should be given as a Pint Quantity instance'
# quantitites
with pytest.raises(TraitError) as exc:
self.pup.b = 3 * pq.cm
assert exc.value.args[0] == 'b should be given as a Pint Quantity instance'
def test_invalid_units(self):
from astropy import units as u
with pytest.raises(TraitError) as exc:
self.pup.a = 5 * ureg.s
assert exc.value.args[0] == 'a should be in units convertible to meter'
with pytest.raises(TraitError) as exc:
self.pup.b = np.ones((2, 5)) * ureg.s
assert exc.value.args[0] == 'b should be in units convertible to centimeter / second'
class QuantitiesUnitsProperties(HasTraits):
a = NumericalTrait(convertible_to=pq.m)
b = NumericalTrait(convertible_to=pq.cm / pq.s)
class TestQuantitiesUnits(object):
def setup_method(self, method):
self.qup = QuantitiesUnitsProperties()
def test_valid(self):
from astropy import units as u
self.qup.a = 3 * pq.m
self.qup.a = [1, 2, 3] * pq.cm
self.qup.a = np.ones((2, 2)) * pq.pc
self.qup.b = 3 * pq.m / pq.year
self.qup.b = [1, 2, 3] * pq.cm / pq.s
self.qup.b = np.ones((2, 2)) * pq.pc / pq.s
def test_invalid_type(self):
with pytest.raises(TraitError) as exc:
self.qup.a = 5
assert exc.value.args[0] == 'a should be given as a quantities Quantity instance'
with pytest.raises(TraitError) as exc:
self.qup.b = np.ones((2, 5))
assert exc.value.args[0] == 'b should be given as a quantities Quantity instance'
def test_invalid_framework(self):
# astropy.units
with pytest.raises(TraitError) as exc:
self.qup.a = 5 * u.m
assert exc.value.args[0] == 'a should be given as a quantities Quantity instance'
# pint
with pytest.raises(TraitError) as exc:
self.qup.b = 3 * ureg.cm
assert exc.value.args[0] == 'b should be given as a quantities Quantity instance'
def test_invalid_units(self):
from astropy import units as u
with pytest.raises(TraitError) as exc:
self.qup.a = 5 * pq.s
assert exc.value.args[0] == 'a should be in units convertible to m'
with pytest.raises(TraitError) as exc:
self.qup.b = np.ones((2, 5)) * pq.s
assert exc.value.args[0] == 'b should be in units convertible to cm/s'
# TODO: add test for domain with units
def test_inconsistent_ndim_shape():
with pytest.raises(TraitError) as exc:
a = NumericalTrait(ndim=3, shape=(3, 3))
assert exc.value.args[0] == "shape=(3, 3) and ndim=3 are inconsistent"
def test_invalid_unit_framework():
with pytest.raises(TraitError) as exc:
a = NumericalTrait(convertible_to='m')
assert exc.value.args[0] == "Could not identify unit framework for target unit of type str"