From 9586155ec8137c952c5c6b39baa498af3813a6ae Mon Sep 17 00:00:00 2001 From: javoha Date: Fri, 4 Dec 2026 15:30:33 +0100 Subject: [PATCH] 2D plots: draw lithology, fault domains and age masks without smoothing matplotlib smooths images by default ("antialiased") when they are shown at less than about 3 pixels per cell, and it averages the data values before colouring them. For lithology ids that creates formations that are not there: between the basement (0) and a young formation (5), the averages 1-4 are drawn as thin lines or dots of the formations in between. It shows up in flat models drawn to scale, e.g. the Weisweiler case study (110 x 95 cells, 28 km wide, 3.8 km deep: one pixel per cell in z), where the 2D section had red WalhornFM dots inside WilhelmineFM and thin lines of other formations along the boundaries. interpolation="nearest" for the categorical images: - plot_structural_model_2D: lithology - plot_fault_model_2D: fault domain map - StructuralFrame.plot_age_mask_section: age mask (True/False) Not changed: the scalar field section (continuous values) and the simulation contour plots. Tests: a section where the basement borders the youngest formation, drawn at under 3 pixels per cell, may only contain those two colours (fails without the fix); the three images use nearest. Co-Authored-By: Claude Opus 5.5 --- .../structural_modeling_visualization.py | 7 +++- .../structural_objects/structural_objects.py | 1 + .../test_visualization_2d.py | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/core/structural_modeling_components/structural_modeling_visualization/structural_modeling_visualization.py b/core/structural_modeling_components/structural_modeling_visualization/structural_modeling_visualization.py index c3735a08..780bc785 100644 --- a/core/structural_modeling_components/structural_modeling_visualization/structural_modeling_visualization.py +++ b/core/structural_modeling_components/structural_modeling_visualization/structural_modeling_visualization.py @@ -629,7 +629,11 @@ def plot_structural_model_2D( colors = [id_to_color[i] for i in sorted_ids] cmap = ListedColormap(colors) norm = BoundaryNorm(sorted_ids + [sorted_ids[-1] + 1], len(colors)) - ax.imshow(slice_lith, origin='lower', cmap=cmap, norm=norm, extent=extent, alpha=1, zorder=-100) + # interpolation='nearest': matplotlib otherwise smooths images shown at less than about 3 pixels + # per cell (flat models drawn to scale) and averages the lithology ids before colouring them, + # so formations that lie in between appear as thin lines or dots where they don't exist. + ax.imshow(slice_lith, origin='lower', cmap=cmap, norm=norm, extent=extent, alpha=1, zorder=-100, + interpolation='nearest') if show_grid: _draw_section_cells(ax, frame, dim, index) @@ -1110,6 +1114,7 @@ def plot_fault_model_2D( cmap=cmap, norm=norm, alpha=1.0, + interpolation="nearest", # domain ids are categories: no smoothing (see plot_structural_model_2D) ) cbar = fig.colorbar(im, ax=ax, ticks=domain_values) cbar.set_label("Fault Domain") diff --git a/core/structural_modeling_components/structural_objects/structural_objects.py b/core/structural_modeling_components/structural_objects/structural_objects.py index c5ac8a07..6568afbb 100644 --- a/core/structural_modeling_components/structural_objects/structural_objects.py +++ b/core/structural_modeling_components/structural_objects/structural_objects.py @@ -826,6 +826,7 @@ def plot_age_mask_section(self, group_nr: int = 0, axis: str = "y", index: int = vmin=0, vmax=1, aspect="equal", + interpolation="nearest", # a mask is True or False: no smoothed grey edges ) cbar = plt.colorbar(im, ax=ax, ticks=[0, 1]) diff --git a/tests/tests_structural_modeling_components/test_visualization_2d.py b/tests/tests_structural_modeling_components/test_visualization_2d.py index ba208dae..5bb50513 100644 --- a/tests/tests_structural_modeling_components/test_visualization_2d.py +++ b/tests/tests_structural_modeling_components/test_visualization_2d.py @@ -168,3 +168,44 @@ def test_2d_plots_return_their_own_figure_for_additions(results, monkeypatch): viz.plot_structural_model_2D(results["regular"]) assert shown == [True] # shown by default, as before + + +def _rendered_colors(fig, image) -> set: + """The colours matplotlib actually draws for an image at the figure's current size.""" + fig.canvas.draw() + rgba = image.make_image(fig.canvas.get_renderer(), unsampled=False)[0] + pixels = np.asarray(rgba).reshape(-1, 4) + return {tuple(p) for p in pixels[pixels[:, 3] > 0][:, :3]} + + +def test_lithology_section_shows_only_formations_of_the_section_when_drawn_small(results): + """Drawn at under 3 pixels per cell (e.g. a flat model drawn to scale), matplotlib smooths images by + default and averages the lithology ids, so formations in between would appear as thin lines where they + don't exist. Here the basement (0) borders the youngest formation directly: only these two colours may + be drawn.""" + frame = results["regular"].model_copy() + nx, ny, nz = frame.grid.resolution + youngest = max(int(v) for v in np.unique(results["regular"].lith_block)) + z = np.arange(nz)[None, None, :] + x = np.arange(nx)[:, None, None] + frame.lith_block = np.broadcast_to(np.where(z > nz / 2 + 2 * np.sin(x / 2.0), youngest, 0), + (nx, ny, nz)).astype(np.int32) + viz.plot_structural_model_2D(frame, axis="y", show_input_data=False, show_fault_contours=False) + fig = plt.gcf() + fig.set_size_inches(2.0, 1.2) + fig.set_dpi(30) # under 3 pixels per cell, where matplotlib would smooth + image = fig.axes[0].images[0] + allowed = {tuple((np.asarray(image.cmap(image.norm(v))[:3]) * 255).round().astype(int)) for v in (0, youngest)} + drawn = {tuple(int(c) for c in p) for p in _rendered_colors(fig, image)} + assert drawn <= allowed, f"colours of lithologies not in the section: {drawn - allowed}" + + +@pytest.mark.parametrize("kind", ["octree", "regular"]) +def test_categorical_2d_images_are_drawn_without_smoothing(results, kind): + frame = results[kind] + viz.plot_structural_model_2D(frame, axis="y") + assert plt.gcf().axes[0].images[0].get_interpolation() == "nearest" + viz.plot_fault_model_2D(frame.fault_frame, axis="y") + assert plt.gcf().axes[0].images[0].get_interpolation() == "nearest" + frame.plot_age_mask_section(group_nr=0, axis="x", index=3) + assert plt.gcf().axes[0].images[0].get_interpolation() == "nearest"