8000 Fix putmask by charris · Pull Request #106 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Fix putmask #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2011
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
ENH: Fix some functions to use copyto instead of the deprecated putmask.
  • Loading branch information
charris committed Jul 9, 2011
commit b5cdaee35bab2a06604f204ba18e00bf465879a7
14 changes: 7 additions & 7 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,9 @@ def unwrap(p, discont=pi, axis=-1):
slice1 = [slice(None, None)]*nd # full slices
slice1[axis] = slice(1, None)
ddmod = mod(dd+pi, 2*pi)-pi
_nx.putmask(ddmod, (ddmod==-pi) & (dd > 0), pi)
_nx.copyto(ddmod, pi, where=(ddmod==-pi) & (dd > 0))
ph_correct = ddmod - dd;
_nx.putmask(ph_correct, abs(dd)<discont, 0)
_nx.copyto(ph_correct, 0, where=abs(dd)<discont)
up = array(p, copy=True, dtype='d')
up[slice1] = p[slice1] + ph_correct.cumsum(axis)
return up
Expand Down Expand Up @@ -1273,7 +1273,7 @@ def extract(condition, arr):

See Also
--------
take, put, putmask, compress
take, put, copyto, compress

Examples
--------
Expand Down Expand Up @@ -1303,9 +1303,9 @@ def place(arr, mask, vals):
"""
Change elements of an array based on conditional and input values.

Similar to ``np.putmask(arr, mask, vals)``, the difference is that `place`
Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place`
uses the first N elements of `vals`, where N is the number of True values
in `mask`, while `putmask` uses the elements where `mask` is True.
in `mask`, while `copyto` uses the elements where `mask` is True.

Note that `extract` does the exact opposite of `place`.

Expand All @@ -1322,7 +1322,7 @@ def place(arr, mask, vals):

See Also
--------
putmask, put, take, extract
copyto, put, take, extract

Examples
--------
Expand Down Expand Up @@ -1366,7 +1366,7 @@ def _nanop(op, fill, a, axis=None):
# y[mask] = fill
# We can't use fancy indexing here as it'll mess w/ MaskedArrays
# Instead, let's fill the array directly...
np.putmask(y, mask, fill)
np.copyto(y, fill, where=mask)
res = op(y, axis=axis)
mask_all_along_axis = mask.all(axis=axis)

Expand Down
46 changes: 23 additions & 23 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def __call__ (self, a, *args, **kwargs):
if m is not nomask:
# In case result has a lower dtype than the inputs (as in equal)
try:
np.putmask(result, m, d)
np.copyto(result, d, where=m)
except TypeError:
pass
# Transform to
Expand Down Expand Up @@ -939,7 +939,7 @@ def __call__ (self, a, b, *args, **kwargs):
# Case 2. : array
# Revert result to da where masked
if m.any():
np.putmask(result, m, 0)
np.copyto(result, 0, casting='unsafe', where=m)
# This only makes sense if the operation preserved the dtype
if result.dtype == da.dtype:
result += m * da
Expand Down Expand Up @@ -1001,7 +1001,7 @@ def outer (self, a, b):
(da, db) = (getdata(a), getdata(b))
d = self.f.outer(da, db)
if m is not nomask:
np.putmask(d, m, da)
np.copyto(d, da, where=m)
if d.shape:
d = d.view(get_masked_subclass(a, b))
d._mask = m
Expand Down Expand Up @@ -1084,7 +1084,7 @@ def __call__(self, a, b, *args, **kwargs):
else:
return result
# When the mask is True, put back da
np.putmask(result, m, 0)
np.copyto(result, 0, casting='unsafe', where=m)
result += m * da
result = result.view(get_masked_subclass(a, b))
result._mask = m
Expand Down Expand Up @@ -2289,7 +2289,7 @@ def _recursive_printoption(result, mask, printopt):
if curdata.dtype.names:
_recursive_printoption(curdata, curmask, printopt)
else:
np.putmask(curdata, curmask, printopt)
np.copyto(curdata, printopt, where=curmask)
return

_print_templates = dict(long_std="""\
Expand Down Expand Up @@ -2334,7 +2334,7 @@ def _recursive_filled(a, mask, fill_value):
if current.dtype.names:
_recursive_filled(current, mask[name], fill_value[name])
else:
np.putmask(current, mask[name], fill_value[name])
np.copyto(current, fill_value[name], where=mask[name])



