Forum Navigation
You need to log in to create posts and topics.

Expression selection

Dear all

I want to know whether the expression selection function can adopt the arrays using python?

For example, I used a script like this

# Expression selection:
list = [2693, 5317, 2109, 2143]

for i in range(len(list)):

pipeline.modifiers.append(ExpressionSelectionModifier(expression = 'ParticleIdentifier==list[i]'))

And it results in

RuntimeError: Export of animation frame 0 failed, because data pipeline evaluation did not succeed. Status message: Modifier 'Expression selection' reported: Unexpected token "[i] " found at position 24.

Thanks in advance.

Hi,

please note that expression is a string. Analogous to using an Expression Selection modifier in the Desktop application with the following Boolean expression,

ParticleIdentifier == 2693 || ParticleIdentifier == 5317 || ParticleIdentifier == 2109 || ParticleIdentifier == 2143

you can do the following:

# Expression selection:
list = [2693, 5317, 2109, 2143]

expression = f'ParticleIdentifier=={list[0]}'
for i in range(1, len(list)):
    expression += f' || ParticleIdentifier=={list[i]}'

pipeline.modifiers.append(ExpressionSelectionModifier(expression = expression))

but you can also use directly create your particle selection like this:

data.particles_.create_property("Selection", data = numpy.isin(data.particles.identifiers, list))

Please also have a look at https://www.ovito.org/docs/current/reference/pipelines/modifiers/expression_select.html and https://www.ovito.org/docs/current/python/introduction/data_manipulation.html#creating-new-properties

-Constanze

 

 

Dear Constanze

Thanks a lot for your kind reply and I have solved this problem!

Regards

Lucia

新的OVITO微信频道!
New for our users in China: OVITO on WeChat 

Official OVITO WeChat channel operated by Foshan Diesi Technology Co., Ltd.