-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Clean up units.py #10370
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
Clean up units.py #10370
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
""" | ||
The classes here provide support for using custom classes with | ||
matplotlib, e.g., those that do not expose the array interface but know | ||
Matplotlib, e.g., those that do not expose the array interface but know | ||
how to convert themselves to arrays. It also supports classes with | ||
units and units conversion. Use cases include converters for custom | ||
objects, e.g., a list of datetime objects, as well as for objects that | ||
are unit aware. We don't assume any particular units implementation; | ||
rather a units implementation must provide the register with the Registry | ||
converter dictionary and a ConversionInterface. For example, | ||
converter dictionary and a `ConversionInterface`. For example, | ||
here is a complete implementation which supports plotting with native | ||
datetime objects:: | ||
|
||
|
@@ -37,7 +37,7 @@ def default_units(x, axis): | |
'return the default unit for x or None' | ||
return 'date' | ||
|
||
# finally we register our object type with a converter | ||
# Finally we register our object type with a converter | ||
units.registry[datetime.date] = DateConverter() | ||
|
||
""" | ||
|
@@ -51,17 +51,29 @@ def default_units(x, axis): | |
|
||
|
||
class AxisInfo(object): | ||
"""information to support default axis labeling and tick labeling, and | ||
default limits""" | ||
""" | ||
Information to support default axis labeling, tick labeling, and | ||
default limits. | ||
""" | ||
def __init__(self, majloc=None, minloc=None, | ||
majfmt=None, minfmt=None, label=None, | ||
default_limits=None): | ||
""" | ||
majloc and minloc: TickLocators for the major and minor ticks | ||
majfmt and minfmt: TickFormatters for the major and minor ticks | ||
label: the default axis label | ||
default_limits: the default min, max of the axis if no data is present | ||
If any of the above are None, the axis will simply use the default | ||
Parameters | ||
---------- | ||
majloc, minloc : | ||
TickLocators for the major and minor ticks. | ||
majfmt, minfmt : | ||
TickFormatters for the major and minor ticks. | ||
label : | ||
The default axis label | ||
default_limits : | ||
The default min, max of the axis if no data is present | ||
|
||
Notes | ||
----- | ||
If any of the above are ``None``, the axis will simply use the | ||
default value. | ||
""" | ||
self.majloc = majloc | ||
self.minloc = minloc | ||
|
@@ -74,37 +86,37 @@ def __init__(self, majloc=None, minloc=None, | |
class ConversionInterface(object): | ||
""" | ||
The minimal interface for a converter to take custom instances (or | ||
sequences) and convert them to values mpl can use | ||
sequences) and convert them to values Matplotlib can use. | ||
""" | ||
@staticmethod | ||
def axisinfo(unit, axis): | ||
'return an units.AxisInfo instance for axis with the specified units' | ||
''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tripple double quotes (PEP 257) |
||
Return an `~units.AxisInfo` instance for axis with the specified units. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the axis |
||
''' | ||
return None | ||
|
||
@staticmethod | ||
def default_units(x, axis): | ||
'return the default unit for x or None for the given axis' | ||
'Return the default unit for *x* or ``None`` for the given axis.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tripple double quotes |
||
return None | ||
|
||
@staticmethod | ||
def convert(obj, unit, axis): | ||
""" | ||
convert obj using unit for the specified axis. If obj is a sequence, | ||
return the converted sequence. The output must be a sequence of | ||
scalars that can be used by the numpy array layer | ||
Convert *obj* using *unit* for the specified *axis*. | ||
If *obj* is a sequence, return the converted sequence. | ||
The output must be a sequence of scalars that can be used by the numpy | ||
array layer. | ||
""" | ||
return obj | ||
|
||
@staticmethod | ||
def is_numlike(x): | ||
""" | ||
The matplotlib datalim, autoscaling, locators etc work with | ||
The Matplotlib datalim, autoscaling, locators etc work with | ||
scalars which are the units converted to floats given the | ||
current unit. The converter may be passed these floats, or | ||
arrays of them, even when units are set. Derived conversion | ||
interfaces may opt to pass plain-ol unitless numbers through | ||
the conversion interface and this is a helper function for | ||
them. | ||
arrays of them, even when units are set. | ||
""" | ||
if iterable(x): | ||
for thisx in x: | ||
|
@@ -115,14 +127,14 @@ def is_numlike(x): | |
|
||
class Registry(dict): | ||
""" | ||
register types with conversion interface | ||
Register types with conversion interface. | ||
""" | ||
def __init__(self): | ||
dict.__init__(self) | ||
self._cached = {} | ||
|
||
def get_converter(self, x): | ||
'get the converter interface instance for x, or None' | ||
'Get the converter interface instance for *x*, or ``None``.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tripple double quotes |
||
|
||
if not len(self): | ||
return None # nothing registered | ||
|
@@ -153,7 +165,7 @@ def get_converter(self, x): | |
# not ever return a non-subclass for a single element. | ||
next_item = xravel[0] | ||
if (not isinstance(next_item, np.ndarray) or | ||
next_item.shape != x.shape): | ||
next_item.shape != x.shape): | ||
converter = self.get_converter(next_item) | ||
return converter | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
„The colon must be omitted if the type is absent.“ https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt#sections
Also in the following lines.