Author Topic: How to save data as a txt file?  (Read 436 times)

SC

  • Newbie
  • *
  • Posts: 5
How to save data as a txt file?
« on: October 17, 2018, 10:17:19 PM »
Dear Dr. Stukowski,

Can you please tell me how i can save the results obtained by partial RDF (The first python script on this page http://forum.ovito.org/index.php?topic=131.0) in a txt file?

I used this command line :
Code: [Select]
np.savetxt("partial_rdf.dat", np.column_stack((radii,rdf))) but got an error: unindent does not match any outer indentation level

Best regards,

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 113
Re: How to save data as a txt file?
« Reply #1 on: October 18, 2018, 11:03:01 AM »
Dear SC,

indentation matters in python, see e.g. here
https://docs.python.org/3/reference/lexical_analysis.html?highlight=indentation.
The problem is the leading whitespace, which you need to remove.

Maybe you'll find these python tutorials for beginners helpful:  https://www.python.org/about/gettingstarted/

-Constanze

SC

  • Newbie
  • *
  • Posts: 5
Re: How to save data as a txt file?
« Reply #2 on: October 18, 2018, 12:47:27 PM »
Dear Constanze,

Thanks for your help. I used
Code: [Select]
strip() to remove leading whitespace :

Code: [Select]
    RDF = np.column_stack((radii,rdf))
    RDF = RDF.strip()
    np.savetxt("partial_rdf.dat", RDF)

But i still get error. sorry I am not good at Python scripting.

Best regards,

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 113
Re: How to save data as a txt file?
« Reply #3 on: October 18, 2018, 01:30:23 PM »
Dear SC,

to me it looked like there was a leading whitespace at the beginning of your source code which needs to be removed.

Code: [Select]
#correct
np.savetxt("partial_rdf.dat", np.column_stack((radii,rdf)))
#here you need to remove the leading white space
 np.savetxt("partial_rdf.dat", np.column_stack((radii,rdf)))

Sorry for the confusion, in case there is none, can you copy the whole code here? Maybe the indentation level of the previous lines doesn't match your last line.
In any case, you don't need to modify the RDF by doing this RDF.strip().

-Constanze
« Last Edit: October 18, 2018, 01:37:28 PM by Constanze Kalcher »

SC

  • Newbie
  • *
  • Posts: 5
Re: How to save data as a txt file?
« Reply #4 on: October 18, 2018, 03:02:18 PM »
Dear Constanze,

I appreciate your help. Here is the code:

Code: [Select]
import numpy as np

num_bins = 100

def modify(frame, input, output):
hist, bin_edges = np.histogram(input.bond_properties.length.array, bins=num_bins)

rho = input.number_of_particles / input.cell.volume
factor = 4./3. * np.pi * rho * input.number_of_particles

radii = bin_edges[:-1]
radii_right = bin_edges[1:]
rdf = hist / (factor * (radii_right**3 - radii**3))

    np.savetxt("partial_rdf.dat", np.column_stack((radii,rdf)))

Best regards,

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 113
Re: How to save data as a txt file?
« Reply #5 on: October 18, 2018, 03:26:14 PM »
Ah okay I see, the problem is that np.savetxt(...) is missing a couple of whitespaces in the beginning of the line. You need to make it match the indentation level of the previous lines, so it's part of the modify function:

Code: [Select]
def modify(frame, input, output):
hist, bin_edges = np.histogram(input.bond_properties.length.array, bins=num_bins)

rho = input.number_of_particles / input.cell.volume
factor = 4./3. * np.pi * rho * input.number_of_particles

radii = bin_edges[:-1]
radii_right = bin_edges[1:]
rdf = hist / (factor * (radii_right**3 - radii**3))
np.savetxt("partial_rdf.dat", np.column_stack((radii,rdf)))



-Constanze