增加环绕侦察场景适配

This commit is contained in:
2026-01-08 15:44:38 +08:00
parent 3eba1f962b
commit 10c5bb5a8a
5441 changed files with 40219 additions and 379695 deletions

View File

@@ -35,6 +35,7 @@ from numpy import (
amax,
amin,
angle,
array as narray, # noqa: F401
bool_,
expand_dims,
finfo, # noqa: F401
@@ -42,11 +43,9 @@ from numpy import (
iscomplexobj,
ndarray,
)
from numpy import array as narray # noqa: F401
from numpy._core import multiarray as mu
from numpy._core.numeric import normalize_axis_tuple
from numpy._utils import set_module
from numpy._utils._inspect import formatargspec, getargspec
__all__ = [
'MAError', 'MaskError', 'MaskType', 'MaskedArray', 'abs', 'absolute',
@@ -134,18 +133,6 @@ def doc_note(initialdoc, note):
return ''.join(notesplit[:1] + [notedoc] + notesplit[1:])
def get_object_signature(obj):
"""
Get the signature from obj
"""
try:
sig = formatargspec(*getargspec(obj))
except TypeError:
sig = ''
return sig
###############################################################################
# Exceptions #
###############################################################################
@@ -181,7 +168,8 @@ default_filler = {'b': True,
'S': b'N/A',
'u': 999999,
'V': b'???',
'U': 'N/A'
'U': 'N/A',
'T': 'N/A'
}
# Add datetime64 and timedelta64 types
@@ -264,16 +252,17 @@ def default_fill_value(obj):
The default filling value depends on the datatype of the input
array or the type of the input scalar:
======== ========
datatype default
======== ========
bool True
int 999999
float 1.e20
complex 1.e20+0j
object '?'
string 'N/A'
======== ========
=========== ========
datatype default
=========== ========
bool True
int 999999
float 1.e20
complex 1.e20+0j
object '?'
string 'N/A'
StringDType 'N/A'
=========== ========
For structured types, a structured scalar is returned, with each field the
default fill value for its type.
@@ -498,7 +487,7 @@ def _check_fill_value(fill_value, ndtype):
fill_value = np.asarray(fill_value, dtype=object)
fill_value = np.array(_recursive_set_fill_value(fill_value, ndtype),
dtype=ndtype)
elif isinstance(fill_value, str) and (ndtype.char not in 'OSVU'):
elif isinstance(fill_value, str) and (ndtype.char not in 'OSTVU'):
# Note this check doesn't work if fill_value is not a scalar
err_msg = "Cannot set fill value of string with array of dtype %s"
raise TypeError(err_msg % ndtype)
@@ -4818,7 +4807,6 @@ class MaskedArray(ndarray):
fill_value=999999)
"""
kwargs.update(order=kwargs.get('order', 'C'))
result = self._data.reshape(*s, **kwargs).view(type(self))
result._update_from(self)
mask = self._mask
@@ -5454,8 +5442,8 @@ class MaskedArray(ndarray):
The default is to use the mean of the flattened array as reference.
dtype : dtype, optional
Type to use in computing the variance. For arrays of integer type
the default is float32; for arrays of float types it is the same as
the array type.
the default is float32; for arrays of float types it is the same as
the array type.
See Also
--------
@@ -5628,7 +5616,7 @@ class MaskedArray(ndarray):
is used.
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
The sorting algorithm used.
order : list, optional
order : str or list of str, optional
When `a` is an array with fields defined, this argument specifies
which fields to compare first, second, etc. Not all fields need be
specified.
@@ -5812,11 +5800,6 @@ class MaskedArray(ndarray):
stable : bool, optional
Only for compatibility with ``np.sort``. Ignored.
Returns
-------
sorted_array : ndarray
Array of the same type and shape as `a`.
See Also
--------
numpy.ndarray.sort : Method to sort an array in-place.
@@ -6656,14 +6639,14 @@ class mvoid(MaskedArray):
def tolist(self):
"""
Transforms the mvoid object into a tuple.
Transforms the mvoid object into a tuple.
Masked fields are replaced by None.
Masked fields are replaced by None.
Returns
-------
returned_tuple
Tuple of fields
Returns
-------
returned_tuple
Tuple of fields
"""
_mask = self._mask
if _mask is nomask:
@@ -7051,7 +7034,7 @@ ptp.__doc__ = MaskedArray.ptp.__doc__
##############################################################################
class _frommethod:
def _frommethod(methodname: str, reversed: bool = False):
"""
Define functions from existing MaskedArray methods.
@@ -7059,44 +7042,47 @@ class _frommethod:
----------
methodname : str
Name of the method to transform.
reversed : bool, optional
Whether to reverse the first two arguments of the method. Default is False.
"""
method = getattr(MaskedArray, methodname)
assert callable(method)
def __init__(self, methodname, reversed=False):
self.__name__ = methodname
self.__qualname__ = methodname
self.__doc__ = self.getdoc()
self.reversed = reversed
signature = inspect.signature(method)
params = list(signature.parameters.values())
params[0] = params[0].replace(name="a") # rename 'self' to 'a'
def getdoc(self):
"Return the doc of the function (from the doc of the method)."
meth = getattr(MaskedArray, self.__name__, None) or\
getattr(np, self.__name__, None)
signature = self.__name__ + get_object_signature(meth)
if meth is not None:
doc = f""" {signature}
{getattr(meth, '__doc__', None)}"""
return doc
if reversed:
assert len(params) >= 2
params[0], params[1] = params[1], params[0]
def __call__(self, a, *args, **params):
if self.reversed:
args = list(args)
a, args[0] = args[0], a
def wrapper(a, b, *args, **params):
return getattr(asanyarray(b), methodname)(a, *args, **params)
marr = asanyarray(a)
method_name = self.__name__
method = getattr(type(marr), method_name, None)
if method is None:
# use the corresponding np function
method = getattr(np, method_name)
else:
def wrapper(a, *args, **params):
return getattr(asanyarray(a), methodname)(*args, **params)
return method(marr, *args, **params)
wrapper.__signature__ = signature.replace(parameters=params)
wrapper.__name__ = wrapper.__qualname__ = methodname
# __doc__ is None when using `python -OO ...`
if method.__doc__ is not None:
str_signature = f"{methodname}{signature}"
# TODO: For methods with a docstring "Parameters" section, that do not already
# mention `a` (see e.g. `MaskedArray.var.__doc__`), it should be inserted there.
wrapper.__doc__ = f" {str_signature}\n{method.__doc__}"
return wrapper
all = _frommethod('all')
anomalies = anom = _frommethod('anom')
any = _frommethod('any')
argmax = _frommethod('argmax')
argmin = _frommethod('argmin')
compress = _frommethod('compress', reversed=True)
count = _frommethod('count')
cumprod = _frommethod('cumprod')
cumsum = _frommethod('cumsum')
copy = _frommethod('copy')
@@ -7120,7 +7106,6 @@ swapaxes = _frommethod('swapaxes')
trace = _frommethod('trace')
var = _frommethod('var')
count = _frommethod('count')
def take(a, indices, axis=None, out=None, mode='raise'):
"""
@@ -7209,9 +7194,6 @@ def power(a, b, third=None):
return result
argmin = _frommethod('argmin')
argmax = _frommethod('argmax')
def argsort(a, axis=np._NoValue, kind=None, order=None, endwith=True,
fill_value=None, *, stable=None):
"Function version of the eponymous method."
@@ -8620,7 +8602,7 @@ def asarray(a, dtype=None, order=None):
subok=False, order=order)
def asanyarray(a, dtype=None):
def asanyarray(a, dtype=None, order=None):
"""
Convert the input to a masked array, conserving subclasses.
@@ -8633,9 +8615,13 @@ def asanyarray(a, dtype=None):
Input data, in any form that can be converted to an array.
dtype : dtype, optional
By default, the data-type is inferred from the input data.
order : {'C', 'F'}, optional
Whether to use row-major ('C') or column-major ('FORTRAN') memory
representation. Default is 'C'.
order : {'C', 'F', 'A', 'K'}, optional
Memory layout. 'A' and 'K' depend on the order of input array ``a``.
'C' row-major (C-style),
'F' column-major (Fortran-style) memory representation.
'A' (any) means 'F' if ``a`` is Fortran contiguous, 'C' otherwise
'K' (keep) preserve input order
Defaults to 'K'.
Returns
-------
@@ -8665,9 +8651,18 @@ def asanyarray(a, dtype=None):
"""
# workaround for #8666, to preserve identity. Ideally the bottom line
# would handle this for us.
if isinstance(a, MaskedArray) and (dtype is None or dtype == a.dtype):
if (
isinstance(a, MaskedArray)
and (dtype is None or dtype == a.dtype)
and (
order in {None, 'A', 'K'}
or order == 'C' and a.flags.carray
or order == 'F' and a.flags.f_contiguous
)
):
return a
return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True)
return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True,
order=order)
##############################################################################
@@ -8745,79 +8740,77 @@ def fromflex(fxarray):
return masked_array(fxarray['_data'], mask=fxarray['_mask'])
class _convert2ma:
def _convert2ma(funcname: str, np_ret: str, np_ma_ret: str,
params: dict[str, str] | None = None):
"""Convert function from numpy to numpy.ma."""
func = getattr(np, funcname)
params = params or {}
"""
Convert functions from numpy to numpy.ma.
@functools.wraps(func, assigned=set(functools.WRAPPER_ASSIGNMENTS) - {"__module__"})
def wrapper(*args, **kwargs):
common_params = kwargs.keys() & params.keys()
extras = params | {p: kwargs.pop(p) for p in common_params}
Parameters
----------
_methodname : string
Name of the method to transform.
result = func.__call__(*args, **kwargs).view(MaskedArray)
"""
__doc__ = None
def __init__(self, funcname, np_ret, np_ma_ret, params=None):
self._func = getattr(np, funcname)
self.__doc__ = self.getdoc(np_ret, np_ma_ret)
self._extras = params or {}
def getdoc(self, np_ret, np_ma_ret):
"Return the doc of the function (from the doc of the method)."
doc = getattr(self._func, '__doc__', None)
sig = get_object_signature(self._func)
if doc:
doc = self._replace_return_type(doc, np_ret, np_ma_ret)
# Add the signature of the function at the beginning of the doc
if sig:
sig = f"{self._func.__name__}{sig}\n"
doc = sig + doc
return doc
def _replace_return_type(self, doc, np_ret, np_ma_ret):
"""
Replace documentation of ``np`` function's return type.
Replaces it with the proper type for the ``np.ma`` function.
Parameters
----------
doc : str
The documentation of the ``np`` method.
np_ret : str
The return type string of the ``np`` method that we want to
replace. (e.g. "out : ndarray")
np_ma_ret : str
The return type string of the ``np.ma`` method.
(e.g. "out : MaskedArray")
"""
if np_ret not in doc:
raise RuntimeError(
f"Failed to replace `{np_ret}` with `{np_ma_ret}`. "
f"The documentation string for return type, {np_ret}, is not "
f"found in the docstring for `np.{self._func.__name__}`. "
f"Fix the docstring for `np.{self._func.__name__}` or "
"update the expected string for return type."
)
return doc.replace(np_ret, np_ma_ret)
def __call__(self, *args, **params):
# Find the common parameters to the call and the definition
_extras = self._extras
common_params = set(params).intersection(_extras)
# Drop the common parameters from the call
for p in common_params:
_extras[p] = params.pop(p)
# Get the result
result = self._func.__call__(*args, **params).view(MaskedArray)
if "fill_value" in common_params:
result.fill_value = _extras.get("fill_value", None)
result.fill_value = extras["fill_value"]
if "hardmask" in common_params:
result._hardmask = bool(_extras.get("hard_mask", False))
result._hardmask = bool(extras["hardmask"])
return result
# workaround for a doctest bug in Python 3.11 that incorrectly assumes `__code__`
# exists on wrapped functions
del wrapper.__wrapped__
# `arange`, `empty`, `empty_like`, `frombuffer`, and `zeros` have no signature
try:
signature = inspect.signature(func)
except ValueError:
signature = inspect.Signature([
inspect.Parameter('args', inspect.Parameter.VAR_POSITIONAL),
inspect.Parameter('kwargs', inspect.Parameter.VAR_KEYWORD),
])
if params:
sig_params = list(signature.parameters.values())
# pop `**kwargs` if present
sig_kwargs = None
if sig_params[-1].kind is inspect.Parameter.VAR_KEYWORD:
sig_kwargs = sig_params.pop()
# add new keyword-only parameters
for param_name, default in params.items():
new_param = inspect.Parameter(
param_name,
inspect.Parameter.KEYWORD_ONLY,
default=default,
)
sig_params.append(new_param)
# re-append `**kwargs` if it was present
if sig_kwargs:
sig_params.append(sig_kwargs)
signature = signature.replace(parameters=sig_params)
wrapper.__signature__ = signature
# __doc__ is None when using `python -OO ...`
if func.__doc__ is not None:
assert np_ret in func.__doc__, (
f"Failed to replace `{np_ret}` with `{np_ma_ret}`. "
f"The documentation string for return type, {np_ret}, is not "
f"found in the docstring for `np.{func.__name__}`. "
f"Fix the docstring for `np.{func.__name__}` or "
"update the expected string for return type."
)
wrapper.__doc__ = inspect.cleandoc(func.__doc__).replace(np_ret, np_ma_ret)
return wrapper
arange = _convert2ma(
'arange',

View File

@@ -19,12 +19,12 @@ __all__ = [
'setdiff1d', 'setxor1d', 'stack', 'unique', 'union1d', 'vander', 'vstack',
]
import functools
import itertools
import warnings
import numpy as np
from numpy import array as nxarray
from numpy import ndarray
from numpy import array as nxarray, ndarray
from numpy.lib._function_base_impl import _ureduce
from numpy.lib._index_tricks_impl import AxisConcatenator
from numpy.lib.array_utils import normalize_axis_index, normalize_axis_tuple
@@ -245,151 +245,93 @@ def masked_all_like(arr):
#####--------------------------------------------------------------------------
#---- --- Standard functions ---
#####--------------------------------------------------------------------------
class _fromnxfunction:
def _fromnxfunction_function(_fromnxfunction):
"""
Defines a wrapper to adapt NumPy functions to masked arrays.
An instance of `_fromnxfunction` can be called with the same parameters
as the wrapped NumPy function. The docstring of `newfunc` is adapted from
the wrapped function as well, see `getdoc`.
This class should not be used directly. Instead, one of its extensions that
provides support for a specific type of input should be used.
Decorator to wrap a "_fromnxfunction" function, wrapping a numpy function as a
masked array function, with proper docstring and name.
Parameters
----------
funcname : str
The name of the function to be adapted. The function should be
in the NumPy namespace (i.e. ``np.funcname``).
_fromnxfunction : ({params}) -> ndarray, {params}) -> masked_array
Wrapper function that calls the wrapped numpy function
Returns
-------
decorator : (f: ({params}) -> ndarray) -> ({params}) -> masked_array
Function that accepts a numpy function and returns a masked array function
"""
def decorator(npfunc, /):
def wrapper(*args, **kwargs):
return _fromnxfunction(npfunc, *args, **kwargs)
def __init__(self, funcname):
self.__name__ = funcname
self.__qualname__ = funcname
self.__doc__ = self.getdoc()
functools.update_wrapper(wrapper, npfunc, assigned=("__name__", "__qualname__"))
wrapper.__doc__ = ma.doc_note(
npfunc.__doc__,
"The function is applied to both the ``_data`` and the ``_mask``, if any.",
)
return wrapper
def getdoc(self):
"""
Retrieve the docstring and signature from the function.
The ``__doc__`` attribute of the function is used as the docstring for
the new masked array version of the function. A note on application
of the function to the mask is appended.
Parameters
----------
None
"""
npfunc = getattr(np, self.__name__, None)
doc = getattr(npfunc, '__doc__', None)
if doc:
sig = ma.get_object_signature(npfunc)
doc = ma.doc_note(doc, "The function is applied to both the _data "
"and the _mask, if any.")
if sig:
sig = self.__name__ + sig + "\n\n"
return sig + doc
return
def __call__(self, *args, **params):
pass
return decorator
class _fromnxfunction_single(_fromnxfunction):
@_fromnxfunction_function
def _fromnxfunction_single(npfunc, a, /, *args, **kwargs):
"""
A version of `_fromnxfunction` that is called with a single array
argument followed by auxiliary args that are passed verbatim for
both the data and mask calls.
Wraps a NumPy function that can be called with a single array argument followed by
auxiliary args that are passed verbatim for both the data and mask calls.
"""
def __call__(self, x, *args, **params):
func = getattr(np, self.__name__)
if isinstance(x, ndarray):
_d = func(x.__array__(), *args, **params)
_m = func(getmaskarray(x), *args, **params)
return masked_array(_d, mask=_m)
else:
_d = func(np.asarray(x), *args, **params)
_m = func(getmaskarray(x), *args, **params)
return masked_array(_d, mask=_m)
return masked_array(
data=npfunc(np.asarray(a), *args, **kwargs),
mask=npfunc(getmaskarray(a), *args, **kwargs),
)
class _fromnxfunction_seq(_fromnxfunction):
@_fromnxfunction_function
def _fromnxfunction_seq(npfunc, arys, /, *args, **kwargs):
"""
A version of `_fromnxfunction` that is called with a single sequence
of arrays followed by auxiliary args that are passed verbatim for
both the data and mask calls.
Wraps a NumPy function that can be called with a single sequence of arrays followed
by auxiliary args that are passed verbatim for both the data and mask calls.
"""
def __call__(self, x, *args, **params):
func = getattr(np, self.__name__)
_d = func(tuple(np.asarray(a) for a in x), *args, **params)
_m = func(tuple(getmaskarray(a) for a in x), *args, **params)
return masked_array(_d, mask=_m)
return masked_array(
data=npfunc(tuple(np.asarray(a) for a in arys), *args, **kwargs),
mask=npfunc(tuple(getmaskarray(a) for a in arys), *args, **kwargs),
)
class _fromnxfunction_args(_fromnxfunction):
@_fromnxfunction_function
def _fromnxfunction_allargs(npfunc, /, *arys, **kwargs):
"""
A version of `_fromnxfunction` that is called with multiple array
arguments. The first non-array-like input marks the beginning of the
arguments that are passed verbatim for both the data and mask calls.
Array arguments are processed independently and the results are
returned in a list. If only one array is found, the return value is
just the processed array instead of a list.
Wraps a NumPy function that can be called with multiple array arguments.
All args are converted to arrays even if they are not so already.
This makes it possible to process scalars as 1-D arrays.
Only keyword arguments are passed through verbatim for the data and mask calls.
Arrays arguments are processed independently and the results are returned in a list.
If only one arg is present, the return value is just the processed array instead of
a list.
"""
def __call__(self, *args, **params):
func = getattr(np, self.__name__)
arrays = []
args = list(args)
while len(args) > 0 and issequence(args[0]):
arrays.append(args.pop(0))
res = []
for x in arrays:
_d = func(np.asarray(x), *args, **params)
_m = func(getmaskarray(x), *args, **params)
res.append(masked_array(_d, mask=_m))
if len(arrays) == 1:
return res[0]
return res
out = tuple(
masked_array(
data=npfunc(np.asarray(a), **kwargs),
mask=npfunc(getmaskarray(a), **kwargs),
)
for a in arys
)
return out[0] if len(out) == 1 else out
class _fromnxfunction_allargs(_fromnxfunction):
"""
A version of `_fromnxfunction` that is called with multiple array
arguments. Similar to `_fromnxfunction_args` except that all args
are converted to arrays even if they are not so already. This makes
it possible to process scalars as 1-D arrays. Only keyword arguments
are passed through verbatim for the data and mask calls. Arrays
arguments are processed independently and the results are returned
in a list. If only one arg is present, the return value is just the
processed array instead of a list.
"""
def __call__(self, *args, **params):
func = getattr(np, self.__name__)
res = []
for x in args:
_d = func(np.asarray(x), **params)
_m = func(getmaskarray(x), **params)
res.append(masked_array(_d, mask=_m))
if len(args) == 1:
return res[0]
return res
atleast_1d = _fromnxfunction_allargs(np.atleast_1d)
atleast_2d = _fromnxfunction_allargs(np.atleast_2d)
atleast_3d = _fromnxfunction_allargs(np.atleast_3d)
vstack = row_stack = _fromnxfunction_seq(np.vstack)
hstack = _fromnxfunction_seq(np.hstack)
column_stack = _fromnxfunction_seq(np.column_stack)
dstack = _fromnxfunction_seq(np.dstack)
stack = _fromnxfunction_seq(np.stack)
atleast_1d = _fromnxfunction_allargs('atleast_1d')
atleast_2d = _fromnxfunction_allargs('atleast_2d')
atleast_3d = _fromnxfunction_allargs('atleast_3d')
vstack = row_stack = _fromnxfunction_seq('vstack')
hstack = _fromnxfunction_seq('hstack')
column_stack = _fromnxfunction_seq('column_stack')
dstack = _fromnxfunction_seq('dstack')
stack = _fromnxfunction_seq('stack')
hsplit = _fromnxfunction_single('hsplit')
diagflat = _fromnxfunction_single('diagflat')
hsplit = _fromnxfunction_single(np.hsplit)
diagflat = _fromnxfunction_single(np.diagflat)
#####--------------------------------------------------------------------------
@@ -1447,14 +1389,13 @@ def in1d(ar1, ar2, assume_unique=False, invert=False):
Test whether each element of an array is also present in a second
array.
The output is always a masked array. See `numpy.in1d` for more details.
The output is always a masked array.
We recommend using :func:`isin` instead of `in1d` for new code.
See Also
--------
isin : Version of this function that preserves the shape of ar1.
numpy.in1d : Equivalent function for ndarrays.
Examples
--------
@@ -1731,8 +1672,8 @@ def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None):
return result
def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, allow_masked=True,
ddof=np._NoValue):
def corrcoef(x, y=None, rowvar=True, allow_masked=True,
):
"""
Return Pearson product-moment correlation coefficients.
@@ -1753,32 +1694,17 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, allow_masked=True,
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias : _NoValue, optional
Has no effect, do not use.
.. deprecated:: 1.10.0
allow_masked : bool, optional
If True, masked values are propagated pair-wise: if a value is masked
in `x`, the corresponding value is masked in `y`.
If False, raises an exception. Because `bias` is deprecated, this
argument needs to be treated as keyword only to avoid a warning.
ddof : _NoValue, optional
Has no effect, do not use.
.. deprecated:: 1.10.0
See Also
--------
numpy.corrcoef : Equivalent function in top-level NumPy module.
cov : Estimate the covariance matrix.
Notes
-----
This function accepts but discards arguments `bias` and `ddof`. This is
for backwards compatibility with previous versions of this function. These
arguments had no effect on the return values of the function and can be
safely ignored in this and previous versions of numpy.
Examples
--------
>>> import numpy as np
@@ -1793,10 +1719,6 @@ def corrcoef(x, y=None, rowvar=True, bias=np._NoValue, allow_masked=True,
dtype=float64)
"""
msg = 'bias and ddof have no effect and are deprecated'
if bias is not np._NoValue or ddof is not np._NoValue:
# 2015-03-15, 1.10
warnings.warn(msg, DeprecationWarning, stacklevel=2)
# Estimate the covariance matrix.
corr = cov(x, y, rowvar, allow_masked=allow_masked)
# The non-masked version returns a masked value for a scalar.

