rapids/src/features/google_activity_recognition.py

38 lines
1.7 KiB
Python
Raw Normal View History

import pandas as pd
import numpy as np
import scipy.stats as stats
2019-11-18 20:22:08 +01:00
day_segment = snakemake.params["segment"]
#Read csv into a pandas dataframe
data = pd.read_csv(snakemake.input[0])
2019-11-18 20:22:08 +01:00
columns = ['count','most_common_activity','count_unique_activities','activity_change_count']
columns = list("ar_" + str(day_segment) + "_" + column for column in columns)
2019-11-12 20:48:19 +01:00
if data.empty:
2019-11-18 20:22:08 +01:00
finalDataset = pd.DataFrame(columns = columns)
2019-11-12 20:48:19 +01:00
else:
data.local_date_time = pd.to_datetime(data.local_date_time)
resampledData = data.set_index(data.local_date_time)
resampledData.drop(columns=['local_date_time'],inplace=True)
2019-11-18 20:22:08 +01:00
if(day_segment!='daily'):
resampledData = resampledData.loc[resampledData['local_day_segment'] == str(day_segment)]
2019-11-12 20:48:19 +01:00
count = resampledData['activity_type'].resample('D').count()
#Finding most common activity of the day
mostCommonActivity = resampledData['activity_type'].resample('D').apply(lambda x:stats.mode(x)[0])
#finding different number of activities during a day
uniqueActivities = resampledData['activity_type'].resample('D').nunique()
#finding Number of times activity changed
2019-11-18 20:22:08 +01:00
resampledData['activity_type_shift'] = resampledData['activity_type'].shift().fillna(resampledData['activity_type'].head(1),inplace=True)
resampledData['different_activity'] = np.where(resampledData['activity_type']!=resampledData['activity_type_shift'],1,0)
2019-11-12 20:48:19 +01:00
countChanges = resampledData['different_activity'].resample('D').sum()
2019-11-18 20:22:08 +01:00
finalDataset = pd.concat([count, mostCommonActivity, uniqueActivities, countChanges],axis=1)
2019-11-12 20:48:19 +01:00
2019-11-18 20:22:08 +01:00
finalDataset.index.names = ['local_date']
finalDataset.columns=columns
finalDataset.to_csv(snakemake.output[0])