Skip to content

Commit

Permalink
feat(slicer): explorer available as accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Dec 19, 2024
1 parent 7267aa6 commit 7058ade
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 39 deletions.
52 changes: 27 additions & 25 deletions examples/jupyter/xarray-preview.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"execution_count": 1,
"id": "87e682c0-d2d0-4991-8302-f876b310e982",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
},
"scrolled": true
},
"outputs": [
Expand Down Expand Up @@ -41,27 +37,48 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"id": "ce12be13-2544-4dd8-90e3-ff076604dadb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dd23320974a046288894080e986d46cb",
"model_id": "87e2ad7df494452e910de90d39082679",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<iframe id=\"pan3d_accessor_2_trame__template_main\" src=\"http://localhost:55486/index.html?ui=main&…"
"HTML(value='<iframe id=\"pan3d_accessor_1_trame__template_main\" src=\"http://localhost:56532/index.html?ui=main&…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"await ds.pan3d.preview"
"preview = ds.pan3d.preview\n",
"preview"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6ff48739-b9a2-435d-83bf-379a2d4d392d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'longitude': [0, 479], 'latitude': [0, 240], 'level': [0, 2]}\n",
"{'month': 1, 'longitude': [0, 480, 1], 'latitude': [0, 241, 1], 'level': [0, 3, 1]}\n"
]
}
],
"source": [
"print(preview.source.slice_extents)\n",
"print(preview.source.slices)"
]
},
{
Expand All @@ -74,25 +91,10 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "c4688d5b-333a-4138-a4c1-774eaab2202b",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0663dc8cbeb54d85949f5d410626f3bc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<iframe id=\"preview_trame__template_main\" src=\"http://localhost:55516/index.html?ui=main&reconnect…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"from pan3d.viewers.preview import XArrayViewer\n",
"\n",
Expand Down
122 changes: 122 additions & 0 deletions examples/jupyter/xarray-slicer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0a67097d-b562-444a-8c2c-ea383d46fa48",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/sebastien.jourdain/Documents/code/sbir/pan3d/.venv/lib/python3.10/site-packages/xarray/conventions.py:287: SerializationWarning: variable 'z' has non-conforming '_FillValue' np.float64(nan) defined, dropping '_FillValue' entirely.\n",
" var = coder.decode(var, name=name)\n",
"/Users/sebastien.jourdain/Documents/code/sbir/pan3d/.venv/lib/python3.10/site-packages/xarray/conventions.py:287: SerializationWarning: variable 'u' has non-conforming '_FillValue' np.float64(nan) defined, dropping '_FillValue' entirely.\n",
" var = coder.decode(var, name=name)\n",
"/Users/sebastien.jourdain/Documents/code/sbir/pan3d/.venv/lib/python3.10/site-packages/xarray/conventions.py:287: SerializationWarning: variable 'v' has non-conforming '_FillValue' np.float64(nan) defined, dropping '_FillValue' entirely.\n",
" var = coder.decode(var, name=name)\n"
]
}
],
"source": [
"import xarray as xr\n",
"\n",
"ds = xr.tutorial.load_dataset(\"eraint_uvz\")"
]
},
{
"cell_type": "markdown",
"id": "2bb6f93d-0005-4734-9af1-3db0ace2533b",
"metadata": {},
"source": [
"## Use XArray dataset accessor"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9a0c47e0-a635-4c95-8bac-382c4e9ab264",
"metadata": {},
"outputs": [],
"source": [
"slicer = ds.pan3d.slicer"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "84f3a34a-4d6b-4b64-b7a1-2cde5505672f",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "07124cd9886a4e8d82550fed74222412",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<iframe id=\"pan3d_accessor_1_trame__template_main\" src=\"http://localhost:56626/index.html?ui=main&…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"slicer"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c848393f-0022-41c7-b82a-46789b9acc78",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'month': 0}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"slicer.source.slices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a57244be-f5d0-4cff-a325-e92b968fa025",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
27 changes: 20 additions & 7 deletions pan3d/explorers/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from pan3d.xarray.algorithm import vtkXArrayRectilinearSource

from trame.app import get_server
from trame.app import get_server, asynchronous
from trame_client.widgets.core import TrameDefault
from trame.decorators import TrameApp, change
from trame.ui.vuetify3 import SinglePageWithDrawerLayout
Expand All @@ -54,7 +54,7 @@ class XArraySlicer:
using VTK while interacting with the slice in 2D or 3D.
"""

def __init__(self, source=None, server=None):
def __init__(self, xarray=None, source=None, server=None):
# trame setup
self.server = get_server(server)
if self.server.hot_reload:
Expand All @@ -65,23 +65,29 @@ def __init__(self, source=None, server=None):
parser.add_argument(
"--import-state",
help="Pass a string with this argument to specify a startup configuration. This value must be a local path to a JSON file which adheres to the schema specified in the [Configuration Files documentation](../api/configuration.md).",
required=(source is None),
required=(source is None and xarray is None),
)
args, _ = parser.parse_known_args()

# Check if we have what we need
config_file = Path(args.import_state) if args.import_state else None
if (config_file is None or not config_file.exists()) and source is None:
if (
(config_file is None or not config_file.exists())
and source is None
and xarray is None
):
parser.print_help()
sys.exit(0)

# Build Viz and UI
self.ui = None
self._setup_vtk(source, config_file)
self._setup_vtk(xarray, source, config_file)
self._build_ui()

def _setup_vtk(self, source=None, import_state=None):
if source is not None:
def _setup_vtk(self, xarray=None, source=None, import_state=None):
if xarray is not None:
self.source = vtkXArrayRectilinearSource(input=xarray)
elif source is not None:
self.source = source
elif import_state is not None:
self.source = vtkXArrayRectilinearSource()
Expand Down Expand Up @@ -786,6 +792,13 @@ def _build_ui(self, *args, **kwargs):
)
return layout

async def _async_display(self):
await self.ui.ready
self.ui._ipython_display_()

def _ipython_display_(self):
asynchronous.create_task(self._async_display())


def main():
app = XArraySlicer()
Expand Down
8 changes: 5 additions & 3 deletions pan3d/ui/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def __init__(self, source, update_rendering):
v3.VRangeSlider(
v_if="slice_x_type === 'range'",
prepend_icon="mdi-axis-x-arrow",
v_model=("slice_x_range", [0, 1]),
v_model=("slice_x_range", None),
min=("slice_extents[axis_names[0]][0]",),
max=("slice_extents[axis_names[0]][1]",),
step=1,
Expand Down Expand Up @@ -448,7 +448,7 @@ def __init__(self, source, update_rendering):
v3.VRangeSlider(
v_if="slice_y_type === 'range'",
prepend_icon="mdi-axis-y-arrow",
v_model=("slice_y_range", [0, 1]),
v_model=("slice_y_range", None),
min=("slice_extents[axis_names[1]][0]",),
max=("slice_extents[axis_names[1]][1]",),
step=1,
Expand Down Expand Up @@ -496,7 +496,7 @@ def __init__(self, source, update_rendering):
v3.VRangeSlider(
v_if="slice_z_type === 'range'",
prepend_icon="mdi-axis-z-arrow",
v_model=("slice_z_range", [0, 1]),
v_model=("slice_z_range", None),
min=("slice_extents[axis_names[2]][0]",),
max=("slice_extents[axis_names[2]][1]",),
step=1,
Expand Down Expand Up @@ -764,6 +764,8 @@ def on_change(self, slice_t, **_):
continue

if self.state[f"slice_{axis}_type"] == "range":
if self.state[f"slice_{axis}_range"] is None:
continue
slices[axis_name] = [
*self.state[f"slice_{axis}_range"],
int(self.state[f"slice_{axis}_step"]),
Expand Down
7 changes: 7 additions & 0 deletions pan3d/viewers/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ def save_dataset(self, file_path):
self.state.show_save_dialog = False
return asynchronous.create_task(self._save_dataset(file_path))

async def _async_display(self):
await self.ui.ready
self.ui._ipython_display_()

def _ipython_display_(self):
asynchronous.create_task(self._async_display())


# -----------------------------------------------------------------------------
# Main executable
Expand Down
11 changes: 7 additions & 4 deletions pan3d/xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ def __init__(self, xarray):
self.accessor_id = 0

@property
async def preview(self):
def preview(self):
from pan3d.viewers.preview import XArrayViewer

viewer = XArrayViewer(xarray=self.xarray, server=self.next_id())
await viewer.ui.ready
return XArrayViewer(xarray=self.xarray, server=self.next_id())

return viewer.ui
@property
def slicer(self):
from pan3d.explorers.slicer import XArraySlicer

return XArraySlicer(xarray=self.xarray, server=self.next_id())


@xr.register_dataarray_accessor("vtk")
Expand Down

0 comments on commit 7058ade

Please sign in to comment.