In [ ]:
import common
reload(common)
processed_dir = common.processed_dir
bedpost_script_path = common.bedpost_script_path
hardi_dir = common.hardi_dir
sc_dir = common.sc_dir
sc_script_dir = common.sc_script_dir
subject_list = common.subject_list
variables = common.variables
get_setup_data = common.get_setup_data

In [ ]:
from string import Template

probtrack_setup = Template("""#!/bin/bash
# Generated at ${sub_date}

${sub_variables}
cd $bdir
mkdir -p anat results cluster pic

if [ ! -f $str ]; then
echo Making freesurfer image...
mri_convert $str_mgz $str > /dev/null
# fslswapdim $str RL PA IS $str
fi

if [ ! -f $fs ]; then
echo Making structural image...
mri_convert $fs_mgz $fs > /dev/null
#fslswapdim $fs RL PA IS $fs
fi

if [ ! -f $ventricles ]; then
echo Making ventricles...
mri_binarize --i $aparcaseg_mgz --ventricles --o $ventricles > /dev/null
# fslswapdim $ventricles RL PA IS $ventricles
fi

if [ ! -f $lh_wm ]; then
echo Making lh white matter...
mri_binarize --i $aparcaseg_mgz --match 2 --o $lh_wm > /dev/null
# fslswapdim $lh_wm RL PA IS $lh_wm
fi

if [ ! -f $rh_wm ]; then
echo Making rh white matter...
mri_binarize --i $aparcaseg_mgz --match 41 --o $rh_wm > /dev/null
# fslswapdim $rh_wm RL PA IS $rh_wm
fi

# Put binarized wm images into txt file
ls -1 anat/wm* > $waypoints

# Move FS
echo "Moving FS data..."
rsync $recon_dir/label/*.label label/
rsync $recon_dir/surf/{l,r}h.white surf/

# dtifit for FA
if [ ! -f dtifit/dtifit_FA.nii.gz ]; then
mkdir -p dtifit
echo Running dtifit...
dtifit \
    -k M1/data \
    -o dtifit/dtifit \
    -m M1/nodif_brain_mask \
    -r M1/bvecs \
    -b M1/bvals
fi

# Skullstrip for registration
if [ ! -f $str_bet ]; then
echo Skullstriping...
bet2 $str $str_bet -f .3
fi

# Registration
# register structural to Fs
echo structural to fs
tkregister2 --mov $fs --targ $str --regheader --reg /tmp/junk --fslregout $fs2str --noedit > /dev/null
# invert to create fs2str
echo fs to structural
convert_xfm -omat $str2fs -inverse $fs2str

# Register FA to structural
echo fa to structural
flirt -in $fa -ref $str_bet -omat $fa2str -dof 6
# invert to create str2fa
echo structural to fa
convert_xfm -omat $str2fa -inverse $fa2str
# verify str registration to FA
flirt -in $str -ref bedpostx/nodif_brain -out anat/str_dif -applyxfm -init $str2fa
echo verify pic/str_dif.png
slices bedpostx/nodif_brain anat/str_dif -o pic/str_dif.png

# Concatenate and inverse
echo fa to str to fs
convert_xfm -omat $fa2fs -concat $str2fs $fa2str
echo fs to fa
convert_xfm -omat $fs2fa -inverse $fa2fs

# Verify
# fs looks good in dti space
flirt -in $fs -ref bedpostx/nodif_brain -out anat/fs_dif -applyxfm -init $fs2fa
echo verify pic/fs_dif.png
slices bedpostx/nodif_brain anat/fs_dif -o pic/fs_dif.png

# Make surfaces
if [ -f $seed_list ]
then
    rm $seed_list
fi

for hemi in lh rh
do
    for lab in `cat ${sub_bdir}/scripts/label_order.txt`
    do
        label=label/$hemi.$lab.label
        vol=${label/%.label/.nii.gz}
        echo converting $label to $vol
        if [ ! -f $vol ]
        then
            mri_label2vol --label $label --temp $fs --identity --o $vol --fillthresh 0.5 > /dev/null
        fi
        echo $vol >> $seed_list
    done
done
""")

def setup_script_path(subject):
    return os.path.join(sc_script_dir(subject), 'setup.sh')

def order_path(subject):
    return os.path.join(sc_script_dir(subject), 'label_order.txt')


def prepare_for_probtrack(subject):
    setup_data = get_setup_data(subject)
    setup_data['sub_variables'] = variables.safe_substitute(**setup_data)
    with open(order_path(subject), 'w') as f:
        f.write('\n'.join(better_ordering()))
    setup_path = setup_script_path(subject)
    with open(setup_path, 'w') as f:
        f.write(probtrack_setup.safe_substitute(**setup_data))
    os.chmod(setup_path, S_IRWXU)
    return setup_path

Execute to write setup scripts


In [ ]:
scripts = []
for subject in subject_list:
    setup_path = prepare_for_probtrack(subject)
    print("Wrote setup to {}".format(setup_path))
    scripts.append(setup_path)

Execute to run setup scripts in parallel


In [ ]:
from IPython.parallel import Client
c = Client(profile='default')
lview = c.load_balanced_view()

def execute(script):
    from subprocess import check_output
    check_output(script)
    
results = lview.map_async(execute, scripts)

Execute to view registrations


In [ ]:
for subject in subject_list:
    template = '/fs0/New_Server/RCV/MR_Processed/{}/StructConn/pic/*_dif.png'.format(subject)
    images = glob(template)
    for im in images:
        print(im)
        display(Image(filename=im))