Skip to content

Commit

Permalink
Added initial paths to Analyzing Meshes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbednar authored Mar 12, 2018
1 parent d4e804e commit f68cb52
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
15 changes: 11 additions & 4 deletions earthsim/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class LineCrossSection(param.Parameterized):
Distance between samples in meters. Used for interpolation
of the cross-section paths.""")

def __init__(self, obj, **params):
def __init__(self, obj, paths=None, **params):
super(LineCrossSection, self).__init__(**params)
self.obj = obj
self.path = Path([])
paths = [] if paths is None else paths
self.path = Path(paths, crs=ccrs.GOOGLE_MERCATOR)
self.path_stream = PolyDraw(source=self.path)
PolyEdit(source=self.path)
self.sections = Dynamic(self.obj, operation=self._sample,
Expand Down Expand Up @@ -72,7 +73,10 @@ def _sample(self, obj, data):
a variable number of elements batching must be enabled and
the legend_limit must be set to 0.
"""
path = self.path_stream.element
if self.path_stream.data is None:
path = self.path
else:
path = self.path_stream.element
if isinstance(obj, TriMesh):
vdim = obj.nodes.vdims[0]
else:
Expand Down Expand Up @@ -151,7 +155,10 @@ def _sample(self, obj, data):
Rasterizes the supplied object across times returning
an Image of the sampled data across time and distance.
"""
path = self.path_stream.element
if self.path_stream.data is None:
path = self.path
else:
path = self.path_stream.element
if isinstance(obj, TriMesh):
vdim = obj.nodes.vdims[0]
else:
Expand Down
96 changes: 53 additions & 43 deletions examples/user_guide/Analyzing_Meshes.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Very often there will be specific regions of a set of results that are the most of interest, and in those cases it can be helpful to pull out a particular region's data for visualization in a clearer way. The ``LineCrossSection`` and ``SurfaceCrossSection`` classes allow taking cross-sections of a mesh (or some other rasterizable object) along poly-lines drawn using the ``PolyDraw`` tool, letting you make an *x*/*z* or *x*/*z*/*t* plot from data laid out in *x*/*y*/*z* or *x*/*y*/*z*/*t* (respectively). These classes rasterize the data at a fixed resolution, and then sample the data at that resolution given the underlying polylines. The ``resolution`` may be defined in the units of the underlying coordinate system."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -28,22 +35,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook provides a demo of ``LineCrossSection`` and ``SurfaceCrossSection`` helpers which allows taking cross-sections of a mesh along linestrings drawn using the ``PolyDraw`` tool. The helper rasterize the data at a fixed resolution and then samples the data at the same resolution given the underlying line strings. The ``resolution`` may be defined in the units of the underlying\n",
"coordinate system."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1: A static TriMesh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a simple example we will pass the static TriMesh to the ``CrossSector`` and display it. If now draw poly lines on the plot (left) we can see the depth along the path appear on the right."
"## Example 1: A static TriMesh\n",
"\n",
"As a first simple example, we will pass a static TriMesh to the ``LineCrossSection`` and display it. We'll pass a couple of sample polylines to start with, and if a live Python process is available we will be able to see the depth along the paths appear on the right, for those paths and any more that are subsequently drawn by the user."
]
},
{
Expand All @@ -52,27 +46,29 @@
"metadata": {},
"outputs": [],
"source": [
"path = '../data/Chesapeake_and_Delaware_Bays.3dm'\n",
"tris, verts = read_3dm_mesh(path)\n",
"filename = '../data/Chesapeake_and_Delaware_Bays.3dm'\n",
"\n",
"cb_paths = [[(-8523594., 4588993.), (-8476533., 4578872.),\n",
" (-8449931., 4562126.), (-8435409., 4539747.)],\n",
" [(-8477265., 4544109.), (-8469725., 4430387.)]]\n",
" \n",
"tris, verts = read_3dm_mesh(filename)\n",
"points = gv.operation.project_points(gv.Points(verts, vdims=['z']))\n",
"trimesh = hv.TriMesh((tris, points))\n",
"\n",
"sector = LineCrossSection(trimesh)\n",
"sector.view()"
"sector1 = LineCrossSection(trimesh, cb_paths)\n",
"sector1.view()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2: A time-varying mesh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``CrossSector`` also allows working with time-varying meshes. Here we will modify the static TriMesh data with a time-varying random offset to demonstrate that the ``CrossSector`` works even for data that is evolving temporally."
"Each individual polyline (thick black connected line segments) on the left should result in one curve on the right, with a colored dot on that path corresponding to the color of the curve. The location of the dot will change as you hover in the plot on the right, allowing you to see which value in the curve corresponds to which location along the polyline.\n",
"\n",
"## Example 2: A time-varying mesh\n",
"\n",
"``LineCrossSection`` also allows working with time-varying meshes. Here we will modify the static TriMesh data with a time-varying random offset to demonstrate that the ``LineCrossSection`` also works for data that is evolving temporally."
]
},
{
Expand All @@ -81,21 +77,29 @@
"metadata": {},
"outputs": [],
"source": [
"fpath = '../data/SanDiego_Mesh/SanDiego.3dm'\n",
"tris, verts = read_3dm_mesh(fpath, skiprows=2)\n",
"sd_paths = [\n",
" [(-13037161., 3843454.), (-13041435., 3854275.), (-13045822., 3857996.),\n",
" (-13048664., 3857210.), (-13050429., 3855575.), (-13048385., 3849452.),\n",
" (-13048027., 3847896.)],\n",
" [(-13042975., 3850040.), (-13063919., 3844636.)]\n",
"]\n",
"\n",
"filename = '../data/SanDiego_Mesh/SanDiego.3dm'\n",
"tris, verts = read_3dm_mesh(filename, skiprows=2)\n",
"points = gv.operation.project_points(gv.Points(verts, vdims=['z'], crs=ccrs.UTM(11)))\n",
"trimesh = gv.TriMesh((tris, points))\n",
"\n",
"fpath = '../data/SanDiego_Mesh/SanDiego_ovl.dat'\n",
"dfs = read_mesh2d(fpath)\n",
"filename2 = '../data/SanDiego_Mesh/SanDiego_ovl.dat'\n",
"dfs = read_mesh2d(filename2)\n",
"\n",
"points = gv.operation.project_points(gv.Points((verts.x, verts.y), crs=ccrs.UTM(11)))\n",
"\n",
"def time_mesh(time):\n",
" depth_points = points.add_dimension('Velocity', 0, dfs[time].values[:, 0], vdim=True)\n",
" return gv.TriMesh((tris, depth_points), crs=ccrs.GOOGLE_MERCATOR)\n",
"\n",
"meshes = hv.DynamicMap(time_mesh, kdims='Time').redim.values(Time=sorted(dfs.keys()))"
"time_dim = hv.Dimension('Time', values=sorted(dfs.keys()), default=3600)\n",
"meshes = hv.DynamicMap(time_mesh, kdims=time_dim)"
]
},
{
Expand All @@ -104,32 +108,38 @@
"metadata": {},
"outputs": [],
"source": [
"sector = LineCrossSection(meshes, resolution=100)\n",
"sector.view(cmap=cm_n.rainbow, shade=True)"
"sector2 = LineCrossSection(meshes, sd_paths, resolution=100)\n",
"sector2.view(cmap=cm_n.rainbow_r, shade=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 3: Sampling a Surface"
"You can use the scrubber controls to animate both plots, showing both the map-based data and the curve cross sections over time.\n",
"\n",
"## Example 3: Sampling a Surface\n",
"\n",
"Instead of sampling individual Curve elements for each time as in example 2, we can take cross-sections across time, returning an Image plotting the sampled value across both distance and time. The ``SurfaceCrossSection`` class is a simple subclass of ``LineCrossSection`` that overrides the ``sample`` method to apply the sampling for all time values and return an ``Image``."
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Instead of sampling individual Curve elements for each time, we can also take cross-sections across time returning an Image plotting the sampled value across both distance and time. Here we will subclass the ``CrossSector`` and override the ``sample`` method to apply the sampling for all time values and return an ``Image``."
"sector3 = SurfaceCrossSection(meshes, sd_paths[:1], resolution=100)\n",
"sector3.view(cmap=cm_n.rainbow_r, shade=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"sector = SurfaceCrossSection(meshes, resolution=100)\n",
"sector.view(cmap=cm_n.rainbow, shade=False)"
"The scrubber will now only animate the plot on the left, as time has been laid out vertically on the plot on the right. \n",
"\n",
"If you have a running Python process and want to draw your own line, first delete the existing one, because an overlay of non-transparent images will only show the one on top."
]
}
],
Expand Down

0 comments on commit f68cb52

Please sign in to comment.