hey @Kenichiro-Yoshimi,
sorry for bothering with a direct tag, from the post you made on my other post, I tried implementing something based on it that would use the same logic, but I am not achieving a speed up (actually it takes the double time), would you mind giving me your oppinion?
what I came up to:
def extractSelectionByListOfValues_test(filter=[], listOfValues=[]):
if filter==[]:
filter= GetActiveSource()
if isinstance(filter,str):
filter=FindSource(filter)
# create a new 'Programmable Filter'
programmableFilter1 = ProgrammableFilter(registrationName='ProgrammableFilter1', Input=filter)
# find source
# Properties modified on programmableFilter1
programmableFilter1.Set(
RequestInformationScript='',
RequestUpdateExtentScript='',
PythonPath='',
Script=f"""import numpy as np
from vtkmodules.vtkCommonCore import vtkIdList
from vtkmodules.vtkCommonDataModel import vtkSelectionNode, vtkSelection
from vtkmodules.vtkFiltersExtraction import vtkExtractSelection
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy
from vtkmodules.util.numpy_support import numpy_to_vtkIdTypeArray
# --- Get inputs ---
def get_vtk_dataset(wrapped_input):
vtk_obj = wrapped_input.VTKObject
if vtk_obj.IsA("vtkMultiBlockDataSet"):
return vtk_obj.GetBlock(0)
return vtk_obj
typeOfIDs='CellIds'
#get the input object
vtk_ref = get_vtk_dataset(inputs[0]) # removedLayer
wrap_ref = dsa.WrapDataObject(vtk_ref)
#get the list of IDs in a np.array for typeOfIDs
gid_ref = np.asarray(wrap_ref.CellData[typeOfIDs]).ravel() #typeOfIDs="CellIds"
cell_array = np.asarray(wrap_ref.CellData["CellIds"])
listOfValues={listOfValues}
if listOfValues:
if isinstance(listOfValues,list):
gid_toExtract=np.array(listOfValues)
elif isinstance(listOfValues,numpy.ndarray):
gid_toExtract=listOfValues
else:
error
else:
vtk_tgt = get_vtk_dataset(inputs[1]) # finalChannel
nCells = vtk_tgt.GetNumberOfCells()
wrap_tgt = dsa.WrapDataObject(vtk_tgt)
gid_toExtract = np.asarray(wrap_tgt.CellData[typeOfIDs]).ravel() #typeOfIDs="CellIds"
out = self.GetOutputDataObject(0)
# Sort ref, then searchsorted to find which target points exist in ref
gid_ref_sorted = np.sort(gid_ref)
pos = np.searchsorted(gid_ref_sorted, gid_toExtract)
pos_clipped = np.clip(pos, 0, len(gid_ref_sorted) - 1)
mask = gid_ref_sorted[pos_clipped] == gid_toExtract
shared_local_ids = np.nonzero(mask)[0]
selection_ids = numpy_to_vtkIdTypeArray(pos_clipped[shared_local_ids].astype(np.int64), deep=1)
node = vtkSelectionNode()
node.SetFieldType(vtkSelectionNode.CELL)
node.SetContentType(vtkSelectionNode.INDICES)
node.SetSelectionList(selection_ids)
sel = vtkSelection()
sel.AddNode(node)
extract = vtkExtractSelection()
extract.SetInputDataObject(0, vtk_ref)
extract.SetInputDataObject(1, sel)
extract.Update()
out.ShallowCopy(extract.GetOutputDataObject(0))
""",
)
return programmableFilter1
vs
def extractSelectionByListOfValues_old(filter=[], listOfValues=[]):
SetActiveSource(filter)
QuerySelect(QueryString=f'(isin({fieldName}, {listOfValues}))', FieldType=FieldType, InsideOut=complementary)
extracted = ExtractSelection(Input=filter,registrationName=registrationName)
SetActiveSource(filter)
ClearSelection()
return extracted
I was hopping a speedup due to paraview creating the ‘pink cells’ when using QuerySelect, but for my current test, extractSelectionByListOfValues_old is running in 14 s, where extractSelectionByListOfValues_test is taking 32 s.
the filter is a unstructured grid of 630 209 cells and for the test I using the function several times for different lengths of listOfValues ([1766, 367694, 2, 86990, 4, 27714, 20387, 11144, 26251, 8742, 27682, 10872, 18895, 19929, 1642, 4, 5, 4, 480, 2] cells each test) so rader small, as in your test case the speed was crazy even for larger examples, i am afraid i am missing something.
thanks (and sorry for the tag, but since I was basing the funciton on your post, would appreciate your oppinion)