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

DataFrame

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
  • data (DataFrame) – dataframe which should be extracted

  • saliva_type (str or list of str) – saliva type variable which or list of saliva types should be used to extract columns (e.g. ‘cortisol’)

Returns

list of suggested columns containing saliva data or dict of such if saliva_type is a list

Return type

list or dict

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 from

  • saliva_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 using get_saliva_column_suggestions(). If col_pattern is a list, it must be the same length like saliva_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() or slope()) 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 or DataFrame) – saliva sampling times in a Python datetime- or timedelta-related format. If sample_times is a Series, it is assumed to be in long-format and will be unstacked into wide-format along the sample level. If sample_times is a DataFrame, it is assumed to be in wide-format already. If values in sample_times are str, 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

DataFrame

Raises

ValueError – if sample times are not in a datetime- or timedelta-related format