8000 Cleanup scales by anntzer · Pull Request #7144 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanup scales #7144

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 2 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
134 changes: 30 additions & 104 deletions lib/matplotlib/scale.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ def get_transform(self):
return IdentityTransform()


def _mask_non_positives(a):
"""
Return a Numpy array where all non-positive values are
replaced with NaNs. If there are no non-positive values, the
original array is returned.
"""
mask = a <= 0.0
if mask.any():
return np.where(mask, np.nan, a)
return a


def _clip_non_positives(a):
a = np.array(a, float)
a[a <= 0.0] = 1e-300
return a


class LogTransformBase(Transform):
input_dims = 1
output_dims = 1
Expand All @@ -111,31 +93,35 @@ class LogTransformBase(Transform):
def __init__(self, nonpos):
Transform.__init__(self)
if nonpos == 'mask':
self._handle_nonpos = _mask_non_positives
self._fill_value = np.nan
else:
self._handle_nonpos = _clip_non_positives


class Log10Transform(LogTransformBase):
base = 10.0
self._fill_value = 1e-300

def transform_non_affine(self, a):
a = self._handle_nonpos(a * 10.0)
return np.log10(a)

def inverted(self):
return InvertedLog10Transform()
with np.errstate(invalid="ignore"):
a = np.where(a <= 0, self._fill_value, a)
return np.divide(np.log(a, out=a), np.log(self.base), out=a)


class InvertedLog10Transform(Transform):
class InvertedLogTransformBase(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True
base = 10.0

def transform_non_affine(self, a):
return ma.power(10.0, a) / 10.0
return ma.power(self.base, a)


class Log10Transform(LogTransformBase):
base = 10.0

def inverted(self):
return InvertedLog10Transform()
Copy link
Member

Choose a reason for hiding this comment

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

You could rewrite this like:

class Log10Transform(LogTransformBase):
    base = 10.0
    inverted = InvertedLog10Transform

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because InvertedLog10Transform is not defined yet at that point. (InvertedLog10Transform could define inverted as a reference to the Log10Transform class but I decided to keep definitions symmetric.)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yes. You are right.



class InvertedLog10Transform(InvertedLogTransformBase):
base = 10.0

def inverted(self):
return Log10Transform()
Expand All @@ -144,88 +130,45 @@ def inverted(self):
class Log2Transform(LogTransformBase):
base = 2.0

def transform_non_affine(self, a):
a = self._handle_nonpos(a * 2.0)
return np.log2(a)

def inverted(self):
return InvertedLog2Transform()


class InvertedLog2Transform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True
class InvertedLog2Transform(InvertedLogTransformBase):
base = 2.0

def transform_non_affine(self, a):
return ma.power(2.0, a) / 2.0

def inverted(self):
return Log2Transform()


class NaturalLogTransform(LogTransformBase):
base = np.e

def transform_non_affine(self, a):
a = self._handle_nonpos(a * np.e)
return np.log(a)

def inverted(self):
return InvertedNaturalLogTransform()


class InvertedNaturalLogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True
class InvertedNaturalLogTransform(InvertedLogTransformBase):
base = np.e

def transform_non_affine(self, a):
return ma.power(np.e, a) / np.e

def inverted(self):
return NaturalLogTransform()


class LogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True

class LogTransform(LogTransformBase):
def __init__(self, base, nonpos):
Transform.__init__(self)
LogTransformBase.__init__(self, nonpos)
self.base = base
if nonpos == 'mask':
self._handle_nonpos = _mask_non_positives
else:
self._handle_nonpos = _clip_non_positives

def transform_non_affine(self, a):
a = self._handle_nonpos(a * self.base)
return np.log(a) / np.log(self.base)

def inverted(self):
return InvertedLogTransform(self.base)


class InvertedLogTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True

class InvertedLogTransform(InvertedLogTransformBase):
def __init__(self, base):
Transform.__init__(self)
InvertedLogTransformBase.__init__(self)
self.base = base

def transform_non_affine(self, a):
return ma.power(self.base, a) / self.base

def inverted(self):
return LogTransform(self.base)

Expand Down Expand Up @@ -474,25 +417,6 @@ def get_transform(self):
return self._transform


def _mask_non_logit(a):
"""
Return a Numpy array where all values outside ]0, 1[ are
replaced with NaNs. If all values are inside ]0, 1[, the original
array is returned.
"""
mask = (a <= 0.0) | (a >= 1.0)
if mask.any():
return np.where(mask, np.nan, a)
return a


def _clip_non_logit(a):
a = np.array(a, float)
a[a <= 0.0] = 1e-300
a[a >= 1.0] = 1 - 1e-300
return a


class LogitTransform(Transform):
input_dims = 1
output_dims = 1
Expand All @@ -502,15 +426,17 @@ class LogitTransform(Transform):
def __init__(self, nonpos):
Transform.__init__(self)
if nonpos == 'mask':
self._handle_nonpos = _mask_non_logit
self._fill_value = np.nan
else:
self._handle_nonpos = _clip_non_logit
self._fill_value = 1e-300
self._nonpos = nonpos

def transform_non_affine(self, a):
"""logit transform (base 10), masked or clipped"""
a = self._handle_nonpos(a)
return np.log10(1.0 * a / (1.0 - a))
with np.errstate(invalid="ignore"):
a = np.select(
[a <= 0, a >= 1], [self._fill_value, 1 - self._fill_value], a)
return np.log10(a / (1 - a))

def inverted(self):
return LogisticTransform(self._nonpos)
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/loglog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
0