Add Episodes GAR Metrics

replace/e77805d3569b494eec48444a5b93edb1ebd6654d
Echhit Joshi 2019-11-27 18:06:31 -05:00
parent e5390ee0e2
commit 80376f0c35
2 changed files with 25 additions and 5 deletions

View File

@ -42,7 +42,7 @@ rule google_activity_recognition_deltas:
input: input:
"data/raw/{pid}/plugin_google_activity_recognition_with_datetime.csv" "data/raw/{pid}/plugin_google_activity_recognition_with_datetime.csv"
output: output:
"data/processed/{pid}/google_activity_recognition_deltas.csv" "data/processed/{pid}/plugin_google_activity_recognition_deltas.csv"
script: script:
"../src/features/google_activity_recognition_deltas.R" "../src/features/google_activity_recognition_deltas.R"
@ -70,8 +70,8 @@ rule bluetooth_metrics:
rule activity_metrics: rule activity_metrics:
input: input:
"data/raw/{pid}/plugin_google_activity_recognition_with_datetime.csv", gar_events = "data/raw/{pid}/plugin_google_activity_recognition_with_datetime.csv",
"data/raw/{pid}/plugin_google_activity_recognition_deltas.csv" gar_deltas = "data/processed/{pid}/plugin_google_activity_recognition_deltas.csv"
params: params:
segment = "{day_segment}", segment = "{day_segment}",
metrics = config["GOOGLE_ACTIVITY_RECOGNITION"]["METRICS"] metrics = config["GOOGLE_ACTIVITY_RECOGNITION"]["METRICS"]

View File

@ -1,17 +1,26 @@
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import scipy.stats as stats import scipy.stats as stats
from features_utils import splitOvernightEpisodes, splitMultiSegmentEpisodes
day_segment = snakemake.params["segment"] day_segment = snakemake.params["segment"]
#Read csv into a pandas dataframe #Read csv into a pandas dataframe
data = pd.read_csv(snakemake.input[0]) data = pd.read_csv(snakemake.input['gar_events'],parse_dates=['local_date_time'])
columns = ['count','most_common_activity','count_unique_activities','activity_change_count'] ar_deltas = pd.read_csv(snakemake.input['gar_deltas'],parse_dates=["local_start_date_time", "local_end_date_time", "local_start_date", "local_end_date"])
columns = ['count','most_common_activity','count_unique_activities','activity_change_count','sumstationary','summobile','sumvehicle']
columns = list("ar_" + str(day_segment) + "_" + column for column in columns) columns = list("ar_" + str(day_segment) + "_" + column for column in columns)
if data.empty: if data.empty:
finalDataset = pd.DataFrame(columns = columns) finalDataset = pd.DataFrame(columns = columns)
else: else:
ar_deltas = splitOvernightEpisodes(ar_deltas, [],['activity'])
if day_segment != "daily":
ar_deltas = splitMultiSegmentEpisodes(ar_deltas, day_segment, [])
data.local_date_time = pd.to_datetime(data.local_date_time) data.local_date_time = pd.to_datetime(data.local_date_time)
resampledData = data.set_index(data.local_date_time) resampledData = data.set_index(data.local_date_time)
resampledData.drop(columns=['local_date_time'],inplace=True) resampledData.drop(columns=['local_date_time'],inplace=True)
@ -33,6 +42,17 @@ else:
countChanges = resampledData['different_activity'].resample('D').sum() countChanges = resampledData['different_activity'].resample('D').sum()
finalDataset = pd.concat([count, mostCommonActivity, uniqueActivities, countChanges],axis=1) finalDataset = pd.concat([count, mostCommonActivity, uniqueActivities, countChanges],axis=1)
deltas_metrics = {'sumstationary':['still','tilting'],
'summobile':['on_foot','running','on_bicycle'],
'sumvehicle':['on_vehicle']}
for column, activity_labels in deltas_metrics.items():
metric = (ar_deltas[ar_deltas['activity'].isin(pd.Series(activity_labels))]
.groupby(['local_start_date'])['time_diff']
.agg({"ar_" + str(day_segment) + "_" + str(column) :'sum'}))
finalDataset = finalDataset.merge(metric,how='outer',left_index=True,right_index=True)
finalDataset.fillna(0,inplace=True)
finalDataset.index.names = ['local_date'] finalDataset.index.names = ['local_date']
finalDataset.columns=columns finalDataset.columns=columns
finalDataset.to_csv(snakemake.output[0]) finalDataset.to_csv(snakemake.output[0])