-
Notifications
You must be signed in to change notification settings - Fork 5
/
smash.py
255 lines (180 loc) · 6.28 KB
/
smash.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import sys
import re
import commands
def get_vectors(strat):
paths = []
def dfs(val_list, path):
if val_list == []:
paths.append(list(path.split(',')[1:]))
else:
vals = val_list.pop()
for val in vals:
dfs(list(val_list), path + ',' + val)
key_list = []
val_list = []
for k, v in sorted(strat.items()):
key_list.append(k)
val_list.append(v)
dfs(val_list, '')
return paths
def get_strategies(K, iterables):
p_s_m = {}
sm = {}
strat = {}
for key in K:
vals = key.split('/')
vals.remove('')
for val in vals:
if not val in p_s_m:
p_s_m[val] = 1
k = sorted(p_s_m.keys())
for val in k:
match = 0
for variable in iterables:
if variable in val and val.split(variable)[0] == '':
match = 1
it = val.split(variable)[1]
if variable in strat:
item = strat[variable]
item.append(val)
else:
strat[variable] = [val]
if match == 0:
strat[val] = [val]
strat_vect = get_vectors(strat)
return strat_vect
def get_match(sink_dir, p, strategies):
p1 = p.split(sink_dir)[1:]
p1 = p1[0]
p_vals = p1.split('/')[2:]
print p_vals
flag = 0
strats = []
for strat in strategies:
flag = 0
for val in p_vals:
if not val in strat:
flag = 1
break
if flag == 0:
strats.append(str(strat))
if strats == []:
print "WARNING : No strategy found for ", p
return None
return strats
def smash(sink_dir, maps, strategies):
new_maps = {}
for p in sorted(maps.keys()):
strats = get_match(sink_dir, p, strategies)
for strat in strats:
if strat in new_maps:
list1 = list(new_maps[strat])
new_maps[strat] = sorted(list(set(maps[p] + list1)))
else:
new_maps[strat] = sorted(list(maps[p]))
return dict(new_maps)
def make_links(new_maps, sink_dir, sub):
sym_path = os.path.join(sink_dir, 'sym_links')
cmd = 'mkdir -p %s' % (sym_path)
print cmd
commands.getoutput(cmd)
idx = 0
f = open('%s/label_linkage.txt' % (sym_path), 'w')
labels = {}
strats = sorted(new_maps.keys())
orig_sub_dir = os.path.join(sink_dir, sub)
wfs = sorted(os.listdir(orig_sub_dir))
sessions = os.listdir(os.path.join(orig_sub_dir, wfs[1]))
for strat in strats:
idx += 1
print >>f, 'label_'+str(idx), ' ', strat
labels[strat] = 'label_'+str(idx)
l_path = os.path.join(sym_path, 'label_'+str(idx))
cmd = 'mkdir -p %s' % (l_path)
print cmd
commands.getoutput(cmd)
sub_path = os.path.join(l_path, sub)
cmd = 'mkdir -p %s' % (sub_path)
print cmd
commands.getoutput(cmd)
for wf in wfs:
wf_path = os.path.join(sub_path, wf)
cmd = 'mkdir -p %s' % (wf_path)
print cmd
commands.getoutput(cmd)
for file in new_maps[strat]:
if '/' + wf + '/' in file:
cmd = 'ln -s %s %s' %(file, os.path.join(wf_path, os.path.basename(file)))
print cmd
commands.getoutput(cmd)
f.close()
def make_sym_links(sink_dir, strategies, subj_list):
for subject in subj_list:
maps = {}
dfs_string = commands.getoutput("find %s/ -type f | perl -lne 'print tr:/::, \" $_\"' | sort -n | cut -d' ' -f2" % (os.path.join(sink_dir, subject)))
dfs_files = dfs_string.split('\n')
for file in dfs_files:
pdir = os.path.dirname(file)
val = None
if pdir in maps:
val = maps[pdir]
val.append(file)
else:
val = [file]
maps[os.path.dirname(file)] = val
new_maps = smash(sink_dir, maps, strategies)
make_links(new_maps, sink_dir, subject)
def main():
import os
sink_dir = '/home/data/PreProc/ABIDE/Leuven_2'
subj_list = os.listdir(sink_dir)
subjectDir = '/home/data/PreProc/ABIDE/Leuven_2/%s' % (subj_list[0])
iterables = ['threshold', 'csf_threshold', 'fwhm', 'gm_threshold', 'hp', 'lp', 'nc', 'scrubbed', 'seeds', 'selector', 'session_id', 'target_angle', 'wm_threshold']
wfs = os.listdir(subjectDir)
labels = {}
label_id = 0
dfs_string = commands.getoutput("find %s/ -type f | perl -lne 'print tr:/::, \" $_\"' | sort -n | cut -d' ' -f2" % (subjectDir))
dfs_files = dfs_string.split('\n')
maps = {}
m1 = {}
for file in dfs_files:
f = str(file)
spath = os.path.join(sink_dir, subj_list[0])
vmhc_path = os.path.join(spath, 'vmhc')
alff_path = os.path.join(spath, 'alff')
nuisance_path = os.path.join(spath, 'nuisance')
sca_path = os.path.join(spath, 'sca')
scrubbing_path = os.path.join(spath, 'scrubbing')
anat_path = os.path.join(spath, 'anat')
func_path = os.path.join(spath, 'func')
seg_path = os.path.join(spath, 'segment')
reg_path = os.path.join(spath, 'reg')
file = re.sub(r'%s' % (vmhc_path), '', file)
file = re.sub(r'%s' % (alff_path), '', file)
file = re.sub(r'%s' % (nuisance_path), '', file)
file = re.sub(r'%s' % (sca_path), '', file)
file = re.sub(r'%s' % (scrubbing_path), '', file)
file = re.sub(r'%s' % (anat_path), '', file)
file = re.sub(r'%s' % (func_path), '', file)
file = re.sub(r'%s' % (reg_path), '', file)
file = re.sub(r'%s' % (seg_path), '', file)
#print file
pdir = os.path.dirname(file)
val = None
v = None
if pdir in maps:
val = maps[pdir]
v = m1[pdir]
val.append(file)
v.append(f)
else:
val = [file]
v = [f]
maps[os.path.dirname(file)] = val
m1[os.path.dirname(file)] = v
strategies = list(get_strategies(maps.keys(), iterables))
print strategies
make_sym_links(sink_dir, strategies, subj_list)
if __name__ == "__main__":
sys.exit(main())