Created
September 26, 2012 11:57
-
-
Save satra/3787612 to your computer and use it in GitHub Desktop.
Dicom conversion heuristic script examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isMoco(dcmfile): | |
| """Determine if a dicom file is a mocoseries | |
| """ | |
| import subprocess | |
| cmd = ['mri_probedicom', '--i', dcmfile, '--t', '8', '103e'] | |
| proc = subprocess.Popen(cmd, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE) | |
| stdout, stderr = proc.communicate() | |
| return stdout.strip().startswith('MoCoSeries') | |
| def infotodict(sdir, dicominfofile): | |
| """Heuristic evaluator for determining which runs belong where | |
| """ | |
| import numpy as np | |
| from glob import glob | |
| import os | |
| #raise Exception('Please provide your own heuristic file') | |
| seq = np.genfromtxt(dicominfofile,dtype=object) | |
| info = dict(anatomy=dict(highres001=[]), | |
| fieldmap=dict(fieldmap128=dict(magnitude=[], phase=[]), | |
| fieldmap64=dict(magnitude=[], phase=[]), | |
| fieldmap_resting=dict(magnitude=[], phase=[])), | |
| diffusion=dict(dwi=[]), | |
| BOLD=dict(task001_run001=dict(bold=[]), | |
| task002_run001=dict(bold=[]), | |
| task003_run001=dict(bold=[]), | |
| task004_run001=dict(bold=[]), | |
| task005_run001=dict(bold=[]), | |
| task006_run001=dict(bold=[]), | |
| task007_run001=dict(bold=[]), | |
| task007_run002=dict(bold=[]), | |
| resting=[])) | |
| first = True | |
| first_f128 = True | |
| first_f64 = True | |
| first_frest = True | |
| task7 = 0 | |
| for s in seq: | |
| #MPRAGE | |
| x,y,sl,nt = (int(s[6]),int(s[7]),int(s[8]),int(s[9])) | |
| #if (sl == 176) and (nt == 1) and ('MPRAGE' in s[12]): | |
| if ('MPRAGE' in s[12]): | |
| info['anatomy']['highres001'].append(int(s[2])) | |
| elif ('functional' in s[12]) and sl == 27 and nt == 155: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| info['BOLD']['task002_run001']['bold'].append(int(s[2])) | |
| elif ('functional' in s[12]) and sl == 27 and nt == 222: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| info['BOLD']['task003_run001']['bold'].append(int(s[2])) | |
| elif ('functional' in s[12]) and sl == 27 and nt == 114: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| info['BOLD']['task004_run001']['bold'].append(int(s[2])) | |
| elif ('ge_func' in s[12]) and sl == 32 and nt == 324: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| info['BOLD']['task006_run001']['bold'].append(int(s[2])) | |
| elif ('ge_func' in s[12]) and sl == 32 and nt == 136: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| if not task7: | |
| info['BOLD']['task007_run001']['bold'].append(int(s[2])) | |
| task7 += 1 | |
| else: | |
| info['BOLD']['task007_run002']['bold'].append(int(s[2])) | |
| elif ('functional' in s[12]) and sl == 27 and nt == 156: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| if first: | |
| info['BOLD']['task001_run001']['bold'].append(int(s[2])) | |
| first=False | |
| else: | |
| info['BOLD']['task005_run001']['bold'].append(int(s[2])) | |
| elif s[12].startswith('field_mapping'): | |
| if 'Resting' in s[12]: | |
| if first_frest: | |
| info['fieldmap']['fieldmap_resting']['magnitude'].append(int(s[2])) | |
| first_frest=False | |
| else: | |
| info['fieldmap']['fieldmap_resting']['phase'].append(int(s[2])) | |
| elif x ==128: | |
| if first_f128: | |
| info['fieldmap']['fieldmap128']['magnitude'].append(int(s[2])) | |
| first_f128=False | |
| else: | |
| info['fieldmap']['fieldmap128']['phase'].append(int(s[2])) | |
| else: | |
| if first_f64: | |
| info['fieldmap']['fieldmap64']['magnitude'].append(int(s[2])) | |
| first_f64=False | |
| else: | |
| info['fieldmap']['fieldmap64']['phase'].append(int(s[2])) | |
| elif 'Resting' in s[12]: | |
| if not isMoco(glob(os.path.join(sdir,s[1]))[0]): | |
| info['BOLD']['resting'].append(int(s[2])) | |
| elif s[12].startswith('DIFFUSION') and sl == 64 and nt ==70: | |
| info['diffusion']['dwi'].append(int(s[2])) | |
| else: | |
| pass | |
| return info | |
| if __name__ == '__main__': | |
| dcmfile = '/mindhive/gablab/rtsmoking/test_retest/brain_data/niftis/sub01s2/dicominfo.txt' | |
| sdir = '/mindhive/gablab/rtsmoking/test_retest/brain_data/dicoms/sub01s2/' | |
| info = infotodict(sdir,dcmfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment