biopsykit.saliva.utils module¶
Utility functions for working with saliva dataframes.
- biopsykit.saliva.utils.saliva_feature_wide_to_long(data, saliva_type)[source]¶
Convert
SalivaFeatureDataFrame
from wide-format into long-format.- Parameters
data (
SalivaFeatureDataFrame
) – dataframe containing saliva features in wide-format, i.e. one column per saliva sample, one row per subject.saliva_type (str) – saliva type (e.g. ‘cortisol’)
- Returns
dataframe with saliva features in long-format
- Return type
- biopsykit.saliva.utils.get_saliva_column_suggestions(data, saliva_type)[source]¶
Automatically extract possible saliva data columns from a pandas dataframe.
This is for example useful when one large dataframe is used to store demographic information, questionnaire data and saliva data.
- Parameters
- Returns
list of suggested columns containing saliva data or dict of such if
saliva_type
is a list- Return type
- biopsykit.saliva.utils.extract_saliva_columns(data, saliva_type, col_pattern=None)[source]¶
Extract saliva sample columns from a pandas dataframe.
- Parameters
data (
DataFrame
) – dataframe to extract columns fromsaliva_type (str or list of str) – saliva type variable or list of saliva types which should be used to extract columns (e.g. ‘cortisol’)
col_pattern (str, optional) – string pattern or list of string patterns to identify saliva columns. If
None
, it is attempted to automatically infer column names usingget_saliva_column_suggestions()
. Ifcol_pattern
is a list, it must be the same length likesaliva_type
.
- Returns
pandas dataframe with extracted columns or dict of such if
saliva_type
is a list- Return type
DataFrame
or dict
- biopsykit.saliva.utils.sample_times_datetime_to_minute(sample_times)[source]¶
Convert sample times from datetime or timedelta objects into minutes.
In order to compute certain saliva features (such as
auc()
orslope()
) the saliva sampling times are needed. This function can be used to convert sampling times into minutes relative to the first saliva sample.- Parameters
sample_times (
Series
orDataFrame
) – saliva sampling times in a Python datetime- or timedelta-related format. Ifsample_times
is a Series, it is assumed to be in long-format and will be unstacked into wide-format along the sample level. Ifsample_times
is a DataFrame, it is assumed to be in wide-format already. If values insample_times
arestr
, they are assumed to be strings with time information only (not including date), e.g., “09:00”, “09:15”, …- Returns
dataframe in wide-format with saliva sampling times in minutes relative to the first saliva sample
- Return type
- Raises
ValueError – if sample times are not in a datetime- or timedelta-related format