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

Skip to content

Commit eff243f

Browse files
committed
Simplify units.Registry.get_converter.
The caching of the converter was disabled 10 years ago (be73ef4) and is unlikely to ever work as the correct converter for ndarrays depends on the type of the contents, not on the container, so just drop the cache-related code. When a masked array is passed, it's OK to just get the first underlying data, even if it is masked (as it should have the same type as the unmasked entries), for the purpose of getting the converter.
1 parent 31d2c2f commit eff243f

File tree

1 file changed

+28
-70
lines changed

1 file changed

+28
-70
lines changed

lib/matplotlib/units.py

Lines changed: 28 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ def default_units(x, axis):
5151

5252
class AxisInfo(object):
5353
"""
54-
Information to support default axis labeling, tick labeling, and
55-
default limits. An instance of this class must be returned by
56-
:meth:`ConversionInterface.axisinfo`.
54+
Information to support default axis labeling, tick labeling, and limits.
55+
56+
An instance of this class must be returned by
57+
`ConversionInterface.axisinfo`.
5758
"""
5859
def __init__(self, majloc=None, minloc=None,
5960
majfmt=None, minfmt=None, label=None,
@@ -92,8 +93,7 @@ class ConversionInterface(object):
9293
@staticmethod
9394
def axisinfo(unit, axis):
9495
"""
95-
Return an `~units.AxisInfo` instance for the axis with the
96-
specified units.
96+
Return an `~units.AxisInfo` for the axis with the specified units.
9797
"""
9898
return None
9999

@@ -108,19 +108,19 @@ def default_units(x, axis):
108108
def convert(obj, unit, axis):
109109
"""
110110
Convert *obj* using *unit* for the specified *axis*.
111-
If *obj* is a sequence, return the converted sequence.
112-
The output must be a sequence of scalars that can be used by the numpy
113-
array layer.
111+
112+
If *obj* is a sequence, return the converted sequence. The output must
113+
be a sequence of scalars that can be used by the numpy array layer.
114114
"""
115115
return obj
116116

117117
@staticmethod
118118
def is_numlike(x):
119119
"""
120-
The Matplotlib datalim, autoscaling, locators etc work with
121-
scalars which are the units converted to floats given the
122-
current unit. The converter may be passed these floats, or
123-
arrays of them, even when units are set.
120+
The Matplotlib datalim, autoscaling, locators etc work with scalars
121+
which are the units converted to floats given the current unit. The
122+
converter may be passed these floats, or arrays of them, even when
123+
units are set.
124124
"""
125125
if iterable(x):
126126
for thisx in x:
@@ -130,70 +130,28 @@ def is_numlike(x):
130130

131131

132132
class Registry(dict):
133-
"""
134-
A register that maps types to conversion interfaces.
135-
"""
136-
def __init__(self):
137-
dict.__init__(self)
138-
self._cached = {}
133+
"""Register types with conversion interface."""
139134

140135
def get_converter(self, x):
141-
"""
142-
Get the converter for data that has the same type as *x*. If no
143-
converters are registered for *x*, returns ``None``.
144-
"""
145-
146-
if not len(self):
147-
return None # nothing registered
148-
# DISABLED idx = id(x)
149-
# DISABLED cached = self._cached.get(idx)
150-
# DISABLED if cached is not None: return cached
151-
152-
converter = None
153-
classx = getattr(x, '__class__', None)
154-
155-
if classx is not None:
156-
converter = self.get(classx)
157-
158-
if converter is None and hasattr(x, "values"):
159-
# this unpacks pandas series or dataframes...
160-
x = x.values
161-
162-
# If x is an array, look inside the array for data with units
163-
if isinstance(x, np.ndarray) and x.size:
164-
xravel = x.ravel()
136+
"""Get the converter interface instance for *x*, or None."""
137+
if isinstance(x, np.ndarray):
138+
# In case x in a masked array, access the underlying data (only its
139+
# type matters). If x is a regular ndarray, getdata() just returns
140+
# the array itself.
141+
x = np.ma.getdata(x).ravel()
142+
try:
143+
return self[type(x)]
144+
except KeyError:
165145
try:
166-
# pass the first value of x that is not masked back to
167-
# get_converter
168-
if not np.all(xravel.mask):
169-
# some elements are not masked
170-
converter = self.get_converter(
171-
xravel[np.argmin(xravel.mask)])
172-
return converter
173-
except AttributeError:
174-
# not a masked_array
175-
# Make sure we don't recurse forever -- it's possible for
176-
# ndarray subclasses to continue to return subclasses and
177-
# not ever return a non-subclass for a single element.
178-
next_item = xravel[0]
179-
if (not isinstance(next_item, np.ndarray) or
180-
next_item.shape != x.shape):
181-
converter = self.get_converter(next_item)
182-
return converter
183-
184-
# If we haven't found a converter yet, try to get the first element
185-
if converter is None:
186-
try:
187-
thisx = safe_first_element(x)
146+
first = safe_first_element(x)
188147
except (TypeError, StopIteration):
189148
pass
190149
else:
191-
if classx and classx != getattr(thisx, '__class__', None):
192-
converter = self.get_converter(thisx)
193-
return converter
194-
195-
# DISABLED self._cached[idx] = converter
196-
return converter
150+
# Avoid infinite recursion for pathological iterables for which
151+
# indexing returns instances of the same iterable class.
152+
if type(first) is not type(x):
153+
return self.get_converter(first)
154+
return None
197155

198156

199157
registry = Registry()

0 commit comments

Comments
 (0)