-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New colormap normalizations: sqrt, arcsinh #1780
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b6ef15
added sqrt and asinh normalization
keflavich 5a61fe7
pep8
keflavich 7cbe06b
pep8 on test_colors
keflavich 0a24c60
added inverse functions and tests for asinh, sqrt norms
keflavich dcab549
changed "good" values in asinh test
keflavich 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
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 |
---|---|---|
|
@@ -1000,7 +1000,140 @@ def autoscale_None(self, A): | |
self.vmin = ma.min(A) | ||
if self.vmax is None: | ||
self.vmax = ma.max(A) | ||
|
||
|
||
|
||
class SqrtNorm(Normalize): | ||
""" | ||
Normalize a given value to the 0-1 range on a square (or n'th) root scale | ||
""" | ||
def __init__(self, vmin=None, vmax=None, clip=False, nthroot=2): | ||
""" | ||
nthroot allows cube roots, fourth roots, etc. | ||
""" | ||
self.vmin = vmin | ||
self.vmax = vmax | ||
self.clip = clip | ||
self.nthroot = nthroot | ||
|
||
def __call__(self, value, clip=None, midpoint=None): | ||
|
||
if clip is None: | ||
clip = self.clip | ||
|
||
if cbook.iterable(value): | ||
vtype = 'array' | ||
val = ma.asarray(value).astype(np.float) | ||
else: | ||
vtype = 'scalar' | ||
val = ma.array([value]).astype(np.float) | ||
|
||
self.autoscale_None(val) | ||
vmin, vmax = self.vmin, self.vmax | ||
|
||
if vmin > vmax: | ||
raise ValueError("minvalue must be less than or equal to maxvalue") | ||
elif vmin == vmax: | ||
return 0.0 * val | ||
else: | ||
if clip: | ||
mask = ma.getmask(val) | ||
val = ma.array(np.clip(val.filled(vmax), vmin, vmax), | ||
mask=mask) | ||
result = (val-vmin) * (1.0/(vmax-vmin)) | ||
#(arcsinh(val)-arcsinh(vmin))/(arcsinh(vmax)-arcsinh(vmin)) | ||
result = result**(1./self.nthroot) | ||
if vtype == 'scalar': | ||
result = result[0] | ||
return result | ||
|
||
def autoscale_None(self, A): | ||
""" autoscale only None-valued vmin or vmax """ | ||
if self.vmin is None: | ||
self.vmin = ma.min(A) | ||
if self.vmax is None: | ||
self.vmax = ma.max(A) | ||
|
||
def inverse(self, value): | ||
if not self.scaled(): | ||
raise ValueError("Not invertible until scaled") | ||
vmin, vmax = self.vmin, self.vmax | ||
|
||
if cbook.iterable(value): | ||
val = ma.asarray(value) | ||
# r = sqrt(v-vmin/(vmax-vmin)) | ||
# v = r**2 * (vmax-vmin) + vmin | ||
return val**self.nthroot * (vmax-vmin) + vmin | ||
else: | ||
return value**self.nthroot * (vmax-vmin) + vmin | ||
|
||
|
||
class AsinhNorm(Normalize): | ||
""" | ||
Normalize a range of values to 0-1 on an arcsinh scale (nice alternative to | ||
SymLogNormalize) | ||
""" | ||
def __init__(self, vmin=None, vmax=None, clip=False, vmid=None): | ||
self.vmid = vmid | ||
self.vmin = vmin | ||
self.vmax = vmax | ||
self.clip = clip | ||
|
||
def __call__(self, value, clip=None, midpoint=None): | ||
|
||
if clip is None: | ||
clip = self.clip | ||
|
||
if cbook.iterable(value): | ||
vtype = 'array' | ||
val = ma.asarray(value).astype(np.float) | ||
else: | ||
vtype = 'scalar' | ||
val = ma.array([value]).astype(np.float) | ||
|
||
self.autoscale_None(val) | ||
vmin, vmax = self.vmin, self.vmax | ||
|
||
vmid = self.vmid if self.vmid is not None else (vmax+vmin)/2.0 | ||
|
||
if midpoint is None: | ||
midpoint = (vmid - vmin) / (vmax - vmin) | ||
|
||
if vmin > vmax: | ||
raise ValueError("minvalue must be less than or equal to maxvalue") | ||
elif vmin == vmax: | ||
return 0.0 * val | ||
else: | ||
if clip: | ||
mask = ma.getmask(val) | ||
val = ma.array(np.clip(val.filled(vmax), vmin, vmax), | ||
mask=mask) | ||
result = (val-vmin) * (1.0/(vmax-vmin)) | ||
result = ma.arcsinh(result/midpoint) / ma.arcsinh(1./midpoint) | ||
if vtype == 'scalar': | ||
result = result[0] | ||
return result | ||
|
||
def autoscale_None(self, A): | ||
' autoscale only None-valued vmin or vmax' | ||
if self.vmin is None: | ||
self.vmin = ma.min(A) | ||
if self.vmax is None: | ||
self.vmax = ma.max(A) | ||
if self.vmid is None: | ||
self.vmid = (self.vmax+self.vmin)/2.0 | ||
|
||
def inverse(self, value): | ||
if not self.scaled(): | ||
raise ValueError("Not invertible until scaled") | ||
vmin, vmax = self.vmin, self.vmax | ||
|
||
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. PEP compliance: there should be only 2 blank lines here. |
||
if cbook.iterable(value): | ||
val = ma.asarray(value) | ||
# r = arcsinh(v-vmin/(vmax-vmin) / midpoint) / arcsinh(1/midpoint) | ||
# v = sinh(r * arcsinh(1/midpoint)) * midpoint * (vmax-vmin) + vmin | ||
return np.sinh(val * np.arcsinh(1./midpoint)) * midpoint * (vmax-vmin) + vmin | ||
else: | ||
return np.sinh(value * np.arcsinh(1./midpoint)) * midpoint * (vmax-vmin) + vmin | ||
|
||
class SymLogNorm(Normalize): | ||
""" | ||
|
@@ -1039,7 +1172,7 @@ def __call__(self, value, clip=None): | |
result, is_scalar = self.process_value(value) | ||
self.autoscale_None(result) | ||
vmin, vmax = self.vmin, self.vmax | ||
|
||
if vmin > vmax: | ||
raise ValueError("minvalue must be less than or equal to maxvalue") | ||
elif vmin == vmax: | ||
|
@@ -1048,7 +1181,7 @@ def __call__(self, value, clip=None): | |
if clip: | ||
mask = ma.getmask(result) | ||
result = ma.array(np.clip(result.filled(vmax), vmin, vmax), | ||
mask=mask) | ||
mask=mask) | ||
# in-place equivalent of above can be much faster | ||
resdat = self._transform(result.data) | ||
resdat -= self._lower | ||
|
@@ -1057,8 +1190,8 @@ def __call__(self, value, clip=None): | |
if is_scalar: | ||
result = result[0] | ||
return result | ||
def _transform(self, a): | ||
|
||
def _transform(self, a): | ||
""" | ||
Inplace transformation. | ||
""" | ||
|
@@ -1069,7 +1202,7 @@ def _transform(self, a): | |
a[masked] = log | ||
a[~masked] *= self._linscale_adj | ||
return a | ||
|
||
def _inv_transform(self, a): | ||
""" | ||
Inverse inplace Transformation. | ||
|
@@ -1081,7 +1214,7 @@ def _inv_transform(self, a): | |
a[masked] = exp | ||
a[~masked] /= self._linscale_adj | ||
return a | ||
|
||
def _transform_vmin_vmax(self): | ||
""" | ||
Calculates vmin and vmax in the transformed system. | ||
|
@@ -1090,7 +1223,6 @@ def _transform_vmin_vmax(self): | |
arr = np.array([vmax, vmin]) | ||
self._upper, self._lower = self._transform(arr) | ||
|
||
|
||
def inverse(self, value): | ||
if not self.scaled(): | ||
raise ValueError("Not invertible until scaled") | ||
|
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
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.
PEP8 compliance: there should only be one blank line here.