Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vtk interface #296

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/test_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 8 * * MON'
workflow_dispatch:

jobs:
Expand All @@ -33,9 +35,9 @@ jobs:
- name: Trial imports
run: python -c 'import pytools as pt'

import-matrix:
ubuntu_22_04_versions:

runs-on: ubuntu-latest
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
Expand Down
16 changes: 16 additions & 0 deletions examples/write_htg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import vtk
import pytools as pt

# This initializes a hypertreegrid from the given reader.
reader = pt.vlsvfile.VlsvReader("../../bulk.0002189.vlsv")
htg = pt.vlsvfile.vtkVlsvHyperTreeGrid(reader)

# These functions grab one SpatialGrid variable and map that to
# the hypertreegrid. Variable vector sizes of 1,2,3,4,9 supported.
htg.addArrayFromVlsv("vg_b_vol")
htg.addArrayFromVlsv("vg_beta_star")

writer = vtk.vtkXMLHyperTreeGridWriter()
writer.SetFileName("output_EGE.htg")
writer.SetInputData(htg)
writer.Write()
16 changes: 10 additions & 6 deletions pyPlots/colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,13 @@
_cm_name = _cm_name[:-4]
_cm_data = np.loadtxt(_f)
_cm = LinearSegmentedColormap.from_list(_cm_name, _cm_data)
if Version(mpl_version) > Version("3.5.0"):
mcm.register(_cm)
mcm.register(_cm.reversed())
else:
plt.register_cmap(cmap=_cm)
plt.register_cmap(cmap=_cm.reversed())
try:
if Version(mpl_version) > Version("3.5.0"):
mcm.register(_cm)
mcm.register(_cm.reversed())
else:
plt.register_cmap(cmap=_cm)
plt.register_cmap(cmap=_cm.reversed())
except Exception as e:
logging.warning("Problem registering colormap " + _cm_name + ". Produced exception was:\n"+str(e))

164 changes: 164 additions & 0 deletions pyVlsv/vlsvVtkParaReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
"""This module demonstrates various ways of adding
VTKPythonAlgorithmBase subclasses as filters, sources, readers,
and writers in ParaView"""

# https://github.com/Kitware/ParaView/blob/master/Examples/Plugins/PythonAlgorithm/PythonAlgorithmExamples.py

# 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 pytools as pt

# Use the analysator reader, but include Paraview decorators via inheriting/chaining
# necessary methods etc

#------------------------------------------------------------------------------
# A reader example.
#------------------------------------------------------------------------------

# To add a reader, we can use the following decorators
# @smproxy.source(name="PythonCSVReader", label="Python-based CSV Reader")
# @smhint.xml("""<ReaderFactory extensions="csv" file_description="Numpy CSV files" />""")
# or directly use the "@reader" decorator.
@smproxy.reader(name="PythonVLSVReader", label="Python-based VLSV Reader, outputs htg",
extensions="vlsv", file_description="VLSV files")
class PythonPvVLSVReader(pt.vlsvfile.VlsvVtkReader):
"""A reader that wraps an Analysator VLSV file reader.
"""
def __init__(self):
super().__init__()


@smproperty.stringvector(name="FileName")
@smdomain.filelist()
@smhint.filechooser(extensions="vlsv", file_description="Vlasiator VLSV files")
def SetFileName(self, filename):
"""Specify filename for the file to read."""
print(filename)
super().SetFileName(filename)

@smproperty.doublevector(name="TimestepValues", information_only="1", si_class="vtkSITimeStepsProperty")
def GetTimestepValues(self):
return None

# 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 super().GetDataArraySelection()


