Author Topic: Position Y histogram of of each frame  (Read 57 times)

ad

  • Newbie
  • *
  • Posts: 20
Position Y histogram of of each frame
« on: November 29, 2018, 09:59:46 AM »
Hello Alex,

I tried following code to get the histogram and writing them to file:
Code: [Select]
from ovito.io import *
from ovito.modifiers import *
import numpy

node = import_file("/Users/asd/test_sim/analysis/abc_10_g_cc", columns=["Particle Type", "Position.X", "Position.Y", "Position.Z"], multiple_frames = True)

def histogram(frame, input, output):
    output.attributes["Histogram"] = numpy.histogram(input.particles["Position"][:,1])
node.modifiers.append(PythonScriptModifier(function=histogram))
export_file(node, "/Users/asd/test_sim/analysis/abc_histogram.txt", "txt", columns=["Frame","Histogram"], multiple_frames = True)
I got following output

# "Frame" "Histogram"
0 "(array([2, 0, 0, 0, 0, 0, 0, 0, 0, 1]), array([0.203213 , 0.3399507, 0.4766884, 0.6134261, 0.7501638, 0.8869015,
       1.0236392, 1.1603769, 1.2971146, 1.4338523, 1.57059  ]))"
1 "(array([5, 3, 0, 1, 0, 0, 0, 0, 0, 1]), array([-0.949916 , -0.2938844,  0.3621472,  1.0181788,  1.6742104,
        2.330242 ,  2.9862736,  3.6423052,  4.2983368,  4.9543684,
        5.6104   ]))"
2 "(array([5, 1, 0, 0, 1, 1, 0, 0, 0, 1]), array([-0.964007 , -0.7175453, -0.4710836, -0.2246219,  0.0218398,
        0.2683015,  0.5147632,  0.7612249,  1.0076866,  1.2541483,
        1.50061  ]))"

I am expecting
Frame Histogram
0 [Distribution of histogram along row]

so it will be easy for me to post process ( basically I am looking for the way will make easy post processing).
I tried
Code: [Select]
export_file(node, "/Users/asd/test_sim/analysis/abc_histogram.txt", "txt", columns=["Frame"], row=["Histogram"], multiple_frames = True)
But it not gonna work.

Thanks,
ad

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 119
Re: Position Y histogram of of each frame
« Reply #1 on: November 29, 2018, 12:16:08 PM »
Hi ad,

global attributes are actually not meant to save whole arrays. In your case it would be easier to just loop over all frames and use numpy.savetxt() to save the current histogram to a txt file, for example:

Code: [Select]
node = import_file(...)

for frame_index in range(node.source.num_frames):
    data = node.compute(frame_index)
    hist, bin_edges = numpy.histogram(data.particles["Position"][:,1])
    numpy.savetxt("histogram%d.txt" %frame, np.column_stack((bin_edges[:-1],hist)))

-Constanze
« Last Edit: November 29, 2018, 01:08:06 PM by Constanze Kalcher »

ad

  • Newbie
  • *
  • Posts: 20
Re: Position Y histogram of of each frame
« Reply #2 on: December 03, 2018, 10:00:25 PM »
Thanks Constanze,

I have few more questions about same. How do I normalize the histogram over the range?

I tried follows:
Code: [Select]
hist, bin_edges = numpy.histogram(data.particles["Position"][:,1], density= True, normed =True)

And it normalized to 1 (for each frame), how do I plot cumulative histogram over the trajectory (each frame)?

So I'll get such plot : https://docs.scipy.org/doc/scipy/reference/_images/scipy-stats-cumfreq-1.png

Thanks,
-ad
« Last Edit: December 04, 2018, 01:56:06 AM by ad »

Constanze Kalcher

  • Administrator
  • Full Member
  • *****
  • Posts: 119
Re: Position Y histogram of of each frame
« Reply #3 on: December 04, 2018, 10:42:10 AM »
Hi ad,
I seem to miss what the OVITO specific question is here.
As stated in the numpy documentation, using "density=True" will cause the histogram to be normalized such that the integral over the range is 1. And from the link you posted it seems that you already found the corresponding page in the scipy documentation to plot the cumulative sum of your histogram?
-Constanze