8000 ENH: Added FuncNorm and PiecewiseNorm classes in colors by alvarosg · Pull Request #7294 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

ENH: Added FuncNorm and PiecewiseNorm classes in colors #7294

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
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2d90c5a
Added ArbitaryNorm and RootNorm classes in colors, as well as example…
Oct 17, 2016
57aad3d
PEP8 formatting on examples, plotting using the object oriented appro…
Oct 18, 2016
f818aff
Added title/description to the examples
Oct 18, 2016
ffe1b9d
Class attributes are now hidden
Oct 18, 2016
1d22b90
Major update: complete refactorization of code. A much more powerful …
Oct 19, 2016
b5801ea
Corrected lambda function syntax that was not compatible with python …
Oct 19, 2016
e93d82d
Added FuncNorm: now everything inherits from this. Changed the name o…
Oct 20, 2016
3749b0a
Forgot to uncomment an import
Oct 20, 2016
de62491
Improved the auto-tick feature, and corrected some pep8 issues
Oct 20, 2016
d148756
Improved examples, created a new file for generating sample data.'
alvarosg Oct 22, 2016
5373a98
Corrected a double line, and removed a comment
alvarosg Oct 22, 2016
13edeab
Tests for FuncNorm added, and bug corrected in FuncNorm
alvarosg Oct 22, 2016
21d5cd0
Added compatibility for python 3 string check, added tests for Piecew…
alvarosg Oct 22, 2016
d359a4e
Added tests on all classes, including all public methods
alvarosg Oct 22, 2016
4622829
Change type of arrays in tests from int to float
alvarosg Oct 22, 2016
30ff404
Corrected wrong `super()` for RootNorm
alvarosg Oct 22, 2016
df835cb
Solve problem with implicit int to float casting that was not working…
alvarosg Oct 22, 2016
dfaa0f8
Added documentation in the numpydoc format
alvarosg Oct 23, 2016
a386395
Improve style in the examples. Corrected intending problem in the doc…
alvarosg Oct 23, 2016
b9dafb0
Added example in `FuncNorm` docstring
alvarosg Oct 23, 2016
d10be73
Finished with the examples in the docstrings
alvarosg Oct 24, 2016
c85a14c
Implemented clipping behavoir. Refactored _func_parser
alvarosg Oct 26, 2016
7597ddd
It now allows some string functions with parameters. Added a test for…
alvarosg Oct 28, 2016
7fce503
Forgot to add a file...
alvarosg Oct 28, 2016
bcd7dd0
Forgot to add another file...
alvarosg Oct 28, 2016
a71e1e9
Improved tests, documentation, and exceptions
alvarosg Oct 31, 2016
33f57d1
Removed test_colors.py from __init__.py after including parametrize
alvarosg Oct 31, 2016
9687173
Moved the string function parser to its own class in cbook. Added tes…
alvarosg Nov 1, 2016
46395aa
Improved documentation
Nov 2, 2016
63dab61
Added new example
Nov 2, 2016
8abf2c2
Added example for PiecewiseNorm, and MirrorPiecewiseNorm. String in t…
alvarosg Nov 3, 2016
b4ecdb2
Removed sampledata.py no longer necessary, and changed examples in do…
alvarosg Nov 3, 2016
42007ee
Added examples for MirrorRootNorm and RootNorm
alvarosg Nov 3, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moved the string function parser to its own class in cbook. Added tes…
…ts. Since now parametrized is not used in test_colors.py anymore, the file has been readded in __init__.py for testing with nose
  • Loading branch information
alvarosg committed Nov 1, 2016
commit 96871733250e10df87766bb9d8a496b4dc62ef5c
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,7 @@ def _jupyter_nbextension_paths():
'matplotlib.tests.test_coding_standards',
'matplotlib.tests.test_collections',
'matplotlib.tests.test_colorbar',
'matplotlib.tests.test_colors',
'matplotlib.tests.test_compare_images',
'matplotlib.tests.test_container',
'matplotlib.tests.test_contour',
Expand Down
77 changes: 77 additions & 0 deletions lib/matplotlib/cbook.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -2696,3 +2696,80 @@ def __exit__(self, exc_type, exc_value, traceback):
os.rmdir(path)
except OSError:
pass


class _StringFuncParser(object):
# Each element has:
# -The direct function,
# -The inverse function,
# -A boolean indicating whether the function
# is bounded in the interval 0-1

