Python numpy.ma.MaskedArray() Examples

The following are 30 code examples of numpy.ma.MaskedArray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy.ma , or try the search function .
Example #1
Source File: transforms.py    From matplotlib-4-abaqus with MIT License 6 votes vote down vote up
def transform_non_affine(self, points):
        if self._x.is_affine and self._y.is_affine:
            return points
        x = self._x
        y = self._y

        if x == y and x.input_dims == 2:
            return x.transform_non_affine(points)

        if x.input_dims == 2:
            x_points = x.transform_non_affine(points)[:, 0:1]
        else:
            x_points = x.transform_non_affine(points[:, 0])
            x_points = x_points.reshape((len(x_points), 1))

        if y.input_dims == 2:
            y_points = y.transform_non_affine(points)[:, 1:]
        else:
            y_points = y.transform_non_affine(points[:, 1])
            y_points = y_points.reshape((len(y_points), 1))

        if isinstance(x_points, MaskedArray) or isinstance(y_points, MaskedArray):
            return ma.concatenate((x_points, y_points), 1)
        else:
            return np.concatenate((x_points, y_points), 1) 
Example #2
Source File: masks.py    From argos with GNU General Public License v3.0 6 votes vote down vote up
def fillValuesToNan(masked_array):
    """ Replaces the fill_values of the masked array by NaNs

        If the array is None or it does not contain floating point values, it cannot contain NaNs.
        In that case the original array is returned.
    """
    if masked_array is not None and masked_array.dtype.kind == 'f':
        check_class(masked_array, ma.masked_array)
        logger.debug("Replacing fill_values by NaNs")
        masked_array[:] = ma.filled(masked_array, np.nan)
        masked_array.set_fill_value(np.nan)
    else:
        return masked_array


#TODO: does recordMask help here?
# https://docs.scipy.org/doc/numpy/reference/maskedarray.baseclass.html#numpy.ma.MaskedArray.recordmask 
Example #3
Source File: mstats_extras.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def hdmedian(data, axis=-1, var=False):
    """
    Returns the Harrell-Davis estimate of the median along the given axis.

    Parameters
    ----------
    data : ndarray
        Data array.
    axis : int, optional
        Axis along which to compute the quantiles. If None, use a flattened
        array.
    var : bool, optional
        Whether to return the variance of the estimate.

    Returns
    -------
    hdmedian : MaskedArray
        The median values.  If ``var=True``, the variance is returned inside
        the masked array.  E.g. for a 1-D array the shape change from (1,) to
        (2,).

    """
    result = hdquantiles(data,[0.5], axis=axis, var=var)
    return result.squeeze() 
Example #4
Source File: masks.py    From argos with GNU General Public License v3.0 6 votes vote down vote up
def createFromMaskedArray(cls, masked_arr):
        """ Creates an ArrayWithMak

            :param masked_arr: a numpy MaskedArray or numpy array
            :return: ArrayWithMask
        """
        if isinstance(masked_arr, ArrayWithMask):
            return masked_arr

        check_class(masked_arr, (np.ndarray, ma.MaskedArray))

        # A MaskedConstant (i.e. masked) is a special case of MaskedArray. It does not seem to have
        # a fill_value so we use None to use the default.
        # https://docs.scipy.org/doc/numpy/reference/maskedarray.baseclass.html#numpy.ma.masked
        fill_value = getattr(masked_arr, 'fill_value', None)

        return cls(masked_arr.data, masked_arr.mask, fill_value) 
Example #5
Source File: test_mstats_basic.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_1D(self):
        a = (1,2,3,4)
        actual = mstats.gmean(a)
        desired = np.power(1*2*3*4,1./4.)
        assert_almost_equal(actual, desired, decimal=14)

        desired1 = mstats.gmean(a,axis=-1)
        assert_almost_equal(actual, desired1, decimal=14)
        assert_(not isinstance(desired1, ma.MaskedArray))

        a = ma.array((1,2,3,4),mask=(0,0,0,1))
        actual = mstats.gmean(a)
        desired = np.power(1*2*3,1./3.)
        assert_almost_equal(actual, desired,decimal=14)

        desired1 = mstats.gmean(a,axis=-1)
        assert_almost_equal(actual, desired1, decimal=14) 
