Skip to content
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

[VTK] Various fix #1406

Merged
merged 4 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions panel/pane/vtk/synchronizable_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ def lookupTableSerializer(parent, lookupTable, lookupTableId, context, depth):
'properties': {
'numberOfColors': lookupTable.GetNumberOfColors(),
'valueRange': lookupTableRange,
'range': lookupTableRange,
'hueRange': lookupTableHueRange,
# 'alphaRange': lutAlphaRange, # Causes weird rendering artifacts on client
'saturationRange': lutSatRange,
Expand Down
57 changes: 38 additions & 19 deletions panel/pane/vtk/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(self, object, **params):

@classmethod
def applies(cls, obj, **kwargs):
if 'vtk' not in sys.modules:
if 'vtk' not in sys.modules and 'vtkmodules' not in sys.modules:
return False
else:
import vtk
Expand All @@ -230,25 +230,36 @@ def get_renderer(self):
"""
return list(self.object.GetRenderers())[0]

def get_color_mappers(self):
cmaps = []
for view_prop in self.get_renderer().GetViewProps():
if view_prop.IsA('vtkScalarBarActor'):
rgba_arr = np.frombuffer(
memoryview(view_prop.GetLookupTable().GetTable()),
dtype=np.uint8
).reshape((-1, 4))
palette = [self._rgb2hex(*rgb) for rgb in rgba_arr[:,:3]]
low, high = view_prop.GetLookupTable().GetTableRange()
name = view_prop.GetTitle()
cmaps.append(
LinearColorMapper(low=low, high=high, name=name, palette=palette)
)
def _vtklut2bkcmap(self, lut, name):
table = lut.GetTable()
low, high = lut.GetTableRange()
rgba_arr = np.frombuffer(memoryview(table), dtype=np.uint8).reshape((-1, 4))
palette = [self._rgb2hex(*rgb) for rgb in rgba_arr[:,:3]]
return LinearColorMapper(low=low, high=high, name=name, palette=palette)

def get_color_mappers(self, infer=False):
if not infer:
cmaps = []
for view_prop in self.get_renderer().GetViewProps():
if view_prop.IsA('vtkScalarBarActor'):
name = view_prop.GetTitle()
lut = view_prop.GetLookupTable()
cmaps.append(self._vtklut2bkcmap(lut, name))
else:
infered_cmaps = {}
for actor in self.get_renderer().GetActors():
mapper = actor.GetMapper()
cmap_name = mapper.GetArrayName()
if cmap_name and cmap_name not in infered_cmaps:
lut = mapper.GetLookupTable()
infered_cmaps[cmap_name] = self._vtklut2bkcmap(lut, cmap_name)
cmaps = infered_cmaps.values()
return cmaps

@param.depends('color_mappers')
def _construct_colorbars(self):
color_mappers = self.color_mappers
def _construct_colorbars(self, color_mappers=None):
if not color_mappers:
color_mappers = self.color_mappers
from bokeh.models import Plot, ColorBar, FixedTicker
cbs = []
for color_mapper in color_mappers:
Expand All @@ -264,8 +275,13 @@ def _construct_colorbars(self):
[plot.add_layout(cb, 'below') for cb in cbs]
return plot

def construct_colorbars(self):
return Pane(self._construct_colorbars)
def construct_colorbars(self, infer=True):
if infer:
color_mappers = self.get_color_mappers(infer)
return Pane(self._construct_colorbars(color_mappers))
else:
return Pane(self._construct_colorbars)


def export_scene(self, filename='vtk_scene'):
if '.' not in filename:
Expand Down Expand Up @@ -306,6 +322,7 @@ def _serialize_ren_win(self, ren_win, context, binary=False, compression=True, e
import panel.pane.vtk.synchronizable_serializer as rws
if exclude_arrays is None:
exclude_arrays = []
store_offscreen_rendering = ren_win.GetOffScreenRendering()
ren_win.OffScreenRenderingOn() # to not pop a vtk windows
ren_win.Modified()
ren_win.Render()
Expand All @@ -314,6 +331,8 @@ def _serialize_ren_win(self, ren_win, context, binary=False, compression=True, e
arrays = {name: context.getCachedDataArray(name, binary=binary, compression=compression)
for name in context.dataArrayCache.keys()
if name not in exclude_arrays}
ren_win.Finalize()
ren_win.SetOffScreenRendering(store_offscreen_rendering)
return scene, arrays

@staticmethod
Expand Down
11 changes: 8 additions & 3 deletions panel/tests/pane/test_vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ def test_vtk_pane_more_complex(document, comm, tmp_path):
assert isinstance(model, VTKSynchronizedPlot)
assert pane._models[model.ref['id']][0] is model

colorbars = pane.construct_colorbars().object()
assert len(colorbars.below) == 3
assert all(isinstance(cb, ColorBar) for cb in colorbars.below)
colorbars_infered = pane.construct_colorbars().object

assert len(colorbars_infered.below) == 2 # infer only actor color bars
assert all(isinstance(cb, ColorBar) for cb in colorbars_infered.below)

colorbars_in_scene = pane.construct_colorbars(infer=False).object()
assert len(colorbars_in_scene.below) == 3
assert all(isinstance(cb, ColorBar) for cb in colorbars_in_scene.below)
# add axes
pane.axes = dict(
origin = [-5, 5, -2],
Expand Down