-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implemented __copy__ in Colormap #8314
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ed55f4
Added __copy__ to Colormap
vidursatija 5003047
Updated __copy__ in Colormap
vidursatija 82cccd0
Fixed __copy__ indentation
vidursatija 3ece4cb
Updated __copy__ indentation
vidursatija 7e8a940
Added tests for Colormap copy
vidursatija 5cafb57
Update test_colors.py
vidursatija 3e6a689
Fixed copy test function
vidursatija f7eadbf
Fixed indentation
vidursatija eeae70d
Changed copy test
vidursatija File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixed __copy__ indentation
- Loading branch information
commit 82cccd003e1a9eff6fcca7de9f24fcd44a2b4657
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -532,15 +532,14 @@ def __call__(self, X, alpha=None, bytes=False): | |
|
||
def __copy__(self): | ||
"""Create new object with the same class and update attributes | ||
If object is initialized, copy the elements of _lut list | ||
""" | ||
cls = self.__class__ | ||
newCMapObj = cls.__new__(cls) | ||
newCMapObj.__dict__.update(self.__dict__) | ||
if self._isinit: | ||
newCMapObj._lut = np.copy(self._lut) | ||
return newCMapObj | ||
|
||
If object is initialized, copy the elements of _lut list | ||
""" | ||
cls = self.__class__ | ||
newCMapObj = cls.__new__(cls) | ||
newCMapObj.__dict__.update(self.__dict__) | ||
if self._isinit: | ||
newCMapObj._lut = np.copy(self._lut) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use spaces instead of tabs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update it |
||
return newCMapObj | ||
|
||
def set_bad(self, color='k', alpha=None): | ||
"""Set color to be used for masked values. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really the best way to do this? It seems to be dropping down to a low layer of python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The low layer of python is presumably faster than the normal python code. Another way of doing this would be creating a normal Colormap object and manually copying each attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should I implement a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get a color map, use it (
cm([-1, 0, .5, 1, np.nan, np.inf])
, copy it, useset_bad
,set_over
,set_under
on one or both of them, use them both and check that the colors come back as expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not expect this to be much faster. If you think it is can you bench mark it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will bench mark it and implement the tests. I'll post the results soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have bench marked the 2 functions.
The following code is used:
I get the following output:
Method 1 :1.8358230590820312e-05
Method 2 :2.6941299438476562e-05
I can definitely say that using low level python functions would lead to a simpler, faster code, which might be a little difficult to understand by seeing it.
Test
I've used the copy_1 function to copy the object.
Output is as follows:
(0.75, 0.75, 0.0, 1)
(0.0, 0.0, 1.0, 1)
(0.75, 0.75, 0.0, 1)
(1.0, 0.0, 0.0, 1)
(0.0, 0.75, 0.75, 1)
(0.75, 0.0, 0.75, 1)
As we can see, using the function on both Colormaps doesn't change the other
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My last commit (3ece4cb) has used lower level python to implement copy. The code has also used spaces instead of tabs