Skip to content

FIX: handle fully masked data #9285

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 3 commits into from
Oct 12, 2017

Conversation

tacaswell
Copy link
Member

As min/max do not make sense for an array with no non-masked values
numpy returns a singleton, np.ma.masked, which can not be cast to
a number. In this case just treat numbers as in range (0, 1) (even
though it will just be ignored due to the masking).

closes #9280

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@tacaswell tacaswell added this to the 2.1.1 (next bug fix release) milestone Oct 5, 2017
@anntzer
Copy link
Contributor

anntzer commented Oct 5, 2017

probably worth adding in the comment that the actual value doesn't matter.

try:
a_max = a_max.astype(scaled_dtype)
except AttributeError:
a_min = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simpler?

if np.ma.count(A) == 0:
    a_min, a_max = 0, 1   # all masked, so values don't matter
else:
    a_min = np.ma.min(A).astype(scaled_dtype)
    a_max = np.ma.max(A).astype(scaled_dtype)

a_max = np.ma.max(A)

# we need these try/except blocks to handle
# fully-masked/invalid input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And delete the 4 lines above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

details....

@tacaswell tacaswell force-pushed the fix_fullymasked_image branch from f5c69f6 to 659c9f3 Compare October 11, 2017 03:18
@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2017

Actually now that I think of it it may be slightly more efficient to check whether a_min is np.ma.masked? Seems a bit silly to iterate yet another time over the entire array (when it is masked).
Not that I'd consider it a blocker.

@tacaswell
Copy link
Member Author

It is always masked because an earlier step in the processing chain makes it so.

@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2017

Then it definitely seems better to avoid a redundant full iteration...

@tacaswell tacaswell force-pushed the fix_fullymasked_image branch from 659c9f3 to aa66aee Compare October 11, 2017 03:43
@tacaswell
Copy link
Member Author

ok, so back to the original version of this PR.

@anntzer
Copy link
Contributor

anntzer commented Oct 11, 2017

I would make the test on masked explicit rather than relying on attributeerror (but again no big deal)

except AttributeError:
a_min = 0
try:
a_max = a_max.astype(scaled_dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gee, can't leave it alone.

try:
    a_min  = a_min.astype(scaled_dtype)
except AttributeError:
    a_min, a_max = 0, 1
else:
    a_max = a_max.astype(scaled_dtype)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or shorten the whole block. From the top:

try:
    a_min = A.min().astype(scaled_dtype)
except AttributeError:
    a_min, a_max = 0, 1  # all masked, so values don't matter
else:
    a_max = A.max().astype(scaled_dtype)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And taking @anntzer's suggestion:

a_min = A.min().astype(scaled_dtype)
if a_min is np.ma.masked:
    a_min, a_max = 0, 1  # all masked, so values don't matter
else:
    a_max = A.max().astype(scaled_dtype)

@efiring
Copy link
Member

efiring commented Oct 12, 2017

I took the liberty of directly making a commit with a short form that also addresses @anntzer's suggestion instead of pestering you with yet another code chunk in a comment. The image code is sufficiently long and complicated that it is worthwhile to keep the handling of this special case as concise and readable as possible.

@anntzer
Copy link
Contributor

anntzer commented Oct 12, 2017

@efiring take care of the merge conflict too?

@efiring
Copy link
Member

efiring commented Oct 12, 2017

Yup, just did it. Tests are running.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conditional on tests passing

@anntzer
Copy link
Contributor

anntzer commented Oct 12, 2017

ah the joys of pep8

@efiring
Copy link
Member

efiring commented Oct 12, 2017

The problem there is the lousy editor available for in-place editing in github. It neither shows nor removes trailing whitespace. I will try again, but it will be a shot in the dark. If it doesn't work I guess I will have to go through the whole pull/modify/commit/push dance.

tacaswell and others added 3 commits October 12, 2017 01:13
As min/max do not make sense for an array with no non-masked values
numpy returns a singleton, `np.ma.masked`, which can not be cast to
a number.  In this case just treat numbers as in range (0, 1) (even
though it will just be ignored due to the masking).

closes matplotlib#9280
@tacaswell tacaswell force-pushed the fix_fullymasked_image branch from a4cd618 to 63d3fb0 Compare October 12, 2017 06:13
@tacaswell
Copy link
Member Author

rebased and force-pushed to remove the merge commit.

@anntzer anntzer merged commit b03e0b2 into matplotlib:v2.1.x Oct 12, 2017
@anntzer
Copy link
Contributor

anntzer commented Oct 12, 2017

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants