2022-03-28 16:18:29 +02:00
|
|
|
import pandas as pd
|
|
|
|
from scipy.stats import entropy
|
|
|
|
|
2022-04-19 15:24:46 +02:00
|
|
|
from cr_features.helper_functions import convert_to2d, accelerometer_features, frequency_features
|
|
|
|
from cr_features.calculate_features import calculate_features
|
2022-04-25 15:07:03 +02:00
|
|
|
from cr_features_helper_methods import get_sample_rate, extract_second_order_features
|
2022-03-28 16:18:29 +02:00
|
|
|
|
2022-03-29 11:41:51 +02:00
|
|
|
import sys
|
2022-03-28 16:18:29 +02:00
|
|
|
|
2022-04-19 15:24:46 +02:00
|
|
|
def get_sample_rate(data):
|
2022-03-28 16:18:29 +02:00
|
|
|
try:
|
2022-03-30 17:00:11 +02:00
|
|
|
timestamps_diff = data['timestamp'].diff().dropna().mean()
|
2022-03-28 16:18:29 +02:00
|
|
|
except:
|
2022-03-30 17:00:11 +02:00
|
|
|
raise Exception("Error occured while trying to get the mean sample rate from the data.")
|
2022-03-28 16:18:29 +02:00
|
|
|
|
2022-04-19 15:24:46 +02:00
|
|
|
return int(1000/timestamps_diff)
|
2022-03-28 16:18:29 +02:00
|
|
|
|
2022-04-19 15:24:46 +02:00
|
|
|
def extract_acc_features_from_intraday_data(acc_intraday_data, features, window_length, time_segment, filter_data_by_segment):
|
2022-03-28 16:18:29 +02:00
|
|
|
acc_intraday_features = pd.DataFrame(columns=["local_segment"] + features)
|
|
|
|
|
|
|
|
if not acc_intraday_data.empty:
|
2022-04-19 15:24:46 +02:00
|
|
|
sample_rate = get_sample_rate(acc_intraday_data)
|
2022-03-28 16:18:29 +02:00
|
|
|
|
|
|
|
acc_intraday_data = filter_data_by_segment(acc_intraday_data, time_segment)
|
|
|
|
|
|
|
|
if not acc_intraday_data.empty:
|
|
|
|
|
|
|
|
acc_intraday_features = pd.DataFrame()
|
|
|
|
|
|
|
|
# apply methods from calculate features module
|
2022-04-12 16:00:44 +02:00
|
|
|
if window_length is None:
|
|
|
|
acc_intraday_features = \
|
2022-04-19 15:24:46 +02:00
|
|
|
acc_intraday_data.groupby('local_segment').apply(lambda x: calculate_features( \
|
|
|
|
convert_to2d(x['double_values_0'], x.shape[0]), \
|
|
|
|
convert_to2d(x['double_values_1'], x.shape[0]), \
|
|
|
|
convert_to2d(x['double_values_2'], x.shape[0]), \
|
|
|
|
fs=sample_rate, feature_names=features))
|
2022-04-12 16:00:44 +02:00
|
|
|
else:
|
|
|
|
acc_intraday_features = \
|
2022-04-19 15:24:46 +02:00
|
|
|
acc_intraday_data.groupby('local_segment').apply(lambda x: calculate_features( \
|
|
|
|
convert_to2d(x['double_values_0'], window_length*sample_rate), \
|
|
|
|
convert_to2d(x['double_values_1'], window_length*sample_rate), \
|
|
|
|
convert_to2d(x['double_values_2'], window_length*sample_rate), \
|
|
|
|
fs=sample_rate, feature_names=features))
|
2022-03-28 16:18:29 +02:00
|
|
|
|
|
|
|
acc_intraday_features.reset_index(inplace=True)
|
|
|
|
|
|
|
|
return acc_intraday_features
|
|
|
|
|
|
|
|
|
2022-03-29 11:41:51 +02:00
|
|
|
|
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:00:11 +02:00
|
|
|
acc_intraday_data = pd.read_csv(sensor_data_files["sensor_data"])
|
2022-03-28 16:18:29 +02:00
|
|
|
|
|
|
|
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-28 16:18:29 +02:00
|
|
|
# name of the features this function can compute
|
2022-04-19 15:24:46 +02:00
|
|
|
base_intraday_features_names = accelerometer_features + frequency_features
|
2022-03-28 16:18:29 +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
|
|
|
acc_intraday_features = extract_acc_features_from_intraday_data(acc_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-28 16:18:29 +02:00
|
|
|
|
2022-04-25 15:07:03 +02:00
|
|
|
if calc_windows:
|
|
|
|
so_features_names = provider["WINDOWS"]["SECOND_ORDER_FEATURES"]
|
|
|
|
acc_second_order_features = extract_second_order_features(acc_intraday_features, so_features_names)
|
|
|
|
return acc_intraday_features, acc_second_order_features
|
|
|
|
|
2022-03-30 17:00:11 +02:00
|
|
|
return acc_intraday_features
|