Skip to content
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

Interpret data to normalize as ndarrays #8696

Merged
merged 1 commit into from Jun 7, 2017

Conversation

ngoldbaum
Copy link
Contributor

This follows on from #6622. That PR fixed the case where we were calling imshow with linear normalization. This fixes the case when we call imshow with log, symlog, or power normalization. For example:

import numpy as np

from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from yt.units import km

plt.imshow(np.random.random((16, 16))*km, norm=LogNorm())

Currently this dies with a unit error in LogNorm. Analogous to the fix applied in #6622 the fix is to interpret data handed to us by a user as an ndarray.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 1, 2017
@efiring
Copy link
Member

efiring commented Jun 1, 2017

Can this as well as the #6622 fix be consolidated into Normalize.process_value()? That seems to me to be the logical place to strip off the units.

@ngoldbaum
Copy link
Contributor Author

ngoldbaum commented Jun 2, 2017

Added logic to process_value. Unfortunately I couldn't use the new return value I added to process_value for all the normalizers since some of them do some additional data processing after the call to process_value.

I also added a test.

Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

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

A simple one-line change replaces everything else. I think that means #6622 can be reverted as well.

result = np.ma.array(value, dtype=dtype, copy=True)
return result, is_scalar
result_data = np.asarray(result.data)
Copy link
Member

Choose a reason for hiding this comment

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

I think that all you really need to do here is add the kwarg subok=False to the np.ma.array() line returning result. Then result will be a plain masked array, and result.data will be a plain ndarray, so you will not need to extract and return it here. The reason all this came up is that np.ma.array has a surprising default of subok=True, unlike np.array, which has the default subok=False.

@ngoldbaum
Copy link
Contributor Author

@efiring subok didn't seem to work (I think it only affects the type of result, not result.data) but the approach I just pushed does seem to work. Are you ok with just unconditionally calling np.asarray unless we're passed a masked array?

@efiring
Copy link
Member

efiring commented Jun 2, 2017

Try this substitution:

# result = np.ma.array(value, dtype=dtype, copy=True)
mask = np.ma.getmask(value)
data = np.asarray(np.ma.getdata(value))
result = np.ma.array(data, mask=mask, dtype=dtype, copy=True)

@ngoldbaum ngoldbaum force-pushed the normalize-ndarray branch 2 times, most recently from 95168b2 to 590f829 Compare June 2, 2017 22:12
@ngoldbaum
Copy link
Contributor Author

Cool that works. Thanks for the suggestions, if it isn't clear I know basically nothing about masked arrays :)

@phobson phobson merged commit 3f99cd0 into matplotlib:master Jun 7, 2017
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.

None yet

4 participants