Example #6
Source File: nsview.py    From spyder-kernels with MIT License 6 votes vote down vote up
def get_size(item):
    """Return size of an item of arbitrary type"""
    if isinstance(item, (list, set, tuple, dict)):
        return len(item)
    elif isinstance(item, (ndarray, MaskedArray)):
        return item.shape
    elif isinstance(item, Image):
        return item.size
    if isinstance(item, (DataFrame, Index, Series)):
        try:
            return item.shape
        except RecursionError:
            # This is necessary to avoid an error when trying to
            # get the shape of these objects.
            # Fixes spyder-ide/spyder-kernels#217
            return (-1, -1)
    else:
        return 1 
Example #7
Source File: nsview.py    From spyder-kernels with MIT License 6 votes vote down vote up
def get_human_readable_type(item):
    """Return human-readable type string of an item"""
    if isinstance(item, (ndarray, MaskedArray)):
        return u'Array of ' + item.dtype.name
    elif isinstance(item, Image):
        return "Image"
    else:
        text = get_type_string(item)
        if text is None:
            text = to_text_string('Unknown')
        else:
            return text[text.find('.')+1:]


#==============================================================================
# Globals filter: filter namespace dictionaries (to be edited in
# CollectionsEditor)
#============================================================================== 
Example #8
Source File: transforms.py    From Computable with MIT License 6 votes vote down vote up
def transform_non_affine(self, points):
        if self._x.is_affine and self._y.is_affine:
            return points
        x = self._x
        y = self._y

        if x == y and x.input_dims == 2:
            return x.transform_non_affine(points)

        if x.input_dims == 2:
            x_points = x.transform_non_affine(points)[:, 0:1]
        else:
            x_points = x.transform_non_affine(points[:, 0])
            x_points = x_points.reshape((len(x_points), 1))

        if y.input_dims == 2:
            y_points = y.transform_non_affine(points)[:, 1:]
        else:
            y_points = y.transform_non_affine(points[:, 1])
            y_points = y_points.reshape((len(y_points), 1))

        if isinstance(x_points, MaskedArray) or isinstance(y_points, MaskedArray):
            return ma.concatenate((x_points, y_points), 1)
        else:
            return np.concatenate((x_points, y_points), 1) 
Example #9
Source File: scale.py    From neural-network-animation with MIT License 6 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * self.base)
        if isinstance(a, ma.MaskedArray):
            return ma.log(a) / np.log(self.base)
        return np.log(a) / np.log(self.base) 
Example #10
Source File: transforms.py    From neural-network-animation with MIT License 6 votes vote down vote up
def transform_non_affine(self, points):
        if self._x.is_affine and self._y.is_affine:
            return points
        x = self._x
        y = self._y

        if x == y and x.input_dims == 2:
            return x.transform_non_affine(points)

        if x.input_dims == 2:
            x_points = x.transform_non_affine(points)[:, 0:1]
        else:
            x_points = x.transform_non_affine(points[:, 0])
            x_points = x_points.reshape((len(x_points), 1))

        if y.input_dims == 2:
            y_points = y.transform_non_affine(points)[:, 1:]
        else:
            y_points = y.transform_non_affine(points[:, 1])
            y_points = y_points.reshape((len(y_points), 1))

        if isinstance(x_points, MaskedArray) or isinstance(y_points, MaskedArray):
            return ma.concatenate((x_points, y_points), 1)
        else:
            return np.concatenate((x_points, y_points), 1) 
Example #11
Source File: datatypes.py    From pixelworld with MIT License 6 votes vote down vote up
def view_field(self, name, type=None):
        """construct a view of one data field
        
        Parameters
        ----------
        name : string
            the name of the field
        type : type, optional
            the type of the returned array
        
        Returns
        -------
        view : MaskedArray
            a view of the specified field
        """
        view = self.data[name]
        
        if type is not None:
            return view.view(type=type)
        else:
            return view 
Example #12
Source File: test_mrecords.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_view_simple_dtype(self):
        (mrec, a, b, arr) = self.data
        ntype = (float, 2)
        test = mrec.view(ntype)
        assert_(isinstance(test, ma.MaskedArray))
        assert_equal(test, np.array(list(zip(a, b)), dtype=float))
        assert_(test[3, 1] is ma.masked) 
Example #13
Source File: test_recfunctions.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_join_subdtype(self):
        # tests the bug in https://stackoverflow.com/q/44769632/102441
        from numpy.lib import recfunctions as rfn
        foo = np.array([(1,)],
                       dtype=[('key', int)])
        bar = np.array([(1, np.array([1,2,3]))],
                       dtype=[('key', int), ('value', 'uint16', 3)])
        res = join_by('key', foo, bar)
        assert_equal(res, bar.view(ma.MaskedArray)) 
