Skip to content

Commit 7256d50

Browse files
shimwelljon-proximafusionpaulromano
authored
extra checks for pixels (#4009)
Co-authored-by: Jonathan Shimwell <jon@proximafusion.com> Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent 216651b commit 7256d50

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

openmc/model/model.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def __call__(self, val: float, **kwargs: Any) -> None:
3434
...
3535

3636

37+
def _check_pixels(pixels: int | Sequence[int]) -> None:
38+
if isinstance(pixels, Integral):
39+
check_greater_than('pixels', pixels, 0)
40+
else:
41+
check_length('pixels', pixels, 2)
42+
for p in pixels:
43+
check_greater_than('pixels', p, 0)
44+
45+
3746
class Model:
3847
"""Model container.
3948
@@ -1051,12 +1060,7 @@ def _set_plot_defaults(
10511060
pixels: int | Sequence[int],
10521061
basis: str
10531062
):
1054-
if isinstance(pixels, int):
1055-
check_greater_than('pixels', pixels, 0)
1056-
else:
1057-
check_length('pixels', pixels, 2)
1058-
for p in pixels:
1059-
check_greater_than('pixels', p, 0)
1063+
_check_pixels(pixels)
10601064

10611065
x, y, _ = _BASIS_INDICES[basis]
10621066

@@ -1220,6 +1224,8 @@ def slice_data(
12201224
"""
12211225
import openmc.lib
12221226

1227+
_check_pixels(pixels)
1228+
12231229
if width is not None and (u_span is not None or v_span is not None):
12241230
raise ValueError("width is mutually exclusive with u_span/v_span.")
12251231

openmc/plots.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,36 +383,36 @@ def id_map_to_rgb(
383383
"""
384384
# Initialize RGB array with white background (values between 0 and 1 for matplotlib)
385385
img = np.ones(id_map.shape, dtype=float)
386-
386+
387387
# Get the appropriate index based on color_by
388388
if color_by == 'cell':
389389
id_index = 0 # Cell IDs are in the first channel
390390
elif color_by == 'material':
391391
id_index = 2 # Material IDs are in the third channel
392392
else:
393393
raise ValueError("color_by must be either 'cell' or 'material'")
394-
394+
395395
# Get all unique IDs in the plot
396396
unique_ids = np.unique(id_map[:, :, id_index])
397-
397+
398398
# Generate default colors if not provided
399399
if colors is None:
400400
colors = {}
401-
401+
402402
# Convert colors dict to use IDs as keys
403403
color_map = {}
404404
for key, color in colors.items():
405405
if isinstance(key, (openmc.Cell, openmc.Material)):
406406
color_map[key.id] = color
407407
else:
408408
color_map[key] = color
409-
409+
410410
# Generate random colors for IDs not in color_map
411411
rng = np.random.RandomState(1)
412412
for uid in unique_ids:
413413
if uid > 0 and uid not in color_map:
414414
color_map[uid] = rng.randint(0, 256, (3,))
415-
415+
416416
# Apply colors to each pixel
417417
for uid in unique_ids:
418418
if uid == -1: # Background/void
@@ -432,7 +432,7 @@ def id_map_to_rgb(
432432
rgb = color
433433
mask = id_map[:, :, id_index] == uid
434434
img[mask] = np.array(rgb) / 255.0
435-
435+
436436
return img
437437

438438
class PlotBase(IDManagerMixin):

tests/unit_tests/test_model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,8 @@ def test_model_plot_invalid_inputs():
679679
model.plot(pixels=(0, 100))
680680
with pytest.raises(ValueError):
681681
model.plot(pixels=(100,))
682+
with pytest.raises(ValueError):
683+
model.slice_data(u_span=(2, 0, 0), v_span=(0, 2, 0), pixels=-1)
682684

683685

684686
def test_model_id_map_initialization(run_in_tmpdir):

0 commit comments

Comments
 (0)