From 95a07b8085c20b91f647af2ee3a96f74069708de Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 17 Apr 2023 15:13:56 +0100 Subject: [PATCH 1/8] adding optional outline to plots --- openmc/universe.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/openmc/universe.py b/openmc/universe.py index 2ed96f4bcb..3743245b29 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -295,7 +295,7 @@ class Universe(UniverseBase): def plot(self, origin=(0., 0., 0.), width=(1., 1.), pixels=(200, 200), basis='xy', color_by='cell', colors=None, seed=None, - openmc_exec='openmc', axes=None, **kwargs): + openmc_exec='openmc', axes=None, outline=False, **kwargs): """Display a slice plot of the universe. Parameters @@ -332,6 +332,10 @@ class Universe(UniverseBase): **kwargs Keyword arguments passed to :func:`matplotlib.pyplot.imshow` + .. versionadded:: 0.13.4 + outline : bool + Whether outlines between color boundaries should be drawn + Returns ------- matplotlib.image.AxesImage @@ -396,6 +400,18 @@ class Universe(UniverseBase): height = pixels[0]*px/(params.top - params.bottom) fig.set_size_inches(width, height) + if outline: + combined_rgb = np.sum(img, 2) + + axes.contour( + combined_rgb.reshape(img.shape[0], img.shape[1]), + origin="upper", + colors="k", + linestyles="solid", + linewidths=1, + extent=(x_min, x_max, y_min, y_max), + ) + # Plot image and return the axes return axes.imshow(img, extent=(x_min, x_max, y_min, y_max), **kwargs) From ffaea6187404ede60f5c62d70ae387533e942c85 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 18 Apr 2023 15:10:49 +0100 Subject: [PATCH 2/8] converting rgb to int --- openmc/universe.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/openmc/universe.py b/openmc/universe.py index 3743245b29..6863511f8a 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -1,3 +1,4 @@ +import typing from abc import ABC, abstractmethod from collections import OrderedDict from collections.abc import Iterable @@ -20,6 +21,14 @@ from .plots import _SVG_COLORS from .surface import _BOUNDARY_TYPES +def get_int_from_rgb(rgb: typing.Tuple[int, int, int]) -> int: + """Converts a tuple of ints into a single int""" + red = rgb[0] + green = rgb[1] + blue = rgb[2] + return (red << 16) + (green << 8) + blue + + class UniverseBase(ABC, IDManagerMixin): """A collection of cells that can be repeated. @@ -401,10 +410,14 @@ class Universe(UniverseBase): fig.set_size_inches(width, height) if outline: - combined_rgb = np.sum(img, 2) + + image_value = [ + [get_int_from_rgb(inner_entry) for inner_entry in outer_entry] + for outer_entry in img + ] axes.contour( - combined_rgb.reshape(img.shape[0], img.shape[1]), + image_value, #combined_rgb.reshape(img.shape[0], img.shape[1]), origin="upper", colors="k", linestyles="solid", @@ -988,4 +1001,4 @@ class DAGMCUniverse(UniverseBase): clone.volume = self.volume clone.auto_geom_ids = self.auto_geom_ids clone.auto_mat_ids = self.auto_mat_ids - return clone \ No newline at end of file + return clone From f1ce0913b3045c132cf9a92f5270c8644d1c6e17 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 18 Apr 2023 15:37:07 +0100 Subject: [PATCH 3/8] remove comment --- openmc/universe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/universe.py b/openmc/universe.py index 6863511f8a..3ad17778e6 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -417,7 +417,7 @@ class Universe(UniverseBase): ] axes.contour( - image_value, #combined_rgb.reshape(img.shape[0], img.shape[1]), + image_value, origin="upper", colors="k", linestyles="solid", From 37f9bdfaca37d2a0ddfd152b152ccd414d3183ea Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 Apr 2023 08:07:41 +0100 Subject: [PATCH 4/8] [skip ci] improve doc string from review Co-authored-by: Paul Romano --- openmc/universe.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openmc/universe.py b/openmc/universe.py index 3ad17778e6..f35b1d364e 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -341,10 +341,11 @@ class Universe(UniverseBase): **kwargs Keyword arguments passed to :func:`matplotlib.pyplot.imshow` - .. versionadded:: 0.13.4 outline : bool Whether outlines between color boundaries should be drawn + .. versionadded:: 0.13.4 + Returns ------- matplotlib.image.AxesImage From 2487c52d6e75e0fac6e264503fc34fcd8ec51c88 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 Apr 2023 12:00:31 +0100 Subject: [PATCH 5/8] working for libpng and no libpng users --- openmc/universe.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/openmc/universe.py b/openmc/universe.py index f35b1d364e..fd0d6d44a4 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -21,14 +21,6 @@ from .plots import _SVG_COLORS from .surface import _BOUNDARY_TYPES -def get_int_from_rgb(rgb: typing.Tuple[int, int, int]) -> int: - """Converts a tuple of ints into a single int""" - red = rgb[0] - green = rgb[1] - blue = rgb[2] - return (red << 16) + (green << 8) + blue - - class UniverseBase(ABC, IDManagerMixin): """A collection of cells that can be repeated. @@ -412,10 +404,8 @@ class Universe(UniverseBase): if outline: - image_value = [ - [get_int_from_rgb(inner_entry) for inner_entry in outer_entry] - for outer_entry in img - ] + rgb = (img * 256).astype(int) + image_value = (rgb[..., 0] << 16) + (rgb[..., 1] << 8) + (rgb[..., 2]) axes.contour( image_value, From 90e7e1d3f06f6074b7574272cc0d04fae8f254ef Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 Apr 2023 12:22:46 +0100 Subject: [PATCH 6/8] added outline to plot testing --- tests/unit_tests/test_plots.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/unit_tests/test_plots.py b/tests/unit_tests/test_plots.py index 183341c26e..ee3b2c1e27 100644 --- a/tests/unit_tests/test_plots.py +++ b/tests/unit_tests/test_plots.py @@ -12,6 +12,7 @@ def myplot(): plot.filename = 'myplot' plot.type = 'slice' plot.basis = 'yz' + plot.outline = True plot.background = 'black' plot.background = (0, 0, 0) @@ -59,26 +60,31 @@ def myprojectionplot(): plot.overlap_color = (255, 211, 0) plot.overlap_color = 'yellow' - + plot.wireframe_thickness = 2 plot.level = 1 return plot + def test_attributes(myplot): assert myplot.name == 'myplot' + def test_attributes_proj(myprojectionplot): assert myprojectionplot.name == 'myprojectionplot' + def test_repr(myplot): r = repr(myplot) assert isinstance(r, str) + def test_repr_proj(myprojectionplot): r = repr(myprojectionplot) assert isinstance(r, str) + def test_from_geometry(): width = 25. s = openmc.Sphere(r=width/2, boundary_type='vacuum') @@ -155,8 +161,8 @@ def test_plots(run_in_tmpdir): # from_xml new_plots = openmc.Plots.from_xml() - assert len(plots) - assert plots[0].origin == p1.origin - assert plots[0].colors == p1.colors - assert plots[0].mask_components == p1.mask_components - assert plots[1].origin == p2.origin + assert len(new_plots) + assert new_plots[0].origin == p1.origin + assert new_plots[0].colors == p1.colors + assert new_plots[0].mask_components == p1.mask_components + assert new_plots[1].origin == p2.origin From 887cfd2e78fe3bd29c614e7d03437bd1a7c49b16 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 Apr 2023 15:59:55 +0100 Subject: [PATCH 7/8] [skip ci] review improvment from Paul Co-authored-by: Paul Romano --- openmc/universe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/universe.py b/openmc/universe.py index fd0d6d44a4..4f36c3d85e 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -403,7 +403,7 @@ class Universe(UniverseBase): fig.set_size_inches(width, height) if outline: - + # Combine R, G, B values into a single int rgb = (img * 256).astype(int) image_value = (rgb[..., 0] << 16) + (rgb[..., 1] << 8) + (rgb[..., 2]) From a8e7d3855585b11c2fc2296c0a856b54064d4761 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 Apr 2023 16:02:07 +0100 Subject: [PATCH 8/8] moved plot outline test, added levels --- openmc/universe.py | 1 + tests/unit_tests/test_plots.py | 1 - tests/unit_tests/test_universe.py | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/openmc/universe.py b/openmc/universe.py index 4f36c3d85e..4054485fda 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -413,6 +413,7 @@ class Universe(UniverseBase): colors="k", linestyles="solid", linewidths=1, + levels=np.unique(image_value), extent=(x_min, x_max, y_min, y_max), ) diff --git a/tests/unit_tests/test_plots.py b/tests/unit_tests/test_plots.py index ee3b2c1e27..0dddc827cc 100644 --- a/tests/unit_tests/test_plots.py +++ b/tests/unit_tests/test_plots.py @@ -12,7 +12,6 @@ def myplot(): plot.filename = 'myplot' plot.type = 'slice' plot.basis = 'yz' - plot.outline = True plot.background = 'black' plot.background = (0, 0, 0) diff --git a/tests/unit_tests/test_universe.py b/tests/unit_tests/test_universe.py index a70bd46673..1bfbbf7ff8 100644 --- a/tests/unit_tests/test_universe.py +++ b/tests/unit_tests/test_universe.py @@ -59,6 +59,7 @@ def test_plot(run_in_tmpdir, sphere_model): pixels=(10, 10), color_by='material', colors=colors, + outline=True, )