Author Topic: Problems with the access of property data  (Read 97 times)

MikeShi

  • Newbie
  • *
  • Posts: 2
Problems with the access of property data
« on: November 05, 2018, 05:36:19 AM »
Hi,

I try to modify the position data via ovito python interface. But I always met the following problem:
Code: [Select]
RuntimeError: Modifying the values of this property is not allowed, because it is currently shared by more than one property container or data collection. Please explicitly request a mutable version of the property by
using the '_' notation.
I have read the Ovito python interface documentation and used "with" statement to make the array writable. But it doesn't work. My current Ovito version is 3.0 dev 293. Code is also attached as following:
Code: [Select]
from ovito.io import import_file, export_file
pipeline = import_file('650000.cfg')
data = pipeline.compute()
pos_property = data.particles['Position']
with pos_property:
    pos_property[0] = (0,0,0)
Hope you can help me solve it.

Thank you very much!

Linyuan

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 501
Re: Problems with the access of property data
« Reply #1 on: November 05, 2018, 08:05:19 AM »
Dear Linyuan,

I'm sorry for the confusion. This error is due to recent changes to the Python data access interface of OVITO and also the underlying data model (introduced with build 3.0.0-dev264), which have not been documented yet. I will try to update the documentation within the coming days (maybe weeks) to explain this important change in more detail.

Here is the main rule you need to follow according to the changed data access interface:

Whenever you are going to modify a data object in a data collection in any way, you need to explicitly request a mutable version of that object. This is done by appending a '_' to the end of the identifier of the object.

In your particular case, you need to write:
Code: [Select]

pos_property = data.particles_['Position_']
with pos_property:
    pos_property[0] = (0,0,0)

A _ has been appended to the particle property name "Position", because you are going to modify the per-particle values stored in that Property object. And note that the _ was also appended to the DataCollection.particles accessor, because you are going to modify one of the sub-objects of this PropertyContainer.

This brings me to the second rule:
A mutable version of a data object can only be requested from a parent data object that is mutable itself. In other words, when modifying a sub-object in the hierarchy of data objects, the container object needs to be made mutable as well using the _ notation.

Any attempt to modify a data object that is marked as read-only (immutable state) will raise an error like the one you saw. By default, all data objects that get passed to a user-defined modifier function by the system, or which are contained in a DataCollection produced by the Pipeline.compute() method, are immutable. The reason is that these data objects are owned by the data pipeline system and must be preserved in their original state to avoid unexpected side effects. Requesting a mutable version of these objects using the _ notation creates a data copy that is safe to modify by the user code.

-Alex
« Last Edit: November 05, 2018, 08:09:26 AM by Alexander Stukowski »

MikeShi

  • Newbie
  • *
  • Posts: 2
Re: Problems with the access of property data
« Reply #2 on: November 06, 2018, 04:56:15 PM »
Hi Alex,

Thanks for your kind and detailed explanation. I have modified my code and it works smoothly now.

Cheers,

Linyuan