-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] FEATURE: Multivolume rendering for multi-dimensional data #186
base: master
Are you sure you want to change the base?
Changes from all commits
c8bb77a
47caedb
8de9b84
9bc0e9f
68b330e
89a6d79
a4d809c
2321bf7
f6b0986
2054248
3cf0733
11b1108
6aca2a7
678c9b8
18a1e3e
e1d09db
68fc135
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
from ipyvolume import utils | ||
from ipyvolume import examples | ||
from ipyvolume import headless | ||
|
||
from ipyvolume.transferfunction import linear_transfer_function | ||
|
||
_last_figure = None | ||
|
||
|
@@ -657,12 +657,11 @@ def recompute(*_ignore): | |
|
||
|
||
def volshow(data, lighting=False, data_min=None, data_max=None, | ||
max_shape=256, tf=None, stereo=False, | ||
max_shape=256, tf_colornames=None, stereo=False, | ||
ambient_coefficient=0.5, diffuse_coefficient=0.8, | ||
specular_coefficient=0.5, specular_exponent=5, | ||
downscale=1, | ||
level=[0.1, 0.5, 0.9], opacity=[0.01, 0.05, 0.1], level_width=0.1, | ||
controls=True, max_opacity=0.2, memorder='C', extent=None): | ||
controls=True, memorder='C', extent=None): | ||
"""Visualize a 3d array using volume rendering. | ||
|
||
Currently only 1 volume can be rendered. | ||
|
@@ -675,67 +674,71 @@ def volshow(data, lighting=False, data_min=None, data_max=None, | |
:param float data_min: minimum value to consider for data, if None, computed using np.nanmin | ||
:param float data_max: maximum value to consider for data, if None, computed using np.nanmax | ||
:parap int max_shape: maximum shape for the 3d cube, if larger, the data is reduced by skipping/slicing (data[::N]), set to None to disable. | ||
:param tf: transfer function (or a default one) | ||
:param tf_colornames: transfer function (or a default one) | ||
:param bool stereo: stereo view for virtual reality (cardboard and similar VR head mount) | ||
:param ambient_coefficient: lighting parameter | ||
:param diffuse_coefficient: lighting parameter | ||
:param specular_coefficient: lighting parameter | ||
:param specular_exponent: lighting parameter | ||
:param float downscale: downscale the rendering for better performance, for instance when set to 2, a 512x512 canvas will show a 256x256 rendering upscaled, but it will render twice as fast. | ||
:param level: level(s) for the where the opacity in the volume peaks, maximum sequence of length 3 | ||
:param opacity: opacity(ies) for each level, scalar or sequence of max length 3 | ||
:param level_width: width of the (gaussian) bumps where the opacity peaks, scalar or sequence of max length 3 | ||
:param bool controls: add controls for lighting and transfer function or not | ||
:param float max_opacity: maximum opacity for transfer function controls | ||
:param extent: list of [[xmin, xmax], [ymin, ymax], [zmin, zmax]] values that define the bounds of the volume, otherwise the viewport is used | ||
:return: | ||
""" | ||
fig = gcf() | ||
|
||
if tf is None: | ||
tf = transfer_function(level, opacity, level_width, controls=controls, max_opacity=max_opacity) | ||
if data.ndim == 3: # input data has only one channel | ||
data = np.expand_dims(data, -1) | ||
if tf_colornames is None: | ||
default_colors = ['red', 'green', 'blue', 'grey', 'cyan', 'magenta', 'yellow'] | ||
n_volumes = data.shape[-1] | ||
colors = default_colors[:n_volumes] | ||
if data_min is None: | ||
data_min = np.nanmin(data) | ||
if data_max is None: | ||
data_max = np.nanmax(data) | ||
if memorder is 'F': | ||
data = data.T | ||
|
||
if extent is None: | ||
extent = [(0, k) for k in data.shape[::-1]] | ||
|
||
extent = [(0, k) for k in data[..., -1].shape[::-1]] | ||
if extent: | ||
_grow_limits(*extent) | ||
|
||
vol = ipv.Volume(data_original = data, | ||
tf=tf, | ||
data_min = data_min, | ||
data_max = data_max, | ||
show_min = data_min, | ||
show_max = data_max, | ||
extent_original = extent, | ||
data_max_shape = max_shape, | ||
ambient_coefficient = ambient_coefficient, | ||
diffuse_coefficient = diffuse_coefficient, | ||
specular_coefficient = specular_coefficient, | ||
specular_exponent = specular_exponent, | ||
rendering_lighting = lighting) | ||
|
||
vol._listen_to(fig) | ||
data = np.moveaxis(data, -1, 0) # for more convenient looping | ||
for i, (subdata, color) in enumerate(zip(data, colors)): | ||
tf = linear_transfer_function(color) | ||
vol = ipv.Volume(data_original = subdata, | ||
tf=tf, | ||
data_min = data_min, | ||
data_max = data_max, | ||
show_min = data_min, | ||
show_max = data_max, | ||
extent_original = extent, | ||
data_max_shape = max_shape, | ||
ambient_coefficient = ambient_coefficient, | ||
diffuse_coefficient = diffuse_coefficient, | ||
specular_coefficient = specular_coefficient, | ||
specular_exponent = specular_exponent, | ||
rendering_lighting = lighting) | ||
|
||
vol._listen_to(fig) | ||
|
||
if controls: | ||
widget_opacity_scale = ipywidgets.FloatLogSlider(base=10, min=-2, max=2, | ||
description="opacity") | ||
widget_brightness = ipywidgets.FloatLogSlider(base=10, min=-1, max=1, | ||
description="brightness") | ||
ipywidgets.jslink((vol, 'opacity_scale'), (widget_opacity_scale, 'value')) | ||
ipywidgets.jslink((vol, 'brightness'), (widget_brightness, 'value')) | ||
widgets_bottom = [ipywidgets.HBox([widget_opacity_scale, widget_brightness])] | ||
current.container.children += tuple(widgets_bottom, ) | ||
|
||
fig.volumes = fig.volumes + [vol] | ||
|
||
return vol | ||
if controls: | ||
widget_opacity_scale = ipywidgets.FloatLogSlider(base=10, min=-2, max=2, | ||
description="opacity") | ||
widget_brightness = ipywidgets.FloatLogSlider(base=10, min=-1, max=1, | ||
description="brightness") | ||
widget_colorpicker = ipywidgets.ColorPicker(value=color, | ||
layout=ipywidgets.Layout(width='15%')) | ||
ipywidgets.jslink((vol, 'opacity_scale'), (widget_opacity_scale, 'value')) | ||
ipywidgets.jslink((vol, 'brightness'), (widget_brightness, 'value')) | ||
def change_transfer_function(vol, color): | ||
vol.tf = linear_transfer_function(color.new) | ||
widget_colorpicker.observe(lambda x, vol=vol: change_transfer_function(vol, x), names='value') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would create a new widget for each color picked, I think this is a good reason to make this happen on the javascript side. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to do that. What's the javascript equivalent of observe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is best to look at this implementation: |
||
|
||
widgets_bottom = [ipywidgets.HBox([widget_colorpicker, widget_opacity_scale, widget_brightness])] | ||
current.container.children += tuple(widgets_bottom, ) | ||
|
||
fig.volumes = fig.volumes + [vol] | ||
|
||
return fig | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm against returning figure, since it does not follow conventions/intuition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return the full list of volumes, then? I'll do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, check my above comment, why would volshow do this? Why not repeatedly call volshow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, for a quick chat, https://gitter.im/maartenbreddels/ipyvolume may be useful, and we could also do a videochat if that speeds things up. |
||
|
||
|
||
def save(filepath, makedirs=True, title=u'IPyVolume Widget', all_states=False, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about letting tf be a string or tuple, like in this PR where the icon can be a string or Icon widget.