def task_fetch_data(): """Step 00: Download the openfmri ds117 dataset files.""" for subject in subjects: t1_fname = fname.t1(subject=subject) sss_fnames = [fname.sss(subject=subject, run=run) for run in range(1, 7)] flash5_fname = fname.flash5(subject=subject) yield dict( name=subject, file_dep=['00_fetch_data.py'], targets=[t1_fname, flash5_fname] + sss_fnames, actions=['python 00_fetch_data.py %s' % subject], )
def task_anatomy(): """Step 01: Run the FreeSurfer anatomical-MRI segmentation program.""" for subject in subjects: t1_fname = fname.t1(subject=subject) surf_fnames = [fname.surface(subject=subject, surf=surf) for surf in ['inner_skull', 'outer_skull', 'outer_skin']] bem_fname = fname.bem(subject=subject) yield dict( name=subject, task_dep=['fetch_data'], file_dep=[t1_fname, '01_anatomy.py'], targets=surf_fnames + [bem_fname], actions=['python 01_anatomy.py %s' % subject], )
def tee_output(command, log_file): """Write the output of a command to a logfile as well as stdout.""" print('Writing the output of the command below to', log_file) print(' '.join(command)) with open(log_file, 'wb') as fid: proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: fid.write(line) if proc.wait() != 0: raise RuntimeError('Command failed') tee_output(['recon-all', '-all', '-s', subject, '-sd', fname.subjects_dir, '-i', fname.t1(subject=subject)], fname.freesurfer_log(subject=subject)) print('Copying FLASH files') os.makedirs(fname.flash_dir(subject=subject), exist_ok=True) for f_src in glob.glob(fname.flash_glob(subject=subject)): f_dst = op.basename(f_src).replace("meflash_", "mef") f_dst = op.join(fname.flash_dir(subject=subject), f_dst) shutil.copy(f_src, f_dst) # Fix the headers for subject 19 if subject == 'sub019': print('Fixing FLASH files for %s' % (subject,)) flash_files = (['mef05_%d.mgz' % x for x in range(7)] + ['mef30_%d.mgz' % x for x in range(7)])
"""Write the output of a command to a logfile as well as stdout.""" print('Writing the output of the command below to', log_file) print(' '.join(command)) with open(log_file, 'wb') as fid: proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in proc.stdout: fid.write(line) if proc.wait() != 0: raise RuntimeError('Command failed') tee_output([ 'recon-all', '-all', '-s', subject, '-sd', fname.subjects_dir, '-i', fname.t1(subject=subject) ], fname.freesurfer_log(subject=subject)) print('Copying FLASH files') os.makedirs(fname.flash_dir(subject=subject), exist_ok=True) for f_src in glob.glob(fname.flash_glob(subject=subject)): f_dst = op.basename(f_src).replace("meflash_", "mef") f_dst = op.join(fname.flash_dir(subject=subject), f_dst) shutil.copy(f_src, f_dst) # Fix the headers for subject 19 if subject == 'sub019': print('Fixing FLASH files for %s' % (subject, )) flash_files = (['mef05_%d.mgz' % x for x in range(7)] + ['mef30_%d.mgz' % x for x in range(7)])