Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 467f536

Browse files
committed
Deprecate redundant log-scale transform classes.
1 parent 93b80e2 commit 467f536

File tree

4 files changed

+74
-45
lines changed

4 files changed

+74
-45
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Deprecations
2+
````````````
3+
4+
- The ``LogTransformBase``, ``Log10Transform``, ``Log2Transform``,
5+
``NaturalLogTransformLog``, ``InvertedLogTransformBase``,
6+
``InvertedLog10Transform``, ``InvertedLog2Transform``, and
7+
``InvertedNaturalLogTransform`` classes (all defined in
8+
:mod:`matplotlib.scales`) are deprecated. As a replacement, use the general
9+
`LogTransform` and `InvertedLogTransform` classes, whose constructors take a
10+
*base* argument.

lib/matplotlib/scale.py

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def get_transform(self):
9494
return IdentityTransform()
9595

9696

97+
@cbook.deprecated("3.1", alternative="LogTransform")
9798
class LogTransformBase(Transform):
9899
input_dims = 1
99100
output_dims = 1
@@ -105,28 +106,14 @@ def __init__(self, nonpos='clip'):
105106
self._clip = {"clip": True, "mask": False}[nonpos]
106107

107108
def transform_non_affine(self, a):
108-
# Ignore invalid values due to nans being passed to the transform
109-
with np.errstate(divide="ignore", invalid="ignore"):
110-
out = np.log(a)
111-
out /= np.log(self.base)
112-
if self._clip:
113-
# SVG spec says that conforming viewers must support values up
114-
# to 3.4e38 (C float); however experiments suggest that
115-
# Inkscape (which uses cairo for rendering) runs into cairo's
116-
# 24-bit limit (which is apparently shared by Agg).
117-
# Ghostscript (used for pdf rendering appears to overflow even
118-
# earlier, with the max value around 2 ** 15 for the tests to
119-
# pass. On the other hand, in practice, we want to clip beyond
120-
# np.log10(np.nextafter(0, 1)) ~ -323
121-
# so 1000 seems safe.
122-
out[a <= 0] = -1000
123-
return out
109+
return LogTransform.transform_non_affine(self, a)
124110

125111
def __str__(self):
126112
return "{}({!r})".format(
127113
type(self).__name__, "clip" if self._clip else "mask")
128114

129115

116+
@cbook.deprecated("3.1", alternative="InvertedLogTransform")
130117
class InvertedLogTransformBase(Transform):
131118
input_dims = 1
132119
output_dims = 1
@@ -140,79 +127,118 @@ def __str__(self):
140127
return "{}()".format(type(self).__name__)
141128

142129

130+
@cbook.deprecated("3.1", alternative="LogTransform")
143131
class Log10Transform(LogTransformBase):
144132
base = 10.0
145133

146134
def inverted(self):
147135
return InvertedLog10Transform()
148136

149137

138+
@cbook.deprecated("3.1", alternative="InvertedLogTransform")
150139
class InvertedLog10Transform(InvertedLogTransformBase):
151140
base = 10.0
152141

153142
def inverted(self):
154143
return Log10Transform()
155144

156145

146+
@cbook.deprecated("3.1", alternative="LogTransform")
157147
class Log2Transform(LogTransformBase):
158148
base = 2.0
159149

160150
def inverted(self):
161151
return InvertedLog2Transform()
162152

163153

154+
@cbook.deprecated("3.1", alternative="InvertedLogTransform")
164155
class InvertedLog2Transform(InvertedLogTransformBase):
165156
base = 2.0
166157

167158
def inverted(self):
168159
return Log2Transform()
169160

170161

162+
@cbook.deprecated("3.1", alternative="LogTransform")
171163
class NaturalLogTransform(LogTransformBase):
172164
base = np.e
173165

174166
def inverted(self):
175167
return InvertedNaturalLogTransform()
176168

177169

170+
@cbook.deprecated("3.1", alternative="InvertedLogTransform")
178171
class InvertedNaturalLogTransform(InvertedLogTransformBase):
179172
base = np.e
180173

181174
def inverted(self):
182175
return NaturalLogTransform()
183176

184177

