Update fitbit_parse_steps.py: add summary data

pull/103/head
Meng Li 2020-10-28 13:07:35 -04:00
parent 2489f70ea7
commit 84068ab33c
1 changed files with 18 additions and 2 deletions

View File

@ -4,6 +4,11 @@ import numpy as np
from datetime import datetime, timezone
from math import trunc
STEPS_SUMMARY_COLUMNS = ("device_id",
"steps_rapids_intradaycountallsteps",
"local_date_time",
"timestamp")
STEPS_INTRADAY_COLUMNS = ("device_id",
"steps",
"local_date_time",
@ -14,12 +19,23 @@ def parseStepsData(steps_data):
if steps_data.empty:
return pd.DataFrame(), pd.DataFrame(columns=STEPS_INTRADAY_COLUMNS)
device_id = steps_data["device_id"].iloc[0]
records_intraday = []
records_summary, records_intraday = [], []
# Parse JSON into individual records
for record in steps_data.fitbit_data:
record = json.loads(record) # Parse text into JSON
# Parse summary data
curr_date = datetime.strptime(
record["activities-steps"][0]["dateTime"], "%Y-%m-%d")
row_summary = (device_id,
record["activities-steps"][0]["value"],
curr_date,
0)
records_summary.append(row_summary)
# Parse intraday data
dataset = record["activities-steps-intraday"]["dataset"]
for data in dataset:
d_time = datetime.strptime(data["time"], '%H:%M:%S').time()
@ -32,7 +48,7 @@ def parseStepsData(steps_data):
records_intraday.append(row_intraday)
return pd.DataFrame(data=[], columns=["local_date_time", "timestamp"]), pd.DataFrame(data=records_intraday, columns=STEPS_INTRADAY_COLUMNS)
return pd.DataFrame(data=records_summary, columns=STEPS_SUMMARY_COLUMNS), pd.DataFrame(data=records_intraday, columns=STEPS_INTRADAY_COLUMNS)
table_format = snakemake.params["table_format"]
timezone = snakemake.params["timezone"]