hpgem/nanomesh

Improve documentation to calculate metrics from just generated mesh

Closed this issue · 4 comments

Laar commented

The notebook for mesh metrics show it with a mesh that it loads from file. Now I try to compute them from a just generated mesh without saving and loading it. The basic approach from the script is

# Image processing happening before this
mesher = Mesher2D(segmentation)
mesher.generate_contour(max_contour_dist=3)
# Some padding and setting region markers left out
mesh = mesher.triangulate(opts='pAq30a')
metrics.histogram(mesh, metric='max_angle')
# ax = metrics.plot2d(mesh, metric='max_angle')

As result it errors on making the histogram

Warning: Appending zeros to replace the missing geometrical tag data.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/var/folders/yp/w25rz7hs47n4mhxgqmbl8xch0000gn/T/ipykernel_64069/1301693132.py in <module>
      6 fig.set_size_inches(24,4)
      7 
----> 8 metrics.histogram(mesh_local_thresh_wave, metric='max_angle')
      9 ax = metrics.plot2d(mesh_local_thresh_wave, metric='max_angle')

/usr/local/Caskroom/miniconda/base/envs/nanomesh/lib/python3.8/site-packages/nanomesh/metrics.py in histogram(mesh, metric, ax, **kwargs)
    296 
    297     descriptor = _metric_dispatch[metric]
--> 298     quality = descriptor.func(mesh)  # type: ignore
    299 
    300     fig, ax = plt.subplots()

/usr/local/Caskroom/miniconda/base/envs/nanomesh/lib/python3.8/site-packages/nanomesh/metrics.py in __call__(self, mesh)
     23 
     24     def __call__(self, mesh: BaseMesh) -> np.ndarray:
---> 25         grid = mesh.to_pyvista_unstructured_grid()
     26         ret = grid.compute_cell_quality(self.metric)
     27         quality = ret.cell_data['CellQuality']

AttributeError: 'MeshContainer' object has no attribute 'to_pyvista_unstructured_grid'

This is working as intendend. You must specify which cell type you want to run the metrics on.

The triangulation always returns a MeshContainer, which includes line and triangle cells.

Try this:

triangle_mesh = mesh.get('triangle')
metrics.histogram(triangle_mesh, metric='max_angle')
Laar commented

That indeed works. Could it be useful to highlight/document this difference?

As idea, the metrics example starts with:

mesh = TriangleMesh.from_meshio(meshio.read('out.msh'))
mesh.prune_z_0()
mesh.plot()
....
metrics.histogram(mesh, metric='min_angle')

Which might not make it directly clear that TriangleMesh is quite different from the MeshContainer. The latter does contain multiple meshes, but in practice I do think of it like the mesh for the highest dimension.

Yeah, good point. That notebook needs some attention anyway. I don't think I updated it since I first made it 😅

I updated the notebook in #238, so I hope that addresses the issue. I will add some sort of workflow chart to the documentation (#241) to make the distinction between the different types.

Closed by #238