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

Use ipydatawidgets.NDArrayWidget #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
xarray
numpy>=1.17
ipygany
ipydatawidgets
ipywidgets
ipython
23 changes: 14 additions & 9 deletions src/ipyfastscape/topoviz3d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Callable

import ipywidgets as widgets
from ipydatawidgets import NDArrayWidget
from ipygany import Component, IsoColor, PolyMesh, Scene, WarpByScalar

from .common import AppComponent, Coloring, VizApp
Expand Down Expand Up @@ -41,11 +42,13 @@ def _reset_canvas(self):
elev_da = self.dataset._widgets.elevation
elev_min = elev_da.min()
elev_max = elev_da.max()
elev_arr = self.dataset._widgets.current_elevation.values

self.warp_data = NDArrayWidget(self.dataset._widgets.current_elevation.values)
self.color_data = NDArrayWidget(self.dataset._widgets.current_color.values)

data = {
'color': [Component(name='value', array=elev_arr, min=elev_min, max=elev_max)],
'warp': [Component(name='value', array=elev_arr, min=elev_min, max=elev_max)],
'color': [Component(name='value', array=self.color_data, min=elev_min, max=elev_max)],
'warp': [Component(name='value', array=self.warp_data, min=elev_min, max=elev_max)],
}

self.polymesh = PolyMesh(vertices=vertices, triangle_indices=triangle_indices, data=data)
Expand All @@ -54,17 +57,19 @@ def _reset_canvas(self):
)
self.warp = WarpByScalar(self.isocolor, input='warp', factor=1)

# TODO: remove this when ipygany supports listening to NDArrayWidget.array changes
# see https://github.com/QuantStack/ipygany/issues/73
widgets.dlink((self.warp_data, 'array'), (self.polymesh[('warp', 'value')], 'array'))
widgets.dlink((self.color_data, 'array'), (self.polymesh[('color', 'value')], 'array'))

self.canvas = Scene([self.warp])

def _update_step(self):
new_warp_array = self.dataset._widgets.current_elevation.values
self.polymesh[('warp', 'value')].array = new_warp_array

new_color_array = self.dataset._widgets.current_color.values
self.polymesh[('color', 'value')].array = new_color_array
self.warp_data.array = self.dataset._widgets.current_elevation.values
self.color_data.array = self.dataset._widgets.current_color.values

def _update_scene_color_var(self):
self.polymesh[('color', 'value')].array = self.dataset._widgets.current_color
self.color_data.array = self.dataset._widgets.current_color

def _update_scene_color_range(self, da):
self.isocolor.min = da.min()
Expand Down