8000 General cleanup of Transform classes in scale.py. · matplotlib/matplotlib@7a6ad6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a6ad6a

Browse files
committed
General cleanup of Transform classes in scale.py.
1 parent 684861b commit 7a6ad6a

File tree

1 file changed

+30
-103
lines changed

1 file changed

+30
-103
lines changed

lib/matplotlib/scale.py

Lines changed: 30 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,6 @@ def get_transform(self):
8484
return IdentityTransform()
8585

8686

87-
def _mask_non_positives(a):
88-
"""
89-
Return a Numpy array where all non-positive values are
90-
replaced with NaNs. If there are no non-positive values, the
91-
original array is returned.
92-
"""
93-
mask = a <= 0.0
94-
if mask.any():
95-
return np.where(mask, np.nan, a)
96-
return a
97-
98-
99-
def _clip_non_positives(a):
100-
a = np.array(a, float)
101-
a[a <= 0.0] = 1e-300
102-
return a
103-
104-
105 10000 87
class LogTransformBase(Transform):
10688
input_dims = 1
10789
output_dims = 1
@@ -111,31 +93,37 @@ class LogTransformBase(Transform):
11193
def __init__(self, nonpos):
11294
Transform.__init__(self)
11395
if nonpos == 'mask':
114-
self._handle_nonpos = _mask_non_positives
96+
self._fill_value = np.nan
11597
else:
116-
self._handle_nonpos = _clip_non_positives
117-
118-
119-
class Log10Transform(LogTransformBase):
120-
base = 10.0
98+
self._fill_value = 1e-300
12199

122100
def transform_non_affine(self, a):
123-
a = self._handle_nonpos(a * 10.0)
124-
return np.log10(a)
125-
126-
def inverted(self):
127-
return InvertedLog10Transform()
101+
a = np.where(a <= 0, self._fill_value, a)
102+
np.log(a, a)
103+
a /= np.log(self.base)
104+
a += 1
105+
return a
128106

129107

130-
class InvertedLog10Transform(Transform):
108+
class InvertedLogTransformBase(Transform):
131109
input_dims = 1
132110
output_dims = 1
133111
is_separable = True
134112
has_inverse = True
135-
base = 10.0
136113

137114
def transform_non_affine(self, a):
138-
return ma.power(10.0, a) / 10.0
115+
return ma.power(self.base, a - 1)
116+
117+
118+
class Log10Transform(LogTransformBase):
119+
base = 10.0
120+
121+
def inverted(self):
122+
return InvertedLog10Transform()
123+
124+
125+
class InvertedLog10Transform(InvertedLogTransformBase):
126+
base = 10.0
139127

140128
def inverted(self):
141129
return Log10Transform()
@@ -144,88 +132,45 @@ def inverted(self):
144132
class Log2Transform(LogTransformBase):
145133
base = 2.0
146134

147-
def transform_non_affine(self, a):
148-
a = self._handle_nonpos(a * 2.0)
149-
return np.log2(a)
150-
151135
def inverted(self):
152136
return InvertedLog2Transform()
153137

154138

155-
class InvertedLog2Transform(Transform):
156-
input_dims = 1
157-
output_dims = 1
158-
is_separable = True
159-
has_inverse = True
139+
class InvertedLog2Transform(InvertedLogTransformBase):
160140
base = 2.0
161141

162-
def transform_non_affine(self, a):
163-
return ma.power(2.0, a) / 2.0
164-
165142
def inverted(self):
166143
return Log2Transform()
167144

168145

169146
class NaturalLogTransform(LogTransformBase):
170147
base = np.e
171148

172-
def transform_non_affine(self, a):
173-
a = self._handle_nonpos(a * np.e)
174-
return np.log(a)
175-
176149
def inverted(self):
177150
return InvertedNaturalLogTransform()
178151

179152

180-
class InvertedNaturalLogTransform(Transform):
181-
input_dims = 1
182-
output_dims = 1
183-
is_separable = True
184-
has_inverse = True
153+
class InvertedNaturalLogTransform(InvertedLogTransformBase):
185154
base = np.e
186155

187-
def transform_non_affine(self, a):
188-
return ma.power(np.e, a) / np.e
189-
190156
def inverted(self):
191157
return NaturalLogTransform()
192158

193159

194-
class LogTransform(Transform):
195-
input_dims = 1
196-
output_dims = 1
197-
is_separable = True
198-
has_inverse = True
199-
160+
class LogTransform(LogTransformBase):
200161
def __init__(self, base, nonpos):
201-
Transform.__init__(self)
162+
LogTransformBase.__init__(self, nonpos)
202163
self.base = base
203-
if nonpos == 'mask':
204-
self._handle_nonpos = _mask_non_positives
205-
else:
206-
self._handle_nonpos = _clip_non_positives
207-
208-
def transform_non_affine(self, a):
209-
a = self._handle_nonpos(a * self.base)
210-
return np.log(a) / np.log(self.base)
211164

212165
def inverted(self):
213166
return InvertedLogTransform(self.base)
214167

215168

216-
class InvertedLogTransform(Transform):
217-
input_dims = 1
218-
output_dims = 1
219-
is_separable = True
220-
has_inverse = True
221-
169+
class InvertedLogTransform(InvertedLogTransformBase):
222170
def __init__(self, base):
223-
Transform.__init__(self)
171+
InvertedLogTransformBase.__init__(self)
224172
self.base = base
225173

226-
def transform_non_affine(self, a):
227-
return ma.power(self.base, a) / self.base
228-
229174
def inverted(self):
230175
return LogTransform(self.base)
231176

@@ -474,25 +419,6 @@ def get_transform(self):
474419
return self._transform
475420

476421

477-
def _mask_non_logit(a):
478-
"""
479-
Return a Numpy array where all values outside ]0, 1[ are
480-
replaced with NaNs. If all values are inside ]0, 1[, the original
481-
array is returned.
482-
"""
483-
mask = (a <= 0.0) | (a >= 1.0)
484-
if mask.any():
485-
return np.where(mask, np.nan, a)
486-
return a
487-
488-
489-
def _clip_non_logit(a):
490-
a = np.array(a, float)
491-
a[a <= 0.0] = 1e-300
492-
a[a >= 1.0] = 1 - 1e-300
493-
return a
494-
495-
496422
class LogitTransform(Transform):
497423
input_dims = 1
498424
output_dims = 1
@@ -502,14 +428,15 @@ class LogitTransform(Transform):
502428
def __init__(self, nonpos):
503429
Transform.__init__(self)
504430
if nonpos == 'mask':
505-
self._handle_nonpos = _mask_non_logit
431+
self._fill_value = np.nan
506432
else:
507-
self._handle_nonpos = _clip_non_logit
433+
self._fill_value = 1e-300
508434
self._nonpos = nonpos
509435

510436
def transform_non_affine(self, a):
511437
"""logit transform (base 10), masked or clipped"""
512-
a = self._handle_nonpos(a)
438+
a = np.select(
439+
[a <= 0, a >= 1], [self._fill_value, 1 - self._fill_value], a)
513440
return np.log10(1.0 * a / (1.0 - a))
514441

515442
def inverted(self):

0 commit comments

Comments
 (0)
0