Skip to content

Commit

Permalink
ENH: Multi-component vmin, vmax
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jun 18, 2020
1 parent 93a6b74 commit aaf2d81
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
44 changes: 36 additions & 8 deletions itkwidgets/widget_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ class Viewer(ViewerParent):
help="Region of interest: [[lower_x, lower_y, lower_z), (upper_x, upper_y, upper_z]]")\
.tag(sync=True, **array_serialization)\
.valid(shape_constraints(2, 3))
vmin = CFloat(
vmin = List(trait=CFloat(),
default_value=None,
allow_none=True,
help="Value that maps to the minimum of image colormap.").tag(
sync=True)
vmax = CFloat(
vmax = List(trait=CFloat(),
default_value=None,
allow_none=True,
help="Value that maps to the maximum of image colormap.").tag(
Expand Down Expand Up @@ -345,6 +345,14 @@ def __init__(self, **kwargs): # noqa: C901
proposal = {'value': kwargs['cmap']}
cmap_list = self._validate_cmap(proposal)
kwargs['cmap'] = cmap_list
if 'vmin' in kwargs and kwargs['vmin'] is not None:
proposal = {'value': kwargs['vmin']}
vmin_list = self._validate_vmin(proposal)
kwargs['vmin'] = vmin_list
if 'vmax' in kwargs and kwargs['vmax'] is not None:
proposal = {'value': kwargs['vmax']}
vmax_list = self._validate_vmax(proposal)
kwargs['vmax'] = vmax_list
self.observe(self._on_geometries_changed, ['geometries'])
have_label_map = 'label_map' in kwargs and kwargs['label_map'] is not None
if have_label_map:
Expand Down Expand Up @@ -587,6 +595,26 @@ def _validate_cmap(self, proposal):
else:
return [value]

@validate('vmin')
def _validate_vmin(self, proposal):
value = proposal['value']
if value is None:
return None
elif isinstance(value, list):
return value
else:
return [value]

@validate('vmax')
def _validate_vmax(self, proposal):
value = proposal['value']
if value is None:
return None
elif isinstance(value, list):
return value
else:
return [value]

@validate('point_set_colors')
def _validate_point_set_colors(self, proposal):
value = proposal['value']
Expand Down Expand Up @@ -807,13 +835,13 @@ def view(image=None, # noqa: C901
label_map_blend : float, default: 0.5
Label map blend with intensity image, from 0.0 to 1.0.
vmin: float, default: None
Value that maps to the minimum of image colormap. Defaults to minimum of
the image pixel buffer.
vmin: list of floats, default: Minimum of the image pixel buffer
Value that maps to the minimum of image colormap. A single value
can be provided or a list for multi-component images.
vmax: float, default: None
Value that maps to the minimum of image colormap. Defaults to maximum of
the image pixel buffer.
vmax: list of floats, default: Maximum of the image pixel buffer
Value that maps to the minimum of image colormap. A single value can
be provided or a list for multi-component images.
cmap: list of strings
default:
Expand Down
34 changes: 24 additions & 10 deletions js/lib/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,13 +798,21 @@ const ViewerView = widgets.DOMWidgetView.extend({

const onChangeColorRange = (component, colorRange) => {
const vmin = this.model.get('vmin')
if (colorRange[0] !== vmin) {
this.model.set('vmin', colorRange[0])
if (vmin === null) {
vmin = []
}
if (colorRange[0] !== vmin[component]) {
vmin[component] = colorRange[0]
this.model.set('vmin', vmin)
this.model.save_changes()
}
const vmax = this.model.get('vmax')
if (colorRange[1] !== vmax) {
this.model.set('vmax', colorRange[1])
if (vmax === null) {
vmax = []
}
if (colorRange[1] !== vmax[component]) {
vmax[component] = colorRange[1]
this.model.set('vmax', vmax)
this.model.save_changes()
}
}
Expand Down Expand Up @@ -1514,18 +1522,24 @@ const ViewerView = widgets.DOMWidgetView.extend({
vmin_changed: function () {
const vmin = this.model.get('vmin')
if (vmin !== null && this.model.hasOwnProperty('itkVtkViewer')) {
const colorRange = this.model.itkVtkViewer.getColorRange(0).slice()
colorRange[0] = vmin
this.model.itkVtkViewer.setColorRange(0, colorRange)
const rendered_image = this.model.get('rendered_image')
for (let component = 0; component < rendered_image.imageType.components; component++) {
const colorRange = this.model.itkVtkViewer.getColorRange(component).slice()
colorRange[0] = vmin[component]
this.model.itkVtkViewer.setColorRange(component, colorRange)
}
}
},

vmax_changed: function () {
const vmax = this.model.get('vmax')
if (vmax !== null && this.model.hasOwnProperty('itkVtkViewer')) {
const colorRange = this.model.itkVtkViewer.getColorRange(0).slice()
colorRange[1] = vmax
this.model.itkVtkViewer.setColorRange(0, colorRange)
const rendered_image = this.model.get('rendered_image')
for (let component = 0; component < rendered_image.imageType.components; component++) {
const colorRange = this.model.itkVtkViewer.getColorRange(component).slice()
colorRange[1] = vmax[component]
this.model.itkVtkViewer.setColorRange(component, colorRange)
}
}
},

Expand Down

0 comments on commit aaf2d81

Please sign in to comment.