pyLife core

class pylife.core.signal.PylifeSignal(pandas_obj)[source]

Base class for signal accessor classes

Parameters:pandas_obj (pandas.DataFrame or pandas.Series) –

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()

pylife.core.signal.register_method(cls, method_name)[source]

Registers a method to a class derived from PyifeSignal

Parameters:
  • cls (class) – The class the method is registered to.
  • method_name (str) – The name of the method
Raises:
  • ValueError – if method_name is already registered for the class
  • ValueError – if method_name the class has already an attribute method_name

Notes

The function is meant to be used as a decorator for a function that is to be installed as a method for a class. The class is assumed to contain a pandas object in self._obj.

Examples

import pandas as pd
import pylife as pl

@pd.api.extensions.register_dataframe_accessor('foo')
class FooAccessor(pl.signal.PyifeSignal):
    def __init__(self, obj):
        # self._validate(obj) could come here
        self._obj = obj


@pl.signal.register_method(FooAccessor, 'bar')
def bar(df):
    return pd.DataFrame({'baz': df['foo'] + df['bar']})
>>> df = pd.DataFrame({'foo': [1.0, 2.0], 'bar': [-1.0, -2.0]})
>>> df.foo.bar()
   baz
0  0.0
1  0.0
class pylife.core.data_validator.DataValidator[source]
fail_if_key_missing(signal, keys_to_check, msg=None)[source]

Raises 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) – A list of keys that need to be available in signal
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.StressTensorVoigtAccessor

static fill_member(key, values)[source]
get_missing_keys(signal, keys_to_check)[source]

Gets a list of missing keys that are needed for a signal object

Parameters:
  • signal (pandas.DataFrame or pandas.Series) – The object to be checked
  • keys_to_check (list) – A list of keys that need to be available in signal
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.