Repository navigation
Description
I've been working on a method to visualize MPAS-O output on a static plot in python as an alternative to the more time-consuming route of rendering viz in ParaView. I'm looking for a way to faithfully represent the grid without regridding.
It seems like datashader/holoviews/geoviews is the way to go, but I'm having trouble getting it to work fully. I'm wondering if anyone has used this route or has a solution.
Method 1: Datashader points and shade:
from colorcet import cwr
import datashader
import datashader.transfer_functions as tf
# Single time slice of air-sea CO2 flux.
ds = xr.open_dataset('MPAS-O.fgco2.nc')
verts = pd.DataFrame({'x': ds.lonCell, 'y': ds.latCell, 'z': ds.fgco2})
cvs = datashader.Canvas(plot_height=400,plot_width=400)
tf.shade(cvs.points(verts, 'x', 'y', agg=datashader.mean('z')), cmap=cwr,
)This produces a map by coloring each pixel with an RGB value. This isn't a faithful representation of the grid, and I haven't found an easy way to plot this on a map projection.
Method 2: Trimesh
This is ultimately what I'd like to get working. This can easily be drawn on a map projection using geoviews once it works.
import datashader
import datashader.transfer_functions as tf
import pandas as pd
import xarray as xr
ds = xr.open_dataset('MPAS-O.fgco2.nc')
mesh = xr.open_dataset('oRRS30to10v3.171128.nc')
# Set up table with coordinates for each `nCell` index as well as the data being plotted ('z')
verts = pd.DataFrame({'x': ds.lonCell, 'y': ds.latCell, 'z': ds.fgco2})
# Construct indices for each triangle.
cell0 = mesh.nCells # Base cell
cell1 = mesh.cellsOnCell.isel(maxEdges=1) - 1 # NE cell
cell2 = mesh.cellsOnCell.isel(maxEdges=2) - 1 # N cell
# Points to the triangle vertices (`verts`) for each triangle being plotted.
tris = pd.DataFrame({'v0': cell0, 'v1': cell1, 'v2': cell2})
cvs = datashader.Canvas(plot_height=400, plot_width=400)
tf.shade(cvs.trimesh(verts, tris))I tried also including all possible triangles, without success.
# Pairs of neighboring cell indices to construct triangles from.
PAIRS = [
(0,1),
(1,2),
(2,3),
(3,4),
(4,5),
(5,0)
]
cell0 = mesh.nCells
df = pd.DataFrame()
for idx in PAIRS:
cell1 = mesh.cellsOnCell.isel(maxEdges=idx[0]) - 1
cell2 = mesh.cellsOnCell.isel(maxEdges=idx[1]) - 1
tris = pd.DataFrame({'v0': cell0,
'v1': cell1,
'v2': cell2})
df = pd.concat([df, tris], ignore_index=True)
cvs = datashader.Canvas(plot_height=400,plot_width=400)
tf.shade(cvs.trimesh(verts, df), cmap=cwr)Any thoughts? The tris argument should be a three-column list of pointers to indices in verts to form a non-overlapping set of all triangles on the mesh.



Activity
bradyrx commentedon Jan 27, 2020
CC @xylar, @pwolfram
Also I can host these example files on a Google Bucket or something. This should be straightforward to assess with any arbitrary MPAS-O variable on any mesh.
xylar commentedon Jan 27, 2020
@bradyrx, I'd love to look at this more sometime soon but don't have time today. For now, I figured I'd point you to some viz stuff I have for another context that might be helpful. Here, I create a patch collection to reuse for many plots:

