pyLife core#
- class pylife.PylifeSignal(pandas_obj)[source]#
Base class for signal accessor classes.
A signal accessor wraps a
pandas.Seriesorpandas.DataFrameand gives it domain specific methods while leaving the data itself untouched. Derived classes register themselves withpandas.api.extensions.register_dataframe_accessor()orpandas.api.extensions.register_series_accessor(), which makes them available as an attribute of any pandas object, e.g.df.woehler.basquin_cycles(...).- Parameters:
pandas_obj (pandas.DataFrame or pandas.Series) – The object the accessor is instantiated with.
- Raises:
AttributeError – If
pandas_objdoes not fulfill the signal contract of the derived class.
See also
fail_if_key_missingAssert mandatory keys inside
_validate().get_missing_keysQuery mandatory keys inside
_validate().register_methodAdd a method to a signal class from outside.
Notes
Derived classes need to implement the method
_validate(self)which checksself._objand raises an exception (e.g.AttributeErrororValueError) if it is not a valid object for the kind of signal. The methodsfail_if_key_missing()andget_missing_keys()are meant to be used for that.For a derived class you can register methods without modifying the class’ code itself. This can be useful if you want to make signal accessor classes extendable.
- fail_if_key_missing(keys_to_check, msg=None)[source]#
Raise an exception if any key is missing in the signal.
- Parameters:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series, or if any ofkeys_to_checkis not provided by the signal.
See also
get_missing_keysReturn the missing keys instead of raising.
Notes
This is the canonical way to assert a signal contract in the
_validate()implementation of a derived class.
- classmethod from_parameters(**kwargs)[source]#
Make a signal instance from a parameter set.
This is a convenience function to instantiate a signal from individual parameters rather than from a pandas object.
- Parameters:
**kwargs (scalar or array_like) – The signal keys and their values. If more than one value is given and the values are iterable, a
pandas.DataFramebacked signal is created, otherwise apandas.Seriesbacked one.- Returns:
An instance of the signal class the method is called on.
- Return type:
Examples
For a signal class like
@pd.api.extensions.register_dataframe_accessor('foo_signal') class FooSignal(PylifeSignal): pass
the following two expressions are equivalent
pd.Series({'foo': 1.0, 'bar': 2.0}).foo_signal FooSignal.from_parameters(foo=1.0, bar=2.0)
- get_missing_keys(keys_to_check)[source]#
Get a list of keys that are missing in the signal.
- Parameters:
keys_to_check (list of str) – The keys that need to be available in the signal.
- Returns:
The keys of
keys_to_checkthat the signal does not provide, in the order in which they were given. An empty list means that the signal is complete.- Return type:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series.
See also
fail_if_key_missingRaise instead of returning the missing keys.
Notes
This method is meant to be used in the
_validate()implementation of a derived class when a missing key is not necessarily an error, e.g. when a default value can be derived.
- keys()[source]#
Get the keys the signal provides.
- Returns:
The keys of the underlying pandas object.
- Return type:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series.
Notes
For a
pandas.DataFramebacked signal the columns are returned, for apandas.Seriesbacked one the index.
- to_pandas()[source]#
Expose the pandas object of the signal.
- Returns:
The pandas object representing the signal.
- Return type:
Notes
The default implementation just returns the object given when instantiating the signal class. Derived classes may return a modified or augmented object, if they store some extra information.
By default the object is not copied. So make a copy yourself, if you intend to modify it.
- class pylife.Broadcaster(pandas_obj)[source]#
Align a pandas object with an operand of a different index.
The purpose of the
Broadcasteris to take two numerical objects and return two objects of the same numerical data with an aligned index. That means that mathematical operations using the two objects as operands can be implemented using numpy’s broadcasting functionality.In most cases the
Broadcasterclass is not used directly. Its functionality is inherited byPylifeSignal, from which all pyLife signal accessors are derived.- Parameters:
pandas_obj (pandas.Series or pandas.DataFrame) – The object to broadcast operands to.
See also
broadcastAlign an operand with the object of
self.pylife.PylifeSignalThe signal base class using the
Broadcaster.
Notes
The broadcasting is done in the following ways:
object parameter returned object returned parameter Series Scalar Series Scalar |------|-----| |------|-----| | idx | | | idx | | |------|-----| 5.0 -> |------|-----| 5.0 | foo | 1.0 | | foo | 1.0 | | bar | 2.0 | | bar | 2.0 | |------|-----| |------|-----| DataFrame Scalar DataFrame Series |------|-----|-----| |------|-----|-----| |------|-----| | idx | foo | bar | | idx | foo | bar | | idx | | |------|-----|-----| |------|-----|-----| |------|-----| | 0 | 1.0 | 2.0 | 5.0 -> | 0 | 1.0 | 2.0 | | 0 | 5.0 | | 1 | 1.0 | 2.0 | | 1 | 1.0 | 2.0 | | 1 | 5.0 | | ... | ... | ... | | ... | ... | ... | | ... | ... | |------|-----|-----| |------|-----|-----| |------|-----| Series Series/DataFrame DataFrame Series/DataFrame |------|-----| |------|-----| |------|-----|-----| |------|-----| | None | | | idx | | | idx | foo | bar | | idx | | |------|-----| |------|-----| -> |------|-----|-----| |------|-----| | foo | 1.0 | | 0 | 5.0 | | 0 | 1.0 | 2.0 | | 0 | 5.0 | | bar | 2.0 | | 1 | 6.0 | | 1 | 1.0 | 2.0 | | 1 | 6.0 | |------|-----| | ... | ... | | ... | ... | ... | | ... | ... | |------|-----| |------|-----|-----| |------|-----| Series/DataFrame Series/DataFrame Series/DataFrame Series/DataFrame |------|-----| |------|-----| |------|-----| |------|-----| | xidx | | | xidx | | | xidx | | | xidx | | |------|-----| |------|-----| -> |------|-----| |------|-----| | foo | 1.0 | | tau | 5.0 | | foo | 1.0 | | foo | nan | | bar | 2.0 | | bar | 6.0 | | bar | 2.0 | | bar | 6.0 | |------|-----| |------|-----| | tau | nan | | tau | 5.0 | |------|-----| |------|-----| Series/DataFrame Series/DataFrame Series/DataFrame Series/DataFrame |------|-----| |------|-----| |------|------|-----| |------|------|-----| | xidx | | | yidx | | | xidx | yidx | | | xidx | yidx | | |------|-----| |------|-----| -> |------|------|-----| |------|------|-----| | foo | 1.0 | | tau | 5.0 | | foo | tau | 1.0 | | foo | tau | 5.0 | | bar | 2.0 | | chi | 6.0 | | | chi | 1.0 | | | chi | 6.0 | |------|-----| |------|-----| | bar | tau | 2.0 | | bar | tau | 5.0 | | | chi | 2.0 | | | chi | 6.0 | |------|------|-----| |------|------|-----|
- broadcast(parameter, droplevel=None)[source]#
Broadcast the parameter to the object of
self.- Parameters:
parameter (scalar, numpy.ndarray, pandas.Series or pandas.DataFrame) – The operand to align with the object of
self.droplevel (list of str, optional) – Names of index levels that are dropped from the returned
parameterbefore it is returned. Use it when a parameter must not be resolved along those levels, e.g. when a material parameter is constant over all load steps. Default isNone, which keeps all levels.
- Returns:
parameter (scalar, pandas.Series or pandas.DataFrame) – The broadcast operand, index aligned with
obj.obj (pandas.Series or pandas.DataFrame) – The broadcast object of
self, index aligned withparameter.
Examples
The behavior of the Broadcaster is best illustrated by examples:
Broadcasting
pandas.Seriesto a scalar results in a scalar and apandas.Series.obj = pd.Series([1.0, 2.0], index=pd.Index(['foo', 'bar'], name='idx')) obj
idx foo 1.0 bar 2.0 dtype: float64
parameter, obj = Broadcaster(obj).broadcast(5.0) parameter
array(5.)
objidx foo 1.0 bar 2.0 dtype: float64
Broadcasting
pandas.DataFrameto a scalar results in apandas.DataFrameand apandas.Series.obj = pd.DataFrame({ 'foo': [1.0, 2.0], 'bar': [3.0, 4.0] }, index=pd.Index([1, 2], name='idx')) obj
foo bar idx 1 1.0 3.0 2 2.0 4.0 parameter, obj = Broadcaster(obj).broadcast(5.0) parameter
idx 1 5.0 2 5.0 dtype: float64
objfoo bar idx 1 1.0 3.0 2 2.0 4.0 Broadcasting
pandas.DataFrameto a apandas.Seriesresults in apandas.DataFrameand apandas.Series, if and only if the index name of the object isNone.obj = pd.Series([1.0, 2.0], index=pd.Index(['tau', 'chi'])) obj
tau 1.0 chi 2.0 dtype: float64
parameter = pd.Series([3.0, 4.0], index=pd.Index(['foo', 'bar'], name='idx')) parameter
idx foo 3.0 bar 4.0 dtype: float64
parameter, obj = Broadcaster(obj).broadcast(parameter) parameter
idx foo 3.0 bar 4.0 dtype: float64
objtau chi idx foo 1.0 2.0 bar 1.0 2.0
- class pylife.DataValidator[source]#
Check pandas objects for the keys a pyLife signal requires.
The validator works on both
pandas.Seriesandpandas.DataFrameobjects and hides the difference between the two: for aDataFramethe keys are the columns, for aSeriesthey are the index entries.Usually you do not instantiate a
DataValidatoryourself. Signal accessors derived fromPylifeSignalexpose the same checks asfail_if_key_missing()andget_missing_keys().See also
pylife.PylifeSignalSignal base class using these checks in
_validate().
Examples
>>> import pandas as pd >>> from pylife import DataValidator >>> DataValidator().get_missing_keys(pd.Series({'sigma_a': 100.0}), ... ['sigma_a', 'sigma_m']) ['sigma_m']
- fail_if_key_missing(signal, keys_to_check, msg=None)[source]#
Raise an exception if any key is missing in a signal object.
- Parameters:
signal (pandas.DataFrame or pandas.Series) – The object to be checked.
keys_to_check (list of str) – The keys that need to be available in
signal.msg (str, optional) – A printf style format string for the error message taking two
%sarguments: the expected keys and the missing keys. By default a message naming the calling signal class is generated.
- Raises:
AttributeError – If
signalis neither apandas.DataFramenor apandas.Series, or if any ofkeys_to_checkis not found in the keys ofsignal.
See also
get_missing_keysReturn the missing keys instead of raising.
Notes
If
signalis apandas.DataFrame, all keys ofkeys_to_checkneed to be found insignal.columns. Ifsignalis apandas.Series, they need to be found insignal.index.
- get_missing_keys(signal, keys_to_check)[source]#
Get a list of keys that are missing in a signal object.
- Parameters:
signal (pandas.DataFrame or pandas.Series) – The object to be checked.
keys_to_check (list of str) – The keys that need to be available in
signal.
- Returns:
The keys of
keys_to_checkthat are not present insignal, in the order in which they were given. An empty list means thatsignalis complete.- Return type:
- Raises:
AttributeError – If
signalis neither apandas.DataFramenor apandas.Series.
See also
fail_if_key_missingRaise instead of returning the missing keys.
Notes
If
signalis apandas.DataFrame, the keys are looked up insignal.columns, if it is apandas.Series, insignal.index.
- keys(signal)[source]#
Get the keys a signal object provides.
- Parameters:
signal (pandas.DataFrame or pandas.Series) – The object to be checked.
- Returns:
The keys of
signal.- Return type:
- Raises:
AttributeError – If
signalis neither apandas.DataFramenor apandas.Series.
Notes
If
signalis apandas.DataFrame,signal.columnsis returned. Ifsignalis apandas.Series,signal.indexis returned.Examples
>>> import pandas as pd >>> from pylife import DataValidator >>> list(DataValidator().keys(pd.Series({'sigma_a': 100.0}))) ['sigma_a']