Author Topic: Python script for cluster size  (Read 707 times)

ftayebek

  • Newbie
  • *
  • Posts: 2
Python script for cluster size
« on: January 22, 2018, 04:56:05 PM »
I want to calculate cluster size in a colloidal system.  I tried using the following command suggested by the manual (the path has been modified). 

import ovito
import numpy

import os
path="c:\\Users\ft3859\Desktop"
output_filepath = "Cluster3.txt"
output = ovito.dataset.selected_node.compute()
cluster_sizes = numpy.bincount(output.particle_properties['Cluster'].array)
numpy.savetxt(output_filepath, cluster_sizes)

The error  I get is as the following:

The Python script has exited with an error.
Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "C:\Program Files\Ovito\lib\site-packages\numpy\lib\npyio.py", line 1022, in savetxt
    fh = open(fname, 'wb')
PermissionError: [Errno 13] Permission denied: 'Cluster3.txt'

Thanks for spending your time to address the problem.


Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Python script for cluster size
« Reply #1 on: January 22, 2018, 06:34:50 PM »
Since you are working under Windows, the current working directory while you are running this script in Ovito is probably "C:\Program Files\Ovito\".
That means the call to numpy.savetxt("Cluster3.txt") will try to write to the output path "C:\Program Files\Ovito\Cluster3.txt", which probably refers to a write-protected location. The file I/O operation fails.

The solution is to specify an absolute output path by prepending the destination directory to the file name:

output_filepath = path + "\\Cluster3.txt"

ftayebek

  • Newbie
  • *
  • Posts: 2
Re: Python script for cluster size
« Reply #2 on: January 22, 2018, 07:14:09 PM »
Thanks for the quick reply.  I corrected that part but still I do not get any output from the script.  Ovito shows "Performing cluster analysis 100%" in the status bar, but I guess it still cannot print the results in the given file with the given path. 

import ovito
import numpy


path="C:\\Users\\ft3859\\Desktop"

output_filepath = path + "\\Cluster3.txt"
output = ovito.dataset.selected_node.compute()
cluster_sizes = numpy.bincount(output.particle_properties['Cluster'].array)
numpy.savetxt(output_filepath, cluster_sizes)


Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Python script for cluster size
« Reply #3 on: January 22, 2018, 07:21:01 PM »
Is this the entire script code? It surprises me that the initial script version was failing with an error message in the numpy.savetxt() line, but now the corrected script, which doesn't generate the permission-denied error anymore, doesn't produce any file output. I cannot imagine that it gets stuck inside the savetxt() function.

How are you running this script? Are you using the "Run Script file" function form the menu (which would be correct), or are you running this in the context of a Python script modifier (which would be wrong)?

debasis Sengupta

  • Newbie
  • *
  • Posts: 12
Re: Python script for cluster size
« Reply #4 on: July 03, 2018, 04:04:59 AM »
I have a very similar problem.  Today I tried using the script you mentioned (also in the webpage).  Ovito does not respond (screen greys out) and then crashes.  I ran it through Scripting --> Run Script File.  I have approximately 55000 particles. I am using cut-off distance option. 
Any reason why?

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: Python script for cluster size
« Reply #5 on: July 03, 2018, 10:07:45 AM »
Could you please let us know which operating system and version of OVITO you are using and upload the data file and the script file as an attachment here? Then I will try to reproduce the problem. Right now I don't have a clear idea what is happening.
... And if you perform any actions in the graphical user interface before running the script, please save everything as a .ovito state file and post that one too. Thanks!