CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/main/seminar2/heuristic.py
Views: 63
from collections import defaultdict1import re234def create_key(template, outtype=('nii.gz',), annotation_classes=None):5if template is None or not template:6raise ValueError('Template must be a valid format string')7return template, outtype, annotation_classes8910def infotodict(seqinfo):11"""Heuristic evaluator for determining which runs belong where1213allowed template fields - follow python string module:1415item: index within category16subject: participant id17seqitem: run number during scanning18subindex: sub index within group1920SeqInfo(total_files_till_now=15,21example_dcm_file='1.2.840.113619.2.312.6945.1127356.11111.1520442292.27.dcm',22series_id='1-RESTING STATE NEW/1',23dcm_dir_name='1.2.840.113619.2.312.6945.1127356.12562.1520442107.776',24unspecified2='-',25unspecified3='-',26dim1=256,27dim2=256,28dim3=15,29dim4=1,30TR=0.004888,31TE=1.3,32protocol_name=u'RESTING STATE NEW/1',33is_motion_corrected=False,34is_derived=False,35patient_id=u'13832',36study_description=u'RESTING STATE',37referring_physician_name='',38series_description=u'3 Plane Localizer',39sequence_name='fgre',40image_type=('ORIGINAL', 'PRIMARY', 'OTHER'),41accession_number=u'',42patient_age='025Y',43patient_sex='F',44date='20180101',45series_uid='1.2.840.113619.2.312.6945.1127356.12562.1520442107.776')46"""47#key_template = 'sub-{{subject}}/{}/sub-{{subject}}_{}'48key_template = 'sub-{{subject}}/{}/sub-{{subject}}_acq-{}'4950info = defaultdict(list)5152for header in seqinfo:53if 'EPI' in str(header.image_type) and 'EPI' in str(header.sequence_name.upper()):54task = 'task-' + re.sub('[^a-zA-Z0-9]', '', header.series_description.title())55task += '_bold'56key = create_key(key_template.format('func', task))57elif 'T2' in str(header.protocol_name.upper()) and 'FL' in str(header.protocol_name.upper()):58series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())59key = create_key(key_template.format('anat', series + '_FLAIR'))60elif 'T2' in str(header.protocol_name.upper()):61series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())62key = create_key(key_template.format('anat', series + '_T2w'))63else:64series = re.sub('[^a-zA-Z0-9]', '', header.series_description.title())65key = create_key(key_template.format('anat', series + '_T1w'))66info[key].append(header.series_id)67return info6869