11import scitran_client .flywheel_analyzer as fa
22from scitran_client import ScitranClient
33
4+ client = ScitranClient ('https://flywheel-cni.scitran.stanford.edu' )
5+ with fa .installed_client (client ):
6+ project = fa .find_project (label = 'ENGAGE' )
7+ sessions = client .request ('projects/{}/sessions' .format (project ['_id' ])).json ()
8+ session_by_subject = {}
9+ second_to_first_visit_id = {}
10+ for s in sessions :
11+ subject = s ['subject' ]['code' ][:7 ].upper ()
12+ session_by_subject .setdefault (subject , []).append (s )
13+ for subject , subject_sessions in session_by_subject .iteritems ():
14+ # we need at least two sessions
15+ if len (subject_sessions ) < 2 :
16+ continue
17+ subject_sessions .sort (key = lambda s : s ['timestamp' ])
18+ # HACK this is a bit of a heuristic. this will certainly fail
19+ # for some folks that skipped a BV, or folks that missed the 2mo
20+ # but hopefully, those folks will otherwise have data that is just
21+ # fine.
22+ second_to_first_visit_id [subject_sessions [1 ]['_id' ]] = subject_sessions [0 ]['_id' ]
23+
424
525# XXX at least make this be just the first thing without ' 2'?
626label_to_task_type = {
@@ -39,9 +59,17 @@ def define_analysis(gear_name, acquisition_label, create_inputs):
3959 label = analysis_label (gear_name , acquisition_label ))
4060
4161
42- def reactivity_inputs (acquisition_label , acquisitions , ** kwargs ):
62+ def reactivity_inputs (acquisition_label , acquisitions , session , ** kwargs ):
4363 functional = fa .find (acquisitions , label = acquisition_label )
4464 structural = fa .find (acquisitions , label = 'T1w 1mm' )
65+ if not structural :
66+ assert session ['_id' ] in second_to_first_visit_id ,\
67+ 'the only sessions that should be missing T1w are second visits. {} was missing a T1w' \
68+ .format (session ['_id' ])
69+ first_visit_session_id = second_to_first_visit_id [session ['_id' ]]
70+ first_visit_acquisitions = client .request (
71+ 'sessions/{}/acquisitions' .format (first_visit_session_id )).json ()
72+ structural = fa .find (first_visit_acquisitions , label = 'T1w 1mm' )
4573
4674 return dict (
4775 functional = functional .find_file ('*.nii.gz' ),
@@ -81,7 +109,7 @@ def first_level_model_inputs(acquisition_label, analyses, acquisitions):
81109 ), dict (task_type = label_to_task_type [acquisition_label ])
82110
83111if __name__ == '__main__' :
84- with fa .installed_client (ScitranClient ( 'https://flywheel-cni.scitran.stanford.edu' ) ):
112+ with fa .installed_client (client ):
85113 fa .run ([
86114 define_analysis ('reactivity-preprocessing' , 'go-no-go 2' , reactivity_inputs ),
87115 define_analysis ('connectivity-preprocessing' , 'go-no-go 2' , connectivity_inputs ),
@@ -98,4 +126,4 @@ def first_level_model_inputs(acquisition_label, analyses, acquisitions):
98126 define_analysis ('reactivity-preprocessing' , 'EmoReg' , reactivity_inputs ),
99127 define_analysis ('connectivity-preprocessing' , 'EmoReg' , connectivity_inputs ),
100128 # define_analysis('first-level-models', 'EmoReg', first_level_model_inputs),
101- ], project = fa . find_project ( label = 'ENGAGE' ) )
129+ ], project = project )
0 commit comments