2022-03-30 17:01:24 +02:00
|
|
|
import pandas as pd
|
2022-06-09 15:35:15 +02:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
2022-03-30 17:01:24 +02:00
|
|
|
|
2022-06-03 14:34:36 +02:00
|
|
|
from cr_features.helper_functions import convert_to2d, hrv_features
|
2022-04-19 15:24:46 +02:00
|
|
|
from cr_features.hrv import extract_hrv_features_2d_wrapper
|
2022-05-10 13:36:49 +02:00
|
|
|
from cr_features_helper_methods import extract_second_order_features
|
2022-03-30 17:01:24 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2022-06-09 15:35:15 +02:00
|
|
|
# pd.set_option('display.max_rows', 1000)
|
|
|
|
pd.set_option('display.max_columns', None)
|
2022-03-30 17:01:24 +02:00
|
|
|
|
2022-04-19 15:24:46 +02:00
|
|
|
def extract_bvp_features_from_intraday_data(bvp_intraday_data, features, window_length, time_segment, filter_data_by_segment):
|
2022-03-30 17:01:24 +02:00
|
|
|
bvp_intraday_features = pd.DataFrame(columns=["local_segment"] + features)
|
|
|
|
|
2022-04-14 13:51:53 +02:00
|
|
|
if not bvp_intraday_data.empty:
|
2022-05-10 13:36:49 +02:00
|
|
|
sample_rate = 64
|
2022-03-30 17:01:24 +02:00
|
|
|
|
|
|
|
bvp_intraday_data = filter_data_by_segment(bvp_intraday_data, time_segment)
|
|
|
|
|
|
|
|
if not bvp_intraday_data.empty:
|
|
|
|
|
|
|
|
bvp_intraday_features = pd.DataFrame()
|
|
|
|
|
2022-04-12 16:00:44 +02:00
|
|
|
# apply methods from calculate features module
|
|
|
|
if window_length is None:
|
|
|
|
bvp_intraday_features = \
|
|
|
|
bvp_intraday_data.groupby('local_segment').apply(\
|
2022-04-14 13:51:53 +02:00
|
|
|
lambda x:
|
2022-04-19 15:24:46 +02:00
|
|
|
extract_hrv_features_2d_wrapper(
|
|
|
|
convert_to2d(x['blood_volume_pulse'], x.shape[0]),
|
2022-05-13 15:35:34 +02:00
|
|
|
sampling=sample_rate, hampel_fiter=False, median_filter=False, mod_z_score_filter=True, feature_names=features))
|
2022-04-14 13:51:53 +02:00
|
|
|
|
2022-04-12 16:00:44 +02:00
|
|
|
else:
|
|
|
|
bvp_intraday_features = \
|
|
|
|
bvp_intraday_data.groupby('local_segment').apply(\
|
2022-04-14 13:51:53 +02:00
|
|
|
lambda x:
|
2022-04-19 15:24:46 +02:00
|
|
|
extract_hrv_features_2d_wrapper(
|
|
|
|
convert_to2d(x['blood_volume_pulse'], window_length*sample_rate),
|
|
|
|
sampling=sample_rate, hampel_fiter=False, median_filter=False, mod_z_score_filter=True, feature_names=features))
|
|
|
|
|
2022-03-30 17:01:24 +02:00
|
|
|
bvp_intraday_features.reset_index(inplace=True)
|
|
|
|
|
|
|
|
return bvp_intraday_features
|
|
|
|
|
|
|
|
|
2022-04-12 16:00:44 +02:00
|
|
|
def cr_features(sensor_data_files, time_segment, provider, filter_data_by_segment, *args, **kwargs):
|
2022-03-30 17:01:24 +02:00
|
|
|
bvp_intraday_data = pd.read_csv(sensor_data_files["sensor_data"])
|
|
|
|
|
|
|
|
requested_intraday_features = provider["FEATURES"]
|
2022-04-12 16:00:44 +02:00
|
|
|
|
2022-04-13 15:18:23 +02:00
|
|
|
calc_windows = kwargs.get('calc_windows', False)
|
|
|
|
|
|
|
|
if provider["WINDOWS"]["COMPUTE"] and calc_windows:
|
2022-04-12 16:00:44 +02:00
|
|
|
requested_window_length = provider["WINDOWS"]["WINDOW_LENGTH"]
|
|
|
|
else:
|
|
|
|
requested_window_length = None
|
|
|
|
|
2022-03-30 17:01:24 +02:00
|
|
|
# name of the features this function can compute
|
2022-06-03 14:34:36 +02:00
|
|
|
base_intraday_features_names = hrv_features
|
2022-03-30 17:01:24 +02:00
|
|
|
# the subset of requested features this function can compute
|
|
|
|
intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names))
|
|
|
|
|
|
|
|
# extract features from intraday data
|
2022-04-19 15:24:46 +02:00
|
|
|
bvp_intraday_features = extract_bvp_features_from_intraday_data(bvp_intraday_data, intraday_features_to_compute,
|
2022-04-12 16:00:44 +02:00
|
|
|
requested_window_length, time_segment, filter_data_by_segment)
|
2022-03-30 17:01:24 +02:00
|
|
|
|
2022-04-25 15:07:03 +02:00
|
|
|
if calc_windows:
|
2022-06-09 15:35:15 +02:00
|
|
|
if provider["WINDOWS"].get("STANDARDIZE_SO_FEATURES", False):
|
|
|
|
fo_columns = bvp_intraday_features.columns.values[2:]
|
|
|
|
fo_columns_z_score = [col + "_zscore" for col in fo_columns]
|
|
|
|
bvp_intraday_features[fo_columns_z_score] = StandardScaler().fit_transform(bvp_intraday_features[fo_columns])
|
|
|
|
|
2022-04-25 15:07:03 +02:00
|
|
|
so_features_names = provider["WINDOWS"]["SECOND_ORDER_FEATURES"]
|
|
|
|
bvp_second_order_features = extract_second_order_features(bvp_intraday_features, so_features_names)
|
2022-06-09 15:35:15 +02:00
|
|
|
|
2022-04-25 15:07:03 +02:00
|
|
|
return bvp_intraday_features, bvp_second_order_features
|
|
|
|
|
2022-03-30 17:01:24 +02:00
|
|
|
return bvp_intraday_features
|