2
2
import faulthandler
3
3
import unittest
4
4
5
- import numpy
5
+ import numpy as np
6
6
7
7
faulthandler .enable () # to debug seg faults and timeouts
8
8
@@ -14,7 +14,7 @@ class AuxiliaryCoordinateTest(unittest.TestCase):
14
14
15
15
aux1 = cf .AuxiliaryCoordinate ()
16
16
aux1 .standard_name = "latitude"
17
- a = numpy .array (
17
+ a = np .array (
18
18
[
19
19
- 30 ,
20
20
- 23.5 ,
@@ -33,7 +33,7 @@ class AuxiliaryCoordinateTest(unittest.TestCase):
33
33
)
34
34
aux1 .set_data (cf .Data (a , "degrees_north" ))
35
35
bounds = cf .Bounds ()
36
- b = numpy .empty (a .shape + (2 ,))
36
+ b = np .empty (a .shape + (2 ,))
37
37
b [:, 0 ] = a - 0.1
38
38
b [:, 1 ] = a + 0.1
39
39
bounds .set_data (cf .Data (b ))
@@ -97,7 +97,7 @@ def test_AuxiliaryCoordinate_transpose(self):
97
97
x = self .f .auxiliary_coordinate ("longitude" ).copy ()
98
98
99
99
bounds = cf .Bounds (
100
- data = cf .Data (numpy .arange (9 * 10 * 4 ).reshape (9 , 10 , 4 ))
100
+ data = cf .Data (np .arange (9 * 10 * 4 ).reshape (9 , 10 , 4 ))
101
101
)
102
102
x .set_bounds (bounds )
103
103
@@ -116,7 +116,7 @@ def test_AuxiliaryCoordinate_squeeze(self):
116
116
x = self .f .auxiliary_coordinate ("longitude" ).copy ()
117
117
118
118
bounds = cf .Bounds (
119
- data = cf .Data (numpy .arange (9 * 10 * 4 ).reshape (9 , 10 , 4 ))
119
+ data = cf .Data (np .arange (9 * 10 * 4 ).reshape (9 , 10 , 4 ))
120
120
)
121
121
x .set_bounds (bounds )
122
122
x .insert_dimension (1 , inplace = True )
@@ -139,61 +139,53 @@ def test_AuxiliaryCoordinate_floor(self):
139
139
a = aux .array
140
140
b = aux .bounds .array
141
141
142
- self .assertTrue ((aux .floor ().array == numpy .floor (a )).all ())
143
- self .assertTrue ((aux .floor ().bounds .array == numpy .floor (b )).all ())
144
- self .assertTrue (
145
- (aux .floor (bounds = False ).array == numpy .floor (a )).all ()
146
- )
142
+ self .assertTrue ((aux .floor ().array == np .floor (a )).all ())
143
+ self .assertTrue ((aux .floor ().bounds .array == np .floor (b )).all ())
144
+ self .assertTrue ((aux .floor (bounds = False ).array == np .floor (a )).all ())
147
145
self .assertTrue ((aux .floor (bounds = False ).bounds .array == b ).all ())
148
146
149
147
aux .del_bounds ()
150
- self .assertTrue ((aux .floor ().array == numpy .floor (a )).all ())
151
- self .assertTrue (
152
- (aux .floor (bounds = False ).array == numpy .floor (a )).all ()
153
- )
148
+ self .assertTrue ((aux .floor ().array == np .floor (a )).all ())
149
+ self .assertTrue ((aux .floor (bounds = False ).array == np .floor (a )).all ())
154
150
155
151
self .assertIsNone (aux .floor (inplace = True ))
156
- self .assertTrue ((aux .array == numpy .floor (a )).all ())
152
+ self .assertTrue ((aux .array == np .floor (a )).all ())
157
153
158
154
def test_AuxiliaryCoordinate_ceil (self ):
159
155
aux = self .aux1 .copy ()
160
156
161
157
a = aux .array
162
158
b = aux .bounds .array
163
159
164
- self .assertTrue ((aux .ceil ().array == numpy .ceil (a )).all ())
165
- self .assertTrue ((aux .ceil ().bounds .array == numpy .ceil (b )).all ())
166
- self .assertTrue ((aux .ceil (bounds = False ).array == numpy .ceil (a )).all ())
160
+ self .assertTrue ((aux .ceil ().array == np .ceil (a )).all ())
161
+ self .assertTrue ((aux .ceil ().bounds .array == np .ceil (b )).all ())
162
+ self .assertTrue ((aux .ceil (bounds = False ).array == np .ceil (a )).all ())
167
163
self .assertTrue ((aux .ceil (bounds = False ).bounds .array == b ).all ())
168
164
169
165
aux .del_bounds ()
170
- self .assertTrue ((aux .ceil ().array == numpy .ceil (a )).all ())
171
- self .assertTrue ((aux .ceil (bounds = False ).array == numpy .ceil (a )).all ())
166
+ self .assertTrue ((aux .ceil ().array == np .ceil (a )).all ())
167
+ self .assertTrue ((aux .ceil (bounds = False ).array == np .ceil (a )).all ())
172
168
173
169
self .assertIsNone (aux .ceil (inplace = True ))
174
- self .assertTrue ((aux .array == numpy .ceil (a )).all ())
170
+ self .assertTrue ((aux .array == np .ceil (a )).all ())
175
171
176
172
def test_AuxiliaryCoordinate_trunc (self ):
177
173
aux = self .aux1 .copy ()
178
174
179
175
a = aux .array
180
176
b = aux .bounds .array
181
177
182
- self .assertTrue ((aux .trunc ().array == numpy .trunc (a )).all ())
183
- self .assertTrue ((aux .trunc ().bounds .array == numpy .trunc (b )).all ())
184
- self .assertTrue (
185
- (aux .trunc (bounds = False ).array == numpy .trunc (a )).all ()
186
- )
178
+ self .assertTrue ((aux .trunc ().array == np .trunc (a )).all ())
179
+ self .assertTrue ((aux .trunc ().bounds .array == np .trunc (b )).all ())
180
+ self .assertTrue ((aux .trunc (bounds = False ).array == np .trunc (a )).all ())
187
181
self .assertTrue ((aux .trunc (bounds = False ).bounds .array == b ).all ())
188
182
189
183
aux .del_bounds ()
190
- self .assertTrue ((aux .trunc ().array == numpy .trunc (a )).all ())
191
- self .assertTrue (
192
- (aux .trunc (bounds = False ).array == numpy .trunc (a )).all ()
193
- )
184
+ self .assertTrue ((aux .trunc ().array == np .trunc (a )).all ())
185
+ self .assertTrue ((aux .trunc (bounds = False ).array == np .trunc (a )).all ())
194
186
195
187
self .assertIsNone (aux .trunc (inplace = True ))
196
- self .assertTrue ((aux .array == numpy .trunc (a )).all ())
188
+ self .assertTrue ((aux .array == np .trunc (a )).all ())
197
189
198
190
def test_AuxiliaryCoordinate_rint (self ):
199
191
aux = self .aux1 .copy ()
@@ -204,17 +196,17 @@ def test_AuxiliaryCoordinate_rint(self):
204
196
x0 = aux .rint ()
205
197
x = x0 .array
206
198
207
- self .assertTrue ((x == numpy .rint (a )).all (), x )
208
- self .assertTrue ((aux .rint ().bounds .array == numpy .rint (b )).all ())
209
- self .assertTrue ((aux .rint (bounds = False ).array == numpy .rint (a )).all ())
199
+ self .assertTrue ((x == np .rint (a )).all (), x )
200
+ self .assertTrue ((aux .rint ().bounds .array == np .rint (b )).all ())
201
+ self .assertTrue ((aux .rint (bounds = False ).array == np .rint (a )).all ())
210
202
self .assertTrue ((aux .rint (bounds = False ).bounds .array == b ).all ())
211
203
212
204
aux .del_bounds ()
213
- self .assertTrue ((aux .rint ().array == numpy .rint (a )).all ())
214
- self .assertTrue ((aux .rint (bounds = False ).array == numpy .rint (a )).all ())
205
+ self .assertTrue ((aux .rint ().array == np .rint (a )).all ())
206
+ self .assertTrue ((aux .rint (bounds = False ).array == np .rint (a )).all ())
215
207
216
208
self .assertIsNone (aux .rint (inplace = True ))
217
- self .assertTrue ((aux .array == numpy .rint (a )).all ())
209
+ self .assertTrue ((aux .array == np .rint (a )).all ())
218
210
219
211
def test_AuxiliaryCoordinate_sin_cos_tan (self ):
220
212
aux = self .aux1 .copy ()
@@ -269,18 +261,17 @@ def test_AuxiliaryCoordinate_round(self):
269
261
aux = self .aux1 .copy ()
270
262
271
263
self .assertTrue (
272
- (aux .round (decimals ).array == numpy .round (a , decimals )).all ()
264
+ (aux .round (decimals ).array == np .round (a , decimals )).all ()
273
265
)
274
266
self .assertTrue (
275
267
(
276
- aux .round (decimals ).bounds .array
277
- == numpy .round (b , decimals )
268
+ aux .round (decimals ).bounds .array == np .round (b , decimals )
278
269
).all ()
279
270
)
280
271
self .assertTrue (
281
272
(
282
273
aux .round (decimals , bounds = False ).array
283
- == numpy .round (a , decimals )
274
+ == np .round (a , decimals )
284
275
).all ()
285
276
)
286
277
self .assertTrue (
@@ -289,51 +280,64 @@ def test_AuxiliaryCoordinate_round(self):
289
280
290
281
aux .del_bounds ()
291
282
self .assertTrue (
292
- (aux .round (decimals ).array == numpy .round (a , decimals )).all ()
283
+ (aux .round (decimals ).array == np .round (a , decimals )).all ()
293
284
)
294
285
self .assertTrue (
295
286
(
296
287
aux .round (decimals , bounds = False ).array
297
- == numpy .round (a , decimals )
288
+ == np .round (a , decimals )
298
289
).all ()
299
290
)
300
291
301
292
self .assertIsNone (aux .round (decimals , inplace = True ))
302
- self .assertTrue ((aux .array == numpy .round (a , decimals )).all ())
293
+ self .assertTrue ((aux .array == np .round (a , decimals )).all ())
303
294
304
295
def test_AuxiliaryCoordinate_clip (self ):
305
296
aux = self .aux1 .copy ()
306
297
307
298
a = aux .array
308
299
b = aux .bounds .array
309
300
301
+ self .assertTrue ((aux .clip (- 15 , 25 ).array == np .clip (a , - 15 , 25 )).all ())
310
302
self .assertTrue (
311
- (aux .clip (- 15 , 25 ).array == numpy .clip (a , - 15 , 25 )).all ()
312
- )
313
- self .assertTrue (
314
- (aux .clip (- 15 , 25 ).bounds .array == numpy .clip (b , - 15 , 25 )).all ()
303
+ (aux .clip (- 15 , 25 ).bounds .array == np .clip (b , - 15 , 25 )).all ()
315
304
)
316
305
self .assertTrue (
317
306
(
318
- aux .clip (- 15 , 25 , bounds = False ).array == numpy .clip (a , - 15 , 25 )
307
+ aux .clip (- 15 , 25 , bounds = False ).array == np .clip (a , - 15 , 25 )
319
308
).all ()
320
309
)
321
310
self .assertTrue (
322
311
(aux .clip (- 15 , 25 , bounds = False ).bounds .array == b ).all ()
323
312
)
324
313
325
314
aux .del_bounds ()
326
- self .assertTrue (
327
- (aux .clip (- 15 , 25 ).array == numpy .clip (a , - 15 , 25 )).all ()
328
- )
315
+ self .assertTrue ((aux .clip (- 15 , 25 ).array == np .clip (a , - 15 , 25 )).all ())
329
316
self .assertTrue (
330
317
(
331
- aux .clip (- 15 , 25 , bounds = False ).array == numpy .clip (a , - 15 , 25 )
318
+ aux .clip (- 15 , 25 , bounds = False ).array == np .clip (a , - 15 , 25 )
332
319
).all ()
333
320
)
334
321
335
322
self .assertIsNone (aux .clip (- 15 , 25 , inplace = True ))
336
323
324
+ def test_AuxiliaryCoordinate_filled (self ):
325
+ """Test AuxiliaryCoordinate.filled."""
326
+ a = self .aux1 .copy ()
327
+ a .data .where (cf .lt (0 ), cf .masked , inplace = 1 )
328
+ self .assertEqual (a .data .count_masked (), 6 )
329
+ self .assertIsNone (a .filled (- 999 , inplace = True ))
330
+ values , counts = np .unique (a , return_counts = True )
331
+ self .assertEqual (values [0 ], - 999 )
332
+ self .assertEqual (counts [0 ], 6 )
333
+
334
+ a .bounds .data .where (cf .lt (0 ), cf .masked , inplace = 1 )
335
+ self .assertEqual (a .bounds .data .count_masked (), 13 )
336
+ self .assertIsNone (a .filled (- 999 , inplace = True ))
337
+ values , counts = np .unique (a .bounds , return_counts = True )
338
+ self .assertEqual (values [0 ], - 999 )
339
+ self .assertEqual (counts [0 ], 13 )
340
+
337
341
338
342
if __name__ == "__main__" :
339
343
print ("Run date:" , datetime .datetime .now ())
0 commit comments