human-readable reporting

This commit is contained in:
Martin Diehl 2022-02-23 06:49:38 +01:00
parent ae2aa096fb
commit 59755c67ce
2 changed files with 65 additions and 10 deletions

View File

@ -459,6 +459,43 @@ class VTK:
def __repr__(self) -> str: def __repr__(self) -> str:
info = []
if isinstance(self.vtk_data,vtk.vtkImageData):
info.append('vtkImageData')
elif isinstance(self.vtk_data,vtk.vtkUnstructuredGrid):
info.append('vtkUnstructuredGrid')
elif isinstance(self.vtk_data,vtk.vtkPolyData):
info.append('vtkPolyData')
elif isinstance(self.vtk_data,vtk.vtkRectilinearGrid):
info.append('vtkRectilinearGrid')
for data in ['Cell Data', 'Point Data']:
if data == 'Cell Data': info.append(f'\n# cells: {self.N_cells}')
if data == 'Point Data': info.append(f'\n# points: {self.N_points}')
if data in self.labels:
info.append(f'with {data}:')
info += self.labels[data]
return util.srepr(info)
def __eq__(self,
other: object) -> bool:
"""
Test equality of other.
Parameters
----------
other : damask.VTK
VTK to compare self against.
"""
if not isinstance(other, VTK):
return NotImplemented
return self.as_ASCII() == other.as_ASCII()
def as_ASCII(self) -> str:
"""ASCII representation of the VTK data.""" """ASCII representation of the VTK data."""
writer = vtk.vtkDataSetWriter() writer = vtk.vtkDataSetWriter()
writer.SetHeader(f'# {util.execution_stamp("VTK")}') writer.SetHeader(f'# {util.execution_stamp("VTK")}')

View File