View File

@@ -1,6 +1,17 @@
from _typeshed import Incomplete
from collections.abc import Sequence
from typing import SupportsIndex, TypeAlias, TypeVar, overload
import numpy as np
from numpy import _CastingKind
from numpy._typing import (
ArrayLike,
DTypeLike,
_AnyShape,
_ArrayLike,
_DTypeLike,
_ShapeLike,
)
from numpy.lib._function_base_impl import average
from numpy.lib._index_tricks_impl import AxisConcatenator
@@ -55,59 +66,207 @@ __all__ = [
"vstack",
]
def count_masked(arr, axis=...): ...
def masked_all(shape, dtype=...): ...
_ScalarT = TypeVar("_ScalarT", bound=np.generic)
_ScalarT1 = TypeVar("_ScalarT1", bound=np.generic)
_ScalarT2 = TypeVar("_ScalarT2", bound=np.generic)
_MArrayT = TypeVar("_MArrayT", bound=MaskedArray)
_MArray: TypeAlias = MaskedArray[_AnyShape, np.dtype[_ScalarT]]
###
# keep in sync with `numpy._core.shape_base.atleast_1d`
@overload
def atleast_1d(a0: _ArrayLike[_ScalarT], /) -> _MArray[_ScalarT]: ...
@overload
def atleast_1d(a0: _ArrayLike[_ScalarT1], a1: _ArrayLike[_ScalarT2], /) -> tuple[_MArray[_ScalarT1], _MArray[_ScalarT2]]: ...
@overload
def atleast_1d(
a0: _ArrayLike[_ScalarT], a1: _ArrayLike[_ScalarT], /, *arys: _ArrayLike[_ScalarT]
) -> tuple[_MArray[_ScalarT], ...]: ...
@overload
def atleast_1d(a0: ArrayLike, /) -> _MArray[Incomplete]: ...
@overload
def atleast_1d(a0: ArrayLike, a1: ArrayLike, /) -> tuple[_MArray[Incomplete], _MArray[Incomplete]]: ...
@overload
def atleast_1d(a0: ArrayLike, a1: ArrayLike, /, *ai: ArrayLike) -> tuple[_MArray[Incomplete], ...]: ...
# keep in sync with `numpy._core.shape_base.atleast_2d`
@overload
def atleast_2d(a0: _ArrayLike[_ScalarT], /) -> _MArray[_ScalarT]: ...
@overload
def atleast_2d(a0: _ArrayLike[_ScalarT1], a1: _ArrayLike[_ScalarT2], /) -> tuple[_MArray[_ScalarT1], _MArray[_ScalarT2]]: ...
@overload
def atleast_2d(
a0: _ArrayLike[_ScalarT], a1: _ArrayLike[_ScalarT], /, *arys: _ArrayLike[_ScalarT]
) -> tuple[_MArray[_ScalarT], ...]: ...
@overload
def atleast_2d(a0: ArrayLike, /) -> _MArray[Incomplete]: ...
@overload
def atleast_2d(a0: ArrayLike, a1: ArrayLike, /) -> tuple[_MArray[Incomplete], _MArray[Incomplete]]: ...
@overload
def atleast_2d(a0: ArrayLike, a1: ArrayLike, /, *ai: ArrayLike) -> tuple[_MArray[Incomplete], ...]: ...
# keep in sync with `numpy._core.shape_base.atleast_2d`
@overload
def atleast_3d(a0: _ArrayLike[_ScalarT], /) -> _MArray[_ScalarT]: ...
@overload
def atleast_3d(a0: _ArrayLike[_ScalarT1], a1: _ArrayLike[_ScalarT2], /) -> tuple[_MArray[_ScalarT1], _MArray[_ScalarT2]]: ...
@overload
def atleast_3d(
a0: _ArrayLike[_ScalarT], a1: _ArrayLike[_ScalarT], /, *arys: _ArrayLike[_ScalarT]
) -> tuple[_MArray[_ScalarT], ...]: ...
@overload
def atleast_3d(a0: ArrayLike, /) -> _MArray[Incomplete]: ...
@overload
def atleast_3d(a0: ArrayLike, a1: ArrayLike, /) -> tuple[_MArray[Incomplete], _MArray[Incomplete]]: ...
@overload
def atleast_3d(a0: ArrayLike, a1: ArrayLike, /, *ai: ArrayLike) -> tuple[_MArray[Incomplete], ...]: ...
# keep in sync with `numpy._core.shape_base.vstack`
@overload
def vstack(
tup: Sequence[_ArrayLike[_ScalarT]],
*,
dtype: None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def vstack(
tup: Sequence[ArrayLike],
*,
dtype: _DTypeLike[_ScalarT],
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def vstack(
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike | None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[Incomplete]: ...
row_stack = vstack
# keep in sync with `numpy._core.shape_base.hstack`
@overload
def hstack(
tup: Sequence[_ArrayLike[_ScalarT]],
*,
dtype: None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def hstack(
tup: Sequence[ArrayLike],
*,
dtype: _DTypeLike[_ScalarT],
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def hstack(
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike | None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[Incomplete]: ...
# keep in sync with `numpy._core.shape_base_impl.column_stack`
@overload
def column_stack(tup: Sequence[_ArrayLike[_ScalarT]]) -> _MArray[_ScalarT]: ...
@overload
def column_stack(tup: Sequence[ArrayLike]) -> _MArray[Incomplete]: ...
# keep in sync with `numpy._core.shape_base_impl.dstack`
@overload
def dstack(tup: Sequence[_ArrayLike[_ScalarT]]) -> _MArray[_ScalarT]: ...
@overload
def dstack(tup: Sequence[ArrayLike]) -> _MArray[Incomplete]: ...
# keep in sync with `numpy._core.shape_base.stack`
@overload
def stack(
arrays: Sequence[_ArrayLike[_ScalarT]],
axis: SupportsIndex = 0,
out: None = None,
*,
dtype: None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = 0,
out: None = None,
*,
dtype: _DTypeLike[_ScalarT],
casting: _CastingKind = "same_kind"
) -> _MArray[_ScalarT]: ...
@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = 0,
out: None = None,
*,
dtype: DTypeLike | None = None,
casting: _CastingKind = "same_kind"
) -> _MArray[Incomplete]: ...
@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex,
out: _MArrayT,
*,
dtype: DTypeLike | None = None,
casting: _CastingKind = "same_kind",
) -> _MArrayT: ...
@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = 0,
*,
out: _MArrayT,
dtype: DTypeLike | None = None,
casting: _CastingKind = "same_kind",
) -> _MArrayT: ...
# keep in sync with `numpy._core.shape_base_impl.hsplit`
@overload
def hsplit(ary: _ArrayLike[_ScalarT], indices_or_sections: _ShapeLike) -> list[_MArray[_ScalarT]]: ...
@overload
def hsplit(ary: ArrayLike, indices_or_sections: _ShapeLike) -> list[_MArray[Incomplete]]: ...
# keep in sync with `numpy._core.twodim_base_impl.hsplit`
@overload
def diagflat(v: _ArrayLike[_ScalarT], k: int = 0) -> _MArray[_ScalarT]: ...
@overload
def diagflat(v: ArrayLike, k: int = 0) -> _MArray[Incomplete]: ...
# TODO: everything below
def count_masked(arr, axis=None): ...
def masked_all(shape, dtype=float): ... # noqa: PYI014
def masked_all_like(arr): ...
class _fromnxfunction:
__name__: Incomplete
__doc__: Incomplete
def __init__(self, funcname) -> None: ...
def getdoc(self): ...
def __call__(self, *args, **params): ...
class _fromnxfunction_single(_fromnxfunction):
def __call__(self, x, *args, **params): ...
class _fromnxfunction_seq(_fromnxfunction):
def __call__(self, x, *args, **params): ...
class _fromnxfunction_allargs(_fromnxfunction):
def __call__(self, *args, **params): ...
atleast_1d: _fromnxfunction_allargs
atleast_2d: _fromnxfunction_allargs
atleast_3d: _fromnxfunction_allargs
vstack: _fromnxfunction_seq
row_stack: _fromnxfunction_seq
hstack: _fromnxfunction_seq
column_stack: _fromnxfunction_seq
dstack: _fromnxfunction_seq
stack: _fromnxfunction_seq
hsplit: _fromnxfunction_single
diagflat: _fromnxfunction_single
def apply_along_axis(func1d, axis, arr, *args, **kwargs): ...
def apply_over_axes(func, a, axes): ...
def median(a, axis=..., out=..., overwrite_input=..., keepdims=...): ...
def compress_nd(x, axis=...): ...
def compress_rowcols(x, axis=...): ...
def median(a, axis=None, out=None, overwrite_input=False, keepdims=False): ...
def compress_nd(x, axis=None): ...
def compress_rowcols(x, axis=None): ...
def compress_rows(a): ...
def compress_cols(a): ...
def mask_rows(a, axis=...): ...
def mask_cols(a, axis=...): ...
def ediff1d(arr, to_end=..., to_begin=...): ...
def unique(ar1, return_index=..., return_inverse=...): ...
def intersect1d(ar1, ar2, assume_unique=...): ...
def setxor1d(ar1, ar2, assume_unique=...): ...
def in1d(ar1, ar2, assume_unique=..., invert=...): ...
def isin(element, test_elements, assume_unique=..., invert=...): ...
def ediff1d(arr, to_end=None, to_begin=None): ...
def unique(ar1, return_index=False, return_inverse=False): ...
def intersect1d(ar1, ar2, assume_unique=False): ...
def setxor1d(ar1, ar2, assume_unique=False): ...
def in1d(ar1, ar2, assume_unique=False, invert=False): ...
def isin(element, test_elements, assume_unique=False, invert=False): ...
def union1d(ar1, ar2): ...
def setdiff1d(ar1, ar2, assume_unique=...): ...
def cov(x, y=..., rowvar=..., bias=..., allow_masked=..., ddof=...): ...
def corrcoef(x, y=..., rowvar=..., bias=..., allow_masked=..., ddof=...): ...
def setdiff1d(ar1, ar2, assume_unique=False): ...
def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None): ...
def corrcoef(x, y=None, rowvar=True, allow_masked=True): ...
class MAxisConcatenator(AxisConcatenator):
__slots__ = ()
@@ -124,15 +283,15 @@ class mr_class(MAxisConcatenator):
mr_: mr_class
def ndenumerate(a, compressed=...): ...
def ndenumerate(a, compressed=True): ...
def flatnotmasked_edges(a): ...
def notmasked_edges(a, axis=...): ...
def notmasked_edges(a, axis=None): ...
def flatnotmasked_contiguous(a): ...
def notmasked_contiguous(a, axis=...): ...
def notmasked_contiguous(a, axis=None): ...
def clump_unmasked(a): ...
def clump_masked(a): ...
def vander(x, n=...): ...
def polyfit(x, y, deg, rcond=..., full=..., w=..., cov=...): ...
def vander(x, n=None): ...
def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False): ...
#
def mask_rowcols(a: Incomplete, axis: Incomplete | None = None) -> MaskedArray[Incomplete, np.dtype[Incomplete]]: ...

View File

@@ -657,8 +657,7 @@ def openfile(fname):
def fromtextfile(fname, delimiter=None, commentchar='#', missingchar='',
varnames=None, vartypes=None,
*, delimitor=np._NoValue): # backwards compatibility
varnames=None, vartypes=None):
"""
Creates a mrecarray from data stored in the file `filename`.
@@ -682,16 +681,6 @@ def fromtextfile(fname, delimiter=None, commentchar='#', missingchar='',
Ultra simple: the varnames are in the header, one line"""
if delimitor is not np._NoValue:
if delimiter is not None:
raise TypeError("fromtextfile() got multiple values for argument "
"'delimiter'")
# NumPy 1.22.0, 2021-09-23
warnings.warn("The 'delimitor' keyword argument of "
"numpy.ma.mrecords.fromtextfile() is deprecated "
"since NumPy 1.22.0, use 'delimiter' instead.",
DeprecationWarning, stacklevel=2)
delimiter = delimitor
# Try to open the file.
ftext = openfile(fname)

