def process_run(rundir, subid, logger): """ 1. Find raw 2. Split func into xfm dir 3. Align func, create mean and 4d 4. Bias correct anat 5. Invxfm anat2func 6. Generate movement files 7. Clean up """ _, run = os.path.split(rundir) rawdir = os.path.join(rundir, 'raw') exists, mvdir, xfmdir = check_make_dirs(rundir, logger) if exists: return anat, aparc, func = find_raw_files(rawdir, subid, logger) if anat is None: return funcs = split_file(func, xfmdir, subid, run, logger) if funcs is None: return movement, movedfuncs = align_funcs(funcs, logger) exists, refunc = join_files(movedfuncs, rundir, subid, run) if not exists: return #Write movement and displacement logger.info('Write movement') save_movement(movement, mvdir) #Make mean from aligned meanfunc = reg.make_mean(movedfuncs) #Bias correct anat b4anat = reg.n4_biascorrect(anat) if b4anat is None: logger.error('{0}: Bias correction failed'.format(anat)) return #Bias correct mean b4meanfunc = reg.n4_biascorrect(meanfunc) if b4meanfunc is None: logger.error('{0}: Bias correction failed'.format(meanfunc)) return #Reslice mean to anat space rb4meanfunc = reslice_mean(b4anat, b4meanfunc) #Make mask by growing func (5x5x5 kernel) and mask with anat mask = reg.grow_mask(rb4meanfunc, size= 3) #Find mapping from func to anat func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= mask) #Apply inverse mapping to anat anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = False) #Apply transform to aparc nearest neighbor aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = True) #Move for item in (anat2func, aparc2func): cmd = 'mv %s %s'%(item, rundir) os.system(cmd)
def align_means(funcs, meanfunc, run3meanfunc, affines, logger): assert len(funcs) - 1 == len(affines) allmoved = [] func_target = run3meanfunc meanaffine = reg.affine_register_cc(func_target, meanfunc) moved = reg.apply_transform(funcs[0], meanaffine, target=func_target) allmoved.append(moved) for movin, affine in zip(funcs[1:], affines): moved = reg.apply_transform(movin, "{0} {1}".format(affine, meanaffine), target=func_target) allmoved.append(moved) return allmoved
def process_run3(rundir, subid, datadir, logger): """ 1. Find raw 2. Split func into xfm dir 3. Center func 4. Align func, create mean and 4d 5. Bias correct anat 6. Invxfm anat2func 7. Generate movement files 8. Clean up """ _, run = os.path.split(rundir) rawdir = os.path.join(rundir, 'raw') exists, mvdir, xfmdir = check_make_dirs(rundir, logger) if exists: return anat, aparc, func = find_raw_files(rawdir, subid, logger) if anat is None: return funcs = split_file(func, xfmdir, subid, run, logger) if funcs is None: return cfuncs = suzanne_center(funcs) movement, movedfuncs = align_funcs(cfuncs, logger) exists, refunc = join_files(movedfuncs, rundir, subid, run) if not exists: return logger.info('Write movement') save_movement(movement, mvdir) meanfunc = reg.make_mean(movedfuncs) threshold_mean(meanfunc, logger, thresh=275) b4anat = bias_correct(anat, logger, xfmdir) b4meanfunc = bias_correct(meanfunc, logger, xfmdir) func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None) anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), target=b4meanfunc, use_nn = False) aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), target=b4meanfunc, use_nn = True) pth, aparcnme = os.path.split(aparc2func) aparc_mask = os.path.join(datadir, 'mask_{0}'.format(aparcnme)) mask_from_aparc(aparc2func, aparc_mask) for item in (anat2func, aparc2func): cmd = 'mv %s %s'%(item, datadir) os.system(cmd) return b4meanfunc
def align_funcs(funcs, logger): allmove = [] allmoved = [funcs[0]] func_target = funcs[0] for movin in funcs[1:]: tmpaff = reg.affine_register_cc(func_target, movin) if tmpaff is None: continue displacement, translate, angles = xfm.params_from_ants(tmpaff) allmove.append([translate.tolist() + angles + [displacement]]) moved = reg.apply_transform(movin, tmpaff, target = func_target) allmoved.append(moved) return allmove, allmoved
affines = sorted(glob(os.path.join(subj, 'B*0Affine.txt'))) # n4brain = sorted(glob(os.path.join(subj, 'n4*brainmask.nii.gz')))[0] # n4func = sorted(glob(os.path.join(subj, 'n4_mean*.nii.gz')))[0] # func2brainaffine = reg.affine_register_mi(n4brain, n4func, mask = None) # func2brainaffine = sorted(glob(os.path.join(subj, 'n4*to*n4*')))[0] # warpdir = os.path.join(subjdir, 'warp') # warp = os.path.join(warpdir, 'n4_%s_brainmask_to_MNI_template_brainmaskWarp.nii.gz'%subjnme) # warpaffine =os.path.join(warpdir, 'n4_%s_brainmask_to_MNI_template_brainmaskAffine.txt'%subjnme) # template = os.path.join(warpdir, 'MNI_template_brainmask.nii.gz') warped_funcs = [] funcfirst = funcs.pop(0) # first_transform = '{} {} {}'.format(warp, warpaffine, func2brainaffine) # ffunc2mni = reg.apply_transform(funcfirst, first_transform, target = template, use_nn = False) # ffunc2mni = reg.apply_transform(funcfirst, func2brainaffine, target = n4brain, use_nn = False) warped_funcs.append(funcfirst) for val, func in enumerate(funcs): # full_transform = '{} {} {} {}'.format(warp, warpaffine, func2brainaffine, affines[val]) # func2mni = reg.apply_transform(func, full_transform, target = template, use_nn = False) # full_transform = '{} {}'.format(func2brainaffine, affines[val]) func2mni = reg.apply_transform(func, affines[val], target = funcfirst, use_nn = False) warped_funcs.append(func2mni) exists, func4d = join_files(warped_funcs, rundir, subjnme, runnme) if exists: for f in warped_funcs: os.remove(f)
import os, sys from glob import glob sys.path.insert(0, '/home/jagust/fmri-pstask/scripts/ANTS/ants_tools') import register as reg basedir = '/home/jagust/fmri-pstask/subjects' taudirs = sorted(glob(os.path.join(basedir, 'B*/tau'))) for tau in taudirs: mri = os.path.join(tau, 'masknu.nii') scan = os.path.join(tau, 'suvr_80to100ish_normcereg.nii') mri3t = sorted(glob(os.path.join(tau, '*brainmask.nii.gz')))[0] mri2mri3t_xfm = reg.affine_register_cc(mri3t, mri) tau2mri3t = reg.apply_transform(scan, mri2mri3t_xfm, target=mri3t, use_nn=False) mri2mri3t = reg.apply_transform(mri, mri2mri3t_xfm, target=mri3t, use_nn=False)
maskimg = ni.Nifti1Image(mask, img.get_affine()) maskimg.to_filename(outfile) if __name__ == '__main__': try: taskid = os.environ['SGE_TASK_ID'] except: raise IOError('Are you using SGE?') basedir = '/home/jagust/fmri-pstask/subjects' subjects = sorted(glob(os.path.join(basedir, 'NotBackedUp/B*/warp'))) ysubjects = sorted(glob(os.path.join(basedir, 'young/NotBackedUp/B*/warp'))) for y in ysubjects: subjects.append(y) subj = subjects[int(taskid)-1] pth, _ = os.path.split(subj) _, nme = os.path.split(pth) template = os.path.join(subj, 'MNI_template_brainmask.nii.gz') brainmask = os.path.join(subj, 'n4_%s_brainmask.nii.gz'%nme) aparc = os.path.join(subj, '%s_aparcaseg.nii.gz'%nme) affine, warp = reg.warp_cc(template, brainmask) full_transform = '{} {}'.format(warp, affine) warpbrainmask = reg.apply_transform(brainmask, full_transform, target = template, use_nn = False) warpaparc = reg.apply_transform(aparc, full_transform, target = template, use_nn = True) aparc_mask = os.path.join(subj, 'maskMNI_%s_aparcaseg.nii.gz'%nme) mask_from_aparc(warpaparc, aparc_mask)
for subj in subjects: jnk, subid = os.path.split(subj) roidir = os.path.join(subj, 'rois') func = sorted(glob(os.path.join(subj, '*run3', 'xfm', 'n4_mean*nii.gz')))[0] link_func = sym_link(func, roidir, link = False) if os.path.isdir(roidir) and not os.path.isfile(link_func): anat = sorted(glob(os.path.join(subj, '*run3', 'xfm', 'n4*brainmask.nii.gz')))[0] affine = sorted(glob(os.path.join(subj, '*run3', 'xfm', 'n4*n4*txt')))[0] link_func = sym_link(func, roidir, link = True) link_anat = sym_link(anat, roidir, link = True) link_affine = sym_link(affine, roidir, link = True) ranat = os.path.join(roidir, 'r0p5%s_brainmask.nii.gz'%(subid)) rois = sorted(glob(os.path.join(roidir, '*noEC*')))[0] raffine = reg.affine_register_cc(link_anat, ranat) xfm_ranat = reg.apply_transform(ranat, raffine, target = link_anat, use_nn = False) xfm_rois = reg.apply_transform(rois, raffine, target = link_anat, use_nn = True) final_ranat = reg.apply_transform(xfm_ranat, '-i %s'%(link_affine), target = link_func, use_nn = False) final_rois = reg.apply_transform(xfm_rois, '-i %s'%(link_affine), target = link_func, use_nn = True) os.remove(xfm_ranat) os.remove(xfm_rois) else: continue
if os.path.isdir(roidir) and not os.path.isdir(repdir): os.mkdir(repdir) sim1 = os.path.join(repdir, 'similar1_repsup.nii.gz') sim2 = os.path.join(repdir, 'similar2_repsup.nii.gz') zstats = sorted(glob(os.path.join(subj, '*rep*gfeat/cope*/stats/zstat*'))) os.system('ln -s %s %s'%(zstats[3], sim1)) os.system('ln -s %s %s'%(zstats[4], sim2)) petaffine = sorted(glob(os.path.join(roidir, 'n4*Affine.txt')))[0] pettarget = sorted(glob(os.path.join(roidir, 'n4*brainmask.nii.gz')))[0] mriaffine = sorted(glob(os.path.join(roidir, 'r*brainmaskAffine.txt')))[0] mritarget = sorted(glob(os.path.join(roidir, 'r*brainmask.nii.gz')))[0] xfmsim1 = reg.apply_transform(sim1, petaffine, target = pettarget, use_nn = False) xfmsim2 = reg.apply_transform(sim2, petaffine, target = pettarget, use_nn = False) invxfmsim1 = reg.apply_transform(xfmsim1, '-i %s'%(mriaffine), target = mritarget, use_nn = False) invxfmsim2 = reg.apply_transform(xfmsim2, '-i %s'%(mriaffine), target = mritarget, use_nn = False) os.remove(xfmsim1) os.remove(xfmsim2) rois = sorted(glob(os.path.join(roidir, 'subfields', 'Left*'))) rrois = sorted(glob(os.path.join(roidir, 'subfields', 'Right*'))) for r in rrois: rois.append(r) outfiles = sorted(glob(os.path.join(repdir, 'inv*'))) for outname in outfiles:
cmd = 'ln -s {} {}'.format(target, subdir) os.system(cmd) target = os.path.join(subdir, targetnme) _, labelnme = os.path.split(target_labels) cmd = 'ln -s {} {}'.format(target_labels, subdir) os.system(cmd) target_labels = os.path.join(subdir, labelnme) moving, moving_aparc = getdata(subdir) moving_labels = aparc_to_mask(moving_aparc, labels) #Subject ROIs - Manually drawn #Register subject to template affine = register.affine_register_mi(target, moving) #Apply to structural and labels xfm_moving = register.apply_transform(moving, affine, outfile = None, target = target, use_nn = False) xfm_moving_labels = register.apply_transform(moving_labels, affine, outfile = None, target = target, use_nn = True) psepct = [0.5, .75, 1.0] for pct in psepct: outname = os.path.join(subdir, 'PSEtest_{}'.format(pct)) cmd = psewarp(target, xfm_moving, target_labels, xfm_moving_labels, pct, outname) print cmd os.system(cmd) pseaffine = outname + 'Affine.txt' psewarp = outname + 'Warp.nii.gz' full_transform = ' '.join(psewarp, pseaffine, affine) final_brainmask = xfm_moving.replace('xfm_', 'finalxfm_{}_'.format(pct))
def process_runs(rundir, subid, run3meanfunc, logger): _, run = os.path.split(rundir) rawdir = os.path.join(rundir, 'raw') exists, mvdir, xfmdir = check_make_dirs(rundir, logger) if exists: return _, run3 = os.path.split(run3meanfunc) cmd = 'cp %s %s'%(run3meanfunc, xfmdir) os.system(cmd) run3meanfunc = os.path.join(xfmdir, run3) anat, aparc, func = find_raw_files(rawdir, subid, logger) if anat is None: return funcs = split_file(func, xfmdir, subid, run, logger) if funcs is None: return movement, affines, moved = make_affines(funcs, logger) logger.info('Write movement') save_movement(movement, mvdir) meanfunc = reg.make_mean(moved) #Remove old movedfuncs newdir = os.path.join(xfmdir, 'first_realign') os.mkdir(newdir) for f in moved[1:]: cmd = 'mv %s %s'%(f, newdir) os.system(cmd) #Bias correct anat b4anat = reg.n4_biascorrect(anat) if b4anat is None: logger.error('{0}: Bias correction failed'.format(anat)) return _, b4anatname = os.path.split(b4anat) cmd = 'mv %s %s'%(b4anat, xfmdir) os.system(cmd) b4anat = os.path.join(xfmdir, b4anatname) # threshold mean to remove "cruft" current value == 300 threshold_mean(meanfunc, logger, thresh=300) #Bias correct mean #b4meanfunc = reg.n4_biascorrect(meanfunc) #if b4meanfunc is None: # logger.error('{0}: Bias correction failed'.format(meanfunc)) # return b4meanfunc = meanfunc #Reslice mean to anat space rb4meanfunc = reslice_mean(b4anat, b4meanfunc) #Make mask by growing func (5x5x5 kernel) and mask with anat # mask = reg.grow_mask(rb4meanfunc, size= 3) #Find mapping from func to anat func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None) #Apply inverse mapping to anat anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = False) #Apply transform to aparc nearest neighbor aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = True) #Skull strip shit pth, aparcnme = os.path.split(aparc2func) aparc_mask = os.path.join(rundir, 'mask_{0}'.format(aparcnme)) mask_from_aparc(aparc2func, aparc_mask) #masked_b4meanfunc = b4meanfunc.replace('n4_', 'masked_n4_') #mask_img(b4meanfunc, aparc_mask, masked_b4meanfunc) #Make new moved funcs based on 2 affines movedfuncs = align_means(funcs, b4meanfunc, run3meanfunc, affines, logger) exists, refunc = join_files(movedfuncs, rundir, subid, run) if not exists: return #Move for item in (anat2func, aparc2func): cmd = 'mv %s %s'%(item, rundir) os.system(cmd)
import os, sys from glob import glob sys.path.insert(0, '/home/jagust/fmri-pstask/scripts/ANTS/ants_tools') import register as reg basedir = '/home/jagust/fmri-pstask/subjects' fdgdirs = sorted(glob(os.path.join(basedir, 'B*/fdg'))) for fdg in fdgdirs: testxfm = sorted(glob(os.path.join(fdg, 'xfm*'))) if not testxfm: scans = sorted(glob(os.path.join(fdg, '*nii*'))) fdg2anat_xfm = reg.affine_register_mi(scans[0], scans[1], mask=None) #0=Brainmask, 1=FDG fdg2anat = reg.apply_transform(scans[1], fdg2anat_xfm, target=scans[0], use_nn=False)
#basedir = '/home/jagust/fmri-pstask/subjects/' target = '/home/jagust/fmri-pstask/subjects/test_labelalign/template/rois/r0p5-mni_template.nii.gz' brainmask = glob(os.path.join(dir, 'r*brainmask.nii.gz'))[0] affine = glob(os.path.join(dir, 'r*Affine*'))[0] pseaffine = glob(os.path.join(dir, 'PSE-0.75', '*_Affine*'))[0] psewarp = glob(os.path.join(dir, 'PSE-0.75', '*_Warp*'))[0] full_transform = '{} {} {}'.format(psewarp, pseaffine, affine) funcstr = ['cope*', 'zstat*'] subjdir, _ = os.path.split(dir) meanfunc = glob(os.path.join(subjdir, '*run3', 'xfm', 'mean*nii.gz'))[0] _, mfuncnme = os.path.split(meanfunc) if not os.path.isfile(os.path.join(dir, mfuncnme)): cmd = 'ln -s {} {}'.format(meanfunc, dir) os.system(cmd) meanfunc = os.path.join(dir, mfuncnme) mfuncaffine = register.affine_register_mi(brainmask, meanfunc) xfm_mfunc = register.apply_transform(meanfunc, mfuncaffine, outfile = None, target = brainmask, use_nn = False) statdir = glob(os.path.join(subjdir, '*uncorr*', '*feat', 'stats'))[0] funcs = [] for globstr in funcstr: funcs.extend(glob(os.path.join(statdir, globstr))) for func in funcs: xfm_func = register.apply_transform(func, mfuncaffine, outfile = None, target = brainmask, use_nn = False) final_func = xfm_func.replace('xfm_', 'finalxfm_') final_func = register.apply_transform(xfm_func, full_transform, outfile = final_func, target = target, use_nn = False)
""" if __name__ == '__main__': try: taskid = os.environ['SGE_TASK_ID'] except: raise IOError('Are you using SGE??') basedir = '/home/jagust/fmri-pstask/subjects/young/' roidirs = sorted(glob(os.path.join(basedir, 'B*/rois'))) dir = roidirs[int(taskid)-1] psedir = os.path.join(dir, 'PSE-0.75') outdir = os.path.join(dir, 'newmodel/template_space') if os.path.isdir(psedir): funcs = sorted(glob(os.path.join(dir, 'newmodel/*.nii.gz'))) target = '/home/jagust/fmri-pstask/subjects/test_labelalign/template/rois/r0p5-mni_template.nii.gz' inv_affine = glob(os.path.join(dir, 'r0p5*n4*Affine.txt'))[0] coreg_affine = glob(os.path.join(dir, 'n4*n4*Affine.txt'))[0] affine = glob(os.path.join(dir, '*templateAffine.txt'))[0] pseaffine = glob(os.path.join(psedir, '*Affine.txt'))[0] psewarp = glob(os.path.join(psedir, '*_Warp.nii.gz'))[0] full_transform = '{} {} {} -i {} {}'.format(psewarp, pseaffine, affine, inv_affine, coreg_affine) for func in funcs: _, funcnme = os.path.split(func) final_func = os.path.join(outdir, 'warped_%s'%funcnme) final_func = register.apply_transform(func, full_transform, outfile = final_func, target = target, use_nn = False)
def process_run3(rundir, subid, logger): """ 1. Find raw 2. Split func into xfm dir 3. Align func, create mean and 4d 4. Bias correct anat 5. Invxfm anat2func 6. Generate movement files 7. Clean up """ _, run = os.path.split(rundir) rawdir = os.path.join(rundir, 'raw') exists, mvdir, xfmdir = check_make_dirs(rundir, logger) if exists: return anat, aparc, func = find_raw_files(rawdir, subid, logger) if anat is None: return funcs = split_file(func, xfmdir, subid, run, logger) if funcs is None: return movement, movedfuncs = align_funcs(funcs, logger) exists, refunc = join_files(movedfuncs, rundir, subid, run) if not exists: return #Write movement and displacement logger.info('Write movement') save_movement(movement, mvdir) #Make mean from aligned meanfunc = reg.make_mean(movedfuncs) #Bias correct anat b4anat = reg.n4_biascorrect(anat) if b4anat is None: logger.error('{0}: Bias correction failed'.format(anat)) return _, b4anatname = os.path.split(b4anat) cmd = 'mv %s %s'%(b4anat, xfmdir) os.system(cmd) b4anat = os.path.join(xfmdir, b4anatname) # threshold mean to remove "cruft" current value == 300 threshold_mean(meanfunc, logger, thresh=300) #Bias correct mean #b4meanfunc = reg.n4_biascorrect(meanfunc) #if b4meanfunc is None: # logger.error('{0}: Bias correction failed'.format(meanfunc)) # return #Reslice mean to anat space b4meanfunc = meanfunc rb4meanfunc = reslice_mean(b4anat, b4meanfunc) #Make mask by growing func (5x5x5 kernel) and mask with anat # mask = reg.grow_mask(rb4meanfunc, size= 3) #Find mapping from func to anat func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None) #Apply inverse mapping to anat anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = False) #Apply transform to aparc nearest neighbor aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), target=meanfunc, use_nn = True) #Skull strip shit pth, aparcnme = os.path.split(aparc2func) aparc_mask = os.path.join(datadir, 'mask_{0}'.format(aparcnme)) mask_from_aparc(aparc2func, aparc_mask) #masked_b4meanfunc = b4meanfunc.replace('n4_', 'masked_n4_') #mask_img(b4meanfunc, aparc_mask, masked_b4meanfunc) #Move for item in (anat2func, aparc2func): cmd = 'mv %s %s'%(item, rundir) os.system(cmd) return b4meanfunc
# meanvalue = str(mean.outputs.out_stat) # fid1.write('%s\t%s\t%s\t%s\n'%(subjnme, xfmnme, roi, meanvalue)) # os.remove(mout) if os.path.isdir(newdir): # if not os.path.isdir(newdir): # os.mkdir(newdir) zstats = sorted(glob(os.path.join(nbudir, subjnme, '%s_newmodel_p05.gfeat/cope1.feat/stats/zstat*'%subjnme))) os.system('ln -s %s %s/New_norepeat.nii.gz'%(zstats[0], newdir)) # os.system('ln -s %s %s/sub_Hits.nii.gz'%(zstats[5], newdir)) # os.system('ln -s %s %s/sub_FA.nii.gz'%(zstats[6], newdir)) # os.system('ln -s %s %s/Hits.nii.gz'%(zstats[7], newdir)) # os.system('ln -s %s %s/FA.nii.gz'%(zstats[8], newdir)) zstats = sorted(glob(os.path.join(newdir, 'New*nii.gz'))) for z in zstats: xfm = reg.apply_transform(z, full_transform, target=rbrain) _, xfmnme = os.path.split(xfm) xfmnme.replace('.nii.gz', '') for sub in subs: _, roi = os.path.split(sub) roi.replace('.nii.gz', '') mname = '{}_{}'.format(xfmnme, roi) mout = os.path.join(newdir, mname) mask = fsl.ApplyMask(in_file=xfm, mask_file=sub, out_file=mout) mask.run() mean = fsl.ImageStats(in_file=mout, op_string='-M') mean = mean.run() meanvalue = str(mean.outputs.out_stat) fid1.write('%s\t%s\t%s\t%s\n'%(subjnme, xfmnme, roi, meanvalue)) os.remove(mout)
# try: # taskid = os.environ['SGE_TASK_ID'] # except: # raise IOError('Are you using SGE??') # subj = dirs[int(taskid) - 1] for subj in subjs: jnk, subid = os.path.split(subj) raw_dir = os.path.join(subj, 'run3/raw') xfm_dir = os.path.join(subj, 'run3/xfm') anat = os.path.join(raw_dir, '%s_brainmask.nii.gz'%(subid)) aparc = os.path.join(raw_dir, '%s_aparcaseg.nii.gz'%(subid)) b4meanfunc = glob(os.path.join(xfm_dir, 'n4_mean*'))[0] b4anat = bias_correct(anat, xfm_dir) func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None) anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), target=b4meanfunc, use_nn = False) aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), target=b4meanfunc, use_nn = True) pth, aparcnme = os.path.split(aparc2func) aparc_mask = os.path.join(subj, 'mask_{0}'.format(aparcnme)) mask_from_aparc(aparc2func, aparc_mask) for item in (anat2func, aparc2func): cmd = 'mv %s %s'%(item, subj) os.system(cmd) # ANTSPATH='/home/jagust/cindeem/bin/ANTs-1.9.v4-Linux/bin' # def affine_register_mi_iter(target, moving): # """ uses ANTS to create an affine mapping between different # modalities of the same subjects anatomy,
roiaffine = sorted(glob(os.path.join(roidir, 'r*_brainmaskAffine.txt')))[0] funcaffine = sorted(glob(os.path.join(roidir, 'n4*n4*_brainmaskAffine.txt')))[0] full_transform = '{} {} {} -i {} {}'.format(psewarp, pseaffine, affine, roiaffine, funcaffine) cdir = os.path.join(roidir, 'copes') zdir = os.path.join(roidir, 'zstats') cfiles = sorted(glob(os.path.join(cdir, '*nii.gz'))) zfiles = sorted(glob(os.path.join(zdir, '*nii.gz'))) for z in zfiles: cfiles.append(z) for c in cfiles: pth, nme = os.path.split(c) outfile = os.path.join(pth, 'template_space', 'PSExfm_'+nme) xfm = reg.apply_transform(c, full_transform, outfile = outfile, target = target, use_nn = False) # subjdir, _ = os.path.split(roidir) # rundirs = sorted(glob(os.path.join(subjdir, 'run*'))) # for run in rundirs: # os.chdir(run) # func4d = sorted(glob(os.path.join(run, 'native-a*')))[0] # os.system('fslsplit %s frame -t'%(func4d)) # funcs = sorted(glob(os.path.join(run, 'frame*'))) # newfuncs = [] # for func in funcs: # xfm = reg.apply_transform(func, full_transform, target = target, use_nn = False) # newfuncs.append(func) # newfuncs.append(xfm) # outfile = func4d.replace('native-', '') # globstr = os.path.join(run, 'inv*')