Erro in Computing CNA bond indices

Hi there,

First, I run Voronoi analysis and generate neighbor bonds, and then common neighbor analysis. After that, I was trying to use the following code to analyze the CNA bond indices generated by “Voronoi analysis” in the Python Script section in Ovito. However, it always shows that “Invalid Python script. It does not define the function named render().” Could you help me look into this problem?

Thank you,
Han

from ovito.data import *
import numpy

def row_histogram(a):
ca = numpy.ascontiguousarray(a).view([(’’, a.dtype)] * a.shape[1])
unique, indices, inverse = numpy.unique(ca, return_index=True, return_inverse=True)
counts = numpy.bincount(inverse)
return (a[indices], counts)

def modify(frame, data):

# Used below for enumerating the bonds of each particle:
bond_enumerator = BondsEnumerator(data.particles.bonds)
cna_indices = data.particles.bonds['CNA Indices']
# Loop over particles and print their CNA indices.
for particle_index in range(data.particles.count):

    # Print particle index (1-based).
    sys.stdout.write("%i " % (particle_index+1))
    
    # Create local list with CNA indices of the bonds of the current particle.
    bond_index_list = list(bond_enumerator.bonds_of_particle(particle_index))
    local_cna_indices = cna_indices[bond_index_list]

    # Count how often each type of CNA triplet occurred.
    unique_triplets, triplet_counts = row_histogram(local_cna_indices)

    # Print list of triplets with their respective counts.
    for triplet, count in zip(unique_triplets, triplet_counts):
        sys.stdout.write("%s:%i " % (triplet, count))

    # End of particle line
    sys.stdout.write("\n")

Hi Han,

the error message suggests, that you tried to use this script as a Python script viewport layer, see
https://www.ovito.org/docs/current/viewport_layers.php,
which is a user-defined Python function that gets called by OVITO every time a viewport image is being rendered.

What you probably intended to do is to create a user-defined modifier that is part of the pipeline, i.e. that gets executed automatically by OVITO whenever the data pipeline is evaluated.
https://www.ovito.org/docs/current/particles.modifiers.python_script.php
[https://www.ovito.org/docs/current/python/introduction/custom_modifiers.php#writing-custom-modifiers][https://www.ovito.org/docs/current/python/introduction/custom_modifiers.php#writing-custom-modifiers)

In the GUI, please switch to the pipeline modification editor (the tab on the very left), click on Add modification and choose Python Script Modifier from the drop-down menu. Now you can open the Script editor and copy your script there.

-Constanze