def probtrackx2_connectogram(outdir, bedpostx_dir, seed_masks, tracto_mask, stop_mask = None, avoid_mask = None, subdir = "probtrackx2", nsamples = 5000, nsteps = 2000, #cthr=0.2, #loopcheck=None, #onewaycondition=None, #usef=None, #simple=None, #seedref=None, #steplength=0.5, #fibthresh=0.01, #distthresh=0.0, #sampvox=0.0, #network=None ): """ <unit> <output name="proba_matrix" type="File" /> <output name="network_matrix" type="File" /> <input name="outdir" type="Directory" /> <input name="bedpostx_dir" type="Directory" /> <input name="seed_masks" type="List" content="File" /> <input name="tracto_mask" type="File" /> <input name="stop_mask" type="File" /> <input name="avoid_mask" type="File" /> <input name="subdir" type="Str" /> <input name="nsamples" type="Int" /> <input name="nsteps" type="Int" /> </unit> """ if subdir: outdir = os.path.join(outdir, subdir) if not os.path.isdir(outdir): os.makedirs(outdir) file_listing_masks = os.path.join(outdir, "masks.txt") np.savetxt(file_listing_masks, np.asarray(seed_masks), delimiter=" ", fmt="%s") basepath_samples = os.path.join(bedpostx_dir, "merged") proba_files, network_matrix = probtrackx2(network = True, seed = file_listing_masks, loopcheck = True, onewaycondition = True, samples = basepath_samples, mask = tracto_mask, dir = outdir, nsamples = nsamples, nsteps = nsteps) proba_matrix = proba_files[0] return proba_matrix, network_matrix
We compute the fibers starting at a specific gyrus, connecting the other gyri only with the 'network' option. The result is stored in the 'fdt_paths' image. The 'fdt_network_matrix' is a 2D region-by-region connectivity matrix. """ probtrackx2_outdir = os.path.join(outdir, "proba_region_connectivity") masks_file = os.path.join(probtrackx2_outdir, "masks.txt") if not os.path.isdir(probtrackx2_outdir): os.mkdir(probtrackx2_outdir) if not os.path.isfile(masks_file): numpy.savetxt(masks_file, numpy.asarray(seeds), delimiter=" ", fmt="%s") proba_files, network_file = probtrackx2( network=True, seed=masks_file, loopcheck=True, onewaycondition=True, samples=os.path.join(bedpostx_outdir, "merged"), mask=mask_file, dir=probtrackx2_outdir) else: proba_file = glob.glob(os.path.join(probtrackx2_outdir, "fdt_paths*"))[0] network_file = os.path.join(probtrackx2_outdir, "fdt_network_matrix") weights_file = os.path.join(probtrackx2_outdir, "waytotal") for restored_file in [network_file, proba_file, weights_file]: if not os.path.isfile(restored_file): raise IOError("FileDoesNotExist: '{0}'.".format(restored_file)) snap_file = os.path.join(qcdir, "fiber_density_map.pdf") plot_image(b0_file, overlay_file=proba_file, snap_file=snap_file, name="density", overlay_cmap="cold_hot") snap_file = os.path.join(qcdir, "prob_gyri_connectogram.pdf")
def probtrackx2_connectogram_seeding_wm(outdir, bedpostx_dir, txt_roi_masks, tracto_mask, seed_mask, stop_mask, avoid_mask, nsamples, nsteps, steplength, cthr=None, loopcheck=True, fibthresh=None, distthresh=None, sampvox=None, waypoints=None, subdir="probtrackx2"): """ Generate a connectogram using probtrackx2 with the following approach: - seed the white matter with <nsamples> in each voxel - for each sample, bidirectionally propagate a fiber until both ends of the fiber reach the stop mask. Parameters ---------- outdir: str Path to the directory where to output. bedpostx_dir: str Path to the bedpostx output dir. txt_roi_masks: str Path to the txt file listing the paths to the ROI masks. tracto_mask: str Path to the tractography mask (nodif_brain_mask.nii.gz). seed_mask: str Path to the mask where to start the tractography. It corresponds to the voxels of white matter where the FA > 0.2. stop_mask: str Path to the stop mask (inverse of white matter). subdir: str, default "probtrackx2" If set, the outputs are written in a subdirectory of outdir. <other args>: 'probtrackx2 --help' """ if subdir: outdir = os.path.join(outdir, subdir) if not os.path.isdir(outdir): os.makedirs(outdir) probtrackx2(dir=outdir, samples=os.path.join(bedpostx_dir, "merged"), mask=tracto_mask, seed=seed_mask, omatrix3=True, target3=txt_roi_masks, stop=stop_mask, avoid=avoid_mask, nsamples=nsamples, nsteps=nsteps, cthr=cthr, loopcheck=loopcheck, steplength=steplength, fibthresh=fibthresh, distthresh=distthresh, sampvox=sampvox, waypoints=waypoints) fiber_density = os.path.join(outdir, "fdt_paths.nii.gz") # Convert omatrix3 output (VOXELxVOXEL fiber count) to ROIxROI fiber count txt_matrix = omatrix3_to_roi_network(outdir) # Create a list of labels from the list of mask paths roi_masks = np.loadtxt(txt_roi_masks, dtype=str) labels = [os.path.basename(x).split(".nii")[0] for x in roi_masks] txt_labels = os.path.join(outdir, "labels.txt") np.savetxt(txt_labels, labels, fmt="%s") # Create a normalized connectogram in the same directory txt_matrix_normalized, _ = normalize_connectogram_by_target_surf_area( outdir, txt_roi_masks, txt_matrix, txt_labels) return fiber_density, txt_matrix, txt_matrix_normalized, txt_labels
def get_profile(ico_order, nodif_file, nodifmask_file, seed_file, bedpostx_samples, outdir, t1_file, trf_file, dat_file, fsdir, sid, fsconfig): """ Probabilistic profile Computes the tractography using FSL probtrackx2 and projects the result on the cortical surface using FS mri_vol2surf. Parameters ---------- ico_order: int (mandatory) icosahedron order in [0, 7] that will be used to generate the cortical surface texture at a specific tessalation (the corresponding cortical surface can be resampled using the 'clindmri.segmentation.freesurfer.resample_cortical_surface' function). nodif_file: str (mandatory) file for probtrackx2 containing the no diffusion volume and associated space information. nodifmask_file: str (mandatory) file for probtrackx2 containing the tractography mask (ie., a mask of the white matter). seed_file: str (mandatory) text file for probtrackx2 containing seed coordinates. bedpostx_samples: str (mandatory) path prefix for bedpostX model samples files injected in probtrackx2 (eg., fsl.bedpostX/merged). outdir: str (mandatory) the output directory. t1_file : str (mandatory) T1 image file used to align the produced probabilitic tractography map to the T1 space. trf_file : str (mandatory) diffusion to t1 space affine transformation matrix file. dat_file: str (mandatory) structural to FreeSurfer space affine transformation matrix '.dat' file as computed by 'tkregister2'. fsdir: str (mandatory) FreeSurfer subjects directory 'SUBJECTS_DIR'. sid: str (mandatory) FreeSurfer subject identifier. fsconfig: str (mandatory) the FreeSurfer '.sh' config file. Returns ------- proba_file: str the seed probabilistic tractography volume. textures: dict a dictionary containing the probabilist texture for each hemisphere. """ # Generates the diffusion probability map proba_files, _ = probtrackx2(simple=True, seedref=nodif_file, out="fdt_paths", seed=seed_file, loopcheck=True, onewaycondition=True, samples=bedpostx_samples, mask=nodifmask_file, dir=outdir) # Check that only one 'fdt_paths' has been generated if len(proba_files) != 1: raise Exception("One probabilistic tractography file expected at this " "point: {0}".format(proba_files)) proba_file = proba_files[0] proba_fname = os.path.basename(proba_file).replace(".nii.gz", "") # Apply 'trf_file' affine transformation matrix using FSL flirt function: # probability map (diffusion space) -> probability map (T1 space). flirt_t1_file = os.path.join(outdir, proba_fname + "_t1_flirt.nii.gz") flirt(proba_file, t1_file, out=flirt_t1_file, applyxfm=True, init=trf_file) # Project the volumic probability map (T1 space) generated with FSL flirt # on the cortical surface (Freesurfer space) (both hemispheres) using # Freesurfer's mri_vol2surf and applying the 'dat_file' transformation. textures = {} for hemi in ["lh", "rh"]: prob_texture_file = os.path.join( outdir, "{0}.{1}_vol2surf.mgz".format(hemi, proba_fname)) mri_vol2surf(hemi, flirt_t1_file, prob_texture_file, ico_order, dat_file, fsdir, sid, surface_name="white", fsconfig=fsconfig) textures[hemi] = prob_texture_file return proba_file, textures
def get_profile(ico_order, nodif_file, nodifmask_file, seed_file, bedpostx_samples, outdir, t1_file, trf_file, dat_file, fsdir, sid, fsconfig): """ Probabilistic profile Computes the tractography using FSL probtrackx2 and projects the result on the cortical surface using FS mri_vol2surf. Parameters ---------- ico_order: int (mandatory) icosahedron order in [0, 7] that will be used to generate the cortical surface texture at a specific tessalation (the corresponding cortical surface can be resampled using the 'clindmri.segmentation.freesurfer.resample_cortical_surface' function). nodif_file: str (mandatory) file for probtrackx2 containing the no diffusion volume and associated space information. nodifmask_file: str (mandatory) file for probtrackx2 containing the tractography mask (ie., a mask of the white matter). seed_file: str (mandatory) text file for probtrackx2 containing seed coordinates. bedpostx_samples: str (mandatory) path prefix for bedpostX model samples files injected in probtrackx2 (eg., fsl.bedpostX/merged). outdir: str (mandatory) the output directory. t1_file : str (mandatory) T1 image file used to align the produced probabilitic tractography map to the T1 space. trf_file : str (mandatory) diffusion to t1 space affine transformation matrix file. dat_file: str (mandatory) structural to FreeSurfer space affine transformation matrix '.dat' file as computed by 'tkregister2'. fsdir: str (mandatory) FreeSurfer subjects directory 'SUBJECTS_DIR'. sid: str (mandatory) FreeSurfer subject identifier. fsconfig: str (mandatory) the FreeSurfer '.sh' config file. Returns ------- proba_file: str the seed probabilistic tractography volume. textures: dict a dictionary containing the probabilist texture for each hemisphere. """ # Generates the diffusion probability map proba_files, _ = probtrackx2( simple=True, seedref=nodif_file, out="fdt_paths", seed=seed_file, loopcheck=True, onewaycondition=True, samples=bedpostx_samples, mask=nodifmask_file, dir=outdir) # Check that only one 'fdt_paths' has been generated if len(proba_files) != 1: raise Exception("One probabilistic tractography file expected at this " "point: {0}".format(proba_files)) proba_file = proba_files[0] proba_fname = os.path.basename(proba_file).replace(".nii.gz", "") # Apply 'trf_file' affine transformation matrix using FSL flirt function: # probability map (diffusion space) -> probability map (T1 space). flirt_t1_file = os.path.join(outdir, proba_fname + "_t1_flirt.nii.gz") flirt(proba_file, t1_file, out=flirt_t1_file, applyxfm=True, init=trf_file) # Project the volumic probability map (T1 space) generated with FSL flirt # on the cortical surface (Freesurfer space) (both hemispheres) using # Freesurfer's mri_vol2surf and applying the 'dat_file' transformation. textures = {} for hemi in ["lh", "rh"]: prob_texture_file = os.path.join( outdir, "{0}.{1}_vol2surf.mgz".format(hemi, proba_fname)) mri_vol2surf(hemi, flirt_t1_file, prob_texture_file, ico_order, dat_file, fsdir, sid, surface_name="white", fsconfig=fsconfig) textures[hemi] = prob_texture_file return proba_file, textures