CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
adasegroup

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: adasegroup/NEUROML2022
Path: blob/main/seminar2/heuristic.py
Views: 63
1
from collections import defaultdict
2
import re
3
4
5
def create_key(template, outtype=('nii.gz',), annotation_classes=None):
6
if template is None or not template:
7
raise ValueError('Template must be a valid format string')
8
return template, outtype, annotation_classes
9
10
11
def infotodict(seqinfo):
12
"""Heuristic evaluator for determining which runs belong where
13
14
allowed template fields - follow python string module:
15
16
item: index within category
17
subject: participant id
18
seqitem: run number during scanning
19
subindex: sub index within group
20
21
SeqInfo(total_files_till_now=15,
22
example_dcm_file='1.2.840.113619.2.312.6945.1127356.11111.1520442292.27.dcm',
23
series_id='1-RESTING STATE NEW/1',
24
dcm_dir_name='1.2.840.113619.2.312.6945.1127356.12562.1520442107.776',
25
unspecified2='-',
26
unspecified3='-',
27
dim1=256,
28
dim2=256,
29
dim3=15,
30
dim4=1,
31
TR=0.004888,
32
TE=1.3,
33
protocol_name=u'RESTING STATE NEW/1',
34
is_motion_corrected=False,
35
is_derived=False,
36
patient_id=u'13832',
37
study_description=u'RESTING STATE',
38
referring_physician_name='',
39
series_description=u'3 Plane Localizer',
40
sequence_name='fgre',
41
image_type=('ORIGINAL', 'PRIMARY', 'OTHER'),
42
accession_number=u'',
43
patient_age='025Y',
44
patient_sex='F',
45
date='20180101',
46
series_uid='1.2.840.113619.2.312.6945.1127356.12562.1520442107.776')
47
"""
48
#key_template = 'sub-{{subject}}/{}/sub-{{subject}}_{}'
49
key_template = 'sub-{{subject}}/{}/sub-{{subject}}_acq-{}'
50
51
info = defaultdict(list)
52
53
for header in seqinfo:
54
if 'EPI' in str(header.image_type) and 'EPI' in str(header.sequence_name.upper()):
55
task = 'task-' + re.sub('[^a-zA-Z0-9]', '', header.series_description.title())
56
task += '_bold'
57
key = create_key(key_template.format('func', task))
58
elif 'T2' in str(header.protocol_name.upper()) and 'FL' in str(header.protocol_name.upper()):
59
series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())
60
key = create_key(key_template.format('anat', series + '_FLAIR'))
61
elif 'T2' in str(header.protocol_name.upper()):
62
series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())
63
key = create_key(key_template.format('anat', series + '_T2w'))
64
else:
65
series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())
66
key = create_key(key_template.format('anat', series + '_T1w'))
67
info[key].append(header.series_id)
68
return info
69