Author Topic: AttributeError: DataCollection does not contain the particle property  (Read 583 times)

dekoning

  • Newbie
  • *
  • Posts: 9
Hi,

I'm trying to define a new particle property called "Bisectors" to contain
a 3 dimensional vector that I'd like to plot. The python script (to be called after creating bonds) is included below.
Everything seems to be fine up to the moment I make the call

vector_display = output.particle_properties.bisectors.display

where it complains: AttributeError: DataCollection does not contain the particle property 'bisectors'.

But when I call

print(list(output.particle_properties.keys()))

it returns ['Particle Identifier', 'Particle Type', 'Position', 'Bisectors']. So it seems to be there.

Any ideas as to what might be going on?

Maurice

********************************************

from ovito.vis import VectorDisplay
import numpy as np

def modify(frame, input, output):
   print("The input contains %i particles." % input.number_of_particles)
   bisectors = output.create_user_particle_property("Bisectors","float",3)
   particle_positions = input.particle_properties.position.array
   nparticles = len(particle_positions)
   bonds_array = input.bonds.array
   bond_vectors = particle_positions[bonds_array[:,1]] - particle_positions[bonds_array[:,0]]
   bond_vectors += np.dot(output.cell.matrix[:,:3], output.bonds.pbc_vectors.T).T
   type = input.particle_properties.particle_type.array
   
   print(output)
   print(list(output.particle_properties.keys()))
   
   for i in range(len(bonds_array)):
      particle = bonds_array[i,0]
      if type[particle]==1:
         bisectors.marray[particle] += bond_vectors[i,:]       
   bisectors_norm = np.empty([nparticles],float)
   
   for i in range(nparticles):
      bisectors_norm = sum(bisectors.marray[i,:]**2)
      if type == 1:
         bisectors.marray[i,:] /= np.sqrt(bisectors_norm)

   vector_display = output.particle_properties.bisectors.display
   vector_display.enabled = True 
   vector_display.color = (0.0, 1.0, 0.0)
   print("Done!")

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 515
Re: AttributeError: DataCollection does not contain the particle property
« Reply #1 on: December 13, 2017, 06:21:25 PM »
Hi Maurice,

This type of property access

   particle_properties.bisectors

doesn't work for user-defined particle properties, only for standard properties such as "position". In the new OVITO version 3.0 it has been marked as deprecated, even for standard properties (see this page). Instead, use standard Python indexing. That means, use a key string to look up the particle property in the dictionary, i.e.

   particle_properties['Bisectors']

-Alex

dekoning

  • Newbie
  • *
  • Posts: 9
Re: AttributeError: DataCollection does not contain the particle property
« Reply #2 on: December 13, 2017, 07:36:04 PM »
Thanks,

Now, how can I draw the 3-component elements of output.particle_properties["Bisectors"] as an arrow?

For instance, with a standard property such as one can write

vector_display = node.source.particle_properties.force.display
vector_display.color = (1.0, 0.5, 0.5)

but this (i.e., adding .display at the end) is not working for the Bisectors case.

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 515
Re: AttributeError: DataCollection does not contain the particle property
« Reply #3 on: December 13, 2017, 11:09:18 PM »
Current versions of OVITO require you to "abuse" one of the standard properties for this, because user-defined particle properties are created without an attached display object. Standard properties that are created with an attached VectorDisplay object are the "Force", "Displacement" and "Dipole Orientation" properties.

That being said, have you already tried creating and assigning a new instance of the VectorDisplay class to the display attribute of the property data object? Perhaps this allows you to activate the visualisation of arrows even for your user-defined property.
For instance, In you modify() function, which creates the "Bisector" property:

Code: [Select]
bisectors.display = VectorDisplay()
bisectors.display.enabled = True
...

dekoning

  • Newbie
  • *
  • Posts: 9
Re: AttributeError: DataCollection does not contain the particle property
« Reply #4 on: December 14, 2017, 12:49:01 PM »
Thanks Alex, your suggestion of assigning a new instance of the VectorDisplay class worked.