2020-12-15 02:30:34 +01:00
|
|
|
import pandas as pd
|
2021-02-02 23:34:56 +01:00
|
|
|
from scipy.stats import entropy
|
2020-12-15 02:30:34 +01:00
|
|
|
|
|
|
|
|
2021-02-12 02:56:27 +01:00
|
|
|
def statsFeatures(heartrate_data, features, heartrate_features):
|
2021-02-02 23:34:56 +01:00
|
|
|
col_name = "heartrate"
|
|
|
|
if "sumhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["sumhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].sum()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "maxhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["maxhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].max()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "minhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["minhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].min()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "avghr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["avghr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].mean()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "medianhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["medianhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].median()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "modehr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["modehr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].agg(lambda x: pd.Series.mode(x)[0])
|
2021-02-02 23:34:56 +01:00
|
|
|
if "stdhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["stdhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].std()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "diffmaxmodehr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
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])
|
2021-02-02 23:34:56 +01:00
|
|
|
if "diffminmodehr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
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()
|
2021-02-02 23:34:56 +01:00
|
|
|
if "entropyhr" in features:
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_features["entropyhr"] = heartrate_data[["local_segment", col_name]].groupby(["local_segment"])[
|
|
|
|
col_name].agg(entropy)
|
2021-02-02 23:34:56 +01:00
|
|
|
|
|
|
|
return heartrate_features
|
2020-12-15 02:30:34 +01:00
|
|
|
|
2021-02-12 02:56:27 +01:00
|
|
|
|
2021-02-02 23:34:56 +01:00
|
|
|
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()
|
2021-02-12 02:56:27 +01:00
|
|
|
|
2021-02-02 23:34:56 +01:00
|
|
|
# get stats of heartrate
|
|
|
|
heartrate_intraday_features = statsFeatures(heartrate_intraday_data, features, heartrate_intraday_features)
|
|
|
|
|
2021-02-16 01:00:04 +01:00
|
|
|
heartrate_intraday_features.reset_index(inplace=True)
|
2021-02-02 23:34:56 +01:00
|
|
|
|
|
|
|
return heartrate_intraday_features
|
2020-12-15 02:30:34 +01:00
|
|
|
|
2021-02-02 23:34:56 +01:00
|
|
|
|
|
|
|
def dbdp_features(sensor_data_files, time_segment, provider, filter_data_by_segment, *args, **kwargs):
|
|
|
|
heartrate_intraday_data = pd.read_csv(sensor_data_files["sensor_data"])
|
|
|
|
|
|
|
|
requested_intraday_features = provider["FEATURES"]
|
|
|
|
# name of the features this function can compute
|
2021-02-12 02:56:27 +01:00
|
|
|
base_intraday_features_names = ["maxhr", "minhr", "avghr", "medianhr", "modehr", "stdhr", "diffmaxmodehr",
|
|
|
|
"diffminmodehr", "entropyhr"]
|
2021-02-02 23:34:56 +01:00
|
|
|
# the subset of requested features this function can compute
|
|
|
|
intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names))
|
2021-02-12 02:56:27 +01:00
|
|
|
|
2021-02-02 23:34:56 +01:00
|
|
|
# extract features from intraday data
|
2021-02-12 02:56:27 +01:00
|
|
|
heartrate_intraday_features = extractHRFeaturesFromIntradayData(heartrate_intraday_data,
|
|
|
|
intraday_features_to_compute, time_segment,
|
|
|
|
filter_data_by_segment)
|
|
|
|
|
2021-02-12 01:16:35 +01:00
|
|
|
return heartrate_intraday_features
|