dask-array 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. dask_array/__init__.py +228 -0
  2. dask_array/_backends.py +76 -0
  3. dask_array/_backends_array.py +99 -0
  4. dask_array/_blockwise.py +1410 -0
  5. dask_array/_broadcast.py +272 -0
  6. dask_array/_chunk.py +445 -0
  7. dask_array/_chunk_types.py +54 -0
  8. dask_array/_collection.py +1644 -0
  9. dask_array/_concatenate.py +331 -0
  10. dask_array/_core_utils.py +1365 -0
  11. dask_array/_dispatch.py +141 -0
  12. dask_array/_einsum.py +277 -0
  13. dask_array/_expr.py +544 -0
  14. dask_array/_expr_flow.py +586 -0
  15. dask_array/_gufunc.py +805 -0
  16. dask_array/_histogram.py +617 -0
  17. dask_array/_map_blocks.py +652 -0
  18. dask_array/_new_collection.py +10 -0
  19. dask_array/_numpy_compat.py +135 -0
  20. dask_array/_overlap.py +1159 -0
  21. dask_array/_rechunk.py +1050 -0
  22. dask_array/_reshape.py +710 -0
  23. dask_array/_routines.py +102 -0
  24. dask_array/_shuffle.py +448 -0
  25. dask_array/_stack.py +264 -0
  26. dask_array/_svg.py +291 -0
  27. dask_array/_templates.py +29 -0
  28. dask_array/_test_utils.py +257 -0
  29. dask_array/_ufunc.py +385 -0
  30. dask_array/_utils.py +349 -0
  31. dask_array/_visualize.py +223 -0
  32. dask_array/_xarray.py +337 -0
  33. dask_array/core/__init__.py +34 -0
  34. dask_array/core/_blockwise_funcs.py +312 -0
  35. dask_array/core/_conversion.py +422 -0
  36. dask_array/core/_from_graph.py +97 -0
  37. dask_array/creation/__init__.py +71 -0
  38. dask_array/creation/_arange.py +121 -0
  39. dask_array/creation/_diag.py +116 -0
  40. dask_array/creation/_diagonal.py +241 -0
  41. dask_array/creation/_eye.py +103 -0
  42. dask_array/creation/_linspace.py +102 -0
  43. dask_array/creation/_mesh.py +134 -0
  44. dask_array/creation/_ones_zeros.py +454 -0
  45. dask_array/creation/_pad.py +270 -0
  46. dask_array/creation/_repeat.py +55 -0
  47. dask_array/creation/_tile.py +36 -0
  48. dask_array/creation/_tri.py +28 -0
  49. dask_array/creation/_utils.py +296 -0
  50. dask_array/fft.py +320 -0
  51. dask_array/io/__init__.py +39 -0
  52. dask_array/io/_base.py +10 -0
  53. dask_array/io/_from_array.py +257 -0
  54. dask_array/io/_from_delayed.py +95 -0
  55. dask_array/io/_from_graph.py +54 -0
  56. dask_array/io/_from_npy_stack.py +67 -0
  57. dask_array/io/_store.py +336 -0
  58. dask_array/io/_tiledb.py +159 -0
  59. dask_array/io/_to_npy_stack.py +65 -0
  60. dask_array/io/_zarr.py +449 -0
  61. dask_array/linalg/__init__.py +39 -0
  62. dask_array/linalg/_cholesky.py +234 -0
  63. dask_array/linalg/_lu.py +300 -0
  64. dask_array/linalg/_norm.py +94 -0
  65. dask_array/linalg/_qr.py +601 -0
  66. dask_array/linalg/_solve.py +349 -0
  67. dask_array/linalg/_svd.py +394 -0
  68. dask_array/linalg/_tensordot.py +334 -0
  69. dask_array/linalg/_utils.py +74 -0
  70. dask_array/manipulation/__init__.py +45 -0
  71. dask_array/manipulation/_expand.py +321 -0
  72. dask_array/manipulation/_flip.py +92 -0
  73. dask_array/manipulation/_roll.py +78 -0
  74. dask_array/manipulation/_transpose.py +309 -0
  75. dask_array/random/__init__.py +125 -0
  76. dask_array/random/_choice.py +181 -0
  77. dask_array/random/_expr.py +256 -0
  78. dask_array/random/_generator.py +441 -0
  79. dask_array/random/_random_state.py +259 -0
  80. dask_array/random/_utils.py +84 -0
  81. dask_array/reductions/__init__.py +84 -0
  82. dask_array/reductions/_arg_reduction.py +130 -0
  83. dask_array/reductions/_common.py +1082 -0
  84. dask_array/reductions/_cumulative.py +522 -0
  85. dask_array/reductions/_percentile.py +261 -0
  86. dask_array/reductions/_reduction.py +725 -0
  87. dask_array/reductions/_trace.py +56 -0
  88. dask_array/routines/__init__.py +133 -0
  89. dask_array/routines/_apply.py +84 -0
  90. dask_array/routines/_bincount.py +112 -0
  91. dask_array/routines/_broadcast.py +111 -0
  92. dask_array/routines/_coarsen.py +115 -0
  93. dask_array/routines/_diff.py +79 -0
  94. dask_array/routines/_gradient.py +158 -0
  95. dask_array/routines/_indexing.py +65 -0
  96. dask_array/routines/_insert_delete.py +132 -0
  97. dask_array/routines/_misc.py +122 -0
  98. dask_array/routines/_nonzero.py +72 -0
  99. dask_array/routines/_search.py +123 -0
  100. dask_array/routines/_select.py +113 -0
  101. dask_array/routines/_statistics.py +171 -0
  102. dask_array/routines/_topk.py +82 -0
  103. dask_array/routines/_triangular.py +74 -0
  104. dask_array/routines/_unique.py +232 -0
  105. dask_array/routines/_where.py +62 -0
  106. dask_array/slicing/__init__.py +67 -0
  107. dask_array/slicing/_basic.py +550 -0
  108. dask_array/slicing/_blocks.py +138 -0
  109. dask_array/slicing/_bool_index.py +145 -0
  110. dask_array/slicing/_setitem.py +329 -0
  111. dask_array/slicing/_squeeze.py +101 -0
  112. dask_array/slicing/_utils.py +1133 -0
  113. dask_array/slicing/_vindex.py +282 -0
  114. dask_array/stacking/__init__.py +15 -0
  115. dask_array/stacking/_block.py +83 -0
  116. dask_array/stacking/_simple.py +58 -0
  117. dask_array/templates/array.html.j2 +48 -0
  118. dask_array/tests/__init__.py +0 -0
  119. dask_array/tests/conftest.py +22 -0
  120. dask_array/tests/test_api.py +40 -0
  121. dask_array/tests/test_binary_op_chunks.py +107 -0
  122. dask_array/tests/test_coarse_slice_through_blockwise.py +362 -0
  123. dask_array/tests/test_collection.py +799 -0
  124. dask_array/tests/test_creation.py +1102 -0
  125. dask_array/tests/test_expr_flow.py +143 -0
  126. dask_array/tests/test_linalg.py +1130 -0
  127. dask_array/tests/test_map_blocks_multi_output.py +104 -0
  128. dask_array/tests/test_rechunk_pushdown.py +214 -0
  129. dask_array/tests/test_reductions.py +1091 -0
  130. dask_array/tests/test_routines.py +2853 -0
  131. dask_array/tests/test_shuffle_chunks.py +67 -0
  132. dask_array/tests/test_slice_pushdown.py +968 -0
  133. dask_array/tests/test_slice_through_blockwise.py +678 -0
  134. dask_array/tests/test_slice_through_overlap.py +366 -0
  135. dask_array/tests/test_slice_through_reshape.py +272 -0
  136. dask_array/tests/test_slicing.py +839 -0
  137. dask_array/tests/test_transpose_slice_pushdown.py +208 -0
  138. dask_array/tests/test_visualize.py +94 -0
  139. dask_array/tests/test_xarray.py +193 -0
  140. dask_array-0.1.0.dist-info/METADATA +48 -0
  141. dask_array-0.1.0.dist-info/RECORD +144 -0
  142. dask_array-0.1.0.dist-info/WHEEL +4 -0
  143. dask_array-0.1.0.dist-info/entry_points.txt +2 -0
  144. dask_array-0.1.0.dist-info/licenses/LICENSE +29 -0
