def get_fiber_direction(self): # Getting fiber direction csamodel = shm.CsaOdfModel(self.gtab, 6) if self.atlas == "stanford": white_matter = binary_dilation((self.labels == 1) | (self.labels == 2)) elif self.atlas == "desikan": white_matter = binary_dilation((self.labels == 41) | (self.labels == 2)) if self.data.shape[:3] == white_matter.shape: csapeaks = peaks.peaks_from_model(model=csamodel, data=self.data, sphere=peaks.default_sphere, relative_peak_threshold=.8, min_separation_angle=45, mask=white_matter) # mask, b0_mask = self.create_mask() # self.make_tensor(b0_mask) # csd_peaks = self.make_csd(mask, b0_mask) else: print(f"{LogLVL.lvl1}ERR: dimensions are different:") print(f"{LogLVL.lvl2}data shape: {self.data.shape}") print(f"{LogLVL.lvl2}white matter shape: {white_matter.shape}") print(f"{LogLVL.lvl1}ERR: cannot continue") csapeaks = None return csapeaks, white_matter
labels = labels_img.get_data() fetch_stanford_t1() t1 = read_stanford_t1() t1_data = t1.get_data() """ We've loaded an image called ``labels_img`` which is a map of tissue types such that every integer value in the array ``labels`` represents an anatomical structure or tissue type [#]_. For this example, the image was created so that white matter voxels have values of either 1 or 2. We'll use ``peaks_from_model`` to apply the ``CsaOdfModel`` to each white matter voxel and estimate fiber orientations which we can use for tracking. """ white_matter = (labels == 1) | (labels == 2) csamodel = shm.CsaOdfModel(gtab, 6) csapeaks = peaks.peaks_from_model(model=csamodel, data=data, sphere=peaks.default_sphere, relative_peak_threshold=.8, min_separation_angle=45, mask=white_matter) """ Now we can use EuDX to track all of the white matter. To keep things reasonably fast we use ``density=2`` which will result in 8 seeds per voxel. We'll set ``a_low`` (the parameter which determines the threshold of FA/QA under which tracking stops) to be very low because we've already applied a white matter mask. """ seeds = utils.seeds_from_mask(white_matter, density=2)
def deterministic(diffusion_file, bvecs_file, bvals_file, outdir, mask_file=None, order=4, nb_seeds_per_voxel=1, step=0.5, fmt="%.4f"): """ Compute a deterministic tractography using an ODF model. Parameters ---------- diffusion_file: str (mandatory) a file containing the preprocessed diffusion data. bvecs_file: str (mandatory) a file containing the diffusion gradient directions. bvals_file: str (mandatory) a file containing the diffusion b-values. outdir: str (mandatory) the output directory. mask_file: str (optional, default None) an image used to mask the diffusion data during the tractography. If not set, all the image voxels are considered. order: int (optional, default 4) the order of the ODF model. nb_seeds_per_voxel: int (optional, default 1) the number of seeds per voxel used during the propagation. step: float (optional, default 0.5) the integration step in voxel fraction used during the propagation. fmt: str (optional, default '%.4f') the saved track elements format. Returns ------- track_file: str a determinist model of the white matter organization. """ # Read diffusion sequence bvals, bvecs = read_bvals_bvecs(bvals_file, bvecs_file) gtab = gradient_table(bvals, bvecs) diffusion_array = nibabel.load(diffusion_file).get_data() if mask_file is not None: mask_array = nibabel.load(mask_file).get_data() else: mask_array = numpy.ones(diffusion_array.shape[:3], dtype=numpy.uint8) # Estimate ODF model csamodel = shm.CsaOdfModel(gtab, order) csapeaks = peaks.peaks_from_model(model=csamodel, data=diffusion_array, sphere=peaks.default_sphere, relative_peak_threshold=.8, min_separation_angle=45, mask=mask_array) # Compute deterministic tractography in voxel space so affine is equal # to identity seeds = utils.seeds_from_mask(mask_array, density=nb_seeds_per_voxel) streamline_generator = EuDX(csapeaks.peak_values, csapeaks.peak_indices, odf_vertices=peaks.default_sphere.vertices, a_low=.05, step_sz=step, seeds=seeds) affine = streamline_generator.affine streamlines = list(streamline_generator) # Save the tracks track_file = os.path.join(outdir, "fibers.txt") savetxt(track_file, streamlines, fmt=fmt) return track_file
'b values' gtab.bvals 'b vecs' gtab.bvecs print ' gradient table calculation finished' #Build Brain Mask bm = np.where(labels == 0, 0, 1) mask = bm print 'masking the brain finished' #Compute odf in Brain Mask print 'Start computing odf' csamodel = shm.CsaOdfModel(gtab, 6) # spherical hamronics order 6 start_time = time.time() #sphere = get_sphere('symmetric724') csapeaks = peaks.peaks_from_model( model=csamodel, data=data, sphere=peaks.default_sphere, #sphere=sphere, relative_peak_threshold=.1, min_separation_angle=25, mask=mask, parallel=True, sh_order=8, npeaks=5) time_parallel = time.time() - start_time
def deterministic(diffusion_file, bvecs_file, bvals_file, trackfile, mask_file=None, order=4, nb_seeds_per_voxel=1, step=0.5): """ Compute a deterministic tractography using an ODF model. Parameters ---------- diffusion_file: str (mandatory) a file containing the preprocessed diffusion data. bvecs_file: str (mandatory) a file containing the diffusion gradient directions. bvals_file: str (mandatory) a file containing the diffusion b-values. trackfile: str (mandatory) a file path where the fibers will be saved in trackvis format. mask_file: str (optional, default None) an image used to mask the diffusion data during the tractography. If not set, all the image voxels are considered. order: int (optional, default 4) the order of the ODF model. nb_seeds_per_voxel: int (optional, default 1) the number of seeds per voxel used during the propagation. step: float (optional, default 0.5) the integration step in voxel fraction used during the propagation. Returns ------- streamlines: tuple of 3-uplet the computed fiber tracks in trackvis format (points: ndarray shape (N,3) where N is the number of points, scalars: None or ndarray shape (N, M) where M is the number of scalars per point, properties: None or ndarray shape (P,) where P is the number of properties). hdr: structured array structured array with trackvis header fields (voxel size, voxel order, dim). """ # Read diffusion sequence bvals, bvecs = read_bvals_bvecs(bvals_file, bvecs_file) gtab = gradient_table(bvals, bvecs) diffusion_image = nibabel.load(diffusion_file) diffusion_array = diffusion_image.get_data() if mask_file is not None: mask_array = nibabel.load(mask_file).get_data() else: mask_array = numpy.ones(diffusion_array.shape[:3], dtype=numpy.uint8) # Estimate ODF model csamodel = shm.CsaOdfModel(gtab, order) csapeaks = peaks.peaks_from_model(model=csamodel, data=diffusion_array, sphere=peaks.default_sphere, relative_peak_threshold=.8, min_separation_angle=45, mask=mask_array) # Compute deterministic tractography in voxel space so affine is equal # to identity seeds = utils.seeds_from_mask(mask_array, density=nb_seeds_per_voxel) streamline_generator = EuDX(csapeaks.peak_values, csapeaks.peak_indices, odf_vertices=peaks.default_sphere.vertices, a_low=.05, step_sz=step, seeds=seeds) # affine = streamline_generator.affine # Save the tracks in trackvis format hdr = nibabel.trackvis.empty_header() hdr["voxel_size"] = diffusion_image.get_header().get_zooms()[:3] hdr["voxel_order"] = "LAS" hdr["dim"] = diffusion_array.shape[:3] streamlines = [track for track in streamline_generator] random.shuffle(streamlines) streamlines = ((track, None, None) for track in streamlines) nibabel.trackvis.write(trackfile, streamlines, hdr, points_space="voxel") return streamlines, hdr