{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Particle Filtering Tractography\nParticle Filtering Tractography (PFT) [Girard2014]_ uses tissue partial\nvolume estimation (PVE) to reconstruct trajectories connecting the gray matter,\nand not incorrectly stopping in the white matter or in the corticospinal fluid.\nIt relies on a stopping criterion that identifies the tissue where the\nstreamline stopped. If the streamline correctly stopped in the gray matter, the\ntrajectory is kept. If the streamline incorrectly stopped in the white matter\nor in the corticospinal fluid, PFT uses anatomical information to find an\nalternative streamline segment to extend the trajectory. When this segment is\nfound, the tractography continues until the streamline correctly stops in the\ngray matter.\n\nPFT finds an alternative streamline segment whenever the stopping criterion\nreturns a position classified as 'INVALIDPOINT'.\n\nThis example is an extension of `example_tracking_probabilistic` and\n`example_tracking_stopping_criterion` examples. We begin by loading the\ndata, fitting a Constrained Spherical Deconvolution (CSD) reconstruction\nmodel, creating the probabilistic direction getter and defining the seeds.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom dipy.core.gradients import gradient_table\nfrom dipy.data import get_fnames, default_sphere\nfrom dipy.direction import ProbabilisticDirectionGetter\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.tracking.local_tracking import (LocalTracking,\n ParticleFilteringTracking)\nfrom dipy.tracking.streamline import Streamlines\nfrom dipy.tracking import utils\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')\nf_pve_csf, f_pve_gm, f_pve_wm = get_fnames('stanford_pve_maps')\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\npve_csf_data = load_nifti_data(f_pve_csf)\npve_gm_data = load_nifti_data(f_pve_gm)\npve_wm_data, _, voxel_size = load_nifti(f_pve_wm, return_voxsize=True)\n\nshape = labels.shape\n\nresponse, ratio = auto_response_ssst(gtab, data, roi_radii=10, fa_thr=0.7)\ncsd_model = ConstrainedSphericalDeconvModel(gtab, response)\ncsd_fit = csd_model.fit(data, mask=pve_wm_data)\n\ndg = ProbabilisticDirectionGetter.from_shcoeff(csd_fit.shm_coeff,\n max_angle=20.,\n sphere=default_sphere)\n\nseed_mask = (labels == 2)\nseed_mask[pve_wm_data < 0.5] = 0\nseeds = utils.seeds_from_mask(seed_mask, affine, density=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CMC/ACT Stopping Criterion\nContinuous map criterion (CMC) [Girard2014]_ and Anatomically-constrained\ntractography (ACT) [Smith2012]_ both uses PVEs information from\nanatomical images to determine when the tractography stops.\nBoth stopping criterion use a trilinear interpolation\nat the tracking position. CMC stopping criterion uses a probability derived\nfrom the PVE maps to determine if the streamline reaches a 'valid' or 'invalid'\nregion. ACT uses a fixed threshold on the PVE maps. Both stopping criterion can\nbe used in conjunction with PFT. In this example, we used CMC.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from dipy.tracking.stopping_criterion import CmcStoppingCriterion\n\nvoxel_size = np.average(voxel_size[1:4])\nstep_size = 0.2\n\ncmc_criterion = CmcStoppingCriterion.from_pve(pve_wm_data,\n pve_gm_data,\n pve_csf_data,\n step_size=step_size,\n average_voxel_size=voxel_size)\n\n# Particle Filtering Tractography\npft_streamline_generator = ParticleFilteringTracking(dg,\n cmc_criterion,\n seeds,\n affine,\n max_cross=1,\n step_size=step_size,\n maxlen=1000,\n pft_back_tracking_dist=2,\n pft_front_tracking_dist=1,\n particle_count=15,\n return_all=False)\nstreamlines = Streamlines(pft_streamline_generator)\nsft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)\nsave_trk(sft, \"tractogram_pft.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_pft.png',\n size=(800, 800))\n if interactive:\n window.show(scene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: tractogram_pft.png\n :align: center\n\n **Corpus Callosum using particle filtering tractography**\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Local Probabilistic Tractography\nprob_streamline_generator = LocalTracking(dg,\n cmc_criterion,\n seeds,\n affine,\n max_cross=1,\n step_size=step_size,\n maxlen=1000,\n return_all=False)\nstreamlines = Streamlines(prob_streamline_generator)\nsft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)\nsave_trk(sft, \"tractogram_probabilistic_cmc.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_probabilistic_cmc.png',\n size=(800, 800))\n if interactive:\n window.show(scene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: tractogram_probabilistic_cmc.png\n :align: center\n\n **Corpus Callosum using probabilistic tractography**\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References\n.. [Girard2014] Girard, G., Whittingstall, K., Deriche, R., & Descoteaux, M.\n Towards quantitative connectivity analysis: reducing tractography biases.\n NeuroImage, 98, 266-278, 2014.\n\n.. [Smith2012] Smith, R. E., Tournier, J.-D., Calamante, F., & Connelly, A.\n Anatomically-constrained tractography: Improved diffusion MRI\n streamlines tractography through effective use of anatomical\n information. NeuroImage, 63(3), 1924-1938, 2012.\n\n.. include:: ../links_names.inc\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 }