{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Visualize bundles and metrics on bundles\n\nFirst, let's download some available datasets. Here we are using a dataset\nwhich provides metrics and bundles.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom dipy.viz import window, actor\nfrom dipy.data import fetch_bundles_2_subjects, read_bundles_2_subjects\nfrom dipy.tracking.streamline import transform_streamlines\n\nfetch_bundles_2_subjects()\ndix = read_bundles_2_subjects(subj_id='subj_1', metrics=['fa'],\n bundles=['cg.left', 'cst.right'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Store fractional anisotropy.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fa = dix['fa']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Store grid to world transformation matrix.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "affine = dix['affine']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Store the cingulum bundle. A bundle is a list of streamlines.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bundle = dix['cg.left']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It happened that this bundle is in world coordinates and therefore we need to\ntransform it into native image coordinates so that it is in the same coordinate\nspace as the ``fa`` image.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bundle_native = transform_streamlines(bundle, np.linalg.inv(affine))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show every streamline with an orientation color\n\nThis is the default option when you are using ``line`` or ``streamtube``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene = window.Scene()\n\nstream_actor = actor.line(bundle_native)\n\nscene.set_camera(position=(-176.42, 118.52, 128.20),\n focal_point=(113.30, 128.31, 76.56),\n view_up=(0.18, 0.00, 0.98))\n\nscene.add(stream_actor)\n\n# Uncomment the line below to show to display the window\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle1.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle1.png\n :align: center\n\n One orientation color for every streamline.\n\nYou may wonder how we knew how to set the camera. This is very easy. You just\nneed to run ``window.show`` once to see how you want to see the object and then\nclose the window and call the ``camera_info`` method which prints the position,\nfocal point and view up vectors of the camera.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.camera_info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show every point with a value from a volume with default colormap\n\nHere we will need to input the ``fa`` map in ``streamtube`` or ``line``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\nstream_actor2 = actor.line(bundle_native, fa, linewidth=0.1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also show the scalar bar.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bar = actor.scalar_bar()\n\nscene.add(stream_actor2)\nscene.add(bar)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle2.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle2.png\n :align: center\n\n Every point with a color from FA.\n\n## Show every point with a value from a volume with your colormap\n\nHere we will need to input the ``fa`` map in ``streamtube``\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\n\nhue = (0.0, 0.0) # red only\nsaturation = (0.0, 1.0) # white to red\n\nlut_cmap = actor.colormap_lookup_table(hue_range=hue,\n saturation_range=saturation)\n\nstream_actor3 = actor.line(bundle_native, fa, linewidth=0.1,\n lookup_colormap=lut_cmap)\nbar2 = actor.scalar_bar(lut_cmap)\n\nscene.add(stream_actor3)\nscene.add(bar2)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle3.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle3.png\n :align: center\n\n Every point with a color from FA using a non default colormap.\n\n\n## Show every bundle with a specific color\n\nYou can have a bundle with a specific color. In this example, we are choosing\norange.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\nstream_actor4 = actor.line(bundle_native, (1., 0.5, 0), linewidth=0.1)\n\nscene.add(stream_actor4)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle4.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle4.png\n :align: center\n\n Entire bundle with a specific color.\n\n## Show every streamline of a bundle with a different color\n\nLet's make a colormap where every streamline of the bundle is colored by its\nlength.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\n\nfrom dipy.tracking.streamline import length\n\nlengths = length(bundle_native)\n\nhue = (0.5, 0.5) # blue only\nsaturation = (0.0, 1.0) # black to white\n\nlut_cmap = actor.colormap_lookup_table(\n scale_range=(lengths.min(), lengths.max()),\n hue_range=hue,\n saturation_range=saturation)\n\nstream_actor5 = actor.line(bundle_native, lengths, linewidth=0.1,\n lookup_colormap=lut_cmap)\n\nscene.add(stream_actor5)\nbar3 = actor.scalar_bar(lut_cmap)\n\nscene.add(bar3)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle5.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle5.png\n :align: center\n\n Color every streamline by the length of the streamline\n\n\n## Show every point of every streamline with a different color\n\nIn this case in which we want to have a color per point and per streamline,\nwe can create a list of the colors to correspond to the list of streamlines\n(bundles). Here in ``colors`` we will insert some random RGB colors.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\n\ncolors = [np.random.rand(*streamline.shape) for streamline in bundle_native]\n\nstream_actor6 = actor.line(bundle_native, colors, linewidth=0.2)\n\nscene.add(stream_actor6)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\nwindow.record(scene, out_path='bundle6.png', size=(600, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: bundle6.png\n :align: center\n\n Random colors per point per streamline.\n\nIn summary, we showed that there are many useful ways for visualizing maps\non bundles.\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 }