DAMASK_EICMD/python/tests/test_VTK.py

253 lines
10 KiB
Python
Raw Normal View History

2020-03-31 14:34:06 +05:30
import os
import filecmp
import time
2022-02-22 18:30:36 +05:30
import string
2022-03-27 02:38:09 +05:30
import sys
2020-03-31 14:34:06 +05:30
import pytest
import numpy as np
import numpy.ma as ma
from vtkmodules.vtkCommonCore import vtkVersion
2020-03-31 14:34:06 +05:30
from damask import VTK
2022-02-16 03:08:02 +05:30
from damask import Table
from damask import Colormap
2020-03-31 14:34:06 +05:30
@pytest.fixture
def res_path(res_path_base):
"""Directory containing testing resources."""
return res_path_base/'VTK'
2020-03-31 14:34:06 +05:30
@pytest.fixture
def default():
"""Simple VTK."""
2020-12-04 02:28:24 +05:30
cells = np.array([5,6,7],int)
size = np.array([.6,1.,.5])
2021-12-22 17:11:16 +05:30
return VTK.from_image_data(cells,size)
2020-03-31 14:34:06 +05:30
class TestVTK:
@pytest.fixture(autouse=True)
def _patch_execution_stamp(self, patch_execution_stamp):
print('patched damask.util.execution_stamp')
@pytest.mark.parametrize('cmap',[Colormap.from_predefined('cividis'),'strain'])
@pytest.mark.skipif(sys.platform == 'win32', reason='DISPLAY has no effect on Windows OS')
def test_show(sef,default,cmap,monkeypatch):
monkeypatch.delenv('DISPLAY',raising=False)
default.show(colormap=cmap)
2022-02-23 11:19:38 +05:30
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)
2022-02-25 05:45:28 +05:30
string = str(v)
2022-02-23 11:19:38 +05:30
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')
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
2020-03-31 14:34:06 +05:30
def test_rectilinearGrid(self,tmp_path):
grid = np.sort(np.random.random((3,10)))
v = VTK.from_rectilinear_grid(grid)
2022-02-25 05:45:28 +05:30
string = str(v)
2022-02-23 11:19:38 +05:30
string = v.as_ASCII()
v.save(tmp_path/'rectilinearGrid',False)
vtr = VTK.load(tmp_path/'rectilinearGrid.vtr')
2020-06-28 03:07:46 +05:30
with open(tmp_path/'rectilinearGrid.vtk','w') as f:
f.write(string)
vtk = VTK.load(tmp_path/'rectilinearGrid.vtk','VTK_rectilinearGrid')
2022-02-23 11:19:38 +05:30
assert (string == vtr.as_ASCII() == vtk.as_ASCII())
2020-03-31 14:34:06 +05:30
def test_polyData(self,tmp_path):
points = np.random.rand(100,3)
2020-10-27 18:12:49 +05:30
v = VTK.from_poly_data(points)
2022-02-25 05:45:28 +05:30
string = str(v)
2022-02-23 11:19:38 +05:30
string = v.as_ASCII()
v.save(tmp_path/'polyData',False)
vtp = VTK.load(tmp_path/'polyData.vtp')
2020-06-28 03:07:46 +05:30
with open(tmp_path/'polyData.vtk','w') as f:
f.write(string)
vtk = VTK.load(tmp_path/'polyData.vtk','polyData')
2022-02-23 11:19:38 +05:30
assert(string == vtp.as_ASCII() == vtk.as_ASCII())
2020-03-31 14:34:06 +05:30
@pytest.mark.parametrize('cell_type,n',[
('VTK_hexahedron',8),
('TETRA',4),
('quad',4),
('VTK_TRIANGLE',3)
]
)
def test_unstructuredGrid(self,tmp_path,cell_type,n):
nodes = np.random.rand(n,3)
connectivity = np.random.choice(np.arange(n),n,False).reshape(-1,n)
2020-10-27 18:12:49 +05:30
v = VTK.from_unstructured_grid(nodes,connectivity,cell_type)
2022-02-25 05:45:28 +05:30
string = str(v)
2022-02-23 11:19:38 +05:30
string = v.as_ASCII()
v.save(tmp_path/'unstructuredGrid',False)
vtu = VTK.load(tmp_path/'unstructuredGrid.vtu')
2020-06-28 03:07:46 +05:30
with open(tmp_path/'unstructuredGrid.vtk','w') as f:
f.write(string)
vtk = VTK.load(tmp_path/'unstructuredGrid.vtk','unstructuredgrid')
2022-02-23 11:19:38 +05:30
assert(string == vtu.as_ASCII() == vtk.as_ASCII())
def test_parallel_out(self,tmp_path):
points = np.random.rand(102,3)
2020-10-27 18:12:49 +05:30
v = VTK.from_poly_data(points)
fname_s = tmp_path/'single.vtp'
fname_p = tmp_path/'parallel.vtp'
v.save(fname_s,False)
v.save(fname_p,True)
for i in range(10):
if os.path.isfile(fname_p) and filecmp.cmp(fname_s,fname_p):
assert(True)
return
time.sleep(.5)
assert(False)
2020-11-14 22:24:47 +05:30
def test_compress(self,tmp_path):
points = np.random.rand(102,3)
v = VTK.from_poly_data(points)
2020-11-16 21:04:49 +05:30
fname_c = tmp_path/'compressed.vtp'
2020-11-14 22:24:47 +05:30
fname_p = tmp_path/'plain.vtp'
v.save(fname_c,parallel=False,compress=False)
v.save(fname_p,parallel=False,compress=True)
2022-02-23 11:19:38 +05:30
assert(VTK.load(fname_c).as_ASCII() == VTK.load(fname_p).as_ASCII())
2020-11-14 22:24:47 +05:30
2020-11-04 22:38:04 +05:30
@pytest.mark.parametrize('fname',['a','a.vtp','a.b','a.b.vtp'])
def test_filename_variations(self,tmp_path,fname):
points = np.random.rand(102,3)
v = VTK.from_poly_data(points)
v.save(tmp_path/fname)
@pytest.mark.parametrize('fname,dataset_type',[('a_file.vtk', None),
('a_file.vtk','vtk'),
('a_file.vtx', None)])
def test_invalid_dataset_type(self,tmp_path,fname,dataset_type):
open(tmp_path/fname,'a').close()
with pytest.raises(TypeError):
VTK.load(tmp_path/fname,dataset_type)
def test_file_not_found(self):
with pytest.raises(FileNotFoundError):
VTK.load('/dev/null')
2020-11-06 02:08:00 +05:30
def test_add_extension(self,tmp_path,default):
default.save(tmp_path/'default.txt',parallel=False)
2021-12-22 17:11:16 +05:30
assert os.path.isfile(tmp_path/'default.txt.vti')
def test_invalid_get(self,default):
2022-03-07 15:58:17 +05:30
with pytest.raises(KeyError):
default.get('does_not_exist')
def test_invalid_set_shape(self,default):
with pytest.raises(ValueError):
default.set('valid',np.ones(3))
2020-08-25 20:47:49 +05:30
def test_invalid_set_missing_label(self,default):
2020-08-25 20:47:49 +05:30
data = np.random.randint(9,size=np.prod(np.array(default.vtk_data.GetDimensions())-1))
with pytest.raises(ValueError):
default.set(data=data)
def test_invalid_set_type(self,default):
with pytest.raises(TypeError):
default.set(label='valid',data='invalid_type')
with pytest.raises(TypeError):
default.set(label='valid',table='invalid_type')
def test_invalid_set_dual(self,default):
with pytest.raises(KeyError):
default.set(label='valid',data=0,table=0)
2020-06-28 03:07:46 +05:30
@pytest.mark.parametrize('data_type,shape',[(float,(3,)),
(float,(3,3)),
(float,(1,)),
(int,(4,)),
(str,(1,))])
@pytest.mark.parametrize('N_values',[5*6*7,6*7*8])
def test_set_get(self,default,data_type,shape,N_values):
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
new = default.set('data',data)
2022-02-21 16:47:00 +05:30
assert (np.squeeze(data.reshape(N_values,-1)) == new.get('data')).all()
2022-02-16 03:08:02 +05:30
@pytest.mark.parametrize('shapes',[{'scalar':(1,),'vector':(3,),'tensor':(3,3)},
{'vector':(6,),'tensor':(3,3)},
{'tensor':(3,3),'scalar':(1,)}])
def test_set_table(self,default,shapes):
2022-02-16 03:08:02 +05:30
N = np.random.choice([default.N_points,default.N_cells])
2022-02-16 03:12:17 +05:30
d = dict()
2022-02-16 03:08:02 +05:30
for k,s in shapes.items():
2022-02-16 03:12:17 +05:30
d[k] = dict(shape = s,
data = np.random.random(N*np.prod(s)).reshape((N,-1)))
new = default.set(table=Table(shapes,np.column_stack([d[k]['data'] for k in shapes.keys()])))
2022-02-16 03:08:02 +05:30
for k,s in shapes.items():
2022-02-21 16:47:00 +05:30
assert np.allclose(np.squeeze(d[k]['data']),new.get(k),rtol=1e-7)
2022-02-16 03:08:02 +05:30
def test_set_masked(self,default):
data = np.random.rand(5*6*7,3)
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
mask_auto = default.set('D',masked)
mask_manual = default.set('D',np.where(masked.mask,masked.fill_value,masked))
2022-02-23 11:19:38 +05:30
assert mask_manual == mask_auto
2022-02-22 18:30:36 +05:30
@pytest.mark.parametrize('data_type,shape',[(float,(3,)),
(float,(3,3)),
(float,(1,)),
(int,(4,)),
(str,(1,))])
@pytest.mark.parametrize('N_values',[5*6*7,6*7*8])
def test_labels(self,default,data_type,shape,N_values):
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
ALPHABET = np.array(list(string.ascii_lowercase + ' '))
label = ''.join(np.random.choice(ALPHABET, size=10))
new = default.set(label,data)
2022-02-22 18:30:36 +05:30
if N_values == default.N_points: assert label in new.labels['Point Data']
if N_values == default.N_cells: assert label in new.labels['Cell Data']
def test_comments(self,tmp_path,default):
default.comments += ['this is a comment']
default.save(tmp_path/'with_comments',parallel=False)
2021-12-22 17:11:16 +05:30
new = VTK.load(tmp_path/'with_comments.vti')
assert new.comments == ['this is a comment']
2020-06-28 03:07:46 +05:30
@pytest.mark.xfail(vtkVersion.GetVTKMajorVersion()<8, reason='missing METADATA')
def test_compare_reference_polyData(self,update,res_path,tmp_path):
2020-06-28 03:07:46 +05:30
points=np.dstack((np.linspace(0.,1.,10),np.linspace(0.,2.,10),np.linspace(-1.,1.,10))).squeeze()
polyData = VTK.from_poly_data(points).set('coordinates',points)
2020-06-28 03:07:46 +05:30
if update:
polyData.save(res_path/'polyData')
2020-06-28 03:07:46 +05:30
else:
reference = VTK.load(res_path/'polyData.vtp')
2022-02-23 11:19:38 +05:30
assert polyData.as_ASCII() == reference.as_ASCII() and \
2022-02-02 13:40:24 +05:30
np.allclose(polyData.get('coordinates'),points)
2020-06-28 03:07:46 +05:30
@pytest.mark.xfail(vtkVersion.GetVTKMajorVersion()<8, reason='missing METADATA')
def test_compare_reference_rectilinearGrid(self,update,res_path,tmp_path):
grid = [np.arange(4)**2.,
np.arange(5)**2.,
np.arange(6)**2.] # ParaView renders tetrahedral meshing unless using float coordinates!
coords = np.stack(np.meshgrid(*grid,indexing='ij'),axis=-1)
c = coords[:-1,:-1,:-1,:].reshape(-1,3,order='F')
n = coords[:,:,:,:].reshape(-1,3,order='F')
2022-02-21 16:47:00 +05:30
rectilinearGrid = VTK.from_rectilinear_grid(grid) \
.set('cell',np.ascontiguousarray(c)) \
.set('node',np.ascontiguousarray(n))
2020-06-28 03:07:46 +05:30
if update:
rectilinearGrid.save(res_path/'rectilinearGrid')
2020-06-28 03:07:46 +05:30
else:
reference = VTK.load(res_path/'rectilinearGrid.vtr')
2022-02-23 11:19:38 +05:30
assert rectilinearGrid.as_ASCII() == reference.as_ASCII() and \
2022-02-02 13:40:24 +05:30
np.allclose(rectilinearGrid.get('cell'),c)