Example #14
Source File: recfunctions.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def _fix_output(output, usemask=True, asrecarray=False):
    """
    Private function: return a recarray, a ndarray, a MaskedArray
    or a MaskedRecords depending on the input parameters
    """
    if not isinstance(output, MaskedArray):
        usemask = False
    if usemask:
        if asrecarray:
            output = output.view(MaskedRecords)
    else:
        output = ma.filled(output)
        if asrecarray:
            output = output.view(recarray)
    return output 
Example #15
Source File: recfunctions.py    From pySINDy with MIT License 5 votes vote down vote up
def _fix_output(output, usemask=True, asrecarray=False):
    """
    Private function: return a recarray, a ndarray, a MaskedArray
    or a MaskedRecords depending on the input parameters
    """
    if not isinstance(output, MaskedArray):
        usemask = False
    if usemask:
        if asrecarray:
            output = output.view(MaskedRecords)
    else:
        output = ma.filled(output)
        if asrecarray:
            output = output.view(recarray)
    return output 
Example #16
Source File: dc2_matched_table.py    From gcr-catalogs with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_star_array(q, is_star, is_matched):
    mask = _get_star_mask(is_star, is_matched)
    return ma.MaskedArray(q, mask=mask) 
Example #17
Source File: scale.py    From neural-network-animation with MIT License 5 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * 2.0)
        if isinstance(a, ma.MaskedArray):
            return ma.log(a) / np.log(2)
        return np.log2(a) 
Example #18
Source File: scale.py    From neural-network-animation with MIT License 5 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * 10.0)
        if isinstance(a, ma.MaskedArray):
            return ma.log10(a)
        return np.log10(a) 
Example #19
Source File: scale.py    From neural-network-animation with MIT License 5 votes vote down vote up
def _mask_non_positives(a):
    """
    Return a Numpy masked array where all non-positive values are
    masked.  If there are no non-positive values, the original array
    is returned.
    """
    mask = a <= 0.0
    if mask.any():
        return ma.MaskedArray(a, mask=mask)
    return a 
Example #20
Source File: dc2_matched_table.py    From gcr-catalogs with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _generate_quantity_modifiers(self):
        # modify native quantities
        quantity_modifiers = {
            'galaxy_match_mask': (_get_galaxy_mask, self._is_star, self._match_flag),
            'star_match_mask':   (_get_star_mask, self._is_star, self._match_flag),
            'redshift_true_galaxy': (_get_galaxy_array, 'redshift_true', self._is_star, self._match_flag),
        }
        modified_quantity_list = [c for c in self._column_names if self._is_star not in c and self._match_flag not in c and 'redshift' not in c]
        for q in modified_quantity_list:
            #self._quantity_modifiers[q + '_galaxy'] = (lambda x: ma.MaskedArray(x, mask=self._galaxy_match_mask), q)
            #self._quantity_modifiers[q + '_star'] = (lambda x: ma.MaskedArray(x, mask=self._star_match_mask), q)
            quantity_modifiers[q + '_galaxy'] = (_get_galaxy_array, q, self._is_star, self._match_flag)
            quantity_modifiers[q + '_star'] = (_get_star_array, q, self._is_star, self._match_flag)

        return quantity_modifiers 
Example #21
Source File: transforms.py    From neural-network-animation with MIT License 5 votes vote down vote up
def transform_affine(self, points):
        mtx = self.get_matrix()
        if isinstance(points, MaskedArray):
            tpoints = affine_transform(points.data, mtx)
            return ma.MaskedArray(tpoints, mask=ma.getmask(points))
        return affine_transform(points, mtx) 
Example #22
Source File: datatypes.py    From pixelworld with MIT License 5 votes vote down vote up
def _set_data(self, data):
        """set the data array
        
        Parameters
        ----------
        data : MaskedArray
            the new data array
        """
        if isinstance(data, ma.MaskedArray) and not isinstance(data, SerializableMaskedArray):
            self._data = SerializableMaskedArray(data)            
        elif isinstance(data, SerializableMaskedArray):
            self._data = data
        else:
            assert False, 'unknown data type'
        
        #set the fill value of each field
        for field in self.fields:
            #this fails with multi-dimensional fields, in which case we do it
            #the wonky way below
            try:
                self._data[field].set_fill_value(self._null_values[field])
            except TypeError:
                self._data._fill_value[field] = self._null_values[field]
        
        #process the change to the data array
        self._process_data_change() 
