Skip to content

Commit

Permalink
ENH: Support label_map_names
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jun 3, 2020
1 parent b3a0702 commit 4401035
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
42 changes: 34 additions & 8 deletions examples/LabelMap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -65,27 +65,53 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7416595a86114b29b9cdd40676af0e84",
"model_id": "37dcf1f9c89b4f9c8e7ab039d3f38dde",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Viewer(cmap='Grayscale', geometries=[], gradient_opacity=0.8, interpolation=False, point_sets=[], rendered_ima"
"Viewer(cmap='Grayscale', geometries=[], gradient_opacity=0.5, interpolation=False, label_map_names=[(0, 'Backg"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"view(image, label_map=label_map, rotate=True, gradient_opacity=0.8)"
"names = [(0, 'Background'), (1, 'First cell'), (2, 'Second cell')]\n",
"viewer = view(image,\n",
" label_map=label_map,\n",
" label_map_names=names,\n",
" rotate=True,\n",
" gradient_opacity=0.5,\n",
" slicing_planes=True)\n",
"viewer"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"names = [(0, 'Background'), (1, 'First cell'), (2, 'Second cell')]\n",
"viewer.label_map_names = names"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
13 changes: 12 additions & 1 deletion itkwidgets/widget_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import itk
import numpy as np
import ipywidgets as widgets
from traitlets import CBool, CFloat, CInt, Unicode, CaselessStrEnum, List, validate
from traitlets import CBool, CFloat, CInt, Unicode, CaselessStrEnum, List, validate, TraitError, Tuple
from ipydatawidgets import NDArray, array_serialization, shape_constraints
from .trait_types import ITKImage, ImagePointTrait, ImagePoint, PointSetList, PolyDataList, itkimage_serialization, image_point_serialization, polydata_list_serialization, Colormap

Expand Down Expand Up @@ -143,6 +143,12 @@ class Viewer(ViewerParent):
allow_none=True).tag(
sync=True,
**itkimage_serialization)
label_map_names = List(
trait=Tuple(),
allow_none=True,
default_value=None,
help="Names for labels in the label map.").tag(
sync=True)
interpolation = CBool(
default_value=True,
help="Use linear interpolation in slicing planes.").tag(sync=True)
Expand Down Expand Up @@ -649,6 +655,7 @@ def roi_slice(self):

