forked from scitran-apps/dicom-mr-classifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classification_from_label.py
305 lines (274 loc) · 10.1 KB
/
classification_from_label.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
'''
Infer acquisition classification by parsing the description label.
Example usage:
## Update acquisition measurement in the DB
labels=list(db.acquisitions.find({},['label']))
labels_only = []
for l in labels:
labels_only.append(l['label'])
unique_labels = set(labels_only)
uls = list(unique_labels)
for l in uls:
measurement = infer_measurement(l)
db.acquisitions.update_many({'label': l}, {'$set': {'measurement': measurement}})
'''
import re
# Anatomy, T1
def is_anatomy_t1(label):
regexes = [
re.compile('t1', re.IGNORECASE),
re.compile('t1w', re.IGNORECASE),
re.compile('(?=.*3d anat)(?![inplane])', re.IGNORECASE),
re.compile('(?=.*3d)(?=.*bravo)(?![inplane])', re.IGNORECASE),
re.compile('spgr', re.IGNORECASE),
re.compile('tfl', re.IGNORECASE),
re.compile('mprage', re.IGNORECASE),
re.compile('(?=.*mm)(?=.*iso)', re.IGNORECASE),
re.compile('(?=.*mp)(?=.*rage)', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Anatomy, T2
def is_anatomy_t2(label):
regexes = [
re.compile('t2', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Aanatomy, Inplane
def is_anatomy_inplane(label):
regexes = [
re.compile('inplane', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Anatomy, other
def is_anatomy(label):
regexes = [
re.compile('(?=.*IR)(?=.*EPI)', re.IGNORECASE),
re.compile('flair', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Diffusion
def is_diffusion(label):
regexes = [
re.compile('dti', re.IGNORECASE),
re.compile('dwi', re.IGNORECASE),
re.compile('diff_', re.IGNORECASE),
re.compile('diffusion', re.IGNORECASE),
re.compile('(?=.*diff)(?=.*dir)', re.IGNORECASE),
re.compile('hardi', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Diffusion - Derived
def is_diffusion_derived(label):
regexes = [
re.compile('_ADC$', re.IGNORECASE),
re.compile('_TRACEW$', re.IGNORECASE),
re.compile('_ColFA$', re.IGNORECASE),
re.compile('_FA$', re.IGNORECASE),
re.compile('_EXP$', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Functional
def is_functional(label):
regexes = [
re.compile('functional', re.IGNORECASE),
re.compile('fmri', re.IGNORECASE),
re.compile('func', re.IGNORECASE),
re.compile('bold', re.IGNORECASE),
re.compile('resting', re.IGNORECASE),
re.compile('(?=.*rest)(?=.*state)', re.IGNORECASE),
# NON-STANDARD
re.compile('(?=.*ret)(?=.*bars)', re.IGNORECASE),
re.compile('(?=.*ret)(?=.*wedges)', re.IGNORECASE),
re.compile('(?=.*ret)(?=.*rings)', re.IGNORECASE),
re.compile('(?=.*ret)(?=.*check)', re.IGNORECASE),
re.compile('go-no-go', re.IGNORECASE),
re.compile('words', re.IGNORECASE),
re.compile('checkers', re.IGNORECASE),
re.compile('retinotopy', re.IGNORECASE),
re.compile('faces', re.IGNORECASE),
re.compile('rings', re.IGNORECASE),
re.compile('wedges', re.IGNORECASE),
re.compile('emoreg', re.IGNORECASE),
re.compile('conscious', re.IGNORECASE),
re.compile('^REST$'),
re.compile('ep2d', re.IGNORECASE),
re.compile('task', re.IGNORECASE),
re.compile('rest', re.IGNORECASE),
re.compile('fBIRN', re.IGNORECASE),
re.compile('^Curiosity', re.IGNORECASE),
re.compile('^DD_', re.IGNORECASE),
re.compile('^Poke', re.IGNORECASE),
re.compile('^Effort', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Functional, Derived
def is_functional_derived(label):
regexes = [
re.compile('mocoseries', re.IGNORECASE),
re.compile('GLM$', re.IGNORECASE),
re.compile('t-map', re.IGNORECASE),
re.compile('design', re.IGNORECASE),
re.compile('StartFMRI', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Localizer
def is_localizer(label):
regexes = [
re.compile('localizer', re.IGNORECASE),
re.compile('localiser', re.IGNORECASE),
re.compile('survey', re.IGNORECASE),
re.compile('loc\.', re.IGNORECASE),
re.compile(r'\bscout\b', re.IGNORECASE),
re.compile('(?=.*plane)(?=.*loc)', re.IGNORECASE),
re.compile('(?=.*plane)(?=.*survey)', re.IGNORECASE),
re.compile('3-plane', re.IGNORECASE),
re.compile('^loc*', re.IGNORECASE),
re.compile('Scout', re.IGNORECASE),
re.compile('AdjGre', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Shim
def is_shim(label):
regexes = [
re.compile('(?=.*HO)(?=.*shim)', re.IGNORECASE), # Contians 'ho' and 'shim'
re.compile(r'\bHOS\b', re.IGNORECASE),
re.compile('_HOS_', re.IGNORECASE),
re.compile('.*shim', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Fieldmap
def is_fieldmap(label):
regexes = [
re.compile('(?=.*field)(?=.*map)', re.IGNORECASE),
re.compile('(?=.*bias)(?=.*ch)', re.IGNORECASE),
re.compile('field', re.IGNORECASE),
re.compile('fmap', re.IGNORECASE),
re.compile('topup', re.IGNORECASE),
re.compile('DISTORTION', re.IGNORECASE),
re.compile('se[-_][aprl]{2}$', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Calibration
def is_calibration(label):
regexes = [
re.compile('(?=.*asset)(?=.*cal)', re.IGNORECASE),
re.compile('^asset$', re.IGNORECASE),
re.compile('calibration', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Coil Survey
def is_coil_survey(label):
regexes = [
re.compile('(?=.*coil)(?=.*survey)', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Perfusion: Arterial Spin Labeling
def is_perfusion(label):
regexes = [
re.compile('asl', re.IGNORECASE),
re.compile('(?=.*blood)(?=.*flow)', re.IGNORECASE),
re.compile('(?=.*art)(?=.*spin)', re.IGNORECASE),
re.compile('tof', re.IGNORECASE),
re.compile('perfusion', re.IGNORECASE),
re.compile('angio', re.IGNORECASE),
]
return regex_search_label(regexes, label)
# Proton Density
def is_proton_density(label):
regexes = [
re.compile('^PD$'),
re.compile('(?=.*proton)(?=.*density)', re.IGNORECASE),
re.compile('pd_'),
re.compile('_pd')
]
return regex_search_label(regexes, label)
# Phase Map
def is_phase_map(label):
regexes = [
re.compile('(?=.*phase)(?=.*map)', re.IGNORECASE),
re.compile('^phase$', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Screen Save / Screenshot
def is_screenshot(label):
regexes = [
re.compile('(?=.*screen)(?=.*save)', re.IGNORECASE),
re.compile('.*screenshot', re.IGNORECASE),
re.compile('.*screensave', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Utility: Check a list of regexes for truthyness
def regex_search_label(regexes, label):
if any(regex.search(label) for regex in regexes):
return True
else:
return False
# Spectroscopy
def is_spectroscopy(label):
regexes = [
re.compile('mip', re.IGNORECASE),
re.compile('mrs', re.IGNORECASE),
re.compile('svs', re.IGNORECASE),
re.compile('GABA', re.IGNORECASE),
re.compile('csi', re.IGNORECASE)
]
return regex_search_label(regexes, label)
# Call all functions to determine new label
def infer_classification(label):
if not label:
return {}
else:
classification = {}
if is_anatomy_inplane(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['T1']
classification['Features'] = ['In-Plane']
elif is_fieldmap(label):
classification['Intent'] = ['Fieldmap']
classification['Measurement'] = ['B0']
elif is_diffusion_derived(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['Diffusion']
classification['Features'] = ['Derived']
elif is_diffusion(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['Diffusion']
elif is_functional_derived(label):
classification['Intent'] = ['Functional']
classification['Features'] = ['Derived']
elif is_functional(label):
classification['Intent'] = ['Functional']
classification['Measurement'] = ['T2*']
elif is_anatomy_t1(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['T1']
elif is_anatomy_t2(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['T2']
elif is_anatomy(label):
classification['Intent'] = ['Structural']
elif is_localizer(label):
classification['Intent'] = ['Localizer']
classification['Measurement'] = ['T2']
elif is_shim(label):
classification['Intent'] = ['Shim']
elif is_calibration(label):
classification['Intent'] = ['Calibration']
elif is_coil_survey(label):
classification['Intent'] = ['Calibration']
classification['Measurement'] = ['B1']
elif is_proton_density(label):
classification['Intent'] = ['Structural']
classification['Measurement'] = ['PD']
elif is_perfusion(label):
classification['Measurement'] = ['Perfusion']
elif is_spectroscopy(label):
classification['Measurement'] = ['Spectroscopy']
elif is_phase_map(label):
classification['Custom'] = ['Phase Map']
elif is_screenshot(label):
classification['Intent'] = ['Screenshot']
else:
print label.strip('\n') + ' --->>>> unknown'
return classification