Skip to content

Issue #8299, implemented copy, added test #8375

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
May 25, 2017

Conversation

vidursatija
Copy link
Contributor

@vidursatija vidursatija commented Mar 24, 2017

Closes #8299

Creating new PR to replace #8314 @phobson @tacaswell

@QuLogic QuLogic added this to the 2.0.1 (next bug fix release) milestone Mar 24, 2017
@QuLogic QuLogic requested review from tacaswell and phobson March 24, 2017 23:01
@@ -530,6 +530,17 @@ def __call__(self, X, alpha=None, bytes=False):
rgba = tuple(rgba[0, :])
return rgba

def __copy__(self):
"""Create new object with the same class and update attributes
If object is initialized, copy the elements of _lut list
Copy link
Member

Choose a reason for hiding this comment

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

Much like the test, I don't think you need to mention _lut in the doc here

If object is initialized, copy the elements of _lut list
"""
cls = self.__class__
newCMapObj = cls.__new__(cls)
Copy link
Member

Choose a reason for hiding this comment

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

CamelCase is for classes, please use snake_case in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I change the case and remove the mention?

Copy link
Member

Choose a reason for hiding this comment

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

Go ahead.

def test_colormap_copy():
cm = plt.cm.Reds
cm([-1, 0, .5, np.nan, np.inf])
cm.set_bad('y')
Copy link
Member

Choose a reason for hiding this comment

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

@tacaswell Won't this leak into the global state of plt.cm.Reds? Why do we have global state anyway? Maybe the colormaps should be made read-only until copied?

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 this will leak into the reds

Copy link
Member

Choose a reason for hiding this comment

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

I am also not sure why the color maps are global like this. I assume it is an optimization from long ago?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should I change it?

Copy link
Member

Choose a reason for hiding this comment

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

make a copy before using set_*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay yes. That would prevent leaks into red

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Improvements to tests.

def test_colormap_copy():
cm = plt.cm.Reds
cm([-1, 0, .5, np.nan, np.inf])
cm.set_bad('y')
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 this will leak into the reds

expected1.set_over('c')
expected2.set_over('m')

assert_array_equal(cm._lut, expected1._lut)
Copy link
Member

Choose a reason for hiding this comment

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

I would rather test this by comparing the mapped values rather than reaching in at looking at the private attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do I do that?

Copy link
Member

Choose a reason for hiding this comment

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

Something like

ret1 = cm([-1, 0, .5, 1, np.nan, np.inf])
cm2 = copy.copy(cm)
cm2.set_bad(...)
ret2 = cm([-1, 5, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I do the following :

cm = plt.cm.Reds
ret1 = cm([-1, 0, 0.5, 1, np.nan, np.inf])
cm2 = copy.copy(cm)
cm2.set_bad('g')
ret2 = cm([-1, 5, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)

I get the assertion error.
The copy function does work but how do I find the error?

Copy link
Member

Choose a reason for hiding this comment

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

Because the input arrays are different (I apparently can not type).

@QuLogic
Copy link
Member

QuLogic commented Apr 21, 2017

Ping @vidursatija?

@vidursatija
Copy link
Contributor Author

I'll get back soon. I'm having exams. Just a week

@QuLogic
Copy link
Member

QuLogic commented Apr 29, 2017

@vidursatija Hope you can get to this this weekend; it's really annoying in tests.

@vidursatija
Copy link
Contributor Author

Can you please navigate me? I'm getting a bit confused with the input arrays

@phobson
Copy link
Member

phobson commented Apr 30, 2017

@vidursatija As far as I can tell, you just need to use the same values in ret1 and ret2. Tom had a typo in his previous message.

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@vidursatija
Copy link
Contributor Author

By doing the following :

cm = plt.cm.Reds
ret1 = cm([-1, 0, .5, 1, np.nan, np.inf])
cm2 = copy.copy(cm)
cm2.set_bad('g')
ret2 = cm([-1, 0, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)

I get a runtime warning but no assertion error.
Should I commit this change?

@QuLogic
Copy link
Member

QuLogic commented May 4, 2017

Sounds good to me. You might want to put those cm() in with np.errstate(invalid='ignore'): since those nan/inf are intentional.

@vidursatija
Copy link
Contributor Author

By doing the following :

cm = plt.cm.Reds
with np.errstate(invalid='ignore'):
  ret1 = cm([-1, 0, .5, 1, np.nan, np.inf])
cm2 = copy.copy(cm)
cm2.set_bad('g')
with np.errstate(invalid='ignore'):
  ret2 = cm([-1, 0, .5, 1, np.nan, np.inf])
assert_array_equal(ret1, ret2)

This works. Should I commit it? I'll do it ASAP

@QuLogic
Copy link
Member

QuLogic commented May 11, 2017

Ping?

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.0.3 (next bug fix release) May 12, 2017
Implemented with no leak into Reds
@phobson phobson changed the title Issue #8299, implemented copy, added test [MRG+1] Issue #8299, implemented copy, added test May 18, 2017
@phobson
Copy link
Member

phobson commented May 18, 2017

@tacaswell I think this is ready to go. Could you update your review?

@tacaswell tacaswell merged commit d50843e into matplotlib:master May 25, 2017
@QuLogic QuLogic changed the title [MRG+1] Issue #8299, implemented copy, added test Issue #8299, implemented copy, added test Jun 8, 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.

4 participants