funcs = {'linear': (lambda x: x, lambda x: x, True),
'quadratic': (lambda x: x**2, lambda x: x**(1. / 2), True),
'cubic': (lambda x: x**3, lambda x: x**(1. / 3), True),
'sqrt': (lambda x: x**(1. / 2), lambda x: x**2, True),
'crt': (lambda x: x**(1. / 3), lambda x: x**3, True),
'log10': (lambda x: np.log10(x), lambda x: (10**(x)), False),
'log': (lambda x: np.log(x), lambda x: (np.exp(x)), False),
'power{a}': (lambda x, a: x**a,
lambda x, a: x**(1. / a), True),
'root{a}': (lambda x, a: x**(1. / a),
lambda x, a: x**a, True),
'log10(x+{a})': (lambda x, a: np.log10(x + a),
lambda x, a: 10**x - a, True),
'log(x+{a})': (lambda x, a: np.log(x + a),
lambda x, a: np.exp(x) - a, True)}

def __init__(self, str_func):
self.str_func = str_func

def is_string(self):
return not hasattr(self.str_func, '__call__')

def get_func(self):
return self._get_element(0)

def get_invfunc(self):
return self._get_element(1)

def is_bounded_0_1(self):
return self._get_element(2)

def _get_element(self, ind):
if not self.is_string():
raise ValueError("The argument passed is not a string.")

str_func = six.text_type(self.str_func)
# Checking if it comes with a parameter
param = None
regex = '\{(.*?)\}'
search = re.search(regex, str_func)
if search is not None:
parstring = search.group(1)

try:
param = float(parstring)
except:
raise ValueError("'a' in parametric function strings must be "
"replaced by a number different than 0, "
"e.g. 'log10(x+{0.1})'.")
if param == 0:
raise ValueError("'a' in parametric function strings must be "
"replaced by a number different than 0.")
str_func = re.sub(regex, '{a}', str_func)

try:
output = self.funcs[str_func][ind]
if param is not None:
output = (lambda x, output=output: output(x, param))

return output
except KeyError:
raise ValueError("%s: invalid function. The only strings "
"recognized as functions are %s." %
(str_func, self.funcs.keys()))
except:
raise ValueError("Invalid function. The only strings recognized "
"as functions are %s." %
(self.funcs.keys()))
150 changes: 61 additions & 89 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,15 +968,15 @@ class FuncNorm(Normalize):

def __init__(self, f, finv=None, **normalize_kw):
"""
Specify the function to be used (and its inverse), as well as other
Specify the function to be used, and its inverse, as well as other
parameters to be passed to `Normalize`. The normalization will be
calculated as (f(x)-f(vmin))/(f(max)-f(vmin)).

Parameters
----------
f : callable or string
Function to be used for the normalization receiving a single
parameter, compatible with scalar values, and ndarrays.
parameter, compatible with scalar values and ndarrays.
Alternatively a string from the list ['linear', 'quadratic',
'cubic', 'sqrt', 'crt','log', 'log10', 'power{a}', 'root{a}',
'log(x+{a})', 'log10(x+{a})'] can be used, replacing 'a' by a
Expand Down Expand Up @@ -1004,7 +1004,11 @@ def __init__(self, f, finv=None, **normalize_kw):

"""

f, finv = FuncNorm._func_parser([f, finv])
func_parser = cbook._StringFuncParser(f)
if func_parser.is_string():
f = func_parser.get_func()
finv = func_parser.get_invfunc()

if finv is None:
raise ValueError("Inverse function finv not provided")

Expand Down Expand Up @@ -1033,8 +1037,8 @@ def __call__(self, value, clip=None):
-------
result : masked array of floats
Normalized data to the `[0.0, 1.0]` interval. If clip == False,
the values original below vmin or above vmax will be assigned to
-0.1 or 1.1, respectively.
the values original below vmin and above vmax will be assigned to
-0.1 and 1.1, respectively.

