-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add power-law normalization #1204
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
Conversation
return vmax**gamma * pow(val, -gamma) | ||
|
||
def autoscale(self, A): | ||
''' |
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.
This is not a docstring. Use """
instead.
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.
Good to know. I actually stole this code outright from LogNorm
. I'll put together a patch fixing this class as well.
How does this look now? Comments? |
I doubt this will make v1.2.x as the feature freeze has already happened. However, it will still be interesting to get this in for the next release. The todo list is: unit tests and new entry to "what's new". Would also be nice to add some examples to the gallery. I, too, would also like to make certain any sort of ambiguities regarding normalizations that are offset from zero. |
No worries regarding not making v1.2. I'll be sure to handle the tasks you mention in the next spin of the patch. I'm not really sure what the best way to handle offset data is. I implemented what I believe is the obvious approach in 42a8b47. Here I simply remove the offset and adjust the normalization appropriately. This gives,
Does this seem reasonable? |
Seeing as there were no objections to my handling of offset data, I squashed this commit as well as another fix into b277f87. |
vmin, vmax = self.vmin, self.vmax | ||
if vmin > vmax: | ||
raise ValueError("minvalue must be less than or equal to maxvalue") | ||
elif vmin==vmax: |
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.
PEP8: Spaces needed around the ==
.
Let's try and get this into @WeatherGod Are you happy with the handling of offsetting? |
This needs a test, an entry in |
Thanks for the comments! I'll try to incorporate them this weekend. |
@@ -4,7 +4,7 @@ | |||
|
|||
from __future__ import print_function | |||
import numpy as np | |||
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal |
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 SymLogNorm
test uses the assert_array_almost_equal
function. Removing this made the test fail because it couldn't find it.
I apologize for the sad state of the previous branch. It was admittedly incomplete work and I hadn't managed to get the testsuite to run. Thanks for the suggestions. |
@bgamari No problem! That's why I'm here :) |
@dmcdougall, how do things look now? I think this should be in good shape now although if you have any suggestions for further additions to the |
@@ -0,0 +1,27 @@ | |||
#!/usr/bin/python | |||
|
|||
from scipy.stats import norm |
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.
Matplotlib doesn't have scipy as a dependency, so we can't have this line in the example. On the plus side, it looks like this function isn't even used in the example.
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.
Yes, this snuck in. Killed now
@mdboom, indeed it does seem that the |
self.vmin = ma.min(A) | ||
if self.vmin < 0: | ||
self.vmin = 0 | ||
warn("Power-law scaling on negative values is ill-defined, clamping to 0.") |
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 think this needs to be warnings.warn
, and is the cause of the Travis failure.
@mdboom, good catch! We'll see how this branch fares. |
self.vmin = ma.min(A) | ||
if self.vmin < 0: | ||
self.vmin = 0 | ||
warnings.warn("Power-law scaling on negative values is ill-defined, clamping to 0.") |
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.
pep8 here
I've fixed the remaining PEP8 issues as well as the testcase. It seems that Travis approves. |
|
||
a = [-0.5, 0, 0.5, 1, 1.5] | ||
pnorm = mcolors.PowerNorm(1.5) | ||
assert_array_almost_equal(a, pnorm.inverse(pnorm(a))) |
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.
This is testing that the inverse method is working, but there is no test to check that power norm is actually doing the right thing. For example, I believe I could change line 65 from mcolors.PowerNorm(1.5)
to mcolors.Normalize()
and the test will still pass.
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.
@bgamari Did this get addressed?
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.
No, not yet.
Looks like a small test extension and a rebase would see this into v1.4.0. Is there any chance you could take a look @bgamari? |
How does this look? |
Travis produces a divide by 0 error in this test. |
assert_array_almost_equal(norm(a), pnorm(a)) | ||
|
||
a = [0.5, 0, 2, 4, 8] | ||
expected = [1/0, 0, 1./16, 1./4, 1] |
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 divide by zero error is explicit in the test. That does not seem right.....
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.
Yep, sorry about that; I was still waiting for my tests to finish when I pushed this branch. The 1/0
should have been np.nan
.
@bagmari: It looks like this no longer merges cleanly. Would you mind rebasing against master? |
I believe I (finally) have the test issues sorted out and the branch rebased. |
There is still a py3k issue:
It looks like something needs to be explicitly made into an numpy array. I am very confused why this also does not happen on 2.... |
Looks like we've finally converged. |
@@ -181,19 +189,6 @@ added. Furthermore, the the subplottool is now implemented as a modal | |||
dialog. It was previously a QMainWindow, leaving the SPT open if one closed the | |||
plotwindow. | |||
|
|||
Cairo backends |
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.
This probably should not be removed.
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.
damn you git rerere
@tacaswell, thanks again for catching all of these mistakes. I think I probably owe you a case of beer or two by now. |
This should get a medal as an exceptionally long running but finally merged PR. |
Here is a first cut at introducing power-law normalization (e.g. gamma correction) to
matplotlib.colors
. Do others feel this would be a useful addition to matplotlib?I should note that it's unclear if/how data which is offset from zero should be handled which gives me pause.