本文整理匯總了Python中numpy.ma.masked_where方法的典型用法代碼示例。如果您正苦於以下問題:Python ma.masked_where方法的具體用法?Python ma.masked_where怎麽用?Python ma.masked_where使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.ma
的用法示例。
在下文中一共展示了ma.masked_where方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: transform_non_affine
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def transform_non_affine(self, ll):
longitude = ll[:, 0:1]
latitude = ll[:, 1:2]
# Pre-compute some values
half_long = longitude / 2.0
cos_latitude = np.cos(latitude)
alpha = np.arccos(cos_latitude * np.cos(half_long))
# Mask this array or we'll get divide-by-zero errors
alpha = ma.masked_where(alpha == 0.0, alpha)
# The numerators also need to be masked so that masked
# division will be invoked.
# We want unnormalized sinc. numpy.sinc gives us normalized
sinc_alpha = ma.sin(alpha) / alpha
x = (cos_latitude * ma.sin(half_long)) / sinc_alpha
y = (ma.sin(latitude) / sinc_alpha)
return np.concatenate((x.filled(0), y.filled(0)), 1)
示例2: _contour_args
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def _contour_args(self, args, kwargs):
if self.filled:
fn = 'contourf'
else:
fn = 'contour'
Nargs = len(args)
if Nargs <= 2:
z = ma.asarray(args[0], dtype=np.float64)
x, y = self._initialize_x_y(z)
args = args[1:]
elif Nargs <= 4:
x, y, z = self._check_xyz(args[:3], kwargs)
args = args[3:]
else:
raise TypeError("Too many arguments to %s; see help(%s)" %
(fn, fn))
z = ma.masked_invalid(z, copy=False)
self.zmax = ma.maximum(z)
self.zmin = ma.minimum(z)
if self.logscale and self.zmin <= 0:
z = ma.masked_where(z <= 0, z)
warnings.warn('Log scale: values of z <= 0 have been masked')
self.zmin = z.min()
self._contour_level_args(z, args)
return (x, y, z)
示例3: test_mode
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def test_mode(self):
a1 = [0,0,0,1,1,1,2,3,3,3,3,4,5,6,7]
a2 = np.reshape(a1, (3,5))
a3 = np.array([1,2,3,4,5,6])
a4 = np.reshape(a3, (3,2))
ma1 = ma.masked_where(ma.array(a1) > 2, a1)
ma2 = ma.masked_where(a2 > 2, a2)
ma3 = ma.masked_where(a3 < 2, a3)
ma4 = ma.masked_where(ma.array(a4) < 2, a4)
assert_equal(mstats.mode(a1, axis=None), (3,4))
assert_equal(mstats.mode(a1, axis=0), (3,4))
assert_equal(mstats.mode(ma1, axis=None), (0,3))
assert_equal(mstats.mode(a2, axis=None), (3,4))
assert_equal(mstats.mode(ma2, axis=None), (0,3))
assert_equal(mstats.mode(a3, axis=None), (1,1))
assert_equal(mstats.mode(ma3, axis=None), (2,1))
assert_equal(mstats.mode(a2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(ma2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
assert_equal(mstats.mode(a2, axis=-1), ([[0],[3],[3]], [[3],[3],[1]]))
assert_equal(mstats.mode(ma2, axis=-1), ([[0],[1],[0]], [[3],[1],[0]]))
assert_equal(mstats.mode(ma4, axis=0), ([[3,2]], [[1,1]]))
assert_equal(mstats.mode(ma4, axis=-1), ([[2],[3],[5]], [[1],[1],[1]]))
示例4: test_pcolormesh
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def test_pcolormesh():
n = 12
x = np.linspace(-1.5, 1.5, n)
y = np.linspace(-1.5, 1.5, n*2)
X, Y = np.meshgrid(x, y)
Qx = np.cos(Y) - np.cos(X)
Qz = np.sin(Y) + np.sin(X)
Qx = (Qx + 1.1)
Z = np.sqrt(X**2 + Y**2)/5
Z = (Z - Z.min()) / (Z.max() - Z.min())
# The color array can include masked values:
Zm = ma.masked_where(np.fabs(Qz) < 0.5*np.amax(Qz), Z)
fig = plt.figure()
ax = fig.add_subplot(131)
ax.pcolormesh(Qx, Qz, Z, lw=0.5, edgecolors='k')
ax = fig.add_subplot(132)
ax.pcolormesh(Qx, Qz, Z, lw=2, edgecolors=['b', 'w'])
ax = fig.add_subplot(133)
ax.pcolormesh(Qx, Qz, Z, shading="gouraud")
示例5: _contour_args
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def _contour_args(self, args, kwargs):
if self.filled:
fn = 'contourf'
else:
fn = 'contour'
Nargs = len(args)
if Nargs <= 2:
z = ma.asarray(args[0], dtype=np.float64)
x, y = self._initialize_x_y(z)
args = args[1:]
elif Nargs <= 4:
x, y, z = self._check_xyz(args[:3], kwargs)
args = args[3:]
else:
raise TypeError("Too many arguments to %s; see help(%s)" %
(fn, fn))
z = ma.masked_invalid(z, copy=False)
self.zmax = float(z.max())
self.zmin = float(z.min())
if self.logscale and self.zmin <= 0:
z = ma.masked_where(z <= 0, z)
warnings.warn('Log scale: values of z <= 0 have been masked')
self.zmin = float(z.min())
self._contour_level_args(z, args)
return (x, y, z)
示例6: transform_non_affine
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def transform_non_affine(self, a):
"""
This transform takes an Nx1 ``numpy`` array and returns a
transformed copy. Since the range of the Mercator scale
is limited by the user-specified threshold, the input
array must be masked to contain only valid values.
``matplotlib`` will handle masked arrays and remove the
out-of-range data from the plot. Importantly, the
``transform`` method *must* return an array that is the
same shape as the input array, since these values need to
remain synchronized with values in the other dimension.
"""
masked = ma.masked_where((a < -self.thresh) | (a > self.thresh), a)
if masked.mask.any():
return ma.log(np.abs(ma.tan(masked) + 1.0 / ma.cos(masked)))
else:
return np.log(np.abs(np.tan(a) + 1.0 / np.cos(a)))
示例7: test_pcolormesh
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def test_pcolormesh():
n = 12
x = np.linspace(-1.5, 1.5, n)
y = np.linspace(-1.5, 1.5, n*2)
X, Y = np.meshgrid(x, y)
Qx = np.cos(Y) - np.cos(X)
Qz = np.sin(Y) + np.sin(X)
Qx = (Qx + 1.1)
Z = np.hypot(X, Y) / 5
Z = (Z - Z.min()) / Z.ptp()
# The color array can include masked values:
Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.pcolormesh(Qx, Qz, Z, lw=0.5, edgecolors='k')
ax2.pcolormesh(Qx, Qz, Z, lw=2, edgecolors=['b', 'w'])
ax3.pcolormesh(Qx, Qz, Z, shading="gouraud")
示例8: _plot_well_traj
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def _plot_well_traj(self, zvals, hvals):
"""Plot the trajectory as a black line"""
zvals_copy = ma.masked_where(zvals < self._zmin, zvals)
hvals_copy = ma.masked_where(zvals < self._zmin, hvals)
self._figure.add_trace(
{
"x": hvals_copy,
"y": zvals_copy,
"name": self._well.name,
"marker": {"color": "black"},
},
self.main_trace_row,
1,
)
# ax.plot(hvals_copy, zvals_copy, linewidth=6, c="black")
# pylint: disable-too-many-locals
示例9: mask_tiles_to_bbox
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def mask_tiles_to_bbox(self, min_lat, max_lat, min_lon, max_lon, tiles):
for tile in tiles:
tile.latitudes = ma.masked_outside(tile.latitudes, min_lat, max_lat)
tile.longitudes = ma.masked_outside(tile.longitudes, min_lon, max_lon)
# Or together the masks of the individual arrays to create the new mask
data_mask = ma.getmaskarray(tile.times)[:, np.newaxis, np.newaxis] \
| ma.getmaskarray(tile.latitudes)[np.newaxis, :, np.newaxis] \
| ma.getmaskarray(tile.longitudes)[np.newaxis, np.newaxis, :]
tile.data = ma.masked_where(data_mask, tile.data)
tiles[:] = [tile for tile in tiles if not tile.data.mask.all()]
return tiles
示例10: mask_tiles_to_bbox_and_time
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def mask_tiles_to_bbox_and_time(self, min_lat, max_lat, min_lon, max_lon, start_time, end_time, tiles):
for tile in tiles:
tile.times = ma.masked_outside(tile.times, start_time, end_time)
tile.latitudes = ma.masked_outside(tile.latitudes, min_lat, max_lat)
tile.longitudes = ma.masked_outside(tile.longitudes, min_lon, max_lon)
# Or together the masks of the individual arrays to create the new mask
data_mask = ma.getmaskarray(tile.times)[:, np.newaxis, np.newaxis] \
| ma.getmaskarray(tile.latitudes)[np.newaxis, :, np.newaxis] \
| ma.getmaskarray(tile.longitudes)[np.newaxis, np.newaxis, :]
tile.data = ma.masked_where(data_mask, tile.data)
tiles[:] = [tile for tile in tiles if not tile.data.mask.all()]
return tiles
示例11: mask_tiles_to_time_range
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def mask_tiles_to_time_range(self, start_time, end_time, tiles):
"""
Masks data in tiles to specified time range.
:param start_time: The start time to search for tiles
:param end_time: The end time to search for tiles
:param tiles: List of tiles
:return: A list tiles with data masked to specified time range
"""
if 0 < start_time <= end_time:
for tile in tiles:
tile.times = ma.masked_outside(tile.times, start_time, end_time)
# Or together the masks of the individual arrays to create the new mask
data_mask = ma.getmaskarray(tile.times)[:, np.newaxis, np.newaxis] \
| ma.getmaskarray(tile.latitudes)[np.newaxis, :, np.newaxis] \
| ma.getmaskarray(tile.longitudes)[np.newaxis, np.newaxis, :]
tile.data = ma.masked_where(data_mask, tile.data)
tiles[:] = [tile for tile in tiles if not tile.data.mask.all()]
return tiles
示例12: test_mem_masked_where
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def test_mem_masked_where(self):
# Ticket #62
from numpy.ma import masked_where, MaskType
a = np.zeros((1, 1))
b = np.zeros(a.shape, MaskType)
c = masked_where(b, a)
a-c
示例13: PlotRaster
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def PlotRaster(RasterFile, Map, alpha=1.):
"""
Description goes here...
MDH
"""
print "Plotting raster..."
#Read data
gdata = gdal.Open(RasterFile, gdal.GA_ReadOnly)
geo = gdata.GetGeoTransform()
Data = gdata.ReadAsArray()
# make topodat a masked array, masking values lower than sea level.
Data = ma.masked_where(Data < 0, Data)
#setup meshgrid for raster plotting
xres = geo[1]
yres = geo[5]
xmin = geo[0] + xres * 0.5
xmax = geo[0] + (xres * gdata.RasterXSize) - xres * 0.5
ymin = geo[3] + (yres * gdata.RasterYSize) + yres * 0.5
ymax = geo[3] - yres * 0.5
x,y = np.mgrid[xmin:xmax+xres:xres, ymax+yres:ymin:yres]
x,y = Map(x,y)
Map.pcolormesh(x, y, Data.T, cmap=plt.cm.Greys, alpha=alpha)
示例14: __init__
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def __init__(self,vals,vals_dmin,vals_dmax,mask=ma.nomask):
super(UncertContainer, self).__init__()
# If input data already masked arrays extract unmasked data
if ma.isMaskedArray(vals):
vals = vals.data
if ma.isMaskedArray(vals_dmin):
vals_dmin = vals_dmin.data
if ma.isMaskedArray(vals_dmax):
vals_dmax = vals_dmax.data
# Adjust negative values
ineg = np.where(vals_dmin <= 0.0)
vals_dmin[ineg] = TOL*vals[ineg]
# Calculate weight based on fractional uncertainty
diff = vals_dmax - vals_dmin
diff_m = ma.masked_where(vals_dmax == vals_dmin,diff)
self.vals = ma.masked_where(vals == 0.0,vals)
self.wt = (self.vals/diff_m)**2
self.uncert = diff_m/self.vals
self.wt.fill_value = np.inf
self.uncert.fill_vaule = np.inf
assert np.all(self.wt.mask == self.uncert.mask)
# Mask data if uncertainty is not finite or if any of the inputs were
# already masked
mm = ma.mask_or(self.wt.mask,mask)
self.vals.mask = mm
self.wt.mask = mm
self.uncert.mask = mm
self.dmin = ma.array(vals_dmin,mask=mm,fill_value=np.inf)
self.dmax = ma.array(vals_dmax,mask=mm,fill_value=np.inf)
self.mask = ma.getmaskarray(self.vals)
示例15: test_mem_masked_where
# 需要導入模塊: from numpy import ma [as 別名]
# 或者: from numpy.ma import masked_where [as 別名]
def test_mem_masked_where(self,level=rlevel):
# Ticket #62
from numpy.ma import masked_where, MaskType
a = np.zeros((1, 1))
b = np.zeros(a.shape, MaskType)
c = masked_where(b, a)
a-c