185-
class LogTransform(LogTransformBase):
178+
class LogTransform(Transform):
179+
input_dims = 1
180+
output_dims = 1
181+
is_separable = True
182+
has_inverse = True
183+
186184
def __init__(self, base, nonpos='clip'):
187-
LogTransformBase.__init__(self, nonpos)
185+
Transform.__init__(self)
188186
self.base = base
187+
self._clip = {"clip": True, "mask": False}[nonpos]
188+
189+
def __str__(self):
190+
return "{}(base={}, nonpos={!r})".format(
191+
type(self).__name__, self.base, "clip" if self._clip else "mask")
192+
193+
def transform_non_affine(self, a):
194+
# Ignore invalid values due to nans being passed to the transform.
195+
with np.errstate(divide="ignore", invalid="ignore"):
196+
log = {np.e: np.log, 2: np.log2, 10: np.log10}.get(self.base)
197+
if log: # If possible, do everything in a single call to Numpy.
198+
out = log(a)
199+
else:
200+
out = np.log(a)
201+
out /= np.log(self.base)
202+
if self._clip:
203+
# SVG spec says that conforming viewers must support values up
204+
# to 3.4e38 (C float); however experiments suggest that
205+
# Inkscape (which uses cairo for rendering) runs into cairo's
206+
# 24-bit limit (which is apparently shared by Agg).
207+
# Ghostscript (used for pdf rendering appears to overflow even
208+
# earlier, with the max value around 2 ** 15 for the tests to
209+
# pass. On the other hand, in practice, we want to clip beyond
210+
# np.log10(np.nextafter(0, 1)) ~ -323
211+
# so 1000 seems safe.
212+
out[a <= 0] = -1000
213+
return out
189214

190215
def inverted(self):
191216
return InvertedLogTransform(self.base)
192217

193218

194-
class InvertedLogTransform(InvertedLogTransformBase):
219+
class InvertedLogTransform(Transform):
220+
input_dims = 1
221+
output_dims = 1
222+
is_separable = True
223+
has_inverse = True
224+
195225
def __init__(self, base):
196-
InvertedLogTransformBase.__init__(self)
226+
Transform.__init__(self)
197227
self.base = base
198228

229+
def __str__(self):
230+
return "{}(base={})".format(type(self).__name__, self.base)
231+
232+
def transform_non_affine(self, a):
233+
return ma.power(self.base, a)
234+
199235
def inverted(self):
200236
return LogTransform(self.base)
201237

202238

203239
class LogScale(ScaleBase):
204240
"""
205-
A standard logarithmic scale. Care is taken so non-positive
206-
values are not plotted.
207-
208-
For computational efficiency (to push as much as possible to Numpy
209-
C code in the common cases), this scale provides different
210-
transforms depending on the base of the logarithm:
211-
212-
- base 10 (:class:`Log10Transform`)
213-
- base 2 (:class:`Log2Transform`)
214-
- base e (:class:`NaturalLogTransform`)
215-
- arbitrary base (:class:`LogTransform`)
241+
A standard logarithmic scale. Care is taken to only plot positive values.
216242
"""
217243
name = 'log'
218244

@@ -264,18 +290,13 @@ def __init__(self, axis, **kwargs):
264290
if base <= 0 or base == 1:
265291
raise ValueError('The log base cannot be <= 0 or == 1')
266292

267-
if base == 10.0:
268-
self._transform = self.Log10Transform(nonpos)
269-
elif base == 2.0:
270-
self._transform = self.Log2Transform(nonpos)
271-
elif base == np.e:
272-
self._transform = self.NaturalLogTransform(nonpos)
273-
else:
274-
self._transform = self.LogTransform(base, nonpos)
275-
276-
self.base = base
293+
self._transform = self.LogTransform(base, nonpos)
277294
self.subs = subs
278295

296+
@property
297+
def base(self):
298+
return self._transform.base
299+
279300
def set_default_locators_and_formatters(self, axis):
280301
"""
281302
Set the locators and formatters to specialized versions for

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5270,8 +5270,7 @@ def test_title_location_roundtrip():
52705270

52715271

52725272
@image_comparison(baseline_images=["loglog"], remove_text=True,
5273-
extensions=['png'],
5274-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0))
5273+
extensions=['png'], tol=0.02)
52755274
def test_loglog():
52765275
fig, ax = plt.subplots()
52775276
x = np.arange(1, 11)

lib/matplotlib/tests/test_scale.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ def test_logscale_transform_repr():
9999

100100

101101
@image_comparison(baseline_images=['logscale_nonpos_values'], remove_text=True,
102-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0),
103-
extensions=['png'], style='mpl20')
102+
extensions=['png'], tol=0.02, style='mpl20')
104103
def test_logscale_nonpos_values():
105104
np.random.seed(19680801)
106105
xs = np.random.normal(size=int(1e3))

0 commit comments

Comments
 (0)