{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import k3d\n", "import math\n", "import numpy as np\n", "import nibabel as nib\n", "from k3d.helpers import download\n", "import ipywidgets as widgets\n", "import vtk\n", "from vtk.util import numpy_support" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "basic_color_maps = [(attr, getattr(k3d.basic_color_maps, attr)) for attr in dir(k3d.basic_color_maps) if not attr.startswith('__')]\n", "paraview_color_maps = [(attr, getattr(k3d.paraview_color_maps, attr)) for attr in dir(k3d.paraview_color_maps) if not attr.startswith('__')]\n", "matplotlib_color_maps = [(attr, getattr(k3d.matplotlib_color_maps, attr)) for attr in dir(k3d.matplotlib_color_maps) if not attr.startswith('__')]\n", "colormaps = basic_color_maps + paraview_color_maps + matplotlib_color_maps" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "filename = download('https://github.com/marcomusy/vtkplotter/raw/master/vtkplotter/data/embryo.slc')\n", "reader = vtk.vtkSLCReader()\n", "reader.SetFileName(filename)\n", "reader.Update()\n", "vti = reader.GetOutput()\n", "\n", "bounds = vti.GetBounds()\n", "x, y, z = vti.GetDimensions()\n", "img = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "38fea5e84bf74131beedb9a46b35dbed", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='ColorMap:', options=(('Binary', [0, 1, 1, 1, 1, 0, 0, 0]), ('Black…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "tf_editor = k3d.transfer_function_editor()\n", "volume = k3d.volume(img.astype(np.float16))\n", "\n", "@widgets.interact(x=widgets.Dropdown(options=colormaps, description='ColorMap:'))\n", "def g(x):\n", " tf_editor.color_map = np.array(x, dtype=np.float32)\n", "\n", "_ = widgets.link((tf_editor, 'color_map'), (volume, 'color_map'))\n", "_ = widgets.link((tf_editor, 'opacity_function'), (volume, 'opacity_function'))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6bb50c10085b4ec2bd5e5a2e6a094ba4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e378ec5a70d0496aa8981af2b56719e1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot = k3d.plot()\n", "plot += volume\n", "tf_editor.display()\n", "plot.display()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0. , 0. , 0.5625 , 0.11111099,\n", " 0. , 0. , 1. , 0.3344039 , 0. ,\n", " 1. , 1. , 0.4920635 , 0.5 , 1. ,\n", " 0.5 , 0.6190475 , 1. , 1. , 0. ,\n", " 0.873016 , 1. , 0. , 0. , 1. ,\n", " 0.5 , 0. , 0. ], dtype=float32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf_editor.color_map" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0. , 0.33132064, 0.39725 , 1. ,\n", " 1. ], dtype=float32)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf_editor.opacity_function" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0. , 0. , 0.5625 , 0.11111099,\n", " 0. , 0. , 1. , 0.3344039 , 0. ,\n", " 1. , 1. , 0.4920635 , 0.5 , 1. ,\n", " 0.5 , 0.6190475 , 1. , 1. , 0. ,\n", " 0.873016 , 1. , 0. , 0. , 1. ,\n", " 0.5 , 0. , 0. ], dtype=float32)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume.color_map" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0. , 0.33132064, 0.39725 , 1. ,\n", " 1. ], dtype=float32)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume.opacity_function" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1,\n", " 0,\n", " 0,\n", " 0.5625,\n", " -0.777778,\n", " 0,\n", " 0,\n", " 1,\n", " -0.269841,\n", " 0,\n", " 1,\n", " 1,\n", " -0.015873,\n", " 0.5,\n", " 1,\n", " 0.5,\n", " 0.238095,\n", " 1,\n", " 1,\n", " 0,\n", " 0.746032,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 0.5,\n", " 0,\n", " 0]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k3d.colormaps.basic_color_maps.Jet" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5eb26a70d8714b8faa07199dfbec7b64", "version_major": 2, "version_minor": 0 }, "text/plain": [ "TF_editor(color_map=array([0. , 0. , 0. , 0.5625 , 0.11111099,\n", " 0. , 0. …" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "tf_editor" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = widgets.link((tf_editor, 'color_map'), (volume, 'color_map'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }