Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mean temperature and light report at predictions.csv #53

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions src/asleep/get_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd
import json
import os
import sys
import joblib
import urllib
import shutil
Expand Down Expand Up @@ -149,6 +150,26 @@ 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.")

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]
Copy link
Member

@angerhang angerhang Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include an exception handler here to exit the file processing when no temperature or light data is available? No all the devices have these information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also some of the CI testes failed. Would be good if these are also addressed :D

https://github.com/OxWearables/asleep/actions/runs/9999071709/job/27810403819?pr=53

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spiousas the CI test still failed though. The formatting should be easy to fix :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, I missed some fixes. Now it should be OK.


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
# non_wear_flag: N x 1
Expand Down Expand Up @@ -240,6 +261,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 adds mean temp. and light columns to the predictions.csv file.")
parser.add_argument(
"--pytorch_device",
"-d",
Expand Down Expand Up @@ -305,6 +330,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)
Expand Down Expand Up @@ -346,14 +377,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())
Expand Down
Loading