In [64]:
import sys
import os.path as osp
import os
import subprocess
import json
sys.path.append('/home/grg/git/alfa')
os.chdir('/home/grg/git/alfa')

def shell_execute(cmd):
    proc = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, shell=False)
    (out, err) = proc.communicate()
    return out

Simplifying brain-twisting endless commands

and minimize the chance of typos between two tries

Let's say I need to type these commands :

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10013/T1/10013_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10013/DWI/10013_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10013/DWI/ants_elast_dwi-to-t1_.nii.gz

WarpImageMultiTransform 3 /home/grg/data/ALFA_DWI/10013/DWI/10013_MD.nii.gz /home/grg/data/ALFA_DWI/10013/DWI/10013_MD_t1space.nii.gz -R /home/grg/data/ALFA_DWI/10013/T1/10013_mabonlm_nobias.nii /home/grg/data/ALFA_DWI/10013/DWI/ants_elast_dwi-to-t1_Warp.nii.gz /home/grg/data/ALFA_DWI/10013/DWI/ants_elast_dwi-to-t1_Affine.txt

I'll first create a JSON giving a template of the commands.


In [50]:
j = {'ants_dwi_to_t1': u'ANTS 3 -m CC[ %s, %s, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o %s',
     'warp_md_to_t1': u'WarpImageMultiTransform 3 %s %s -R %s %s %s'}
json.dump(j, open('/tmp/templates.json','w'))

The $\%s$ represent parameters (such as filenames) that will be provided later.

This is like separating bones (the command itself) from the meat (the data).

$thesaurus$ will then fill in the ($\%s$) blanks. Just name the desired command and give the parameters in the same order as they appear in the command.


In [ ]:


In [58]:
cmd = './thesaurus.py -n --name ants_dwi_to_t1 /tmp/templates.json target.nii moving.nii trans_'
print '$', cmd
print shell_execute(cmd)

cmd = './thesaurus.py -n --name warp_md_to_t1 /tmp/templates.json target.nii moving.nii output.nii trans_Warp.nii trans_Affine.txt'
print '\n$', cmd
print shell_execute(cmd)


