Add light features

replace/8362dcb7e81340b31dd3e298cdc36f36fb99df59
Meng Li 2020-01-14 09:51:39 -05:00
parent a67eeda255
commit 2b4e29ce6e
4 changed files with 51 additions and 1 deletions

View File

@ -33,6 +33,9 @@ rule all:
expand("data/processed/{pid}/screen_{day_segment}.csv",
pid = config["PIDS"],
day_segment = config["SCREEN"]["DAY_SEGMENTS"]),
expand("data/processed/{pid}/light_{day_segment}.csv",
pid = config["PIDS"],
day_segment = config["LIGHT"]["DAY_SEGMENTS"]),
# Reports
expand("reports/figures/{pid}/{sensor}_heatmap_rows.html", pid=config["PIDS"], sensor=config["SENSORS"]),
expand("reports/figures/{pid}/compliance_heatmap.html", pid=config["PIDS"]),

View File

@ -72,4 +72,8 @@ SCREEN:
DAY_SEGMENTS: *day_segments
METRICS_EVENTS: ["counton", "countunlock", "unlocksperminute"]
METRICS_DELTAS: ["sumduration", "maxduration", "minduration", "avgduration", "stdduration"]
EPISODES: ["unlock"]
EPISODES: ["unlock"]
LIGHT:
DAY_SEGMENTS: *day_segments
METRICS: ["count", "maxlux", "minlux", "avglux", "medianlux", "stdlux"]

View File

@ -108,3 +108,14 @@ rule screen_metrics:
"data/processed/{pid}/screen_{day_segment}.csv"
script:
"../src/features/screen_metrics.py"
rule light_metrics:
input:
"data/raw/{pid}/light_with_datetime.csv",
params:
day_segment = "{day_segment}",
metrics = config["LIGHT"]["METRICS"],
output:
"data/processed/{pid}/light_{day_segment}.csv"
script:
"../src/features/light_metrics.py"

View File

@ -0,0 +1,32 @@
import pandas as pd
import numpy as np
light_data = pd.read_csv(snakemake.input[0], parse_dates=["local_date_time", "local_date"])
day_segment = snakemake.params["day_segment"]
metrics = snakemake.params["metrics"]
light_features = pd.DataFrame(columns=["local_date"] + ["light_" + day_segment + "_" + x for x in metrics])
if not light_data.empty:
if day_segment != "daily":
light_data =light_data[light_data["local_day_segment"] == day_segment]
if not light_data.empty:
light_features = pd.DataFrame()
if "count" in metrics:
light_features["light_" + day_segment + "_count"] = light_data.groupby(["local_date"]).count()["timestamp"]
# get light ambient luminance related features
if "maxlux" in metrics:
light_features["light_" + day_segment + "_maxlux"] = light_data.groupby(["local_date"])["double_light_lux"].max()
if "minlux" in metrics:
light_features["light_" + day_segment + "_minlux"] = light_data.groupby(["local_date"])["double_light_lux"].min()
if "avglux" in metrics:
light_features["light_" + day_segment + "_avglux"] = light_data.groupby(["local_date"])["double_light_lux"].mean()
if "medianlux" in metrics:
light_features["light_" + day_segment + "_medianlux"] = light_data.groupby(["local_date"])["double_light_lux"].median()
if "stdlux" in metrics:
light_features["light_" + day_segment + "_stdlux"] = light_data.groupby(["local_date"])["double_light_lux"].std()
light_features = light_features.fillna(0).reset_index()
light_features.to_csv(snakemake.output[0], index=False)