{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Simple volume slicing\n\nHere we present an example for visualizing slices from 3D images.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n\nfrom dipy.data import fetch_bundles_2_subjects\nfrom dipy.io.image import load_nifti, load_nifti_data\nfrom dipy.viz import window, actor, ui" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's download and load a T1.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fetch_bundles_2_subjects()\n\nfname_t1 = os.path.join(os.path.expanduser('~'), '.dipy',\n 'exp_bundles_and_maps', 'bundles_2_subjects',\n 'subj_1', 't1_warped.nii.gz')\n\ndata, affine = load_nifti(fname_t1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a Scene object which holds all the actors which we want to visualize.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene = window.Scene()\nscene.background((0.5, 0.5, 0.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Render slices from T1 with a specific value range\n\nThe T1 has usually a higher range of values than what can be visualized in an\nimage. We can set the range that we would like to see.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mean, std = data[data > 0].mean(), data[data > 0].std()\nvalue_range = (mean - 0.5 * std, mean + 1.5 * std)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``slice`` function will read data and resample the data using an affine\ntransformation matrix. The default behavior of this function is to show the\nmiddle slice of the last dimension of the resampled data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "slice_actor = actor.slicer(data, affine, value_range)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``slice_actor`` contains an axial slice.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.add(slice_actor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same actor can show any different slice from the given data using its\n``display`` function. However, if we want to show multiple slices we need to\ncopy the actor first.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "slice_actor2 = slice_actor.copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have a new ``slice_actor`` which displays the middle slice of the\nsagittal plane.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "slice_actor2.display(slice_actor2.shape[0]//2, None, None)\n\nscene.add(slice_actor2)\n\nscene.reset_camera()\nscene.zoom(1.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to interact with the data you will need to uncomment the line below.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# window.show(scene, size=(600, 600), reset_camera=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Otherwise, you can save a screenshot using the following command.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "window.record(scene, out_path='slices.png', size=(600, 600),\n reset_camera=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: slices.png\n :align: center\n\n Simple slice viewer.\n\n## Render slices from FA with your colormap\n\nIt is also possible to set the colormap of your preference. Here we are loading\nan FA image and showing it in a non-standard way using an HSV colormap.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fname_fa = os.path.join(os.path.expanduser('~'), '.dipy',\n 'exp_bundles_and_maps', 'bundles_2_subjects',\n 'subj_1', 'fa_1x1x1.nii.gz')\n\nfa = load_nifti_data(fname_fa)\n\nlut = actor.colormap_lookup_table(scale_range=(fa.min(), fa.max()*0.8),\n hue_range=(0.4, 1.),\n saturation_range=(1, 1.),\n value_range=(0., 1.))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is because the lookup table is applied in the slice after interpolating\nto (0, 255).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fa_actor = actor.slicer(fa, affine, lookup_colormap=lut)\n\nscene.clear()\nscene.add(fa_actor)\n\nscene.reset_camera()\nscene.zoom(1.4)\n\n# window.show(scene, size=(600, 600), reset_camera=False)\n\nwindow.record(scene, out_path='slices_lut.png', size=(600, 600),\n reset_camera=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: slices_lut.png\n :align: center\n\n **Simple slice viewer with an HSV colormap**.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we would like to add the ability to click on a voxel and show its value\non a panel in the window. The panel is a UI element which requires access to\ndifferent areas of the visualization pipeline and therefore we don't recommend\nusing it with ``window.show``. The more appropriate way is to use the\n``ShowManager`` object, which allows accessing the pipeline in different areas.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show_m = window.ShowManager(scene, size=(1200, 900))\nshow_m.initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll start by creating the panel and adding it to the ``ShowManager``\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "label_position = ui.TextBlock2D(text='Position:')\nlabel_value = ui.TextBlock2D(text='Value:')\n\nresult_position = ui.TextBlock2D(text='')\nresult_value = ui.TextBlock2D(text='')\n\npanel_picking = ui.Panel2D(size=(250, 125),\n position=(20, 20),\n color=(0, 0, 0),\n opacity=0.75,\n align=\"left\")\n\npanel_picking.add_element(label_position, (0.1, 0.55))\npanel_picking.add_element(label_value, (0.1, 0.25))\n\npanel_picking.add_element(result_position, (0.45, 0.55))\npanel_picking.add_element(result_value, (0.45, 0.25))\n\nscene.add(panel_picking)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a left-click callback to the slicer. Also disable interpolation so you can\nsee what you are picking.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def left_click_callback(obj, ev):\n \"\"\"Get the value of the clicked voxel and show it in the panel.\"\"\"\n event_pos = show_m.iren.GetEventPosition()\n\n obj.picker.Pick(event_pos[0], event_pos[1], 0, scene)\n\n i, j, k = obj.picker.GetPointIJK()\n result_position.message = '({}, {}, {})'.format(str(i), str(j), str(k))\n result_value.message = '%.8f' % data[i, j, k]\n\n\nfa_actor.SetInterpolate(False)\nfa_actor.AddObserver('LeftButtonPressEvent', left_click_callback, 1.0)\n\n# show_m.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a mosaic\n\nBy using the ``copy`` and ``display`` method of the ``slice_actor`` it becomes\neasy and efficient to create a mosaic of all the slices.\n\nSo, let's clear the scene and change the projection from perspective to\nparallel. We'll also need a new show manager and an associated callback.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scene.clear()\nscene.projection('parallel')\n\nresult_position.message = ''\nresult_value.message = ''\n\nshow_m_mosaic = window.ShowManager(scene, size=(1200, 900))\nshow_m_mosaic.initialize()\n\n\ndef left_click_callback_mosaic(obj, ev):\n \"\"\"Get the value of the clicked voxel and show it in the panel.\"\"\"\n event_pos = show_m_mosaic.iren.GetEventPosition()\n\n obj.picker.Pick(event_pos[0], event_pos[1], 0, scene)\n\n i, j, k = obj.picker.GetPointIJK()\n result_position.message = '({}, {}, {})'.format(str(i), str(j), str(k))\n result_value.message = '%.8f' % data[i, j, k]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we need to create two nested for loops which will set the positions of\nthe grid of the mosaic and add the new actors to the scene. We are going\nto use 15 columns and 10 rows but you can adjust those with your datasets.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cnt = 0\n\nX, Y, Z = slice_actor.shape[:3]\n\nrows = 10\ncols = 15\nborder = 10\n\nfor j in range(rows):\n for i in range(cols):\n slice_mosaic = slice_actor.copy()\n slice_mosaic.display(None, None, cnt)\n slice_mosaic.SetPosition((X + border) * i,\n 0.5 * cols * (Y + border) - (Y + border) * j,\n 0)\n slice_mosaic.SetInterpolate(False)\n slice_mosaic.AddObserver('LeftButtonPressEvent',\n left_click_callback_mosaic,\n 1.0)\n scene.add(slice_mosaic)\n cnt += 1\n if cnt > Z:\n break\n if cnt > Z:\n break\n\nscene.reset_camera()\nscene.zoom(1.0)\n\n# show_m_mosaic.ren.add(panel_picking)\n# show_m_mosaic.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you uncomment the two lines above, you will be able to move\nthe mosaic up/down and left/right using the middle mouse button drag,\nzoom in/out using the scroll wheel, and pick voxels with left click.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "window.record(scene, out_path='mosaic.png', size=(900, 600),\n reset_camera=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: mosaic.png\n :align: center\n\n A mosaic of all the slices in the T1 volume.\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 }