#------------------------------------------------------------------------------
# A writer example.
#------------------------------------------------------------------------------
'''
@smproxy.writer(extensions="npz", file_description="NumPy Compressed Arrays", support_reload=False)
@smproperty.input(name="Input", port_index=0)
@smdomain.datatype(dataTypes=["vtkTable"], composite_data_supported=False)
class NumpyWriter(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self, nInputPorts=1, nOutputPorts=0, inputType='vtkTable')
self._filename = None

@smproperty.stringvector(name="FileName", panel_visibility="never")
@smdomain.filelist()
def SetFileName(self, fname):
"""Specify filename for the file to write."""
if self._filename != fname:
self._filename = fname
self.Modified()

def RequestData(self, request, inInfoVec, outInfoVec):
from vtkmodules.vtkCommonDataModel import vtkTable
from vtkmodules.numpy_interface import dataset_adapter as dsa

table = dsa.WrapDataObject(vtkTable.GetData(inInfoVec[0], 0))
kwargs = {}
for aname in table.RowData.keys():
kwargs[aname] = table.RowData[aname]

import numpy
numpy.savez_compressed(self._filename, **kwargs)
return 1

def Write(self):
self.Modified()
self.Update()
'''
#------------------------------------------------------------------------------
# A filter example.
#------------------------------------------------------------------------------
'''
@smproxy.filter()
@smproperty.input(name="InputTable", port_index=1)
@smdomain.datatype(dataTypes=["vtkTable"], composite_data_supported=False)
@smproperty.input(name="InputDataset", port_index=0)
@smdomain.datatype(dataTypes=["vtkDataSet"], composite_data_supported=False)
class ExampleTwoInputFilter(VTKPythonAlgorithmBase):
def __init__(self):
VTKPythonAlgorithmBase.__init__(self, nInputPorts=2, nOutputPorts=1, outputType="vtkPolyData")

def FillInputPortInformation(self, port, info):
if port == 0:
info.Set(self.INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet")
else:
info.Set(self.INPUT_REQUIRED_DATA_TYPE(), "vtkTable")
return 1

def RequestData(self, request, inInfoVec, outInfoVec):
from vtkmodules.vtkCommonDataModel import vtkTable, vtkDataSet, vtkPolyData
input0 = vtkDataSet.GetData(inInfoVec[0], 0)
input1 = vtkDataSet.GetData(inInfoVec[1], 0)
output = vtkPolyData.GetData(outInfoVec, 0)
# do work
print("Pretend work done!")
return 1

@smproxy.filter()
@smproperty.input(name="Input")
@smdomain.datatype(dataTypes=["vtkDataSet"], composite_data_supported=False)
class PreserveInputTypeFilter(VTKPythonAlgorithmBase):
"""
Example filter demonstrating how to write a filter that preserves the input
dataset type.
"""
def __init__(self):
super().__init__(nInputPorts=1, nOutputPorts=1, outputType="vtkDataSet")

def RequestDataObject(self, request, inInfo, outInfo):
inData = self.GetInputData(inInfo, 0, 0)
outData = self.GetOutputData(outInfo, 0)
assert inData is not None
if outData is None or (not outData.IsA(inData.GetClassName())):
outData = inData.NewInstance()
outInfo.GetInformationObject(0).Set(outData.DATA_OBJECT(), outData)
return super().RequestDataObject(request, inInfo, outInfo)

def RequestData(self, request, inInfo, outInfo):
inData = self.GetInputData(inInfo, 0, 0)
outData = self.GetOutputData(outInfo, 0)
print("input type =", inData.GetClassName())
print("output type =", outData.GetClassName())
assert outData.IsA(inData.GetClassName())
return 1
'''

def test_PythonVLSVReader(fname):
reader = PythonVLSVReader()
reader.SetFileName(fname)
reader.Update()
assert reader.GetOutputDataObject(0).GetNumberOfRows() > 0

if __name__ == "__main__":
#test_PythonSuperquadricSource()
test_PythonVLSVReader("/tmp/data.csv")

from paraview.detail.pythonalgorithm import get_plugin_xmls
from xml.dom.minidom import parseString
for xml in get_plugin_xmls(globals()):
dom = parseString(xml)
print(dom.toprettyxml(" ","\n"))
1 change: 1 addition & 0 deletions pyVlsv/vlsvfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from vlsvreader import fsDecompositionFromGlobalIds,fsReadGlobalIdsPerRank,fsGlobalIdToGlobalIndex
from vlsvwriter import VlsvWriter
from vlasiatorreader import VlasiatorReader
from vlsvvtkinterface import vtkVlsvHyperTreeGrid,VlsvVtkReader

from vlsvparticles import VlsvParticles
import reduction
Expand Down
35 changes: 35 additions & 0 deletions pyVlsv/vlsvreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,41 @@ def __check_datareducer(self, name, reducer):

return reducer_ok

def get_variables(self):

varlist = []

for child in self.__xml_root:
if child.tag == "VARIABLE" and "name" in child.attrib:
name = child.attrib["name"]
varlist.append(name)

return varlist

def get_reducers(self):

varlist = []

reducer_max_len = 0

for reducer_reg in [datareducers, multipopdatareducers, v5reducers, multipopv5reducers]:
for k in reducer_reg.keys():
reducer_max_len = max(reducer_max_len, len(k))



for reducer_reg in [datareducers, multipopdatareducers, v5reducers, multipopv5reducers]:
for name, reducer in reducer_reg.items():
self.__current_reducer_tree_nodes.clear()
if self.__check_datareducer(name,reducer):
if name[:3] == 'pop':
for pop in self.active_populations:
varlist.append(pop+'/'+name[4:])
else:
varlist.append(name)

return varlist


def list(self, parameter=True, variable=True, mesh=False, datareducer=False, operator=False, other=False):
''' Print out a description of the content of the file. Useful
Expand Down
Loading
Loading