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 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
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)
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
#'/home/jagust/fmri-pstask/pilot/B13-004/pstask' #allseries = sorted(glob(os.path.join(rootdir, 'series_*'))) allseries = [rootdir,] reg.function_logger(rootdir) logger = logging.getLogger('antsregister.wrapper') for datadir in allseries: logger.info(datadir) globstr = os.path.join(datadir, 'B13-005_sess*.nii.gz') allf = sorted(glob(globstr)) target = allf[0] basedir, _ = os.path.split(target) reg.function_logger(basedir) allmove = [] allmoved = [target] for movin in allf[1:3]: tmpaff = reg.affine_register_cc(target, movin) if tmpaff is None: continue displacement, translate, angles = xfm.params_from_ants(tmpaff) allmove.append([translate.tolist() + angles + [displacement]]) moved = reg.apply_affine(movin, tmpaff) allmoved.append(moved) ## merge aligned into 4D merged_fname = os.path.join(datadir, 'aligned_4d.nii.gz') try: merged_img = ni.concat_images(allmoved) merged_img.to_filename(merged_fname) except: cmd = 'fslmerge -t %s '%(merged_fname) cmd = cmd + ' '.join([x for x in allmoved]) os.system(cmd)