start adding MaskedArray in numeric package

This commit is contained in:
wyq 2021-07-16 15:51:45 +08:00
parent e4d3a044e8
commit e5bbee86cf
7 changed files with 218 additions and 5 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\common_math\optimize">
<Path OpenPath="D:\Working\MIScript\Jython\mis\array">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
@ -12,21 +12,23 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\geotiff"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\bar"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\optimize"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\cluster\dbscan_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\bar\bar_hatch.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\optimize\fixed_point.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\masked_array_1.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\cluster\dbscan_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\bar\bar_hatch.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\optimize\fixed_point.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\masked_array_1.py"/>
</RecentFiles>
</File>
<Font>
@ -34,5 +36,5 @@
</Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-7,-7" MainFormSize="1293,693"/>
<Startup MainFormLocation="-7,0" MainFormSize="1380,813"/>
</MeteoInfo>

View File

@ -1,5 +1,48 @@
import mipylib.numeric as np
def resample_nn_1d(a, centers):
"""Return one-dimensional nearest-neighbor indexes based on user-specified centers.
Parameters
----------
a : array-like
1-dimensional array of numeric values from which to extract indexes of
nearest-neighbors
centers : array-like
1-dimensional array of numeric values representing a subset of values to approximate
Returns
-------
A list of indexes (in type given by `array.argmin()`) representing values closest to
given array values.
"""
ix = []
for center in centers:
index = (np.abs(a - center)).argmin()
if index not in ix:
ix.append(index)
return ix
def nearest_intersection_idx(a, b):
"""Determine the index of the point just before two lines with common x values.
Parameters
----------
a : array-like
1-dimensional array of y-values for line 1
b : array-like
1-dimensional array of y-values for line 2
Returns
-------
An array of indexes representing the index of the values
just before the intersection(s) of the two lines.
"""
# Difference in the two y-value sets
difference = a - b
# Determine the point just before the intersection of the lines
# Will return multiple points for multiple intersections
sign_change_idx, = np.nonzero(np.diff(np.sign(difference)))
return sign_change_idx
def _remove_nans(*variables):
"""Remove NaNs from arrays that cause issues with calculations.
Takes a variable number of arguments and returns masked arrays in the same

View File

@ -4,6 +4,7 @@ from . import lib
from .lib import *
from . import linalg
from . import random
from . import ma
from . import fitting
from . import stats
from . import interpolate
@ -17,6 +18,6 @@ __all__ = []
__all__.extend(['__version__'])
__all__.extend(core.__all__)
__all__.extend(lib.__all__)
__all__.extend(['linalg', 'fitting', 'random', 'stats', 'interpolate', 'optimize', 'signal', 'spatial',
__all__.extend(['linalg', 'fitting', 'random', 'ma', 'stats', 'interpolate', 'optimize', 'signal', 'spatial',
'special'])
__all__.extend(['griddata'])

View File

@ -0,0 +1 @@
from .core import masked_array, MaskedArray

View File

@ -0,0 +1,165 @@
from mipylib import numeric as np
from ..core._ndarray import NDArray
from org.meteoinfo.ndarray.math import ArrayUtil
nomask = False
class MaskedArray(NDArray):
"""
An array class with possibly masked values.
Masked values of True exclude the corresponding element from any
computation.
Construction::
x = MaskedArray(data, mask=nomask, dtype=None, copy=False, subok=True,
ndmin=0, fill_value=None, keep_mask=True, hard_mask=None,
shrink=True, order=None)
Parameters
----------
data : array_like
Input data.
mask : sequence, optional
Mask. Must be convertible to an array of booleans with the same
shape as `data`. True indicates a masked (i.e. invalid) data.
dtype : dtype, optional
Data type of the output.
If `dtype` is None, the type of the data argument (``data.dtype``)
is used. If `dtype` is not None and different from ``data.dtype``,
a copy is performed.
copy : bool, optional
Whether to copy the input data (True), or to use a reference instead.
Default is False.
subok : bool, optional
Whether to return a subclass of `MaskedArray` if possible (True) or a
plain `MaskedArray`. Default is True.
ndmin : int, optional
Minimum number of dimensions. Default is 0.
fill_value : scalar, optional
Value used to fill in the masked values when necessary.
If None, a default based on the data-type is used.
"""
def __init__(self, data, mask=nomask, dtype=None, copy=False,
subok=True, ndmin=0, fill_value=None):
if isinstance(data, NDArray):
data = data._array
super(MaskedArray, self).__init__(data)
self._data = NDArray(self._array)
self._baseclass = getattr(data, '_baseclass', type(self._data))
if mask is nomask:
self._mask = mask
else:
self._mask = np.array(mask)
if self._mask.shape != self._data.shape:
self._mask = self._mask.reshape(self._data.shape)
if self._mask.dtype != np.dtype.bool:
self._mask = self._mask.astype(np.dtype.bool)
self._fill_value = fill_value
def __str__(self):
r = 'masked_array(data=' + ArrayUtil.convertToString(self._data._array) + ',\n\tmask='
if self._mask is nomask:
r = r + 'False,'
else:
r = r + ArrayUtil.convertToString(self._mask._array) + ','
r = r + '\n\tfill_value=' + str(self._fill_value) + ')'
return r
def __repr__(self):
return self.__str__()
def filled(self, fill_value=None):
"""
Return a copy of self, with masked values filled with a given value.
**However**, if there are no masked values to fill, self will be
returned instead as an ndarray.
Parameters
----------
fill_value : array_like, optional
The value to use for invalid entries. Can be scalar or non-scalar.
If non-scalar, the resulting ndarray must be broadcastable over
input array. Default is None, in which case, the `fill_value`
attribute of the array is used instead.
Returns
-------
filled_array : ndarray
A copy of ``self`` with invalid entries replaced by *fill_value*
(be it the function argument or the attribute of ``self``), or
``self`` itself as an ndarray if there are no invalid entries to
be replaced.
Notes
-----
The result is **not** a MaskedArray!
Examples
--------
>>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999)
>>> x.filled()
array([ 1, 2, -999, 4, -999])
>>> x.filled(fill_value=1000)
array([ 1, 2, 1000, 4, 1000])
>>> type(x.filled())
<class 'numpy.ndarray'>
Subclassing is preserved. This means that if, e.g., the data part of
the masked array is a recarray, `filled` returns a recarray:
>>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray)
>>> m = np.ma.array(x, mask=[(True, False), (False, True)])
>>> m.filled()
rec.array([(999999, 2), ( -3, 999999)],
dtype=[('f0', '<i8'), ('f1', '<i8')])
"""
m = self._mask
if m is nomask:
return self._data
if fill_value is None:
fill_value = self._fill_value
result = self._data.copy()
result[m] = fill_value
return result
def sum(self, axis=None):
"""
Sum of array elements over a given axis.
:param axis: (*int*) Axis along which the standard deviation is computed.
The default is to compute the standard deviation of the flattened array.
returns: (*array_like*) Sum result.
"""
if self._mask is nomask:
return self._data.sum(axis=axis)
else:
r = self.filled(0)
return r.sum(axis=axis)
def count(self, axis=None):
"""
Count of valid array elements over a given axis.
:param axis: (*int*) Axis along which the standard deviation is computed.
The default is to compute the standard deviation of the flattened array.
:return: (*array_like*) Count result.
"""
if self._mask is nomask:
mask = np.ones(self._data.shape)
return mask.sum(axis=axis)
else:
return (~self._mask).sum(axis=axis)
def mean(self, axis=None):
"""
Compute tha arithmetic mean along the specified axis.
:param axis: (*int*) Axis along which the value is computed.
The default is to compute the value of the flattened array.
returns: (*array_like*) Mean result
"""
if self._mask is nomask:
return self._data.mean(axis=axis)
else:
dsum = self.sum(axis=axis)
cnt = self.count(axis=axis)
return dsum * 1. / cnt
masked_array = MaskedArray

View File

@ -7763,6 +7763,7 @@ public class ArrayMath {
Number n = new Double(v);
switch (dt) {
case INT:
case BOOLEAN:
return n.intValue();
case FLOAT:
return n.floatValue();