Merge pull request #2194 from shimwell/more_informative_error_message_writing_vtk

added mesh size and tally mean numbers to error message
This commit is contained in:
Patrick Shriwise 2022-08-30 11:13:41 -05:00 committed by GitHub
commit fa0f95642e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -216,8 +216,8 @@ class StructuredMesh(MeshBase):
Raises
------
RuntimeError
When the size of a dataset doesn't match the number of cells
ValueError
When the size of a dataset doesn't match the number of mesh cells
Returns
-------
@ -229,14 +229,17 @@ 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"
for label, dataset in datasets.items():
errmsg = (
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.dimension[0] * self.dimension[1]* self.dimension[2]:
raise RuntimeError(errmsg.format(label))
if not dataset.size == self.num_mesh_cells:
raise ValueError(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 ValueError(errmsg)
cv.check_type('label', label, str)
vtk_grid = vtk.vtkStructuredGrid()

View file

@ -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,6 +73,12 @@ 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"
with pytest.raises(RuntimeError, match=expected_error_msg):
mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})
# 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}\)"
)
with pytest.raises(ValueError, match=expected_error_msg):
mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})