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

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

项目:sims_featureScheduler    作者:lsst    | 项目源码 | 文件源码
def update_conditions(self, conditions):
        """
        Parameters
        ----------
        conditions : dict
            Keys should include airmass, sky_brightness, seeing.
        """
        m5 = np.empty(conditions['skybrightness'][self.filtername].size)
        m5.fill(hp.UNSEEN)
        m5_mask = np.zeros(m5.size, dtype=bool)
        m5_mask[np.where(conditions['skybrightness'][self.filtername] == hp.UNSEEN)] = True
        good = np.where(conditions['skybrightness'][self.filtername] != hp.UNSEEN)
        m5[good] = m5_flat_sed(self.filtername, conditions['skybrightness'][self.filtername][good],
                               conditions['FWHMeff_%s' % self.filtername][good],
                               self.expTime, conditions['airmass'][good])
        self.feature = m5
        self.feature[m5_mask] = hp.UNSEEN
        self.feature = hp.ud_grade(self.feature, nside_out=self.nside)
        self.feature = ma.masked_values(self.feature, hp.UNSEEN)
项目:stance-conditional    作者:sheffieldnlp    | 项目源码 | 文件源码
def __call__(self, sess, epoch, iteration, model, loss):
        if iteration == 0 and epoch % self.at_every_epoch == 0:
            total = 0
            total_old = 0
            correct_old = 0
            correct = 0
            for values in self.batcher:
                total_old += len(values[-1])
                feed_dict = {}
                for i in range(0, len(self.placeholders)):
                    feed_dict[self.placeholders[i]] = values[i]
                truth = np.argmax(values[-1], 1)

                # mask truth
                truth_noneutral = ma.masked_values(truth, 0)
                truth_noneutral_compr = truth_noneutral.compressed()

                predicted = sess.run(tf.arg_max(tf.nn.softmax(model), 1),
                                     feed_dict=feed_dict)

                pred_nonneutral = ma.array(predicted, mask=truth_noneutral.mask)
                pred_nonneutral_compr = pred_nonneutral.compressed()

                correct_old += sum(truth == predicted)
                correct += sum(truth_noneutral_compr == pred_nonneutral_compr)
                total += len(truth_noneutral_compr)

            acc = float(correct) / total
            self.update_summary(sess, iteration, "AccurayNonNeut", acc)
            print("Epoch " + str(epoch) +
                  "\tAccNonNeut " + str(acc) +
                  "\tCorrect " + str(correct) + "\tTotal " + str(total))
            return acc
        return 0.0
项目:GDAL_Python3    作者:jdegene    | 项目源码 | 文件源码
def histo(input1,inBins=100, inRange=None, inNormed=False, inWeights=None, inDensity=None,
          NoDataValue = None, draw = True):

    #test if input is a string (filepath) or already numpy array
    if type(input1) == str:
        array = singleTifToArray(input1)
    else:
        array = input1

    array2 = ma.masked_values(array, NoDataValue)
    pixNum = array2.count() #size on masked arrays would still return masked pixels

    # returns tuple of 2 arrays, one with unique raster values and of with corresponding count
    unique = np.unique(array2.compressed()) #array of unique values
    uniqueMax = unique.max() #maximum value

    array1D = array2.compressed()

    #To draw histograms with values smaller one or more than 256 use 100 bins by default
    if uniqueMax <= 1 or uniqueMax >= 256:
        inBins = inBins
    else:
        inBins = range(1,int(uniqueMax)+2)


    h = np.histogram(array2.compressed(),bins=inBins, normed=inNormed, 
                        weights=inWeights, density=inDensity)

    #Returns graphic histogram by standard
    if draw:
        plt.hist(array1D,bins=inBins, range = inRange, normed = inNormed, weights = inWeights)


    #plt.show()

    print("Total number of pixel (noData included): ", array2.size, 
                                "(noData excluded): ", pixNum)

    #return total pix number, masked pix number, and tuple of histogram array    
    return array2.size,pixNum, h


#########################################################################################
# Create a mask from vector and convert vectors to raster
#########################################################################################
项目:PaleoView    作者:GlobalEcologyLab    | 项目源码 | 文件源码
def hexbin(self,x,y,**kwargs):
        """
        Make a hexagonal binning plot of x versus y, where x, y are 1-D
        sequences of the same length, N. If C is None (the default), this is a
        histogram of the number of occurences of the observations at
        (x[i],y[i]).

        If C is specified, it specifies values at the coordinate (x[i],y[i]).
        These values are accumulated for each hexagonal bin and then reduced
        according to reduce_C_function, which defaults to the numpy mean function
        (np.mean). (If C is specified, it must also be a 1-D sequence of the
        same length as x and y.)

        x, y and/or C may be masked arrays, in which case only unmasked points
        will be plotted.

        (see matplotlib.pyplot.hexbin documentation).

        Extra keyword ``ax`` can be used to override the default axis instance.

        Other \**kwargs passed on to matplotlib.pyplot.hexbin
        """
        ax, plt = self._ax_plt_from_kw(kwargs)
        # allow callers to override the hold state by passing hold=True|False
        b = ax.ishold()
        h = kwargs.pop('hold',None)
        if h is not None:
            ax.hold(h)
        try:
            # make x,y masked arrays
            # (masked where data is outside of projection limb)
            x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
            y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
            ret = ax.hexbin(x,y,**kwargs)
        except:
            ax.hold(b)
            raise
        ax.hold(b)
        # reset current active image (only if pyplot is imported).
        if plt:
            plt.sci(ret)
        # clip for round polar plots.
        if self.round: ret,c = self._clipcircle(ax,ret)
        # set axes limits to fit map region.
        self.set_axes_limits(ax=ax)
        return ret
