Example #1
0
    def __getitem__(self, indx):
        """x.__getitem__(y) <==> x[y]
        
        Returns the item described by indx. Not a copy.
        """

        _data = ndarray.__getattribute__(self, '_data')
        _mask = ndarray.__getattribute__(self, '_mask')
        _err  = ndarray.__getattribute__(self, '_err')

        output = _data.__getitem__(indx)
        if not getattr(output, 'ndim', False):
            if _mask is not nomask and _mask[indx]:
                return masked
            if _err is not noerr:
                return (output, _err[indx])
            else:
                return (output, None)

        newdvect = _data.__getitem__(indx).view(type(self))
        newdvect._mask = _mask[indx]
        if _err is noerr:
            newdvect._err = noerr
        else:
            newdvect._err = _err[indx]
        newdvect._name  = self._name
        newdvect._units = self._units
        return newdvect
Example #2
0
    def __setattr__(self, attr, val):
        """
        Sets the attribute attr to the value val.

        """
        # Should we call __setmask__ first ?
        if attr in ['mask', 'fieldmask']:
            self.__setmask__(val)
            return
        # Create a shortcut (so that we don't have to call getattr all the time)
        _localdict = object.__getattribute__(self, '__dict__')
        # Check whether we're creating a new field
        newattr = attr not in _localdict
        try:
            # Is attr a generic attribute ?
            ret = object.__setattr__(self, attr, val)
        except Exception:
            # Not a generic attribute: exit if it's not a valid field
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            optinfo = ndarray.__getattribute__(self, '_optinfo') or {}
            if not (attr in fielddict or attr in optinfo):
                raise
        else:
            # Get the list of names
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            # Check the attribute
            if attr not in fielddict:
                return ret
            if newattr:
                # We just added this one or this setattr worked on an
                # internal attribute.
                try:
                    object.__delattr__(self, attr)
                except Exception:
                    return ret
        # Let's try to set the field
        try:
            res = fielddict[attr][:2]
        except (TypeError, KeyError) as e:
            raise AttributeError(
                f'record array has no attribute {attr}') from e

        if val is masked:
            _fill_value = _localdict['_fill_value']
            if _fill_value is not None:
                dval = _localdict['_fill_value'][attr]
            else:
                dval = val
            mval = True
        else:
            dval = filled(val)
            mval = getmaskarray(val)
        obj = ndarray.__getattribute__(self, '_data').setfield(dval, *res)
        _localdict['_mask'].__setitem__(attr, mval)
        return obj
Example #3
0
    def __setattr__(self, attr, val):
        """
        Sets the attribute attr to the value val.

        """
        # Should we call __setmask__ first ?
        if attr in ['mask', 'fieldmask']:
            self.__setmask__(val)
            return
        # Create a shortcut (so that we don't have to call getattr all the time)
        _localdict = object.__getattribute__(self, '__dict__')
        # Check whether we're creating a new field
        newattr = attr not in _localdict
        try:
            # Is attr a generic attribute ?
            ret = object.__setattr__(self, attr, val)
        except:
            # Not a generic attribute: exit if it's not a valid field
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            optinfo = ndarray.__getattribute__(self, '_optinfo') or {}
            if not (attr in fielddict or attr in optinfo):
                exctype, value = sys.exc_info()[:2]
                raise exctype(value)
        else:
            # Get the list of names
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            # Check the attribute
            if attr not in fielddict:
                return ret
            if newattr:
                # We just added this one or this setattr worked on an
                # internal attribute.
                try:
                    object.__delattr__(self, attr)
                except:
                    return ret
        # Let's try to set the field
        try:
            res = fielddict[attr][:2]
        except (TypeError, KeyError):
            raise AttributeError("record array has no attribute %s" % attr)

        if val is masked:
            _fill_value = _localdict['_fill_value']
            if _fill_value is not None:
                dval = _localdict['_fill_value'][attr]
            else:
                dval = val
            mval = True
        else:
            dval = filled(val)
            mval = getmaskarray(val)
        obj = ndarray.__getattribute__(self, '_data').setfield(dval, *res)
        _localdict['_mask'].__setitem__(attr, mval)
        return obj
Example #4
0
    def filled(self, fill_value=None):
        """Returns an array of the same class as the _data part, where masked
    values are filled with fill_value.
    If fill_value is None, self.fill_value is used instead.

    Subclassing is preserved.

        """
        _localdict = self.__dict__
        d = ndarray.__getattribute__(self, '_data')
        fm = _localdict['_fieldmask']
        if not np.asarray(fm, dtype=bool_).any():
            return d
        #
        if fill_value is None:
            value = _check_fill_value(_localdict['_fill_value'], self.dtype)
        else:
            value = fill_value
            if np.size(value) == 1:
                value = [
                    value,
                ] * len(self.dtype)
        #
        if self is masked:
            result = np.asanyarray(value)
        else:
            result = d.copy()
            for (n, v) in zip(fm.dtype.names, value):
                np.putmask(np.asarray(result[n]), np.asarray(fm[n]), v)
        return result