View File

@@ -1,8 +1,10 @@
from typing import Any, TypeVar
from typing import Any, Generic
from typing_extensions import TypeVar
from numpy import dtype
import numpy as np
from numpy._typing import _AnyShape
from . import MaskedArray
from .core import MaskedArray
__all__ = [
"MaskedRecords",
@@ -13,10 +15,10 @@ __all__ = [
"addfield",
]
_ShapeT_co = TypeVar("_ShapeT_co", covariant=True, bound=tuple[int, ...])
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, covariant=True)
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], default=_AnyShape, covariant=True)
_DTypeT_co = TypeVar("_DTypeT_co", bound=np.dtype, default=np.dtype, covariant=True)
class MaskedRecords(MaskedArray[_ShapeT_co, _DTypeT_co]):
class MaskedRecords(MaskedArray[_ShapeT_co, _DTypeT_co], Generic[_ShapeT_co, _DTypeT_co]):
def __new__(
cls,
shape,
@@ -48,49 +50,47 @@ class MaskedRecords(MaskedArray[_ShapeT_co, _DTypeT_co]):
def __setattr__(self, attr, val): ...
def __getitem__(self, indx): ...
def __setitem__(self, indx, value): ...
def view(self, dtype=..., type=...): ...
def view(self, dtype=None, type=None): ...
def harden_mask(self): ...
def soften_mask(self): ...
def copy(self): ...
def tolist(self, fill_value=...): ...
def tolist(self, fill_value=None): ...
def __reduce__(self): ...
mrecarray = MaskedRecords
def fromarrays(
arraylist,
dtype=...,
shape=...,
formats=...,
names=...,
titles=...,
aligned=...,
byteorder=...,
fill_value=...,
dtype=None,
shape=None,
formats=None,
names=None,
titles=None,
aligned=False,
byteorder=None,
fill_value=None,
): ...
def fromrecords(
reclist,
dtype=...,
shape=...,
formats=...,
names=...,
titles=...,
aligned=...,
byteorder=...,
fill_value=...,
dtype=None,
shape=None,
formats=None,
names=None,
titles=None,
aligned=False,
byteorder=None,
fill_value=None,
mask=...,
): ...
def fromtextfile(
fname,
delimiter=...,
commentchar=...,
missingchar=...,
varnames=...,
vartypes=...,
# NOTE: deprecated: NumPy 1.22.0, 2021-09-23
# delimitor=...,
delimiter=None,
commentchar="#",
missingchar="",
varnames=None,
vartypes=None,
): ...
def addfield(mrecord, newfield, newfieldname=...): ...
def addfield(mrecord, newfield, newfieldname=None): ...

View File

@@ -1,15 +1,11 @@
"""Test deprecation and future warnings.
"""
import io
import textwrap
import pytest
import numpy as np
from numpy.ma.core import MaskedArrayFutureWarning
from numpy.ma.testutils import assert_equal
from numpy.testing import assert_warns
class TestArgsort:
@@ -23,7 +19,7 @@ class TestArgsort:
# argsort has a bad default for >1d arrays
arr_2d = np.array([[1, 2], [3, 4]]).view(cls)
result = assert_warns(
result = pytest.warns(
np.ma.core.MaskedArrayFutureWarning, argsort, arr_2d)
assert_equal(result, argsort(arr_2d, axis=None))
@@ -53,10 +49,10 @@ class TestMinimumMaximum:
ma_max = np.ma.maximum.reduce
# check that the default axis is still None, but warns on 2d arrays
result = assert_warns(MaskedArrayFutureWarning, ma_max, data2d)
result = pytest.warns(MaskedArrayFutureWarning, ma_max, data2d)
assert_equal(result, ma_max(data2d, axis=None))
result = assert_warns(MaskedArrayFutureWarning, ma_min, data2d)
result = pytest.warns(MaskedArrayFutureWarning, ma_min, data2d)
assert_equal(result, ma_min(data2d, axis=None))
# no warnings on 1d, as both new and old defaults are equivalent
@@ -67,21 +63,3 @@ class TestMinimumMaximum:
result = ma_max(data1d)
assert_equal(result, ma_max(data1d, axis=None))
assert_equal(result, ma_max(data1d, axis=0))
class TestFromtextfile:
def test_fromtextfile_delimitor(self):
# NumPy 1.22.0, 2021-09-23
textfile = io.StringIO(textwrap.dedent(
"""
A,B,C,D
'string 1';1;1.0;'mixed column'
'string 2';2;2.0;
'string 3';3;3.0;123
'string 4';4;4.0;3.14
"""
))
with pytest.warns(DeprecationWarning):
result = np.ma.mrecords.fromtextfile(textfile, delimitor=';')

View File

@@ -5,8 +5,8 @@ Adapted from the original test_ma by Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
"""
import inspect
import itertools
import warnings
import pytest
@@ -68,7 +68,6 @@ from numpy.ma.testutils import (
assert_array_equal,
assert_equal,
)
from numpy.testing import assert_warns, suppress_warnings
class TestGeneric:
@@ -746,7 +745,7 @@ class TestCompressFunctions:
x = array(np.arange(9).reshape(3, 3),
mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
with assert_warns(DeprecationWarning):
with pytest.warns(DeprecationWarning):
res = func(x, axis=axis)
assert_equal(res, mask_rowcols(x, rowcols_axis))
@@ -1078,7 +1077,7 @@ class TestMedian:
x = np.ma.arange(24).reshape(3, 4, 2)
x[x % 3 == 0] = masked
assert_equal(median(x, 0), [[12, 9], [6, 15], [12, 9], [18, 15]])
x.shape = (4, 3, 2)
x = x.reshape((4, 3, 2))
assert_equal(median(x, 0), [[99, 10], [11, 99], [13, 14]])
x = np.ma.arange(24).reshape(4, 3, 2)
x[x % 5 == 0] = masked
@@ -1287,19 +1286,14 @@ class TestMedian:
def test_empty(self):
# empty arrays
a = np.ma.masked_array(np.array([], dtype=float))
with suppress_warnings() as w:
w.record(RuntimeWarning)
with pytest.warns(RuntimeWarning):
assert_array_equal(np.ma.median(a), np.nan)
assert_(w.log[0].category is RuntimeWarning)
# multiple dimensions
a = np.ma.masked_array(np.array([], dtype=float, ndmin=3))
# no axis
with suppress_warnings() as w:
w.record(RuntimeWarning)
warnings.filterwarnings('always', '', RuntimeWarning)
with pytest.warns(RuntimeWarning):
assert_array_equal(np.ma.median(a), np.nan)
assert_(w.log[0].category is RuntimeWarning)
# axis 0 and 1
b = np.ma.masked_array(np.array([], dtype=float, ndmin=2))
@@ -1308,10 +1302,8 @@ class TestMedian:
# axis 2
b = np.ma.masked_array(np.array(np.nan, dtype=float, ndmin=2))
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', RuntimeWarning)
with pytest.warns(RuntimeWarning):
assert_equal(np.ma.median(a, axis=2), b)
assert_(w[0].category is RuntimeWarning)
def test_object(self):
o = np.ma.masked_array(np.arange(7.))
@@ -1322,11 +1314,11 @@ class TestMedian:
class TestCov:
def setup_method(self):
self.data = array(np.random.rand(12))
def _create_data(self):
return array(np.random.rand(12))
def test_covhelper(self):
x = self.data
x = self._create_data()
# Test not mask output type is a float.
assert_(_covhelper(x, rowvar=True)[1].dtype, np.float32)
assert_(_covhelper(x, y=x, rowvar=False)[1].dtype, np.float32)
@@ -1347,7 +1339,7 @@ class TestCov:
def test_1d_without_missing(self):
# Test cov on 1D variable w/o missing values
x = self.data
x = self._create_data()
assert_almost_equal(np.cov(x), cov(x))
assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
assert_almost_equal(np.cov(x, rowvar=False, bias=True),
@@ -1355,7 +1347,7 @@ class TestCov:
def test_2d_without_missing(self):
# Test cov on 1 2D variable w/o missing values
x = self.data.reshape(3, 4)
x = self._create_data().reshape(3, 4)
assert_almost_equal(np.cov(x), cov(x))
assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
assert_almost_equal(np.cov(x, rowvar=False, bias=True),
@@ -1363,7 +1355,7 @@ class TestCov:
def test_1d_with_missing(self):
# Test cov 1 1D variable w/missing values
x = self.data
x = self._create_data()
x[-1] = masked
x -= x.mean()
nx = x.compressed()
@@ -1387,7 +1379,7 @@ class TestCov:
def test_2d_with_missing(self):
# Test cov on 2D variable w/ missing value
x = self.data
x = self._create_data()
x[-1] = masked
x = x.reshape(3, 4)
valid = np.logical_not(getmaskarray(x)).astype(int)
@@ -1409,74 +1401,33 @@ class TestCov:
class TestCorrcoef:
def setup_method(self):
self.data = array(np.random.rand(12))
self.data2 = array(np.random.rand(12))
def test_ddof(self):
# ddof raises DeprecationWarning
x, y = self.data, self.data2
expected = np.corrcoef(x)
expected2 = np.corrcoef(x, y)
with suppress_warnings() as sup:
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, x, ddof=-1)
sup.filter(DeprecationWarning, "bias and ddof have no effect")
# ddof has no or negligible effect on the function
assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0))
assert_almost_equal(corrcoef(x, ddof=-1), expected)
assert_almost_equal(corrcoef(x, y, ddof=-1), expected2)
assert_almost_equal(corrcoef(x, ddof=3), expected)
assert_almost_equal(corrcoef(x, y, ddof=3), expected2)
def test_bias(self):
x, y = self.data, self.data2
expected = np.corrcoef(x)
# bias raises DeprecationWarning
with suppress_warnings() as sup:
warnings.simplefilter("always")
assert_warns(DeprecationWarning, corrcoef, x, y, True, False)
assert_warns(DeprecationWarning, corrcoef, x, y, True, True)
assert_warns(DeprecationWarning, corrcoef, x, bias=False)
sup.filter(DeprecationWarning, "bias and ddof have no effect")
# bias has no or negligible effect on the function
assert_almost_equal(corrcoef(x, bias=1), expected)
def _create_data(self):
data = array(np.random.rand(12))
data2 = array(np.random.rand(12))
return data, data2
def test_1d_without_missing(self):
# Test cov on 1D variable w/o missing values
x = self.data
x = self._create_data()[0]
assert_almost_equal(np.corrcoef(x), corrcoef(x))
assert_almost_equal(np.corrcoef(x, rowvar=False),
corrcoef(x, rowvar=False))
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
def test_2d_without_missing(self):
# Test corrcoef on 1 2D variable w/o missing values
x = self.data.reshape(3, 4)
x = self._create_data()[0].reshape(3, 4)
assert_almost_equal(np.corrcoef(x), corrcoef(x))
assert_almost_equal(np.corrcoef(x, rowvar=False),
corrcoef(x, rowvar=False))
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
def test_1d_with_missing(self):
# Test corrcoef 1 1D variable w/missing values
x = self.data
x = self._create_data()[0]
x[-1] = masked
x -= x.mean()
nx = x.compressed()
assert_almost_equal(np.corrcoef(nx), corrcoef(x))
assert_almost_equal(np.corrcoef(nx, rowvar=False),
corrcoef(x, rowvar=False))
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True),
corrcoef(x, rowvar=False, bias=True))
try:
corrcoef(x, allow_masked=False)
except ValueError:
@@ -1486,36 +1437,20 @@ class TestCorrcoef:
assert_almost_equal(np.corrcoef(nx, nx[::-1]), corrcoef(x, x[::-1]))
assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False),
corrcoef(x, x[::-1], rowvar=False))
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
# ddof and bias have no or negligible effect on the function
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
corrcoef(x, x[::-1], bias=1))
assert_almost_equal(np.corrcoef(nx, nx[::-1]),
corrcoef(x, x[::-1], ddof=2))
def test_2d_with_missing(self):
# Test corrcoef on 2D variable w/ missing value
x = self.data
x = self._create_data()[0]
x[-1] = masked
x = x.reshape(3, 4)
test = corrcoef(x)
control = np.corrcoef(x)
assert_almost_equal(test[:-1, :-1], control[:-1, :-1])
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
# ddof and bias have no or negligible effect on the function
assert_almost_equal(corrcoef(x, ddof=-2)[:-1, :-1],
control[:-1, :-1])
assert_almost_equal(corrcoef(x, ddof=3)[:-1, :-1],
control[:-1, :-1])
assert_almost_equal(corrcoef(x, bias=1)[:-1, :-1],
control[:-1, :-1])
class TestPolynomial:
#
def test_polyfit(self):
# Tests polyfit
# On ndarrays
@@ -1877,6 +1812,18 @@ class TestShapeBase:
assert_equal(b.shape, (1, 1))
assert_equal(b.mask.shape, b.data.shape)
@pytest.mark.parametrize("fn", [atleast_1d, vstack, diagflat])
def test_inspect_signature(self, fn):
name = fn.__name__
assert getattr(np.ma, name) is fn
assert fn.__module__ == "numpy.ma.extras"
wrapped = getattr(np, fn.__name__)
sig_wrapped = inspect.signature(wrapped)
sig = inspect.signature(fn)
assert sig == sig_wrapped
class TestNDEnumerate:

View File

@@ -8,9 +8,11 @@ import pickle
import numpy as np
import numpy.ma as ma
from numpy._core.records import fromarrays as recfromarrays
from numpy._core.records import fromrecords as recfromrecords
from numpy._core.records import recarray
from numpy._core.records import (
fromarrays as recfromarrays,
fromrecords as recfromrecords,
recarray,
)
from numpy.ma import masked, nomask
from numpy.ma.mrecords import (
MaskedRecords,
@@ -20,11 +22,7 @@ from numpy.ma.mrecords import (
fromtextfile,
mrecarray,
)
from numpy.ma.testutils import (
assert_,
assert_equal,
assert_equal_records,
)
from numpy.ma.testutils import assert_, assert_equal, assert_equal_records
from numpy.testing import temppath
@@ -352,24 +350,24 @@ class TestMRecords:
class TestView:
def setup_method(self):
(a, b) = (np.arange(10), np.random.rand(10))
def _create_data(self):
a, b = (np.arange(10), np.random.rand(10))
ndtype = [('a', float), ('b', float)]
arr = np.array(list(zip(a, b)), dtype=ndtype)
mrec = fromarrays([a, b], dtype=ndtype, fill_value=(-9., -99.))
mrec.mask[3] = (False, True)
self.data = (mrec, a, b, arr)
return mrec, a, b, arr
def test_view_by_itself(self):
(mrec, a, b, arr) = self.data
mrec = self._create_data()[0]
test = mrec.view()
assert_(isinstance(test, MaskedRecords))
assert_equal_records(test, mrec)
assert_equal_records(test._mask, mrec._mask)
def test_view_simple_dtype(self):
(mrec, a, b, arr) = self.data
mrec, a, b, _ = self._create_data()
ntype = (float, 2)
test = mrec.view(ntype)
assert_(isinstance(test, ma.MaskedArray))
@@ -377,7 +375,7 @@ class TestView:
assert_(test[3, 1] is ma.masked)
def test_view_flexible_type(self):
(mrec, a, b, arr) = self.data
mrec, _, _, arr = self._create_data()
alttype = [('A', float), ('B', float)]
test = mrec.view(alttype)
assert_(isinstance(test, MaskedRecords))

View File

@@ -83,11 +83,7 @@ from numpy.ma import (
where,
zeros,
)
from numpy.testing import (
assert_,
assert_equal,
assert_raises,
)
from numpy.testing import assert_, assert_equal, assert_raises
pi = np.pi
@@ -101,7 +97,7 @@ def eq(v, w, msg=''):
class TestMa:
def setup_method(self):
def _create_data(self):
x = np.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.])
y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
a10 = 10.
@@ -114,11 +110,11 @@ class TestMa:
xf = np.where(m1, 1e+20, x)
s = x.shape
xm.set_fill_value(1e+20)
self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf, s)
return x, y, a10, m1, m2, xm, ym, z, zm, xf, s
def test_testBasic1d(self):
# Test of basic array creation and properties in 1 dimension.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, _, _, m1, _, xm, _, _, _, xf, s = self._create_data()
assert_(not isMaskedArray(x))
assert_(isMaskedArray(xm))
assert_equal(shape(xm), s)
@@ -133,7 +129,7 @@ class TestMa:
@pytest.mark.parametrize("s", [(4, 3), (6, 2)])
def test_testBasic2d(self, s):
# Test of basic array creation and properties in 2 dimensions.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, y, _, m1, _, xm, ym, _, _, xf, s = self._create_data()
x.shape = s
y.shape = s
xm.shape = s
@@ -152,7 +148,7 @@ class TestMa:
def test_testArithmetic(self):
# Test of basic arithmetic.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, y, a10, _, _, xm, ym, _, _, xf, s = self._create_data()
a2d = array([[1, 2], [0, 4]])
a2dm = masked_array(a2d, [[0, 0], [1, 0]])
assert_(eq(a2d * a2d, a2d * a2dm))
@@ -196,7 +192,7 @@ class TestMa:
def test_testUfuncs1(self):
# Test various functions such as sin, cos.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, y, _, _, _, xm, ym, z, zm, _, _ = self._create_data()
assert_(eq(np.cos(x), cos(xm)))
assert_(eq(np.cosh(x), cosh(xm)))
assert_(eq(np.sin(x), sin(xm)))
@@ -242,7 +238,7 @@ class TestMa:
def test_testMinMax(self):
# Test minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, _, _, _, _, xm, _, _, _, _, _ = self._create_data()
xr = np.ravel(x) # max doesn't work if shaped
xmr = ravel(xm)
@@ -252,7 +248,7 @@ class TestMa:
def test_testAddSumProd(self):
# Test add, sum, product.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x, y, _, _, _, xm, ym, _, _, _, s = self._create_data()
assert_(eq(np.add.reduce(x), add.reduce(x)))
assert_(eq(np.add.accumulate(x), add.accumulate(x)))
assert_(eq(4, sum(array(4), axis=0)))
@@ -421,7 +417,7 @@ class TestMa:
assert_(eq(x, [0, 1, 10, 40, 4]))
def test_testMaPut(self):
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
_, _, _, _, _, _, ym, _, zm, _, _ = self._create_data()
m = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]
i = np.nonzero(m)[0]
put(ym, i, zm)
@@ -781,8 +777,9 @@ class TestMa:
class TestUfuncs:
def setup_method(self):
self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
def _create_data(self):
return (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
def test_testUfuncRegression(self):
@@ -811,7 +808,7 @@ class TestUfuncs:
except AttributeError:
uf = getattr(fromnumeric, f)
mf = getattr(np.ma, f)
args = self.d[:uf.nin]
args = self._create_data()[:uf.nin]
with np.errstate():
if f in f_invalid_ignore:
np.seterr(invalid='ignore')
@@ -823,7 +820,7 @@ class TestUfuncs:
assert_(eqmask(ur.mask, mr.mask))
def test_reduce(self):
a = self.d[0]
a = self._create_data()[0]
assert_(not alltrue(a, axis=0))
assert_(sometrue(a, axis=0))
assert_equal(sum(a[:3], axis=0), 0)
@@ -847,7 +844,7 @@ class TestUfuncs:
class TestArrayMethods:
def setup_method(self):
def _create_data(self):
x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
@@ -867,10 +864,10 @@ class TestArrayMethods:
mX = array(data=X, mask=m.reshape(X.shape))
mXX = array(data=XX, mask=m.reshape(XX.shape))
self.d = (x, X, XX, m, mx, mX, mXX)
return x, X, XX, m, mx, mX, mXX
def test_trace(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
_, X, _, _, _, mX, _ = self._create_data()
mXdiag = mX.diagonal()
assert_equal(mX.trace(), mX.diagonal().compressed().sum())
assert_(eq(mX.trace(),
@@ -878,15 +875,15 @@ class TestArrayMethods:
axis=0)))
def test_clip(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
x, _, _, _, mx, _, _ = self._create_data()
clipped = mx.clip(2, 8)
assert_(eq(clipped.mask, mx.mask))
assert_(eq(clipped._data, x.clip(2, 8)))
assert_(eq(clipped._data, mx._data.clip(2, 8)))
def test_ptp(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
(n, m) = X.shape
_, X, _, m, mx, mX, _ = self._create_data()
n, m = X.shape
# print(type(mx), mx.compressed())
# raise Exception()
assert_equal(mx.ptp(), np.ptp(mx.compressed()))
@@ -900,28 +897,28 @@ class TestArrayMethods:
assert_(eq(mX.ptp(1), rows))
def test_swapaxes(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
_, _, _, _, _, mX, mXX = self._create_data()
mXswapped = mX.swapaxes(0, 1)
assert_(eq(mXswapped[-1], mX[:, -1]))
mXXswapped = mXX.swapaxes(0, 2)
assert_equal(mXXswapped.shape, (2, 2, 3, 3))
def test_cumprod(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
mX = self._create_data()[5]
mXcp = mX.cumprod(0)
assert_(eq(mXcp._data, mX.filled(1).cumprod(0)))
mXcp = mX.cumprod(1)
assert_(eq(mXcp._data, mX.filled(1).cumprod(1)))
def test_cumsum(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
mX = self._create_data()[5]
mXcp = mX.cumsum(0)
assert_(eq(mXcp._data, mX.filled(0).cumsum(0)))
mXcp = mX.cumsum(1)
assert_(eq(mXcp._data, mX.filled(0).cumsum(1)))
def test_varstd(self):
(x, X, XX, m, mx, mX, mXX,) = self.d
_, X, XX, _, _, mX, mXX = self._create_data()
assert_(eq(mX.var(axis=None), mX.compressed().var()))
assert_(eq(mX.std(axis=None), mX.compressed().std()))
assert_(eq(mXX.var(axis=3).shape, XX.var(axis=3).shape))

View File

@@ -1,10 +1,5 @@
import numpy as np
from numpy.testing import (
assert_,
assert_allclose,
assert_array_equal,
suppress_warnings,
)
from numpy.testing import assert_, assert_array_equal
class TestRegression:
@@ -62,18 +57,6 @@ class TestRegression:
a.var(out=mout)
assert_(mout._data == 0)
def test_ddof_corrcoef(self):
# See gh-3336
x = np.ma.masked_equal([1, 2, 3, 4, 5], 4)
y = np.array([2, 2.5, 3.1, 3, 5])
# this test can be removed after deprecation.
with suppress_warnings() as sup:
sup.filter(DeprecationWarning, "bias and ddof have no effect")
r0 = np.ma.corrcoef(x, y, ddof=0)
r1 = np.ma.corrcoef(x, y, ddof=1)
# ddof should not have an effect (it gets cancelled out)
assert_allclose(r0.data, r1.data)
def test_mask_not_backmangled(self):
# See gh-10314. Test case taken from gh-3140.
a = np.ma.MaskedArray([1., 2.], mask=[False, False])

View File

@@ -188,10 +188,10 @@ class WrappedArray(NDArrayOperatorsMixin):
class TestSubclassing:
# Test suite for masked subclasses of ndarray.
def setup_method(self):
def _create_data(self):
x = np.arange(5, dtype='float')
mx = msubarray(x, mask=[0, 1, 0, 0, 0])
self.data = (x, mx)
return x, mx
def test_data_subclassing(self):
# Tests whether the subclass is kept.
@@ -205,19 +205,19 @@ class TestSubclassing:
def test_maskedarray_subclassing(self):
# Tests subclassing MaskedArray
(x, mx) = self.data
mx = self._create_data()[1]
assert_(isinstance(mx._data, subarray))
def test_masked_unary_operations(self):
# Tests masked_unary_operation
(x, mx) = self.data
x, mx = self._create_data()
with np.errstate(divide='ignore'):
assert_(isinstance(log(mx), msubarray))
assert_equal(log(x), np.log(x))
def test_masked_binary_operations(self):
# Tests masked_binary_operation
(x, mx) = self.data
x, mx = self._create_data()
# Result should be a msubarray
assert_(isinstance(add(mx, mx), msubarray))
assert_(isinstance(add(mx, x), msubarray))
@@ -230,7 +230,7 @@ class TestSubclassing:
def test_masked_binary_operations2(self):
# Tests domained_masked_binary_operation
(x, mx) = self.data
x, mx = self._create_data()
xmx = masked_array(mx.data.__array__(), mask=mx.mask)
assert_(isinstance(divide(mx, mx), msubarray))
assert_(isinstance(divide(mx, x), msubarray))
@@ -427,20 +427,20 @@ def test_array_no_inheritance():
class TestClassWrapping:
# Test suite for classes that wrap MaskedArrays
def setup_method(self):
def _create_data(self):
m = np.ma.masked_array([1, 3, 5], mask=[False, True, False])
wm = WrappedArray(m)
self.data = (m, wm)
return m, wm
def test_masked_unary_operations(self):
# Tests masked_unary_operation
(m, wm) = self.data
wm = self._create_data()[1]
with np.errstate(divide='ignore'):
assert_(isinstance(np.log(wm), WrappedArray))
def test_masked_binary_operations(self):
# Tests masked_binary_operation
(m, wm) = self.data
m, wm = self._create_data()
# Result should be a WrappedArray
assert_(isinstance(np.add(wm, wm), WrappedArray))
assert_(isinstance(np.add(m, wm), WrappedArray))

View File

@@ -121,7 +121,7 @@ def assert_equal(actual, desired, err_msg=''):
if not isinstance(actual, dict):
raise AssertionError(repr(type(actual)))
assert_equal(len(actual), len(desired), err_msg)
for k, i in desired.items():
for k in desired:
if k not in actual:
raise AssertionError(f"{k} not in {actual}")
assert_equal(actual[k], desired[k], f'key={k!r}\n{err_msg}')
@@ -159,7 +159,7 @@ def fail_if_equal(actual, desired, err_msg='',):
if not isinstance(actual, dict):
raise AssertionError(repr(type(actual)))
fail_if_equal(len(actual), len(desired), err_msg)
for k, i in desired.items():
for k in desired:
if k not in actual:
raise AssertionError(repr(k))
fail_if_equal(actual[k], desired[k], f'key={k!r}\n{err_msg}')