项目:PaleoView    作者:GlobalEcologyLab    | 项目源码 | 文件源码
def hexbin(self,x,y,**kwargs):
        """
        Make a hexagonal binning plot of x versus y, where x, y are 1-D
        sequences of the same length, N. If C is None (the default), this is a
        histogram of the number of occurences of the observations at
        (x[i],y[i]).

        If C is specified, it specifies values at the coordinate (x[i],y[i]).
        These values are accumulated for each hexagonal bin and then reduced
        according to reduce_C_function, which defaults to the numpy mean function
        (np.mean). (If C is specified, it must also be a 1-D sequence of the
        same length as x and y.)

        x, y and/or C may be masked arrays, in which case only unmasked points
        will be plotted.

        (see matplotlib.pyplot.hexbin documentation).

        Extra keyword ``ax`` can be used to override the default axis instance.

        Other \**kwargs passed on to matplotlib.pyplot.hexbin
        """
        ax, plt = self._ax_plt_from_kw(kwargs)
        # allow callers to override the hold state by passing hold=True|False
        b = ax.ishold()
        h = kwargs.pop('hold',None)
        if h is not None:
            ax.hold(h)
        try:
            # make x,y masked arrays
            # (masked where data is outside of projection limb)
            x = ma.masked_values(np.where(x > 1.e20,1.e20,x), 1.e20)
            y = ma.masked_values(np.where(y > 1.e20,1.e20,y), 1.e20)
            ret = ax.hexbin(x,y,**kwargs)
        except:
            ax.hold(b)
            raise
        ax.hold(b)
        # reset current active image (only if pyplot is imported).
        if plt:
            plt.sci(ret)
        # clip for round polar plots.
        if self.round: ret,c = self._clipcircle(ax,ret)
        # set axes limits to fit map region.
        self.set_axes_limits(ax=ax)
        return ret
项目:oceansdb    作者:castelao    | 项目源码 | 文件源码
def woa_profile_from_dap(var, d, lat, lon, depth, cfg):
    """
    Monthly Climatologic Mean and Standard Deviation from WOA,
    used either for temperature or salinity.

    INPUTS
        time: [day of the year]
        lat: [-90<lat<90]
        lon: [-180<lon<180]
        depth: [meters]

    Reads the WOA Monthly Climatology NetCDF file and
    returns the corresponding WOA values of salinity or temperature mean and
    standard deviation for the given time, lat, lon, depth.
    """
    if lon < 0:
        lon = lon+360

    url = cfg['url']

    doy = int(d.strftime('%j'))
    dataset = open_url(url)

    dn = (np.abs(doy-dataset['time'][:])).argmin()
    xn = (np.abs(lon-dataset['lon'][:])).argmin()
    yn = (np.abs(lat-dataset['lat'][:])).argmin()

    if re.match("temperature\d?$", var):
        mn = ma.masked_values(dataset.t_mn.t_mn[dn, :, yn, xn].reshape(
            dataset['depth'].shape[0]), dataset.t_mn.attributes['_FillValue'])
        sd = ma.masked_values(dataset.t_sd.t_sd[dn, :, yn, xn].reshape(
            dataset['depth'].shape[0]), dataset.t_sd.attributes['_FillValue'])
        # se = ma.masked_values(dataset.t_se.t_se[dn, :, yn, xn].reshape(
        #    dataset['depth'].shape[0]), dataset.t_se.attributes['_FillValue'])
        # Use this in the future. A minimum # of samples
        # dd = ma.masked_values(dataset.t_dd.t_dd[dn, :, yn, xn].reshape(
        #    dataset['depth'].shape[0]), dataset.t_dd.attributes['_FillValue'])
    elif re.match("salinity\d?$", var):
        mn = ma.masked_values(dataset.s_mn.s_mn[dn, :, yn, xn].reshape(
            dataset['depth'].shape[0]), dataset.s_mn.attributes['_FillValue'])
        sd = ma.masked_values(dataset.s_sd.s_sd[dn, :, yn, xn].reshape(
            dataset['depth'].shape[0]), dataset.s_sd.attributes['_FillValue'])
        # dd = ma.masked_values(dataset.s_dd.s_dd[dn, :, yn, xn].reshape(
        #    dataset['depth'].shape[0]), dataset.s_dd.attributes['_FillValue'])
    zwoa = ma.array(dataset.depth[:])

    ind = (depth <= zwoa.max()) & (depth >= zwoa.min())
    # Mean value profile
    f = interp1d(zwoa[~ma.getmaskarray(mn)].compressed(), mn.compressed())
    mn_interp = ma.masked_all(depth.shape)
    mn_interp[ind] = f(depth[ind])
    # The stdev profile
    f = interp1d(zwoa[~ma.getmaskarray(sd)].compressed(), sd.compressed())
    sd_interp = ma.masked_all(depth.shape)
    sd_interp[ind] = f(depth[ind])

    output = {'woa_an': mn_interp, 'woa_sd': sd_interp}

    return output