@@ -0,0 +1,1102 @@
1
+ from __future__ import annotations
2
+
3
+ import pickle
4
+
5
+ import pytest
6
+
7
+ from dask._task_spec import Alias
8
+ from dask.base import collections_to_expr
9
+
10
+ pytest.importorskip("numpy")
11
+
12
+ import numpy as np
13
+ import pytest
14
+ from tlz import concat
15
+
16
+ import dask
17
+ import dask_array as da
18
+ from dask_array._core_utils import normalize_chunks
19
+ from dask_array._numpy_compat import NUMPY_GE_210, AxisError
20
+ from dask_array._test_utils import assert_eq, same_keys
21
+
22
+
23
+ @pytest.mark.parametrize(
24
+ "backend",
25
+ ["numpy", pytest.param("cupy", marks=pytest.mark.gpu)],
26
+ )
27
+ @pytest.mark.parametrize(
28
+ "funcname",
29
+ [
30
+ "empty_like",
31
+ "empty",
32
+ "ones_like",
33
+ "ones",
34
+ "zeros_like",
35
+ "zeros",
36
+ "full_like",
37
+ "full",
38
+ ],
39
+ )
40
+ @pytest.mark.parametrize("cast_shape", [tuple, list, np.asarray])
41
+ @pytest.mark.parametrize("cast_chunks", [tuple, list, np.asarray])
42
+ @pytest.mark.parametrize("shape, chunks", [((10, 10), (4, 4))])
43
+ @pytest.mark.parametrize("name", [None, "my-name"])
44
+ @pytest.mark.parametrize("order", ["C", "F"])
45
+ @pytest.mark.parametrize("dtype", ["i4"])
46
+ def test_arr_like(funcname, shape, cast_shape, dtype, cast_chunks, chunks, name, order, backend):
47
+ backend_lib = pytest.importorskip(backend)
48
+ with dask.config.set({"array.backend": backend}):
49
+ np_func = getattr(backend_lib, funcname)
50
+ da_func = getattr(da, funcname)
51
+ shape = cast_shape(shape)
52
+ chunks = cast_chunks(chunks)
53
+
54
+ if "full" in funcname:
55
+ old_np_func = np_func
56
+ old_da_func = da_func
57
+
58
+ np_func = lambda *a, **k: old_np_func(*a, fill_value=5, **k)
59
+ da_func = lambda *a, **k: old_da_func(*a, fill_value=5, **k)
60
+
61
+ dtype = np.dtype(dtype)
62
+
63
+ if "like" in funcname:
64
+ a = backend_lib.random.randint(0, 10, shape).astype(dtype)
65
+
66
+ np_r = np_func(a, order=order)
67
+ da_r = da_func(a, order=order, chunks=chunks, name=name)
68
+ else:
69
+ np_r = np_func(shape, order=order, dtype=dtype)
70
+ da_r = da_func(shape, order=order, dtype=dtype, chunks=chunks, name=name)
71
+
72
+ assert np_r.shape == da_r.shape
73
+ assert np_r.dtype == da_r.dtype
74
+
75
+ # Make sure we are using the desired backend
76
+ assert isinstance(da_r._meta, backend_lib.ndarray)
77
+ assert isinstance(da_r.compute(), backend_lib.ndarray)
78
+
79
+ if "empty" not in funcname:
80
+ assert_eq(np_r, da_r)
81
+
82
+ if name is None:
83
+ assert funcname.split("_")[0] in da_r.name
84
+ else:
85
+ assert da_r.name == name
86
+
87
+ if "order" == "F": # FIXME: this is clearly a bug! # noqa: PLR0133
88
+ assert np.isfortran(da_r.compute())
89
+ else:
90
+ assert not np.isfortran(da_r.compute())
91
+
92
+
93
+ @pytest.mark.parametrize(
94
+ "funcname, kwargs",
95
+ [
96
+ ("empty_like", {}),
97
+ ("ones_like", {}),
98
+ ("zeros_like", {}),
99
+ ("full_like", {"fill_value": 5}),
100
+ ],
101
+ )
102
+ @pytest.mark.parametrize(
103
+ "shape, chunks, out_shape",
104
+ [
105
+ ((10, 10), (4, 4), None),
106
+ ((10, 10), (4, 4), (20, 3)),
107
+ ((10, 10), (4), (20)),
108
+ ((10, 10, 10), (4, 2), (5, 5)),
109
+ ((2, 3, 5, 7), None, (3, 5, 7)),
110
+ ((2, 3, 5, 7), (2, 5, 3), (3, 5, 7)),
111
+ ((2, 3, 5, 7), (2, 5, 3, "auto", 3), (11,) + (2, 3, 5, 7)),
112
+ ((2, 3, 5, 7), "auto", (3, 5, 7)),
113
+ ],
114
+ )
115
+ @pytest.mark.parametrize("dtype", ["i4"])
116
+ def test_arr_like_shape(funcname, kwargs, shape, dtype, chunks, out_shape):
117
+ np_func = getattr(np, funcname)
118
+ da_func = getattr(da, funcname)
119
+ a = np.random.randint(0, 10, shape).astype(dtype)
120
+ np_r = np_func(a, shape=out_shape, **kwargs)
121
+ da_r = da_func(a, chunks=chunks, shape=out_shape, **kwargs)
122
+
123
+ assert np_r.shape == da_r.shape
124
+ assert np_r.dtype == da_r.dtype
125
+
126
+ if "empty" not in funcname:
127
+ assert_eq(np_r, da_r)
128
+
129
+
130
+ @pytest.mark.skip(reason="dask scalar inputs not supported")
131
+ @pytest.mark.parametrize("endpoint", [True, False])
132
+ def test_linspace(endpoint):
133
+ darr = da.linspace(6, 49, endpoint=endpoint, chunks=5)
134
+ nparr = np.linspace(6, 49, endpoint=endpoint)
135
+ assert_eq(darr, nparr)
136
+
137
+ darr = da.linspace(1.4, 4.9, endpoint=endpoint, chunks=5, num=13)
138
+ nparr = np.linspace(1.4, 4.9, endpoint=endpoint, num=13)
139
+ assert_eq(darr, nparr)
140
+
141
+ darr = da.linspace(6, 49, endpoint=endpoint, chunks=5, dtype=float)
142
+ nparr = np.linspace(6, 49, endpoint=endpoint, dtype=float)
143
+ assert_eq(darr, nparr)
144
+
145
+ darr, dstep = da.linspace(6, 49, endpoint=endpoint, chunks=5, retstep=True)
146
+ nparr, npstep = np.linspace(6, 49, endpoint=endpoint, retstep=True)
147
+ assert np.allclose(dstep, npstep)
148
+ assert_eq(darr, nparr)
149
+
150
+ darr = da.linspace(1.4, 4.9, endpoint=endpoint, chunks=5, num=13, dtype=int)
151
+ nparr = np.linspace(1.4, 4.9, num=13, endpoint=endpoint, dtype=int)
152
+ assert_eq(darr, nparr)
153
+ assert sorted(da.linspace(1.4, 4.9, endpoint=endpoint, chunks=5, num=13).dask) == sorted(
154
+ da.linspace(1.4, 4.9, endpoint=endpoint, chunks=5, num=13).dask
155
+ )
156
+ assert sorted(da.linspace(6, 49, endpoint=endpoint, chunks=5, dtype=float).dask) == sorted(
157
+ da.linspace(6, 49, endpoint=endpoint, chunks=5, dtype=float).dask
158
+ )
159
+
160
+ x = da.array([0.2, 6.4, 3.0, 1.6])
161
+ nparr = np.linspace(0, 2, 8, endpoint=endpoint)
162
+ darr = da.linspace(da.argmin(x), da.argmax(x) + 1, 8, endpoint=endpoint)
163
+ assert_eq(darr, nparr)
164
+
165
+ nparr = np.linspace(0, 0, 0, endpoint=endpoint)
166
+ darr = da.linspace(0, 0, 0, endpoint=endpoint)
167
+ assert_eq(darr, nparr)
168
+
169
+ nparr = np.linspace(1, 1, 0, endpoint=endpoint)
170
+ darr = da.linspace(1, 1, 0, endpoint=endpoint)
171
+ assert_eq(darr, nparr)
172
+
173
+ nparr = np.linspace(1, 5, 0, endpoint=endpoint)
174
+ darr = da.linspace(1, 5, 0, endpoint=endpoint)
175
+ assert_eq(darr, nparr)
176
+
177
+ nparr = np.linspace(0, 0, 1, endpoint=endpoint)
178
+ darr = da.linspace(0, 0, 1, endpoint=endpoint)
179
+ assert_eq(darr, nparr)
180
+
181
+ nparr = np.linspace(1, 1, 1, endpoint=endpoint)
182
+ darr = da.linspace(1, 1, 1, endpoint=endpoint)
183
+ assert_eq(darr, nparr)
184
+
185
+ nparr = np.linspace(1, 5, 1, endpoint=endpoint)
186
+ darr = da.linspace(1, 5, 1, endpoint=endpoint)
187
+ assert_eq(darr, nparr)
188
+
189
+
190
+ def test_arange():
191
+ darr = da.arange(77, chunks=13)
192
+ nparr = np.arange(77)
193
+ assert_eq(darr, nparr)
194
+
195
+ darr = da.arange(2, 13, chunks=5)
196
+ nparr = np.arange(2, 13)
197
+ assert_eq(darr, nparr)
198
+
199
+ darr = da.arange(4, 21, 9, chunks=13)
200
+ nparr = np.arange(4, 21, 9)
201
+ assert_eq(darr, nparr)
202
+
203
+ # negative steps
204
+ darr = da.arange(53, 5, -3, chunks=5)
205
+ nparr = np.arange(53, 5, -3)
206
+ assert_eq(darr, nparr)
207
+
208
+ darr = da.arange(77, chunks=13, dtype=float)
209
+ nparr = np.arange(77, dtype=float)
210
+ assert_eq(darr, nparr)
211
+
212
+ darr = da.arange(2, 13, chunks=5, dtype=int)
213
+ nparr = np.arange(2, 13, dtype=int)
214
+ assert_eq(darr, nparr)
215
+ assert sorted(da.arange(2, 13, chunks=5).dask) == sorted(da.arange(2, 13, chunks=5).dask)
216
+ assert sorted(da.arange(77, chunks=13, dtype=float).dask) == sorted(da.arange(77, chunks=13, dtype=float).dask)
217
+
218
+ # 0 size output
219
+ darr = da.arange(0, 1, -0.5, chunks=20)
220
+ nparr = np.arange(0, 1, -0.5)
221
+ assert_eq(darr, nparr)
222
+
223
+ darr = da.arange(0, -1, 0.5, chunks=20)
224
+ nparr = np.arange(0, -1, 0.5)
225
+ assert_eq(darr, nparr)
226
+
227
+ # stop and/or step as kwargs
228
+ darr = da.arange(stop=10)
229
+ nparr = np.arange(stop=10)
230
+ assert_eq(darr, nparr)
231
+
232
+ darr = da.arange(10, step=2)
233
+ nparr = np.arange(10, step=2)
234
+ assert_eq(darr, nparr)
235
+
236
+ darr = da.arange(stop=10, step=2)
237
+ nparr = np.arange(stop=10, step=2)
238
+ assert_eq(darr, nparr)
239
+
240
+ darr = da.arange(3, stop=10, step=2)
241
+ nparr = np.arange(3, stop=10, step=2)
242
+ assert_eq(darr, nparr)
243
+
244
+ with pytest.raises(TypeError, match="requires stop"):
245
+ da.arange()
246
+
247
+ # Unexpected or missing kwargs
248
+ with pytest.raises(TypeError, match="whatsthis"):
249
+ da.arange(10, chunks=-1, whatsthis=1)
250
+
251
+ assert da.arange(10).chunks == ((10,),)
252
+
253
+
254
+ arange_dtypes = [
255
+ np.uint8,
256
+ np.uint64,
257
+ np.int8,
258
+ np.int64,
259
+ np.float32,
260
+ np.float64,
261
+ ]
262
+
263
+
264
+ # FIXME hypothesis would be much better suited for this
265
+ @pytest.mark.parametrize("start_type", arange_dtypes + [int, float])
266
+ @pytest.mark.parametrize("stop_type", arange_dtypes + [int, float])
267
+ @pytest.mark.parametrize("step_type", arange_dtypes + [int, float])
268
+ def test_arange_dtype_infer(start_type, stop_type, step_type):
269
+ start = start_type(3)
270
+ stop = stop_type(13)
271
+ step = step_type(2)
272
+ a_np = np.arange(start, stop, step)
273
+ a_da = da.arange(start, stop, step)
274
+ assert_eq(a_np, a_da)
275
+
276
+
277
+ @pytest.mark.parametrize("dtype", arange_dtypes)
278
+ def test_arange_dtype_force(dtype):
279
+ assert da.arange(10, dtype=dtype).dtype == dtype
280
+ assert da.arange(np.float32(10), dtype=dtype).dtype == dtype
281
+ assert da.arange(np.int64(10), dtype=dtype).dtype == dtype
282
+
283
+
284
+ @pytest.mark.skipif(np.array(0).dtype == np.int32, reason="64-bit platforms only")
285
+ @pytest.mark.parametrize(
286
+ "start,stop,step",
287
+ [
288
+ (2**63 - 10_000, 2**63 - 1, 1),
289
+ (2**63 - 1, 2**63 - 10_000, -1),
290
+ (0, 2**63 - 1, 2**63 - 10_000),
291
+ (0.0, 2**63 - 1, 2**63 - 10_000),
292
+ (0.0, -9_131_138_316_486_228_481, -92_233_720_368_547_759),
293
+ (-72_057_594_037_927_945, -72_057_594_037_927_938, 1.0),
294
+ (-72_057_594_037_927_945, -72_057_594_037_927_938, 1.5),
295
+ ],
296
+ )
297
+ @pytest.mark.parametrize("chunks", ["auto", 1])
298
+ def test_arange_very_large_args(start, stop, step, chunks):
299
+ """Test args that are very close to 2**63
300
+ https://github.com/dask/dask/issues/11706
301
+ """
302
+ a_np = np.arange(start, stop, step)
303
+ a_da = da.arange(start, stop, step, chunks=chunks)
304
+ assert_eq(a_np, a_da)
305
+
306
+
307
+ @pytest.mark.xfail(
308
+ reason="Casting floats to ints is not supported since edge behavior is not specified or guaranteed by NumPy."
309
+ )
310
+ def test_arange_cast_float_int_step():
311
+ darr = da.arange(3.3, -9.1, -0.25, chunks=3, dtype="i8")
312
+ nparr = np.arange(3.3, -9.1, -0.25, dtype="i8")
313
+ assert_eq(darr, nparr)
314
+
315
+
316
+ def test_arange_float_step():
317
+ darr = da.arange(2.0, 13.0, 0.3, chunks=4)
318
+ nparr = np.arange(2.0, 13.0, 0.3)
319
+ assert_eq(darr, nparr)
320
+
321
+ darr = da.arange(7.7, 1.5, -0.8, chunks=3)
322
+ nparr = np.arange(7.7, 1.5, -0.8)
323
+ assert_eq(darr, nparr)
324
+
325
+ darr = da.arange(0, 1, 0.01, chunks=20)
326
+ nparr = np.arange(0, 1, 0.01)
327
+ assert_eq(darr, nparr)
328
+
329
+ darr = da.arange(0, 1, 0.03, chunks=20)
330
+ nparr = np.arange(0, 1, 0.03)
331
+ assert_eq(darr, nparr)
332
+
333
+
334
+ def test_indices_wrong_chunks():
335
+ with pytest.raises(ValueError):
336
+ da.indices((1,), chunks=tuple())
337
+
338
+
339
+ def test_indices_dimensions_chunks():
340
+ chunks = ((1, 4, 2, 3), (5, 5))
341
+ darr = da.indices((10, 10), chunks=chunks)
342
+ assert darr.chunks == ((1, 1),) + chunks
343
+
344
+ with dask.config.set({"array.chunk-size": "50 MiB"}):
345
+ shape = (10000, 10000)
346
+ expected = normalize_chunks("auto", shape=shape, dtype=int)
347
+ result = da.indices(shape, chunks="auto")
348
+ # indices prepends a dimension
349
+ actual = result.chunks[1:]
350
+ assert expected == actual
351
+
352
+
353
+ def test_empty_indices():
354
+ darr = da.indices(tuple(), chunks=tuple())
355
+ nparr = np.indices(tuple())
356
+ assert darr.shape == nparr.shape
357
+ assert darr.dtype == nparr.dtype
358
+ assert_eq(darr, nparr)
359
+
360
+ darr = da.indices(tuple(), float, chunks=tuple())
361
+ nparr = np.indices(tuple(), float)
362
+ assert darr.shape == nparr.shape
363
+ assert darr.dtype == nparr.dtype
364
+ assert_eq(darr, nparr)
365
+
366
+ darr = da.indices((0,), float, chunks=(1,))
367
+ nparr = np.indices((0,), float)
368
+ assert darr.shape == nparr.shape
369
+ assert darr.dtype == nparr.dtype
370
+ assert_eq(darr, nparr)
371
+
372
+ darr = da.indices((0, 1, 2), float, chunks=(1, 1, 2))
373
+ nparr = np.indices((0, 1, 2), float)
374
+ assert darr.shape == nparr.shape
375
+ assert darr.dtype == nparr.dtype
376
+ assert_eq(darr, nparr)
377
+
378
+
379
+ def test_indices():
380
+ darr = da.indices((1,), chunks=(1,))
381
+ nparr = np.indices((1,))
382
+ assert_eq(darr, nparr)
383
+
384
+ darr = da.indices((1,), float, chunks=(1,))
385
+ nparr = np.indices((1,), float)
386
+ assert_eq(darr, nparr)
387
+
388
+ darr = da.indices((2, 1), chunks=(2, 1))
389
+ nparr = np.indices((2, 1))
390
+ assert_eq(darr, nparr)
391
+
392
+ darr = da.indices((2, 3), chunks=(1, 2))
393
+ nparr = np.indices((2, 3))
394
+ assert_eq(darr, nparr)
395
+
396
+
397
+ @pytest.mark.parametrize(
398
+ "shapes, chunks",
399
+ [
400
+ ([()], [()]),
401
+ ([(0,)], [(0,)]),
402
+ ([(2,), (3,)], [(1,), (2,)]),
403
+ ([(2,), (3,), (4,)], [(1,), (2,), (3,)]),
404
+ ([(2,), (3,), (4,), (5,)], [(1,), (2,), (3,), (4,)]),
405
+ ([(2, 3), (4,)], [(1, 2), (3,)]),
406
+ ],
407
+ )
408
+ @pytest.mark.parametrize("indexing", ["ij", "xy"])
409
+ @pytest.mark.parametrize("sparse", [False, True])
410
+ def test_meshgrid(shapes, chunks, indexing, sparse):
411
+ xi_a = []
412
+ xi_d = []
413
+ xi_dc = []
414
+ for each_shape, each_chunk in zip(shapes, chunks):
415
+ xi_a.append(np.random.random(each_shape))
416
+ xi_d_e = da.from_array(xi_a[-1], chunks=each_chunk)
417
+ xi_d.append(xi_d_e)
418
+ xi_d_ef = xi_d_e.flatten()
419
+ xi_dc.append(xi_d_ef.chunks[0])
420
+ do = list(range(len(xi_dc)))
421
+ if indexing == "xy" and len(xi_dc) > 1:
422
+ do[0], do[1] = do[1], do[0]
423
+ xi_dc[0], xi_dc[1] = xi_dc[1], xi_dc[0]
424
+ xi_dc = tuple(xi_dc)
425
+
426
+ r_a = np.meshgrid(*xi_a, indexing=indexing, sparse=sparse)
427
+ r_d = da.meshgrid(*xi_d, indexing=indexing, sparse=sparse)
428
+
429
+ assert type(r_d) is type(r_a)
430
+ assert len(r_a) == len(r_d)
431
+
432
+ for e_r_a, e_r_d, i in zip(r_a, r_d, do):
433
+ assert_eq(e_r_a, e_r_d)
434
+ if sparse:
435
+ assert e_r_d.chunks[i] == xi_dc[i]
436
+ else:
437
+ assert e_r_d.chunks == xi_dc
438
+
439
+
440
+ def test_meshgrid_inputcoercion():
441
+ a = [1, 2, 3]
442
+ b = np.array([4, 5, 6, 7])
443
+ x, y = np.meshgrid(a, b, indexing="ij")
444
+ z = x * y
445
+
446
+ x_d, y_d = da.meshgrid(a, b, indexing="ij")
447
+ z_d = x_d * y_d
448
+
449
+ assert z_d.shape == (len(a), len(b))
450
+ assert_eq(z, z_d)
451
+
452
+
453
+ @pytest.mark.parametrize(
454
+ "N, M, k, dtype, chunks",
455
+ [
456
+ (3, None, 0, float, "auto"),
457
+ (4, None, 0, float, "auto"),
458
+ (3, 4, 0, bool, "auto"),
459
+ (3, None, 1, int, "auto"),
460
+ (3, None, -1, int, "auto"),
461
+ (3, None, 2, int, 1),
462
+ (6, 8, -2, int, (3, 4)),
463
+ (6, 8, 0, int, (3, "auto")),
464
+ ],
465
+ )
466
+ def test_tri(N, M, k, dtype, chunks):
467
+ assert_eq(da.tri(N, M, k, dtype, chunks), np.tri(N, M, k, dtype))
468
+
469
+
470
+ def test_eye():
471
+ assert_eq(da.eye(9, chunks=3), np.eye(9))
472
+ assert_eq(da.eye(9), np.eye(9))
473
+ assert_eq(da.eye(10, chunks=3), np.eye(10))
474
+ assert_eq(da.eye(9, chunks=3, M=11), np.eye(9, M=11))
475
+ assert_eq(da.eye(11, chunks=3, M=9), np.eye(11, M=9))
476
+ assert_eq(da.eye(7, chunks=3, M=11), np.eye(7, M=11))
477
+ assert_eq(da.eye(11, chunks=3, M=7), np.eye(11, M=7))
478
+ assert_eq(da.eye(9, chunks=3, k=2), np.eye(9, k=2))
479
+ assert_eq(da.eye(9, chunks=3, k=-2), np.eye(9, k=-2))
480
+ assert_eq(da.eye(7, chunks=3, M=11, k=5), np.eye(7, M=11, k=5))
481
+ assert_eq(da.eye(11, chunks=3, M=7, k=-6), np.eye(11, M=7, k=-6))
482
+ assert_eq(da.eye(6, chunks=3, M=9, k=7), np.eye(6, M=9, k=7))
483
+ assert_eq(da.eye(12, chunks=3, M=6, k=-3), np.eye(12, M=6, k=-3))
484
+
485
+ assert_eq(da.eye(9, chunks=3, dtype=int), np.eye(9, dtype=int))
486
+ assert_eq(da.eye(10, chunks=3, dtype=int), np.eye(10, dtype=int))
487
+ assert_eq(da.eye(10, chunks=-1, dtype=int), np.eye(10, dtype=int))
488
+ assert_eq(da.eye(9, chunks=3, dtype=None), np.eye(9, dtype=None))
489
+
490
+ with dask.config.set({"array.chunk-size": "50 MiB"}):
491
+ x = da.eye(10000, "auto")
492
+ assert 4 < x.npartitions < 32
493
+
494
+
495
+ @pytest.mark.parametrize("k", [0, 3, -3, 8])
496
+ def test_diag_bad_input(k):
497
+ # when input numpy array is neither 1d nor 2d:
498
+ v = np.arange(2 * 3 * 4).reshape((2, 3, 4))
499
+ with pytest.raises(ValueError, match="Array must be 1d or 2d only"):
500
+ da.diag(v, k)
501
+
502
+ # when input dask array is neither 1d nor 2d:
503
+ v = da.arange(2 * 3 * 4).reshape((2, 3, 4))
504
+ with pytest.raises(ValueError, match="Array must be 1d or 2d only"):
505
+ da.diag(v, k)
506
+
507
+ # when input is not an array:
508
+ v = 1
509
+ with pytest.raises(TypeError, match="v must be a dask array or numpy array"):
510
+ da.diag(v, k)
511
+
512
+
513
+ @pytest.mark.parametrize("k", [0, 3, -3, 8])
514
+ def test_diag_2d_array_creation(k):
515
+ # when input 1d-array is a numpy array:
516
+ v = np.arange(11)
517
+ assert_eq(da.diag(v, k), np.diag(v, k))
518
+
519
+ # when input 1d-array is a dask array:
520
+ v = da.arange(11, chunks=3)
521
+ darr = da.diag(v, k)
522
+ nparr = np.diag(v, k)
523
+ assert_eq(darr, nparr)
524
+ assert sorted(da.diag(v, k).dask) == sorted(da.diag(v, k).dask)
525
+
526
+ v = v + v + 3
527
+ darr = da.diag(v, k)
528
+ nparr = np.diag(v, k)
529
+ assert_eq(darr, nparr)
530
+
531
+ v = da.arange(11, chunks=11)
532
+ darr = da.diag(v, k)
533
+ nparr = np.diag(v, k)
534
+ assert_eq(darr, nparr)
535
+ assert sorted(da.diag(v, k).dask) == sorted(da.diag(v, k).dask)
536
+
537
+
538
+ @pytest.mark.parametrize("k", [0, 3, -3, 8])
539
+ def test_diag_extraction(k):
540
+ # when input 2d-array is a square numpy array:
541
+ x = np.arange(64).reshape((8, 8))
542
+ assert_eq(da.diag(x, k), np.diag(x, k))
543
+ # when input 2d-array is a square dask array:
544
+ d = da.from_array(x, chunks=(4, 4))
545
+ assert_eq(da.diag(d, k), np.diag(x, k))
546
+ # heterogeneous chunks:
547
+ d = da.from_array(x, chunks=((3, 2, 3), (4, 1, 2, 1)))
548
+ assert_eq(da.diag(d, k), np.diag(x, k))
549
+
550
+ # when input 2d-array is a rectangular numpy array:
551
+ y = np.arange(5 * 8).reshape((5, 8))
552
+ assert_eq(da.diag(y, k), np.diag(y, k))
553
+ # when input 2d-array is a rectangular dask array:
554
+ d = da.from_array(y, chunks=(4, 4))
555
+ assert_eq(da.diag(d, k), np.diag(y, k))
556
+ # heterogeneous chunks:
557
+ d = da.from_array(y, chunks=((3, 2), (4, 1, 2, 1)))
558
+ assert_eq(da.diag(d, k), np.diag(y, k))
559
+
560
+
561
+ @pytest.mark.skip(reason="data_producer not implemented")
562
+ def test_creation_data_producers():
563
+ x = np.arange(64).reshape((8, 8))
564
+ d = da.from_array(x, chunks=(4, 4))
565
+ dsk = collections_to_expr([d]).__dask_graph__()
566
+ assert all(v.data_producer for v in dsk.values())
567
+
568
+ # blockwise fusion
569
+ x = d.astype("float64")
570
+ dsk = collections_to_expr([x]).__dask_graph__()
571
+ assert sum(v.data_producer for v in dsk.values()) == 4
572
+ assert sum(isinstance(v, Alias) for v in dsk.values()) == 4
573
+ assert len(dsk) == 8
574
+
575
+ # linear fusion
576
+ x = d[slice(0, 6), None].astype("float64")
577
+ dsk = collections_to_expr([x]).__dask_graph__()
578
+ assert sum(v.data_producer for v in dsk.values()) == 4
579
+ assert sum(isinstance(v, Alias) for v in dsk.values()) == 4
580
+ assert len(dsk) == 8
581
+
582
+ # no fusion
583
+ x = d[[1, 3, 5, 7, 6, 4, 2, 0]].astype("float64")
584
+ dsk = collections_to_expr([x]).__dask_graph__()
585
+ assert sum(v.data_producer and "array-" in k[0] for k, v in dsk.items()) == 4
586
+ assert sum(v.data_producer for v in dsk.values()) == 8 # getitem data nodes
587
+ assert len(dsk) == 24
588
+
589
+
590
+ def test_diagonal():
591
+ v = np.arange(11)
592
+ with pytest.raises(ValueError):
593
+ da.diagonal(v)
594
+
595
+ v = np.arange(4).reshape((2, 2))
596
+ with pytest.raises(ValueError):
597
+ da.diagonal(v, axis1=0, axis2=0)
598
+
599
+ with pytest.raises(AxisError):
600
+ da.diagonal(v, axis1=-4)
601
+
602
+ with pytest.raises(AxisError):
603
+ da.diagonal(v, axis2=-4)
604
+
605
+ v = np.arange(4 * 5 * 6).reshape((4, 5, 6))
606
+ v = da.from_array(v, chunks=2)
607
+ assert_eq(da.diagonal(v), np.diagonal(v))
608
+ # Empty diagonal.
609
+ assert_eq(da.diagonal(v, offset=10), np.diagonal(v, offset=10))
610
+ assert_eq(da.diagonal(v, offset=-10), np.diagonal(v, offset=-10))
611
+
612
+ with pytest.raises(ValueError):
613
+ da.diagonal(v, axis1=-2)
614
+
615
+ # Negative axis.
616
+ assert_eq(da.diagonal(v, axis1=-1), np.diagonal(v, axis1=-1))
617
+ assert_eq(da.diagonal(v, offset=1, axis1=-1), np.diagonal(v, offset=1, axis1=-1))
618
+
619
+ # Heterogeneous chunks.
620
+ v = np.arange(2 * 3 * 4 * 5 * 6).reshape((2, 3, 4, 5, 6))
621
+ v = da.from_array(v, chunks=(1, (1, 2), (1, 2, 1), (2, 1, 2), (5, 1)))
622
+
623
+ assert_eq(da.diagonal(v), np.diagonal(v))
624
+ assert_eq(
625
+ da.diagonal(v, offset=2, axis1=3, axis2=1),
626
+ np.diagonal(v, offset=2, axis1=3, axis2=1),
627
+ )
628
+
629
+ assert_eq(
630
+ da.diagonal(v, offset=-2, axis1=3, axis2=1),
631
+ np.diagonal(v, offset=-2, axis1=3, axis2=1),
632
+ )
633
+
634
+ assert_eq(
635
+ da.diagonal(v, offset=-2, axis1=3, axis2=4),
636
+ np.diagonal(v, offset=-2, axis1=3, axis2=4),
637
+ )
638
+
639
+ assert_eq(da.diagonal(v, 1), np.diagonal(v, 1))
640
+ assert_eq(da.diagonal(v, -1), np.diagonal(v, -1))
641
+ # Positional arguments
642
+ assert_eq(da.diagonal(v, 1, 2, 1), np.diagonal(v, 1, 2, 1))
643
+
644
+ v = np.arange(2 * 3 * 4 * 5 * 6).reshape((2, 3, 4, 5, 6))
645
+ assert_eq(da.diagonal(v, axis1=1, axis2=3), np.diagonal(v, axis1=1, axis2=3))
646
+ assert_eq(
647
+ da.diagonal(v, offset=1, axis1=1, axis2=3),
648
+ np.diagonal(v, offset=1, axis1=1, axis2=3),
649
+ )
650
+
651
+ assert_eq(
652
+ da.diagonal(v, offset=1, axis1=3, axis2=1),
653
+ np.diagonal(v, offset=1, axis1=3, axis2=1),
654
+ )
655
+
656
+ assert_eq(
657
+ da.diagonal(v, offset=-5, axis1=3, axis2=1),
658
+ np.diagonal(v, offset=-5, axis1=3, axis2=1),
659
+ )
660
+
661
+ assert_eq(
662
+ da.diagonal(v, offset=-6, axis1=3, axis2=1),
663
+ np.diagonal(v, offset=-6, axis1=3, axis2=1),
664
+ )
665
+
666
+ assert_eq(
667
+ da.diagonal(v, offset=-6, axis1=-3, axis2=1),
668
+ np.diagonal(v, offset=-6, axis1=-3, axis2=1),
669
+ )
670
+
671
+ assert_eq(
672
+ da.diagonal(v, offset=-6, axis1=-3, axis2=1),
673
+ np.diagonal(v, offset=-6, axis1=-3, axis2=1),
674
+ )
675
+
676
+ v = da.from_array(v, chunks=2)
677
+ assert_eq(
678
+ da.diagonal(v, offset=1, axis1=3, axis2=1),
679
+ np.diagonal(v, offset=1, axis1=3, axis2=1),
680
+ )
681
+ assert_eq(
682
+ da.diagonal(v, offset=-1, axis1=3, axis2=1),
683
+ np.diagonal(v, offset=-1, axis1=3, axis2=1),
684
+ )
685
+
686
+ v = np.arange(384).reshape((8, 8, 6))
687
+ assert_eq(da.diagonal(v, offset=-1, axis1=2), np.diagonal(v, offset=-1, axis1=2))
688
+
689
+ v = da.from_array(v, chunks=(4, 4, 2))
690
+ assert_eq(da.diagonal(v, offset=-1, axis1=2), np.diagonal(v, offset=-1, axis1=2))
691
+
692
+
693
+ @pytest.mark.parametrize("dtype", [None, "f8", "i8"])
694
+ @pytest.mark.parametrize(
695
+ "func, kwargs",
696
+ [
697
+ (lambda x, y: x + y, {}),
698
+ (lambda x, y, c=1: x + c * y, {}),
699
+ (lambda x, y, c=1: x + c * y, {"c": 3}),
700
+ ],
701
+ )
702
+ def test_fromfunction(func, dtype, kwargs):
703
+ a = np.fromfunction(func, shape=(5, 5), dtype=dtype, **kwargs)
704
+ d = da.fromfunction(func, shape=(5, 5), chunks=(2, 2), dtype=dtype, **kwargs)
705
+
706
+ assert_eq(d, a)
707
+
708
+ d2 = da.fromfunction(func, shape=(5, 5), chunks=(2, 2), dtype=dtype, **kwargs)
709
+
710
+ assert same_keys(d, d2)
711
+
712
+
713
+ def test_repeat():
714
+ x = np.random.random((10, 11, 13))
715
+ d = da.from_array(x, chunks=(4, 5, 3))
716
+
717
+ repeats = [0, 1, 2, 5]
718
+ axes = [-3, -2, -1, 0, 1, 2]
719
+
720
+ for r in repeats:
721
+ for a in axes:
722
+ assert_eq(x.repeat(r, axis=a), d.repeat(r, axis=a))
723
+
724
+ assert_eq(d.repeat(2, 0), da.repeat(d, 2, 0))
725
+
726
+ with pytest.raises(NotImplementedError):
727
+ da.repeat(d, np.arange(10))
728
+
729
+ with pytest.raises(NotImplementedError):
730
+ da.repeat(d, 2, None)
731
+
732
+ with pytest.raises(NotImplementedError):
733
+ da.repeat(d, 2)
734
+
735
+ for invalid_axis in [3, -4]:
736
+ with pytest.raises(ValueError):
737
+ da.repeat(d, 2, axis=invalid_axis)
738
+
739
+ x = np.arange(5)
740
+ d = da.arange(5, chunks=(2,))
741
+
742
+ assert_eq(x.repeat(3), d.repeat(3))
743
+
744
+ for r in [1, 2, 3, 4]:
745
+ assert all(concat(d.repeat(r).chunks))
746
+
747
+
748
+ @pytest.mark.parametrize("reps", [2, (2, 2), (1, 2), (2, 1), (2, 3, 4, 0)])
749
+ def test_tile_basic(reps):
750
+ a = da.asarray([0, 1, 2])
751
+ b = [[1, 2], [3, 4]]
752
+
753
+ assert_eq(np.tile(a.compute(), reps), da.tile(a, reps))
754
+ assert_eq(np.tile(b, reps), da.tile(b, reps))
755
+
756
+
757
+ @pytest.mark.parametrize("shape, chunks", [((10,), (1,)), ((10, 11, 13), (4, 5, 3))])
758
+ @pytest.mark.parametrize("reps", [0, 1, 2, 3, 5, (1,), (1, 2)])
759
+ def test_tile_chunks(shape, chunks, reps):
760
+ x = np.random.random(shape)
761
+ d = da.from_array(x, chunks=chunks)
762
+
763
+ assert_eq(np.tile(x, reps), da.tile(d, reps))
764
+
765
+
766
+ @pytest.mark.parametrize("shape, chunks", [((10,), (1,)), ((10, 11, 13), (4, 5, 3))])
767
+ @pytest.mark.parametrize("reps", [-1, -5])
768
+ def test_tile_neg_reps(shape, chunks, reps):
769
+ x = np.random.random(shape)
770
+ d = da.from_array(x, chunks=chunks)
771
+
772
+ with pytest.raises(ValueError):
773
+ da.tile(d, reps)
774
+
775
+
776
+ @pytest.mark.parametrize("shape, chunks", [((10,), (1,)), ((10, 11, 13), (4, 5, 3))])
777
+ @pytest.mark.parametrize("reps", [0, (0,), (2, 0), (0, 3, 0, 4)])
778
+ def test_tile_zero_reps(shape, chunks, reps):
779
+ x = np.random.random(shape)
780
+ d = da.from_array(x, chunks=chunks)
781
+
782
+ assert_eq(np.tile(x, reps), da.tile(d, reps))
783
+
784
+
785
+ @pytest.mark.parametrize("shape, chunks", [((1, 1, 0), (1, 1, 0)), ((2, 0), (1, 0))])
786
+ @pytest.mark.parametrize("reps", [2, (3, 2, 5)])
787
+ def test_tile_empty_array(shape, chunks, reps):
788
+ x = np.empty(shape)
789
+ d = da.from_array(x, chunks=chunks)
790
+
791
+ assert_eq(np.tile(x, reps), da.tile(d, reps))
792
+
793
+
794
+ @pytest.mark.parametrize("shape", [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)])
795
+ @pytest.mark.parametrize("reps", [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)])
796
+ def test_tile_np_kroncompare_examples(shape, reps):
797
+ x = np.random.random(shape)
798
+ d = da.asarray(x)
799
+
800
+ assert_eq(np.tile(x, reps), da.tile(d, reps))
801
+
802
+
803
+ @pytest.mark.parametrize(
804
+ "shape, chunks, pad_width, mode, kwargs",
805
+ [
806
+ ((10, 11), (4, 5), 0, "constant", {"constant_values": 2}),
807
+ ((10, 11), (4, 5), 0, "edge", {}),
808
+ ((10, 11), (4, 5), 0, "linear_ramp", {"end_values": 2}),
809
+ ((10, 11), (4, 5), 0, "reflect", {}),
810
+ ((10, 11), (4, 5), 0, "symmetric", {}),
811
+ ((10, 11), (4, 5), 0, "wrap", {}),
812
+ ((10, 11), (4, 5), 0, "empty", {}),
813
+ ],
814
+ )
815
+ def test_pad_0_width(shape, chunks, pad_width, mode, kwargs):
816
+ np_a = np.random.random(shape)
817
+ da_a = da.from_array(np_a, chunks=chunks)
818
+
819
+ np_r = np.pad(np_a, pad_width, mode, **kwargs)
820
+ da_r = da.pad(da_a, pad_width, mode, **kwargs)
821
+
822
+ assert da_r is da_a
823
+
824
+ assert_eq(np_r, da_r)
825
+
826
+
827
+ @pytest.mark.parametrize(
828
+ "shape, chunks, pad_width, mode, kwargs",
829
+ [
830
+ ((10,), (3,), 1, "constant", {}),
831
+ ((10,), (3,), 2, "constant", {"constant_values": -1}),
832
+ ((10,), (3,), 2, "constant", {"constant_values": np.array(-1)}),
833
+ ((10,), (3,), ((2, 3)), "constant", {"constant_values": (-1, -2)}),
834
+ (
835
+ (10, 11),
836
+ (4, 5),
837
+ ((1, 4), (2, 3)),
838
+ "constant",
839
+ {"constant_values": ((-1, -2), (2, 1))},
840
+ ),
841
+ ((10,), (3,), 3, "edge", {}),
842
+ ((10,), (3,), 3, "linear_ramp", {}),
843
+ ((10,), (3,), 3, "linear_ramp", {"end_values": 0}),
844
+ (
845
+ (10, 11),
846
+ (4, 5),
847
+ ((1, 4), (2, 3)),
848
+ "linear_ramp",
849
+ {"end_values": ((-1, -2), (4, 3))},
850
+ ),
851
+ ((10, 11), (4, 5), ((1, 4), (2, 3)), "reflect", {}),
852
+ ((10, 11), (4, 5), ((1, 4), (2, 3)), "symmetric", {}),
853
+ ((10, 11), (4, 5), ((1, 4), (2, 3)), "wrap", {}),
854
+ ((10,), (3,), ((2, 3)), "maximum", {"stat_length": (1, 2)}),
855
+ ((10, 11), (4, 5), ((1, 4), (2, 3)), "mean", {"stat_length": ((3, 4), (2, 1))}),
856
+ ((10,), (3,), ((2, 3)), "minimum", {"stat_length": (2, 3)}),
857
+ ((10,), (3,), 1, "empty", {}),
858
+ ],
859
+ )
860
+ def test_pad(shape, chunks, pad_width, mode, kwargs):
861
+ np_a = np.random.random(shape)
862
+ da_a = da.from_array(np_a, chunks=chunks)
863
+
864
+ np_r = np.pad(np_a, pad_width, mode, **kwargs)
865
+ da_r = da.pad(da_a, pad_width, mode, **kwargs)
866
+
867
+ if mode == "empty":
868
+ # empty pads lead to undefined values which may be different
869
+ assert_eq(np_r[pad_width:-pad_width], da_r[pad_width:-pad_width])
870
+ else:
871
+ assert_eq(np_r, da_r)
872
+
873
+
874
+ @pytest.mark.parametrize(
875
+ ["np_a", "pad_value"],
876
+ (
877
+ (np.arange(4, dtype="int64"), np.int64(1)),
878
+ (np.arange(4, dtype="float64"), np.float64(0)),
879
+ (
880
+ np.array(
881
+ ["2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04"],
882
+ dtype="datetime64[ns]",
883
+ ),
884
+ np.datetime64("1972-01-01"),
885
+ ),
886
+ (np.array([True, False, True, True], dtype=np.bool_), np.bool_(False)),
887
+ (np.array(["ab", "bc", "de", "ef"], dtype=np.str_), np.str_("00")),
888
+ (np.arange(4, dtype="int64"), np.array(1, dtype="int64")),
889
+ (np.arange(4, dtype="float64"), np.array(0, dtype="float64")),
890
+ (
891
+ np.array(
892
+ ["2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04"],
893
+ dtype="datetime64[ns]",
894
+ ),
895
+ np.array("1972-01-01", dtype="datetime64[ns]"),
896
+ ),
897
+ (
898
+ np.array([True, False, True, True], dtype=np.bool_),
899
+ np.array(False, dtype=np.bool_),
900
+ ),
901
+ (
902
+ np.array(["ab", "bc", "de", "ef"], dtype=np.str_),
903
+ np.array("00", dtype=np.str_),
904
+ ),
905
+ ),
906
+ )
907
+ def test_pad_constant_values(np_a, pad_value):
908
+ pad_width = (1, 1)
909
+ da_a = da.from_array(np_a, chunks=(2,))
910
+
911
+ np_r = np.pad(np_a, pad_width, mode="constant", constant_values=pad_value)
912
+ da_r = da.pad(da_a, pad_width, mode="constant", constant_values=pad_value)
913
+
914
+ assert_eq(np_r, da_r)
915
+
916
+
917
+ @pytest.mark.parametrize("dtype", [np.uint8, np.int16, np.float32, bool])
918
+ @pytest.mark.parametrize("pad_widths", [2, (2,), (2, 3), ((2, 3),), ((3, 1), (0, 0), (2, 0))])
919
+ @pytest.mark.parametrize(
920
+ "mode",
921
+ [
922
+ "constant",
923
+ "edge",
924
+ "linear_ramp",
925
+ "maximum",
926
+ "mean",
927
+ "minimum",
928
+ pytest.param(
929
+ "reflect",
930
+ marks=pytest.mark.skip(
931
+ reason="Bug when pad_width is larger than dimension: https://github.com/dask/dask/issues/5303"
932
+ ),
933
+ ),
934
+ pytest.param(
935
+ "symmetric",
936
+ marks=pytest.mark.skip(
937
+ reason="Bug when pad_width is larger than dimension: https://github.com/dask/dask/issues/5303"
938
+ ),
939
+ ),
940
+ pytest.param(
941
+ "wrap",
942
+ marks=pytest.mark.skip(
943
+ reason="Bug when pad_width is larger than dimension: https://github.com/dask/dask/issues/5303"
944
+ ),
945
+ ),
946
+ pytest.param(
947
+ "median",
948
+ marks=pytest.mark.skip(reason="Not implemented"),
949
+ ),
950
+ pytest.param(
951
+ "empty",
952
+ marks=pytest.mark.skip(reason="Empty leads to undefined values, which may be different"),
953
+ ),
954
+ ],
955
+ )
956
+ def test_pad_3d_data(dtype, pad_widths, mode):
957
+ np_a = np.arange(2 * 3 * 4).reshape(2, 3, 4).astype(dtype)
958
+ da_a = da.from_array(np_a, chunks="auto")
959
+
960
+ np_r = np.pad(np_a, pad_widths, mode=mode)
961
+ da_r = da.pad(da_a, pad_widths, mode=mode)
962
+
963
+ assert_eq(np_r, da_r)
964
+
965
+
966
+ @pytest.mark.parametrize("kwargs", [{}, {"scaler": 2}])
967
+ def test_pad_udf(kwargs):
968
+ def udf_pad(vector, pad_width, iaxis, inner_kwargs):
969
+ assert kwargs == inner_kwargs
970
+ scaler = inner_kwargs.get("scaler", 1)
971
+ vector[: pad_width[0]] = -scaler * pad_width[0]
972
+ vector[-pad_width[1] :] = scaler * pad_width[1]
973
+ return vector
974
+
975
+ shape = (10, 11)
976
+ chunks = (4, 5)
977
+ pad_width = ((1, 2), (2, 3))
978
+
979
+ np_a = np.random.random(shape)
980
+ da_a = da.from_array(np_a, chunks=chunks)
981
+
982
+ np_r = np.pad(np_a, pad_width, udf_pad, **kwargs)
983
+ da_r = da.pad(da_a, pad_width, udf_pad, **kwargs)
984
+
985
+ assert_eq(np_r, da_r)
986
+
987
+
988
+ def test_pad_constant_chunksizes():
989
+ array = da.ones((10, 10), chunks=(1, 1))
990
+ result = da.pad(array, ((0, 16 - 10), (0, 0)), mode="constant", constant_values=0)
991
+ assert tuple(map(max, result.chunks)) == (1, 1)
992
+ assert_eq(
993
+ result,
994
+ np.pad(
995
+ array.compute(),
996
+ mode="constant",
997
+ constant_values=0,
998
+ pad_width=((0, 16 - 10), (0, 0)),
999
+ ),
1000
+ )
1001
+
1002
+
1003
+ def test_auto_chunks():
1004
+ with dask.config.set({"array.chunk-size": "50 MiB"}):
1005
+ x = da.ones((10000, 10000))
1006
+ assert 4 < x.npartitions < 32
1007
+
1008
+
1009
+ def test_string_auto_chunk():
1010
+ with pytest.raises(ValueError):
1011
+ da.full((10000, 10000), "auto_chunk", chunks="auto")
1012
+
1013
+
1014
+ def test_diagonal_zero_chunks():
1015
+ x = da.ones((8, 8), chunks=(4, 4))
1016
+ dd = da.ones((8, 8), chunks=(4, 4))
1017
+ d = da.diagonal(dd)
1018
+
1019
+ expected = np.ones((8,))
1020
+ assert_eq(d, expected)
1021
+ assert_eq(d + d, 2 * expected)
1022
+ A = d + x
1023
+ assert_eq(A, np.full((8, 8), 2.0))
1024
+
1025
+
1026
+ @pytest.mark.parametrize("fn", ["zeros_like", "ones_like"])
1027
+ @pytest.mark.parametrize("shape_chunks", [((50, 4), (10, 2)), ((50,), (10,))])
1028
+ @pytest.mark.parametrize("dtype", ["u4", np.float32, None, np.int64])
1029
+ def test_nan_zeros_ones_like(fn, shape_chunks, dtype):
1030
+ dafn = getattr(da, fn)
1031
+ npfn = getattr(np, fn)
1032
+ shape, chunks = shape_chunks
1033
+ x1 = da.random.standard_normal(size=shape, chunks=chunks)
1034
+ y1 = x1[x1 < 0.5]
1035
+ x2 = x1.compute()
1036
+ y2 = x2[x2 < 0.5]
1037
+ assert_eq(
1038
+ dafn(y1, dtype=dtype),
1039
+ npfn(y2, dtype=dtype),
1040
+ )
1041
+
1042
+
1043
+ def test_from_array_getitem_fused():
1044
+ arr = np.arange(100).reshape(10, 10)
1045
+ darr = da.from_array(arr, chunks=(5, 5))
1046
+ result = darr[slice(1, 5), :][slice(1, 3), :]
1047
+
1048
+ # Always check correctness
1049
+ assert_eq(result, arr[slice(1, 5), :][slice(1, 3), :])
1050
+
1051
+
1052
+ @pytest.mark.parametrize("shape_chunks", [((50, 4), (10, 2)), ((50,), (10,))])
1053
+ @pytest.mark.parametrize("dtype", ["u4", np.float32, None, np.int64])
1054
+ def test_nan_empty_like(shape_chunks, dtype):
1055
+ shape, chunks = shape_chunks
1056
+ x1 = da.random.standard_normal(size=shape, chunks=chunks)
1057
+ y1 = x1[x1 < 0.5]
1058
+ x2 = x1.compute()
1059
+ y2 = x2[x2 < 0.5]
1060
+ a_da = da.empty_like(y1, dtype=dtype).compute()
1061
+ a_np = np.empty_like(y2, dtype=dtype)
1062
+ assert a_da.shape == a_np.shape
1063
+ assert a_da.dtype == a_np.dtype
1064
+
1065
+
1066
+ @pytest.mark.parametrize("val", [0, 0.0, 99, -1])
1067
+ @pytest.mark.parametrize("shape_chunks", [((50, 4), (10, 2)), ((50,), (10,))])
1068
+ @pytest.mark.parametrize("dtype", ["u4", np.float32, None, np.int64])
1069
+ def test_nan_full_like(val, shape_chunks, dtype):
1070
+ if NUMPY_GE_210 and val == -1 and dtype == "u4":
1071
+ pytest.xfail("can't insert negative numbers into unsigned integer")
1072
+ shape, chunks = shape_chunks
1073
+ x1 = da.random.standard_normal(size=shape, chunks=chunks)
1074
+ y1 = x1[x1 < 0.5]
1075
+ x2 = x1.compute()
1076
+ y2 = x2[x2 < 0.5]
1077
+ assert_eq(
1078
+ da.full_like(y1, val, dtype=dtype),
1079
+ np.full_like(y2, val, dtype=dtype),
1080
+ )
1081
+
1082
+
1083
+ @pytest.mark.parametrize(
1084
+ "func",
1085
+ [
1086
+ da.array,
1087
+ da.asarray,
1088
+ da.asanyarray,
1089
+ pytest.param(da.arange, marks=pytest.mark.skip(reason="graph serialization differs")),
1090
+ pytest.param(da.tri, marks=pytest.mark.skip(reason="graph serialization differs")),
1091
+ ],
1092
+ )
1093
+ def test_like_forgets_graph(func):
1094
+ """Test that array creation functions with like=x do not
1095
+ internally store the graph of x
1096
+ """
1097
+ x = da.arange(3).map_blocks(lambda x: x)
1098
+ with pytest.raises(Exception, match="local object"):
1099
+ pickle.dumps(x)
1100
+
1101
+ a = func(1, like=x)
1102
+ pickle.dumps(a)