In [9]:
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
# scratch_dir = common.scratch_dir
# seed_file_path = common.seed_file_path

from datetime import datetime
import os


importing IPython notebook from common.ipynb

In [12]:
def pbs_path(seed, subject, fs='fs0'):
    return os.path.join(common.sc_dir(subject, fs), 'cluster', '{}.pbs'.format(seed_n(seed))) 

def is_finished(seed, basedir):
    no_label = seed.replace('label/', '')
    result_matrix = os.path.join(basedir, 'results', '{}.probtrackx2'.format(no_label), 'matrix_seeds_to_all_targets.nii.gz')
    return os.path.isfile(result_matrix)

probtrack_pbs = """#!/bin/bash

#PBS -M scott.s.burns@vanderbilt.edu
#PBS -m a
#PBS -W group_list=h_vuiis
#PBS -l nodes=1:ppn=1
#PBS -l mem=14gb
#PBS -l walltime=00:96:00:00
#PBS -o {sub_scratch}/{out}
#PBS -e {sub_scratch}/{err}


cd {sub_scratch}
{sub_variables}
probtrackx2 -x {seed} \\
    -s bedpostx/merged \\
    -m bedpostx/nodif_brain_mask \\
    -l \\
    --usef \\
    --s2tastext \\
    --os2t \\
    --onewaycondition \\
    -c 0.2 \\
    -S 2000 \\
    --steplength=0.5 \\
    -P 5000 \\
    --fibthresh=0.01 \\
    --distthresh=0.0 \\
    --sampvox=0.0 \\
    --xfm=$fs2fa \\
    --meshspace=freesurfer \\
    --avoid=$ventricles \\
    --seedref=$fs \\
    --forcedir \\
    --opd \\
    -V 1 \\
    --omatrix1 \\
    --dir={dir} \\
    --waypoints=$waypoints \\
    --waycond='OR' \\
    --targetmasks=$seed_list \\

rm -rf {dir}*.asc
rsync -a {dir} cutting.vampire:{sub_fs0}/{dir}
"""


def seed_n(seed):
    return seed.replace('label/', '')

def pbs_text(seed, subject):
    seed_name = seed_n(seed)
    pbs_info = {'sub_variables': common.variables.safe_substitute(**common.get_setup_data(subject)),
                'seed': seed,
                'seed_name': seed_name,
                'out': 'cluster/{}.out'.format(seed_name),
                'err': 'cluster/{}.err'.format(seed_name),
                'dir': 'results/{}.probtrackx2/'.format(seed_name),
                'sub_scratch': common.scratch_dir(subject),
                'sub_fs0': common.sc_dir(subject)}
    return probtrack_pbs.format(**pbs_info)

def pbs_path(seed, subject, fs='fs0'):
    return os.path.join(common.sc_dir(subject, fs), 'cluster', '{}.pbs'.format(seed_n(seed))) 

def qsub_text(pbs_script, name=None):
    parts = ['qsub', '-S /bin/bash',]
    if name:
        parts.append('-N {name}')
    parts.append('{pbs_script}')
    return ' '.join(parts).format(**locals())

assert qsub_text('foo.pbs', name='foo') == 'qsub -S /bin/bash -N foo foo.pbs'

Execute to write out cluster pbs scripts


In [13]:
qsubs = []
for subject in common.subject_list:
    scratch_path = common.scratch_dir(subject)
    fs0 = common.sc_dir(subject)
    !rsync -a $fs0/ $scratch_path/ --exclude 'results'
    with open(common.seed_file_path(subject)) as f:
        seeds = [s for s in f.read().split('\n') if s]
    seeds_for_subject = 0
    for i, seed in enumerate(seeds):
        if not is_finished(seed, fs0):
            seeds_for_subject += 1
            seed_name = seed_n(seed)
            text = pbs_text(seed, subject)
            pbs = pbs_path(seed, subject, fs='scratch/burnsss1')
            job_name = '{}.{}'.format(subject.split('_')[0], i)
            with open(pbs, 'w') as f:
                f.write(text)
            qsubs.append(qsub_text(pbs, job_name))
    print("{:d} seeds left for {}".format(seeds_for_subject, subject))
    res_dir = os.path.join(scratch_path, 'results')
    if not os.path.isdir(res_dir):
        os.makedirs(res_dir)

submit_all = datetime.now().strftime('/home/burnsss1/submit_all-%Y-%m-%d.sh')
with open(submit_all, 'w') as f:
    for i, qsub in enumerate(qsubs):
        f.write(qsub + '\n')
        if (i % 4) == 0:
            f.write('sleep 2\n')
print("Wrote {}".format(submit_all))


3 seeds left for 061_206924
0 seeds left for 063_207046
0 seeds left for 064_207264
4 seeds left for 067_207215
6 seeds left for 072_207335
8 seeds left for 130_208994
6 seeds left for 131_209154
6 seeds left for 140_209143
13 seeds left for 141_209157
24 seeds left for 144_209407
16 seeds left for 146_209355
32 seeds left for 147_209378
35 seeds left for 148_209625
51 seeds left for 162_210032
61 seeds left for 170_210044
77 seeds left for 172_209736
102 seeds left for 188_210443
129 seeds left for 191_210512
125 seeds left for 196_210780
12 seeds left for 197_210808
23 seeds left for 199_210894
26 seeds left for 203_211015
17 seeds left for 208_211122
10 seeds left for 216_211291
13 seeds left for 228_211662
Wrote /home/burnsss1/submit_all-2014-05-12.sh