From 2aa0f5c302aa5a40767183690c51c38700f3e516 Mon Sep 17 00:00:00 2001 From: makdl <31526990+makdl@users.noreply.github.com> Date: Mon, 15 Mar 2021 19:26:22 -0400 Subject: [PATCH 1/4] Prefer official VTK package, fallback to ours --- spatialpy/Result.py | 60 +++++++++++++++++++++++++++++++++++------- spatialpy/VTKReader.py | 4 +-- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/spatialpy/Result.py b/spatialpy/Result.py index aea312c9..ad6fa9c6 100644 --- a/spatialpy/Result.py +++ b/spatialpy/Result.py @@ -4,11 +4,18 @@ import pickle import shutil import subprocess +import sys import numpy from spatialpy.Model import * -from spatialpy.VTKReader import VTKReader + +try: + import vtk +except: + print('''Using fallback VTK reader. This is significantly slower than the official VTK package. +For best performance please install the VTK Python package: \'python3 -m pip install vtk\'''') + from spatialpy.VTKReader import VTKReader common_rgb_values=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f', '#bcbd22','#17becf','#ff0000','#00ff00','#0000ff','#ffff00','#00ffff','#ff00ff', @@ -158,17 +165,52 @@ def __setstate__(self, state): raise Exception("Error unpickling model, could not recreate the Result output files: "+str(e)) def read_step(self, step_num, debug=False): - """ Read the data for simulation step 'step_num'. """ - reader = VTKReader(debug=debug) + """ Read the data for simulation step 'step_num'. + + Attributes + ---------- + step_num: int + The step number of the results to read + debug: bool + Whether or not to display debug information + Return + ---------- + tuple: + Contains point coordinate data in the first index and type and property data in the second index """ num = int(step_num * self.model.output_freq) filename = os.path.join(self.result_dir, "output{0}.vtk".format(num)) - #print("read_step({0}) opening '{1}'".format(step_num, filename)) - reader.setfilename(filename) - reader.readfile() - if reader.getpoints() is None or reader.getarrays() is None: + + if debug: + print("read_step({0}) opening '{1}'".format(step_num, filename)) + + if 'vtk' in sys.modules: + reader = vtk.vtkGenericDataObjectReader() + reader.SetFileName(filename) + reader.Update() + data = reader.GetOutput() + + if data is not None: + points = numpy.array(data.GetPoints().GetData()) + pd = data.GetPointData() + vtk_data = {} + + for i in range(pd.GetNumberOfArrays()): + if pd.GetArrayName(i) is None: + break + + if debug: + print(i,pd.GetArrayName(i)) + + vtk_data[pd.GetArrayName(i)] = numpy.array(pd.GetArray(i)) + else: + reader = VTKReader(filename=filename, debug=debug) + reader.readfile() + points = reader.getpoints() + vtk_data = reader.getarrays() + + if points is None or vtk_data is None: raise ResultError("read_step(step_num={0}): got data = None".format(step_num)) - points = reader.getpoints() - vtk_data = reader.getarrays() + return (points, vtk_data) diff --git a/spatialpy/VTKReader.py b/spatialpy/VTKReader.py index 5f237c84..67ad758a 100644 --- a/spatialpy/VTKReader.py +++ b/spatialpy/VTKReader.py @@ -6,9 +6,9 @@ class VTKReader: """VTKReader.py: SpatialPy minimal VTK legacy file reader.""" """Reference: https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf""" - def __init__(self, debug=False): + def __init__(self, filename=None, debug=False): - self.filename = None + self.filename = filename self.pointdatatype = None self.numpoints = None self.points = None From 7012fe23b333a6181eda9ad1f03bc0b9386f5af9 Mon Sep 17 00:00:00 2001 From: makdl <31526990+makdl@users.noreply.github.com> Date: Tue, 13 Apr 2021 00:21:36 -0400 Subject: [PATCH 2/4] Fixed except, typos, and message --- spatialpy/Mesh.py | 2 +- spatialpy/Result.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/spatialpy/Mesh.py b/spatialpy/Mesh.py index 20e7fcab..f68b5f08 100644 --- a/spatialpy/Mesh.py +++ b/spatialpy/Mesh.py @@ -273,7 +273,7 @@ def read_msh_file(cls, filename): try: import meshio except ImportError as e: - raise MeshError("The python package 'meshio' is not istaled.") + raise MeshError("The Python package 'meshio' is not installed.") return cls.import_meshio_object(meshio.msh_io.read(filename)) diff --git a/spatialpy/Result.py b/spatialpy/Result.py index ad6fa9c6..a12d71ca 100644 --- a/spatialpy/Result.py +++ b/spatialpy/Result.py @@ -12,9 +12,8 @@ try: import vtk -except: - print('''Using fallback VTK reader. This is significantly slower than the official VTK package. -For best performance please install the VTK Python package: \'python3 -m pip install vtk\'''') +except ImportError as e: + print('''The Python package 'vtk' is not installed. Using integrated VTK reader. This is significantly slower than the official VTK package.''') from spatialpy.VTKReader import VTKReader common_rgb_values=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f', From c90de0597efe8f3b286a5736bc2ff69f5f2c636b Mon Sep 17 00:00:00 2001 From: makdl <31526990+makdl@users.noreply.github.com> Date: Tue, 13 Apr 2021 00:36:29 -0400 Subject: [PATCH 3/4] Revert "Fixed except, typos, and message" This reverts commit 7012fe23b333a6181eda9ad1f03bc0b9386f5af9. --- spatialpy/Mesh.py | 2 +- spatialpy/Result.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spatialpy/Mesh.py b/spatialpy/Mesh.py index f68b5f08..20e7fcab 100644 --- a/spatialpy/Mesh.py +++ b/spatialpy/Mesh.py @@ -273,7 +273,7 @@ def read_msh_file(cls, filename): try: import meshio except ImportError as e: - raise MeshError("The Python package 'meshio' is not installed.") + raise MeshError("The python package 'meshio' is not istaled.") return cls.import_meshio_object(meshio.msh_io.read(filename)) diff --git a/spatialpy/Result.py b/spatialpy/Result.py index a12d71ca..ad6fa9c6 100644 --- a/spatialpy/Result.py +++ b/spatialpy/Result.py @@ -12,8 +12,9 @@ try: import vtk -except ImportError as e: - print('''The Python package 'vtk' is not installed. Using integrated VTK reader. This is significantly slower than the official VTK package.''') +except: + print('''Using fallback VTK reader. This is significantly slower than the official VTK package. +For best performance please install the VTK Python package: \'python3 -m pip install vtk\'''') from spatialpy.VTKReader import VTKReader common_rgb_values=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f', From 700844b280acfa96ae4f23d7582c95f4ab9d2b69 Mon Sep 17 00:00:00 2001 From: makdl <31526990+makdl@users.noreply.github.com> Date: Tue, 13 Apr 2021 00:39:45 -0400 Subject: [PATCH 4/4] Fixed except, and message, this time without a conflict --- spatialpy/Result.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spatialpy/Result.py b/spatialpy/Result.py index ad6fa9c6..467fcd5d 100644 --- a/spatialpy/Result.py +++ b/spatialpy/Result.py @@ -12,9 +12,9 @@ try: import vtk -except: - print('''Using fallback VTK reader. This is significantly slower than the official VTK package. -For best performance please install the VTK Python package: \'python3 -m pip install vtk\'''') +except ImportError as e: + print('''The Python package 'vtk' is not installed. Using integrated VTK reader. This is significantly +slower than the official VTK package.''') from spatialpy.VTKReader import VTKReader common_rgb_values=['#1f77b4','#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f',