Example #5
0
 def __array_finalize__(self, obj):
     # Make sure we have a _fieldmask by default ..
     _fieldmask = getattr(obj, '_fieldmask', None)
     if _fieldmask is None:
         mdescr = _make_mask_dtype(ndarray.__getattribute__(self, 'dtype'))
         _mask = getattr(obj, '_mask', nomask)
         if _mask is nomask:
             _fieldmask = np.empty(self.shape, dtype=mdescr).view(recarray)
             _fieldmask.flat = tuple([False] * len(mdescr))
         else:
             _fieldmask = narray([tuple([m] * len(mdescr)) for m in _mask],
                                 dtype=mdescr).view(recarray)
     # Update some of the attributes
     if obj is not None:
         _baseclass = getattr(obj, '_baseclass', type(obj))
     else:
         _baseclass = recarray
     attrdict = dict(_fieldmask=_fieldmask,
                     _hardmask=getattr(obj, '_hardmask', False),
                     _fill_value=getattr(obj, '_fill_value', None),
                     _sharedmask=getattr(obj, '_sharedmask', False),
                     _baseclass=_baseclass)
     self.__dict__.update(attrdict)
     # Finalize as a regular maskedarray .....
     # Update special attributes ...
     self._basedict = getattr(obj, '_basedict',
                              getattr(obj, '__dict__', {}))
     self.__dict__.update(self._basedict)
     return
Example #6
0
    def __getitem__(self, indx):
        """Returns all the fields sharing the same fieldname base.
The fieldname base is either `_data` or `_mask`."""
        _localdict = self.__dict__
        _mask = ndarray.__getattribute__(self, '_mask')
        _data = ndarray.view(self, _localdict['_baseclass'])
        # We want a field ........
        if isinstance(indx, basestring):
            #!!!: Make sure _sharedmask is True to propagate back to _fieldmask
            #!!!: Don't use _set_mask, there are some copies being made...
            #!!!: ...that break propagation
            #!!!: Don't force the mask to nomask, that wrecks easy masking
            obj = _data[indx].view(MaskedArray)
            obj._mask = _mask[indx]
            obj._sharedmask = True
            fval = _localdict['_fill_value']
            if fval is not None:
                obj._fill_value = fval[indx]
            # Force to masked if the mask is True
            if not obj.ndim and obj._mask:
                return masked
            return obj
        # We want some elements ..
        # First, the data ........
        obj = np.array(_data[indx], copy=False).view(mrecarray)
        obj._mask = np.array(_mask[indx], copy=False).view(recarray)
        return obj
Example #7
0
    def filled(self, fill_value=None):
        """Returns an array of the same class as the _data part, where masked
    values are filled with fill_value.
    If fill_value is None, self.fill_value is used instead.

    Subclassing is preserved.

        """
        _localdict = self.__dict__
        d = ndarray.__getattribute__(self, '_data')
        fm = _localdict['_fieldmask']
        if not np.asarray(fm, dtype=bool_).any():
            return d
        #
        if fill_value is None:
            value = _check_fill_value(_localdict['_fill_value'],self.dtype)
        else:
            value = fill_value
            if np.size(value) == 1:
                value = [value,] * len(self.dtype)
        #
        if self is masked:
            result = np.asanyarray(value)
        else:
            result = d.copy()
            for (n, v) in zip(fm.dtype.names, value):
                np.putmask(np.asarray(result[n]), np.asarray(fm[n]), v)
        return result
Example #8
0
    def __getitem__(self, indx):
        """
        Returns all the fields sharing the same fieldname base.

        The fieldname base is either `_data` or `_mask`.

        """
        _localdict = self.__dict__
        _mask = ndarray.__getattribute__(self, '_mask')
        _data = ndarray.view(self, _localdict['_baseclass'])
        # We want a field
        if isinstance(indx, str):
            # Make sure _sharedmask is True to propagate back to _fieldmask
            # Don't use _set_mask, there are some copies being made that
            # break propagation Don't force the mask to nomask, that wreaks
            # easy masking
            obj = _data[indx].view(MaskedArray)
            obj._mask = _mask[indx]
            obj._sharedmask = True
            fval = _localdict['_fill_value']
            if fval is not None:
                obj._fill_value = fval[indx]
            # Force to masked if the mask is True
            if not obj.ndim and obj._mask:
                return masked
            return obj
        # We want some elements.
        # First, the data.
        obj = np.array(_data[indx], copy=False).view(mrecarray)
        obj._mask = np.array(_mask[indx], copy=False).view(recarray)
        return obj
