Filtering by cell/point ID

I’m using the paraview’s python scripting capability. I’m looking for a way to filter data by cell and/or point ID, so that a chain of filters where only a subset of the mesh is of interest can run more quickly. We are using paraview as a backend mesh reader/calculator, but we’re not using the visualization capabilities now since we need that to be done in a javascript frontend (bonus question: is there a javascript/react library for paraview similar to vtkjs?)

I’ve seen paraview.simple.SelectIDs but this doesn’t seem to work for me:

point_selection = pvs.SelectIDs(IDs=point_ids, FieldType='POINT')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[33], line 1
----> 1 point_selection = pvs.SelectIDs(IDs=point_ids, FieldType='POINT')

File ~/Documents/ZooHill/Osprey/osprey-react/dev-tools-env/lib/python3.10/site-packages/paraview/selection.py:390, in SelectIDs(IDs, FieldType, ContainingCells, Source, Modifier)
    377 def SelectIDs(IDs=[], FieldType='POINT', ContainingCells=False, Source=None, Modifier=None):
    378     """Select attributes by attribute IDs.
    379
    380     - IDs - list of IDs of attribute types to select. Defined as (process number, attribute ID) pairs
   (...)
    388         should modify the existing selection.
    389     """
--> 390     _selectIDsHelper('IDSelectionSource', **locals())

File ~/Documents/ZooHill/Osprey/osprey-react/dev-tools-env/lib/python3.10/site-packages/paraview/selection.py:330, in _selectIDsHelper(proxyname, IDs, FieldType, ContainingCells, Source, Modifier)
    327 if not Source:
    328     Source = paraview.simple.GetActiveSource()
--> 330 repr = paraview.simple.GetRepresentation(Source)
    332 import paraview.vtk as vtk
    333 reprCollection = vtk.vtkCollection()

File ~/Documents/ZooHill/Osprey/osprey-react/dev-tools-env/lib/python3.10/site-packages/paraview/simple.py:939, in GetRepresentation(proxy, view)
    937     view = active_objects.view
    938 if not view:
--> 939     raise ValueError("view argument cannot be None.")
    940 if not proxy:
    941     proxy = active_objects.source

ValueError: view argument cannot be None.

Any pointers would be much appreciated, thanks in advance!

From what I see, Selection is heavily related to View(s), so you cannot select without a view. So you suggest to create a dummy one (like a Spreadsheet view, that is not so costly) and then you should be able to run your request.

For using ParaView on the web, we have Trame framework (trame | trame), but this is more a @jourdain question

Thanks Nicolas. I actually just got this to work with the help of ChatGPT:

import paraview.simple as pvs
from vtkmodules.numpy_interface import dataset_adapter as dsa
...

# This makes a list like [0, id1, 0, id2, 0, id3, ..., 0, idn]. Not sure why it needs to be in this format
point_ids_formatted = list(itertools.chain.from_iterable(zip(itertools.repeat(0), point_ids)))
point_selection = pvs.IDSelectionSource()
point_selection.FieldType = 'POINT'
point_selection.IDs = point_ids_formatted

extracted_points = pvs.ExtractSelection(Input=reader, Selection=point_selection)

data = pvs.servermanager.Fetch(extracted_points)
data_wrapped = dsa.WrapDataObject(data)
num_cells = data_wrapped.GetNumberOfCells()
num_points = data_wrapped.GetNumberOfPoints()

This looks like it correctly filters down both the cells and the points. Does this look reasonable to you? Any disadvantage of using this method vs. creating a Spreadsheet view?

Thanks again! Bryce

Well, if it works …

At least last 4 lines (Fetch and WrapDataObject) are not related to your question.