8000 Replace some uses of np.iterable by anntzer · Pull Request #14271 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Replace some uses of np.iterable #14271

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 1 commit into from
May 20, 2019
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
8 changes: 2 additions & 6 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,7 @@ def julian2num(j):
float or sequence of floats
Matplotlib date(s)
"""
if np.iterable(j):
j = np.asarray(j)
return j - JULIAN_OFFSET
return np.subtract(j, JULIAN_OFFSET) # Handles both scalar & nonscalar j.


def num2julian(n):
Expand All @@ -459,9 +457,7 @@ def num2julian(n):
float or sequence of floats
Julian date(s)
"""
if np.iterable(n):
n = np.asarray(n)
return n + JULIAN_OFFSET
return np.add(n, JULIAN_OFFSET) # Handles both scalar & nonscalar j.


def num2date(x, tz=None):
Expand Down
34 changes: 9 additions & 25 deletions lib/matplotlib/testing/jpl_units/EpochConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np

from matplotlib import cbook
import matplotlib.units as units
import matplotlib.dates as date_ticker

Expand Down Expand Up @@ -95,28 +96,14 @@ def convert(value, unit, axis):
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U

isNotEpoch = True
isDuration = False

if np.iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [EpochConverter.convert(x, unit, axis) for x in value]

if isinstance(value, U.Epoch):
isNotEpoch = False
elif isinstance(value, U.Duration):
isDuration = True

if (isNotEpoch and not isDuration and
units.ConversionInterface.is_numlike(value)):
if not cbook.is_scalar_or_string(value):
return [EpochConverter.convert(x, unit, axis) for x in value]
if (units.ConversionInterface.is_numlike(value)
and not isinstance(value, (U.Epoch, U.Duration))):
return value

if unit is None:
unit = EpochConverter.default_units(value, axis)

if isDuration:
if isinstance(value, U.Duration):
return EpochConverter.duration2float(value)
else:
return EpochConverter.epoch2float(value, unit)
Expand All @@ -131,10 +118,7 @@ def default_units(value, axis):
= RETURN VALUE
- Returns the default units to use for value.
"""
frame = None
if np.iterable(value) and not isinstance(value, str):
return EpochConverter.default_units(value[0], axis)
if cbook.is_scalar_or_string(value):
return value.frame()
else:
frame = value.frame()

return frame
return EpochConverter.default_units(value[0], axis)
30 changes: 8 additions & 22 deletions lib/matplotlib/testing/jpl_units/UnitDblConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np

from matplotlib import cbook
import matplotlib.units as units
import matplotlib.projections.polar as polar

Expand Down Expand Up @@ -83,35 +84,21 @@ def convert(value, unit, axis):
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U

isNotUnitDbl = True

if np.iterable(value) and not isinstance(value, str):
if len(value) == 0:
return []
else:
return [UnitDblConverter.convert(x, unit, axis) for x in value]

# We need to check to see if the incoming value is actually a
# UnitDbl and set a flag. If we get an empty list, then just
# return an empty list.
if isinstance(value, U.UnitDbl):
isNotUnitDbl = False

if not cbook.is_scalar_or_string(value):
return [UnitDblConverter.convert(x, unit, axis) for x in value]
# If the incoming value behaves like a number, but is not a UnitDbl,
# then just return it because we don't know how to convert it
# (or it is already converted)
if isNotUnitDbl and units.ConversionInterface.is_numlike(value):
if (units.ConversionInterface.is_numlike(value)
and not isinstance(value, U.UnitDbl)):
return value

# If no units were specified, then get the default units to use.
if unit is None:
unit = UnitDblConverter.default_units(value, axis)

# Convert the incoming UnitDbl value/values to float/floats
if isinstance(axis.axes, polar.PolarAxes) and value.type() == "angle":
# Guarantee that units are radians for polar plots.
return value.convert("rad")

return value.convert(unit)

@staticmethod
Expand All @@ -125,10 +112,9 @@ def default_units(value, axis):
- Returns the default units to use for value.
Return the default unit for value, or None.
"""

# Determine the default units based on the user preferences set for
# default units when printing a UnitDbl.
if np.iterable(value) and not isinstance(value, str):
return UnitDblConverter.default_units(value[0], axis)
else:
if cbook.is_scalar_or_string(value):
return UnitDblConverter.defaults[value.type()]
else:
return UnitDblConverter.default_units(value[0], axis)
0