Example #9
0
 def __array_finalize__(self,obj):
     # Make sure we have a _fieldmask by default ..
     _fieldmask = getattr(obj, '_fieldmask', None)
     if _fieldmask is None:
         mdescr = _make_mask_dtype(ndarray.__getattribute__(self, 'dtype'))
         _mask = getattr(obj, '_mask', nomask)
         if _mask is nomask:
             _fieldmask = np.empty(self.shape, dtype=mdescr).view(recarray)
             _fieldmask.flat = tuple([False]*len(mdescr))
         else:
             _fieldmask = narray([tuple([m]*len(mdescr)) for m in _mask],
                                 dtype=mdescr).view(recarray)
     # Update some of the attributes
     if obj is not None:
         _baseclass = getattr(obj,'_baseclass',type(obj))
     else:
         _baseclass = recarray
     attrdict = dict(_fieldmask=_fieldmask,
                     _hardmask=getattr(obj,'_hardmask',False),
                     _fill_value=getattr(obj,'_fill_value',None),
                     _sharedmask=getattr(obj,'_sharedmask',False),
                     _baseclass=_baseclass)
     self.__dict__.update(attrdict)
     # Finalize as a regular maskedarray .....
     # Update special attributes ...
     self._basedict = getattr(obj, '_basedict', getattr(obj,'__dict__',{}))
     self.__dict__.update(self._basedict)
     return
Example #10
0
 def __getattribute__(self, attr):
     try:
         return object.__getattribute__(self, attr)
     except AttributeError:
         # attr must be a fieldname
         pass
     fielddict = ndarray.__getattribute__(self, 'dtype').fields
     try:
         res = fielddict[attr][:2]
     except (TypeError, KeyError) as e:
         raise AttributeError(
             f'record array has no attribute {attr}') from e
     # So far, so good
     _localdict = ndarray.__getattribute__(self, '__dict__')
     _data = ndarray.view(self, _localdict['_baseclass'])
     obj = _data.getfield(*res)
     if obj.dtype.names is not None:
         raise NotImplementedError("MaskedRecords is currently limited to"
                                   "simple records.")
     # Get some special attributes
     # Reset the object's mask
     hasmasked = False
     _mask = _localdict.get('_mask', None)
     if _mask is not None:
         try:
             _mask = _mask[attr]
         except IndexError:
             # Couldn't find a mask: use the default (nomask)
             pass
         tp_len = len(_mask.dtype)
         hasmasked = _mask.view((bool, ((tp_len, ) if tp_len else
                                        ()))).any()
     if (obj.shape or hasmasked):
         obj = obj.view(MaskedArray)
         obj._baseclass = ndarray
         obj._isfield = True
         obj._mask = _mask
         # Reset the field values
         _fill_value = _localdict.get('_fill_value', None)
         if _fill_value is not None:
             try:
                 obj._fill_value = _fill_value[attr]
             except ValueError:
                 obj._fill_value = None
     else:
         obj = obj.item()
     return obj
Example #11
0
 def __getattribute__(self, attr):
     try:
         return object.__getattribute__(self, attr)
     except AttributeError:
         # attr must be a fieldname
         pass
     fielddict = ndarray.__getattribute__(self, 'dtype').fields
     try:
         res = fielddict[attr][:2]
     except (TypeError, KeyError):
         raise AttributeError("record array has no attribute %s" % attr)
     # So far, so good
     _localdict = ndarray.__getattribute__(self, '__dict__')
     _data = ndarray.view(self, _localdict['_baseclass'])
     obj = _data.getfield(*res)
     if obj.dtype.fields:
         raise NotImplementedError("MaskedRecords is currently limited to"
                                   "simple records.")
     # Get some special attributes
     # Reset the object's mask
     hasmasked = False
     _mask = _localdict.get('_mask', None)
     if _mask is not None:
         try:
             _mask = _mask[attr]
         except IndexError:
             # Couldn't find a mask: use the default (nomask)
             pass
         hasmasked = _mask.view((np.bool, (len(_mask.dtype) or 1))).any()
     if (obj.shape or hasmasked):
         obj = obj.view(MaskedArray)
         obj._baseclass = ndarray
         obj._isfield = True
         obj._mask = _mask
         # Reset the field values
         _fill_value = _localdict.get('_fill_value', None)
         if _fill_value is not None:
             try:
                 obj._fill_value = _fill_value[attr]
             except ValueError:
                 obj._fill_value = None
     else:
         obj = obj.item()
     return obj
