{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# An introduction to the Deterministic Maximum Direction Getter\n\nDeterministic maximum direction getter is the deterministic version of the\nprobabilistic direction getter. It can be used with the same local models\nand has the same parameters. Deterministic maximum fiber tracking follows\nthe trajectory of the most probable pathway within the tracking constraint\n(e.g. max angle). In other words, it follows the direction with the highest\nprobability from a distribution, as opposed to the probabilistic direction\ngetter which draws the direction from the distribution. Therefore, the maximum\ndeterministic direction getter is equivalent to the probabilistic direction\ngetter returning always the maximum value of the distribution.\n\nDeterministic maximum fiber tracking is an alternative to EuDX deterministic\ntractography and unlike EuDX does not follow the peaks of the local models but\nuses the entire orientation distributions.\n\nThis example is an extension of the `example_tracking_probabilistic`\nexample. We begin by loading the data, fitting a Constrained Spherical\nDeconvolution (CSD) reconstruction model for the tractography and fitting\nthe constant solid angle (CSA) reconstruction model to define the tracking\nmask (stopping criterion).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from dipy.core.gradients import gradient_table\nfrom dipy.data import default_sphere, get_fnames\nfrom dipy.direction import DeterministicMaximumDirectionGetter\nfrom dipy.io.gradients import read_bvals_bvecs\nfrom dipy.io.image import load_nifti, load_nifti_data\nfrom dipy.io.stateful_tractogram import Space, StatefulTractogram\nfrom dipy.io.streamline import save_trk\nfrom dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel,\n auto_response_ssst)\nfrom dipy.reconst.shm import CsaOdfModel\nfrom dipy.tracking import utils\nfrom dipy.tracking.local_tracking import LocalTracking\nfrom dipy.tracking.stopping_criterion import ThresholdStoppingCriterion\nfrom dipy.tracking.streamline import Streamlines\nfrom dipy.viz import window, actor, colormap, has_fury\n\n# Enables/disables interactive visualization\ninteractive = False\n\n\nhardi_fname, hardi_bval_fname, hardi_bvec_fname = get_fnames('stanford_hardi')\nlabel_fname = get_fnames('stanford_labels')\n\ndata, affine, hardi_img = load_nifti(hardi_fname, return_img=True)\nlabels = load_nifti_data(label_fname)\nbvals, bvecs = read_bvals_bvecs(hardi_bval_fname, hardi_bvec_fname)\ngtab = gradient_table(bvals, bvecs)\n\nseed_mask = labels == 2\nwhite_matter = (labels == 1) | (labels == 2)\nseeds = utils.seeds_from_mask(seed_mask, affine, density=1)\n\nresponse, ratio = auto_response_ssst(gtab, data, roi_radii=10, fa_thr=0.7)\n\ncsd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6)\ncsd_fit = csd_model.fit(data, mask=white_matter)\n\ncsa_model = CsaOdfModel(gtab, sh_order=6)\ngfa = csa_model.fit(data, mask=white_matter).gfa\nstopping_criterion = ThresholdStoppingCriterion(gfa, .25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Fiber Orientation Distribution (FOD) of the CSD model estimates the\ndistribution of small fiber bundles within each voxel. This distribution\ncan be used for deterministic fiber tracking. As for probabilistic tracking,\nthere are many ways to provide those distributions to the deterministic maximum\ndirection getter. Here, the spherical harmonic representation of the FOD\nis used.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "detmax_dg = DeterministicMaximumDirectionGetter.from_shcoeff(\n csd_fit.shm_coeff, max_angle=30., sphere=default_sphere)\nstreamline_generator = LocalTracking(detmax_dg, stopping_criterion, seeds,\n affine, step_size=.5)\nstreamlines = Streamlines(streamline_generator)\n\nsft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)\nsave_trk(sft, \"tractogram_deterministic_dg.trk\")\n\nif has_fury:\n scene = window.Scene()\n scene.add(actor.line(streamlines, colormap.line_colors(streamlines)))\n window.record(scene, out_path='tractogram_deterministic_dg.png',\n size=(800, 800))\n if interactive:\n window.show(scene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: tractogram_deterministic_dg.png\n :align: center\n\n **Corpus Callosum using deterministic maximum direction getter**\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. include:: ../links_names.inc\n\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 0 }