Author Topic: How to coloring particles by its properties  (Read 784 times)

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #15 on: August 06, 2018, 05:20:56 PM »
I want to produce an image or see the computation results in the viewports.

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 475
Re: How to coloring particles by its properties
« Reply #16 on: August 06, 2018, 05:27:13 PM »
If you intend to produce a visual output, it's important that you add the pipeline (or rather its output) to the scene. This doesn't happen automatically for datasets imported via the import_file() function. See the first note on this page in the green box:

http://ovito.org/manual_testing/python/introduction/file_io.html

To add the pipeline's output to the scene and make it visible in the viewports of OVITO and in rendered images, add a call to the add_to_scene() method to your script:

Code: [Select]
node.add_to_scene()

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 475
Re: How to coloring particles by its properties
« Reply #17 on: August 06, 2018, 05:39:52 PM »
Typically, you would insert this statement right after the line that creates the pipeline, i.e. right after the call to import_file().

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #18 on: August 06, 2018, 08:55:10 PM »
I am running successful!
Thank you very much,Mr.Alexander!

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 475
Re: How to coloring particles by its properties
« Reply #19 on: August 08, 2018, 01:26:58 PM »
Your script still didn't do the right thing. I see that you have written a for-loop over all input simulation frames. Within that loop, you do the following:

  • request the output of the data pipeline
  • perform some calculation on that output data
  • define a modifier function, which takes that computed data array to create a particle property from it, and append the function to the data pipeline
  • append a Color Coding modifier to the pipeline

Note that steps 3 and 4 will have no effect during the first loop iteration, because the pipeline was already evaluated in step 1, without the two modifiers being inserted into the pipeline yet. Thus, the user-defined modifier function is never called by the pipeline system. Furthermore, in consecutive loop iterations, the pipeline will get longer and longer, because you insert the user-defined modifier function and the Color Coding modifier multiple times.

The correct approach should look as follows (I have rearranged your script accordingly, see the attached file):

  • define a custom modifier function that takes whatever input data is provided by the pipeline system and performs some calculation on it; append that modifier function to the pipeline
  • append a Color Coding modifier to the pipeline
  • loop over the simulation frames and evaluate the pipeline at each timestep

Note that in this approach, the pipeline is first set up completely. Once all modifiers have been inserted into the pipeline, the static pipeline can be evaluated multiple times, once for each frame. The pipeline system will repeatedly call the user-defined function with the correct input data.
« Last Edit: August 09, 2018, 02:29:22 PM by Constanze Kalcher »

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #20 on: August 08, 2018, 02:23:16 PM »
Thanks your reply.
So I need to create a modifier function at first and then loop over the frames.
I know where I am wrong now.
Thanks you again,Mr.Alexander.

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #21 on: August 08, 2018, 03:12:16 PM »
After execute the modified version, the old problem still happen.
RuntimeError: Data pipeline evaluation failed: Modifier 'Color coding' reported: The property with the name 'myproperty' does not exist.
This error happen at final line:
Code: [Select]
data = node.compute(frame_index)

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 475
Re: How to coloring particles by its properties
« Reply #22 on: August 08, 2018, 04:18:20 PM »
I don't have an explanation for this at the moment. I took the script, removed most of the calculation code and tested it with one of my data files. I didn't see any errors in this case.

Maybe the problem only occurs with your input files, or originates from the actual calculation code which I had to remove for testing. Could you please post your input data files, at least some of them? Then I can test the script with the original input data and investigate why it stops. Please let me also know which exact version of OVITO you are using. Thanks.

Alexander Stukowski

  • Administrator
  • Sr. Member
  • *****
  • Posts: 475
Re: How to coloring particles by its properties
« Reply #23 on: August 09, 2018, 09:49:36 AM »
Thanks for uploading the data files. I ran the script on these files and got the following error message:

Code: [Select]
moglios:eason1021 stuko$ ~/prj/ovito/build/debug/Ovito.app/Contents/MacOS/ovitos.exe test88_modified_orig.py
Traceback (most recent call last):
  File "test88_modified_orig.py", line 349, in compute_myproperty
    locals()["px_f_%s"%k] = np.dot(locals()["px_matrix_%s"%k],locals()["px_matrix_inv_ref%s"%k])
KeyError: 'px_matrix_inv_ref0'
Traceback (most recent call last):
  File "test88_modified_orig.py", line 812, in <module>
    data = node.compute(frame_index)
  File "/Users/stuko/prj/ovito/build/debug/Ovito.app/Contents/MacOS/../Resources/python/ovito/pipeline/pipeline_class.py", line 178, in _Pipeline_compute
    raise RuntimeError("Data pipeline evaluation failed: %s" % state.status.text)
RuntimeError: Data pipeline evaluation failed: Modifier 'Color coding' reported: The property with the name 'myproperty' does not exist.
ERROR: The Python script 'test88_modified_orig.py' has exited with an error.

Note that these are actually two errors. The first one occurs in line 349 of the script file ("KeyError: 'px_matrix_inv_ref0'"). The second one, in line 812, is only a subsequent error. Because the user-defined modifier function raises an error, the new particle property is never created and that's why the Color Coding modifier complains about the missing property.

The problem in your script seems to be the usage of the locals() function in various places. Note that it may return a different dictionary depending on the context from where you call it. Inside the user-defined modifier function you have a different set of locals than in the main body of the program where you process the reference structure. Furthermore, modifying the locals() dictionary is generally discouraged any, see the official Python docs: https://docs.python.org/3/library/functions.html#locals

I suggest you replace it with a simple dictionary that you create at the beginning of your program:

Code: [Select]
myvars = {}

Then replace all occurrences of "locals()" in your code with "myvars".

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #24 on: August 09, 2018, 02:26:05 PM »
Thank you,Mr.Alexander.
You really help me a lot!
I will try to fix my error.And use this modifier to compare my old result.
Thank you again.

Another thing,can I please you remove the test88modified temporarily?
Because the calculation method has not been validated,and I want to keep the process secret.
After verification its accuracy,I will post it again in the net.
Thanks.

Constanze Kalcher

  • Administrator
  • Jr. Member
  • *****
  • Posts: 81
Re: How to coloring particles by its properties
« Reply #25 on: August 09, 2018, 02:30:36 PM »
Hi,
I removed the attached file in Alexander's post.
Best,
Constanze

eason1021

  • Newbie
  • *
  • Posts: 21
Re: How to coloring particles by its properties
« Reply #26 on: August 10, 2018, 08:34:59 AM »
Thanks your help.
Sincere thanks to you.