(dim_num, dtype, contains_nan, dask)
| 371 | |
| 372 | |
| 373 | def construct_dataarray(dim_num, dtype, contains_nan, dask): |
| 374 | # dimnum <= 3 |
| 375 | rng = np.random.default_rng(0) |
| 376 | shapes = [16, 8, 4][:dim_num] |
| 377 | dims = ("x", "y", "z")[:dim_num] |
| 378 | |
| 379 | if np.issubdtype(dtype, np.floating): |
| 380 | array = rng.random(shapes).astype(dtype) |
| 381 | elif np.issubdtype(dtype, np.integer): |
| 382 | array = rng.integers(0, 10, size=shapes).astype(dtype) |
| 383 | elif np.issubdtype(dtype, np.bool_): |
| 384 | array = rng.integers(0, 1, size=shapes).astype(dtype) |
| 385 | elif dtype is str: |
| 386 | array = rng.choice(["a", "b", "c", "d"], size=shapes) |
| 387 | else: |
| 388 | raise ValueError |
| 389 | |
| 390 | if contains_nan: |
| 391 | inds = rng.choice(range(array.size), int(array.size * 0.2)) |
| 392 | dtype, fill_value = dtypes.maybe_promote(array.dtype) |
| 393 | array = array.astype(dtype) |
| 394 | array.flat[inds] = fill_value |
| 395 | |
| 396 | da = DataArray(array, dims=dims, coords={"x": np.arange(16)}, name="da") |
| 397 | |
| 398 | if dask and has_dask: |
| 399 | chunks = dict.fromkeys(dims, 4) |
| 400 | da = da.chunk(chunks) |
| 401 | |
| 402 | return da |
| 403 | |
| 404 | |
| 405 | def from_series_or_scalar(se): |
no test coverage detected
searching dependent graphs…