-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
test_duck_array_ops.py
390 lines (307 loc) · 13.9 KB
/
test_duck_array_ops.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from __future__ import absolute_import, division, print_function
import numpy as np
import pytest
from numpy import array, nan
import warnings
from xarray import DataArray, concat
from xarray.core import duck_array_ops
from xarray.core.duck_array_ops import (
array_notnull_equiv, concatenate, count, first, last, mean, rolling_window,
stack, where)
from xarray.core.pycompat import dask_array_type
from xarray.testing import assert_allclose, assert_equal
from . import (
TestCase, assert_array_equal, has_dask, has_np113, raises_regex,
requires_dask)
class TestOps(TestCase):
def setUp(self):
self.x = array([[[nan, nan, 2., nan],
[nan, 5., 6., nan],
[8., 9., 10., nan]],
[[nan, 13., 14., 15.],
[nan, 17., 18., nan],
[nan, 21., nan, nan]]])
def test_first(self):
expected_results = [array([[nan, 13, 2, 15],
[nan, 5, 6, nan],
[8, 9, 10, nan]]),
array([[8, 5, 2, nan],
[nan, 13, 14, 15]]),
array([[2, 5, 8],
[13, 17, 21]])]
for axis, expected in zip([0, 1, 2, -3, -2, -1],
2 * expected_results):
actual = first(self.x, axis)
assert_array_equal(expected, actual)
expected = self.x[0]
actual = first(self.x, axis=0, skipna=False)
assert_array_equal(expected, actual)
expected = self.x[..., 0]
actual = first(self.x, axis=-1, skipna=False)
assert_array_equal(expected, actual)
with raises_regex(IndexError, 'out of bounds'):
first(self.x, 3)
def test_last(self):
expected_results = [array([[nan, 13, 14, 15],
[nan, 17, 18, nan],
[8, 21, 10, nan]]),
array([[8, 9, 10, nan],
[nan, 21, 18, 15]]),
array([[2, 6, 10],
[15, 18, 21]])]
for axis, expected in zip([0, 1, 2, -3, -2, -1],
2 * expected_results):
actual = last(self.x, axis)
assert_array_equal(expected, actual)
expected = self.x[-1]
actual = last(self.x, axis=0, skipna=False)
assert_array_equal(expected, actual)
expected = self.x[..., -1]
actual = last(self.x, axis=-1, skipna=False)
assert_array_equal(expected, actual)
with raises_regex(IndexError, 'out of bounds'):
last(self.x, 3)
def test_count(self):
assert 12 == count(self.x)
expected = array([[1, 2, 3], [3, 2, 1]])
assert_array_equal(expected, count(self.x, axis=-1))
def test_where_type_promotion(self):
result = where([True, False], [1, 2], ['a', 'b'])
assert_array_equal(result, np.array([1, 'b'], dtype=object))
result = where([True, False], np.array([1, 2], np.float32), np.nan)
assert result.dtype == np.float32
assert_array_equal(result, np.array([1, np.nan], dtype=np.float32))
def test_stack_type_promotion(self):
result = stack([1, 'b'])
assert_array_equal(result, np.array([1, 'b'], dtype=object))
def test_concatenate_type_promotion(self):
result = concatenate([[1], ['b']])
assert_array_equal(result, np.array([1, 'b'], dtype=object))
def test_all_nan_arrays(self):
assert np.isnan(mean([np.nan, np.nan]))
def test_cumsum_1d():
inputs = np.array([0, 1, 2, 3])
expected = np.array([0, 1, 3, 6])
actual = duck_array_ops.cumsum(inputs)
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=0)
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=-1)
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=(0,))
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=())
assert_array_equal(inputs, actual)
def test_cumsum_2d():
inputs = np.array([[1, 2], [3, 4]])
expected = np.array([[1, 3], [4, 10]])
actual = duck_array_ops.cumsum(inputs)
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=(0, 1))
assert_array_equal(expected, actual)
actual = duck_array_ops.cumsum(inputs, axis=())
assert_array_equal(inputs, actual)
def test_cumprod_2d():
inputs = np.array([[1, 2], [3, 4]])
expected = np.array([[1, 2], [3, 2 * 3 * 4]])
actual = duck_array_ops.cumprod(inputs)
assert_array_equal(expected, actual)
actual = duck_array_ops.cumprod(inputs, axis=(0, 1))
assert_array_equal(expected, actual)
actual = duck_array_ops.cumprod(inputs, axis=())
assert_array_equal(inputs, actual)
class TestArrayNotNullEquiv():
@pytest.mark.parametrize("arr1, arr2", [
(np.array([1, 2, 3]), np.array([1, 2, 3])),
(np.array([1, 2, np.nan]), np.array([1, np.nan, 3])),
(np.array([np.nan, 2, np.nan]), np.array([1, np.nan, np.nan])),
])
def test_equal(self, arr1, arr2):
assert array_notnull_equiv(arr1, arr2)
def test_some_not_equal(self):
a = np.array([1, 2, 4])
b = np.array([1, np.nan, 3])
assert not array_notnull_equiv(a, b)
def test_wrong_shape(self):
a = np.array([[1, np.nan, np.nan, 4]])
b = np.array([[1, 2], [np.nan, 4]])
assert not array_notnull_equiv(a, b)
@pytest.mark.parametrize("val1, val2, val3, null", [
(1, 2, 3, None),
(1., 2., 3., np.nan),
(1., 2., 3., None),
('foo', 'bar', 'baz', None),
])
def test_types(self, val1, val2, val3, null):
arr1 = np.array([val1, null, val3, null])
arr2 = np.array([val1, val2, null, null])
assert array_notnull_equiv(arr1, arr2)
def construct_dataarray(dim_num, dtype, contains_nan, dask):
# dimnum <= 3
rng = np.random.RandomState(0)
shapes = [16, 8, 4][:dim_num]
dims = ('x', 'y', 'z')[:dim_num]
if np.issubdtype(dtype, np.floating):
array = rng.randn(*shapes).astype(dtype)
elif np.issubdtype(dtype, np.integer):
array = rng.randint(0, 10, size=shapes).astype(dtype)
elif np.issubdtype(dtype, np.bool_):
array = rng.randint(0, 1, size=shapes).astype(dtype)
elif dtype == str:
array = rng.choice(['a', 'b', 'c', 'd'], size=shapes)
else:
raise ValueError
da = DataArray(array, dims=dims, coords={'x': np.arange(16)}, name='da')
if contains_nan:
da = da.reindex(x=np.arange(20))
if dask and has_dask:
chunks = {d: 4 for d in dims}
da = da.chunk(chunks)
return da
def from_series_or_scalar(se):
try:
return DataArray.from_series(se)
except AttributeError: # scalar case
return DataArray(se)
def series_reduce(da, func, dim, **kwargs):
""" convert DataArray to pd.Series, apply pd.func, then convert back to
a DataArray. Multiple dims cannot be specified."""
if dim is None or da.ndim == 1:
se = da.to_series()
return from_series_or_scalar(getattr(se, func)(**kwargs))
else:
da1 = []
dims = list(da.dims)
dims.remove(dim)
d = dims[0]
for i in range(len(da[d])):
da1.append(series_reduce(da.isel(**{d: i}), func, dim, **kwargs))
if d in da.coords:
return concat(da1, dim=da[d])
return concat(da1, dim=d)
@pytest.mark.parametrize('dim_num', [1, 2])
@pytest.mark.parametrize('dtype', [float, int, np.float32, np.bool_])
@pytest.mark.parametrize('dask', [False, True])
@pytest.mark.parametrize('func', ['sum', 'min', 'max', 'mean', 'var'])
@pytest.mark.parametrize('skipna', [False, True])
@pytest.mark.parametrize('aggdim', [None, 'x'])
def test_reduce(dim_num, dtype, dask, func, skipna, aggdim):
if aggdim == 'y' and dim_num < 2:
pytest.skip('dim not in this test')
if dtype == np.bool_ and func == 'mean':
pytest.skip('numpy does not support this')
if dask and not has_dask:
pytest.skip('requires dask')
rtol = 1e-04 if dtype == np.float32 else 1e-05
da = construct_dataarray(dim_num, dtype, contains_nan=True, dask=dask)
axis = None if aggdim is None else da.get_axis_num(aggdim)
# TODO: remove these after resolving
# https://github.com/dask/dask/issues/3245
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'All-NaN slice')
warnings.filterwarnings('ignore', 'invalid value encountered in')
if has_np113 and da.dtype.kind == 'O' and skipna:
# Numpy < 1.13 does not handle object-type array.
try:
if skipna:
expected = getattr(np, 'nan{}'.format(func))(da.values,
axis=axis)
else:
expected = getattr(np, func)(da.values, axis=axis)
actual = getattr(da, func)(skipna=skipna, dim=aggdim)
assert np.allclose(actual.values, np.array(expected),
rtol=1.0e-4, equal_nan=True)
except (TypeError, AttributeError, ZeroDivisionError):
# TODO currently, numpy does not support some methods such as
# nanmean for object dtype
pass
# make sure the compatiblility with pandas' results.
actual = getattr(da, func)(skipna=skipna, dim=aggdim)
if func == 'var':
expected = series_reduce(da, func, skipna=skipna, dim=aggdim,
ddof=0)
assert_allclose(actual, expected, rtol=rtol)
# also check ddof!=0 case
actual = getattr(da, func)(skipna=skipna, dim=aggdim, ddof=5)
expected = series_reduce(da, func, skipna=skipna, dim=aggdim,
ddof=5)
assert_allclose(actual, expected, rtol=rtol)
else:
expected = series_reduce(da, func, skipna=skipna, dim=aggdim)
assert_allclose(actual, expected, rtol=rtol)
# make sure the dtype argument
if func not in ['max', 'min']:
actual = getattr(da, func)(skipna=skipna, dim=aggdim, dtype=float)
assert actual.dtype == float
# without nan
da = construct_dataarray(dim_num, dtype, contains_nan=False, dask=dask)
actual = getattr(da, func)(skipna=skipna)
expected = getattr(np, 'nan{}'.format(func))(da.values)
if actual.dtype == object:
assert actual.values == np.array(expected)
else:
assert np.allclose(actual.values, np.array(expected), rtol=rtol)
@pytest.mark.parametrize('dim_num', [1, 2])
@pytest.mark.parametrize('dtype', [float, int, np.float32, np.bool_, str])
@pytest.mark.parametrize('contains_nan', [True, False])
@pytest.mark.parametrize('dask', [False, True])
@pytest.mark.parametrize('func', ['min', 'max'])
@pytest.mark.parametrize('skipna', [False, True])
@pytest.mark.parametrize('aggdim', ['x', 'y'])
def test_argmin_max(dim_num, dtype, contains_nan, dask, func, skipna, aggdim):
# pandas-dev/pandas#16830, we do not check consistency with pandas but
# just make sure da[da.argmin()] == da.min()
if aggdim == 'y' and dim_num < 2:
pytest.skip('dim not in this test')
if dask and not has_dask:
pytest.skip('requires dask')
if contains_nan:
if not skipna:
pytest.skip("numpy's argmin (not nanargmin) does not handle "
"object-dtype")
if skipna and np.dtype(dtype).kind in 'iufc':
pytest.skip("numpy's nanargmin raises ValueError for all nan axis")
da = construct_dataarray(dim_num, dtype, contains_nan=contains_nan,
dask=dask)
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'All-NaN slice')
if aggdim == 'y' and contains_nan and skipna:
with pytest.raises(ValueError):
actual = da.isel(**{
aggdim: getattr(da, 'arg' + func)(
dim=aggdim, skipna=skipna).compute()})
return
actual = da.isel(**{aggdim: getattr(da, 'arg' + func)
(dim=aggdim, skipna=skipna).compute()})
expected = getattr(da, func)(dim=aggdim, skipna=skipna)
assert_allclose(actual.drop(actual.coords),
expected.drop(expected.coords))
def test_argmin_max_error():
da = construct_dataarray(2, np.bool_, contains_nan=True, dask=False)
with pytest.raises(ValueError):
da.argmin(dim='y')
@requires_dask
def test_isnull_with_dask():
da = construct_dataarray(2, np.float32, contains_nan=True, dask=True)
assert isinstance(da.isnull().data, dask_array_type)
assert_equal(da.isnull().load(), da.load().isnull())
@pytest.mark.skipif(not has_dask, reason='This is for dask.')
@pytest.mark.parametrize('axis', [0, -1])
@pytest.mark.parametrize('window', [3, 8, 11])
@pytest.mark.parametrize('center', [True, False])
def test_dask_rolling(axis, window, center):
import dask.array as da
x = np.array(np.random.randn(100, 40), dtype=float)
dx = da.from_array(x, chunks=[(6, 30, 30, 20, 14), 8])
expected = rolling_window(x, axis=axis, window=window, center=center,
fill_value=np.nan)
actual = rolling_window(dx, axis=axis, window=window, center=center,
fill_value=np.nan)
assert isinstance(actual, da.Array)
assert_array_equal(actual, expected)
assert actual.shape == expected.shape
# we need to take care of window size if chunk size is small
# window/2 should be smaller than the smallest chunk size.
with pytest.raises(ValueError):
rolling_window(dx, axis=axis, window=100, center=center,
fill_value=np.nan)