-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_series_reader_plugin.py
178 lines (149 loc) · 6.83 KB
/
time_series_reader_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""This module demonstrates various ways of adding
VTKPythonAlgorithmBase subclasses as filters, sources, readers,
and writers in ParaView"""
# This is module to import. It provides VTKPythonAlgorithmBase, the base class
# for all python-based vtkAlgorithm subclasses in VTK and decorators used to
# 'register' the algorithm with ParaView along with information about UI.
from paraview.util.vtkAlgorithm import *
import uuid
from pathlib import Path
#------------------------------------------------------------------------------
# A reader example.
#------------------------------------------------------------------------------
def createModifiedCallback(anobject):
import weakref
weakref_obj = weakref.ref(anobject)
anobject = None
def _markmodified(*args, **kwars):
o = weakref_obj()
if o is not None:
o.Modified()
return _markmodified
# To add a reader, we can use the following decorators
# @smproxy.source(name="PythonnumpyReader", label="Python-based numpy Reader")
# @smhint.xml("""<ReaderFactory extensions="csv" file_description="Numpy numpy files" />""")
# or directly use the "@reader" decorator.
@smproxy.reader(name="Sama Lidar Numpy Reader", label="Python-based numpy pcd Reader for timeseries data",
extensions="npy",
file_description="numpy files")
class PythonNumpyPCDReader(VTKPythonAlgorithmBase):
"""A reader that reads a numpy file. If the numpy has a "time" column, then
the data is treated as a temporal dataset"""
def __init__(self):
VTKPythonAlgorithmBase.__init__(self, nInputPorts=0, nOutputPorts=1, outputType='vtkPolyData')
self._filename = None
self._ndata = None
self._timesteps = None
print("starting",uuid.uuid1())
from vtkmodules.vtkCommonCore import vtkDataArraySelection
self._arrayselection = vtkDataArraySelection()
self._arrayselection.AddObserver("ModifiedEvent", createModifiedCallback(self))
def _get_raw_data(self, requested_time=None):
import numpy
if self._ndata is not None:
if requested_time is not None:
##### load specific npy file from fnmes
fname = self.fnames[int(requested_time)]
self._ndata = numpy.load(fname)
print(self._ndata.dtype)
# self._ndata.dtype = numpy.dtype([("x",numpy.float32),("y",numpy.float32),("z",numpy.float32),("intensity",numpy.float32)])
return self._ndata
return self._ndata
if self._filename is None:
# Note, exceptions are totally fine!
raise RuntimeError("No filename specified")
# self._ndata = numpy.genfromtxt(self._filename, dtype=None, names=True, delimiter=',', autostrip=True)
self.pth = Path(self._filename)
self.fnames = list(self.pth.parent.rglob("*npy"))
self.fnames.sort()
times = [i for i,e in enumerate(self.fnames)]
self._ndata = 0
self._timesteps = times
return self._get_raw_data(requested_time)
def _get_timesteps(self):
self._get_raw_data()
return self._timesteps if self._timesteps is not None else None
def _get_update_time(self, outInfo):
executive = self.GetExecutive()
timesteps = self._get_timesteps()
if timesteps is None or len(timesteps) == 0:
return None
elif outInfo.Has(executive.UPDATE_TIME_STEP()) and len(timesteps) > 0:
utime = outInfo.Get(executive.UPDATE_TIME_STEP())
print("using inner method get update time",utime)
dtime = timesteps[0]
for atime in timesteps:
if atime > utime:
return dtime
else:
dtime = atime
return dtime
else:
assert(len(timesteps) > 0)
return timesteps[0]
def _get_array_selection(self):
return self._arrayselection
@smproperty.stringvector(name="FileName")
@smdomain.filelist()
@smhint.filechooser(extensions="npy", file_description="numpy pcd file")
def SetFileName(self, name):
"""Specify filename for the file to read."""
print(name)
if self._filename != name:
self._filename = name
self._ndata = None
self._timesteps = None
self.Modified()
@smproperty.doublevector(name="TimestepValues", information_only="1", si_class="vtkSITimeStepsProperty")
def GetTimestepValues(self):
print("getting time steps")
return self._get_timesteps()
# Array selection API is typical with readers in VTK
# This is intended to allow ability for users to choose which arrays to
# load. To expose that in ParaView, simply use the
# smproperty.dataarrayselection().
# This method **must** return a `vtkDataArraySelection` instance.
@smproperty.dataarrayselection(name="Arrays")
def GetDataArraySelection(self):
return self._get_array_selection()
def RequestInformation(self, request, inInfoVec, outInfoVec):
print("requesting information")
executive = self.GetExecutive()
outInfo = outInfoVec.GetInformationObject(0)
outInfo.Remove(executive.TIME_STEPS())
outInfo.Remove(executive.TIME_RANGE())
timesteps = self._get_timesteps()
if timesteps is not None:
for t in timesteps:
outInfo.Append(executive.TIME_STEPS(), t)
outInfo.Append(executive.TIME_RANGE(), timesteps[0])
outInfo.Append(executive.TIME_RANGE(), timesteps[-1])
return 1
def RequestData(self, request, inInfoVec, outInfoVec):
print("requesting data")
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.numpy_interface import dataset_adapter as dsa
import vtk
data_time = self._get_update_time(outInfoVec.GetInformationObject(0))
output = dsa.WrapDataObject(vtkPolyData.GetData(outInfoVec, 0))
points = self._get_raw_data(data_time)
#points = self._ndata
vpoints = vtk.vtkPoints()
vpoints.SetNumberOfPoints(points.shape[0])
intensity = vtk.vtkFloatArray()
intensity.SetNumberOfComponents(1)
intensity.SetName("Intensity")
intensity.SetNumberOfTuples(points.shape[0])
for i in range(points.shape[0]):
vpoints.SetPoint(i, points[i][:3])
intensity.SetTuple1(i, points[i][3])
output.GetPointData().SetScalars(intensity)
output.SetPoints(vpoints)
vcells = vtk.vtkCellArray()
for i in range(points.shape[0]):
vcells.InsertNextCell(1)
vcells.InsertCellPoint(i)
output.SetVerts(vcells)
if data_time is not None:
output.GetInformation().Set(output.DATA_TIME_STEP(), data_time)
return 1