Use the output of the calculator filter in pvpython

Hi All,
I have been trying to use the output of the Python Calculator for the clip value within pvpython. I would like it to be a variable that can be added to the script, so when I change the formula, the new value will be automatically calculated when the script is run. For example, here is a simple pvpython script I have been working on. Notice the clip1.value is hardcoded in. I would like this value to come from the result of the python calculator. This could also be derived form the python annotation as well.

from paraview.simple import *

disk_out_refexo = IOSSReader(registrationName='disk_out_ref.exo', FileName=['/.../disk_out_ref/disk_out_ref.exo'])

renderView1 = GetActiveViewOrCreate('RenderView')

disk_out_refexoDisplay = Show(disk_out_refexo, renderView1, 'UnstructuredGridRepresentation')

renderView1.ResetCamera(False)

renderView1.CameraPosition = [0.0, 50.0, 0.0]
renderView1.CameraViewUp = [0.0, 0.0, 1.0]

SetActiveSource(disk_out_refexo)

pythonCalculator1 = PythonCalculator(registrationName='PythonCalculator1', Input=disk_out_refexo)

pythonCalculator1.Expression = 'mean(Temp)'
pythonCalculator1.ArrayName = 'mean_temp'

pythonCalculator1Display = GetDisplayProperties(pythonCalculator1, view=renderView1)

ColorBy(pythonCalculator1Display, ('POINTS', 'Temp'))

pythonCalculator1Display.RescaleTransferFunctionToDataRange(True, False)

pythonCalculator1Display.SetScalarBarVisibility(renderView1, True)

pythonAnnotation1 = PythonAnnotation(registrationName='PythonAnnotation1', Input=disk_out_refexo)

pythonAnnotation1.ArrayAssociation = 'Point Data'
pythonAnnotation1.Expression = 'mean(Temp)'

pythonAnnotation1Display = Show(pythonAnnotation1, renderView1, 'TextSourceRepresentation')

SetActiveSource(pythonAnnotation1)

clip1 = Clip(registrationName='Clip1', Input=pythonCalculator1)

clip1.ClipType = 'Scalar'
clip1.Scalars = ['POINTS', 'Temp']
**clip1.Value = 425**
clip1.Invert = 0

clip1Display = Show(clip1, renderView1, 'UnstructuredGridRepresentation')

clip1Display.Representation = 'Surface'

Hide(disk_out_refexo)
Hide(pythonCalculator1)

Hi,

In such cases, servermanager.Fetch is available. For example, it could be as follows:

calc_vtk = servermanager.Fetch(pythonCalculator1)
mean_temp = calc_vtk.GetPointData().GetArray('mean_temp').GetTuple1(0)

clip1 = Clip(registrationName=‘Clip1’, Input=pythonCalculator1)

clip1.ClipType = ‘Scalar’
clip1.Scalars = [‘POINTS’, ‘Temp’]
clip1.Value = mean_temp
clip1.Invert = 0
1 Like

Phil,

There isn’t a direct way to plug a value computed by a Calculator into a property of a filter. Since you are scripting, it is a little bit easier. Python Annotation is actually nice here, because it produces what is expected to be a single string value.

First, you’ll need to update the pipeline up to PythonAnnotation1:

pythonAnnotation1.UpdatePipeline()

Then, you’ll need to fetch the data value from it:

stringValue = FetchData(pythonAnnotation1)[0].GetRowData().GetAbstractArray(0).GetValue(0)

Use GetRowData() because the dataset output of the Python Annotation filter is a vtkTable. Use GetAbstractArray() because a vtkStringArray is an abstract array and not a simple vtkDataArray. Use GetValue(0) to get the one string in the vtkStringArray.

You can convert the string value to float with

float(stringValue)

and plug the result into clip1.Value.

Hope that helps!

3 Likes

Hi Cory,
Yes this worked for builtin, but when using a remote server, it crashes ParaView. Is there a way for us to get that value using remote server?

@cory.quammen

Thanks as always,
-Phil

Hmmm, FetchData should handle both built-in and remote servers. Testing locally, I that seems to be the case.

I suggest trying @Kenichiro-Yoshimi’s solution, which is a similar approach. Note that Fetch (different from the FetchData I mentioned) is defined in paraview.servermanager.Fetch.

Hi Cory… Yes that was may mistake your solution appears to work for both now. I just made a stupid mistake and now all works.

Thanks again!

@vgweirs Greg, I know you were having issues with fetches a while back. Sounds like it now works.
Alan

Hello,
I am little bit lost with this, from what I understand we extract the data and transform it to vtk, and then transform it into the values.
I was trying to get for example, the resulting values of a slice with different offset.
the slice I defined as it follows:

slice1 = Slice(registrationName='Slice1', Input=inlet)
slice1.SliceType = 'Plane'
slice1.HyperTreeGridSlicer = 'Plane'
slice1.SliceType.Origin = [0.0, 0.0, 0.0]
slice1.HyperTreeGridSlicer.Origin = [0.0, 0.0, 0.0]
slice1.SliceOffsetValues = [-0.03534826681814725, -0.03390548041740654, -0.03246269401666584, -0.031019907615925137, -0.02957712121518443, -0.028134334814443728, -0.026691548413703026, -0.02524876201296232, -0.023805975612221614, -0.02236318921148091, -0.02092040281074021, -0.019477616409999503, -0.0180348300092588, -0.016592043608518094, -0.015149257207777392, -0.013706470807036686, -0.012263684406295983, -0.01082089800555528, -0.009378111604814575, -0.007935325204073872, -0.0064925388033331664, -0.005049752402592464, -0.003606966001851758, -0.002164179601111052, -0.000721393200370353, 0.000721393200370353, 0.002164179601111059, 0.003606966001851758, 0.005049752402592464, 0.00649253880333317, 0.007935325204073876, 0.009378111604814575, 0.01082089800555528, 0.012263684406295987, 0.013706470807036686, 0.015149257207777392, 0.016592043608518098, 0.018034830009258804, 0.019477616409999503, 0.02092040281074021, 0.022363189211480915, 0.023805975612221614, 0.02524876201296232, 0.026691548413703026, 0.02813433481444373, 0.029577121215184438, 0.031019907615925144, 0.032462694016665836, 0.03390548041740654, 0.03534826681814725]
slice1.SliceType.Normal = [0.0, 1.0, 0.0]

slice1 has point and cell data, then in my script I use the following lines:

calc_vtk = servermanager.Fetch(slice1)
values= calc_vtk.GetPointData().GetArray('k').GetTuple1(0)

and I am getting the error on ‘GetPointData’ already
I read it around the forum that there is problems with multiblocks so in any case I tried to merge blocks filter so:

calc_vtk = servermanager.Fetch(MergeBlocks(registrationName='MergeBlocks2', Input=slice1))
calc_vtk.GetPointData().GetArray('k').GetTuplel()

and now I am getting an error over the GetTuplel()
thanks in advance