Example #12
0
 def __getattribute__(self, attr):
     getattribute = MaskedRecords.__getattribute__
     _dict = getattribute(self, "__dict__")
     if attr in (ndarray.__getattribute__(self, "dtype").names or []):
         obj = getattribute(self, attr).view(ClimateSeries)
         obj._dates = _dict["_dates"]
         obj._ensoindicator = getattribute(self, "_ensoindicator")
         obj._optinfo = getattribute(self, "_optinfo")
         return obj
     return getattribute(self, attr)
Example #13
0
 def __getattribute__(self, attr):
     getattribute = MaskedRecords.__getattribute__
     _dict = getattribute(self, '__dict__')
     if attr in (ndarray.__getattribute__(self, 'dtype').names or []):
         obj = getattribute(self, attr).view(ClimateSeries)
         obj._dates = _dict['_dates']
         obj._ensoindicator = getattribute(self, '_ensoindicator')
         obj._optinfo = getattribute(self, '_optinfo')
         return obj
     return getattribute(self, attr)
Example #14
0
 def __getattribute__(self, attr):
     getattribute = MaskedRecords.__getattribute__
     _dict = getattribute(self,'__dict__')
     if attr == '_dict':
         return _dict
     _names = ndarray.__getattribute__(self,'dtype').names
     if attr in (_names or []):
         obj = getattribute(self,attr).view(TimeSeries)
         obj._dates = _dict['_dates']
         return obj
     return getattribute(self,attr)
Example #15
0
 def __getattribute__(self, attr):
     getattribute = MaskedRecords.__getattribute__
     _dict = getattribute(self, "__dict__")
     if attr == "_dict":
         return _dict
     _names = ndarray.__getattribute__(self, "dtype").names
     if attr in (_names or []):
         obj = getattribute(self, attr).view(TimeSeries)
         obj._dates = _dict["_dates"]
         return obj
     return getattribute(self, attr)
Example #16
0
 def __getattribute__(self, attr):
     try:
         return object.__getattribute__(self, attr)
     except AttributeError:  # attr must be a fieldname
         pass
     fielddict = ndarray.__getattribute__(self, 'dtype').fields
     try:
         res = fielddict[attr][:2]
     except (TypeError, KeyError):
         raise AttributeError, "record array has no attribute %s" % attr
     # So far, so good...
     _localdict = ndarray.__getattribute__(self, '__dict__')
     _data = ndarray.view(self, _localdict['_baseclass'])
     obj = _data.getfield(*res)
     if obj.dtype.fields:
         raise NotImplementedError("MaskedRecords is currently limited to"\
                                   "simple records...")
     obj = obj.view(MaskedArray)
     obj._baseclass = ndarray
     obj._isfield = True
     # Get some special attributes
     _fill_value = _localdict.get('_fill_value', None)
     _mask = _localdict.get('_mask', None)
     # Reset the object's mask
     if _mask is not None:
         try:
             obj._mask = _mask[attr]
         except IndexError:
             # Couldn't find a mask: use the default (nomask)
             pass
     # Reset the field values
     if _fill_value is not None:
         try:
             obj._fill_value = _fill_value[attr]
         except ValueError:
             obj._fill_value = None
     return obj
Example #17
0
 def __getitem__(self, indx):
     """Returns all the fields sharing the same fieldname base.
 The fieldname base is either `_data` or `_mask`."""
     _localdict = self.__dict__
     # We want a field ........
     if indx in ndarray.__getattribute__(self, "dtype").names:
         obj = self._data[indx].view(TimeSeries)
         obj._dates = _localdict["_dates"]
         obj._mask = make_mask(_localdict["_mask"][indx])
         return obj
     # We want some elements ..
     obj = TimeSeries.__getitem__(self, indx)
     if isinstance(obj, MaskedArray) and not isinstance(obj, TimeSeries):
         obj = ndarray.view(obj, MaskedRecords)
     return obj
