/
core.py
1142 lines (955 loc) · 38 KB
/
core.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import copy
import math
import sys
import warnings
from collections.abc import Hashable, Iterable, Mapping, Sequence
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
Literal,
TypeVar,
cast,
overload,
)
import numpy as np
# TODO: get rid of this after migrating this class to array API
from xarray.core import dtypes, formatting, formatting_html
from xarray.core.indexing import (
ExplicitlyIndexed,
ImplicitToExplicitIndexingAdapter,
OuterIndexer,
)
from xarray.namedarray._aggregations import NamedArrayAggregations
from xarray.namedarray._typing import (
ErrorOptionsWithWarn,
_arrayapi,
_arrayfunction_or_api,
_chunkedarray,
_default,
_dtype,
_DType_co,
_ScalarType_co,
_ShapeType_co,
_sparsearrayfunction_or_api,
_SupportsImag,
_SupportsReal,
)
from xarray.namedarray.parallelcompat import guess_chunkmanager
from xarray.namedarray.pycompat import to_numpy
from xarray.namedarray.utils import (
either_dict_or_kwargs,
infix_dims,
is_dict_like,
is_duck_dask_array,
to_0d_object_array,
)
if TYPE_CHECKING:
from numpy.typing import ArrayLike, NDArray
from xarray.core.types import Dims
from xarray.namedarray._typing import (
Default,
_AttrsLike,
_Chunks,
_Dim,
_Dims,
_DimsLike,
_DType,
_IntOrUnknown,
_ScalarType,
_Shape,
_ShapeType,
duckarray,
)
from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint
try:
from dask.typing import (
Graph,
NestedKeys,
PostComputeCallable,
PostPersistCallable,
SchedulerGetCallable,
)
except ImportError:
Graph: Any # type: ignore[no-redef]
NestedKeys: Any # type: ignore[no-redef]
SchedulerGetCallable: Any # type: ignore[no-redef]
PostComputeCallable: Any # type: ignore[no-redef]
PostPersistCallable: Any # type: ignore[no-redef]
if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
T_NamedArray = TypeVar("T_NamedArray", bound="_NamedArray[Any]")
T_NamedArrayInteger = TypeVar(
"T_NamedArrayInteger", bound="_NamedArray[np.integer[Any]]"
)
@overload
def _new(
x: NamedArray[Any, _DType_co],
dims: _DimsLike | Default = ...,
data: duckarray[_ShapeType, _DType] = ...,
attrs: _AttrsLike | Default = ...,
) -> NamedArray[_ShapeType, _DType]: ...
@overload
def _new(
x: NamedArray[_ShapeType_co, _DType_co],
dims: _DimsLike | Default = ...,
data: Default = ...,
attrs: _AttrsLike | Default = ...,
) -> NamedArray[_ShapeType_co, _DType_co]: ...
def _new(
x: NamedArray[Any, _DType_co],
dims: _DimsLike | Default = _default,
data: duckarray[_ShapeType, _DType] | Default = _default,
attrs: _AttrsLike | Default = _default,
) -> NamedArray[_ShapeType, _DType] | NamedArray[Any, _DType_co]:
"""
Create a new array with new typing information.
Parameters
----------
x : NamedArray
Array to create a new array from
dims : Iterable of Hashable, optional
Name(s) of the dimension(s).
Will copy the dims from x by default.
data : duckarray, optional
The actual data that populates the array. Should match the
shape specified by `dims`.
Will copy the data from x by default.
attrs : dict, optional
A dictionary containing any additional information or
attributes you want to store with the array.
Will copy the attrs from x by default.
"""
dims_ = copy.copy(x._dims) if dims is _default else dims
attrs_: Mapping[Any, Any] | None
if attrs is _default:
attrs_ = None if x._attrs is None else x._attrs.copy()
else:
attrs_ = attrs
if data is _default:
return type(x)(dims_, copy.copy(x._data), attrs_)
else:
cls_ = cast("type[NamedArray[_ShapeType, _DType]]", type(x))
return cls_(dims_, data, attrs_)
@overload
def from_array(
dims: _DimsLike,
data: duckarray[_ShapeType, _DType],
attrs: _AttrsLike = ...,
) -> NamedArray[_ShapeType, _DType]: ...
@overload
def from_array(
dims: _DimsLike,
data: ArrayLike,
attrs: _AttrsLike = ...,
) -> NamedArray[Any, Any]: ...
def from_array(
dims: _DimsLike,
data: duckarray[_ShapeType, _DType] | ArrayLike,
attrs: _AttrsLike = None,
) -> NamedArray[_ShapeType, _DType] | NamedArray[Any, Any]:
"""
Create a Named array from an array-like object.
Parameters
----------
dims : str or iterable of str
Name(s) of the dimension(s).
data : T_DuckArray or ArrayLike
The actual data that populates the array. Should match the
shape specified by `dims`.
attrs : dict, optional
A dictionary containing any additional information or
attributes you want to store with the array.
Default is None, meaning no attributes will be stored.
"""
if isinstance(data, NamedArray):
raise TypeError(
"Array is already a Named array. Use 'data.data' to retrieve the data array"
)
# TODO: dask.array.ma.MaskedArray also exists, better way?
if isinstance(data, np.ma.MaskedArray):
mask = np.ma.getmaskarray(data) # type: ignore[no-untyped-call]
if mask.any():
# TODO: requires refactoring/vendoring xarray.core.dtypes and
# xarray.core.duck_array_ops
raise NotImplementedError("MaskedArray is not supported yet")
return NamedArray(dims, data, attrs)
if isinstance(data, _arrayfunction_or_api):
return NamedArray(dims, data, attrs)
if isinstance(data, tuple):
return NamedArray(dims, to_0d_object_array(data), attrs)
# validate whether the data is valid data types.
return NamedArray(dims, np.asarray(data), attrs)
class NamedArray(NamedArrayAggregations, Generic[_ShapeType_co, _DType_co]):
"""
A wrapper around duck arrays with named dimensions
and attributes which describe a single Array.
Numeric operations on this object implement array broadcasting and
dimension alignment based on dimension names,
rather than axis order.
Parameters
----------
dims : str or iterable of hashable
Name(s) of the dimension(s).
data : array-like or duck-array
The actual data that populates the array. Should match the
shape specified by `dims`.
attrs : dict, optional
A dictionary containing any additional information or
attributes you want to store with the array.
Default is None, meaning no attributes will be stored.
Raises
------
ValueError
If the `dims` length does not match the number of data dimensions (ndim).
Examples
--------
>>> data = np.array([1.5, 2, 3], dtype=float)
>>> narr = NamedArray(("x",), data, {"units": "m"}) # TODO: Better name than narr?
"""
__slots__ = ("_data", "_dims", "_attrs")
_data: duckarray[Any, _DType_co]
_dims: _Dims
_attrs: dict[Any, Any] | None
def __init__(
self,
dims: _DimsLike,
data: duckarray[Any, _DType_co],
attrs: _AttrsLike = None,
):
self._data = data
self._dims = self._parse_dimensions(dims)
self._attrs = dict(attrs) if attrs else None
def __init_subclass__(cls, **kwargs: Any) -> None:
if NamedArray in cls.__bases__ and (cls._new == NamedArray._new):
# Type hinting does not work for subclasses unless _new is
# overridden with the correct class.
raise TypeError(
"Subclasses of `NamedArray` must override the `_new` method."
)
super().__init_subclass__(**kwargs)
@overload
def _new(
self,
dims: _DimsLike | Default = ...,
data: duckarray[_ShapeType, _DType] = ...,
attrs: _AttrsLike | Default = ...,
) -> NamedArray[_ShapeType, _DType]: ...
@overload
def _new(
self,
dims: _DimsLike | Default = ...,
data: Default = ...,
attrs: _AttrsLike | Default = ...,
) -> NamedArray[_ShapeType_co, _DType_co]: ...
def _new(
self,
dims: _DimsLike | Default = _default,
data: duckarray[Any, _DType] | Default = _default,
attrs: _AttrsLike | Default = _default,
) -> NamedArray[_ShapeType, _DType] | NamedArray[_ShapeType_co, _DType_co]:
"""
Create a new array with new typing information.
_new has to be reimplemented each time NamedArray is subclassed,
otherwise type hints will not be correct. The same is likely true
for methods that relied on _new.
Parameters
----------
dims : Iterable of Hashable, optional
Name(s) of the dimension(s).
Will copy the dims from x by default.
data : duckarray, optional
The actual data that populates the array. Should match the
shape specified by `dims`.
Will copy the data from x by default.
attrs : dict, optional
A dictionary containing any additional information or
attributes you want to store with the array.
Will copy the attrs from x by default.
"""
return _new(self, dims, data, attrs)
def _replace(
self,
dims: _DimsLike | Default = _default,
data: duckarray[_ShapeType_co, _DType_co] | Default = _default,
attrs: _AttrsLike | Default = _default,
) -> Self:
"""
Create a new array with the same typing information.
The types for each argument cannot change,
use self._new if that is a risk.
Parameters
----------
dims : Iterable of Hashable, optional
Name(s) of the dimension(s).
Will copy the dims from x by default.
data : duckarray, optional
The actual data that populates the array. Should match the
shape specified by `dims`.
Will copy the data from x by default.
attrs : dict, optional
A dictionary containing any additional information or
attributes you want to store with the array.
Will copy the attrs from x by default.
"""
return cast("Self", self._new(dims, data, attrs))
def _copy(
self,
deep: bool = True,
data: duckarray[_ShapeType_co, _DType_co] | None = None,
memo: dict[int, Any] | None = None,
) -> Self:
if data is None:
ndata = self._data
if deep:
ndata = copy.deepcopy(ndata, memo=memo)
else:
ndata = data
self._check_shape(ndata)
attrs = (
copy.deepcopy(self._attrs, memo=memo) if deep else copy.copy(self._attrs)
)
return self._replace(data=ndata, attrs=attrs)
def __copy__(self) -> Self:
return self._copy(deep=False)
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self:
return self._copy(deep=True, memo=memo)
def copy(
self,
deep: bool = True,
data: duckarray[_ShapeType_co, _DType_co] | None = None,
) -> Self:
"""Returns a copy of this object.
If `deep=True`, the data array is loaded into memory and copied onto
the new object. Dimensions, attributes and encodings are always copied.
Use `data` to create a new object with the same structure as
original but entirely new data.
Parameters
----------
deep : bool, default: True
Whether the data array is loaded into memory and copied onto
the new object. Default is True.
data : array_like, optional
Data to use in the new object. Must have same shape as original.
When `data` is used, `deep` is ignored.
Returns
-------
object : NamedArray
New object with dimensions, attributes, and optionally
data copied from original.
"""
return self._copy(deep=deep, data=data)
@property
def ndim(self) -> int:
"""
Number of array dimensions.
See Also
--------
numpy.ndarray.ndim
"""
return len(self.shape)
@property
def size(self) -> _IntOrUnknown:
"""
Number of elements in the array.
Equal to ``np.prod(a.shape)``, i.e., the product of the array’s dimensions.
See Also
--------
numpy.ndarray.size
"""
return math.prod(self.shape)
def __len__(self) -> _IntOrUnknown:
try:
return self.shape[0]
except Exception as exc:
raise TypeError("len() of unsized object") from exc
@property
def dtype(self) -> _DType_co:
"""
Data-type of the array’s elements.
See Also
--------
ndarray.dtype
numpy.dtype
"""
return self._data.dtype
@property
def shape(self) -> _Shape:
"""
Get the shape of the array.
Returns
-------
shape : tuple of ints
Tuple of array dimensions.
See Also
--------
numpy.ndarray.shape
"""
return self._data.shape
@property
def nbytes(self) -> _IntOrUnknown:
"""
Total bytes consumed by the elements of the data array.
If the underlying data array does not include ``nbytes``, estimates
the bytes consumed based on the ``size`` and ``dtype``.
"""
if hasattr(self._data, "nbytes"):
return self._data.nbytes # type: ignore[no-any-return]
else:
return self.size * self.dtype.itemsize
@property
def dims(self) -> _Dims:
"""Tuple of dimension names with which this NamedArray is associated."""
return self._dims
@dims.setter
def dims(self, value: _DimsLike) -> None:
self._dims = self._parse_dimensions(value)
def _parse_dimensions(self, dims: _DimsLike) -> _Dims:
dims = (dims,) if isinstance(dims, str) else tuple(dims)
if len(dims) != self.ndim:
raise ValueError(
f"dimensions {dims} must have the same length as the "
f"number of data dimensions, ndim={self.ndim}"
)
if len(set(dims)) < len(dims):
repeated_dims = {d for d in dims if dims.count(d) > 1}
warnings.warn(
f"Duplicate dimension names present: dimensions {repeated_dims} appear more than once in dims={dims}. "
"We do not yet support duplicate dimension names, but we do allow initial construction of the object. "
"We recommend you rename the dims immediately to become distinct, as most xarray functionality is likely to fail silently if you do not. "
"To rename the dimensions you will need to set the ``.dims`` attribute of each variable, ``e.g. var.dims=('x0', 'x1')``.",
UserWarning,
)
return dims
@property
def attrs(self) -> dict[Any, Any]:
"""Dictionary of local attributes on this NamedArray."""
if self._attrs is None:
self._attrs = {}
return self._attrs
@attrs.setter
def attrs(self, value: Mapping[Any, Any]) -> None:
self._attrs = dict(value) if value else None
def _check_shape(self, new_data: duckarray[Any, _DType_co]) -> None:
if new_data.shape != self.shape:
raise ValueError(
f"replacement data must match the {self.__class__.__name__}'s shape. "
f"replacement data has shape {new_data.shape}; {self.__class__.__name__} has shape {self.shape}"
)
@property
def data(self) -> duckarray[Any, _DType_co]:
"""
The NamedArray's data as an array. The underlying array type
(e.g. dask, sparse, pint) is preserved.
"""
return self._data
@data.setter
def data(self, data: duckarray[Any, _DType_co]) -> None:
self._check_shape(data)
self._data = data
@property
def imag(
self: NamedArray[_ShapeType, np.dtype[_SupportsImag[_ScalarType]]], # type: ignore[type-var]
) -> NamedArray[_ShapeType, _dtype[_ScalarType]]:
"""
The imaginary part of the array.
See Also
--------
numpy.ndarray.imag
"""
if isinstance(self._data, _arrayapi):
from xarray.namedarray._array_api import imag
return imag(self)
return self._new(data=self._data.imag)
@property
def real(
self: NamedArray[_ShapeType, np.dtype[_SupportsReal[_ScalarType]]], # type: ignore[type-var]
) -> NamedArray[_ShapeType, _dtype[_ScalarType]]:
"""
The real part of the array.
See Also
--------
numpy.ndarray.real
"""
if isinstance(self._data, _arrayapi):
from xarray.namedarray._array_api import real
return real(self)
return self._new(data=self._data.real)
def __dask_tokenize__(self) -> object:
# Use v.data, instead of v._data, in order to cope with the wrappers
# around NetCDF and the like
from dask.base import normalize_token
return normalize_token((type(self), self._dims, self.data, self._attrs or None))
def __dask_graph__(self) -> Graph | None:
if is_duck_dask_array(self._data):
return self._data.__dask_graph__()
else:
# TODO: Should this method just raise instead?
# raise NotImplementedError("Method requires self.data to be a dask array")
return None
def __dask_keys__(self) -> NestedKeys:
if is_duck_dask_array(self._data):
return self._data.__dask_keys__()
else:
raise AttributeError("Method requires self.data to be a dask array.")
def __dask_layers__(self) -> Sequence[str]:
if is_duck_dask_array(self._data):
return self._data.__dask_layers__()
else:
raise AttributeError("Method requires self.data to be a dask array.")
@property
def __dask_optimize__(
self,
) -> Callable[..., dict[Any, Any]]:
if is_duck_dask_array(self._data):
return self._data.__dask_optimize__ # type: ignore[no-any-return]
else:
raise AttributeError("Method requires self.data to be a dask array.")
@property
def __dask_scheduler__(self) -> SchedulerGetCallable:
if is_duck_dask_array(self._data):
return self._data.__dask_scheduler__
else:
raise AttributeError("Method requires self.data to be a dask array.")
def __dask_postcompute__(
self,
) -> tuple[PostComputeCallable, tuple[Any, ...]]:
if is_duck_dask_array(self._data):
array_func, array_args = self._data.__dask_postcompute__() # type: ignore[no-untyped-call]
return self._dask_finalize, (array_func,) + array_args
else:
raise AttributeError("Method requires self.data to be a dask array.")
def __dask_postpersist__(
self,
) -> tuple[
Callable[
[Graph, PostPersistCallable[Any], Any, Any],
Self,
],
tuple[Any, ...],
]:
if is_duck_dask_array(self._data):
a: tuple[PostPersistCallable[Any], tuple[Any, ...]]
a = self._data.__dask_postpersist__() # type: ignore[no-untyped-call]
array_func, array_args = a
return self._dask_finalize, (array_func,) + array_args
else:
raise AttributeError("Method requires self.data to be a dask array.")
def _dask_finalize(
self,
results: Graph,
array_func: PostPersistCallable[Any],
*args: Any,
**kwargs: Any,
) -> Self:
data = array_func(results, *args, **kwargs)
return type(self)(self._dims, data, attrs=self._attrs)
@overload
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]: ...
@overload
def get_axis_num(self, dim: Hashable) -> int: ...
def get_axis_num(self, dim: Hashable | Iterable[Hashable]) -> int | tuple[int, ...]:
"""Return axis number(s) corresponding to dimension(s) in this array.
Parameters
----------
dim : str or iterable of str
Dimension name(s) for which to lookup axes.
Returns
-------
int or tuple of int
Axis number or numbers corresponding to the given dimensions.
"""
if not isinstance(dim, str) and isinstance(dim, Iterable):
return tuple(self._get_axis_num(d) for d in dim)
else:
return self._get_axis_num(dim)
def _get_axis_num(self: Any, dim: Hashable) -> int:
_raise_if_any_duplicate_dimensions(self.dims)
try:
return self.dims.index(dim) # type: ignore[no-any-return]
except ValueError:
raise ValueError(f"{dim!r} not found in array dimensions {self.dims!r}")
@property
def chunks(self) -> _Chunks | None:
"""
Tuple of block lengths for this NamedArray's data, in order of dimensions, or None if
the underlying data is not a dask array.
See Also
--------
NamedArray.chunk
NamedArray.chunksizes
xarray.unify_chunks
"""
data = self._data
if isinstance(data, _chunkedarray):
return data.chunks
else:
return None
@property
def chunksizes(
self,
) -> Mapping[_Dim, _Shape]:
"""
Mapping from dimension names to block lengths for this namedArray's data, or None if
the underlying data is not a dask array.
Cannot be modified directly, but can be modified by calling .chunk().
Differs from NamedArray.chunks because it returns a mapping of dimensions to chunk shapes
instead of a tuple of chunk shapes.
See Also
--------
NamedArray.chunk
NamedArray.chunks
xarray.unify_chunks
"""
data = self._data
if isinstance(data, _chunkedarray):
return dict(zip(self.dims, data.chunks))
else:
return {}
@property
def sizes(self) -> dict[_Dim, _IntOrUnknown]:
"""Ordered mapping from dimension names to lengths."""
return dict(zip(self.dims, self.shape))
def chunk(
self,
chunks: int | Literal["auto"] | Mapping[Any, None | int | tuple[int, ...]] = {},
chunked_array_type: str | ChunkManagerEntrypoint[Any] | None = None,
from_array_kwargs: Any = None,
**chunks_kwargs: Any,
) -> Self:
"""Coerce this array's data into a dask array with the given chunks.
If this variable is a non-dask array, it will be converted to dask
array. If it's a dask array, it will be rechunked to the given chunk
sizes.
If neither chunks is not provided for one or more dimensions, chunk
sizes along that dimension will not be updated; non-dask arrays will be
converted into dask arrays with a single block.
Parameters
----------
chunks : int, tuple or dict, optional
Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or
``{'x': 5, 'y': 5}``.
chunked_array_type: str, optional
Which chunked array type to coerce this datasets' arrays to.
Defaults to 'dask' if installed, else whatever is registered via the `ChunkManagerEntrypoint` system.
Experimental API that should not be relied upon.
from_array_kwargs: dict, optional
Additional keyword arguments passed on to the `ChunkManagerEntrypoint.from_array` method used to create
chunked arrays, via whichever chunk manager is specified through the `chunked_array_type` kwarg.
For example, with dask as the default chunked array type, this method would pass additional kwargs
to :py:func:`dask.array.from_array`. Experimental API that should not be relied upon.
**chunks_kwargs : {dim: chunks, ...}, optional
The keyword arguments form of ``chunks``.
One of chunks or chunks_kwargs must be provided.
Returns
-------
chunked : xarray.Variable
See Also
--------
Variable.chunks
Variable.chunksizes
xarray.unify_chunks
dask.array.from_array
"""
if from_array_kwargs is None:
from_array_kwargs = {}
if chunks is None:
warnings.warn(
"None value for 'chunks' is deprecated. "
"It will raise an error in the future. Use instead '{}'",
category=FutureWarning,
)
chunks = {}
if isinstance(chunks, (float, str, int, tuple, list)):
# TODO we shouldn't assume here that other chunkmanagers can handle these types
# TODO should we call normalize_chunks here?
pass # dask.array.from_array can handle these directly
else:
chunks = either_dict_or_kwargs(chunks, chunks_kwargs, "chunk")
if is_dict_like(chunks):
chunks = {self.get_axis_num(dim): chunk for dim, chunk in chunks.items()}
chunkmanager = guess_chunkmanager(chunked_array_type)
data_old = self._data
if chunkmanager.is_chunked_array(data_old):
data_chunked = chunkmanager.rechunk(data_old, chunks) # type: ignore[arg-type]
else:
if not isinstance(data_old, ExplicitlyIndexed):
ndata = data_old
else:
# Unambiguously handle array storage backends (like NetCDF4 and h5py)
# that can't handle general array indexing. For example, in netCDF4 you
# can do "outer" indexing along two dimensions independent, which works
# differently from how NumPy handles it.
# da.from_array works by using lazy indexing with a tuple of slices.
# Using OuterIndexer is a pragmatic choice: dask does not yet handle
# different indexing types in an explicit way:
# https://github.com/dask/dask/issues/2883
ndata = ImplicitToExplicitIndexingAdapter(data_old, OuterIndexer) # type: ignore[assignment]
if is_dict_like(chunks):
chunks = tuple(chunks.get(n, s) for n, s in enumerate(ndata.shape)) # type: ignore[assignment]
data_chunked = chunkmanager.from_array(ndata, chunks, **from_array_kwargs) # type: ignore[arg-type]
return self._replace(data=data_chunked)
def to_numpy(self) -> np.ndarray[Any, Any]:
"""Coerces wrapped data to numpy and returns a numpy.ndarray"""
# TODO an entrypoint so array libraries can choose coercion method?
return to_numpy(self._data)
def as_numpy(self) -> Self:
"""Coerces wrapped data into a numpy array, returning a Variable."""
return self._replace(data=self.to_numpy())
def reduce(
self,
func: Callable[..., Any],
dim: Dims = None,
axis: int | Sequence[int] | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> NamedArray[Any, Any]:
"""Reduce this array by applying `func` along some dimension(s).
Parameters
----------
func : callable
Function which can be called in the form
`func(x, axis=axis, **kwargs)` to return the result of reducing an
np.ndarray over an integer valued axis.
dim : "...", str, Iterable of Hashable or None, optional
Dimension(s) over which to apply `func`. By default `func` is
applied over all dimensions.
axis : int or Sequence of int, optional
Axis(es) over which to apply `func`. Only one of the 'dim'
and 'axis' arguments can be supplied. If neither are supplied, then
the reduction is calculated over the flattened array (by calling
`func(x)` without an axis argument).
keepdims : bool, default: False
If True, the dimensions which are reduced are left in the result
as dimensions of size one
**kwargs : dict
Additional keyword arguments passed on to `func`.
Returns
-------
reduced : Array
Array with summarized data and the indicated dimension(s)
removed.
"""
if dim == ...:
dim = None
if dim is not None and axis is not None:
raise ValueError("cannot supply both 'axis' and 'dim' arguments")
if dim is not None:
axis = self.get_axis_num(dim)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", r"Mean of empty slice", category=RuntimeWarning
)
if axis is not None:
if isinstance(axis, tuple) and len(axis) == 1:
# unpack axis for the benefit of functions
# like np.argmin which can't handle tuple arguments
axis = axis[0]
data = func(self.data, axis=axis, **kwargs)
else:
data = func(self.data, **kwargs)
if getattr(data, "shape", ()) == self.shape:
dims = self.dims
else:
removed_axes: Iterable[int]
if axis is None:
removed_axes = range(self.ndim)
else:
removed_axes = np.atleast_1d(axis) % self.ndim
if keepdims:
# Insert np.newaxis for removed dims
slices = tuple(
np.newaxis if i in removed_axes else slice(None, None)
for i in range(self.ndim)
)
if getattr(data, "shape", None) is None:
# Reduce has produced a scalar value, not an array-like
data = np.asanyarray(data)[slices]
else:
data = data[slices]
dims = self.dims
else:
dims = tuple(
adim for n, adim in enumerate(self.dims) if n not in removed_axes
)
# Return NamedArray to handle IndexVariable when data is nD
return from_array(dims, data, attrs=self._attrs)
def _nonzero(self: T_NamedArrayInteger) -> tuple[T_NamedArrayInteger, ...]:
"""Equivalent numpy's nonzero but returns a tuple of NamedArrays."""
# TODO: we should replace dask's native nonzero
# after https://github.com/dask/dask/issues/1076 is implemented.
# TODO: cast to ndarray and back to T_DuckArray is a workaround
nonzeros = np.nonzero(cast("NDArray[np.integer[Any]]", self.data))
_attrs = self.attrs
return tuple(
cast("T_NamedArrayInteger", self._new((dim,), nz, _attrs))
for nz, dim in zip(nonzeros, self.dims)
)
def __repr__(self) -> str:
return formatting.array_repr(self)
def _repr_html_(self) -> str:
return formatting_html.array_repr(self)
def _as_sparse(
self,
sparse_format: Literal["coo"] | Default = _default,
fill_value: ArrayLike | Default = _default,
) -> NamedArray[Any, _DType_co]:
"""
Use sparse-array as backend.
"""
import sparse
from xarray.namedarray._array_api import astype
# TODO: what to do if dask-backended?
if fill_value is _default:
dtype, fill_value = dtypes.maybe_promote(self.dtype)
else:
dtype = dtypes.result_type(self.dtype, fill_value)
if sparse_format is _default:
sparse_format = "coo"
try:
as_sparse = getattr(sparse, f"as_{sparse_format.lower()}")
except AttributeError as exc:
raise ValueError(f"{sparse_format} is not a valid sparse format") from exc
data = as_sparse(astype(self, dtype).data, fill_value=fill_value)
return self._new(data=data)
def _to_dense(self) -> NamedArray[Any, _DType_co]:
"""
Change backend from sparse to np.array.
"""
if isinstance(self._data, _sparsearrayfunction_or_api):
data_dense: np.ndarray[Any, _DType_co] = self._data.todense()
return self._new(data=data_dense)
else:
raise TypeError("self.data is not a sparse array")
def permute_dims(
self,
*dim: Iterable[_Dim] | ellipsis,
missing_dims: ErrorOptionsWithWarn = "raise",
) -> NamedArray[Any, _DType_co]:
"""Return a new object with transposed dimensions.
Parameters
----------
*dim : Hashable, optional
By default, reverse the order of the dimensions. Otherwise, reorder the
dimensions to this order.
missing_dims : {"raise", "warn", "ignore"}, default: "raise"
What to do if dimensions that should be selected from are not present in the
NamedArray:
- "raise": raise an exception
- "warn": raise a warning, and ignore the missing dimensions
- "ignore": ignore the missing dimensions
Returns
-------
NamedArray
The returned NamedArray has permuted dimensions and data with the
same attributes as the original.