{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Bootstrap and Closest Peak Direction Getters Example\n\nThis example shows how choices in direction-getter impact fiber\ntracking results by demonstrating the bootstrap direction getter (a type of\nprobabilistic tracking, as described in Berman et al. (2008) [Berman2008]_ a\nnd the closest peak direction getter (a type of deterministic tracking).\n(Amirbekian, PhD thesis, 2016)\n\nThis example is an extension of the `example_tracking_introduction_eudx`\nexample. Let's start by loading the necessary modules for executing this\ntutorial.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from dipy.core.gradients import gradient_table\nfrom dipy.data import get_fnames, small_sphere\nfrom dipy.direction import BootDirectionGetter, ClosestPeakDirectionGetter\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.streamline import Streamlines\nfrom dipy.tracking.stopping_criterion import ThresholdStoppingCriterion\nfrom dipy.viz import window, actor, colormap, has_fury\n\n# Enables/disables interactive visualization\ninteractive = False\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\n\nseed_mask = (labels == 2)\nwhite_matter = (labels == 1) | (labels == 2)\nseeds = utils.seeds_from_mask(seed_mask, affine, density=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we fit the CSD model.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "response, ratio = auto_response_ssst(gtab, data, roi_radii=10, fa_thr=0.7)\ncsd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6)\ncsd_fit = csd_model.fit(data, mask=white_matter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we use the CSA fit to calculate GFA, which will serve as our stopping\ncriterion.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "csa_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": [ "Next, we need to set up our two direction getters\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example #1: Bootstrap direction getter with CSD Model\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "boot_dg_csd = BootDirectionGetter.from_data(data, csd_model, max_angle=30.,\n sphere=small_sphere)\nboot_streamline_generator = LocalTracking(boot_dg_csd, stopping_criterion,\n seeds, affine, step_size=.5)\nstreamlines = Streamlines(boot_streamline_generator)\nsft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)\nsave_trk(sft, \"tractogram_bootstrap_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_bootstrap_dg.png',\n size=(800, 800))\n if interactive:\n window.show(scene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: tractogram_bootstrap_dg.png\n :align: center\n\n **Corpus Callosum Bootstrap Probabilistic Direction Getter**\n\nWe have created a bootstrapped probabilistic set of streamlines. If you repeat\nthe fiber tracking (keeping all inputs the same) you will NOT get exactly the\nsame set of streamlines.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example #2: Closest peak direction getter with CSD Model\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pmf = csd_fit.odf(small_sphere).clip(min=0)\npeak_dg = ClosestPeakDirectionGetter.from_pmf(pmf, max_angle=30.,\n sphere=small_sphere)\npeak_streamline_generator = LocalTracking(peak_dg, stopping_criterion, seeds,\n affine, step_size=.5)\nstreamlines = Streamlines(peak_streamline_generator)\nsft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)\nsave_trk(sft, \"closest_peak_dg_CSD.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_closest_peak_dg.png',\n size=(800, 800))\n if interactive:\n window.show(scene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: tractogram_closest_peak_dg.png\n :align: center\n\n **Corpus Callosum Closest Peak Deterministic Direction Getter**\n\nWe have created a set of streamlines using the closest peak direction getter,\nwhich is a type of deterministic tracking. If you repeat the fiber tracking\n(keeping all inputs the same) you will get exactly the same set of streamlines.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References\n.. [Berman2008] Berman, J. et al., Probabilistic streamline q-ball\ntractography using the residual bootstrap, NeuroImage, vol 39, no 1, 2008\n\n.. 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 }