-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy path__init__.py
More file actions
453 lines (351 loc) · 14.4 KB
/
__init__.py
File metadata and controls
453 lines (351 loc) · 14.4 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from __future__ import annotations
import importlib
import platform
import string
import warnings
from collections.abc import Iterable
from contextlib import contextmanager, nullcontext
from typing import Any, Callable
from unittest import mock # noqa: F401
import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_array_equal # noqa: F401
from packaging.version import Version
from pandas.testing import assert_frame_equal # noqa: F401
import xarray.testing
from xarray import Dataset
from xarray.core import utils
from xarray.core.duck_array_ops import allclose_or_equiv # noqa: F401
from xarray.core.indexing import ExplicitlyIndexed
from xarray.core.options import set_options
from xarray.core.variable import IndexVariable
from xarray.testing import ( # noqa: F401
assert_chunks_equal,
assert_duckarray_allclose,
assert_duckarray_equal,
)
# import mpl and change the backend before other mpl imports
try:
import matplotlib as mpl
# Order of imports is important here.
# Using a different backend makes Travis CI work
mpl.use("Agg")
except ImportError:
pass
# https://github.com/pydata/xarray/issues/7322
warnings.filterwarnings("ignore", "'urllib3.contrib.pyopenssl' module is deprecated")
warnings.filterwarnings("ignore", "Deprecated call to `pkg_resources.declare_namespace")
warnings.filterwarnings("ignore", "pkg_resources is deprecated as an API")
arm_xfail = pytest.mark.xfail(
platform.machine() == "aarch64" or "arm" in platform.machine(),
reason="expected failure on ARM",
)
def assert_writeable(ds):
readonly = [
name
for name, var in ds.variables.items()
if not isinstance(var, IndexVariable) and not var.data.flags.writeable
]
assert not readonly, readonly
def _importorskip(
modname: str, minversion: str | None = None
) -> tuple[bool, pytest.MarkDecorator]:
try:
mod = importlib.import_module(modname)
has = True
if minversion is not None:
v = getattr(mod, "__version__", "999")
if Version(v) < Version(minversion):
raise ImportError("Minimum version not satisfied")
except ImportError:
has = False
reason = f"requires {modname}"
if minversion is not None:
reason += f">={minversion}"
func = pytest.mark.skipif(not has, reason=reason)
return has, func
has_matplotlib, requires_matplotlib = _importorskip("matplotlib")
has_scipy, requires_scipy = _importorskip("scipy")
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="'cgi' is deprecated and slated for removal in Python 3.13",
category=DeprecationWarning,
)
has_pydap, requires_pydap = _importorskip("pydap.client")
has_netCDF4, requires_netCDF4 = _importorskip("netCDF4")
with warnings.catch_warnings():
# see https://github.com/pydata/xarray/issues/8537
warnings.filterwarnings(
"ignore",
message="h5py is running against HDF5 1.14.3",
category=UserWarning,
)
has_h5netcdf, requires_h5netcdf = _importorskip("h5netcdf")
has_pynio, requires_pynio = _importorskip("Nio")
has_cftime, requires_cftime = _importorskip("cftime")
has_dask, requires_dask = _importorskip("dask")
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The current Dask DataFrame implementation is deprecated.",
category=DeprecationWarning,
)
has_dask_expr, requires_dask_expr = _importorskip("dask_expr")
has_bottleneck, requires_bottleneck = _importorskip("bottleneck")
has_rasterio, requires_rasterio = _importorskip("rasterio")
has_zarr, requires_zarr = _importorskip("zarr")
has_fsspec, requires_fsspec = _importorskip("fsspec")
has_iris, requires_iris = _importorskip("iris")
has_numbagg, requires_numbagg = _importorskip("numbagg", "0.4.0")
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="is_categorical_dtype is deprecated and will be removed in a future version.",
category=DeprecationWarning,
)
# seaborn uses the deprecated `pandas.is_categorical_dtype`
has_seaborn, requires_seaborn = _importorskip("seaborn")
has_sparse, requires_sparse = _importorskip("sparse")
has_cupy, requires_cupy = _importorskip("cupy")
has_cartopy, requires_cartopy = _importorskip("cartopy")
has_pint, requires_pint = _importorskip("pint")
has_numexpr, requires_numexpr = _importorskip("numexpr")
has_flox, requires_flox = _importorskip("flox")
has_pandas_ge_2_2, __ = _importorskip("pandas", "2.2")
# some special cases
has_scipy_or_netCDF4 = has_scipy or has_netCDF4
requires_scipy_or_netCDF4 = pytest.mark.skipif(
not has_scipy_or_netCDF4, reason="requires scipy or netCDF4"
)
has_numbagg_or_bottleneck = has_numbagg or has_bottleneck
requires_numbagg_or_bottleneck = pytest.mark.skipif(
not has_scipy_or_netCDF4, reason="requires scipy or netCDF4"
)
# _importorskip does not work for development versions
has_pandas_version_two = Version(pd.__version__).major >= 2
requires_pandas_version_two = pytest.mark.skipif(
not has_pandas_version_two, reason="requires pandas 2.0.0"
)
has_numpy_array_api, requires_numpy_array_api = _importorskip("numpy", "1.26.0")
has_h5netcdf_ros3, requires_h5netcdf_ros3 = _importorskip("h5netcdf", "1.3.0")
has_netCDF4_1_6_2_or_above, requires_netCDF4_1_6_2_or_above = _importorskip(
"netCDF4", "1.6.2"
)
# change some global options for tests
set_options(warn_for_unclosed_files=True)
if has_dask:
import dask
class CountingScheduler:
"""Simple dask scheduler counting the number of computes.
Reference: https://stackoverflow.com/questions/53289286/"""
def __init__(self, max_computes=0):
self.total_computes = 0
self.max_computes = max_computes
def __call__(self, dsk, keys, **kwargs):
self.total_computes += 1
if self.total_computes > self.max_computes:
raise RuntimeError(
"Too many computes. Total: %d > max: %d."
% (self.total_computes, self.max_computes)
)
return dask.get(dsk, keys, **kwargs)
def raise_if_dask_computes(max_computes=0):
# return a dummy context manager so that this can be used for non-dask objects
if not has_dask:
return nullcontext()
scheduler = CountingScheduler(max_computes)
return dask.config.set(scheduler=scheduler)
flaky = pytest.mark.flaky
network = pytest.mark.network
class UnexpectedDataAccess(Exception):
pass
class InaccessibleArray(utils.NDArrayMixin, ExplicitlyIndexed):
"""Disallows any loading."""
def __init__(self, array):
self.array = array
def get_duck_array(self):
raise UnexpectedDataAccess("Tried accessing data")
def __array__(self, dtype: np.typing.DTypeLike = None):
raise UnexpectedDataAccess("Tried accessing data")
def __getitem__(self, key):
raise UnexpectedDataAccess("Tried accessing data.")
HANDLED_ARRAY_FUNCTIONS: dict[str, Callable] = {}
def implements(numpy_function):
"""Register an __array_function__ implementation for ManifestArray objects."""
def decorator(func):
HANDLED_ARRAY_FUNCTIONS[numpy_function] = func
return func
return decorator
@implements(np.concatenate)
def concatenate(
arrays: Iterable[ConcatenatableArray], /, *, axis=0
) -> ConcatenatableArray:
if any(not isinstance(arr, ConcatenatableArray) for arr in arrays):
raise TypeError
result = np.concatenate([arr.array for arr in arrays], axis=axis)
return ConcatenatableArray(result)
@implements(np.stack)
def stack(arrays: Iterable[ConcatenatableArray], /, *, axis=0) -> ConcatenatableArray:
if any(not isinstance(arr, ConcatenatableArray) for arr in arrays):
raise TypeError
result = np.stack([arr.array for arr in arrays], axis=axis)
return ConcatenatableArray(result)
@implements(np.result_type)
def result_type(*arrays_and_dtypes) -> np.dtype:
"""Called by xarray to ensure all arguments to concat have the same dtype."""
first_dtype, *other_dtypes = (np.dtype(obj) for obj in arrays_and_dtypes)
for other_dtype in other_dtypes:
if other_dtype != first_dtype:
raise ValueError("dtypes not all consistent")
return first_dtype
@implements(np.broadcast_to)
def broadcast_to(
x: ConcatenatableArray, /, shape: tuple[int, ...]
) -> ConcatenatableArray:
"""
Broadcasts an array to a specified shape, by either manipulating chunk keys or copying chunk manifest entries.
"""
if not isinstance(x, ConcatenatableArray):
raise TypeError
result = np.broadcast_to(x.array, shape=shape)
return ConcatenatableArray(result)
class ConcatenatableArray(utils.NDArrayMixin):
"""Disallows loading or coercing to an index but does support concatenation / stacking."""
# TODO only reason this is different from InaccessibleArray is to avoid it being a subclass of ExplicitlyIndexed
HANDLED_ARRAY_FUNCTIONS = [concatenate, stack, result_type]
def __init__(self, array):
self.array = array
def get_duck_array(self):
raise UnexpectedDataAccess("Tried accessing data")
def __array__(self, dtype: np.typing.DTypeLike = None):
raise UnexpectedDataAccess("Tried accessing data")
def __getitem__(self, key):
raise UnexpectedDataAccess("Tried accessing data.")
def __array_function__(self, func, types, args, kwargs) -> Any:
if func not in HANDLED_ARRAY_FUNCTIONS:
return NotImplemented
# Note: this allows subclasses that don't override
# __array_function__ to handle ManifestArray objects
if not all(issubclass(t, ConcatenatableArray) for t in types):
return NotImplemented
return HANDLED_ARRAY_FUNCTIONS[func](*args, **kwargs)
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs) -> Any:
"""We have to define this in order to convince xarray that this class is a duckarray, even though we will never support ufuncs."""
return NotImplemented
def astype(self, dtype: np.dtype, /, *, copy: bool = True) -> ConcatenatableArray:
"""Needed because xarray will call this even when it's a no-op"""
if dtype != self.dtype:
raise NotImplementedError()
else:
return self
class FirstElementAccessibleArray(InaccessibleArray):
def __getitem__(self, key):
tuple_idxr = key.tuple
if len(tuple_idxr) > 1:
raise UnexpectedDataAccess("Tried accessing more than one element.")
return self.array[tuple_idxr]
class DuckArrayWrapper(utils.NDArrayMixin):
"""Array-like that prevents casting to array.
Modeled after cupy."""
def __init__(self, array: np.ndarray):
self.array = array
def __getitem__(self, key):
return type(self)(self.array[key])
def __array__(self, dtype: np.typing.DTypeLike = None):
raise UnexpectedDataAccess("Tried accessing data")
def __array_namespace__(self):
"""Present to satisfy is_duck_array test."""
class ReturnItem:
def __getitem__(self, key):
return key
class IndexerMaker:
def __init__(self, indexer_cls):
self._indexer_cls = indexer_cls
def __getitem__(self, key):
if not isinstance(key, tuple):
key = (key,)
return self._indexer_cls(key)
def source_ndarray(array):
"""Given an ndarray, return the base object which holds its memory, or the
object itself.
"""
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "DatetimeIndex.base")
warnings.filterwarnings("ignore", "TimedeltaIndex.base")
base = getattr(array, "base", np.asarray(array).base)
if base is None:
base = array
return base
def format_record(record) -> str:
"""Format warning record like `FutureWarning('Function will be deprecated...')`"""
return f"{str(record.category)[8:-2]}('{record.message}'))"
@contextmanager
def assert_no_warnings():
with warnings.catch_warnings(record=True) as record:
yield record
assert (
len(record) == 0
), f"Got {len(record)} unexpected warning(s): {[format_record(r) for r in record]}"
# Internal versions of xarray's test functions that validate additional
# invariants
def assert_equal(a, b, check_default_indexes=True):
__tracebackhide__ = True
xarray.testing.assert_equal(a, b)
xarray.testing._assert_internal_invariants(a, check_default_indexes)
xarray.testing._assert_internal_invariants(b, check_default_indexes)
def assert_identical(a, b, check_default_indexes=True):
__tracebackhide__ = True
xarray.testing.assert_identical(a, b)
xarray.testing._assert_internal_invariants(a, check_default_indexes)
xarray.testing._assert_internal_invariants(b, check_default_indexes)
def assert_allclose(a, b, check_default_indexes=True, **kwargs):
__tracebackhide__ = True
xarray.testing.assert_allclose(a, b, **kwargs)
xarray.testing._assert_internal_invariants(a, check_default_indexes)
xarray.testing._assert_internal_invariants(b, check_default_indexes)
_DEFAULT_TEST_DIM_SIZES = (8, 9, 10)
def create_test_data(
seed: int | None = None,
add_attrs: bool = True,
dim_sizes: tuple[int, int, int] = _DEFAULT_TEST_DIM_SIZES,
) -> Dataset:
rs = np.random.RandomState(seed)
_vars = {
"var1": ["dim1", "dim2"],
"var2": ["dim1", "dim2"],
"var3": ["dim3", "dim1"],
}
_dims = {"dim1": dim_sizes[0], "dim2": dim_sizes[1], "dim3": dim_sizes[2]}
obj = Dataset()
obj["dim2"] = ("dim2", 0.5 * np.arange(_dims["dim2"]))
if _dims["dim3"] > 26:
raise RuntimeError(
f'Not enough letters for filling this dimension size ({_dims["dim3"]})'
)
obj["dim3"] = ("dim3", list(string.ascii_lowercase[0 : _dims["dim3"]]))
obj["time"] = ("time", pd.date_range("2000-01-01", periods=20))
for v, dims in sorted(_vars.items()):
data = rs.normal(size=tuple(_dims[d] for d in dims))
obj[v] = (dims, data)
if add_attrs:
obj[v].attrs = {"foo": "variable"}
if dim_sizes == _DEFAULT_TEST_DIM_SIZES:
numbers_values = np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], dtype="int64")
else:
numbers_values = np.random.randint(0, 3, _dims["dim3"], dtype="int64")
obj.coords["numbers"] = ("dim3", numbers_values)
obj.encoding = {"foo": "bar"}
assert_writeable(obj)
return obj
_CFTIME_CALENDARS = [
"365_day",
"360_day",
"julian",
"all_leap",
"366_day",
"gregorian",
"proleptic_gregorian",
"standard",
]