Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Revert part of #3907 which incorrectly propogated MaskedArray info. #7296

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 1 commit into from
Feb 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ def __call__(self, a, *args, **kwargs):
# Transform to
masked_result = result.view(get_masked_subclass(a))
masked_result._mask = m
masked_result._update_from(result)
masked_result._update_from(a)
return masked_result

def __str__(self):
Expand Down Expand Up @@ -999,7 +999,10 @@ def __call__(self, a, b, *args, **kwargs):
# Transforms to a (subclass of) MaskedArray
masked_result = result.view(get_masked_subclass(a, b))
masked_result._mask = m
masked_result._update_from(result)
if isinstance(a, MaskedArray):
masked_result._update_from(a)
elif isinstance(b, MaskedArray):
masked_result._update_from(b)
return masked_result

def reduce(self, target, axis=0, dtype=None):
Expand Down Expand Up @@ -1030,7 +1033,6 @@ def reduce(self, target, axis=0, dtype=None):
return tr
masked_tr = tr.view(tclass)
masked_tr._mask = mr
masked_tr._update_from(tr)
return masked_tr

def outer(self, a, b):
Expand All @@ -1056,7 +1058,6 @@ def outer(self, a, b):
return d
masked_d = d.view(get_masked_subclass(a, b))
masked_d._mask = m
masked_d._update_from(d)
return masked_d

def accumulate(self, target, axis=0):
Expand All @@ -1068,7 +1069,6 @@ def accumulate(self, target, axis=0):
t = filled(target, self.filly)
result = self.f.accumulate(t, axis)
masked_result = result.view(tclass)
masked_result._update_from(result)
return masked_result

def __str__(self):
Expand Down Expand Up @@ -1145,7 +1145,10 @@ def __call__(self, a, b, *args, **kwargs):
# Transforms to a (subclass of) MaskedArray
masked_result = result.view(get_masked_subclass(a, b))
masked_result._mask = m
masked_result._update_from(result)
if isinstance(a, MaskedArray):
masked_result._update_from(a)
elif isinstance(b, MaskedArray):
masked_result._update_from(b)
return masked_result

def __str__(self):
Expand Down
20 changes: 20 additions & 0 deletions numpy/ma/tests/test_subclassing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def __iadd__(self, other):
subarray = SubArray


class SubMaskedArray(MaskedArray):
"""Pure subclass of MaskedArray, keeping some info on subclass."""
def __new__(cls, info=None, **kwargs):
obj = super(SubMaskedArray, cls).__new__(cls, **kwargs)
obj._optinfo['info'] = info
return obj


class MSubArray(SubArray, MaskedArray):

def __new__(cls, data, info={}, mask=nomask):
Expand Down Expand Up @@ -332,6 +340,18 @@ def test_subclass_str(self):
mxcsub = masked_array(xcsub, mask=[True, False, True, False, False])
self.assertTrue(str(mxcsub) == 'myprefix [-- 1 -- 3 4] mypostfix')

def test_pure_subclass_info_preservation(self):
# Test that ufuncs and methods conserve extra information consistently;
# see gh-7122.
arr1 = SubMaskedArray('test', data=[1,2,3,4,5,6])
arr2 = SubMaskedArray(data=[0,1,2,3,4,5])
diff1 = np.subtract(arr1, arr2)
self.assertTrue('info' in diff1._optinfo)
self.assertTrue(diff1._optinfo['info'] == 'test')
diff2 = arr1 - arr2
self.assertTrue('info' in diff2._optinfo)
self.assertTrue(diff2._optinfo['info'] == 'test')


###############################################################################
if __name__ == '__main__':
Expand Down