def apply_ants_warp(self, runtime, in_file, out_file, rigid): out_rigid = op.basename(add_suffix(out_file, "anat")) continuous_interp = dict(trilinear="trilin", spline="cubic")[self.interp] interp = "nearest" if "mask" in in_file else continuous_interp cmdline_rigid = ["mri_vol2vol", "--mov", in_file, "--reg", rigid, "--fstarg", "--" + interp, "--o", out_rigid, "--no-save-reg"] runtime = submit_cmdline(runtime, cmdline_rigid) continuous_interp = dict(trilinear="trilin", spline="BSpline")[self.interp] interp = "NN" if "mask" in in_file else continuous_interp cmdline_warp = ["WarpImageMultiTransform", "3", out_rigid, out_file, self.inputs.warpfield, self.inputs.affine, "-R", self.ref_file] if interp != "trilin": cmdline_warp.append("--use-" + interp) runtime = submit_cmdline(runtime, cmdline_warp) return runtime
def apply_fsl_rigid(self, runtime, in_file, out_file, rigid): interp = "nn" if "mask" in in_file else self.interp cmdline = [ "applywarp", "-i", in_file, "-o", out_file, "-r", self.ref_file, "--interp={}".format(interp), "--premat={}".format(rigid) ] runtime = submit_cmdline(runtime, cmdline) return runtime
def apply_fsl_rigid(self, runtime, in_file, out_file, rigid): interp = "nn" if "mask" in in_file else self.interp cmdline = ["applywarp", "-i", in_file, "-o", out_file, "-r", self.ref_file, "--interp={}".format(interp), "--premat={}".format(rigid)] runtime = submit_cmdline(runtime, cmdline) return runtime
def combine_rigids(self, runtime, first_rigid, second_rigid): """Invert the first rigid and combine with the second. This creates a transformation from run n to run 1 space, through the anatomical coregistration. """ first_inv = op.basename(add_suffix(first_rigid, "inv")) cmdline_inv = [ "convert_xfm", "-omat", first_inv, "-inverse", first_rigid ] runtime = submit_cmdline(runtime, cmdline_inv) full_rigid = op.basename(add_suffix(second_rigid, "to_epi")) cmdline_full = [ "convert_xfm", "-omat", full_rigid, "-concat", first_inv, second_rigid ] runtime = submit_cmdline(runtime, cmdline_full) return runtime, full_rigid
def combine_rigids(self, runtime, first_rigid, second_rigid): """Invert the first rigid and combine with the second. This creates a transformation from run n to run 1 space, through the anatomical coregistration. """ first_inv = op.basename(add_suffix(first_rigid, "inv")) cmdline_inv = ["convert_xfm", "-omat", first_inv, "-inverse", first_rigid] runtime = submit_cmdline(runtime, cmdline_inv) full_rigid = op.basename(add_suffix(second_rigid, "to_epi")) cmdline_full = ["convert_xfm", "-omat", full_rigid, "-concat", first_inv, second_rigid] runtime = submit_cmdline(runtime, cmdline_full) return runtime, full_rigid
def apply_ants_warp(self, runtime, in_file, out_file, rigid): out_rigid = op.basename(add_suffix(out_file, "anat")) continuous_interp = dict(trilinear="trilin", spline="cubic")[self.interp] interp = "nearest" if "mask" in in_file else continuous_interp cmdline_rigid = [ "mri_vol2vol", "--mov", in_file, "--reg", rigid, "--fstarg", "--" + interp, "--o", out_rigid, "--no-save-reg" ] runtime = submit_cmdline(runtime, cmdline_rigid) continuous_interp = dict(trilinear="trilin", spline="BSpline")[self.interp] interp = "NN" if "mask" in in_file else continuous_interp cmdline_warp = [ "WarpImageMultiTransform", "3", out_rigid, out_file, self.inputs.warpfield, self.inputs.affine, "-R", self.ref_file ] if interp != "trilin": cmdline_warp.append("--use-" + interp) runtime = submit_cmdline(runtime, cmdline_warp) return runtime
def _run_interface(self, runtime): target_brain = fsl.Info.standard_image("avg152T1_brain.nii.gz") cmdline = " ".join([ "antsIntroduction.sh", "-d", "3", "-r", target_brain, "-i", self.inputs.in_file, "-o", "ants_" ]) runtime.environ["ITK_NUM_THREADS"] = "1" runtime = submit_cmdline(runtime, cmdline) os.rename("ants_Affine.txt", "affine.txt") os.rename("ants_Warp.nii.gz", "warpfield.nii.gz") os.rename("ants_deformed.nii.gz", "brain_warp.nii.gz") os.rename("ants_InverseWarp.nii.gz", "inverse_warpfield.nii.gz") return runtime
def _run_interface(self, runtime): target_brain = fsl.Info.standard_image("avg152T1_brain.nii.gz") cmdline = " ".join(["antsIntroduction.sh", "-d", "3", "-r", target_brain, "-i", self.inputs.in_file, "-o", "ants_"]) runtime.environ["ITK_NUM_THREADS"] = "1" runtime = submit_cmdline(runtime, cmdline) os.rename("ants_Affine.txt", "affine.txt") os.rename("ants_Warp.nii.gz", "warpfield.nii.gz") os.rename("ants_deformed.nii.gz", "brain_warp.nii.gz") os.rename("ants_InverseWarp.nii.gz", "inverse_warpfield.nii.gz") return runtime
def _run_interface(self, runtime): n_con = len(self.inputs.contrasts) # Find the basic geometry of the image img = nib.load(self.inputs.copes[0]) x, y, z = img.shape aff, hdr = img.get_affine(), img.get_header() # Get lists of files for each contrast copes = self._unpack_files(self.inputs.copes, n_con) varcopes = self._unpack_files(self.inputs.varcopes, n_con) # Make an image with the DOF for each run dofs = np.array([np.loadtxt(f) for f in self.inputs.dofs]) dof_data = np.ones((x, y, z, len(dofs))) * dofs # Find the intersection of the masks mask_data = [nib.load(f).get_data() for f in self.inputs.masks] common_mask = np.all(mask_data, axis=0) nib.Nifti1Image(common_mask, aff, hdr).to_filename("mask.nii.gz") # Run the flame models flame_results = [] zstat_files = [] for i, contrast in enumerate(self.inputs.contrasts): # Load each run of cope and varcope files into a list cs = [nib.load(f).get_data()[..., np.newaxis] for f in copes[i]] vs = [nib.load(f).get_data()[..., np.newaxis] for f in varcopes[i]] # Find all of the nonzero copes # This handles cases where there were no events for some of # the runs for the contrast we're currently dealing with good_cs = [not np.allclose(d, 0) for d in cs] good_vs = [not np.allclose(d, 0) for d in vs] good = np.all([good_cs, good_vs], axis=0) # Handle the case where no events occured for this contrast if not good.any(): good = np.ones(len(cs), bool) # Concatenate the cope and varcope data, save only the good frames c_data = np.concatenate(cs, axis=-1)[:, :, :, good] v_data = np.concatenate(vs, axis=-1)[:, :, :, good] # Write out the concatenated copes and varcopes nib.Nifti1Image(c_data, aff, hdr).to_filename("cope_4d.nii.gz") nib.Nifti1Image(v_data, aff, hdr).to_filename("varcope_4d.nii.gz") # Write out a correctly sized design for this contrast fsl.L2Model(num_copes=int(good.sum())).run() # Mask the DOF data and write it out for this run contrast_dof = dof_data[:, :, :, good] nib.Nifti1Image(contrast_dof, aff, hdr).to_filename("dof.nii.gz") # Build the flamo commandline and run flamecmd = ["flameo", "--cope=cope_4d.nii.gz", "--varcope=varcope_4d.nii.gz", "--mask=mask.nii.gz", "--dvc=dof.nii.gz", "--runmode=fe", "--dm=design.mat", "--tc=design.con", "--cs=design.grp", "--ld=" + contrast, "--npo"] runtime = submit_cmdline(runtime, flamecmd) # Rename the written file and append to the outputs for kind in ["cope", "varcope"]: os.rename(kind + "_4d.nii.gz", "%s/%s_4d.nii.gz" % (contrast, kind)) # Put the zstats and mask on the surface for hemi in ["lh", "rh"]: projcmd = ["mri_vol2surf", "--mov", "%s/zstat1.nii.gz" % contrast, "--reg", self.inputs.reg_file, "--hemi", hemi, "--projfrac-avg", "0", "1", ".1", "--o", "%s/%s.zstat1.mgz" % (contrast, hemi)] submit_cmdline(runtime, projcmd) # Mask image projcmd = ["mri_vol2surf", "--mov", "%s/mask.nii.gz" % contrast, "--reg", self.inputs.reg_file, "--hemi", hemi, "--projfrac-max", "0", "1", ".1", "--o", "%s/%s.mask.mgz" % (contrast, hemi)] submit_cmdline(runtime, projcmd) flame_results.append(op.abspath(contrast)) zstat_files.append(op.abspath("%s/zstat1.nii.gz" % contrast)) self.flame_results = flame_results self.zstat_files = zstat_files return runtime
def _run_interface(self, runtime): n_con = len(self.inputs.contrasts) # Find the basic geometry of the image img = nib.load(self.inputs.copes[0]) x, y, z = img.shape aff, hdr = img.get_affine(), img.get_header() # Get lists of files for each contrast copes = self._unpack_files(self.inputs.copes, n_con) varcopes = self._unpack_files(self.inputs.varcopes, n_con) # Make an image with the DOF for each run dofs = np.array([np.loadtxt(f) for f in self.inputs.dofs]) dof_data = np.ones((x, y, z, len(dofs))) * dofs # Find the intersection of the masks mask_data = [nib.load(f).get_data() for f in self.inputs.masks] common_mask = np.all(mask_data, axis=0) nib.Nifti1Image(common_mask, aff, hdr).to_filename("mask.nii.gz") # Run the flame models flame_results = [] zstat_files = [] for i, contrast in enumerate(self.inputs.contrasts): # Load each run of cope and varcope files into a list cs = [nib.load(f).get_data()[..., np.newaxis] for f in copes[i]] vs = [nib.load(f).get_data()[..., np.newaxis] for f in varcopes[i]] # Find all of the nonzero copes # This handles cases where there were no events for some of # the runs for the contrast we're currently dealing with good_cs = [not np.allclose(d, 0) for d in cs] good_vs = [not np.allclose(d, 0) for d in vs] good = np.all([good_cs, good_vs], axis=0) # Concatenate the cope and varcope data, save only the good frames c_data = np.concatenate(cs, axis=-1)[:, :, :, good] v_data = np.concatenate(vs, axis=-1)[:, :, :, good] # Write out the concatenated copes and varcopes nib.Nifti1Image(c_data, aff, hdr).to_filename("cope_4d.nii.gz") nib.Nifti1Image(v_data, aff, hdr).to_filename("varcope_4d.nii.gz") # Write out a correctly sized design for this contrast fsl.L2Model(num_copes=int(good.sum())).run() # Mask the DOF data and write it out for this run contrast_dof = dof_data[:, :, :, good] nib.Nifti1Image(contrast_dof, aff, hdr).to_filename("dof.nii.gz") # Build the flamo commandline and run flamecmd = ["flameo", "--cope=cope_4d.nii.gz", "--varcope=varcope_4d.nii.gz", "--mask=mask.nii.gz", "--dvc=dof.nii.gz", "--runmode=fe", "--dm=design.mat", "--tc=design.con", "--cs=design.grp", "--ld=" + contrast, "--npo"] runtime = submit_cmdline(runtime, flamecmd) # Rename the written file and append to the outputs for kind in ["cope", "varcope"]: os.rename(kind + "_4d.nii.gz", "%s/%s_4d.nii.gz" % (contrast, kind)) # Put the zstats and mask on the surface for hemi in ["lh", "rh"]: projcmd = ["mri_vol2surf", "--mov", "%s/zstat1.nii.gz" % contrast, "--reg", self.inputs.reg_file, "--surf-fwhm", "5", "--hemi", hemi, "--projfrac-avg", "0", "1", ".1", "--o", "%s/%s.zstat1.mgz" % (contrast, hemi)] submit_cmdline(runtime, projcmd) # Mask image projcmd = ["mri_vol2surf", "--mov", "%s/mask.nii.gz" % contrast, "--reg", self.inputs.reg_file, "--hemi", hemi, "--projfrac-max", "0", "1", ".1", "--o", "%s/%s.mask.mgz" % (contrast, hemi)] submit_cmdline(runtime, projcmd) flame_results.append(op.abspath(contrast)) zstat_files.append(op.abspath("%s/zstat1.nii.gz" % contrast)) self.flame_results = flame_results self.zstat_files = zstat_files return runtime