diff options
Diffstat (limited to 'numpy/ma')
| -rw-r--r-- | numpy/ma/core.py | 196 | ||||
| -rw-r--r-- | numpy/ma/extras.py | 1 | ||||
| -rw-r--r-- | numpy/ma/mrecords.py | 11 | ||||
| -rw-r--r-- | numpy/ma/tests/test_subclassing.py | 4 | ||||
| -rw-r--r-- | numpy/ma/timer_comparison.py | 15 |
5 files changed, 99 insertions, 128 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 2b17fa343..90aff6ec8 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -44,7 +44,6 @@ from numpy.compat import ( getargspec, formatargspec, long, basestring, unicode, bytes ) from numpy import expand_dims -from numpy.core.multiarray import normalize_axis_index from numpy.core.numeric import normalize_axis_tuple from numpy.core._internal import recursive from numpy.compat import pickle @@ -1198,7 +1197,6 @@ exp = _MaskedUnaryOperation(umath.exp) conjugate = _MaskedUnaryOperation(umath.conjugate) sin = _MaskedUnaryOperation(umath.sin) cos = _MaskedUnaryOperation(umath.cos) -tan = _MaskedUnaryOperation(umath.tan) arctan = _MaskedUnaryOperation(umath.arctan) arcsinh = _MaskedUnaryOperation(umath.arcsinh) sinh = _MaskedUnaryOperation(umath.sinh) @@ -3445,39 +3443,41 @@ class MaskedArray(ndarray): _set_mask = __setmask__ - def _get_mask(self): - """Return the current mask. + @property + def mask(self): + """ Current mask. """ - """ # We could try to force a reshape, but that wouldn't work in some # cases. return self._mask - mask = property(fget=_get_mask, fset=__setmask__, doc="Mask") + @mask.setter + def mask(self, value): + self.__setmask__(value) - def _get_recordmask(self): + @property + def recordmask(self): """ - Return the mask of the records. - - A record is masked when all the fields are masked. + Get or set the mask of the array if it has no named fields. For + structured arrays, returns a ndarray of booleans where entries are + ``True`` if **all** the fields are masked, ``False`` otherwise: + >>> x = np.ma.array([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], + ... mask=[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], + ... dtype=[('a', int), ('b', int)]) + >>> x.recordmask + array([False, False, True, False, False]) """ + _mask = self._mask.view(ndarray) if _mask.dtype.names is None: return _mask return np.all(flatten_structured_array(_mask), axis=-1) - def _set_recordmask(self): - """ - Return the mask of the records. - - A record is masked when all the fields are masked. - - """ + @recordmask.setter + def recordmask(self, mask): raise NotImplementedError("Coming soon: setting the mask per records!") - recordmask = property(fget=_get_recordmask) - def harden_mask(self): """ Force the mask to hard. @@ -3508,8 +3508,10 @@ class MaskedArray(ndarray): self._hardmask = False return self - hardmask = property(fget=lambda self: self._hardmask, - doc="Hardness of the mask") + @property + def hardmask(self): + """ Hardness of the mask """ + return self._hardmask def unshare_mask(self): """ @@ -3529,8 +3531,10 @@ class MaskedArray(ndarray): self._sharedmask = False return self - sharedmask = property(fget=lambda self: self._sharedmask, - doc="Share status of the mask (read-only).") + @property + def sharedmask(self): + """ Share status of the mask (read-only). """ + return self._sharedmask def shrink_mask(self): """ @@ -3563,39 +3567,46 @@ class MaskedArray(ndarray): self._mask = _shrink_mask(self._mask) return self - baseclass = property(fget=lambda self: self._baseclass, - doc="Class of the underlying data (read-only).") + @property + def baseclass(self): + """ Class of the underlying data (read-only). """ + return self._baseclass def _get_data(self): - """Return the current data, as a view of the original - underlying data. + """ + Returns the underlying data, as a view of the masked array. + + If the underlying data is a subclass of :class:`numpy.ndarray`, it is + returned as such. + >>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]]) + >>> x.data + matrix([[1, 2], + [3, 4]]) + + The type of the data can be accessed through the :attr:`baseclass` + attribute. """ return ndarray.view(self, self._baseclass) _data = property(fget=_get_data) data = property(fget=_get_data) - def _get_flat(self): - "Return a flat iterator." + @property + def flat(self): + """ Return a flat iterator, or set a flattened version of self to value. """ return MaskedIterator(self) - def _set_flat(self, value): - "Set a flattened version of self to value." + @flat.setter + def flat(self, value): y = self.ravel() y[:] = value - flat = property(fget=_get_flat, fset=_set_flat, - doc="Flat version of the array.") - - def get_fill_value(self): + @property + def fill_value(self): """ - Return the filling value of the masked array. - - Returns - ------- - fill_value : scalar - The filling value. + The filling value of the masked array is a scalar. When setting, None + will set to a default based on the data type. Examples -------- @@ -3608,8 +3619,17 @@ class MaskedArray(ndarray): (1e+20+0j) >>> x = np.ma.array([0, 1.], fill_value=-np.inf) - >>> x.get_fill_value() + >>> x.fill_value -inf + >>> x.fill_value = np.pi + >>> x.fill_value + 3.1415926535897931 # may vary + + Reset to default: + + >>> x.fill_value = None + >>> x.fill_value + 1e+20 """ if self._fill_value is None: @@ -3623,36 +3643,8 @@ class MaskedArray(ndarray): return self._fill_value[()] return self._fill_value - def set_fill_value(self, value=None): - """ - Set the filling value of the masked array. - - Parameters - ---------- - value : scalar, optional - The new filling value. Default is None, in which case a default - based on the data type is used. - - See Also - -------- - ma.set_fill_value : Equivalent function. - - Examples - -------- - >>> x = np.ma.array([0, 1.], fill_value=-np.inf) - >>> x.fill_value - -inf - >>> x.set_fill_value(np.pi) - >>> x.fill_value - 3.1415926535897931 # may vary - - Reset to default: - - >>> x.set_fill_value() - >>> x.fill_value - 1e+20 - - """ + @fill_value.setter + def fill_value(self, value=None): target = _check_fill_value(value, self.dtype) _fill_value = self._fill_value if _fill_value is None: @@ -3662,8 +3654,9 @@ class MaskedArray(ndarray): # Don't overwrite the attribute, just fill it (for propagation) _fill_value[()] = target - fill_value = property(fget=get_fill_value, fset=set_fill_value, - doc="Filling value.") + # kept for compatibility + get_fill_value = fill_value.fget + set_fill_value = fill_value.fset def filled(self, fill_value=None): """ @@ -4332,31 +4325,21 @@ class MaskedArray(ndarray): raise MaskError('Cannot convert masked element to a Python long.') return long(self.item()) - - def get_imag(self): + @property + def imag(self): """ - Return the imaginary part of the masked array. - - The returned array is a view on the imaginary part of the `MaskedArray` - whose `get_imag` method is called. - - Parameters - ---------- - None + The imaginary part of the masked array. - Returns - ------- - result : MaskedArray - The imaginary part of the masked array. + This property is a view on the imaginary part of this `MaskedArray`. See Also -------- - get_real, real, imag + real Examples -------- >>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) - >>> x.get_imag() + >>> x.imag masked_array(data=[1.0, --, 1.6], mask=[False, True, False], fill_value=1e+20) @@ -4366,32 +4349,24 @@ class MaskedArray(ndarray): result.__setmask__(self._mask) return result - imag = property(fget=get_imag, doc="Imaginary part.") + # kept for compatibility + get_imag = imag.fget - def get_real(self): + @property + def real(self): """ - Return the real part of the masked array. - - The returned array is a view on the real part of the `MaskedArray` - whose `get_real` method is called. + The real part of the masked array. - Parameters - ---------- - None - - Returns - ------- - result : MaskedArray - The real part of the masked array. + This property is a view on the real part of this `MaskedArray`. See Also -------- - get_imag, real, imag + imag Examples -------- >>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) - >>> x.get_real() + >>> x.real masked_array(data=[1.0, --, 3.45], mask=[False, True, False], fill_value=1e+20) @@ -4400,7 +4375,9 @@ class MaskedArray(ndarray): result = self._data.real.view(type(self)) result.__setmask__(self._mask) return result - real = property(fget=get_real, doc="Real part") + + # kept for compatibility + get_real = real.fget def count(self, axis=None, keepdims=np._NoValue): """ @@ -6156,12 +6133,11 @@ class mvoid(MaskedArray): _data.fill_value = fill_value return _data - def _get_data(self): + @property + def _data(self): # Make sure that the _data part is a np.void return super(mvoid, self)._data[()] - _data = property(fget=_get_data) - def __getitem__(self, indx): """ Get the index. diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 2e3b84e1c..a51d83578 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -390,7 +390,6 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): i[axis] = slice(None, None) outshape = np.asarray(arr.shape).take(indlist) i.put(indlist, ind) - j = i.copy() res = func1d(arr[tuple(i.tolist())], *args, **kwargs) # if res is a number, then we have a smaller output array asscalar = np.isscalar(res) diff --git a/numpy/ma/mrecords.py b/numpy/ma/mrecords.py index daf2f8770..9bcb3947d 100644 --- a/numpy/ma/mrecords.py +++ b/numpy/ma/mrecords.py @@ -19,7 +19,6 @@ import sys import warnings import numpy as np -import numpy.core.numerictypes as ntypes from numpy.compat import basestring from numpy import ( bool_, dtype, ndarray, recarray, array as narray @@ -167,24 +166,22 @@ class MaskedRecords(MaskedArray, object): _dict['_baseclass'] = recarray return - def _getdata(self): + @property + def _data(self): """ Returns the data as a recarray. """ return ndarray.view(self, recarray) - _data = property(fget=_getdata) - - def _getfieldmask(self): + @property + def _fieldmask(self): """ Alias to mask. """ return self._mask - _fieldmask = property(fget=_getfieldmask) - def __len__(self): """ Returns the length diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index f8ab52bb9..440b36722 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -66,11 +66,11 @@ class MSubArray(SubArray, MaskedArray): _data.info = subarr.info return _data - def _get_series(self): + @property + def _series(self): _view = self.view(MaskedArray) _view._sharedmask = False return _view - _series = property(fget=_get_series) msubarray = MSubArray diff --git a/numpy/ma/timer_comparison.py b/numpy/ma/timer_comparison.py index 68104ed0a..4ad635e38 100644 --- a/numpy/ma/timer_comparison.py +++ b/numpy/ma/timer_comparison.py @@ -430,11 +430,10 @@ if __name__ == '__main__': setup_cur = "import numpy.ma.core as module\n" + setup_base (nrepeat, nloop) = (10, 10) - if 1: - for i in range(1, 8): - func = 'tester.test_%i()' % i - cur = timeit.Timer(func, setup_cur).repeat(nrepeat, nloop*10) - cur = np.sort(cur) - print("#%i" % i + 50*'.') - print(eval("ModuleTester.test_%i.__doc__" % i)) - print("core_current : %.3f - %.3f" % (cur[0], cur[1])) + for i in range(1, 8): + func = 'tester.test_%i()' % i + cur = timeit.Timer(func, setup_cur).repeat(nrepeat, nloop*10) + cur = np.sort(cur) + print("#%i" % i + 50*'.') + print(eval("ModuleTester.test_%i.__doc__" % i)) + print("core_current : %.3f - %.3f" % (cur[0], cur[1])) |
