{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to create a nested hierarchical mesh?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geomstats.backend as gs\n", "from geomfum.dataset import NotebooksDataset\n", "from geomfum.shape import TriangleMesh\n", "from geomfum.shape.hierarchical import HierarchicalMesh, NestedHierarchicalMesh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Load mesh](00_load_mesh_from_file.ipynb)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO: Data has already been downloaded... using cached file ('C:\\Users\\giuli\\.geomfum\\data\\cat-00.off').\n" ] }, { "data": { "text/plain": [ "(7207, 14410)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset = NotebooksDataset()\n", "mesh = TriangleMesh.from_file(dataset.get_filename(\"cat-00\"))\n", "\n", "mesh.n_vertices, mesh.n_faces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a nested [hierarchical mesh](./11_hierarchical_mesh.ipynb)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\giuli\\OneDrive\\Research\\geomfum_proj\\geomfum\\geomfum\\wrap\\pyrmt.py:69: UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at C:\\actions-runner\\_work\\pytorch\\pytorch\\pytorch\\torch\\csrc\\utils\\tensor_numpy.cpp:209.)\n", " return TriangleMesh(gs.asarray(rlow.vertices), gs.asarray(rlow.triangles))\n" ] }, { "data": { "text/plain": [ "[302, 505, 1004, 7207]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min_n_samples = [1000, 500, 250]\n", "\n", "nested_hmesh = NestedHierarchicalMesh.from_hierarchical_shape(\n", " mesh, HierarchicalMesh.from_registry, min_n_samples=min_n_samples\n", ")\n", "\n", "nested_hmesh.n_vertices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access each mesh from low to high resolution with `nested_hmesh.meshes`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can transfer scalars from low-resolution to high-resolution." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[torch.Size([302]), (505,), (1004,), (7207,)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "low_scalar = gs.random.uniform(size=nested_hmesh.lowest.n_vertices)\n", "\n", "scalars = nested_hmesh.scalar_low_high(low_scalar)\n", "\n", "[scalar_.shape for scalar_ in scalars]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or stop at a particular level." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[torch.Size([302]), (505,), (1004,)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scalars = nested_hmesh.scalar_low_high(low_scalar, n_levels=2)\n", "\n", "[scalar_.shape for scalar_ in scalars]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this, we can extend a [low-resolution basis](./02_mesh_laplacian_spectrum.ipynb) (as above, we can stop at a particular level)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\giuli\\OneDrive\\Research\\geomfum_proj\\geomfum\\geomfum\\_backend\\pytorch\\sparse.py:22: UserWarning: Sparse CSC tensor support is in beta state. If you miss a functionality in the sparse tensor support, please submit a feature request to https://github.com/pytorch/pytorch/issues. (Triggered internally at C:\\actions-runner\\_work\\pytorch\\pytorch\\pytorch\\aten\\src\\ATen\\SparseCsrTensorImpl.cpp:55.)\n", " return _torch.sparse_csc_tensor(ccol_indices, row_indices, values, size=array.shape)\n" ] }, { "data": { "text/plain": [ "[,\n", " ,\n", " ,\n", " ]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nested_hmesh.lowest.laplacian.find_spectrum(spectrum_size=10, set_as_basis=True)\n", "\n", "nested_hmesh.extend_basis(set_as_basis=True)\n", "\n", "[mesh_.basis for mesh_ in nested_hmesh.meshes]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further reading\n", "\n", "* [How to use ReMatching to compute a functional map?](./13_rematching.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "VENV", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" }, "requires": [ "rematching" ] }, "nbformat": 4, "nbformat_minor": 2 }