|
1 | 1 | from typing import Sequence, Tuple, Union, Optional, List, Callable
|
2 | 2 | import numpy
|
3 |
| -from numpy import array, dtype, integer, issubdtype, ndarray, prod, array2string |
| 3 | +from numpy import dtype, ndarray, array2string |
4 | 4 | from collections import namedtuple
|
5 | 5 |
|
6 | 6 | from .SparseNdarray import SparseNdarray
|
|
24 | 24 |
|
25 | 25 | from ._subset import _getitem_subset_preserves_dimensions, _getitem_subset_discards_dimensions, _repr_subset
|
26 | 26 | from ._isometric import translate_ufunc_to_op_simple, translate_ufunc_to_op_with_args
|
27 |
| -from ._statistics import array_mean, array_var, array_sum, _create_offset_multipliers |
| 27 | +from ._statistics import array_mean, array_var, array_sum, _create_offset_multipliers, array_any, array_all |
28 | 28 |
|
29 | 29 | __author__ = "ltla"
|
30 | 30 | __copyright__ = "ltla"
|
@@ -255,6 +255,21 @@ def __array_function__(self, func, types, args, kwargs) -> "DelayedArray":
|
255 | 255 | decimals = 0
|
256 | 256 | return DelayedArray(Round(seed, decimals=decimals))
|
257 | 257 |
|
| 258 | + if func == numpy.mean: |
| 259 | + return self.mean(**kwargs) |
| 260 | + |
| 261 | + if func == numpy.sum: |
| 262 | + return self.sum(**kwargs) |
| 263 | + |
| 264 | + if func == numpy.var: |
| 265 | + return self.var(**kwargs) |
| 266 | + |
| 267 | + if func == numpy.any: |
| 268 | + return self.any(**kwargs) |
| 269 | + |
| 270 | + if func == numpy.all: |
| 271 | + return self.all(**kwargs) |
| 272 | + |
258 | 273 | if func == numpy.shape:
|
259 | 274 | return self.shape
|
260 | 275 |
|
@@ -691,6 +706,66 @@ def __abs__(self) -> "DelayedArray":
|
691 | 706 | """
|
692 | 707 | return DelayedArray(UnaryIsometricOpSimple(self._seed, operation="abs"))
|
693 | 708 |
|
| 709 | + def __or__(self, other) -> "DelayedArray": |
| 710 | + """Element-wise OR with something. |
| 711 | +
|
| 712 | + Args: |
| 713 | + other: |
| 714 | + A numeric scalar; |
| 715 | + or a NumPy array with dimensions as described in |
| 716 | + :py:class:`~delayedarray.UnaryIsometricOpWithArgs.UnaryIsometricOpWithArgs`; |
| 717 | + or a ``DelayedArray`` of the same dimensions as :py:attr:`~shape`. |
| 718 | +
|
| 719 | + Returns: |
| 720 | + A ``DelayedArray`` containing the delayed OR operation. |
| 721 | + """ |
| 722 | + return _wrap_isometric_with_args(self, other, operation="logical_or", right=True) |
| 723 | + |
| 724 | + def __ror__(self, other) -> "DelayedArray": |
| 725 | + """Element-wise OR with the right-hand-side of a ``DelayedArray``. |
| 726 | +
|
| 727 | + Args: |
| 728 | + other: |
| 729 | + A numeric scalar; |
| 730 | + or a NumPy array with dimensions as described in |
| 731 | + :py:class:`~delayedarray.UnaryIsometricOpWithArgs.UnaryIsometricOpWithArgs`; |
| 732 | + or a ``DelayedArray`` of the same dimensions as :py:attr:`~shape`. |
| 733 | +
|
| 734 | + Returns: |
| 735 | + A ``DelayedArray`` containing the delayed OR operation. |
| 736 | + """ |
| 737 | + return _wrap_isometric_with_args(self, other, operation="logical_or", right=False) |
| 738 | + |
| 739 | + def __and__(self, other) -> "DelayedArray": |
| 740 | + """Element-wise AND with something. |
| 741 | +
|
| 742 | + Args: |
| 743 | + other: |
| 744 | + A numeric scalar; |
| 745 | + or a NumPy array with dimensions as described in |
| 746 | + :py:class:`~delayedarray.UnaryIsometricOpWithArgs.UnaryIsometricOpWithArgs`; |
| 747 | + or a ``DelayedArray`` of the same dimensions as :py:attr:`~shape`. |
| 748 | +
|
| 749 | + Returns: |
| 750 | + A ``DelayedArray`` containing the delayed AND operation. |
| 751 | + """ |
| 752 | + return _wrap_isometric_with_args(self, other, operation="logical_and", right=True) |
| 753 | + |
| 754 | + def __rand__(self, other) -> "DelayedArray": |
| 755 | + """Element-wise AND with the right-hand-side of a ``DelayedArray``. |
| 756 | +
|
| 757 | + Args: |
| 758 | + other: |
| 759 | + A numeric scalar; |
| 760 | + or a NumPy array with dimensions as described in |
| 761 | + :py:class:`~delayedarray.UnaryIsometricOpWithArgs.UnaryIsometricOpWithArgs`; |
| 762 | + or a ``DelayedArray`` of the same dimensions as :py:attr:`~shape`. |
| 763 | +
|
| 764 | + Returns: |
| 765 | + A ``DelayedArray`` containing the delayed AND operation. |
| 766 | + """ |
| 767 | + return _wrap_isometric_with_args(self, other, operation="logical_and", right=False) |
| 768 | + |
694 | 769 | # Subsetting.
|
695 | 770 | def __getitem__(self, subset: Tuple[Union[slice, Sequence], ...]) -> Union["DelayedArray", ndarray]:
|
696 | 771 | """Take a subset of this ``DelayedArray``. This follows the same logic as NumPy slicing and will generate a
|
@@ -832,6 +907,79 @@ def var(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype: Optiona
|
832 | 907 | masked=is_masked(self),
|
833 | 908 | )
|
834 | 909 |
|
| 910 | + def any(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype: Optional[numpy.dtype] = None, buffer_size: int = 1e8) -> numpy.ndarray: |
| 911 | + """Test whether any array element along a given axis evaluates to True. |
| 912 | +
|
| 913 | + Compute this test across the ``DelayedArray``, possibly over a |
| 914 | + given axis or set of axes. If the seed has a ``any()`` method, that |
| 915 | + method is called directly with the supplied arguments. |
| 916 | +
|
| 917 | + Args: |
| 918 | + axis: |
| 919 | + A single integer specifying the axis over which to test |
| 920 | + for any. Alternatively, a tuple (multiple axes) or None (no |
| 921 | + axes), see :py:func:`~numpy.any` for details. |
| 922 | +
|
| 923 | + dtype: |
| 924 | + NumPy type for the output array. If None, this is automatically |
| 925 | + chosen based on the type of the ``DelayedArray``, see |
| 926 | + :py:func:`~numpy.any` for details. |
| 927 | +
|
| 928 | + buffer_size: |
| 929 | + Buffer size in bytes to use for block processing. Larger values |
| 930 | + generally improve speed at the cost of memory. |
| 931 | +
|
| 932 | + Returns: |
| 933 | + A NumPy array containing the boolean values. If ``axis = None``, this will |
| 934 | + be a NumPy scalar instead. |
| 935 | + """ |
| 936 | + if hasattr(self._seed, "any"): |
| 937 | + return self._seed.any(axis=axis).astype(dtype) |
| 938 | + else: |
| 939 | + return array_any( |
| 940 | + self, |
| 941 | + axis=axis, |
| 942 | + dtype=dtype, |
| 943 | + reduce_over_x=lambda x, axes, op : _reduce(x, axes, op, buffer_size), |
| 944 | + masked=is_masked(self), |
| 945 | + ) |
| 946 | + |
| 947 | + def all(self, axis: Optional[Union[int, Tuple[int, ...]]] = None, dtype: Optional[numpy.dtype] = None, buffer_size: int = 1e8) -> numpy.ndarray: |
| 948 | + """Test whether all array elements along a given axis evaluate to True. |
| 949 | +
|
| 950 | + Compute this test across the ``DelayedArray``, possibly over a |
| 951 | + given axis or set of axes. If the seed has a ``all()`` method, that |
| 952 | + method is called directly with the supplied arguments. |
| 953 | +
|
| 954 | + Args: |
| 955 | + axis: |
| 956 | + A single integer specifying the axis over which to test |
| 957 | + for all. Alternatively, a tuple (multiple axes) or None (no |
| 958 | + axes), see :py:func:`~numpy.all` for details. |
| 959 | +
|
| 960 | + dtype: |
| 961 | + NumPy type for the output array. If None, this is automatically |
| 962 | + chosen based on the type of the ``DelayedArray``, see |
| 963 | + :py:func:`~numpy.all` for details. |
| 964 | +
|
| 965 | + buffer_size: |
| 966 | + Buffer size in bytes to use for block processing. Larger values |
| 967 | + generally improve speed at the cost of memory. |
| 968 | +
|
| 969 | + Returns: |
| 970 | + A NumPy array containing the boolean values. If ``axis = None``, this will |
| 971 | + be a NumPy scalar instead. |
| 972 | + """ |
| 973 | + if hasattr(self._seed, "all"): |
| 974 | + return self._seed.all(axis=axis).astype(dtype) |
| 975 | + else: |
| 976 | + return array_all( |
| 977 | + self, |
| 978 | + axis=axis, |
| 979 | + dtype=dtype, |
| 980 | + reduce_over_x=lambda x, axes, op : _reduce(x, axes, op, buffer_size), |
| 981 | + masked=is_masked(self), |
| 982 | + ) |
835 | 983 |
|
836 | 984 | @extract_dense_array.register
|
837 | 985 | def extract_dense_array_DelayedArray(x: DelayedArray, subset: Tuple[Sequence[int], ...]) -> numpy.ndarray:
|
|
0 commit comments