#Canvas sizes - Change this if necessary csPict = (350,350) csPlot = (500,300) glob=[] # Needed to protect ROOTs stuff from the python garbage collector def newCanvas(size): # Creates a new canvas and returns it. You probably don't need to call this directly. # size ( (float,float) ) - Tuple of width and height of the new canvas. can=ROOT.TCanvas("internal"+str(len(glob)),str(len(glob)),size[0],size[1]) can.cd() glob.append(can) return can def ImportJSON(filename, limit=-1): # Reads images from the specified json formated file. Returns two objects # the first is a list of images ( [][][]int ), the time at which each # image was taken in minutes ( []float ). # Gzip compressed files are automatically detected and decompressed. # filename ( string ) - Filename (e.g., "data.json" or "data.json.gz") # limit ( int ) - Number of images read from file. -1 reads all images. import gzip import json result=[] if filename.split(".")[-1]=="gz": f=gzip.open(filename) else: f=open(filename) for line in f: if limit==0: break else: limit=limit-1 if len(line)>1: arr = [array.array("b",ls) for ls in json.loads(line)] result.append(arr) f.close() return result, [0.05*i for i in range(len(result))] def DrawGraph(xdata,ydata,title="",xlabel="x",ylabel="y"): # Draws a graph in a new canvas, returns the canvas # xdata ( []float ) - Position of each point on the x-Axis # ydata ( []float ) - Position of each point on the y-Axis # title ( string ) - Text on top of the graph # xlabel ( string ) - Text on the x-Axis # ylabel ( string ) - Text on the y-Axis can=newCanvas(csPlot) g=ROOT.TGraph(len(xdata),array.array('f',xdata),array.array('f',ydata)) glob.append(g) g.SetTitle(title+";"+xlabel+";"+ylabel) g.Draw("AL") ROOT.gPad.Update() return can def AddGraph(xdata,ydata,canvas=None): # Adds a new curve to the canvas that was used last (or the specified one). # xdata ( []float ) - Position of each point on the x-Axis # ydata ( []float ) - Position of each point on the y-Axis # canvas ( internal ) - Canvas that is used for drawing (returned by DrawGraph) if canvas!=None : canvas.cd() g=ROOT.TGraph(len(xdata),array.array('f',xdata),array.array('f',ydata)) glob.append(g) g.Draw("L same") ROOT.gPad.Update() def DrawImage(image, same = False): # Draws the image in a new canvas. # image ( [][]float ) - Image to be drawn, represented as list of list of integers # same ( bool ) - If set to True no new canvas is created (for animations) if not same: newCanvas(csPict) g=ROOT.TH2F("","",len(image),0.0,len(image)-1,len(image[0]),0.0,len(image[0])-1) glob.append(g) for x in range(len(image)): for y in range(len(image[0])): g.Fill(x,y,1.0*image[x][y]) g.Draw("colz") ROOT.gPad.Update() return def AnimateImages(images,wait=0.05): # Animates a list of images # images ( [][][]float ) - List of images to be animated from time import sleep reuse = False prev = None for img in images: if reuse: # Remove the old histogram from glob # and clear its draw calls. # (Allows garbage collection to save memory) ROOT.gPad.Clear() glob.pop() DrawImage(img,reuse) prev=reuse = True ROOT.gPad.Update() sleep(wait) return def SumOfPixelValues(image): # Returns the sum of all pixel values in an image. # image ( [][]float ) - Image whose total sum of pixels is computed summ = 0.0 #WRITE YOUR OWN CODE HERE for row in image: #sol for value in row: #sol summ+=value #sol return summ def MovingAverage(data, distance): # Returns a smoothened copy of the data using a windowed moving average. # The size of the window is specified by the distance between the central point # of the window and its boundaries (For example a distance of 2 means # that 5 data points contribute to the average: 2 before the central one, # the central one and 2 after the central one.). # At the beginning and end of the data the corresponding end of the window # is shortened to match the available data. # data ( []float ) - List of data points # distance ( int ) - Distance of the window boundaries from the central point. smoothened = [] #WRITE YOUR OWN CODE HERE for ci in range(len(data)): #sol #Compute boundaries #sol li=ci-distance #sol ui=ci+distance+1 #sol #Make sure boundaries don't exceed data #sol li=max(li,0) #sol ui=min(ui,len(data)-1) #sol #Slice window and compute average #sol window=data[li:ui] #sol average=sum(window)/len(window) #sol smoothened.append(average) #sol return smoothened def DFT(x, sign=-1): # Returns fourier coefficients of input time series. # x ( []float ) - input data from cmath import pi, exp N = len(x) W = [exp(sign * 2j * pi * i / N) for i in range(N)] X = [sum(W[n * k % N] * x[k] for k in range(N)) for n in range(N)] return X def Amplitudes(X): # Converts DFT coefficients to amplitudes. Returns amplitudes. # X ( []float ) - DFT coefficients (result of DFT function) return [math.sqrt(float(c.real)**2+float(c.imag)**2) for c in X] def Frequencies(n, timestep): # Generates a list of frequencies matching the coefficients # and amplitudes computed by the DFT and Amplitudes function. # n ( int ) - Length of original data (time series given to DFT function) # timestep ( float ) - Time between data points in original data timestep=float(timestep) n=int(n) return [i/(timestep*n) for i in range(0,(n+1)/2)+range(-(n-1)/2,0)] def InverseDFT(X): # Inverse DFT, converts fourier coefficients into time series # X ( []complex ) - DFT coefficients (result of DFT function) N, x = len(X), DFT(X, sign=1) for i in range(N): x[i] = float(x[i].real)/float(N) return x def Log(data): # Returns the logarithm of a list of floats. # data ( []float ) - Input data return [math.log(x) for x in data] def LowPassFilter(frequencies, coefficients, threshold): # Returns a copy of the data with every element set to zero # where the corresponding element in the list of frequencies is # below the specified threshold. # frequencies ( []float ) - frequency list used for filtering the data # coefficients ( []complex ) - input data # threshold ( float ) - threshold for filtering filtered = [] for i in range(len(frequencies)): if math.fabs(frequencies[i])