slight polish

This commit is contained in:
Philip Eisenlohr 2022-02-22 10:56:12 -05:00
parent be11da2284
commit 661b7fb602
2 changed files with 7 additions and 8 deletions

View File

@ -236,12 +236,12 @@ class VTK:
""" """
if not Path(fname).expanduser().is_file(): # vtk has a strange error handling if not Path(fname).expanduser().is_file(): # vtk has a strange error handling
raise FileNotFoundError(f'No such file: {fname}') raise FileNotFoundError(f'file "{fname}" not found')
if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None: if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None:
reader = vtk.vtkGenericDataObjectReader() reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(str(Path(fname).expanduser())) reader.SetFileName(str(Path(fname).expanduser()))
if dataset_type is None: if dataset_type is None:
raise TypeError('Dataset type for *.vtk file not given') raise TypeError('dataset type for *.vtk file not given')
elif dataset_type.lower().endswith(('imagedata','image_data')): elif dataset_type.lower().endswith(('imagedata','image_data')):
reader.Update() reader.Update()
vtk_data = reader.GetStructuredPointsOutput() vtk_data = reader.GetStructuredPointsOutput()
@ -255,7 +255,7 @@ class VTK:
reader.Update() reader.Update()
vtk_data = reader.GetRectilinearGridOutput() vtk_data = reader.GetRectilinearGridOutput()
else: else:
raise TypeError(f'Unknown dataset type "{dataset_type}" for vtk file') raise TypeError(f'unknown dataset type "{dataset_type}" for vtk file')
else: else:
if ext == '.vti': if ext == '.vti':
reader = vtk.vtkXMLImageDataReader() reader = vtk.vtkXMLImageDataReader()
@ -266,7 +266,7 @@ class VTK:
elif ext == '.vtr': elif ext == '.vtr':
reader = vtk.vtkXMLRectilinearGridReader() reader = vtk.vtkXMLRectilinearGridReader()
else: else:
raise TypeError(f'Unknown file extension "{ext}"') raise TypeError(f'unknown file extension "{ext}"')
reader.SetFileName(str(Path(fname).expanduser())) reader.SetFileName(str(Path(fname).expanduser()))
reader.Update() reader.Update()
@ -367,7 +367,7 @@ class VTK:
elif N_data == vtk_data.GetNumberOfCells(): elif N_data == vtk_data.GetNumberOfCells():
vtk_data.GetCellData().AddArray(d) vtk_data.GetCellData().AddArray(d)
else: else:
raise ValueError(f'Data count mismatch ({N_data}{self.N_points} & {self.N_cells})') raise ValueError(f'data count mismatch ({N_data}{self.N_points} & {self.N_cells})')
dup = self.copy() dup = self.copy()
if isinstance(data,np.ndarray): if isinstance(data,np.ndarray):
@ -376,7 +376,7 @@ class VTK:
np.where(data.mask,data.fill_value,data) if isinstance(data,np.ma.MaskedArray) else data, np.where(data.mask,data.fill_value,data) if isinstance(data,np.ma.MaskedArray) else data,
label) label)
else: else:
raise ValueError('No label defined for numpy.ndarray') raise ValueError('no label defined for numpy.ndarray')
elif isinstance(data,Table): elif isinstance(data,Table):
for l in data.labels: for l in data.labels:
_add_array(dup.vtk_data,data.get(l),l) _add_array(dup.vtk_data,data.get(l),l)
@ -426,7 +426,7 @@ class VTK:
# string array # string array
return np.array([vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues())]).astype(str) return np.array([vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues())]).astype(str)
except UnboundLocalError: except UnboundLocalError:
raise ValueError(f'Array "{label}" not found') raise ValueError(f'array "{label}" not found')
@property @property

View File

@ -35,7 +35,6 @@ class TestVTK:
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)))
print(grid)
v = VTK.from_rectilinear_grid(grid) v = VTK.from_rectilinear_grid(grid)
string = v.__repr__() string = v.__repr__()
v.save(tmp_path/'rectilinearGrid',False) v.save(tmp_path/'rectilinearGrid',False)