Skip to content

Commit fbb209d

Browse files
authored
Merge pull request #393 from simpeg/repr-non-finalized-mesh
Allow `TreeMesh.__repr__` to run when non finalized
2 parents 4bfbf8a + 3ad452b commit fbb209d

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

discretize/tree_mesh.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ def __repr__(self):
291291
"{}: {:^13},{:^13}".format(dim_label[dim], n_vector[0], n_vector[-1])
292292
)
293293

294+
# Return partial information if mesh is not finalized
295+
if not self.finalized:
296+
top = f"\n {mesh_name} (non finalized)\n\n"
297+
return top + "\n".join(extent_display)
298+
294299
for i, line in enumerate(extent_display):
295300
if i == len(cell_display):
296301
cell_display.append(" " * (len(cell_display[0]) - 3 - len(line)))
@@ -315,14 +320,47 @@ def __repr__(self):
315320
def _repr_html_(self):
316321
"""HTML representation."""
317322
mesh_name = "{0!s}TreeMesh".format(("Oc" if self.dim == 3 else "Quad"))
323+
style = " style='padding: 5px 20px 5px 20px;'"
324+
dim_label = {0: "x", 1: "y", 2: "z"}
325+
326+
if not self.finalized:
327+
style_bold = '"font-weight: bold; font-size: 1.2em; text-align: center;"'
328+
style_regular = '"font-size: 1.2em; text-align: center;"'
329+
output = [
330+
"<table>", # need to close this tag
331+
"<tr>",
332+
f"<td style={style_bold}>{mesh_name}</td>",
333+
f"<td style={style_regular} colspan='2'>(non finalized)</td>",
334+
"</tr>",
335+
"<table>", # need to close this tag
336+
"<tr>",
337+
"<th></th>",
338+
f'<th {style} colspan="2">Mesh extent</th>',
339+
"</tr>",
340+
"<tr>",
341+
"<th></th>",
342+
f"<th {style}>min</th>",
343+
f"<th {style}>max</th>",
344+
"</tr>",
345+
]
346+
for dim in range(self.dim):
347+
n_vector = getattr(self, "nodes_" + dim_label[dim])
348+
output += [
349+
"<tr>",
350+
f"<td {style}>{dim_label[dim]}</td>",
351+
f"<td {style}>{n_vector[0]}</td>",
352+
f"<td {style}>{n_vector[-1]}</td>",
353+
"</tr>",
354+
]
355+
output += ["</table>", "</table>"]
356+
return "\n".join(output)
357+
318358
level_count = self._count_cells_per_index()
319359
non_zero_levels = np.nonzero(level_count)[0]
320-
dim_label = {0: "x", 1: "y", 2: "z"}
321360
h_gridded = self.h_gridded
322361
mins = np.min(h_gridded, axis=0)
323362
maxs = np.max(h_gridded, axis=0)
324363

325-
style = " style='padding: 5px 20px 5px 20px;'"
326364
# Cell level table:
327365
cel_tbl = "<table>\n"
328366
cel_tbl += "<tr>\n"

tests/tree/test_tree.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,5 +590,64 @@ def test_cell_locator(dim):
590590
assert found
591591

592592

593+
class TestRepr:
594+
"""
595+
Test repr methods on TreeMesh.
596+
597+
Check if no error is raised when calling repr methods on a finalized and
598+
non finalized meshes.
599+
"""
600+
601+
@pytest.fixture(params=["2D", "3D"])
602+
def mesh(self, request):
603+
"""Return a sample TreeMesh"""
604+
nc = 16
605+
if request.param == "2D":
606+
h = [nc, nc]
607+
origin = (-32.4, 245.4)
608+
else:
609+
h = [nc, nc, nc]
610+
origin = (-32.4, 245.4, 192.3)
611+
mesh = discretize.TreeMesh(h, origin, diagonal_balance=True)
612+
return mesh
613+
614+
def finalize(self, mesh):
615+
"""
616+
Finalize the sample tree mesh.
617+
"""
618+
origin = mesh.origin
619+
if mesh.dim == 2:
620+
p1 = (origin[0] + 0.4, origin[1] + 0.4)
621+
p2 = (origin[0] + 0.6, origin[1] + 0.6)
622+
mesh.refine_box(p1, p2, levels=5)
623+
else:
624+
p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7)
625+
p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9)
626+
mesh.refine_box(p1, p2, levels=5)
627+
mesh.finalize()
628+
629+
@pytest.mark.parametrize("finalize", [True, False])
630+
def test_repr(self, mesh, finalize):
631+
"""
632+
Test if __repr__ doesn't raise errors on any TreeMesh.
633+
"""
634+
if finalize:
635+
self.finalize(mesh)
636+
output = mesh.__repr__()
637+
assert type(output) is str
638+
assert len(output) != 0
639+
640+
@pytest.mark.parametrize("finalize", [True, False])
641+
def test_repr_html(self, mesh, finalize):
642+
"""
643+
Test if _repr_html_ doesn't raise errors on any TreeMesh.
644+
"""
645+
if finalize:
646+
self.finalize(mesh)
647+
output = mesh._repr_html_()
648+
assert type(output) is str
649+
assert len(output) != 0
650+
651+
593652
if __name__ == "__main__":
594653
unittest.main()

0 commit comments

Comments
 (0)