Skip to content

Commit

Permalink
feat(xarray): add slicing and compute in algo
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Oct 30, 2024
1 parent d379ce6 commit d1fe31c
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 446 deletions.
178 changes: 62 additions & 116 deletions examples/jupyter/test-xarray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "88a3053b-a228-4099-8bd8-d92abaae317b",
"metadata": {},
"outputs": [],
Expand All @@ -24,172 +24,118 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "41a4ebf1-8b90-4709-8d4c-06efb3afcc51",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/sebastien.jourdain/Documents/code/sbir/pan3d/.venv/lib/python3.10/site-packages/xarray/conventions.py:286: 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:286: 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:286: SerializationWarning: variable 'v' has non-conforming '_FillValue' np.float64(nan) defined, dropping '_FillValue' entirely.\n",
" var = coder.decode(var, name=name)\n"
]
}
],
"metadata": {},
"outputs": [],
"source": [
"dataset = open_dataset(\"eraint_uvz\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "54c88fe2-176a-4468-89e3-3dbf2979776a",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*iHat + v*jHat\n",
"ERROR:root:Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*iHat + v*jHat\n",
"ERROR:root:Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*u + v*v\n",
"ERROR:root:Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*u + v*v\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<vtkmodules.util.execution_model.Pipeline object at 0x146673010>\n"
]
}
],
"source": [
"viewer = XArrayViewer()\n",
"await viewer.ui.ready"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c78e6b6c-6ea0-4f46-ada1-8760ed86474f",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fbd9427e7e9e437fb5ac8e0de61dc9c3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='<iframe id=\"trame_trame__template_main\" src=\"http://localhost:55794/index.html?ui=main&reconnect=a…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[0m\u001b[31m2024-10-29 17:38:35.710 ( 33.601s) [ 1C7CDB]vtkExprTkFunctionParser:481 ERR| vtkExprTkFunctionParser (0x15c353e50): Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*iHat + v*jHat\n",
"\u001b[0m\n",
"\u001b[0m\u001b[31m2024-10-29 17:38:35.710 ( 33.601s) [ 1C7CDB]vtkExprTkFunctionParser:481 ERR| vtkExprTkFunctionParser (0x15c353e50): Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*iHat + v*jHat\n",
"\u001b[0m\n",
"\u001b[0m\u001b[33m2024-10-29 17:38:35.711 ( 33.602s) [ 1C7CDB] vtkArrayCalculator.cxx:581 WARN| vtkArrayCalculator (0x1420eb080): An error occurred when parsing the calculator's function. See previous errors.\u001b[0m\n",
"\u001b[0m\u001b[31m2024-10-29 17:38:35.711 ( 33.602s) [ 1C7CDB]vtkExprTkFunctionParser:481 ERR| vtkExprTkFunctionParser (0x15c3632a0): Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*u + v*v\n",
"\u001b[0m\n",
"\u001b[0m\u001b[31m2024-10-29 17:38:35.711 ( 33.602s) [ 1C7CDB]vtkExprTkFunctionParser:481 ERR| vtkExprTkFunctionParser (0x15c3632a0): Err: 0 Type: [Syntax Error] Msg: ERR201 - Undefined symbol: 'u'\tExpression: u*u + v*v\n",
"\u001b[0m\n",
"\u001b[0m\u001b[33m2024-10-29 17:38:35.712 ( 33.603s) [ 1C7CDB] vtkArrayCalculator.cxx:581 WARN| vtkArrayCalculator (0x1420eb4a0): An error occurred when parsing the calculator's function. See previous errors.\u001b[0m\n"
]
}
],
"outputs": [],
"source": [
"viewer = XArrayViewer()\n",
"await viewer.ui.ready\n",
"viewer.xarray = dataset\n",
"\n",
"viewer.source.order = \"F\" \n",
"viewer.source.computed = {\n",
"viewer.computed = {\n",
" \"_use_scalars\": [\"u\", \"v\"], # optional if empty\n",
" \"_use_vectors\": [], # optional if empty\n",
" \"vec\": \"u*iHat + v*jHat\",\n",
" \"mag\": \"u*u + v*v\",\n",
"}\n",
"\n",
"viewer.ui"
"source = viewer.source"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f993b87-c96c-496b-a819-f08cb2c8d06a",
"id": "1a3db2e2-70f5-41f9-b429-bc0d9c1674ab",
"metadata": {},
"outputs": [],
"source": []
"source": [
"viewer.ui"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6646794d-151a-4e3f-9e3a-b0fad4451719",
"id": "c3bb5924-a1f4-4f0b-8f14-77ccbc66b251",
"metadata": {},
"outputs": [],
"source": [
"from pan3d.xarray.algorithm import vtkXArrayRectilinearSource\n",
"import pyvista as pv\n",
"\n",
"algo = vtkXArrayRectilinearSource(dataset)"
"plotter = pv.Plotter()\n",
"plotter.add_mesh(source, scalars=\"mag\", cmap='coolwarm')\n",
"plotter.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3bb5924-a1f4-4f0b-8f14-77ccbc66b251",
"id": "d5a338fe-6997-4f2e-a237-d972415d1249",
"metadata": {},
"outputs": [],
"source": [
"viewer.show(algo)"
"import ipywidgets as widgets\n",
"\n",
"def update_level(change):\n",
" source.slicing = {\n",
" **source.slicing,\n",
" source.z: change.new, \n",
" }\n",
" viewer.reset_camera()\n",
" plotter.render()\n",
"\n",
"def update_resolution(change):\n",
" v = source.slicing\n",
" v[source.x] = [0, -1, change.new]\n",
" v[source.y] = [0, -1, change.new]\n",
" source.slicing = v\n",
" viewer.reset_camera()\n",
" plotter.render()\n",
"\n",
"slider_level = widgets.IntSlider(value=0, min=0, max=(viewer.xarray[viewer.source.z].size - 1), step=1, description=viewer.source.z)\n",
"slider_level.observe(update_level, names='value')\n",
"slider_resolution = widgets.IntSlider(value=1, min=1, max=10, step=1, description=\"resolution\")\n",
"slider_resolution.observe(update_resolution, names='value')\n",
"widgets.HBox([slider_level, slider_resolution])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e52221f9-3620-4605-9dae-26747474337f",
"metadata": {},
"id": "ad74227e-ea49-44fc-89d7-f4a5c95b5cf6",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print(viewer.source())\n"
"print(source)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1db60e3-5642-4580-aac5-66646f285544",
"id": "6646794d-151a-4e3f-9e3a-b0fad4451719",
"metadata": {},
"outputs": [],
"source": []
"source": [
"from pan3d.xarray.algorithm import vtkXArrayRectilinearSource\n",
"\n",
"algo = vtkXArrayRectilinearSource(dataset)\n",
"algo.computed = {\n",
" \"_use_scalars\": [\"u\", \"v\"], # optional if empty\n",
" \"_use_vectors\": [], # optional if empty\n",
" \"vec\": \"u*iHat + v*jHat\",\n",
" \"mag\": \"u*u + v*v\",\n",
"}"
]
}
],
"metadata": {
Expand Down
9 changes: 9 additions & 0 deletions pan3d/explorers/data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ def xarray(self, value):
self.actor.visibility = 1
self.reset_camera()

@property
def computed(self):
return self.source.computed

@computed.setter
def computed(self, value):
self.source.computed = value
self.refresh_data()

def update(self):
self.ctrl.view_update()

Expand Down
Loading

0 comments on commit d1fe31c

Please sign in to comment.