#!/usr/bin/env python
# coding: utf-8
# # itk-jupyter-widgets
#
# ## Interactive 3D and 2D Visualization in Jupyter
#
# TriPython March 2018 Meeting
#
# Matthew McCormick, PhD
#
# Kitware, Inc
#
# [](https://github.com/InsightSoftwareConsortium/itk-jupyter-widgets)
#
# [](http://trizpug.org/)
#
# [](https://creativecommons.org/share-your-work/public-domain/cc0/)
# In[1]:
from itkwidgets import view
import requests
import itk
import shutil
import os
# Download data
fileName = '005_32months_T2_RegT1_Reg2Atlas_ManualBrainMask_Stripped.nrrd'
if not os.path.exists(fileName):
response = requests.get('https://data.kitware.com/api/v1/file/564a5b078d777f7522dbfaa6/download', stream=True)
with open(fileName, 'wb') as fp:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, fp)
# Download data
brainFileName = 'brainweb165a10f17.mha'
if not os.path.exists(brainFileName):
response = requests.get('https://data.kitware.com/api/v1/file/588271308d777f4f3f3072e2/download', stream=True)
with open(brainFileName, 'wb') as fp:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, fp)
# In[2]:
from itkwidgets import view
import itk
image = itk.imread(fileName)
#view(image)
# ## Agenda
#
# ### Where have we come from? Where are we going?
# ### What is the problem we are solving?
# ### What technical approaches are required for this problem?
# ### How do I use this tool?
# ### What are the next steps?
# ## Where have we come from?
#
# ## Where are we going?
# *with interactive scientific computing in the SciPy ecosystem*
# ### IPython Shell
#
# Original author: Fernando PĂ©rez
#
#
# Image source
#
# Initial release: 2001
# ### IPython Shell
#
# IPython 0.7.2 released June 6, 2006
#
#
# [Image source](https://en.wikipedia.org/wiki/IPython)
# - Interactive: **tab completion**
# - Accessible: Matlab $\rightarrow$ **Scientific Python**
# - Brings powerful programming tools together: **matplotlib** integration
# ### IPython Notebook
#
# IPython 0.12.0 released December 18, 2011
#
#
# [Image source](http://pgbovine.net/ipython-notebook-first-impressions.htm)
# - Interactive: creates *a tool for open, collaborative, reproducible scientific computing*
# - Accessible: Terminal $\rightarrow$ **Web browser**
# - Brings powerful programming tools together: Literate programming: Markdown + $\LaTeX$ **prose**, IPython shell **code** cells, and matplotlib **visualizations**
# ### Jupyter Notebook
#
# Fernando announces Project Jupyter at SciPy 2014
#
#
# [Image source](https://jupyter.org/)
# - Interactive: Adds a file browser, browser-based terminal, and browser-based text editor
# - Accessible: Open standards for interative computing to enabled customized applications: Notebook Document Format, Interactive Computing Protocol, The Kernel
# - Brings powerful programming tools together: Expands support for programming language kernels **other than Python**
# ### JupyterLab
#
# ["JupyterLab is Ready for Users"](https://blog.jupyter.org/jupyterlab-is-ready-for-users-5a6f039b8906) - February 20, 2018
#
#
# [Image source](https://blog.jupyter.org/jupyterlab-is-ready-for-users-5a6f039b8906)
# - Interactive: Browser-based **window management**, customized viewers for different file types
# - Accessible: Drag and drop cells, easy deploy with *Docker-based* JupyterHub
# - Brings powerful programming tools together: Made with **community-developed extensions** in mind
# ### The Future
#
#
# [Image source](https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript)
#
# "To start developing a JupyterLab extension, see the JupyterLab Extension Developer Guide and the **TypeScript** or **JavaScript** extension templates. JupyterLab itself is co-developed on top of **PhosphorJS**, a new **Javascript** library for **building extensible, high-performance, desktop-style web applications**. We use **modern JavaScript technologies** such as **TypeScript, React, Lerna, Yarn, and webpack**."
# - Interactive: **Browser-based application**
# - Accessible: **Zero-install**, works on **all platforms**
# - Brings powerful programming tools together: **JavaScript**, **WebGL**, **WebAssembly**
# ## What is the problem we are solving?
#
# *Interactive visualization to support multi-dimensional spatial analysis*
# ### ipyleaflet
#
# *A Jupyter / Leaflet bridge enabling interactive maps in the Jupyter notebook.*
#
#
#
# [Image source](https://github.com/ellisonbg/ipyleaflet)
# ### GeoNotebook
#
# *GeoNotebook is an application that provides client/server environment with interactive visualization and analysis capabilities using Jupyter, GeoJS and other open source tools.*
#
#
#
# [Image source](https://github.com/OpenGeoscience/geonotebook)
# ### ipyvolume
#
# *3d plotting for Python in the Jupyter notebook based on IPython widgets using WebGL.*
#
#
#
# [Image source](https://github.com/maartenbreddels/ipyvolume)
# ### pythreejs
#
# *A Python / ThreeJS bridge utilizing the Jupyter widget infrastructure.*
#
#
#
# [Image source](https://github.com/jovyan/pythreejs)
# ### itk-jupyter-widgets
#
# These widgets are designed to support spatial analysis with the [Insight Toolkit (ITK)](https://www.itk.org), but they also work with other spatial analysis tools in the scientific Python ecosystem.
#
# These widgets are built on [itk.js](https://github.com/InsightSoftwareConsortium/itk-js) and [vtk.js](https://github.com/Kitware/vtk-js).
#
# [](https://github.com/InsightSoftwareConsortium/itk-jupyter-widgets)
# - 2D or 3D images
# - Excellent volume rendering
# - Slicing
# - Data probe
# - Histogram and opacity transfer function editor
# In[2]:
from itkwidgets import view
import itk
PixelType = itk.ctype('float')
image = itk.imread(brainFileName, PixelType)
view(image)
# In[3]:
# Smooth the image
smoother = itk.CurvatureFlowImageFilter.New(image)
smoother.SetNumberOfIterations(6)
smoother.SetTimeStep(0.05)
smoother.Update()
view(smoother.GetOutput())
# In[4]:
confidence_connected = itk.ConfidenceConnectedImageFilter.New(smoother.GetOutput())
confidence_connected.SetMultiplier(2.5)
confidence_connected.SetNumberOfIterations(5)
confidence_connected.SetInitialNeighborhoodRadius(2)
confidence_connected.SetReplaceValue(255)
Dimension = image.GetImageDimension()
SeedType = itk.Index[Dimension]
seed1 = SeedType()
seed1[0] = 118
seed1[1] = 133
seed1[2] = 92
confidence_connected.AddSeed(seed1)
seed2 = SeedType()
seed2[0] = 63
seed2[1] = 135
seed2[2] = 94
confidence_connected.AddSeed(seed2)
seed3 = SeedType()
seed3[0] = 63
seed3[1] = 157
seed3[2] = 90
confidence_connected.AddSeed(seed3)
seed4 = SeedType()
seed4[0] = 111
seed4[1] = 150
seed4[2] = 90
confidence_connected.AddSeed(seed4)
seed5 = SeedType()
seed5[0] = 111
seed5[1] = 50
seed5[2] = 88
confidence_connected.AddSeed(seed5)
# In[5]:
confidence_connected.Update()
view(confidence_connected.GetOutput())
# ## What technical approaches are required for this problem?
# ### Multiple Languages
#
#
# ### Attention to Performance
# - Browser memory limitations? **No!**
# - Browser compute limitations? **If so, not for long!**
# - Data transfer times? **Yes!**
# ## How do I use this tool?
# ### Window / Level
#
#
# [Image source](https://docs.oracle.com/cd/E19253-01/817-7519/6mmr6chkl/index.html)
# ### Opacity Transfer Function
#
# - Revelant to volume rendering
# - Scalar intensity opacity
# - Gradient-based opacity (shading)
#
# ## What are the next steps?
# ### Impact: Addressing Heart Disease
#
# American Heart Association, Precision Medicine Platform
#
#
# [Image source](https://precision.heart.org/workspace)
# ### Impact: Smart, Data-driven Financial Investments
#
# - [Triangle Quant Investing Strategies Meet-up](https://www.meetup.com/Quant-Investing-strategies/)
# - Quantopia
#
# ### Satellite Image Analysis
#
# - [PlanetLabs](https://www.planet.com/) Notebooks
#
#
# [Image source](https://github.com/planetlabs/notebooks/blob/master/jupyter-notebooks/crop-classification/classify-cart-l8-ps.ipynb)
# ### Enable People to Unlock the Power of Math, Science, and Computing
#
#
#
# ### Next Up for itk-jupyter-widgets
#
# - Support image types other than itk.Image, NumPy Array, e.g. OpenCV Mat, ImageJ2 images, vtkImageData, ...
# - Rendering meshes and point clouds
# - Compression and dynamic downsampling
# - Documentation
# - Embedding
# - JupyterLab Support
#
# - Interactive
# - Accessible
# - Brings powerful ~~programming tools~~ people together
#
#
# [](http://trizpug.org/)
#
# https://twitter.com/thewtex
#
# Email: matt.mccormick@kitware.com
#