本文整理匯總了Python中numpy.ma方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.ma方法的具體用法?Python numpy.ma怎麽用?Python numpy.ma使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.ma方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: timer
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def timer(s, v='', nloop=500, nrep=3):
units = ["s", "ms", "µs", "ns"]
scaling = [1, 1e3, 1e6, 1e9]
print("%s : %-50s : " % (v, s), end=' ')
varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']
setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)
Timer = timeit.Timer(stmt=s, setup=setup)
best = min(Timer.repeat(nrep, nloop)) / nloop
if best > 0.0:
order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)
else:
order = 3
print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
3,
best * scaling[order],
units[order]))
示例2: test_testPut
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def test_testPut(self):
# Test of put
with suppress_warnings() as sup:
sup.filter(
np.ma.core.MaskedArrayFutureWarning,
"setting an item on a masked array which has a "
"shared mask will not copy")
d = arange(5)
n = [0, 0, 0, 1, 1]
m = make_mask(n)
x = array(d, mask=m)
self.assertTrue(x[3] is masked)
self.assertTrue(x[4] is masked)
x[[1, 4]] = [10, 40]
self.assertTrue(x.mask is not m)
self.assertTrue(x[3] is masked)
self.assertTrue(x[4] is not masked)
self.assertTrue(eq(x, [0, 10, 2, -1, 40]))
x = array(d, mask=m)
x.put([0, 1, 2], [-1, 100, 200])
self.assertTrue(eq(x, [-1, 100, 200, 0, 0]))
self.assertTrue(x[3] is masked)
self.assertTrue(x[4] is masked)
示例3: timer
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def timer(s, v='', nloop=500, nrep=3):
units = ["s", "ms", "µs", "ns"]
scaling = [1, 1e3, 1e6, 1e9]
print("%s : %-50s : " % (v, s), end=' ')
varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']
setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)
Timer = timeit.Timer(stmt=s, setup=setup)
best = min(Timer.repeat(nrep, nloop)) / nloop
if best > 0.0:
order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)
else:
order = 3
print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
3,
best * scaling[order],
units[order]))
# ip.magic('timeit -n%i %s' % (nloop,s))
示例4: poly_between
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def poly_between(x, ylower, yupper):
"""
Given a sequence of *x*, *ylower* and *yupper*, return the polygon
that fills the regions between them. *ylower* or *yupper* can be
scalar or iterable. If they are iterable, they must be equal in
length to *x*.
Return value is *x*, *y* arrays for use with
:meth:`matplotlib.axes.Axes.fill`.
"""
if ma.isMaskedArray(ylower) or ma.isMaskedArray(yupper) or ma.isMaskedArray(x):
numpy = ma
else:
numpy = np
Nx = len(x)
if not cbook.iterable(ylower):
ylower = ylower*numpy.ones(Nx)
if not cbook.iterable(yupper):
yupper = yupper*numpy.ones(Nx)
x = numpy.concatenate( (x, x[::-1]) )
y = numpy.concatenate( (yupper, ylower[::-1]) )
return x,y
示例5: poly_between
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def poly_between(x, ylower, yupper):
"""
Given a sequence of *x*, *ylower* and *yupper*, return the polygon
that fills the regions between them. *ylower* or *yupper* can be
scalar or iterable. If they are iterable, they must be equal in
length to *x*.
Return value is *x*, *y* arrays for use with
:meth:`matplotlib.axes.Axes.fill`.
"""
if any(isinstance(var, np.ma.MaskedArray) for var in [ylower, yupper, x]):
numpy = np.ma
else:
numpy = np
Nx = len(x)
if not cbook.iterable(ylower):
ylower = ylower*numpy.ones(Nx)
if not cbook.iterable(yupper):
yupper = yupper*numpy.ones(Nx)
x = numpy.concatenate((x, x[::-1]))
y = numpy.concatenate((yupper, ylower[::-1]))
return x, y
示例6: compare_functions_2v
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def compare_functions_2v(func, nloop=500, test=True,
xs=xs, nmxs=nmxs,
ys=ys, nmys=nmys,
xl=xl, nmxl=nmxl,
yl=yl, nmyl=nmyl):
funcname = func.__name__
print("-"*50)
print("%s on small arrays" % funcname)
module, data = "numpy.ma", "nmxs,nmys"
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
#
print("%s on large arrays" % funcname)
module, data = "numpy.ma", "nmxl,nmyl"
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
return
###############################################################################
################################################################################
示例7: mask_vals
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def mask_vals(f, maskdef, metakeys=_metakeys):
mtype = maskdef.split(',')[0]
mval = ','.join(maskdef.split(',')[1:])
if mtype == 'where':
maskexpr = 'np.ma.masked_where(mask, var[:].view(np.ndarray))'
# mask = eval(mval, None, f.variables)
else:
maskexpr = 'np.ma.masked_%s(var[:], %s)' % (mtype, mval)
for varkey, var in f.variables.items():
if varkey not in metakeys:
try:
vout = eval(maskexpr)
f.variables[varkey] = PseudoNetCDFMaskedVariable(
f, varkey, var.dtype.char, var.dimensions, values=vout,
**dict([(pk, getattr(var, pk)) for pk in var.ncattrs()]))
except Exception as e:
warn('Cannot mask %s: %s' % (varkey, str(e)))
return f
示例8: test_testMixedArithmetic
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def test_testMixedArithmetic(self):
na = np.array([1])
ma = array([1])
assert_(isinstance(na + ma, MaskedArray))
assert_(isinstance(ma + na, MaskedArray))
示例9: test_testUfuncRegression
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def test_testUfuncRegression(self):
f_invalid_ignore = [
'sqrt', 'arctanh', 'arcsin', 'arccos',
'arccosh', 'arctanh', 'log', 'log10', 'divide',
'true_divide', 'floor_divide', 'remainder', 'fmod']
for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
'sin', 'cos', 'tan',
'arcsin', 'arccos', 'arctan',
'sinh', 'cosh', 'tanh',
'arcsinh',
'arccosh',
'arctanh',
'absolute', 'fabs', 'negative',
'floor', 'ceil',
'logical_not',
'add', 'subtract', 'multiply',
'divide', 'true_divide', 'floor_divide',
'remainder', 'fmod', 'hypot', 'arctan2',
'equal', 'not_equal', 'less_equal', 'greater_equal',
'less', 'greater',
'logical_and', 'logical_or', 'logical_xor']:
try:
uf = getattr(umath, f)
except AttributeError:
uf = getattr(fromnumeric, f)
mf = getattr(np.ma, f)
args = self.d[:uf.nin]
with np.errstate():
if f in f_invalid_ignore:
np.seterr(invalid='ignore')
if f in ['arctanh', 'log', 'log10']:
np.seterr(divide='ignore')
ur = uf(*args)
mr = mf(*args)
assert_(eq(ur.filled(0), mr.filled(0), f))
assert_(eqmask(ur.mask, mr.mask))
示例10: compare_functions_1v
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def compare_functions_1v(func, nloop=500,
xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
funcname = func.__name__
print("-"*50)
print("%s on small arrays" % funcname)
module, data = "numpy.ma", "nmxs"
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
print("%s on large arrays" % funcname)
module, data = "numpy.ma", "nmxl"
timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
return
示例11: compare_methods
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def compare_methods(methodname, args, vars='x', nloop=500, test=True,
xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
print("-"*50)
print("%s on small arrays" % methodname)
data, ver = "nm%ss" % vars, 'numpy.ma'
timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
print("%s on large arrays" % methodname)
data, ver = "nm%sl" % vars, 'numpy.ma'
timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
return
示例12: _ensure_large_enough
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def _ensure_large_enough(self, arr, cont, N, newsize, frac_done):
"""Allocate new space while adding gran to data
Helper for _add_gran_to_data, part of the read_period family of
helpers. Does NOT add cont to arr!"""
if isinstance(cont, xarray.Dataset):
raise NotImplementedError("Not used for xarray datasets. "
"But see version history at "
"https://arts.mi.uni-hamburg.de/trac/rt/browser/typhon/trunk/typhon/datasets/dataset.py?rev=10396#L462 "
"if there is a wish to reimplemented!")
else:
if newsize * arr.itemsize > self.maxsize:
raise MemoryError("This dataset is too large "
"for typhons little mind. Continuing might "
"ultimately need {:,.0f} MiB of RAM. This exceeds my "
"maximum (self.maxsize) of {:,.0f} MiB. "
"Sorry! ".format(
newsize*arr.itemsize/MiB,
self.maxsize/MiB))
logger.debug(
"New size ({:d} items, {:,.0f} MiB) would exceed allocated "
"size ({:d} items, {:,.0f} MiB). I'm {:.3%} "
"through. Allocating new: {:d} items, {:,.0f} "
"MiB. New size: {:d} items, {:,.0f} "
"MiB.".format(N+cont.size,
(cont.nbytes+arr.nbytes)/MiB,
arr.size, arr.nbytes/MiB, frac_done,
newsize-arr.size, (newsize-arr.size)*arr.itemsize/MiB,
newsize, newsize*arr.itemsize/MiB))
mod = (numpy.ma if hasattr(arr, "mask") else numpy)
arr = mod.concatenate(
(arr, mod.zeros(dtype=arr.dtype, shape=newsize-arr.size)))
return arr
示例13: _add_pseudo_fields
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def _add_pseudo_fields(self, M, pseudo_fields, extra, f):
D = collections.OrderedDict()
for (k, fnc) in pseudo_fields.items():
try:
D[k] = fnc(M, D, extra, f)
except TypeError as exc:
if "positional argument" in exc.args[0]:
# backward compatibility
D[k] = fnc(M)
else:
raise
if D != {}:
if self.read_returns == "ndarray":
mod = (numpy.ma if hasattr(M, "mask") else numpy)
newM = mod.zeros(shape=M.shape,
dtype=M.dtype.descr + [(k, v.dtype, v.shape[1:]) for (k,v) in D.items()])
for k in M.dtype.names:
newM[k] = M[k]
elif self.read_returns == "xarray":
newM = M
else:
raise ValueError("read_returns must be ndarray or xarray")
for (k, v) in D.items():
newM[k] = v
M = newM
return M
示例14: _read
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ma [as 別名]
def _read(self, p, fields="all"):
"""Reads a single measurement converted to ndarray
Arguments:
p (pathlib.Path): path to file
fields (Iterable[str] or str): What fields to return.
See :func:`Dataset.read_period` for details.
"""
(head, body) = self.read_single(p, fields=fields)
dt = [(s+body.shape if len(s)==2 else (s[0], s[1], s[2]+body.shape))
for s in body.dtype.descr]
dt.extend([("lat", "f8"), ("lon", "f8"), ("time", "M8[s]")])
dt.extend([(s, self._head_dtype[s])
for s in (head.keys() & self._head_dtype.keys())
if s not in {"lat", "lon", "time"}])
if self.filename_fields:
info = self.get_info_for_granule(p)
dt.extend(self.filename_fields.items())
D = numpy.ma.empty(1, dt)
for nm in body.dtype.names:
D[nm] = body[nm]
for nm in {"lat", "lon", "time"}:
D[nm] = head[nm]
for nm in head.keys() & D.dtype.names:
if nm not in {"lat", "lon", "time"}:
D[nm] = head[nm]
if self.filename_fields:
for nm in self.filename_fields.keys():
D[nm] = info[nm]
return (D, {})