-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_file
51 lines (44 loc) · 3.05 KB
/
temp_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
for ind, row in atheletics_sleep.iterrows():
ex_date = pd.to_datetime(row["start_time"])
ex_day = ex_date.date()
ex_time = ex_date.time()
check_next = next_break = False
for (dni, wor) in sleep_data.iterrows():
sleep_date = pd.to_datetime(wor["sleep_start_time"])
sleep_day = sleep_date.date()
sleep_time = sleep_date.time()
if (check_next):
## Check if the difference between sleep records is less than 30mins. This is then the split record
diff = sleep_date - prev_sleep_end_dt
if (diff.seconds < 1800): ## Less than 30mins
## Combine the records in exercise data.
atheletics_sleep.loc[ind, "next_motionless_sleep_duration"] += sleep_data.loc[dni, "motionless_sleep_duration"]
atheletics_sleep.loc[ind, "next_total_sleep_duration"] += sleep_data.loc[dni, "total_duration"]
eff = (atheletics_sleep.loc[ind, "next_sleep_efficiency"] + sleep_data.loc[dni, "efficiency"]) / 2
atheletics_sleep.loc[ind, "next_sleep_efficiency"] = eff
prev_sleep_end_dt = pd.to_datetime(wor["sleep_end_time"])
else:
check_next = False
next_break = True
if (sleep_day == ex_day):
if (sleep_time > ex_time):
## Found a sleep record for same day as the exercise day and sleep time later than exercise time
atheletics_sleep.loc[ind, "next_motionless_sleep_duration"] = sleep_data.loc[dni, "motionless_sleep_duration"]
atheletics_sleep.loc[ind, "next_total_sleep_duration"] = sleep_data.loc[dni, "total_duration"]
atheletics_sleep.loc[ind, "next_sleep_efficiency"] = sleep_data.loc[dni, "efficiency"]
if (sleep_data.loc[dni, "total_duration"] < 240): ## Less than 4hrs of sleep. Maybe sleep records are split.
## Store the current date as previous date and check next record as well.
check_next = True
next_break = False
prev_sleep_end_dt = pd.to_datetime(wor["sleep_end_time"])
elif (sleep_day == (ex_date + pd.Timedelta("1 day")).date()):
if (sleep_time.hour < 2):
atheletics_sleep.loc[ind, "next_motionless_sleep_duration"] = sleep_data.loc[dni, "motionless_sleep_duration"]
atheletics_sleep.loc[ind, "next_total_sleep_duration"] = sleep_data.loc[dni, "total_duration"]
atheletics_sleep.loc[ind, "next_sleep_efficiency"] = sleep_data.loc[dni, "efficiency"]
if (sleep_data.loc[dni, "total_duration"] < 240): ## Less than 4hrs of sleep. Maybe sleep records are split.
## Store the current date as previous date and check next record as well.
check_next = True
next_break = False
prev_sleep_end_dt = pd.to_datetime(wor["sleep_end_time"])
if next_break: break