{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Visualization of ROI Surface Rendered with Streamlines\n\nHere is a simple tutorial following the probabilistic CSA Tracking Example in\nwhich we generate a dataset of streamlines from a corpus callosum ROI, and\nthen display them with the seed ROI rendered in 3D with 50% transparency.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from dipy.core.gradients import gradient_table\nfrom dipy.reconst.shm import CsaOdfModel\nfrom dipy.data import default_sphere, get_fnames\nfrom dipy.direction import peaks_from_model\nfrom dipy.io.gradients import read_bvals_bvecs\nfrom dipy.io.image import load_nifti, load_nifti_data\nfrom dipy.tracking.stopping_criterion import ThresholdStoppingCriterion\nfrom dipy.tracking import utils\nfrom dipy.tracking.local_tracking import LocalTracking\nfrom dipy.tracking.streamline import Streamlines\nfrom dipy.viz import actor, window, colormap as cmap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we need to generate some streamlines. For a more complete\ndescription of these steps, please refer to the CSA Probabilistic Tracking\nTutorial.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "hardi_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\nwhite_matter = (labels == 1) | (labels == 2)\n\ncsa_model = CsaOdfModel(gtab, sh_order=6)\ncsa_peaks = peaks_from_model(csa_model, data, default_sphere,\n relative_peak_threshold=.8,\n min_separation_angle=45,\n mask=white_matter)\n\nstopping_criterion = ThresholdStoppingCriterion(csa_peaks.gfa, .25)\n\nseed_mask = labels == 2\nseeds = utils.seeds_from_mask(seed_mask, affine, density=[1, 1, 1])\n\n# Initialization of LocalTracking. The computation happens in the next step.\nstreamlines = LocalTracking(csa_peaks, stopping_criterion, seeds, affine,\n step_size=2)\n\n# Compute streamlines and store as a list.\nstreamlines = Streamlines(streamlines)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will create a streamline actor from the streamlines.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "streamlines_actor = actor.line(streamlines, cmap.line_colors(streamlines))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we create a surface actor from the corpus callosum seed ROI. We\nprovide the ROI data, the affine, the color in [R,G,B], and the opacity as\na decimal between zero and one. Here, we set the color as blue/green with\n50% opacity.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "surface_opacity = 0.5\nsurface_color = [0, 1, 1]\n\nseedroi_actor = actor.contour_from_roi(seed_mask, affine,\n surface_color, surface_opacity)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we initialize a ''Scene'' object and add both actors\nto the rendering.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene = window.Scene()\nscene.add(streamlines_actor)\nscene.add(seedroi_actor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you uncomment the following line, the rendering will pop up in an\ninteractive window.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interactive = False\nif interactive:\n window.show(scene)\n\nwindow.record(scene, out_path='contour_from_roi_tutorial.png', size=(1200, 900))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: contour_from_roi_tutorial.png\n :align: center\n\n **A top view of corpus callosum streamlines with the blue transparent\n seed ROI in the center**.\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 }