Expand Down Expand Up @@ -2828,7 +2828,7 @@ def __array_wrap__(self, obj, context=None):
# Domain not recognized, use fill_value instead
fill_value = self.fill_value
result = result.copy()
np.putmask(result, d, fill_value)
np.copyto(result, fill_value, where=d)
# Update the mask
if m is nomask:
if d is not nomask:
Expand Down Expand Up @@ -3389,7 +3389,7 @@ def filled(self, fill_value=None):
else:
result = self._data.copy()
try:
np.putmask(result, m, fill_value)
np.copyto(result, fill_value, where=m)
except (TypeError, AttributeError):
fill_value = narray(fill_value, dtype=object)
d = result.astype(object)
Expand Down Expand Up @@ -3516,7 +3516,7 @@ def __str__(self):
m = m.view((bool, len(m.dtype)))
if m.any():
r = np.array(self._data.tolist(), dtype=object)
np.putmask(r, m, f)
np.copyto(r, f, where=m)
return str(tuple(r))
else:
return str(self._data)
Expand Down Expand Up @@ -3771,7 +3771,7 @@ def __ipow__(self, other):
self._mask |= invalid
else:
self._mask = invalid
np.putmask(self._data, invalid, self.fill_value)
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
Expand Down Expand Up @@ -5059,7 +5059,7 @@ def min(self, axis=None, out=None, fill_value=None):
result.__setmask__(newmask)
# Get rid of Infs
if newmask.ndim:
np.putmask(result, newmask, result.fill_value)
np.copyto(result, result.fill_value, where=newmask)
elif newmask:
result = masked
return result
Expand All @@ -5075,7 +5075,7 @@ def min(self, axis=None, out=None, fill_value=None):
errmsg = "Masked data information would be lost in one or more"\
" location."
raise MaskError(errmsg)
np.putmask(out, newmask, np.nan)
np.copyto(out, np.nan, where=newmask)
return out

def mini(self, axis=None):
Expand Down Expand Up @@ -5158,7 +5158,7 @@ def max(self, axis=None, out=None, fill_value=None):
result.__setmask__(newmask)
# Get rid of Infs
if newmask.ndim:
np.putmask(result, newmask, result.fill_value)
np.copyto(result, result.fill_value, where=newmask)
elif newmask:
result = masked
return result
Expand All @@ -5175,7 +5175,7 @@ def max(self, axis=None, out=None, fill_value=None):
errmsg = "Masked data information would be lost in one or more"\
" location."
raise MaskError(errmsg)
np.putmask(out, newmask, np.nan)
np.copyto(out, np.nan, where=newmask)
return out

def ptp(self, axis=None, out=None, fill_value=None):
Expand Down Expand Up @@ -6040,7 +6040,7 @@ def power(a, b, third=None):
# if m.all():
# fa.flat = 1
# else:
# np.putmask(fa,m,1)
# np.copyto(fa, 1, where=m)
# return masked_array(umath.power(fa, fb), m)

#..............................................................................
Expand Down Expand Up @@ -6333,17 +6333,17 @@ def putmask(a, mask, values): #, mode='raise'):
if valmask is not nomask:
a._sharedmask = True
a._mask = make_mask_none(a.shape, a.dtype)
np.putmask(a._mask, mask, valmask)
np.copyto(a._mask, valmask, where=mask)
elif a._hardmask:
if valmask is not nomask:
m = a._mask.copy()
np.putmask(m, mask, valmask)
np.copyto(m, valmask, where=mask)
a.mask |= m
else:
if valmask is nomask:
valmask = getmaskarray(values)
np.putmask(a._mask, mask, valmask)
np.putmask(a._data, mask, valdata)
np.copyto(a._mask, valmask, where=mask)
np.copyto(a._data, valdata, where=mask)
return

def transpose(a, axes=None):
Expand Down Expand Up @@ -6553,12 +6553,12 @@ def where (condition, x=None, y=None):
# Construct an empty array and fill it
d = np.empty(fc.shape, dtype=ndtype).view(MaskedArray)
_data = d._data
np.putmask(_data, fc, xv.astype(ndtype))
np.putmask(_data, notfc, yv.astype(ndtype))
np.copyto(_data, xv.astype(ndtype), where=fc)
np.copyto(_data, yv.astype(ndtype), where=notfc)
# Create an empty mask and fill it
_mask = d._mask = np.zeros(fc.shape, dtype=MaskType)
np.putmask(_mask, fc, getmask(x))
np.putmask(_mask, notfc, getmask(y))
np.copyto(_mask, getmask(x), where=fc)
np.copyto(_mask, getmask(y), where=notfc)
_mask |= getmaskarray(condition)
if not _mask.any():
d._mask = nomask
Expand Down
0