Programmable filter for Pivot table functionality using Pandas

Dear all,

based on this old thread, I implemented a Programmable Filter in ParaView 5.4.1 that uses Pivot Table functionailty in Pandas to calculate a circumferential average of VTK-PointData, in my case read in from csv files and filtered with an Extract Time Step filter.
Hence, input data are from type vtkTable. I used a function from PVGeo to transform vtkTable to Pandas DataFrame. Since my input data do not have headers, standard header names are “Header 0”, “Header 1” etc.
Here, “Header 0” is coordinate x. The programmable filter counts (len) and averages (np.mean) all values in the dataframe and writes point data (Index; Coordinates x,y,z) and two data arrays from the pivot chart (“alpha” in column 2 and “pressure” in column 3) as output, that can be displayed in Spreadsheet View or Line Chart.

Hope it helps.
Kind regards, David

# ParaView Programmable Filter (Python)
# Calculates circumferential averages with Pandas Pivot Table functionality for each axial coordinate x
# Output Data Set Type of the Filter has to be set to vtkPolyData
# Numpy and Pandas have to be installed (no default for Windows installations of ParaView)

import pandas as pd
import numpy as np
from vtk.numpy_interface import dataset_adapter as dsa

def table_to_data_frame(table):
    """Converts a vtkTable to a pandas DataFrame"""
    if not isinstance(table, vtk.vtkTable):
        raise PVGeoError('Input is not a vtkTable')
    num = table.GetNumberOfColumns()
    names = [table.GetColumnName(i) for i in range(num)]
    data = dsa.WrapDataObject(table).RowData
    df = pd.DataFrame()
    for i, n in enumerate(names):
        df[n] = np.array(data[n])
    return df

pdi = self.GetInput()
pdo =  self.GetOutput()

newPoints = vtk.vtkPoints()

alpha = vtk.vtkFloatArray()
alpha.SetName('alpha')
alpha.SetNumberOfComponents(1)

p = vtk.vtkFloatArray()
p.SetName('p')
p.SetNumberOfComponents(1)


df=table_to_data_frame(pdi)

output = pd.pivot_table(df, index="Field 0", aggfunc=[np.mean,len])
index=output.index.tolist()

for i in range(len(output)):
    x=float(index[i])
    alpha1=float(output.values[i,2])
    p1=float(output.values[i,3])
    alpha.InsertNextValue(alpha1)
    p.InsertNextValue(p1)
    newPoints.InsertPoint(i, x, 0, 0)

pdo.SetPoints(newPoints)
pdo.GetPointData().AddArray(alpha)
pdo.GetPointData().AddArray(p)
2 Likes