From fb6984e0a75a759badd50db0eb169f3de2970a8f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 25 Aug 2022 12:50:56 +0100 Subject: [PATCH 1/6] added numbers for size --- openmc/mesh.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index af830158c..54d7ba950 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -217,7 +217,7 @@ class StructuredMesh(MeshBase): Raises ------ RuntimeError - When the size of a dataset doesn't match the number of cells + When the size of a dataset doesn't match the number of mesh cells Returns ------- @@ -229,13 +229,14 @@ class StructuredMesh(MeshBase): from vtk.util import numpy_support as nps # check that the data sets are appropriately sized - errmsg = "The size of the dataset {} should be equal to the number of cells" + errmsg = "The size of the dataset {} ({}) should be equal to the number of mesh cells ({})" for label, dataset in datasets.items(): if isinstance(dataset, np.ndarray): - if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: - raise RuntimeError(errmsg.format(label)) + num_cells = self.dimension[0] * self.dimension[1] * self.dimension[2] + if not dataset.size == num_cells: + raise RuntimeError(errmsg.format(label, dataset.size, num_cells)) else: - if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + if len(dataset) == self.dimension[0] * self.dimension[1] * self.dimension[2]: raise RuntimeError(errmsg.format(label)) cv.check_type('label', label, str) From 19574d08ccd6b3fea9baba839248c604d8b7eb19 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 25 Aug 2022 14:25:48 +0100 Subject: [PATCH 2/6] [skip ci] review suggestion from @pshriwise Co-authored-by: Patrick Shriwise --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 54d7ba950..d58986285 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -229,7 +229,7 @@ class StructuredMesh(MeshBase): from vtk.util import numpy_support as nps # check that the data sets are appropriately sized - errmsg = "The size of the dataset {} ({}) should be equal to the number of mesh cells ({})" + errmsg = "The size of the dataset '{}' ({}) should be equal to the number of mesh cells ({})" for label, dataset in datasets.items(): if isinstance(dataset, np.ndarray): num_cells = self.dimension[0] * self.dimension[1] * self.dimension[2] From 2a903f8eb559c2e8b982712ffa867d46cc792484 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 25 Aug 2022 14:27:45 +0100 Subject: [PATCH 3/6] moving errmsg var closer to usage line --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index d58986285..0950d8dba 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -229,11 +229,11 @@ class StructuredMesh(MeshBase): from vtk.util import numpy_support as nps # check that the data sets are appropriately sized - errmsg = "The size of the dataset '{}' ({}) should be equal to the number of mesh cells ({})" for label, dataset in datasets.items(): if isinstance(dataset, np.ndarray): num_cells = self.dimension[0] * self.dimension[1] * self.dimension[2] if not dataset.size == num_cells: + errmsg = "The size of the dataset '{}' ({}) should be equal to the number of mesh cells ({})" raise RuntimeError(errmsg.format(label, dataset.size, num_cells)) else: if len(dataset) == self.dimension[0] * self.dimension[1] * self.dimension[2]: From cc667cc11a52157bc3228f1753d77111358e8bf6 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 25 Aug 2022 17:18:45 +0100 Subject: [PATCH 4/6] updated error message check --- openmc/mesh.py | 14 ++++++++------ tests/unit_tests/test_mesh_to_vtk.py | 7 +++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 0950d8dba..288d1527c 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -230,14 +230,16 @@ class StructuredMesh(MeshBase): # check that the data sets are appropriately sized for label, dataset in datasets.items(): + errmsg = ( + f"The size of the dataset '{label}' ({dataset.size}) " + f"should be equal to the number of mesh cells ({self.num_mesh_cells})" + ) if isinstance(dataset, np.ndarray): - num_cells = self.dimension[0] * self.dimension[1] * self.dimension[2] - if not dataset.size == num_cells: - errmsg = "The size of the dataset '{}' ({}) should be equal to the number of mesh cells ({})" - raise RuntimeError(errmsg.format(label, dataset.size, num_cells)) + if not dataset.size == self.num_mesh_cells: + raise RuntimeError(errmsg) else: - if len(dataset) == self.dimension[0] * self.dimension[1] * self.dimension[2]: - raise RuntimeError(errmsg.format(label)) + if len(dataset) == self.num_mesh_cells: + raise RuntimeError(errmsg) cv.check_type('label', label, str) vtk_grid = vtk.vtkStructuredGrid() diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index 26a393b41..a6bed7035 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -71,6 +71,9 @@ def test_write_data_to_vtk_size_mismatch(mesh): right_size = mesh.num_mesh_cells data = np.random.random(right_size + 1) - expected_error_msg = "The size of the dataset label should be equal to the number of cells" + expected_error_msg = ( + f"The size of the dataset 'label' ({len(data)}) should be equal to " + f"the number of mesh cells ({mesh.num_mesh_cells})" + ) with pytest.raises(RuntimeError, match=expected_error_msg): - mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data}) \ No newline at end of file + mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data}) From 922a5fcf5a4d41b12b2b001cb1100225b8687d73 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 26 Aug 2022 10:19:39 +0100 Subject: [PATCH 5/6] used escape chars to match regex --- openmc/mesh.py | 4 ++-- tests/unit_tests/test_mesh_to_vtk.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 288d1527c..1b37a4272 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -231,8 +231,8 @@ class StructuredMesh(MeshBase): # check that the data sets are appropriately sized for label, dataset in datasets.items(): errmsg = ( - f"The size of the dataset '{label}' ({dataset.size}) " - f"should be equal to the number of mesh cells ({self.num_mesh_cells})" + f"The size of the dataset '{label}' ({dataset.size}) should be" + f" equal to the number of mesh cells ({self.num_mesh_cells})" ) if isinstance(dataset, np.ndarray): if not dataset.size == self.num_mesh_cells: diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index a6bed7035..2c23a5298 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -28,6 +28,7 @@ spherical_mesh.r_grid = np.linspace(1, 2, num=30) spherical_mesh.phi_grid = np.linspace(0, np.pi, num=50) spherical_mesh.theta_grid = np.linspace(0, np.pi / 2, num=30) + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk(mesh, tmpdir): # BUILD @@ -43,7 +44,7 @@ def test_write_data_to_vtk(mesh, tmpdir): # read file reader = vtk.vtkStructuredGridReader() - reader.SetFileName(filename) + reader.SetFileName(str(filename)) reader.Update() # check name of datasets @@ -58,6 +59,7 @@ def test_write_data_to_vtk(mesh, tmpdir): assert nps.vtk_to_numpy(array1).size == data.size assert nps.vtk_to_numpy(array2).size == data.size + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk_size_mismatch(mesh): """Checks that an error is raised when the size of the dataset @@ -71,9 +73,12 @@ def test_write_data_to_vtk_size_mismatch(mesh): right_size = mesh.num_mesh_cells data = np.random.random(right_size + 1) + # Error message has \ in to escape characters that are otherwise recognized + # by regex. These are needed to make the test string match the error message + # string when using the match argument as that uses regular expression expected_error_msg = ( - f"The size of the dataset 'label' ({len(data)}) should be equal to " - f"the number of mesh cells ({mesh.num_mesh_cells})" + f"The size of the dataset 'label' \({len(data)}\) should be equal to " + f"the number of mesh cells \({mesh.num_mesh_cells}\)" ) with pytest.raises(RuntimeError, match=expected_error_msg): mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data}) From a3a28d094ac821fd5df9927b194087e9b223f061 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 26 Aug 2022 16:22:08 +0100 Subject: [PATCH 6/6] changing RunTimeError to ValueError --- openmc/mesh.py | 6 +++--- tests/unit_tests/test_mesh_to_vtk.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 1b37a4272..8ac4b1594 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -216,7 +216,7 @@ class StructuredMesh(MeshBase): Raises ------ - RuntimeError + ValueError When the size of a dataset doesn't match the number of mesh cells Returns @@ -236,10 +236,10 @@ class StructuredMesh(MeshBase): ) if isinstance(dataset, np.ndarray): if not dataset.size == self.num_mesh_cells: - raise RuntimeError(errmsg) + raise ValueError(errmsg) else: if len(dataset) == self.num_mesh_cells: - raise RuntimeError(errmsg) + raise ValueError(errmsg) cv.check_type('label', label, str) vtk_grid = vtk.vtkStructuredGrid() diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index 2c23a5298..d7d5907a2 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -80,5 +80,5 @@ def test_write_data_to_vtk_size_mismatch(mesh): f"The size of the dataset 'label' \({len(data)}\) should be equal to " f"the number of mesh cells \({mesh.num_mesh_cells}\)" ) - with pytest.raises(RuntimeError, match=expected_error_msg): + with pytest.raises(ValueError, match=expected_error_msg): mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})