$ ./thesaurus.py -n --name ants_dwi_to_t1 /tmp/templates.json target.nii moving.nii trans_
ANTS 3 -m CC[ target.nii, moving.nii, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o trans_


$ ./thesaurus.py -n --name warp_md_to_t1 /tmp/templates.json target.nii moving.nii output.nii trans_Warp.nii trans_Affine.txt
WarpImageMultiTransform 3 target.nii moving.nii -R output.nii trans_Warp.nii trans_Affine.txt

The $-n$ option is just a safety lock to display the command only and not to run it.

Pushing it further..

$thesaurus$ can work with structured databases and preexisting filetypes. Here let's define the various IO of the previous commands.

$@$ indicate input items that must exist prior to running the command. $>$ indicate output items that will be generated by the command.

Naturally all the different filetypes have to be predefined and recognized by the database system.


In [65]:
j = {'ants_dwi_to_t1': [u'@ALFA Denoised Nobias SPM Dilated White matter',
         u'@ALFA DWI B0 Brain FSL FAST White matter',
         u'ALFA ANTS Elast DWI to T1 Transformation Template Filename'],
     'warp_md_to_t1': [u'@ALFA Mean Diffusivity Image',
         u'>ALFA DWI MD Map Warped to T1 space',
         u'@ALFA Denoised Nobias T1 Image',
         u'@ALFA ANTS Elast DWI to T1 Transformation',
         u'@ALFA ANTS Elast DWI to T1 Affine Transformation']}
json.dump(j, open('/tmp/templates_io.json', 'w'))

Then call the desired command by its keyname, with the desired subject and feel light...


In [68]:
import thesaurus as t
a = t.ALFAHelper() #jsonfile='/tmp/templates_io.json')
a.parse_command(subject='10013', name='warp_md_to_t1')


Out[68]:
u'WarpImageMultiTransform 3 /home/grg/data/ALFA_DWI/10013/DWI/10013_MD.nii.gz /home/grg/data/ALFA_DWI/10013/DWI/10013_MD_t1space.nii.gz -R /home/grg/data/ALFA_DWI/10013/T1/10013_mabonlm_nobias.nii /home/grg/data/ALFA_DWI/10013/DWI/ants_elast_dwi-to-t1_Warp.nii.gz /home/grg/data/ALFA_DWI/10013/DWI/ants_elast_dwi-to-t1_Affine.txt'

Based on this file it can also recreate the sequence of a pipeline and that provide you with the progression state of a subject along this pipeline.


In [77]:
step1 = a.current_stage('10013')
print 'step:', step1, '\n'

step2 = a.current_stage('55855')
print 'step:', step2


10013 is complete
step: 0 

55855 is stuck at step before warp_aal_to_dwi (missing: ALFA ANTS Elast T1 to DWI Transformation)
step: warp_aal_to_dwi-1

OK now enough talking. Can you please run that d@£( step over my entire dataset ?


In [86]:
d = '/home/grg/data/ALFA_DWI/'
from glob import glob
subjects = [e.split('/')[-1] for e in glob(osp.join(d, '*')) if osp.isdir(e)]

In [89]:
a.generate_batch(subjects, step='ants_dwi_to_t1', batchfile='/tmp/batch.sh', create_workflow=True)


Failed 10779 /home/grg/data/ALFA_DWI/10779/DWI/10779_B0_brain_seg_1.nii.gz not found (type ALFA DWI B0 Brain FSL FAST White matter) while declared as input (or remove the leading @)
Failed snapshots /home/grg/data/ALFA_DWI/snapshots/T1/snapshots_mabonlm_nobias_spm_c2_d2.nii.gz not found (type ALFA Denoised Nobias SPM Dilated White matter) while declared as input (or remove the leading @)
Failed duplicates /home/grg/data/ALFA_DWI/duplicates/T1/duplicates_mabonlm_nobias_spm_c2_d2.nii.gz not found (type ALFA Denoised Nobias SPM Dilated White matter) while declared as input (or remove the leading @)
Out[89]:
['21056',
 '11136',
 '12636',
 '10472',
 '10265',
 '77109',
 '11943',
 '10225',
 '10338',
 '10117',
 '10162',
 '12975',
 '10396',
 '11248',
 '55297',
 '13070',
 '11902',
 '21057c',
 '10822',
 '13244',
 '55809',
 '55278',
 '13242',
 '12067',
 '10682',
 '12810',
 '10330',
 '66293',
 '11620',
 '11415',
 '77175',
 '10025',
 '11291',
 '10461',
 '10151',
 '12878',
 '12308',
 '12861',
 '21092',
 '77130',
 '13215',
 '11461',
 '12704',
 '10051',
 '12904',
 '10518',
 '11137',
 '10312',
 '10385',
 '77151',
 '12812',
 '10090',
 '11666',
 '77195',
 '10703',
 '11152',
 '11641',
 '11592',
 '11550',
 '10841',
 '12324',
 '11191',
 '11133',
 '13164',
 '77056',
 '11071',
 '11201',
 '10645',
 '55708',
 '10108',
 '11219',
 '11416',
 '10016',
 '12122',
 '12330',
 '77034',
 '12140',
 '10552',
 '77217',
 '55855',
 '10482',
 '12125',
 '12841',
 '44491',
 '66048',
 '10692',
 '12079',
 '55469',
 '66029',
 '10881',
 '12015',
 '10550',
 '12417',
 '10988',
 '66368',
 '11047',
 '10367',
 '11778c',
 '11638',
 '10217',
 '10182',
 '77152',
 '77254',
 '55210',
 '11407',
 '12356',
 '12425',
 '11979',
 '12056',
 '10242',
 '44151',
 '10724',
 '13235',
 '10199',
 '10365',
 '66128',
 '10036',
 '77140',
 '10821',
 '44043',
 '11975',
 '13238',
 '10735',
 '10966',
 '10317',
 '10035',
 '10023',
 '11686',
 '11796',
 '10778',
 '13345',
 '11874',
 '10504',
 '10744',
 '11691',
 '12970',
 '11245',
 '13127',
 '10697',
 '13059',
 '10972',
 '10084',
 '11458',
 '11798',
 '21012',
 '12244',
 '44027',
 '12269',
 '44660',
 '11048',
 '66050',
 '66162',
 '10577',
 '55488',
 '66042',
 '10049',
 '12516',
 '11323',
 '10259',
 '12245',
 '77224',
 '66361',
 '55216',
 '10515',
 '10775',
 '12941',
 '12823',
 '10418',
 '13417',
 '55734',
 '11063',
 '11225',
 '11721',
 '10850',
 '12699',
 '10042',
 '12976',
 '10200',
 '66239',
 '77076',
 '10322',
 '12914',
 '13144',
 '11583',
 '21084',
 '10448',
 '55778',
 '10787',
 '44086',
 '11630',
 '13019cc',
 '12995',
 '12874',
 '11597',
 '66133',
 '10942',
 '10370',
 '10013',
 '77027',
 '10870',
 '10034',
 '44141',
 '10361',
 '66131',
 '55483',
 '10170',
 '66267',
 '12787',
 '10750',
 '13217',
 '11939',
 '11139',
 '44632',
 '13061',
 '10668',
 '11045',
 '10630',
 '10019',
 '10393',
 '10334',
 '11383',
 '55630',
 '10294',
 '11007',
 '10741',
 '11703',
 '10329',
 '11127',
 '11351',
 '55370',
 '11213',
 '10901',
 '66019',
 '10894',
 '44147',
 '12138',
 '12783',
 '11222',
 '10416',
 '11436',
 '77263',
 '12174',
 '44119',
 '13008',
 '66270',
 '12484',
 '55793',
 '66215',
 '10298',
 '10113',
 '11360',
 '13214',
 '77117',
 '77096',
 '10593',
 '66211',
 '13043',
 '13083',
 '10226',
 '10496',
 '77068',
 '55323',
 '10158',
 '11514',
 '12947',
 '11262',
 '12537',
 '66030',
 '21039',
 '10450',
 '10354',
 '77179',
 '66026',
 '10944',
 '10613',
 '11114',
 '11042',
 '10858',
 '10809',
 '21026',
 '55355',
 '77037',
 '55667',
 '10346',
 '66294',
 '12851',
 '11196',
 '11247',
 '66124',
 '11264',
 '10186',
 '77192',
 '11252',
 '10118',
 '12483',
 '11019',
 '10099',
 '10541',
 '11184',
 '11614',
 '13367',
 '10433',
 '10693',
 '10053',
 '55057',
 '11768',
 '13169',
 '12156',
 '12582',
 '10811',
 '12930',
 '44105',
 '12409',
 '10028',
 '10040',
 '55388',
 '12252',
 '66335',
 '12379',
 '10335',
 '11540',
 '10052',
 '10150',
 '77252',
 '12772',
 '13293',
 '10324',
 '55351',
 '10696',
 '11012',
 '10024',
 '13312',
 '11038',
 '11180',
 '11937',
 '10486',
 '12502',
 '10251',
 '13063',
 '10096',
 '12711',
 '66039',
 '66125',
 '77024',
 '10511',
 '10426',
 '12624',
 '10634',
 '55854',
 '11305',
 '10453',
 '11478',
 '66183',
 '12724',
 '11092',
 '13163',
 '10551',
 '44046',
 '10522',
 '10123',
 '21051',
 '13105',
 '21073',
 '12715',
 '10156',
 '10160',
 '11254',
 '10326',
 '10737',
 '11858',
 '10417',
 '10846',
 '11156',
 '10026',
 '11223',
 '12730',
 '12279',
 '12445',
 '66240',
 '13075',
 '10245',
 '10947',
 '13090',
 '10029',
 '66020',
 '11016',
 '77044',
 '11610',
 '44091',
 '11327',
 '11487c',
 '11646',
 '12032',
 '10325',
 '11217',
 '12548',
 '10436',
 '10134',
 '12771',
 '11526',
 '12493',
 '10362',
 '77093',
 '13035',
 '66172',
 '66089',
 '11257c',
 '66257',
 '55058',
 '11687',
 '12391',
 '10015',
 '10855',
 '10536',
 '13049',
 '10463',
 '77188',
 '11552',
 '10102',
 '12765',
 '10235',
 '21011',
 '55539',
 '10678',
 '44094',
 '12133',
 '12511',
 '77040',
 '10070',
 '10528',
 '66268',
 '11426',
 '44229',
 '11414',
 '11590',
 '10166',
 '12121',
 '12893',
 '13268',
 '10071',
 '10212',
 '12172',
 '66044',
 '44733',
 '44068',
 '10576',
 '13322',
 '13151',
 '12785',
 '12479',
 '12271',
 '10248',
 '44062',
 '21042',
 '55142',
 '11850',
 '44004',
 '77241',
 '11195',
 '12637',
 '12659',
 '12323',
 '55538',
 '10010',
 '10538',
 '13054',
 '13309',
 '66164',
 '12399',
 '55353',
 '66017',
 '10563',
 '66169',
 '12296',
 '11387',
 '55166',
 '21130',
 '11561',
 '66492',
 '10530',
 '11292',
 '66341',
 '11474',
 '11872',
 '10038',
 '10725',
 '11355',
 '10263',
 '12327',
 '13236',
 '11941',
 '55636',
 '12767',
 '21002',
 '10282',
 '11435',
 '10253',
 '44723',
 '13138',
 '10397',
 '11737',
 '12186',
 '11144',
 '12858',
 '77047',
 '77094',
 '77051',
 '12920',
 '55152',
 '10178',
 '10657',
 '66312',
 '10586',
 '12239',
 '12304',
 '12542',
 '11747',
 '10032',
 '13118',
 '44057',
 '11882',
 '11656',
 '10144',
 '11593',
 '66264',
 '10180',
 '77191',
 '10640',
 '11054',
 '66094',
 '11015',
 '10493',
 '66085',
 '66498',
 '66309',
 '11481c',
 '10319',
 '11830',
 '10701',
 '10239c',
 '13188',
 '66159',
 '55200',
 '10900',
 '44205',
 '10333c',
 '10756',
 '10056',
 '11030',
 '10419',
 '10794',
 '11829',
 '55529',
 '12778',
 '10065',
 '10041',
 '10213',
 '12963',
 '11711',
 '10081',
 '10308',
 '66141',
 '10946',
 '11679',
 '13306',
 '10106',
 '11803',
 '10313',
 '66171',
 '12331',
 '11658']

In [92]:
b = open('/tmp/batch.sh').readlines()
for each in b[:20]:
    print each


ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/21056/T1/21056_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/21056/DWI/21056_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/21056/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/11136/T1/11136_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/11136/DWI/11136_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/11136/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/12636/T1/12636_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/12636/DWI/12636_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/12636/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10472/T1/10472_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10472/DWI/10472_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10472/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10265/T1/10265_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10265/DWI/10265_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10265/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/77109/T1/77109_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/77109/DWI/77109_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/77109/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/11943/T1/11943_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/11943/DWI/11943_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/11943/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10225/T1/10225_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10225/DWI/10225_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10225/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10338/T1/10338_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10338/DWI/10338_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10338/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10117/T1/10117_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10117/DWI/10117_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10117/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10162/T1/10162_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10162/DWI/10162_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10162/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/12975/T1/12975_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/12975/DWI/12975_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/12975/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10396/T1/10396_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10396/DWI/10396_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10396/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/11248/T1/11248_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/11248/DWI/11248_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/11248/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/55297/T1/55297_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/55297/DWI/55297_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/55297/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/13070/T1/13070_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/13070/DWI/13070_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/13070/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/11902/T1/11902_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/11902/DWI/11902_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/11902/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/21057c/T1/21057c_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/21057c/DWI/21057c_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/21057c/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/10822/T1/10822_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/10822/DWI/10822_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/10822/DWI/ants_elast_dwi-to-t1_.nii.gz

ANTS 3 -m CC[ /home/grg/data/ALFA_DWI/13244/T1/13244_mabonlm_nobias_spm_c2_d2.nii.gz, /home/grg/data/ALFA_DWI/13244/DWI/13244_B0_brain_seg_1.nii.gz, 1, 4] -r Gauss[0,3] -t Elast[1.5] -i 30x20x10 -o /home/grg/data/ALFA_DWI/13244/DWI/ants_elast_dwi-to-t1_.nii.gz


In [ ]: