Bedpost

This notebook builds a bedpost script for subjects.

Functions


In [1]:
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


importing IPython notebook from common.ipynb
importing IPython notebook from common.ipynb

In [2]:
from string import Template

bptemplate = Template("""#!/bin/bash

# Generated automatically at ${sub_date}

bdir=${sub_basedir}
datadir=${sub_datadir}

cd ${datadir}
echo "Setting up bvals/bvecs"
if [ ! \( -L bvals \) ]; then
    ln -s ${sub_bval} bvals
fi
if [ ! \( -L bvecs \) ]; then
    ln -s ${sub_bvec} bvecs
fi
echo

if [ `imtest ${datadir}/data` -eq 0 ]; then
    echo "eddy_correct begin `date`"
    ${FSLDIR}/bin/eddy_correct ${sub_image} data 0
    fdt_rotate_bvecs bvecs rot_bvecs data.ecclog
    mv bvecs old_bvecs
    mv rot_bvecs bvecs
    echo "eddy_correct end `date`"
else
    echo "Skipping eddy_correct"
fi
echo

if [ `${FSLDIR}/bin/imtest ${datadir}/nodif_brain_mask` -eq 0 ]; then
    echo "skull stripping begin `date`"
    ${FSLDIR}/bin/bet2 data nodif_brain -m -f .3
    echo "skull stripping end `date`"
else
    echo "Skipping skull strip"
fi
echo

cd ${bdir}

# Estimation parameters
nfibres=${sub_nfibres}
fudge=${sub_fudge}
burnin=${sub_burnin}
njumps=${sub_njumps}
sampleevery=${sub_sampleevery}

bpxdir=${datadir}.bedpostX

mkdir -p ${bpxdir}
mkdir -p ${bpxdir}/diff_slices
mkdir -p ${bpxdir}/logs
mkdir -p ${bpxdir}/logs/pid_${$}
mkdir -p ${bpxdir}/xfms

echo "bedpostx_preproc begin `date`"
bedpostx_preproc.sh ${datadir} 0
echo "bedpostx_preproc end `date`"
echo

nslices=`${FSLDIR}/bin/fslval ${datadir}/data dim3`
[ -f ${bpxdir}/commands.txt ] && rm ${bpxdir}/commands.txt

slice=0
while [ $slice -lt $nslices ]
do
    slicezp=`$FSLDIR/bin/zeropad $slice 4`
    if [ -f ${bpxdir}/diff_slices/data_slice_$slicezp/dyads2.nii.gz ];then
        echo "slice $slice has already been processed"
    else
        echo "${FSLDIR}/bin/bedpostx_single_slice.sh $datadir $slice --nfibres=$nfibres --fudge=$fudge --burnin=$burnin --njumps=$njumps --sampleevery=$sampleevery --model=1">> ${bpxdir}/commands.txt
    fi
    slice=$(($slice + 1))
done

# parallel processing
echo "parallel processing begin `date`"
${sub_parallel_script} ${bpxdir}/commands.txt --ncpu ${sub_ncpu}
echo "parallel processing end `date`"
echo

# Clean things up
echo "bedpostx_postproc begin `date`"
bedpostx_postproc.sh ${datadir}
echo "bedpostx_postproc end `date`"
echo

mv $bpxdir bedpostx
""")

def get_bedpost_defaults(data='HARDI1.nii', bvec='HARDI1.bvec', bval='HARDI1.bval',
        fudge=1, nfibres=2, burnin=1000, njumps=1250, sampleevery=25,
        par_script='/home/burnsss1/bin/run_parallel.py', ncpu=14):
    return dict(
        sub_bvec=bvec,
        sub_bval=bval,
        sub_image=data,
        sub_fudge=fudge,
        sub_nfibres=nfibres,
        sub_burnin=burnin,
        sub_njumps=njumps,
        sub_sampleevery=sampleevery,
        sub_parallel_script=par_script,
        sub_ncpu=ncpu,)

def prepare_for_bedpost(subject):
    # Make bedpost directory
    scd = sc_dir(subject)
    if not os.path.isdir(scd):
        os.makedirs(scd)
    # cp hardi data
    hardi_data_folder = os.path.join(hardi_dir(subject), 'M1')
    datadir = os.path.join(scd, 'M1')
    if not os.path.isdir(datadir):
        copytree(hardi_data_folder, datadir)
    # make script directory
    script_dir = sc_script_dir(subject)
    if not os.path.isdir(script_dir):
        os.makedirs(script_dir)
    script_path = bedpost_script_path(subject)
    replace = get_bedpost_defaults()
    replace['sub_date'] = datetime.now()
    replace['sub_basedir'] = scd
    replace['sub_datadir'] = datadir
    
    with open(script_path, 'w') as f:
        f.write(bptemplate.safe_substitute(**replace))
    os.chmod(script_path, S_IRWXU)
    return script_path

Execute to write bedpost scripts


In [ ]:
all_scripts = []
for subject in subject_list:
    bedpost_script = prepare_for_bedpost(subject)
    print("Wrote {}".format(bedpost_script))
    all_scripts.append(bedpost_script)
with open('/home/burnsss1/scripts/rcv_bedpost.sh', 'w') as f:
    f.write('\n'.join(all_scripts))
    f.write('\n')