forked from int-brain-lab/IBL-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_latest_date.py
55 lines (48 loc) · 1.95 KB
/
compute_latest_date.py
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
52
53
54
55
'''
This script computes the latest event date happen to individual subject
and manually insert into the table behavior_plotting.LatestDate
'''
import datajoint as dj
from ibl_pipeline import reference, subject, action, acquisition, data, behavior
from ibl_pipeline.analyses import behavior as behavior_analyses
from ibl_pipeline.plotting import behavior as behavior_plotting
import numpy as np
from datetime import datetime
for key in subject.Subject.fetch('KEY'):
behavior_summary = behavior_analyses.BehavioralSummaryByDate & key
water_weight = action.Weighing * action.WaterAdministration & key
if behavior_summary:
latest_behavior = subject.Subject.aggr(
behavior_summary,
last_behavior_date='MAX(session_date)')
if water_weight:
latest_weight = subject.Subject.aggr(
action.Weighing & key,
last_weighing_date='DATE(MAX(weighing_time))')
latest_water = subject.Subject.aggr(
action.WaterAdministration & key,
last_water_date='DATE(MAX(administration_time))')
latest_water_weight = (latest_water * latest_weight).proj(
last_water_weight_date='GREATEST(last_water_date, \
last_weighing_date)'
)
if not(behavior_summary or water_weight):
continue
elif behavior_summary and water_weight:
last_behavior_date = latest_behavior.fetch1(
'last_behavior_date'
)
last_water_weight_date = latest_water_weight.fetch1(
'last_water_weight_date'
)
latest_date = max([last_behavior_date, last_water_weight_date])
elif behavior_summary:
latest_date = latest_behavior.fetch1(
'last_behavior_date'
)
elif water_weight:
latest_date = latest_water_weight.fetch1(
'last_water_weight_date'
)
key['latest_date'] = latest_date
behavior_plotting.LatestDate.insert1(key)