"""
if clip is None:
Expand Down Expand Up @@ -1090,68 +1094,6 @@ def inverse(self, value):
value * (self._f(vmax) - self._f(vmin)) + self._f(vmin))
return value

@staticmethod
def _func_parser(funcsin, onlybounded=False):
if hasattr(funcsin[0], '__call__'):
return funcsin
# Each element has the direct, the inverse, and and interval indicating
# wether the function is bounded in the interval 0-1
funcs = {'linear': (lambda x: x, lambda x: x, True),
'quadratic': (lambda x: x**2, lambda x: x**(1. / 2), True),
'cubic': (lambda x: x**3, lambda x: x**(1. / 3), True),
'sqrt': (lambda x: x**(1. / 2), lambda x: x**2, True),
'crt': (lambda x: x**(1. / 3), lambda x: x**3, True),
'log10': (lambda x: np.log10(x), lambda x: (10**(x)), False),
'log': (lambda x: np.log(x), lambda x: (np.exp(x)), False),
'power{a}': (lambda x, a: x**a,
lambda x, a: x**(1. / a), True),
'root{a}': (lambda x, a: x**(1. / a),
lambda x, a: x**a, True),
'log10(x+{a})': (lambda x, a: np.log10(x + a),
lambda x, a: 10**x - a, True),
'log(x+{a})': (lambda x, a: np.log(x + a),
lambda x, a: np.exp(x) - a, True)}

# Checking if it comes with a parameter
param = None
regex = '\{(.*?)\}'
search = re.search(regex, funcsin[0])
if search is not None:
parstring = search.group(1)

try:
param = float(parstring)
except:
raise ValueError("'a' in parametric function strings must be "
"replaced by a number different than 0, "
"e.g. 'log10(x+{0.1})'.")
if param == 0:
raise ValueError("'a' in parametric function strings must be "
"replaced by a number different than 0.")
funcsin[0] = re.sub(regex, '{a}', funcsin[0])

try:
output = funcs[six.text_type(funcsin[0])]
if onlybounded and not output[2]:
raise ValueError("Only functions bounded in the [0, 1]"
"domain are allowed: %s" %
[key for key in funcs.keys() if funcs[key][2]]
)

if param is not None:
output = (lambda x, output=output: output[0](x, param),
lambda x, output=output: output[1](x, param),
output[2])
return output[0:2]
except KeyError:
raise ValueError("%s: invalid function. The only strings "
"recognized as functions are %s." %
(funcsin[0], funcs.keys()))
except:
raise ValueError("Invalid function. The only strings recognized "
"as functions are %s." %
(funcs.keys()))

@staticmethod
def _fun_normalizer(fun):
if fun(0.) == 0. and fun(1.) == 1.:
Expand Down Expand Up @@ -1246,10 +1188,10 @@ def __init__(self, flist,
refpoints_cm=[None],
**normalize_kw):
"""
Specify a series of functions, as well as intervals to map the data
Specify a series of functions, as well as intervals, to map the data
space into `[0,1]`. Each individual function may not diverge in the
[0,1] interval, as they will be normalized as
fnorm(x)=(f(x)-f(0))/(f(1)-f(0)), to guarantee that fnorm(0)=0 and
fnorm(x)=(f(x)-f(0))/(f(1)-f(0)) to guarantee that fnorm(0)=0 and
fnorm(1)=1. Then each function will be transformed to map a different
data range [d0, d1] into colormap ranges [cm0, cm1] as
ftrans=fnorm((x-d0)/(d1-d0))*(cm1-cm0)+cm0.
Expand All @@ -1270,8 +1212,9 @@ def __init__(self, flist,
Reference points for the colorbar ranges which will go as
`[0., refpoints_cm[0]]`,... ,
`[refpoints_cm[i], refpoints_cm[i+1]]`,
`[refpoints_cm[-1], 0.]`, and for the data
ranges which will go as `[self.vmin, refpoints_data[0]]`,... ,
`[refpoints_cm[-1], 0.]`,
and for the data ranges which will go as
`[self.vmin, refpoints_data[0]]`,... ,
`[refpoints_data[i], refpoints_data[i+1]]`,
`[refpoints_cm[-1], self.vmax]`
It must satisfy
Expand Down Expand Up @@ -1335,17 +1278,30 @@ def __init__(self, flist,
"The values for the reference points for the data "
"`refpoints_data` must be monotonically increasing")

# Parsing the function strings if any:
self._flist = []
self._finvlist = []
for i in range(len(flist)):
funs = FuncNorm._func_parser((flist[i], finvlist[i]))
if funs[0] is None or funs[1] is None:
func_parser = cbook._StringFuncParser(flist[i])
if func_parser.is_string():
if not func_parser.is_bounded_0_1():
raise ValueError("Only functions bounded in the "
"[0, 1] domain are allowed.")

f = func_parser.get_func()
finv = func_parser.get_invfunc()
else:
f = flist[i]
finv = finvlist[i]
if f is None:
raise ValueError(
"Function not provided for %i range" % i)

if finv is None:
raise ValueError(
"Inverse function not provided for %i range" % i)

self._flist.append(FuncNorm._fun_normalizer(funs[0]))
self._finvlist.append(FuncNorm._fun_normalizer(funs[1]))
self._flist.append(FuncNorm._fun_normalizer(f))
self._finvlist.append(FuncNorm._fun_normalizer(finv))

# We just say linear, becuase we cannot really make the function unless
# We now vmin, and vmax, and that does happen till the object is called
Expand Down Expand Up @@ -1479,10 +1435,10 @@ def ticks(self, nticks=None):

class MirrorPiecewiseNorm(PiecewiseNorm):
"""
Normalization alowing a dual `PiecewiseNorm` simmetrically around a point.
Normalization allowing a dual `PiecewiseNorm` symmetrically around a point.

Data above `center_data` will be normalized independently that data below
it. If only one function is give, the normalization will be symmetric
it. If only one function is given, the normalization will be symmetric
around that point.
"""

Expand All @@ -1492,13 +1448,8 @@ def __init__(self,
center_data=0.0, center_cm=.5,
**normalize_kw):
"""
Specify a series of functions, as well as intervals to map the data
space into `[0,1]`. Each individual function may not diverge in the
[0,1] interval, as they will be normalized as
fnorm(x)=(f(x)-f(0))/(f(1)-f(0)), to guarantee that fnorm(0)=0 and
fnorm(1)=1. Then each function will be transformed to map a different
data range [d0, d1] into colormap ranges [cm0, cm1] as
ftrans=fnorm((x-d0)/(d1-d0))*(cm1-cm0)+cm0.
Specify two functions to normalize the data above and below a
provided reference point.

Parameters
----------
Expand Down Expand Up @@ -1548,8 +1499,22 @@ def __init__(self,
fneg = fpos
fneginv = fposinv

fpos, fposinv = PiecewiseNorm._func_parser([fpos, fposinv])
fneg, fneginv = PiecewiseNorm._func_parser([fneg, fneginv])
error_bounded = ("Only functions bounded in the "
"[0, 1] domain are allowed.")

func_parser = cbook._StringFuncParser(fpos)
if func_parser.is_string():
if not func_parser.is_bounded_0_1():
raise ValueError(error_bounded)
fpos = func_parser.get_func()
fposinv = func_parser.get_invfunc()

func_parser = cbook._StringFuncParser(fneg)
if func_parser.is_string():
if not func_parser.is_bounded_0_1():
raise ValueError(error_bounded)
fneg = func_parser.get_func()
fneginv = func_parser.get_invfunc()

if fposinv is None:
raise ValueError(
Expand All @@ -1565,6 +1530,13 @@ def __init__(self,
refpoints_cm = np.array([center_cm])
refpoints_data = np.array([center_data])

# It is important to normalize the functions before
# applying the -fneg(-x + 1) + 1) transformation
fneg = FuncNorm._fun_normalizer(fneg)
fpos = FuncNorm._fun_normalizer(fpos)
fposinv = FuncNorm._fun_normalizer(fposinv)
fneginv = FuncNorm._fun_normalizer(fneginv)

flist = [(lambda x:(-fneg(-x + 1) + 1)), fpos]
finvlist = [(lambda x:(-fneginv(-x + 1) + 1)), fposinv]

Expand All @@ -1582,7 +1554,7 @@ class MirrorRootNorm(MirrorPiecewiseNorm):
`MirrorPiecewiseNorm`.

Data above `center_data` will be normalized with a root of the order
`orderpos` and data below it with a symmetric root of order `orderg neg`.
`orderpos` and data below it with a symmetric root of order `orderneg`.
If only `orderpos` function is given, the normalization will be completely
mirrored.

Expand Down Expand Up @@ -1611,7 +1583,7 @@ def __init__(self, orderpos=2, orderneg=None,
below `center_data` using `MirrorPiecewiseNorm`. Default
`orderpos`.
center_data : float, optional
Value in the data that will separate range. Must be
Value in the data that will separate the ranges. Must be
in the (vmin, vmax) range.
Default 0.0.
center_cm : float, optional
Expand Down
33 changes: 33 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,36 @@ def test_flatiter():

assert 0 == next(it)
assert 1 == next(it)


class TestFuncParser(object):
x_test = np.linspace(0.01, 0.5, 3)
validstrings = ['linear', 'quadratic', 'cubic', 'sqrt', 'crt',
'log', 'log10', 'power{1.5}', 'root{2.5}',
'log(x+{0.5})', 'log10(x+{0.1})']
results = [(lambda x: x),
(lambda x: x**2),
(lambda x: x**3),
(lambda x: x**(1. / 2)),
(lambda x: x**(1. / 3)),
(lambda x: np.log(x)),
(lambda x: np.log10(x)),
(lambda x: x**1.5),
(lambda x: x**(1 / 2.5)),
(lambda x: np.log(x + 0.5)),
(lambda x: np.log10(x + 0.1))]

@pytest.mark.parametrize("string", validstrings, ids=validstrings)
def test_inverse(self, string):
func_parser = cbook._StringFuncParser(string)
f = func_parser.get_func()
finv = func_parser.get_invfunc()
assert_array_almost_equal(finv(f(self.x_test)), self.x_test)

@pytest.mark.parametrize("string, func",
zip(validstrings, results),
ids=validstrings)
def test_values(self, string, func):
func_parser = cbook._StringFuncParser(string)
f = func_parser.get_func()
assert_array_almost_equal(f(self.x_test), func(self.x_test))
Loading
0