From 5f13dec3d0fedb0af329fd727e57e0ff3d6740bb Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Fri, 21 Feb 2025 10:27:51 -0800 Subject: [PATCH 1/6] Allow `TreeMesh.__repr__` to run when non finalized Allow `TreeMesh.__repr__` and `TreeMesh._repr_html_` to run even when the mesh has not been finalized yet. This avoid an error to be raised, and provides minimal information about the mesh extent. --- discretize/tree_mesh.py | 42 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/discretize/tree_mesh.py b/discretize/tree_mesh.py index cfa817a90..17cc18b5a 100644 --- a/discretize/tree_mesh.py +++ b/discretize/tree_mesh.py @@ -291,6 +291,11 @@ def __repr__(self): "{}: {:^13},{:^13}".format(dim_label[dim], n_vector[0], n_vector[-1]) ) + # Return partial information if mesh is not finalized + if not self.finalized: + top = f"\n {mesh_name} (non finalized)\n\n" + return top + "\n".join(extent_display) + for i, line in enumerate(extent_display): if i == len(cell_display): cell_display.append(" " * (len(cell_display[0]) - 3 - len(line))) @@ -315,14 +320,47 @@ def __repr__(self): def _repr_html_(self): """HTML representation.""" mesh_name = "{0!s}TreeMesh".format(("Oc" if self.dim == 3 else "Quad")) + style = " style='padding: 5px 20px 5px 20px;'" + dim_label = {0: "x", 1: "y", 2: "z"} + + if not self.finalized: + style_bold = '"font-weight: bold; font-size: 1.2em; text-align: center;"' + style_regular = '"font-size: 1.2em; text-align: center;"' + output = [ + "", # need to close this tag + "", + f"", + f"", + "", + "
{mesh_name}(non finalized)
", # need to close this tag + "", + "", + '', + "", + "", + "", + f"", + f"", + "", + ] + for dim in range(self.dim): + n_vector = getattr(self, "nodes_" + dim_label[dim]) + output += [ + "", + f"", + f"", + f"", + "", + ] + output += ["
Mesh extent
minmax
{dim_label[dim]}{n_vector[0]}{n_vector[-1]}
", ""] + return "\n".join(output) + level_count = self._count_cells_per_index() non_zero_levels = np.nonzero(level_count)[0] - dim_label = {0: "x", 1: "y", 2: "z"} h_gridded = self.h_gridded mins = np.min(h_gridded, axis=0) maxs = np.max(h_gridded, axis=0) - style = " style='padding: 5px 20px 5px 20px;'" # Cell level table: cel_tbl = "\n" cel_tbl += "\n" From 8c487fab5ee1ffc82a291694e622bdcc9692a37a Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Fri, 21 Feb 2025 10:31:51 -0800 Subject: [PATCH 2/6] Add tests --- tests/tree/test_tree.py | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/tree/test_tree.py b/tests/tree/test_tree.py index 6fd92936f..3ef707c25 100644 --- a/tests/tree/test_tree.py +++ b/tests/tree/test_tree.py @@ -590,5 +590,63 @@ def test_cell_locator(dim): assert found +class TestRepr: + """ + Test repr methods on TreeMesh. + + Check if no error is raised when calling repr methods on a finalized and + non finalized meshes. + """ + + @pytest.fixture(params=["2D", "3D"]) + def mesh(self, request): + """Return a sample TreeMesh""" + nc = 16 + if request.param == "2D": + h = [nc, nc] + origin = (-32.4, 245.4) + else: + h = [nc, nc, nc] + origin = (-32.4, 245.4, 192.3) + mesh = discretize.TreeMesh(h, origin, diagonal_balance=True) + return mesh + + def finalize(self, mesh): + """ + Finalize the sample tree mesh. + """ + origin = mesh.origin + if mesh.dim == 2: + p1 = (origin[0] + 0.4, origin[1] + 0.4) + p2 = (origin[0] + 0.6, origin[1] + 0.6) + mesh.refine_box(p1, p2, levels=5, finalize=True) + else: + p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7) + p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9) + mesh.refine_box(p1, p2, levels=5, finalize=True) + + @pytest.mark.parametrize("finalize", [True, False]) + def test_repr(self, mesh, finalize): + """ + Test if __repr__ doesn't raise errors on any TreeMesh. + """ + if finalize: + self.finalize(mesh) + output = mesh.__repr__() + assert type(output) is str + assert len(output) != 0 + + @pytest.mark.parametrize("finalize", [True, False]) + def test_repr_html(self, mesh, finalize): + """ + Test if _repr_html_ doesn't raise errors on any TreeMesh. + """ + if finalize: + self.finalize(mesh) + output = mesh._repr_html_() + assert type(output) is str + assert len(output) != 0 + + if __name__ == "__main__": unittest.main() From 0721528e6c5b9f37ae8aaf5e860fd9a90bfc94dc Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Fri, 21 Feb 2025 10:56:40 -0800 Subject: [PATCH 3/6] Use style variable --- discretize/tree_mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discretize/tree_mesh.py b/discretize/tree_mesh.py index 17cc18b5a..8eb78dc47 100644 --- a/discretize/tree_mesh.py +++ b/discretize/tree_mesh.py @@ -335,7 +335,7 @@ def _repr_html_(self): "
", # need to close this tag "", "", - '', + '', "", "", "", From 73a275ff9c8e4616801e7f3e9ef289cec40d71a8 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Fri, 21 Feb 2025 10:57:15 -0800 Subject: [PATCH 4/6] Use f-string --- discretize/tree_mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discretize/tree_mesh.py b/discretize/tree_mesh.py index 8eb78dc47..a9518862b 100644 --- a/discretize/tree_mesh.py +++ b/discretize/tree_mesh.py @@ -335,7 +335,7 @@ def _repr_html_(self): "
Mesh extentMesh extent
", # need to close this tag "", "", - '', + f'', "", "", "", From e8399f8806842507985acd66da638baac730d275 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 24 Feb 2025 14:32:43 -0800 Subject: [PATCH 5/6] Make sure to finalize mesh in utility function in tests --- tests/tree/test_tree.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tree/test_tree.py b/tests/tree/test_tree.py index 3ef707c25..7ee404144 100644 --- a/tests/tree/test_tree.py +++ b/tests/tree/test_tree.py @@ -624,6 +624,7 @@ def finalize(self, mesh): p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7) p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9) mesh.refine_box(p1, p2, levels=5, finalize=True) + mesh.finalize() @pytest.mark.parametrize("finalize", [True, False]) def test_repr(self, mesh, finalize): From 7fb6d24f9206ec453100e22327cd0c4176df2e07 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Mon, 24 Feb 2025 14:33:47 -0800 Subject: [PATCH 6/6] Refine and finalize in separate statements --- tests/tree/test_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tree/test_tree.py b/tests/tree/test_tree.py index 7ee404144..2a27fb19d 100644 --- a/tests/tree/test_tree.py +++ b/tests/tree/test_tree.py @@ -619,11 +619,11 @@ def finalize(self, mesh): if mesh.dim == 2: p1 = (origin[0] + 0.4, origin[1] + 0.4) p2 = (origin[0] + 0.6, origin[1] + 0.6) - mesh.refine_box(p1, p2, levels=5, finalize=True) + mesh.refine_box(p1, p2, levels=5) else: p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7) p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9) - mesh.refine_box(p1, p2, levels=5, finalize=True) + mesh.refine_box(p1, p2, levels=5) mesh.finalize() @pytest.mark.parametrize("finalize", [True, False])
Mesh extentMesh extent