Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def prob_tracking_example(model, data, mask, N, hdr, filename):
# Fit data to model
fit = model.fit(data, mask)
# Create objects to be passed to tracker
pdg = ProbabilisticDirectionGetter.from_shfit(fit, 45., default_sphere)
gfa = fit.gfa
gfa = np.where(np.isnan(gfa), 0., gfa)
ttc = ThresholdTissueClassifier(gfa, .2)
# Create around N seeds
seeds = utils.seeds_from_mask(gfa > .25, 2, affine=affine)
seeds = seeds[::len(seeds) // N + 1]
# Create streamline generator
streamlines = LocalTracking(pdg, ttc, seeds, affine, .5, max_cross=1)
trk_streamlines = utils.move_streamlines(streamlines,
input_space=affine,
output_space=trackvis_affine)
trk = ((streamline, None, None) for streamline in trk_streamlines)
# Save streamlines
nib.trackvis.write(filename, trk, hdr)
def detr_tracking_example(model, data, mask, N, hdr, filename):
csapeaks = peaks_from_model(model=csamodel,
data=data,
sphere=sphere,
relative_peak_threshold=.5,
min_separation_angle=45,
mask=mask,
return_odf=False,
normalize_peaks=True)
gfa = csapeaks.gfa
gfa = np.where(np.isnan(gfa), 0., gfa)
ttc = ThresholdTissueClassifier(gfa, .2)
# Create around N seeds
seeds = utils.seeds_from_mask(gfa > .25, 2, affine=affine)
seeds = seeds[::len(seeds) // N + 1]
# Create streamline generator
streamlines = LocalTracking(csapeaks, ttc, seeds, affine, .5, max_cross=1)
trk_streamlines = utils.move_streamlines(streamlines,
input_space=affine,
output_space=trackvis_affine)
trk = ((streamline, None, None) for streamline in trk_streamlines)
# Save streamlines
nib.trackvis.write(filename, trk, hdr)
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.get_affine()
white_matter = (labels == 1) | (labels == 2)
csa_model = CsaOdfModel(gtab, sh_order=6)
csa_peaks = peaks_from_model(csa_model, data, default_sphere,
relative_peak_threshold=.8,
min_separation_angle=45,
mask=white_matter)
classifier = ThresholdTissueClassifier(csa_peaks.gfa, .25)
seed_mask = labels == 2
seeds = utils.seeds_from_mask(seed_mask, density=[1, 1, 1], affine=affine)
# Initialization of LocalTracking.
# The computation happens in the next step.
streamlines = LocalTracking(csa_peaks, classifier, seeds, affine,
step_size=2)
# Compute streamlines and store as a list.
streamlines = list(streamlines)
# Prepare the display objects.
streamlines_actor = actor.line(streamlines, line_colors(streamlines))
seedroi_actor = actor.contour_from_roi(seed_mask, affine, [0, 1, 1], 0.5)
# Create the 3d display.
r = window.ren()
r2 = window.ren()
from dipy.reconst.csdeconv import ConstrainedSphericalDeconvModel
"""
First we load our images and establish seeds. See the Introduction to Basic
Tracking tutorial for more background on these steps.
"""
hardi_img, gtab, labels_img = read_stanford_labels()
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.affine
seed_mask = labels == 2
white_matter = (labels == 1) | (labels == 2)
seeds = utils.seeds_from_mask(seed_mask, density=1, affine=affine)
"""
Next, we fit the CSD model
"""
csd_model = ConstrainedSphericalDeconvModel(gtab, None, sh_order=6)
csd_fit = csd_model.fit(data, mask=white_matter)
"""
we use the CSA fit to calculate GFA, which will serve as our tissue
classifier
"""
from dipy.reconst.shm import CsaOdfModel
csa_model = CsaOdfModel(gtab, sh_order=6)
parallel=True)
"""
A ThresholdStoppingCriterion object is used to segment the data to track only
through areas in which the Generalized Fractional Anisotropy (GFA) is
sufficiently high.
"""
stopping_criterion = ThresholdStoppingCriterion(pnm.gfa, .25)
"""
Tracking will be started from a set of seeds evenly distributed in the white
matter:
"""
seeds = utils.seeds_from_mask(white_matter, affine, density=[2, 2, 2])
"""
For the sake of brevity, we will take only the first 1000 seeds, generating
only 1000 streamlines. Remove this line to track from many more points in all
of the white matter
"""
seeds = seeds[:1000]
"""
We now have the necessary components to construct a tracking pipeline and
execute the tracking
"""
streamline_generator = LocalTracking(pnm, stopping_criterion, seeds, affine,
step_size=.5)
sphere=default_sphere)
"""
The optic radiation is reconstructed by tracking fibers from the calcarine
sulcus (visual cortex V1) to the lateral geniculate nucleus (LGN). We seed
from the calcarine sulcus by selecting a region-of-interest (ROI) cube of
dimensions 3x3x3 voxels.
"""
# Set a seed region region for tractography.
from dipy.tracking import utils
mask = np.zeros(data.shape[:-1], 'bool')
rad = 3
mask[26-rad:26+rad, 29-rad:29+rad, 31-rad:31+rad] = True
seeds = utils.seeds_from_mask(mask, density=[4, 4, 4], affine=affine)
"""
Local Tracking is used for probabilistic tractography which takes the
direction getter along with the classifier and seeds as input.
"""
# Perform tracking using Local Tracking
from dipy.tracking.local import LocalTracking
streamlines = LocalTracking(prob_dg, classifier, seeds, affine, step_size=.5)
# Compute streamlines and store as a list.
streamlines = list(streamlines)
"""
In order to select only the fibers that enter into the LGN, another ROI is
classifier = ThresholdTissueClassifier(csa_peaks.gfa, .25)
"""
3. Before we can begin tracking is to specify where to "seed" (begin) the fiber
tracking. Generally, the seeds chosen will depend on the pathways one is
interested in modeling. In this example, we'll use a $2 \times 2 \times 2$ grid
of seeds per voxel, in a sagittal slice of the corpus callosum. Tracking from
this region will give us a model of the corpus callosum tract. This slice has
label value ``2`` in the labels image.
"""
from dipy.tracking import utils
import numpy as np
seed_mask = labels == 2
seeds = utils.seeds_from_mask(seed_mask, density=[2, 2, 2], affine=np.eye(4))
"""
Finally, we can bring it all together using ``LocalTracking``. We will then
display the resulting streamlines using the ``dipy.viz`` package.
"""
from dipy.tracking.local import LocalTracking
from dipy.viz import window, actor, colormap as cmap, have_fury
from dipy.tracking.streamline import Streamlines
# Enables/disables interactive visualization
interactive = False
# Initialization of LocalTracking. The computation happens in the next step.
streamlines_generator = LocalTracking(csa_peaks, classifier, seeds,
affine=np.eye(4), step_size=.5)
stepper = FixedSizeStepper(1)
"""
Read the voxel size from the image header:
"""
zooms = img.get_header().get_zooms()[:3]
"""
Randomly select some seed points from the mask:
"""
seeds = seeds_from_mask(mask, [1, 1, 1], zooms)
seeds = seeds[:2000]
interpolator = NearestNeighborInterpolator(maskdata, zooms)
pwt = ProbabilisticOdfWeightedTracker(csamodel, interpolator, mask,
stepper, 20, seeds, sphere)
csa_streamlines = list(pwt)
"""
Now that we have our streamlines in memory we can save the results to disk.
For this purpose we can use the TrackVis format (``*.trk``). First, we need to
create a header.
"""
import nibabel as nib