betafractal
betafractal

Reputation: 8054

gnuplot averaging stochastic time data blocks at set intervals of time

Hi I'm using gnuplot to plot data from a simulation structured in data blocks, like this:

CurrentTime  CurrentState
0            2
1.234        2
1.990        1
2.462        0


CurrentTime  CurrentState
0            2
0.895        1
1.456        2
2.052        1
3.017        0

The number of data blocks is not strictly known but is at least 30 blocks. Notice that the number of intervals are different for each CurrentTime. I'm using the following code to plot the data as is

# GNUPlot code
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable

The next thing I want to plot will go in the lower plot due to the multiplot command. That plot I want to be the average of my data at intervals of time that I set. In pseudo code I want:

# pseudo code
float start, step, stop;
assign start, step, stop;
define Interval=start, by step, to stop; typed another way Interval=start:step:stop 
array sum(size(number of data blocks,length(Interval), length(Interval)))
assign sum=0;
for every data block
    for k=0 to length(CurrentTime)
        for j=0 to length(Interval)-1
           (CurrentTime(k) < Interval(j+1) && CurrentTime(k) > Interval(j-1)) ? sum += CurrentState(k) : sum += 0
average=sum/(Number of data blocks)

I am stuck trying to implement that in gnuplot. Any assistance would be awesome!

Upvotes: 1

Views: 585

Answers (1)

betafractal
betafractal

Reputation: 8054

First there is the data file, some of my real data is

CurrentTime CurrentState
0       2
4.36393     1
5.76339     2
13.752      1
13.7645     2
18.2609     1
19.9713     2
33.7285     1
33.789      0


CurrentTime CurrentState
0       2
3.27887     1
3.74072     2
3.86885     1
4.97116     0


CurrentTime CurrentState
0       2
1.19854     1
3.23982     2
7.30501     1
7.83872     0

Then I used python to find the average of the data at the time I intervals I want to check the average. I chose to check at discrete time steps but they could be any time step. The following is my python code

#Loading data file: Goal is to calculate average(TimeIntervals)=averageOfTimeIntervals.
import numpy as np
data=np.genfromtxt('data.txt', comments='C')
CurrentState=data[:,1]
CurrentTime=data[:,0]
numberTimeIntervals=101
TimeIntervals=np.linspace(0,numberTimeIntervals-1,numberTimeIntervals)     #gives integer values of time
stateOfTimeIntervals=np.zeros(numberTimeIntervals,dtype=np.float64)
stateOfTimeIntervals[0]=CurrentState[0]  #setting initial state
#main loop
run=0
numberSimTimes=len(CurrentTime)
for j in range(0,len(stateOfTimeIntervals)): #start at 1 b/c we know   initial state
for k in range(0,numberSimTimes-1):
    lengthThisRun=0
    if CurrentTime[k] <= TimeIntervals[j] and CurrentTime[k+1] > TimeIntervals[j]:
        lengthThisRun+=1
        #Goal is to get the length of this run up to the time we decide to check the state
        stateOfTimeIntervals[j]+=CurrentState[k]
else:
        lengthThisRun+=1
#The number of runs can be claculated using
numberRuns=len(CurrentTime) - np.count_nonzero(CurrentTime)
print "Number of Runs=%f" %(numberRuns)
#Compute the average
averageState=stateOfTimeIntervals/numberRuns
#Write to file and plot with gnuplot
np.savetxt('plot2gnu.txt',averageState)

Then using gnuplot I plotted 'plot2gnu.txt' using the following code

# to plot everything on the same plot use "multiplot"
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable
plot 'plot2gnu.txt' using 1:2 with linespoints

I would like to point out the use of a pseudocolumn 'column(-2)' in the third column specifying line color. 'column(-2)' represents "The index number of the current data set within a file that contains multiple data sets." - From the 'old' gnuplot 4.6 documentation.

Upvotes: 1

Related Questions