Example #23
Source File: rdt.py    From forest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def update_json(props, varname, data, datatype):
    """
    Adds an extra field to the properties of a feature in a geojson file. Provides a consistent way to handle different data types
    :param props: dictionary of properties
    :param varname: string variable name
    :param data: the data to add
    :param datatype: datatype
    :return: updated props
    """

    if isinstance(data, ma.MaskedArray) and (data.shape == ()) and ('int' in str(datatype)):
        props.update({varname: np.int(ma.getdata(data))})

    elif isinstance(data, ma.MaskedArray) and (data.shape == ()) and ('float' in str(datatype)):
        props.update({varname: np.float(ma.getdata(data))})

    elif isinstance(data, np.float32) or isinstance(data, np.float):
        props.update({varname: float(data)})

    elif isinstance(data, np.int) or isinstance(data, np.uint16):
        props.update({varname: int(data)})

    elif str(datatype) == 'string':
        props.update({varname: str(data)})

    else:
        return 
Example #24
Source File: utils.py    From pixelworld with MIT License 5 votes vote down vote up
def ind2sub(shape, idx):
    """like MATLAB's ind2sub: decode linear index values into multiple-subscript
    indices
    
    Parameters
    ----------
    shape : tuple
        the shape of the array
    idx : ndarray
        a linear index array, like that returned by sub2ind
    
    Returns
    -------
    *sub_indices
        the multiple-subscript index arrays encoded by idx
    """
    if isinstance(idx, ma.MaskedArray):
        idx = idx.data
    
    ndim = len(shape)
    
    #initialize the sub_indices list
    sub_indices = [None]*ndim
    
    #decode each dimension
    multiplier = np.prod(shape[:-1])
    for d,sz in enumerate(shape):
        sub_indices[ndim - d - 1] = (idx / multiplier).astype(int)
        idx %= multiplier
        multiplier /= sz
    
    return tuple(sub_indices) 
Example #25
Source File: utils.py    From pixelworld with MIT License 5 votes vote down vote up
def merge_structured_arrays(*arrays):
    """merge a set of structured numpy arrays
    
    Parameters
    ----------
    *arrays
        a set of masked structured arrays. all arrays must have the same number
        of rows.
    
    Returns
    -------
    x : MaskedArray
        the merged array
    """
    #concatenate all of the sub-arrays fields
    dtype = sum((array.dtype.descr for array in arrays), [])
    
    #empty array with all fields
    x = ma.empty(len(arrays[0]), dtype=dtype)
    x = SerializableMaskedArray(x)
    
    #assign each field
    for array in arrays:
        for name in array.dtype.names:
            x[name] = array[name]
    
    return x 
Example #26
Source File: scale.py    From matplotlib-4-abaqus with MIT License 5 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * self.base)
        if isinstance(a, ma.MaskedArray):
            return ma.log(a) / np.log(self.base)
        return np.log(a) / np.log(self.base) 
Example #27
Source File: scale.py    From matplotlib-4-abaqus with MIT License 5 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * 2.0)
        if isinstance(a, ma.MaskedArray):
            return ma.log(a) / np.log(2)
        return np.log2(a) 
Example #28
Source File: scale.py    From matplotlib-4-abaqus with MIT License 5 votes vote down vote up
def transform_non_affine(self, a):
        a = self._handle_nonpos(a * 10.0)
        if isinstance(a, ma.MaskedArray):
            return ma.log10(a)
        return np.log10(a) 
Example #29
Source File: recfunctions.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def _fix_output(output, usemask=True, asrecarray=False):
    """
    Private function: return a recarray, a ndarray, a MaskedArray
    or a MaskedRecords depending on the input parameters
    """
    if not isinstance(output, MaskedArray):
        usemask = False
    if usemask:
        if asrecarray:
            output = output.view(MaskedRecords)
    else:
        output = ma.filled(output)
        if asrecarray:
            output = output.view(recarray)
    return output 
Example #30
Source File: recfunctions.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def _fix_output(output, usemask=True, asrecarray=False):
    """
    Private function: return a recarray, a ndarray, a MaskedArray
    or a MaskedRecords depending on the input parameters
    """
    if not isinstance(output, MaskedArray):
        usemask = False
    if usemask:
        if asrecarray:
            output = output.view(MaskedRecords)
    else:
        output = ma.filled(output)
        if asrecarray:
            output = output.view(recarray)
    return output