https://github.com/MPAS-Dev/MPAS-Model/blob/3e4d773acc4e4c76627124e1319e921dee8a908d/testing_and_setup/compass/ocean/isomip_plus/viz/plot.py#L667-L687
Here is an example of plotting that patch collection:
https://github.com/MPAS-Dev/MPAS-Model/blob/3e4d773acc4e4c76627124e1319e921dee8a908d/testing_and_setup/compass/ocean/isomip_plus/viz/plot.py#L583-L614
This method shows hexagons (etc.) of constant shade, which is what I wanted. Here's an example plot with apologies for the rainbow color map:
bradyrx commentedon Jan 28, 2020
Thanks @xylar for giving this some thought and your working solution here. I'll give that a try as a potential avenue, but it would be good for us to find some solutions with e.g.
holoviewsto add to user documentation. I'll try your solution when I have a moment, but need to focus on analysis for now.Here's a consolidated notebook with my attempts and the example output pulled from Google Storage. I have some running threads on gitter/twitter with folks familiar with this sort of thing so I'll point them here.
Notebook: https://nbviewer.jupyter.org/gist/bradyrx/fe329f47e9a85fb0adacefa113178b44
bradyrx commentedon Jan 28, 2020
I've updated it with a working version. I had to drop
-1partial cells. This still isn't exactly what I want but is getting in the right direction:https://gist.github.com/bradyrx/fe329f47e9a85fb0adacefa113178b44
xylar commentedon Jan 28, 2020
@bradyrx, it looks like you're essentially plotting the dual mesh (with cell centers as the vertices of triangles), is that correct? That's also what ParaView does but it drives me nuts for the reasons that I think you have found, too, that it leaves boundary gaps in the mesh that, in my view, shouldn't be there. I'd personally only be interested in pursuing a viz approach on the native grid if it plots cells as full polygons rather than the triangles on the dual mesh.
milenaveneziani commentedon Jan 28, 2020
just a quick note to say that I am following this and that it is great you are working on it @bradyrx.
I also do not like to tinker with paraview too much, so my simple solution at the moment is to just do a scatter plot..
bradyrx commentedon Jan 28, 2020
Correct -- I'm following this approach since
TriMeshis the only unstructured mesh option I've found in plotting packages so far. But I agree that it's not perfect.Agreed. In short, I want a static solution in python to plot the full polygons as we do in ParaView. The VTK extractor you wrote is awesome, but requires spending time to work with the GUI. In theory you could write some macros or use a VTK package on python, though. But I also stylistically would like to be able to leverage something like
matplotlib/cartopyto make plots for papers for instance. It's really nice to use thematplotlibAPI to zoom into a region, tile a bunch of small multiples, add the coastline, colorbar, etc. This is a lot of manual work to make it look nice from ParaView. (But note I use the ParaView route a lot to make nice movies, etc. and it will continue to be useful)Glad to hear it @milenaveneziani! Hopefully we can find a path forward here. CC @andsery005 who is working on this at NCAR for MPAS-A as well.
milenaveneziani commentedon Jan 28, 2020
Yes, I agree that it would be great to use
cartopy. Here is a zoom-in scatter plot I did recently for the Southern Ocean, usingcartopy. The only thing missing is the labeling of meridians and parallels, but that is something that is coming up in the next released version ofcartopy:bradyrx commentedon Jan 28, 2020
@milenaveneziani, that looks great. I think that's probably the best solution until we can find one that faithfully depicts the polygons. I had used scatter in the past for global checks, but hadn't tried zooming in. I imagine you just tweak the marker size until it doesn't overlap?
milenaveneziani commentedon Jan 28, 2020
exactly, and that's the problem. For one, every time you plot a different mesh, you have to re-do the tweaking. For two, the rendering isn't uniform, of course, and there is bound to be an overlap of cells/dots where resolution is higher.
So, just a temporary solution for sure.
bradyrx commentedon Jan 28, 2020
I spent a little bit of time with @xylar's patches approach:
This looks like it's going down the right road, but it does take 2.5 minutes to render the patch collection. Perhaps it can be saved out and reloaded for the viz? Also I need to figure out how to encode the lat/lon information in the patches collection so I can project, zoom in, etc.
xylar commentedon Jan 28, 2020
Okay, thanks for looking into it. That does sound too slow to be useful. I've only used it for idealized, small test cases.
milenaveneziani commentedon Jan 28, 2020
as @bradyrx says, we could save it though, right? Since it's only mesh dependent. 2.5min for the 30to10 doesn't sound like much to me if it's done only once.
1 remaining item
xylar commentedon Jan 31, 2020
@bradyrx, I'm going to give this some thought over the weekend. I'll let you know what I come up with.
xylar commentedon Feb 1, 2020
@bradyrx, I played with this a bit more. My ignorance of
datashader,holoviewsandgeoviewsknows no bounds, and I prefer typically to work outside of Jupyter notebooks because I'm usually designing viz to be reused from the command line. But these packages seem really hard to use, especially withbokeh, outside of a notebook.That being said, I had some success working from your Method 2 above:
xylar commentedon Feb 1, 2020
@bradyrx, if you can take what I've done and figure out how to store a reusable trimesh (e.g. for plotting different points in time or different field) and also how to use this properly with
geoviewsprojections, nice continent outlines, colorbars, etc., I would be very keen to have that.xylar commentedon Feb 1, 2020
I'm going to try another version that interpolates the given field between cell centers but still includes the triangles (with constant field value) that correspond to boundary edges. This should potentially be more aesthetic and less computationally intensive but I think both versions would ideally be available.
bradyrx commentedon Feb 2, 2020
That looks great @xylar! Yes of course the key is to get this onto a map projection with
geoviews. Thepyviz/pyvizgitter is very active and focuses on this viz stack (which I agree is hard to work with as a new user). So I can play around with getting this onto a projection then ask them for guidance if stuck.I have to focus the next two weeks on getting some plots/analysis ready for Ocean Sciences, so I likely will have to wait til after the meeting to get back on this.
xylar commentedon Feb 2, 2020
@bradyrx, good to know about
pyviz/pyviz. I'll check it out when I have time.xylar commentedon Feb 2, 2020
It the meantime, I figured out a potential approach to viz that's smoother but still renders polygons according to the MPAS-Ocean mesh. It's substantially more complex:
Some triangles are at the periodic boundary but they don't render because they have the wrong winding (a nice coincidence). If one cared about that, it would be necessary to make periodic copies of triangles that cross the boundary and fix the longitude where it's problematic.
bradyrx commentedon Mar 3, 2020
I may not be able to look at this for a bit, but I want to keep it open so we can continue to think about it. I think @xylar got us a long way there. The next goal is to figure out how to actually get it on a projection with zooming/tick labeling, etc. I'm not sure how straight forward that would be though.
xylar commentedon Mar 3, 2020
Thanks for keeping this alive.
bradyrx commentedon Mar 17, 2020
I just came across this due to some activity at
MPAS-QuickViz: https://github.com/MPAS-Dev/MPAS-QuickViz/blob/master/ocean/plotting_library/simple_plotting.py.plot_polylooks like a similar approach to what we're doing here with patches, but more concise. Are you familiar with this @xylar? Any thoughts on this @pwolfram?There's no docstring at all so I'm a little lost as to what is supposed to be input here. It also is going to loop through every cell so it's probably not feasible for 30to10, as we talked about with the patch approach here.
pwolfram commentedon Mar 17, 2020
It is for point-based output. We have other scripts that we've used for patch-based visualization in python, but largely I'm using ParaView these days for that type of output and then having specific diagnostics produce plots.
bradyrx commentedon Mar 17, 2020
@pwolfram, I think the key here is to be able to interactively make visualizations of the native mesh through python. I.e. we don't want to regrid to make
pcolor/contourplots withmatplotlib. And while paraview is super useful, it takes a lot longer to polish up a nice figure. It's tough for exploratory work.xylar commentedon Jul 3, 2020
I'm planning to work on this again over the weekend. I've been working on MPAS-Dev/MPAS-Analysis#586 and realized that I basically need to create a set of triangles that would be needed for this viz in order to do a proper job at transects on the MPAS-Ocean grid, too.
xylar commentedon Feb 3, 2023
Three years after the original post, here's a potential answer:
UXARRAY/uxarray#214