Python matplotlib.units.ConversionInterface() Examples
The following are 27
code examples of matplotlib.units.ConversionInterface().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
matplotlib.units
, or try the search function
.
Example #1
Source File: test_units.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def quantity_converter(): # Create an instance of the conversion interface and # mock so we can check methods called qc = munits.ConversionInterface() def convert(value, unit, axis): if hasattr(value, 'units'): return value.to(unit).magnitude elif iterable(value): try: return [v.to(unit).magnitude for v in value] except AttributeError: return [Quantity(v, axis.get_units()).to(unit).magnitude for v in value] else: return Quantity(value, axis.get_units()).to(unit).magnitude def default_units(value, axis): if hasattr(value, 'units'): return value.units elif np.iterable(value): for v in value: if hasattr(v, 'units'): return v.units return None qc.convert = MagicMock(side_effect=convert) qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u)) qc.default_units = MagicMock(side_effect=default_units) return qc # Tests that the conversion machinery works properly for classes that # work as a facade over numpy arrays (like pint)
Example #2
Source File: EpochConverter.py From twitter-stock-recommendation with MIT License | 5 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if ( iterable(value) and not isinstance(value, six.string_types) ): 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 ) ): return value if ( unit == None ): unit = EpochConverter.default_units( value, axis ) if ( isDuration ): return EpochConverter.duration2float( value ) else: return EpochConverter.epoch2float( value, unit ) #------------------------------------------------------------------------
Example #3
Source File: category.py From twitter-stock-recommendation with MIT License | 5 votes |
def convert(value, unit, axis): """Converts strings in value to floats using mapping information store in the unit object Parameters ---------- value : string or iterable value or list of values to be converted unit : :class:`.UnitData` object string unit information for value axis : :class:`~matplotlib.Axis.axis` axis on which the converted value is plotted Returns ------- mapped_ value : float or ndarray[float] .. note:: axis is not used in this function """ # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, VALID_TYPES)) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #4
Source File: EpochConverter.py From CogAlg with MIT License | 5 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # 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)): return value if unit is None: unit = EpochConverter.default_units(value, axis) if isDuration: return EpochConverter.duration2float(value) else: return EpochConverter.epoch2float(value, unit)
Example #5
Source File: category.py From CogAlg with MIT License | 5 votes |
def convert(value, unit, axis): """Convert strings in value to floats using mapping information store in the unit object. Parameters ---------- value : string or iterable Value or list of values to be converted. unit : `.UnitData` An object mapping strings to integers. axis : `~matplotlib.axis.Axis` axis on which the converted value is plotted. .. note:: *axis* is unused. Returns ------- mapped_value : float or ndarray[float] """ if unit is None: raise ValueError( 'Missing category information for StrCategoryConverter; ' 'this might be caused by unintendedly mixing categorical and ' 'numeric data') # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, (str, bytes))) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #6
Source File: EpochConverter.py From coffeegrindsize with MIT License | 5 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if 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)): return value if unit is None: unit = EpochConverter.default_units(value, axis) if isDuration: return EpochConverter.duration2float(value) else: return EpochConverter.epoch2float(value, unit) # -----------------------------------------------------------------------
Example #7
Source File: test_units.py From coffeegrindsize with MIT License | 5 votes |
def quantity_converter(): # Create an instance of the conversion interface and # mock so we can check methods called qc = munits.ConversionInterface() def convert(value, unit, axis): if hasattr(value, 'units'): return value.to(unit).magnitude elif iterable(value): try: return [v.to(unit).magnitude for v in value] except AttributeError: return [Quantity(v, axis.get_units()).to(unit).magnitude for v in value] else: return Quantity(value, axis.get_units()).to(unit).magnitude def default_units(value, axis): if hasattr(value, 'units'): return value.units elif np.iterable(value): for v in value: if hasattr(v, 'units'): return v.units return None qc.convert = MagicMock(side_effect=convert) qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u)) qc.default_units = MagicMock(side_effect=default_units) return qc # Tests that the conversion machinery works properly for classes that # work as a facade over numpy arrays (like pint)
Example #8
Source File: category.py From coffeegrindsize with MIT License | 5 votes |
def convert(value, unit, axis): """Converts strings in value to floats using mapping information store in the unit object. Parameters ---------- value : string or iterable value or list of values to be converted unit : :class:`.UnitData` object string unit information for value axis : :class:`~matplotlib.Axis.axis` axis on which the converted value is plotted Returns ------- mapped_ value : float or ndarray[float] .. note:: axis is not used in this function """ # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, (str, bytes))) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #9
Source File: EpochConverter.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if 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)): return value if unit is None: unit = EpochConverter.default_units(value, axis) if isDuration: return EpochConverter.duration2float(value) else: return EpochConverter.epoch2float(value, unit) # -----------------------------------------------------------------------
Example #10
Source File: dates.py From Computable with MIT License | 5 votes |
def convert(value, unit, axis): """ If *value* is not already a number or sequence of numbers, convert it with :func:`date2num`. The *unit* and *axis* arguments are not used. """ if units.ConversionInterface.is_numlike(value): return value return date2num(value)
Example #11
Source File: category.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert(value, unit, axis): """Converts strings in value to floats using mapping information store in the unit object. Parameters ---------- value : string or iterable value or list of values to be converted unit : :class:`.UnitData` object string unit information for value axis : :class:`~matplotlib.Axis.axis` axis on which the converted value is plotted Returns ------- mapped_ value : float or ndarray[float] .. note:: axis is not used in this function """ # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, (str, bytes))) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #12
Source File: basic_units.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert(val, unit, axis): if units.ConversionInterface.is_numlike(val): return val if iterable(val): return [thisval.convert_to(unit).get_value() for thisval in val] else: return val.convert_to(unit).get_value()
Example #13
Source File: EpochConverter.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if 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)): return value if unit is None: unit = EpochConverter.default_units(value, axis) if isDuration: return EpochConverter.duration2float(value) else: return EpochConverter.epoch2float(value, unit) # -----------------------------------------------------------------------
Example #14
Source File: category.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def convert(value, unit, axis): """Converts strings in value to floats using mapping information store in the unit object. Parameters ---------- value : string or iterable value or list of values to be converted unit : :class:`.UnitData` object string unit information for value axis : :class:`~matplotlib.Axis.axis` axis on which the converted value is plotted Returns ------- mapped_ value : float or ndarray[float] .. note:: axis is not used in this function """ # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, (str, bytes))) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #15
Source File: EpochConverter.py From neural-network-animation with MIT License | 5 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if ( iterable(value) and not isinstance(value, six.string_types) ): 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 ) ): return value if ( unit == None ): unit = EpochConverter.default_units( value, axis ) if ( isDuration ): return EpochConverter.duration2float( value ) else: return EpochConverter.epoch2float( value, unit ) #------------------------------------------------------------------------
Example #16
Source File: dates.py From neural-network-animation with MIT License | 5 votes |
def convert(value, unit, axis): """ If *value* is not already a number or sequence of numbers, convert it with :func:`date2num`. The *unit* and *axis* arguments are not used. """ if units.ConversionInterface.is_numlike(value): return value return date2num(value)
Example #17
Source File: EpochConverter.py From matplotlib-4-abaqus with MIT License | 5 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for an axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotEpoch = True isDuration = False if ( 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 ) ): return value if ( unit == None ): unit = EpochConverter.default_units( value, axis ) if ( isDuration ): return EpochConverter.duration2float( value ) else: return EpochConverter.epoch2float( value, unit ) #------------------------------------------------------------------------
Example #18
Source File: dates.py From matplotlib-4-abaqus with MIT License | 5 votes |
def convert(value, unit, axis): """ If *value* is not already a number or sequence of numbers, convert it with :func:`date2num`. The *unit* and *axis* arguments are not used. """ if units.ConversionInterface.is_numlike(value): return value return date2num(value)
Example #19
Source File: category.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def convert(value, unit, axis): """Convert strings in value to floats using mapping information store in the unit object. Parameters ---------- value : string or iterable Value or list of values to be converted. unit : `.UnitData` An object mapping strings to integers. axis : `~matplotlib.axis.Axis` axis on which the converted value is plotted. .. note:: *axis* is unused. Returns ------- mapped_value : float or ndarray[float] """ if unit is None: raise ValueError( 'Missing category information for StrCategoryConverter; ' 'this might be caused by unintendedly mixing categorical and ' 'numeric data') # dtype = object preserves numerical pass throughs values = np.atleast_1d(np.array(value, dtype=object)) # pass through sequence of non binary numbers if all((units.ConversionInterface.is_numlike(v) and not isinstance(v, (str, bytes))) for v in values): return np.asarray(values, dtype=float) # force an update so it also does type checking unit.update(values) str2idx = np.vectorize(unit._mapping.__getitem__, otypes=[float]) mapped_value = str2idx(values) return mapped_value
Example #20
Source File: UnitDblConverter.py From GraphicDesignPatternByPython with MIT License | 4 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if 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 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)): 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) # -----------------------------------------------------------------------
Example #21
Source File: UnitDblConverter.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 4 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if 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 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)): 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) # -----------------------------------------------------------------------
Example #22
Source File: UnitDblConverter.py From neural-network-animation with MIT License | 4 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if ( iterable(value) and not isinstance(value, six.string_types) ): 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 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 ) ): return value # If no units were specified, then get the default units to use. if ( unit == 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 ) #------------------------------------------------------------------------
Example #23
Source File: UnitDblConverter.py From coffeegrindsize with MIT License | 4 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if 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 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)): 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) # -----------------------------------------------------------------------
Example #24
Source File: UnitDblConverter.py From CogAlg with MIT License | 4 votes |
def convert(value, unit, axis): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # 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 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): 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)
Example #25
Source File: UnitDblConverter.py From matplotlib-4-abaqus with MIT License | 4 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if ( 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 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 ) ): return value # If no units were specified, then get the default units to use. if ( unit == 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 ) #------------------------------------------------------------------------
Example #26
Source File: test_units.py From twitter-stock-recommendation with MIT License | 4 votes |
def test_numpy_facade(): # Create an instance of the conversion interface and # mock so we can check methods called qc = munits.ConversionInterface() def convert(value, unit, axis): if hasattr(value, 'units'): return value.to(unit).magnitude elif iterable(value): try: return [v.to(unit).magnitude for v in value] except AttributeError: return [Quantity(v, axis.get_units()).to(unit).magnitude for v in value] else: return Quantity(value, axis.get_units()).to(unit).magnitude qc.convert = MagicMock(side_effect=convert) qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u)) qc.default_units = MagicMock(side_effect=lambda x, a: x.units) # Register the class munits.registry[Quantity] = qc # Simple test y = Quantity(np.linspace(0, 30), 'miles') x = Quantity(np.linspace(0, 5), 'hours') fig, ax = plt.subplots() fig.subplots_adjust(left=0.15) # Make space for label ax.plot(x, y, 'tab:blue') ax.axhline(Quantity(26400, 'feet'), color='tab:red') ax.axvline(Quantity(120, 'minutes'), color='tab:green') ax.yaxis.set_units('inches') ax.xaxis.set_units('seconds') assert qc.convert.called assert qc.axisinfo.called assert qc.default_units.called # Tests gh-8908
Example #27
Source File: UnitDblConverter.py From twitter-stock-recommendation with MIT License | 4 votes |
def convert( value, unit, axis ): """: Convert value using unit to a float. If value is a sequence, return the converted sequence. = INPUT VARIABLES - value The value or list of values that need to be converted. - unit The units to use for a axis with Epoch data. = RETURN VALUE - Returns the value parameter converted to floats. """ # Delay-load due to circular dependencies. import matplotlib.testing.jpl_units as U isNotUnitDbl = True if ( iterable(value) and not isinstance(value, six.string_types) ): 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 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 ) ): return value # If no units were specified, then get the default units to use. if ( unit == 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 ) #------------------------------------------------------------------------