Example #18
0
 def __getattribute__(self, attr):
     try:
         return object.__getattribute__(self, attr)
     except AttributeError:  # attr must be a fieldname
         pass
     fielddict = ndarray.__getattribute__(self, "dtype").fields
     try:
         res = fielddict[attr][:2]
     except (TypeError, KeyError):
         raise AttributeError, "record array has no attribute %s" % attr
     # So far, so good...
     _localdict = ndarray.__getattribute__(self, "__dict__")
     _data = ndarray.view(self, _localdict["_baseclass"])
     obj = _data.getfield(*res)
     if obj.dtype.fields:
         raise NotImplementedError("MaskedRecords is currently limited to" "simple records...")
     obj = obj.view(MaskedArray)
     obj._baseclass = ndarray
     obj._isfield = True
     # Get some special attributes
     _fill_value = _localdict.get("_fill_value", None)
     _mask = _localdict.get("_mask", None)
     # Reset the object's mask
     if _mask is not None:
         try:
             obj._mask = _mask[attr]
         except IndexError:
             # Couldn't find a mask: use the default (nomask)
             pass
     # Reset the field values
     if _fill_value is not None:
         try:
             obj._fill_value = _fill_value[attr]
         except ValueError:
             obj._fill_value = None
     return obj
Example #19
0
 def __getitem__(self, indx):
     """Returns all the fields sharing the same fieldname base.
 The fieldname base is either `_data` or `_mask`."""
     _localdict = self.__dict__
     # We want a field ........
     if indx in ndarray.__getattribute__(self, 'dtype').names:
         obj = self._data[indx].view(TimeSeries)
         obj._dates = _localdict['_dates']
         obj._mask = make_mask(_localdict['_mask'][indx])
         return obj
     # We want some elements ..
     obj = TimeSeries.__getitem__(self, indx)
     if isinstance(obj, MaskedArray) and not isinstance(obj, TimeSeries):
         obj = ndarray.view(obj, MaskedRecords)
     return obj
Example #20
0
    def _getmask(self):
        """Return the mask of the mrecord.
    A record is masked when all the fields are masked.

        """
        fieldmask = ndarray.__getattribute__(self, '_fieldmask')
        if fieldmask.size > 1:
            axis = 1
        else:
            axis=None
        try:
            return fieldmask.view((bool_, len(self.dtype))).all(axis)
        except ValueError:
            return np.all([[f[n].all() for n in fieldmask.dtype.names]
                           for f in fieldmask], axis=axis)
Example #21
0
    def _getmask(self):
        """Return the mask of the mrecord.
    A record is masked when all the fields are masked.

        """
        fieldmask = ndarray.__getattribute__(self, '_fieldmask')
        if fieldmask.size > 1:
            axis = 1
        else:
            axis = None
        try:
            return fieldmask.view((bool_, len(self.dtype))).all(axis)
        except ValueError:
            return np.all([[f[n].all() for n in fieldmask.dtype.names]
                           for f in fieldmask],
                          axis=axis)
Example #22
0
 def __getattribute__(self, name):
     if name == "x":
         val = self[0]
     elif name == "y":
         val = self[1]
     elif name == "z":
         val = self[2]
     elif name == "mag":
         val = sqrt(self[0]**2 + self[1]**2 + self[2]**2)
     elif name == "mag2":
         val = self[0]**2 + self[1]**2 + self[2]**2
     else:
         return ndarray.__getattribute__(self, name)
     
     log.debug("getattr", extra={'class':'vector', 'object':self, 'attr':name, 'value':val})
     return val
Example #23
0
 def __array_finalize__(self, obj):
     # Make sure we have a _fieldmask by default ..
     _mask = getattr(obj, "_mask", None)
     if _mask is None:
         objmask = getattr(obj, "_mask", nomask)
         _dtype = ndarray.__getattribute__(self, "dtype")
         if objmask is nomask:
             _mask = ma.make_mask_none(self.shape, dtype=_dtype)
         else:
             mdescr = ma.make_mask_descr(_dtype)
             _mask = narray([tuple([m] * len(mdescr)) for m in objmask], dtype=mdescr).view(recarray)
     # Update some of the attributes
     _dict = self.__dict__
     _dict.update(_mask=_mask, _fieldmask=_mask)
     self._update_from(obj)
     if _dict["_baseclass"] == ndarray:
         _dict["_baseclass"] = recarray
     return
Example #24
0
 def __array_finalize__(self, obj):
     # Make sure we have a _fieldmask by default
     _mask = getattr(obj, '_mask', None)
     if _mask is None:
         objmask = getattr(obj, '_mask', nomask)
         _dtype = ndarray.__getattribute__(self, 'dtype')
         if objmask is nomask:
             _mask = ma.make_mask_none(self.shape, dtype=_dtype)
         else:
             mdescr = ma.make_mask_descr(_dtype)
             _mask = narray([tuple([m] * len(mdescr)) for m in objmask],
                            dtype=mdescr).view(recarray)
     # Update some of the attributes
     _dict = self.__dict__
     _dict.update(_mask=_mask)
     self._update_from(obj)
     if _dict['_baseclass'] == ndarray:
         _dict['_baseclass'] = recarray
     return