From e455af47fc98e7f41beddd0c835a594266863958 Mon Sep 17 00:00:00 2001 From: spiousas Date: Thu, 18 Jul 2024 14:36:57 -0700 Subject: [PATCH 1/3] Added mean temperature and light report --- src/asleep/get_sleep.py | 53 ++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/asleep/get_sleep.py b/src/asleep/get_sleep.py index 646115e..ac4af62 100644 --- a/src/asleep/get_sleep.py +++ b/src/asleep/get_sleep.py @@ -148,6 +148,20 @@ def transform_data2model_input( non_wear = np.load(non_wear_path) return data2model, times, non_wear +def mean_temp_and_light(data): + # Calculates mean temperature and light for each 30s window + print("Calculating mean temperature and light for each 30s interval.") + + data['time'] = pd.to_datetime(data['time']) + # Grouping by time + grouped = data.groupby(pd.Grouper(key='time', freq='30S', origin=data['time'].min())) + # Calculating the mean temperature and light for each group + mean_values = grouped[['temperature', 'light']].mean().iloc[:-1] + + temp = mean_values["temperature"].to_numpy() + light = mean_values["light"].to_numpy() + + return(temp, light) def get_sleep_windows(data2model, times, non_wear, args): # data2model: N x 3 x 900 @@ -240,6 +254,10 @@ def main(): action="store_true", help="Remove intermediate files to save space but it " "will take longer to run the next time.") + parser.add_argument( + "--report_light_and_temp", + action="store_true", + help="If true, it will add mean temperature and light columns to the predictions.csv file.") parser.add_argument( "--pytorch_device", "-d", @@ -305,6 +323,12 @@ def main(): print("times shape: {}".format(times.shape)) print("Non_wear flag shape: {}".format(non_wear.shape)) + # 1.2 Get the mean temperature and light (optional) + if args.report_light_and_temp: + temp, light = mean_temp_and_light(data) + print("temperature shape: {}".format(temp.shape)) + print("light shape: {}".format(light.shape)) + # times and non-wear flag need to be stored for visualization if args.remove_intermediate_files: os.remove(data2model_path) @@ -346,14 +370,27 @@ def main(): sleep_stage_predictions = np.vectorize( SLEEPNET_THRE_CLASS_LABELS.get)(sleepnet_output) - predictions_df = pd.DataFrame( - { - 'time': times, - 'sleep_wake': sleep_wake_predictions, - 'sleep_stage': sleep_stage_predictions, - 'raw_label': sleepnet_output, - } - ) + if args.report_light_and_temp: + predictions_df = pd.DataFrame( + { + 'time': times, + 'sleep_wake': sleep_wake_predictions, + 'sleep_stage': sleep_stage_predictions, + 'raw_label': sleepnet_output, + 'temperature': temp, + 'light': light, + } + ) + else: + predictions_df = pd.DataFrame( + { + 'time': times, + 'sleep_wake': sleep_wake_predictions, + 'sleep_stage': sleep_stage_predictions, + 'raw_label': sleepnet_output, + } + ) + final_prediction_path = os.path.join(args.outdir, 'predictions.csv') print("predictions_df shape: {}".format(predictions_df.shape)) print(predictions_df.head()) From a02b3f9d7654bc1fffd50f5ff8e685323a57aa96 Mon Sep 17 00:00:00 2001 From: Ignacio Spiousas Date: Tue, 23 Jul 2024 15:23:15 -0700 Subject: [PATCH 2/3] PR changes adressed --- src/asleep/get_sleep.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/asleep/get_sleep.py b/src/asleep/get_sleep.py index ac4af62..16b4ac2 100644 --- a/src/asleep/get_sleep.py +++ b/src/asleep/get_sleep.py @@ -149,6 +149,10 @@ def transform_data2model_input( return data2model, times, non_wear def mean_temp_and_light(data): + # It stops processing if data does not include temperature and light columns + if not {'temperature', 'light'}.issubset(data.columns): + sys.exit('There is no temperature and light columns in the raw data.') + # Calculates mean temperature and light for each 30s window print("Calculating mean temperature and light for each 30s interval.") @@ -257,7 +261,7 @@ def main(): parser.add_argument( "--report_light_and_temp", action="store_true", - help="If true, it will add mean temperature and light columns to the predictions.csv file.") + help="If true, it adds mean temp. and light columns to the predictions.csv file.") parser.add_argument( "--pytorch_device", "-d", From c0b029c7c42103fc7c0908e88fbcc73eb900510b Mon Sep 17 00:00:00 2001 From: Ignacio Spiousas Date: Wed, 24 Jul 2024 11:05:49 -0700 Subject: [PATCH 3/3] Added changes for checks --- src/asleep/get_sleep.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/asleep/get_sleep.py b/src/asleep/get_sleep.py index 16b4ac2..bd1ee39 100644 --- a/src/asleep/get_sleep.py +++ b/src/asleep/get_sleep.py @@ -4,6 +4,7 @@ import pandas as pd import json import os +import sys import joblib import urllib import shutil @@ -148,14 +149,15 @@ def transform_data2model_input( non_wear = np.load(non_wear_path) return data2model, times, non_wear + def mean_temp_and_light(data): # It stops processing if data does not include temperature and light columns if not {'temperature', 'light'}.issubset(data.columns): sys.exit('There is no temperature and light columns in the raw data.') - + # Calculates mean temperature and light for each 30s window print("Calculating mean temperature and light for each 30s interval.") - + data['time'] = pd.to_datetime(data['time']) # Grouping by time grouped = data.groupby(pd.Grouper(key='time', freq='30S', origin=data['time'].min())) @@ -165,7 +167,8 @@ def mean_temp_and_light(data): temp = mean_values["temperature"].to_numpy() light = mean_values["light"].to_numpy() - return(temp, light) + return temp, light + def get_sleep_windows(data2model, times, non_wear, args): # data2model: N x 3 x 900