From c6dc7e675a023872654175d414d9de7bba051111 Mon Sep 17 00:00:00 2001 From: JulioV Date: Tue, 2 Feb 2021 17:34:56 -0500 Subject: [PATCH] Add stats features for empatica heartrate Turn off all empatica compute features --- Snakefile | 12 ++-- config.yaml | 14 ++-- src/features/empatica_heartrate/dbdp/main.py | 68 +++++++++++++++----- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/Snakefile b/Snakefile index b06cd351..402ed2e0 100644 --- a/Snakefile +++ b/Snakefile @@ -307,12 +307,12 @@ for provider in config["EMPATICA_HEARTRATE"]["PROVIDERS"].keys(): suffixes = get_zip_suffixes(pid) files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_unzipped_{suffix}.csv", pid=pid, suffix=suffixes)) files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_raw_{suffix}.csv", pid=pid, suffix=suffixes)) -# files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_joined.csv", pid=config["PIDS"])) -# files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_with_datetime.csv", pid=config["PIDS"])) -# files_to_compute.extend(expand("data/interim/{pid}/empatica_heartrate_features/empatica_heartrate_{language}_{provider_key}.csv", pid=config["PIDS"], language=config["EMPATICA_HEARTRATE"]["PROVIDERS"][provider]["SRC_LANGUAGE"].lower(), provider_key=provider.lower())) -# files_to_compute.extend(expand("data/processed/features/{pid}/empatica_heartrate.csv", pid=config["PIDS"])) -# files_to_compute.extend(expand("data/processed/features/{pid}/all_sensor_features.csv", pid=config["PIDS"])) -# files_to_compute.append("data/processed/features/all_participants/all_sensor_features.csv") + files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_joined.csv", pid=config["PIDS"])) + files_to_compute.extend(expand("data/raw/{pid}/empatica_heartrate_with_datetime.csv", pid=config["PIDS"])) + files_to_compute.extend(expand("data/interim/{pid}/empatica_heartrate_features/empatica_heartrate_{language}_{provider_key}.csv", pid=config["PIDS"], language=config["EMPATICA_HEARTRATE"]["PROVIDERS"][provider]["SRC_LANGUAGE"].lower(), provider_key=provider.lower())) + files_to_compute.extend(expand("data/processed/features/{pid}/empatica_heartrate.csv", pid=config["PIDS"])) + files_to_compute.extend(expand("data/processed/features/{pid}/all_sensor_features.csv", pid=config["PIDS"])) + files_to_compute.append("data/processed/features/all_participants/all_sensor_features.csv") for provider in config["EMPATICA_TEMPERATURE"]["PROVIDERS"].keys(): if config["EMPATICA_TEMPERATURE"]["PROVIDERS"][provider]["COMPUTE"]: diff --git a/config.yaml b/config.yaml index ca830600..18f6c6ef 100644 --- a/config.yaml +++ b/config.yaml @@ -7,7 +7,7 @@ TIMEZONE: &timezone America/New_York # See https://www.rapids.science/latest/setup/configuration/#participant-files -PIDS: [e01] +PIDS: [e03] # See https://www.rapids.science/latest/setup/configuration/#automatic-creation-of-participant-files CREATE_PARTICIPANT_FILES: @@ -436,8 +436,8 @@ EMPATICA_HEARTRATE: TABLE: hr PROVIDERS: DBDP: - COMPUTE: True - FEATURES: [] + COMPUTE: False + FEATURES: ["maxhr", "minhr", "avghr", "medianhr", "modehr", "stdhr", "diffmaxmodehr", "diffminmodehr", "entropyhr"] SRC_FOLDER: "dbdp" # inside src/features/empatica_heartrate SRC_LANGUAGE: "python" @@ -445,7 +445,7 @@ EMPATICA_TEMPERATURE: TABLE: temp PROVIDERS: DBDP: - COMPUTE: True + COMPUTE: False FEATURES: [] SRC_FOLDER: "dbdp" # inside src/features/empatica_heartrate SRC_LANGUAGE: "python" @@ -454,7 +454,7 @@ EMPATICA_ELECTRODERMAL_ACTIVITY: TABLE: eda PROVIDERS: DBDP: - COMPUTE: True + COMPUTE: False FEATURES: [] SRC_FOLDER: "dbdp" # inside src/features/empatica_heartrate SRC_LANGUAGE: "python" @@ -463,7 +463,7 @@ EMPATICA_BLOOD_VOLUME_PULSE: TABLE: bvp PROVIDERS: DBDP: - COMPUTE: True + COMPUTE: False FEATURES: [] SRC_FOLDER: "dbdp" # inside src/features/empatica_heartrate SRC_LANGUAGE: "python" @@ -472,7 +472,7 @@ EMPATICA_INTER_BEAT_INTERVAL: TABLE: ibi PROVIDERS: DBDP: - COMPUTE: True + COMPUTE: False FEATURES: [] SRC_FOLDER: "dbdp" # inside src/features/empatica_heartrate SRC_LANGUAGE: "python" diff --git a/src/features/empatica_heartrate/dbdp/main.py b/src/features/empatica_heartrate/dbdp/main.py index 82da2f3a..4f6cc2e2 100644 --- a/src/features/empatica_heartrate/dbdp/main.py +++ b/src/features/empatica_heartrate/dbdp/main.py @@ -1,21 +1,59 @@ import pandas as pd -import numpy as np +from scipy.stats import entropy + +def statsFeatures(heartrate_data, features, heartrate_features): + + col_name = "heartrate" + if "sumhr" in features: + heartrate_features["sumhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].sum() + if "maxhr" in features: + heartrate_features["maxhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].max() + if "minhr" in features: + heartrate_features["minhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].min() + if "avghr" in features: + heartrate_features["avghr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].mean() + if "medianhr" in features: + heartrate_features["medianhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].median() + if "modehr" in features: + heartrate_features["modehr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].agg(lambda x: pd.Series.mode(x)[0]) + if "stdhr" in features: + heartrate_features["stdhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].std() + if "diffmaxmodehr" in features: + heartrate_features["diffmaxmodehr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].max() - heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].agg(lambda x: pd.Series.mode(x)[0]) + if "diffminmodehr" in features: + heartrate_features["diffminmodehr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].agg(lambda x: pd.Series.mode(x)[0]) - heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].min() + if "entropyhr" in features: + heartrate_features["entropyhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[col_name].agg(entropy) + + return heartrate_features + +def extractHRFeaturesFromIntradayData(heartrate_intraday_data, features, time_segment, filter_data_by_segment): + heartrate_intraday_features = pd.DataFrame(columns=["local_segment"] + features) + if not heartrate_intraday_data.empty: + heartrate_intraday_data = filter_data_by_segment(heartrate_intraday_data, time_segment) + + if not heartrate_intraday_data.empty: + heartrate_intraday_features = pd.DataFrame() + + # get stats of heartrate + heartrate_intraday_features = statsFeatures(heartrate_intraday_data, features, heartrate_intraday_features) + + heartrate_intraday_features.reset_index(inplace=True) + + return heartrate_intraday_features + def dbdp_features(sensor_data_files, time_segment, provider, filter_data_by_segment, *args, **kwargs): - sensor_data = pd.read_csv(sensor_data_files["sensor_data"]) - requested_features = provider["FEATURES"] + heartrate_intraday_data = pd.read_csv(sensor_data_files["sensor_data"]) + + requested_intraday_features = provider["FEATURES"] # name of the features this function can compute - base_features_names = [] # ["maxmagnitude", "minmagnitude", "avgmagnitude", "medianmagnitude", "stdmagnitude"] + base_intraday_features_names = ["maxhr", "minhr", "avghr", "medianhr", "modehr", "stdhr", "diffmaxmodehr", "diffminmodehr", "entropyhr"] # the subset of requested features this function can compute - features_to_compute = list(set(requested_features) & set(base_features_names)) - - features = pd.DataFrame(columns=["local_segment"] + features_to_compute) - if not sensor_data.empty: - sensor_data = filter_data_by_segment(sensor_data, time_segment) - - if not sensor_data.empty: - features = pd.DataFrame() - - - return features \ No newline at end of file + intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names)) + + # extract features from intraday data + heartrate_intraday_features = extractHRFeaturesFromIntradayData(heartrate_intraday_data, intraday_features_to_compute, time_segment, filter_data_by_segment) + + return heartrate_intraday_features