Skip to content

Commit

Permalink
Adding tests for ProjectionPlot roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Jan 7, 2025
1 parent 90012e5 commit 58cce6d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
17 changes: 10 additions & 7 deletions openmc/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,9 @@ def _read_xml_attributes(self, elem):
self.camera_position = get_elem_tuple(elem, "camera_position", float)
self.look_at = get_elem_tuple(elem, "look_at", float)

if elem.find("background") is not None:
self.background = get_elem_tuple(elem, "background")

# Set masking information
mask_elem = elem.find("mask")
if mask_elem is not None:
Expand Down Expand Up @@ -1367,28 +1370,28 @@ def from_xml_element(cls, elem):
"""

plot_id = int(elem.get("id"))
plot = cls(plot_id)
plot_name = get_text(elem, 'name', '')
plot = cls(plot_id, plot_name)
plot.type = "projection"

plot._read_xml_attributes(elem)

# Attempt to get wireframe thickness.May not be present
wireframe_thickness = elem.get("wireframe_thickness")
if wireframe_thickness:
plot.wireframe_thickness = int(wireframe_thickness)
wireframe_thickness = elem.find("wireframe_thickness")
if wireframe_thickness is not None:
plot.wireframe_thickness = int(wireframe_thickness.text)
wireframe_color = elem.get("wireframe_color")
if wireframe_color:
plot.wireframe_color = [int(item) for item in wireframe_color]

# Set plot colors
for color_elem in elem.findall("color"):
uid = color_elem.get("id")
plot.colors[uid] = get_elem_tuple(color_elem, "rgb")
uid = int(color_elem.get("id"))
plot.colors[uid] = tuple(int(i) for i in get_text(color_elem, 'rgb').split())
plot.xs[uid] = float(color_elem.get("xs"))

return plot


class PhongPlot(RayTracePlot):

def __init__(self, plot_id=None, name=''):
Expand Down
30 changes: 30 additions & 0 deletions tests/unit_tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import openmc.examples
import pytest

import openmc.plots


@pytest.fixture(scope='module')
def myplot():
Expand Down Expand Up @@ -117,6 +119,34 @@ def test_repr_proj(myprojectionplot):
r = repr(myprojectionplot)
assert isinstance(r, str)

def test_projection_plot_roundtrip(myprojectionplot):

elem = myprojectionplot.to_xml_element()

xml_plot = openmc.ProjectionPlot.from_xml_element(elem)

svg_colors = openmc.plots._SVG_COLORS

assert xml_plot.name == myprojectionplot.name
assert xml_plot.look_at == myprojectionplot.look_at
assert xml_plot.camera_position == myprojectionplot.camera_position
assert xml_plot.pixels == myprojectionplot.pixels
assert xml_plot.filename == myprojectionplot.filename
assert xml_plot.background == svg_colors[myprojectionplot.background]
assert xml_plot.color_by == myprojectionplot.color_by
expected_colors = {m.id: svg_colors[c] for m, c in myprojectionplot.colors.items()}
assert xml_plot.colors == expected_colors
# TODO: needs geometry information
# assert xml_plot.mask_components == myprojectionplot.mask_components
assert xml_plot.mask_background == svg_colors[myprojectionplot.mask_background]
# assert xml_plot.overlap_color == svg_colors[myprojectionplot.overlap_color]
assert xml_plot.wireframe_thickness == myprojectionplot.wireframe_thickness
assert xml_plot.level == myprojectionplot.level






def test_from_geometry():
width = 25.
Expand Down

0 comments on commit 58cce6d

Please sign in to comment.