def view(image=None, # noqa: C901
label_map=None, # noqa: C901
label_map_names=None, # noqa: C901
cmap=None,
select_roi=False,
interpolation=True,
Expand Down Expand Up @@ -720,6 +727,9 @@ def view(image=None, # noqa: C901
The 2D or 3D label map to visualize. If an image is also provided, the
label map must have the same size.
label_map_names : OrderedDict of (label_value, label_name)
String names associated with the integer label values.
vmin: float, optional, default: None
Value that maps to the minimum of image colormap. Defaults to minimum of
the image pixel buffer.
Expand Down Expand Up @@ -886,6 +896,7 @@ def view(image=None, # noqa: C901

viewer = Viewer(image=image,
label_map=label_map,
label_map_names=label_map_names,
cmap=cmap,
select_roi=select_roi,
interpolation=interpolation,
Expand Down
22 changes: 17 additions & 5 deletions js/lib/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import macro from 'vtk.js/Sources/macro'
const widgets = require('@jupyter-widgets/base')

const ANNOTATION_DEFAULT =
'<table style="margin-left: 0;"><tr><td style="margin-left: auto; margin-right: 0;">Index:</td><td>${iIndex},</td><td>${jIndex},</td><td>${kIndex}</td></tr><tr><td style="margin-left: auto; margin-right: 0;">Position:</td><td>${xPosition},</td><td>${yPosition},</td><td>${zPosition}</td></tr><tr><td style="margin-left: auto; margin-right: 0;"">Value:</td><td>${value}</td></tr></table>'
'<table style="margin-left: 0;"><tr><td style="margin-left: auto; margin-right: 0;">Index:</td><td>${iIndex},</td><td>${jIndex},</td><td>${kIndex}</td></tr><tr><td style="margin-left: auto; margin-right: 0;">Position:</td><td>${xPosition},</td><td>${yPosition},</td><td>${zPosition}</td></tr><tr><td style="margin-left: auto; margin-right: 0;"">Value:</td><td style="text-align:center;" colspan="3">${value}</td></tr><tr ${annotationLabelStyle}><td style="margin-left: auto; margin-right: 0;">Label:</td><td style="text-align:center;" colspan="3">${annotation}</td></tr></table>'
const ANNOTATION_CUSTOM_PREFIX =
'<table style="margin-left: 0;"><tr><td style="margin-left: auto; margin-right: 0;">Scale/Index:</td>'
const ANNOTATION_CUSTOM_POSTFIX =
'</tr><tr><td style="margin-left: auto; margin-right: 0;">Position:</td><td>${xPosition},</td><td>${yPosition},</td><td>${zPosition}</td></tr><tr><td style="margin-left: auto; margin-right: 0;"">Value:</td><td>${value}</td></tr></table>'
'</tr><tr><td style="margin-left: auto; margin-right: 0;">Position:</td><td>${xPosition},</td><td>${yPosition},</td><td>${zPosition}</td></tr><tr><td style="margin-left: auto; margin-right: 0;"">Value:</td><td style="text-align:center;" colspan="3">${value}</td></tr><tr ${annotationLabelStyle}><td style="margin-left: auto; margin-right: 0;">Label:</td><td style="text-align:center;" colspan="3">${annotation}</td></tr></table>'

const cores = navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4
const numberOfWorkers = cores + Math.floor(Math.sqrt(cores))
Expand Down Expand Up @@ -116,6 +116,7 @@ const ViewerModel = widgets.DOMWidgetModel.extend(
_view_module_version: '0.27.5',
rendered_image: null,
rendered_label_map: null,
label_map_names: null,
_rendering_image: false,
interpolation: true,
cmap: 'Viridis (matplotlib)',
Expand Down Expand Up @@ -719,6 +720,9 @@ const ViewerView = widgets.DOMWidgetView.extend({
this.select_roi_changed()
this.scale_factors_changed()
}
if (rendered_label_map) {
this.label_map_names_changed()
}

const onUserInterfaceCollapsedToggle = (collapsed) => {
if (collapsed !== this.model.get('ui_collapsed')) {
Expand Down Expand Up @@ -1044,6 +1048,7 @@ const ViewerView = widgets.DOMWidgetView.extend({
this.model.on('change:mode', this.mode_changed, this)
this.model.on('change:units', this.units_changed, this)
this.model.on('change:camera', this.camera_changed, this)
this.model.on('change:label_map_names', this.label_map_names_changed, this)

let toDecompress = []
const rendered_image = this.model.get('rendered_image')
Expand Down Expand Up @@ -1161,6 +1166,14 @@ const ViewerView = widgets.DOMWidgetView.extend({
return Promise.resolve(null)
},

label_map_names_changed: function () {
const label_map_names = this.model.get('label_map_names')
if (label_map_names && this.model.hasOwnProperty('itkVtkViewer')) {
const labelMapNames = new Map(label_map_names)
this.model.itkVtkViewer.setLabelMapNames(labelMapNames)
}
},

point_sets_changed: function () {
const point_sets = this.model.get('point_sets')
if (point_sets && !!point_sets.length) {
Expand Down Expand Up @@ -1524,7 +1537,7 @@ const ViewerView = widgets.DOMWidgetView.extend({
scaleFactors[1] === 1 &&
scaleFactors[2] === 1
) {
viewProxy.setCornerAnnotation('se', `${ANNOTATION_DEFAULT}`)
viewProxy.setSeCornerAnnotation(`${ANNOTATION_DEFAULT}`)
} else {
let scaleIndex = ''
if (scaleFactors[0] === 1) {
Expand All @@ -1542,8 +1555,7 @@ const ViewerView = widgets.DOMWidgetView.extend({
} else {
scaleIndex = `${scaleIndex}<td>${scaleFactors[2]}X</td>`
}
viewProxy.setCornerAnnotation(
'se',
viewProxy.setSeCornerAnnotation(
`${ANNOTATION_CUSTOM_PREFIX}${scaleIndex}${ANNOTATION_CUSTOM_POSTFIX}`
)
}
Expand Down
14 changes: 7 additions & 7 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
"copy-webpack-plugin": "^5.1.1",
"css-element-queries": "^1.2.3",
"itk": "^13.1.0",
"itk-vtk-viewer": "^10.0.0",
"itk-vtk-viewer": "^10.1.0",
"jupyter-dataserializers": "^2.2.0",
"mobx": "^5.15.4",
"regenerator-runtime": "^0.13.5",
"vtk.js": "^14.1.2"
"vtk.js": "^14.3.2"
},
"jupyterlab": {
"extension": "dist/labextension"
Expand Down

0 comments on commit 4401035

Please sign in to comment.