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 dad92d9
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 436 deletions.
167 changes: 61 additions & 106 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,118 +24,36 @@
},
{
"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"
]
}
],
"metadata": {},
"outputs": [],
"source": [
"viewer = XArrayViewer()\n",
"await viewer.ui.ready"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"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.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",
Expand All @@ -146,47 +64,84 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5f993b87-c96c-496b-a819-f08cb2c8d06a",
"id": "d5a338fe-6997-4f2e-a237-d972415d1249",
"metadata": {},
"outputs": [],
"source": []
"source": [
"import ipywidgets as widgets\n",
"\n",
"source = viewer.source\n",
"\n",
"def update_level(change):\n",
" source.slicing = {\n",
" **source.slicing,\n",
" source.z: change.new, \n",
" }\n",
" viewer.reset_camera()\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",
"\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": "6646794d-151a-4e3f-9e3a-b0fad4451719",
"metadata": {},
"id": "ad74227e-ea49-44fc-89d7-f4a5c95b5cf6",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from pan3d.xarray.algorithm import vtkXArrayRectilinearSource\n",
"\n",
"algo = vtkXArrayRectilinearSource(dataset)"
"print(source)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3bb5924-a1f4-4f0b-8f14-77ccbc66b251",
"id": "6646794d-151a-4e3f-9e3a-b0fad4451719",
"metadata": {},
"outputs": [],
"source": [
"viewer.show(algo)"
"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",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e52221f9-3620-4605-9dae-26747474337f",
"id": "c3bb5924-a1f4-4f0b-8f14-77ccbc66b251",
"metadata": {},
"outputs": [],
"source": [
"print(viewer.source())\n"
"import pyvista as pv\n",
"\n",
"plotter = pv.Plotter()\n",
"mesh = pv.wrap(algo())\n",
"plotter.add_mesh(mesh, scalars=\"mag\", cmap='coolwarm')\n",
"plotter.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1db60e3-5642-4580-aac5-66646f285544",
"id": "c4199a7e-024c-4610-aa42-6c8209968f66",
"metadata": {},
"outputs": [],
"source": []
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 dad92d9

Please sign in to comment.