pyLife core

class pylife.PylifeSignal(pandas_obj)[source]

Base class for signal accessor classes.

Notes

Derived classes need to implement the method _validate(self, obj) that gets pandas_obj as obj parameter. This validate() method must raise an Exception (e.g. AttributeError or ValueError) in case obj is not a valid DataFrame for the kind of signal.

For these validation fail_if_key_missing() and get_missing_keys() might be helpful.

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.

See also

fail_if_key_missing() get_missing_keys() register_method()

fail_if_key_missing(keys_to_check, msg=None)[source]

Raise an exception if any key is missing in a self._obj object.

Parameters:
Raises:
  • AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

  • AttributeError – if any of the keys is not found in the self._obj’s keys.

Notes

If self._obj is a pandas.DataFrame, all keys of keys_to_check meed to be found in the self._obj.columns.

If self._obj is a pandas.Series, all keys of keys_to_check meed to be found in the self._obj.index.

See also

get_missing_keys(), stresssignal.StressTensorVoigt

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 pandas objects.

A signal class like

@pd.api.extensions.register_dataframe_accessor('foo_signal')
class FooSignal(PylifeSignal):
    pass

The following two blocks are equivalent:

pd.Series({'foo': 1.0, 'bar': 2.0}).foo_signal
FooSignal.from_parameters(foo=1.0, bar=1.0)
get_missing_keys(keys_to_check)[source]

Get a list of missing keys that are needed for a self._obj object.

Parameters:

keys_to_check (list) – A list of keys that need to be available in self._obj

Returns:

missing_keys – a list of missing keys

Return type:

list

Raises:

AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

Notes

If self._obj is a pandas.DataFrame, all keys of keys_to_check not found in the self._obj.columns are returned.

If self._obj is a pandas.Series, all keys of keys_to_check not found in the self._obj.index are returned.

keys()[source]

Get a list of missing keys that are needed for a signal object.

Returns:

keys – a pandas index of keys

Return type:

pd.Index

Raises:

AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

Notes

If self._obj is a pandas.DataFrame, the self._obj.columns are returned.

If self._obj is a pandas.Series, the self._obj.index are returned.

to_pandas()[source]

Expose the pandas object of the signal.

Returns:

pandas_object – The pandas object representing the signal

Return type:

pd.DataFrame or pd.Series

Notes

The default implementation just returns the object given when instantiating the signal class. Derived classes may return a modified object or augmented, if they store some extra information.

By default the object is not copied. So make a copy yourself, if you intent to modify it.

class pylife.Broadcaster(pandas_obj)[source]

The Broadcaster to align pyLife signals to operands.

Parameters:

pandas_obj (pandas.Series or pandas.DataFrame) – the object of the Broadcaster

In most cases the Broadcaster class is not used directly. The functionality is in most cases used by the derived class PylifeSignal.

The purpose of the Broadcaster is 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.

See method broadcast() documentation for details.

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:

parameters (scalar, numpy array or pandas object) – The parameter to broadcast to

Returns:

parameter, object

Return type:

index aligned numerical objects

Examples

The behavior of the Broadcaster is best illustrated by examples:

  • Broadcasting pandas.Series to a scalar results in a scalar and a pandas.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.)
    
    obj
    
    idx
    foo    1.0
    bar    2.0
    dtype: float64
    
  • Broadcasting pandas.DataFrame to a scalar results in a pandas.DataFrame and a pandas.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
    
    obj
    
    foo bar
    idx
    1 1.0 3.0
    2 2.0 4.0
  • Broadcasting pandas.DataFrame to a a pandas.Series results in a pandas.DataFrame and a pandas.Series, if and only if the index name of the object is None.

    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
    
    obj
    
    tau chi
    idx
    foo 1.0 2.0
    bar 1.0 2.0
class pylife.DataValidator[source]
fail_if_key_missing(signal, keys_to_check, msg=None)[source]

Raise an exception if any key is missing in a signal object.

Parameters:
Raises:
  • AttributeError – if signal is neither a pandas.DataFrame nor a pandas.Series

  • AttributeError – if any of the keys is not found in the signal’s keys.

Notes

If signal is a pandas.DataFrame, all keys of keys_to_check meed to be found in the signal.columns.

If signal is a pandas.Series, all keys of keys_to_check meed to be found in the signal.index.

See also

signal.get_missing_keys(), stresssignal.StressTensorVoigt

get_missing_keys(signal, keys_to_check)[source]

Get a list of missing keys that are needed for a signal object.

Parameters:
Returns:

missing_keys – a list of missing keys

Return type:

list

Raises:

AttributeError – if signal is neither a pandas.DataFrame nor a pandas.Series

Notes

If signal is a pandas.DataFrame, all keys of keys_to_check not found in the signal.columns are returned.

If signal is a pandas.Series, all keys of keys_to_check not found in the signal.index are returned.

keys(signal)[source]

Get a list of missing keys that are needed for a signal object.

Parameters:

signal (pandas.DataFrame or pandas.Series) – The object to be checked

Returns:

keys – a pandas index of keys

Return type:

pd.Index

Raises:

AttributeError – if signal is neither a pandas.DataFrame nor a pandas.Series

Notes

If signal is a pandas.DataFrame, the signal.columns are returned.

If signal is a pandas.Series, the signal.index are returned.