[PYTHON] Summary of vtkThreshold (updated from time to time)

What is vtkThreshold?

A filter that extracts only elements that contain values within the specified range.

** class reference ** https://vtk.org/doc/nightly/html/classvtkThreshold.html

Specifically, the example in the following paraview is easy to understand

Check the function with paraview

For the calculation example, I used pitzDaily from the tutorial of ʻOpenFOAM`.

Before applying Threshold

image.png

After reading the calculation result, press this button image.png

After applying Threshold

Result of applying a specified range of 5 to 15 for pressure p

image.png

After applying the TInvert option

Take the complement

image.png

Operation check with python

environment

python 3.7 vtk 8.1.2

Confirmation method


import Vtk
print(vtk.vtkVersion.GetVTKSourceVersion())
>> vtk version 8.1.2

Base code

I use vtkOpenFOAMReader to read the result of OpenFOAM About vtkOpenFOAMReader, I will also summarize it in here.

import vtk
#Read the result of OpenFOAM
filename = "case1.foam"
reader = vtk.vtkOpenFOAMReader()
reader.SetFileName(filename)
reader.CreateCellToPointOn()
reader.DecomposePolyhedraOn()
reader.EnableAllCellArrays()
reader.Update()
#Apply the latest Time result
n_step = reader.GetTimeValues().GetNumberOfValues()
latest_time = reader.GetTimeValues().GetValue(n_step-1)
reader.UpdateTimeStep(latest_time)
reader.Update()

filter_threshold = vtk.vtkThreshold()
filter_threshold.SetInputConnection(reader.GetOutputPort())

###################
#Add settings here#
###################
filter_threshold.Update()


filter = vtk.vtkGeometryFilter()
filter.SetInputConnection(filter_threshold.GetOutputPort())
filter.Update()

mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(filter.GetOutputPort()) #Set filter in mapper
mapper.SetScalarModeToUseCellFieldData() #Set for scalar data

# renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)            #Set actor in renderer

##Background color setting
renderer.GradientBackgroundOn()      #Set gradient background
renderer.SetBackground2(0.2,0.4,0.6) #Top color
renderer.SetBackground(1,1,1)        #Bottom color

#Window
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)         #Set renderer in Window
iren = vtk.vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
renWin.SetSize(850, 850)
renWin.Render()
iren.Start();

Basic

ThresholdBetween()

Limitation by specifying upper and lower limits


#Set the threshold between 5 and 15
filter_threshold.ThresholdBetween(5,15)

#enum FieldAssociations
#  {
#    FIELD_ASSOCIATION_POINTS,
#    FIELD_ASSOCIATION_CELLS,
#    FIELD_ASSOCIATION_NONE,
#    FIELD_ASSOCIATION_POINTS_THEN_CELLS,
#    FIELD_ASSOCIATION_VERTICES,
#    FIELD_ASSOCIATION_EDGES,
#    FIELD_ASSOCIATION_ROWS,
#    NUMBER_OF_ASSOCIATIONS
#  };
FIELD_ASSOCIATION_POINTS = 0
FIELD_ASSOCIATION_CELLS = 1
# "p"Is filtering against pressure
filter_threshold.SetInputArrayToProcess(0,0,0,FIELD_ASSOCIATION_CELLS ,"p")
filter_threshold.Update()

Consistent with the results in paraview above

image.png

If you don't know how to use the method, the help function may help.

help(filter_threshold.SetInputArrayToProcess)

>> 

Help on built-in function SetInputArrayToProcess:

SetInputArrayToProcess(...) method of vtkFiltersCorePython.vtkThreshold instance
    V.SetInputArrayToProcess(int, int, int, int, string)
    C++: virtual void SetInputArrayToProcess(int idx, int port,
        int connection, int fieldAssociation, const char *name)
    V.SetInputArrayToProcess(int, int, int, int, int)
    C++: virtual void SetInputArrayToProcess(int idx, int port,
        int connection, int fieldAssociation, int fieldAttributeType)
    V.SetInputArrayToProcess(int, vtkInformation)
    C++: virtual void SetInputArrayToProcess(int idx,
        vtkInformation *info)
    V.SetInputArrayToProcess(int, int, int, string, string)
    C++: virtual void SetInputArrayToProcess(int idx, int port,
        int connection, const char *fieldAssociation,
        const char *attributeTypeorName)
    
    Set the input data arrays that this algorithm will process.
    Specifically the idx array that this algorithm will process
    (starting from 0) is the array on port, connection with the
    specified association and name or attribute type (such as
    SCALARS). The fieldAssociation refers to which field in the data
    object the array is stored. See vtkDataObject::FieldAssociations
    for detail.

ThresholdByUpper()

Limit by specifying the upper limit Only areas larger than the specified value are displayed

#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByUpper(10)

Nothing is displayed because the target area does not exist image.png

ThresholdByLower()

Limit by specifying the lower limit Only areas smaller than the specified value are displayed

#filter_threshold.ThresholdBetween(5,15)
filter_threshold.ThresholdByLower(5)

As a result, it matched the result of Invert in the above paraview.

image.png

Invert relationship

Apparently the Invert relationship is already implemented in c ++, but not in the python version.

filter_threshold.SetInvert(True)

AttributeError  
---> 31 filter_threshold.SetInvert(True)
AttributeError: 'vtkFiltersCorePython.vtkThreshold' object has no attribute 'SetInvert'

Other methods

GetUpperThreshold Get the upper limit of the threshold

filter_threshold.ThresholdBetween(-100,100)
filter_threshold.GetUpperThreshold()
>> 100

GetLowerThreshold Get the lower limit of the threshold

filter_threshold.ThresholdBetween(-100,100)
filter_threshold.GetLowerThreshold()
>> -100

Set/GetAttributeMode

filter_threshold.SetAttributeModeToDefault()
filter_threshold.GetAttributeMode()
>>0

filter_threshold.SetAttributeModeToUsePointData()
filter_threshold.GetAttributeMode()
>>1
filter_threshold.GetAttributeModeAsString()
>>'UsePointData'

filter_threshold.SetAttributeModeToUseCellData()
filter_threshold.GetAttributeMode()
>>2
filter_threshold.GetAttributeModeAsString()
>>'UseCellData'

filter_threshold.SetAttributeMode(2)
filter_threshold.GetAttributeModeAsString()
>>'UseCellData'

I will update it when I feel like it

Recommended Posts

Summary of vtkThreshold (updated from time to time)
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
vtkClipPolyData / DataSet Summary (Updated from time to time)
[Updated from time to time] Summary of design patterns in Java
Machine learning python code summary (updated from time to time)
[Updated from time to time] Review of Let Code NumPy
Engineer vocabulary (updated from time to time)
Tensorflow memo [updated from time to time]
Private Python handbook (updated from time to time)
[Updated from time to time] PostmarketOS related notes
[Updated from time to time] LetCode algorithm and library
OpenFOAM post-processing cheat sheet (updated from time to time)
progate Python learning memo (updated from time to time)
Useful help sites, etc. (updated from time to time)
Summary of folders where Ruby, Python, PostgreSQL, etc. are installed on macOS (updated from time to time)
Apache settings, log confirmation, etc. (* Updated from time to time)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
I read the Chainer reference (updated from time to time)
(Updated from time to time) Summary of machine learning APIs that allow you to quickly build apps by Team AI
A memorandum of commands, packages, terms, etc. used in linux (updated from time to time)
(Updated from time to time) Storage location of various VS Code configuration files Memorandum memo
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Python (from first time to execution)
Summary of how to use csvkit
List of my articles that may be useful in competition pros (updated from time to time)
[Note] AI / machine learning / python related websites [updated from time to time]
Summary of advantages (disadvantages) when switching from CUnit to Google Test
[Python] Summary of how to use pandas
[Python] Summary of array generation (initialization) time! !! !!
Easy conversion from UTC to local time
[Python2.7] Summary of how to use unittest
From Attention of Zero Tsuku 2 to Transformer
Summary of procedures up to PyPI registration
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Summary of how to write AWS Lambda
Introduction to Scapy ① (From installation to execution of Scapy)
Updated to Python 2.7.9
measurement of time
[Ansible installation procedure] From installation to execution of playbook
Summary of how to import files in Python 3
Sum from 1 to 10
Summary of how to use MNIST in Python
Change the decimal point of logging from, to.
Summary of studying Python to use AWS Lambda
From the introduction of pyethapp to the execution of contract
The story of moving from Pipenv to Poetry
(Preserved version: Updated from time to time) A collection of useful tutorials for data analysis hackathons by Team AI
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the Java language
[Updated from time to time] Python memos often used for data analysis [N division, etc.]
The wall of changing the Django service from Python 2.7 to Python 3
I want to detect images of cats from Instagram
Summary of tools needed to analyze data in Python
[Blender] Summary of how to install / update / uninstall add-ons
DataNitro, implementation of function to read data from sheet
[Python] Summary of how to specify the color of the figure
Convert elements of numpy array from float to int
Summary of how to share state with multiple functions