-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_abide_randomize.py
183 lines (143 loc) · 6.71 KB
/
run_abide_randomize.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
### Example of calling a BIDS-APP to analyze your data
### in the AWS cloud, using a cluster, with multiple
### runs in parallel
# Download the phenotype file, so we know whats what
import urllib
import time
import os
import commands
import pandas as pd
import json
sge_template = \
'''#! %(shell)s
## SGE batch file - %(timestamp)s
#$ -S %(shell)s
#$ -N %(job_name)s
#$ -t 1-%(num_tasks)d
#$ -V
#$ -wd %(work_dir)s
echo "Start - TASKID " %(env_arr_idx)s " : " $(date)
%(run_cmd)s
echo "End - TASKID " %(env_arr_idx)s " : " $(date)
'''
pheno_file_url = "https://s3.amazonaws.com/fcp-indi/data/Projects/" + \
"ABIDE_Initiative/Phenotypic_V1_0b_preprocessed1.csv"
pheno_file_local = "Phenotypic_V1_0b_preprocessed1.csv"
urllib.urlretrieve(pheno_file_url, pheno_file_local)
# Filter the pheno data to focus on the data used in the
# original ABIDE paper
pheno_df = pd.read_csv(pheno_file_local)
pheno_df.head()
# find the datasets that were in the ABIDE paper, and those
# with a filename
pheno_df = pheno_df.loc[(pheno_df["SUB_IN_SMP"] == 1) &
(pheno_df["FILE_ID"] != "no_filename"), :]
# reduce the dataframe to just the columns we are interested in
pheno_df = pheno_df[['FILE_ID','SITE_ID','DX_GROUP','AGE_AT_SCAN','func_mean_fd']]
pheno_df.columns=[u'participant_id',u'site_id',u'dx',u'age',u'mean_fd']
for ndx in pheno_df.index:
new_sub=pheno_df.loc[ndx,u'participant_id']
new_sub=new_sub.replace("_","")
pheno_df.loc[ndx,'participant_id']='-'.join(['sub', new_sub])
if pheno_df.loc[ndx,'dx']==1:
pheno_df.loc[ndx,'dx']='ASD'
else:
pheno_df.loc[ndx,'dx']='TDC'
model_desc = dict(
model='dx+site_id+age+mean_fd-1',
contrasts=['dx[ASD]-dx[TDC]','dx[TDC]-dx[ASD]'])
# write out a list of the participants that meet
pheno_df.to_csv("abide_analy_pheno.csv")
timestamp = str(time.strftime("%Y_%m_%d_%H_%M_%S"))
strats = ['nofilt_global'] # , 'nofilt_noglobal', 'filt_global', 'filt_noglobal']
pipes = ['cpac'] # , 'dparsf', 'niak', 'ccs']
derivatives = { 'degree_binarize': ['dcb', '']}
#derivatives = {'reho': ['reho', ''],
#'lfcd': ['lfcd', ''],
#'func_mean': ['mean', ''],
#'falff': ['falff', ''],
#'eigenvector_weighted': ['ecw', ''],
#'eigenvector_binarize': ['ecb', ''],
#'degree_weighted': ['dcw', ''],
#'degree_binarize': ['dcb', ''],
#'alff': ['alff', ''],
#'vmhc': ['vmhc', '']}
# ABIDE preproc DL template
abide_dl_link = "https://fcp-indi.s3.amazonaws.com/data/Projects/ABIDE_Initiative" + \
"/Derivatives/%(pipe_str)s/sub-%(sub_id)s/ses-%(sess_id)s/func/" + \
"sub-%(sub_id)s_ses-%(sess_id)s_%(deriv_tag)s.nii.gz"
local_file_name = "sub-%(sub_id)s/ses-%(sess_id)s/func/" + \
"sub-%(sub_id)s_ses-%(sess_id)s_%(deriv_tag)s.nii.gz"
inputs_dir = os.path.join(os.path.expanduser('~'), "bids_in")
if not os.path.isdir(inputs_dir):
os.makedirs(inputs_dir)
outputs_dir = os.path.join(os.path.expanduser('~'), "bids_out")
if not os.path.isdir(outputs_dir):
os.makedirs(outputs_dir)
cluster_files_dir = os.path.join(os.path.expanduser('~'), 'cluster_logs')
if not os.path.isdir(cluster_files_dir):
os.makedirs(cluster_files_dir)
for strat in strats:
for pipe in pipes:
for (deriv, deriv_vals) in derivatives.items():
# make the input, output, and clusterlog directories for this processing
deriv_in_dir = os.path.join(inputs_dir, '_'.join([pipe, strat]), deriv)
if not os.path.isdir(deriv_in_dir):
os.makedirs(deriv_in_dir)
deriv_out_dir = os.path.join(outputs_dir, '_'.join([pipe, strat]), deriv)
if not os.path.isdir(deriv_out_dir):
os.makedirs(deriv_out_dir)
deriv_log_dir = os.path.join(cluster_files_dir, '_'.join([pipe, strat]), deriv)
if not os.path.isdir(deriv_log_dir):
os.makedirs(deriv_log_dir)
dl_ndx=[]
for (ndx,row) in pheno_df.iterrows():
data_dl_config = dict(pipe_str='_'.join([pipe, strat]),
sess_id=1,
sub_id=(row['participant_id'].split('-'))[1],
deriv_tag=deriv_vals[0])
data_dl_string = abide_dl_link % data_dl_config
data_local= os.path.join(deriv_in_dir, local_file_name % data_dl_config)
dl_failed=False
if not os.path.isdir(os.path.dirname(data_local)):
os.makedirs(os.path.dirname(data_local))
if not os.path.isfile(data_local):
try:
urllib.urlretrieve(data_dl_string, data_local)
except:
print "could not download %s to %s"%(data_dl_string,data_local)
dl_failed = True
if not dl_failed:
dl_ndx.append(ndx)
# add the participants.tsv to the outdir
(pheno_df.loc[dl_ndx,]).to_csv(os.path.join(deriv_in_dir,"participants.tsv"),
sep='\t',index=False)
with open(os.path.join(deriv_in_dir,'model.json'), 'w') as outfile:
json.dump(model_desc, outfile)
# Batch file variables
shell = commands.getoutput('echo $SHELL')
confirm_str = '(?<=Your job-array )\d+'
exec_cmd = 'qsub'
run_str = "docker run -v %s:/bids_in -v %s:/bids_out bids/randomise:latest /bids_in /bids_out /tmp group /bids_in/model.json"%(deriv_in_dir,deriv_out_dir)
print "submitting: ",run_str
# Set up config dictionary
config_dict = {'timestamp': timestamp,
'shell': shell,
'job_name': 'run_example',
'num_tasks': 1,
'work_dir': cluster_files_dir,
'env_arr_idx': '$SGE_TASK_ID',
'run_cmd': run_str
}
batch_file_contents = sge_template % config_dict
batch_filepath = '/home/ubuntu/NI_cloud_computing/cluster_run.sge'
with open(batch_filepath, "w") as outfd:
outfd.write(batch_file_contents)
import re
# Get output response from job submission
out = commands.getoutput('%s %s' % (exec_cmd, batch_filepath))
# Check for successful qsub submission
if re.search(confirm_str, out) == None:
err_msg = 'Error submitting %s run to sge queue' % \
(config_dict['job_name'])
raise Exception(err_msg)