@ -33,27 +33,44 @@ class TestVTK:
monkeypatch.delenv('DISPLAY',raising=False) monkeypatch.delenv('DISPLAY',raising=False)
default.show() default.show()
def test_imageData(self,tmp_path):
cells = np.random.randint(5,10,3)
size = np.random.random(3) + 0.1
origin = np.random.random(3) - 0.5
v = VTK.from_image_data(cells,size,origin)
string = v.as_ASCII()
v.save(tmp_path/'imageData',False)
vtr = VTK.load(tmp_path/'imageData.vti')
with open(tmp_path/'imageData.vtk','w') as f:
f.write(string)
vtk = VTK.load(tmp_path/'imageData.vtk','VTK_imageData')
print(vtk)
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
def test_rectilinearGrid(self,tmp_path): def test_rectilinearGrid(self,tmp_path):
grid = np.sort(np.random.random((3,10))) grid = np.sort(np.random.random((3,10)))
v = VTK.from_rectilinear_grid(grid) v = VTK.from_rectilinear_grid(grid)
string = v.__repr__() string = v.as_ASCII()
v.save(tmp_path/'rectilinearGrid',False) v.save(tmp_path/'rectilinearGrid',False)
vtr = VTK.load(tmp_path/'rectilinearGrid.vtr') vtr = VTK.load(tmp_path/'rectilinearGrid.vtr')
with open(tmp_path/'rectilinearGrid.vtk','w') as f: with open(tmp_path/'rectilinearGrid.vtk','w') as f:
f.write(string) f.write(string)
vtk = VTK.load(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid') vtk = VTK.load(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid')
assert(string == vtr.__repr__() == vtk.__repr__()) print(vtk)
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
def test_polyData(self,tmp_path): def test_polyData(self,tmp_path):
points = np.random.rand(100,3) points = np.random.rand(100,3)
v = VTK.from_poly_data(points) v = VTK.from_poly_data(points)
string = v.__repr__() string = v.as_ASCII()
v.save(tmp_path/'polyData',False) v.save(tmp_path/'polyData',False)
vtp = VTK.load(tmp_path/'polyData.vtp') vtp = VTK.load(tmp_path/'polyData.vtp')
with open(tmp_path/'polyData.vtk','w') as f: with open(tmp_path/'polyData.vtk','w') as f:
f.write(string) f.write(string)
vtk = VTK.load(tmp_path/'polyData.vtk','polyData') vtk = VTK.load(tmp_path/'polyData.vtk','polyData')
assert(string == vtp.__repr__() == vtk.__repr__()) print(vtk)
assert(string == vtp.as_ASCII() == vtk.as_ASCII())
@pytest.mark.parametrize('cell_type,n',[ @pytest.mark.parametrize('cell_type,n',[
('VTK_hexahedron',8), ('VTK_hexahedron',8),
@ -66,13 +83,14 @@ class TestVTK:
nodes = np.random.rand(n,3) nodes = np.random.rand(n,3)
connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n) connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n)
v = VTK.from_unstructured_grid(nodes,connectivity,cell_type) v = VTK.from_unstructured_grid(nodes,connectivity,cell_type)
string = v.__repr__() string = v.as_ASCII()
v.save(tmp_path/'unstructuredGrid',False) v.save(tmp_path/'unstructuredGrid',False)
vtu = VTK.load(tmp_path/'unstructuredGrid.vtu') vtu = VTK.load(tmp_path/'unstructuredGrid.vtu')
with open(tmp_path/'unstructuredGrid.vtk','w') as f: with open(tmp_path/'unstructuredGrid.vtk','w') as f:
f.write(string) f.write(string)
vtk = VTK.load(tmp_path/'unstructuredGrid.vtk','unstructuredgrid') vtk = VTK.load(tmp_path/'unstructuredGrid.vtk','unstructuredgrid')
assert(string == vtu.__repr__() == vtk.__repr__()) print(vtk)
assert(string == vtu.as_ASCII() == vtk.as_ASCII())
def test_parallel_out(self,tmp_path): def test_parallel_out(self,tmp_path):
@ -96,7 +114,7 @@ class TestVTK:
fname_p = tmp_path/'plain.vtp' fname_p = tmp_path/'plain.vtp'
v.save(fname_c,parallel=False,compress=False) v.save(fname_c,parallel=False,compress=False)
v.save(fname_p,parallel=False,compress=True) v.save(fname_p,parallel=False,compress=True)
assert(VTK.load(fname_c).__repr__() == VTK.load(fname_p).__repr__()) assert(VTK.load(fname_c).as_ASCII() == VTK.load(fname_p).as_ASCII())
@pytest.mark.parametrize('fname',['a','a.vtp','a.b','a.b.vtp']) @pytest.mark.parametrize('fname',['a','a.vtp','a.b','a.b.vtp'])
@ -170,7 +188,7 @@ class TestVTK:
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.) masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
mask_auto = default.add(masked,'D') mask_auto = default.add(masked,'D')
mask_manual = default.add(np.where(masked.mask,masked.fill_value,masked),'D') mask_manual = default.add(np.where(masked.mask,masked.fill_value,masked),'D')
assert str(mask_manual) == str(mask_auto) assert mask_manual == mask_auto
@pytest.mark.parametrize('data_type,shape',[(float,(3,)), @pytest.mark.parametrize('data_type,shape',[(float,(3,)),
@ -203,7 +221,7 @@ class TestVTK:
polyData.save(ref_path/'polyData') polyData.save(ref_path/'polyData')
else: else:
reference = VTK.load(ref_path/'polyData.vtp') reference = VTK.load(ref_path/'polyData.vtp')
assert polyData.__repr__() == reference.__repr__() and \ assert polyData.as_ASCII() == reference.as_ASCII() and \
np.allclose(polyData.get('coordinates'),points) np.allclose(polyData.get('coordinates'),points)
@pytest.mark.xfail(int(vtk.vtkVersion.GetVTKVersion().split('.')[0])<8, reason='missing METADATA') @pytest.mark.xfail(int(vtk.vtkVersion.GetVTKVersion().split('.')[0])<8, reason='missing METADATA')
@ -221,5 +239,5 @@ class TestVTK:
rectilinearGrid.save(ref_path/'rectilinearGrid') rectilinearGrid.save(ref_path/'rectilinearGrid')
else: else:
reference = VTK.load(ref_path/'rectilinearGrid.vtr') reference = VTK.load(ref_path/'rectilinearGrid.vtr')
assert rectilinearGrid.__repr__() == reference.__repr__() and \ assert rectilinearGrid.as_ASCII() == reference.as_ASCII() and \
np.allclose(rectilinearGrid.get('cell'),c) np.allclose(rectilinearGrid.get('cell'),c)