Python numpy.ma 模块,masked_invalid() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用numpy.ma.masked_invalid()

项目:goodman    作者:soar-telescope    | 项目源码 | 文件源码
def remove_background_by_median(ccd, plots=False):
    """Remove Background of a ccd spectrum image

    Notes:
        This function works well for images without strong sky lines. Or for
        targets embedded in extended sources.

    Args:
        ccd (object): A ccdproc.CCDData instance.

    Returns:
        ccd (object): The modified ccdproc.CCDData instance.

    """
    new_ccd = ccd.copy()
    data = ma.masked_invalid(new_ccd.data)
    # x, y = ccd.data.shape
    median = ma.median(data, axis=0)

    data -= median
    data.set_fill_value(-np.inf)
    new_ccd.data = data.filled()

    # ccd.write('/user/simon/dummy_{:d}.fits'.format(g), clobber=True)
    return new_ccd
项目:operalib    作者:operalib    | 项目源码 | 文件源码
def __call__(self, coefs, ground_truth, Gram,
                 weight=None, zeronan=None):
        """Compute the Empirical OVK ridge risk.

        Parameters
        ----------
        coefs : {vector-like}, shape = [n_samples1 * n_targets]
            Coefficient to optimise

        ground_truth : {vector-like}
            Targets samples

        Gram : {LinearOperator}
            Gram matrix acting on the coefs

        weight: {LinearOperator}

        zeronan: {LinearOperator}

        Returns
        -------
        float : Empirical OVK ridge risk
        """
        np = ground_truth.size
        pred = Gram * coefs
        reg = inner(coefs, pred)  # reg in rkhs
        vgt = masked_invalid(ground_truth)
        vgt[where(vgt.mask)] = pred[where(vgt.mask)]
        if weight is None or zeronan is None:
            obj = norm(pred - vgt) ** 2 / (2 * np)
        else:
            wpred = weight * pred  # sup x identity | unsup x lbda_m x L
            res = zeronan * (wpred - vgt)
            wip = wpred - zeronan * wpred  # only unsup part of wpred
            lap = inner(wip, pred)  # Laplacian part x lambda_m

            obj = norm(zeronan * res) ** 2 / (2 * np)  # Loss
            obj += lap / (2 * np)  # Laplacian regularization
        obj += self.lbda * reg / (2 * np)  # Regulariation
        return obj
项目:operalib    作者:operalib    | 项目源码 | 文件源码
def functional_grad(self, coefs, ground_truth, Gram,
                        weight=None, zeronan=None):
        """Compute the gradient of the Empirical OVK ridge risk.

        Parameters
        ----------
        coefs : {vector-like}, shape = [n_samples1 * n_targets]
            Coefficient to optimise

        ground_truth : {vector-like}
            Targets samples

        Gram : {LinearOperator}
            Gram matrix acting on the coefs

        weight: {LinearOperator}

        zeronan: {LinearOperator}

        Returns
        -------
        {vector-like} : gradient of the Empirical OVK ridge risk
        """
        np = ground_truth.size
        pred = Gram * coefs
        vgt = masked_invalid(ground_truth)
        vgt[where(vgt.mask)] = pred[where(vgt.mask)]
        if weight is None or zeronan is None:
            res = pred - vgt
        else:
            res = weight * pred - zeronan * vgt
        return Gram * res / np + self.lbda * pred / np
项目:operalib    作者:operalib    | 项目源码 | 文件源码
def functional_grad_val(self, coefs, ground_truth, Gram,
                            weight=None, zeronan=None):
        """Compute the gradient and value of the Empirical OVK ridge risk.

        Parameters
        ----------
        coefs : {vector-like}, shape = [n_samples1 * n_targets]
            Coefficient to optimise

        ground_truth : {vector-like}
            Targets samples

        Gram : {LinearOperator}
            Gram matrix acting on the coefs

        L : array, shape = [n_samples_miss, n_samples_miss]
            Graph Laplacian of data with missing targets (semi-supervised
            learning).

        Returns
        -------
        Tuple{float, vector-like} : Empirical OVK ridge risk and its gradient
        returned as a tuple.
        """
        np = ground_truth.size
        pred = Gram * coefs
        vgt = masked_invalid(ground_truth)
        vgt[where(vgt.mask)] = pred[where(vgt.mask)]
        reg = inner(coefs, pred)  # reg in rkhs
        if weight is None or zeronan is None:
            res = pred - vgt
            obj = norm(res) ** 2 / (2 * np)
        else:
            wpred = weight * pred  # sup x identity | unsup x lbda_m x L
            res = wpred - zeronan * vgt
            wip = wpred - zeronan * wpred  # only unsup part of wpred
            lap = inner(wip, pred)  # Laplacian part x lambda_m

            obj = norm(zeronan * res) ** 2 / (2 * np)  # Loss
            obj += lap / (2 * np)  # Laplacian regularization
        obj += self.lbda * reg / (2 * np)  # Regulariation
        return obj, Gram * res / np + self.lbda * pred / np
项目:wrf-python    作者:NCAR    | 项目源码 | 文件源码
def to_np(array):
    """Return the :class:`numpy.ndarray` contained in an 
    :class:`xarray.DataArray` instance.

    If the :class:`xarray.DataArray` instance does not contain a *_FillValue* 
    or *missing_value* attribute, then this routine simply returns the 
    :attr:`xarray.DataArray.values` attribute.  If the 
    :class:`xarray.DataArray` object contains a *_FillValue* or *missing_value* 
    attribute, then this routine returns a :class:`numpy.ma.MaskedArray` 
    instance, where the NaN values (used by xarray to represent missing data) 
    are replaced with the fill value.

    If the object passed in to this routine is not an 
    :class:`xarray.DataArray` instance, then this routine simply returns the
    passed in object.  This is useful in situations where you do not know 
    if you have an :class:`xarray.DataArray` or a :class:`numpy.ndarray` and 
    simply want a :class:`numpy.ndarray` returned.  

    Args:

        array (:class:`xarray.DataArray`, :class:`numpy.ndarray`, or any \
        object): Can be any object type, but is generally 
            used with :class:`xarray.DataArray` or :class:`numpy.ndarray`.

    Returns:

        :class:`numpy.ndarray` or :class:`numpy.ma.MaskedArray`: The 
        extracted array or the *array* object if *array* is not a 
        :class:`xarray.DataArray` object..

    """

    try:
        fill_value = array.attrs["_FillValue"]
    except AttributeError:
        result = array # Not a DataArray
    except KeyError:
        result = array.values # Does not have missing values
    else:
        result = ma.masked_invalid(array.values, copy=False)
        result.set_fill_value(fill_value)

    return result


# Helper utilities for metadata