def logHist(data, i): bins, dat = log_bin(data, a=1.5) plt.plot(bins, dat, label='Cutoff = %d' % (3500 + i * 100)) plt.xscale('log') plt.yscale('log') plt.ylim(1e-7, 1e-0) plt.xlim(1e0, 1e10)
def logBin(self): print 'Binning data...' bins, dat = log_bin(self.data[self.tc:], a=1.25) #for j in range(len(bins)): #dat[j] = pow(bins[j],1.55)*dat[j] plt.plot(np.array(bins), dat, label='All avalanches') print 'Plotting...'
def logHist(data, i): bins, dat = log_bin(data, a=1.75) plt.plot(bins, dat * len(data), label="Cutoff = %d" % (3500 + 100 * i)) plt.xscale('log') plt.yscale('log') plt.ylim(1e-4, 1e2) plt.xlabel(r'Flux (counts)') plt.ylabel(r'Probability Density')
def log_bin(self, a): #x = self.k[5:] x = self.k vals, counts = lb.lin_bin(x, int(max(x))) b, c = lb.log_bin(x, 1., 1.5, a, debug_mode=False) slope = stats.linregress(np.log(b[4:]), np.log(c[4:])) #print np.log(b), np.log(c) return slope, b[1:], c[1:]
def logBinDegrees(self): deg = self.G.degree().values() bins, dat = log_bin(deg, a=1.15) '''plt.scatter(bins,dat) plt.xscale('log') plt.yscale('log') plt.ylim(1e-10,1)''' return bins, dat
def LogBinProbabilities(self, a=1.4): self.avalanche_bin, self.prob_bin = lb.log_bin( self.Data[self.cot_x:self.cot_x + self.T], bin_start=0., first_bin_width=1., a=a, datatype='float', drop_zeros=False, debug_mode=False) self.avalanche_bin = np.array(self.avalanche_bin[1:]) self.prob_bin = np.array(self.prob_bin[1:])
def logBin(self): print 'Binning data...' bins, dat = log_bin(self.data[self.tc:], a=1.25) #for j in range(len(bins)): #dat[j] = pow(bins[j],1.55)*dat[j] plt.plot(np.array(bins), dat, label='No fall from system') print 'Plotting...' plt.legend(loc=0) plt.ylabel(r'$P_{10^{7}} \left( s; 1024\right)$', fontsize=18) plt.xlabel(r'$s$', fontsize=18) plt.xscale('log') plt.yscale('log') plt.show()
def log_bin_data(self, system, a = 1.4): os.chdir('path'/'+str(system.system_size)) f = open('Avalanche Size.txt', 'r') x = [] for line in f: x.append(float(line)) os.chdir('path') x = np.array([int(z) for z in x]) vals, counts = lb.frequency(x) b, c = lb.log_bin(x, 1., 1.5, a, 'integer', debug_mode=False, drop_zeros = False) """vals, counts = lb.lin_bin(x, int(max(x))) b, c = lb.log_bin(x, 1., 1., a, debug_mode=False)""" #return the binned data for each system size return np.array(b), np.array(c)
def LogBin(self, a=1.5): self.deg_bin_original, self.degprob_bin_original = lb.log_bin( self.degrees_float, bin_start=float(self.deg[0]), first_bin_width=1., a=a, datatype='integer', drop_zeros=False, debug_mode=False) degprob_arg_notzero = np.argwhere(self.degprob_bin_original > 0) self.degprob_bin = list( np.array(self.degprob_bin_original)[degprob_arg_notzero].flatten()) self.deg_bin = list( np.array(self.deg_bin_original)[degprob_arg_notzero].flatten())
def logBin(self): print 'Binning data...' offset = 0 for i in range(len(self.data)): bins, dat = log_bin(self.data[i], a=1.25) for j in range(len(bins)): dat[j] = pow(bins[j], 1.55) * dat[j] pass plt.plot(np.array(bins) / pow(pow(2, i + 3), 2.25), dat, label='L = %d' % (pow(2, i + 3))) print 'Plotting...' plt.legend(loc=0) plt.ylabel(r'$P_{10^{7}} \left( s; L \right)$', fontsize=18) plt.xlabel(r'$s$', fontsize=18) plt.xscale('log') plt.yscale('log') plt.show()
def scale_aval(self, collapse=True, drops=False): ''' Plots probability of avalanches or drops before or after collapse for all sizes. ''' fig = plt.figure() ax = fig.add_subplot(111) c = cm.rainbow(np.linspace(0, 1, len(self.sizes))) for i in xrange(len(self.sizes)): print self.sizes[i] if drops: y = self.y[i, :] y = y[np.nonzero(y)] k = [1.002, 1.249] else: y = self.y[i, -self.counts:] k = [1.55, 2.25] xx, yy = lb.log_bin(y, 0, 1., 1.2, 'integer', False) xx, yy = np.array(xx), np.array(yy) if collapse: y = (xx**k[0]) * yy x = xx / (self.sizes[i]**k[1]) ax.loglog(x, y, '-', lw=1.5, color=c[i], label=self.sizes[i]) ax.legend(loc='lower left', ncol=2, prop={'size': 13}) if drops: ax.set_xlabel('$d/L^{1.25}$') ax.set_ylabel('$d^{1.01} P(d;L)$') else: ax.set_xlabel('$s/L^{2.24}$') ax.set_ylabel('$ s^{1.55} P(s;L)$') else: #x, y = np.log2(xx), np.log2(yy) ax.loglog(xx, yy, '-', lw=1.5, color=c[i], label=self.sizes[i]) ax.legend(loc='upper right', ncol=2, prop={'size': 13}) if drops: ax.set_xlabel('$drop\ size\ d$') ax.set_ylabel('$probability\ P(d;L)$') else: ax.set_xlabel('$avalanche\ size\ s$') ax.set_ylabel('$probability\ P(s;L)$')
def prob_aval(self, counts, base=1.2, size=all_sizes[5], drops=False): ''' Plots the log-binning of avalanche probability. ''' prob = self.calc_prob(size=size, counts=counts, drops=drops) fig = plt.figure() x = np.nonzero(prob)[0] y = prob[x] j = np.where(self.sizes == size)[0][0] if base is None: base = np.linspace(1.15, 1.25, 4) ax = fig.add_subplot(111) if drops: s = self.y[j, :] s = s[np.nonzero(s)] ax.set_xlabel('$drop\ size\ d$') ax.set_ylabel('$probability\ P(d;L)$') else: s = self.y[j, -counts:] ax.set_xlabel('$avalanche\ size\ s$') ax.set_ylabel('$probability\ P(s;L)$') xx, yy = lb.log_bin(s, 0., 1., base, 'integer', False) ax.loglog(x, y, 'x') ax.loglog(xx, yy, 'o', lw=2., label='a=' + str(base)) ax.legend(loc='upper right')
def log_binning(s, a): ''' Log bin the avalance sizes.''' b, c = lbcn.log_bin(s, 1.0, 1.5, a, 'integer', drop_zeros=True) return b, c
def varying_N(self, N_range, m, model, plot, collapse, deviation, testing): """ Function to perform tasks associated with the 'Varying N' section of the Project. Inputs: N_range = list of N values to be tested (N = number of nodes in network) m = number of nodes added per time step. model = 'BA', 'RA', 'MA': for either Barabasi-Albert, Random Attachment or Mixed Attachment. plot = Boolean: True - to plot obtained data (log-binned) collapse = Boolean: True - to perform data collapse and observe resulting graph deviation = Boolean: True - analyse the deviation from theory of the data. testing = Boolean: set to True to run SMALLER number of repetitions to quickly check operations Outputs: Outputs of the various methods are all graphs included in this section of the report for visual and statistical analysis. Data Collapse performed according to procedure laid out in Project Report. """ results = [] for n in N_range: start = time.time() repetitions = int(100000000 / float(n)) #repetitions to be averaged over if testing: repetitions = 100 print repetitions values = [] count = 0 for j in range(repetitions): #set-up the networks if model == "BA": model_test = BAmodel(int(n), 2 * m, m, "new") elif model == "RA": model_test = RAmodel(int(n), 2 * m, m) elif model == "MA": model_test = MAmodel(int(n), 2 * m, m, 0.5) kdist, degrees_raw = model_test.degree_distribution() values.extend(degrees_raw) self.empty() if count in [ 1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000, 10000, 50000, 75000, 100000 ]: print count count += 1 print "Time taken: ", time.time() - start kdist = Counter(values) results.append((int(n), kdist, values)) to_plot, to_collapse, to_deviate = [[], [], [] ] #value to be filled for plotting for values in results: degrees = values[2] degrees = [i for i in values[2] if i >= m] if min(degrees) != 0: centres, counts = log_bin( degrees, min(degrees), 1.0, 1.1) #log-bin the degree distribution else: centres, counts = log_bin( degrees, m, 1.0, 1.1) #log-bin the degree distribution counts = counts.tolist() if plot: to_plot.append((centres, counts, values[0], degrees)) if collapse: if model == "MA": print "Can't do this, no analytic expression found for k1" else: p_inf = [ self.p_inf_predicted(l, m, model) for l in centres ] #calculated expected probabilities k1 = [ self.k1_predicted(m, values[0], model) for l in centres ] collapsed = [ float(l) / float(p_inf[counts.index(l)]) for l in counts ] #collapsed kcoll = [ float(l) / float(k1[centres.index(l)]) for l in centres ] to_collapse.append((kcoll, collapsed, values[0])) if deviation: if model == "MA": print "Can't do this - don't have an analytic expression for k1" else: if collapse != True: p_inf = [ self.p_inf_predicted(l, m, model) for l in centres ] collapsed = [ float(l) / float(p_inf[counts.index(l)]) for l in counts ] to_deviate.append((centres, collapsed, values[0])) if plot: plt.figure() for j in to_plot: plt.scatter(j[0], j[1], marker='o', edgecolors='black', label='N=%s' % j[2]) plt.plot(np.linspace(1, max(j[3]), 100), [ self.p_inf_predicted(i, m, model) for i in np.linspace(1, max(j[3]), 100) ], '--', label=r'$p_{{\infty}}(k), m={0}$'.format(m)) plt.xlabel("k") plt.ylabel(r"$\tilde{p}(k)$") plt.legend(fontsize=30) plt.xscale("log") plt.yscale("log") plt.show() if collapse and model != "MA": if len(to_collapse) != 0: plt.figure() for j in to_collapse: plt.scatter(j[0], j[1], marker='o', edgecolors='black', label="N=%s" % j[2]) plt.xlabel(r"$\frac{k}{k_{1}}$") plt.ylabel(r"$\frac{p_{observed}}{p_{\infty}}$") plt.xscale("log") plt.yscale("log") plt.legend(fontsize=30) plt.show() if deviation and model != "MA": plt.figure() for j in to_deviate: plt.scatter(j[0], j[1], marker='o', edgecolors='black', label="N=%s" % j[2]) plt.plot(np.linspace(1, max(j[0]), 100), [1] * 100, '--', label=r"$\frac{p_{observed}}{p_{\infty}} = 1$") plt.xlabel("k") plt.ylabel(r"$\frac{p_{observed}}{p_{\infty}}$") plt.xscale("log") plt.yscale("log") plt.legend(fontsize=30) plt.show()
def varying_m(self, plot, checking, m_range, tests, model, testing): """ Function to perform first set of tasks in the Project Notes - examine behaviour for multiple 'm' values. Inputs: plot = Boolean: True = see plots of raw degree distribution checking = Boolean: True = see plots of log-binned degree distribution m_range = list of m values to be tested. tests = Boolean: True - perform statistical tests on the data and theoretical prediction, output is graph. model = 'BA', 'RA', 'MA': for either Barabasi-Albert, Random Attachment or Mixed Attachment. testing = Boolean: set to True to run SMALLER number of repetitions to quickly check operations Ouputs: For all three boolean inputs, the outputs are graphs used in the final project report. Statistical Testing: A Kolmogorov-Smirnov test was performed to compare the predicted degree distributions with the obtained data. To examine the performance of the models, the KS test was performed over an incrementally increasing range of data points I.e. starting with a very small window (k=m,k=m+1), and expanding this up to the entire data set. """ results = [] for m in m_range: start = time.time() if m == 1: n = int(0) else: n = int(float(m)**(1.0 / 3.0)) repetitions = 100 * (3**( 4 - n)) #Repetitions are maximum 81000, down to 100 at m=81 if testing: repetitions = 100 print repetitions values = [] count = 0 for j in range( repetitions): #build graphs depending on model type. if model == "BA": model_test = BAmodel(self.N, 2 * m, m, "new") elif model == "RA": model_test = RAmodel(self.N, 2 * m, m) elif model == "MA": model_test = MAmodel(self.N, 2 * m, m, 0.5) kdist, degrees_raw = model_test.degree_distribution( ) #get degree distribution values.extend(degrees_raw) self.empty() if count in [ 1, 2, 3, 4, 5, 10, 20, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000, 6000, 10000 ]: print count count += 1 kdist = Counter(values) #collect data over all repetitions results.append((m, kdist, values, int( repetitions * self.N))) #collect full data for this value of m print "Finished for m = ", m print "time taken: ", time.time() - start for_plot, for_check, for_tests = [ [], [], [] ] #empty lists to be filled with data to be plotted tests_2 = [] for values in results: if plot: N = len(values[2]) degrees_filtered = [ i for i in values[1].keys() if i >= values[0] ] #remove k<m p_k = [ float(values[1][i]) / float(N) for i in degrees_filtered ] #find theoretical values for these k values for_plot.append((degrees_filtered, p_k, values[0])) if checking: degrees = [i for i in values[2] if i >= values[0]] #filter k<m centres, counts = log_bin( degrees, min(degrees), 1.0, 1.1) #log bin the degree distribution data. for_check.append((centres, counts, values[0])) if tests: degrees = [i for i in values[2] if i >= values[0]] #filter k<m results_two = [] c = Counter(degrees) degrees_obs = c.keys() counts = c.values() for i in np.arange( values[0], max(degrees_obs) + 1): #define variable window to perform tests over. true = counts[degrees_obs.index(values[0]):i] degs = degrees_obs[degrees_obs.index(values[0]):i] if len(true) != 0: pred = [ self.p_inf_predicted(l, values[0], model) for l in degs ] pred = [ l * (float(sum(true)) / (float(sum(pred)))) for l in pred ] #get predicted counts with SAME sum as obtained results if len(true) == len( pred): #to account for slight over production results_two.append( (i, ks_2samp(true, pred)[1])) #perform KS tests for_tests.append((values[0], results_two)) if tests: #Do same KS tests but on CUMULATIVE DISTRIBUTION to deal with fat tail CDF Data degrees = [i for i in values[2] if i >= values[0]] #filter k<m ress_2 = [] centres = Counter(degrees).keys() counts = np.cumsum(Counter(degrees).values()) for i in centres: if i != min(centres): true = counts[centres.index(min(centres)):centres. index(i)] true = [int(values[3] * a) for a in true ] #make them a count not a probability. degs = centres[centres.index(min(centres)):centres. index(i)] if len(true) != 0: pred = [ self.p_inf_predicted(l, values[0], model) for l in degs ] pred = np.cumsum(pred).tolist() pred = [ l * (float(sum(true)) / (float(sum(pred)))) for l in pred ] if len(true) == len(pred): ress_2.append((i, ks_2samp(true, pred)[1])) tests_2.append((values[0], ress_2)) if plot: plt.figure() for j in for_plot: plt.scatter(j[0], j[1], marker='o', edgecolors='black') plt.plot(np.linspace(1, max(j[0]), 100), [ self.p_inf_predicted(i, j[2], model) for i in np.linspace(1, max(j[0]), 100) ], '--', label=r'$p_{{\infty}}(k), m={0}$'.format(j[2])) plt.xlabel("k") plt.ylabel("p(k)") plt.legend(fontsize=30) plt.xscale("log") plt.yscale("log") plt.show() if checking: plt.figure() for j in for_check: plt.scatter(j[0], j[1], marker='o', edgecolors='black') plt.plot(np.linspace(j[2], max(j[0]), 100), [ self.p_inf_predicted(i, j[2], model) for i in np.linspace(j[2], max(j[0]), 100) ], '--', label=r'$p_{{\infty}}(k), m={0}$'.format(j[2])) plt.xlabel("k") plt.ylabel(r"$\tilde{p}(k)$") plt.legend(fontsize=30) plt.xscale("log") plt.yscale("log") plt.show() plt.figure() for j in for_plot: cdf = np.cumsum(j[1]) plt.scatter(j[0], cdf, marker='o', edgecolors='black') plt.plot(j[0], np.cumsum([ self.p_inf_predicted(i, j[2], model) for i in j[0] ]), '--', label=r'$p_{{\infty}}(k), m={0}$'.format(j[2])) plt.xlabel("k") plt.ylabel(r"$\tilde{p}(k)$") plt.legend(fontsize=30) plt.xscale("log") plt.yscale("log") plt.show() if tests: plt.figure() for res in for_tests: plt.plot([j[0] for j in res[1]], [j[1] for j in res[1]], label="m=%s" % res[0]) plt.xlabel(r"$k_{max}$") plt.ylabel("K-S Test, p-val") plt.legend(fontsize=30) plt.show() plt.figure() for res in tests_2: plt.plot([j[0] for j in res[1]], [j[1] for j in res[1]], label="m=%s" % res[0]) plt.xlabel(r"$k_{max}$") plt.ylabel("K-S Test, p-val") plt.legend(fontsize=30) plt.show()
def degreeProb(self, raw=False, binned=False, pRaw=False, pBinned=False, plotTheory=False, plotRatio=False, checkSum=False, plot=False, plotType1='o', plotType2='o-', plotType3='-', log=False): """ Calculates the probabilities of getting degree values k, p(k) for all graphs. Also log-bins the data.""" K = [] KProbs = [] KCounts = [] expectedCounts = [] graphsCentres = [] graphsProbs = [] theoryData = [] if raw: # Raw probs calculated only is specified for i in range(len(self.sortedDegrees)): k = [] probs = [] averageCounts = [] degrees = np.sort( self.sortedDegrees[i] ) # Getting the list of degrees for the current graph size for x in degrees: # Going in ascending order of the degrees list if x not in k: k.append(x) count = 0 for y in degrees: if y == x: count += 1 # Counting how many times the current degree value occurs in the list elif count >= 1: break # Since the list is sorted in ascending order, if the count is >= 1 and isn't increasing further, there are no more degrees with this value else: pass # If the degree value hasn't been found yet, keep scanning if count == 0: print "COUNT IS ZERO" prob = count / float( len(degrees) ) # Dividing the counted occurrences of all k values by the total number of degrees in the graph averageCounts.append( count / self.loops ) # Calculating the average counts number per degree by diving but the number of loops probs.append(prob) else: pass k = np.asarray( k, dtype='int64' ) # Converting the lists to numpy arrays for easier manipulation averagecounts = np.asarray(averageCounts, dtype='float64') probs = np.asarray(probs, dtype='float64') K.append(k) KCounts.append(averageCounts) KProbs.append(probs) N = self.graphNs[i] if self.random: theoryProbs = np.array([ funcs.func2(float(k), float(self.graphMs[i])) for k in K[i] ], dtype='float64') else: theoryProbs = np.array([ funcs.func1(float(k), float(self.graphMs[i])) for k in K[i] ], dtype='float64') theoryCounts = N * theoryProbs # Calculating the theoretically expected degree counts expectedCounts.append(theoryCounts) # Converting the lists to numpy arrays for easier manipulation K = np.asarray(K) KCounts = np.asarray(KCounts) expectedCounts = np.asarray(expectedCounts) KProbs = np.asarray(KProbs) if checkSum: # Checking to see if all the probabilities add up to 1 ie they are normalised for all system sizes for probs in KProbs: summation = np.sum(probs) print "Summation: ", summation if binned: # Binned probs calculated only is specified for i in range(len(self.sortedDegrees)): degrees = self.sortedDegrees[ i] # Getting the list of avalanche sizes for the current system size centres, binProbs = lb.log_bin( degrees, bin_start=float(self.graphMs[i]), a=self.a, datatype='integer' ) # Calculating the log-binned probs using the log_bin_CN_2016.py module graphsCentres.append(centres) graphsProbs.append(binProbs) for x in range(len(graphsCentres) ): # Calculating the theoretical probability values if self.random: theoryValues = np.array([ funcs.func2(float(k), float(self.graphMs[x])) for k in graphsCentres[x] ], dtype='float64') else: theoryValues = np.array([ funcs.func1(float(k), float(self.graphMs[x])) for k in graphsCentres[x] ], dtype='float64') theoryData.append(theoryValues) if plot: # Plotting the probs p(k) against degree values k on log-log plots (if specified) xLab = r"$k$" title = "Degree Size Probability vs Degree Size" if pRaw and not pBinned: # Plotting just the raw data yLab = r"$p(k)$" legend = ['Raw for m = ' + str(i) for i in self.graphMs] if self.graphNs[0] != self.graphNs[-1]: legend = ['Raw for N = ' + str(i) for i in self.graphNs] plotTypes = [plotType1 for i in range(len(KProbs))] pt.plot(xData=K, yData=KProbs, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) elif pBinned and not pRaw: # Plotting just the binned data yLab = r"$\tildep(k)$" legend = ['Binned for m = ' + str(i) for i in self.graphMs] if self.graphNs[0] != self.graphNs[-1]: legend = ['Binned for N = ' + str(i) for i in self.graphNs] plotTypes = [plotType1 for i in range(len(graphsProbs))] if plotTheory: legend1 = [ 'Binned for m = ' + str(i) for i in self.graphMs ] legend2 = [ 'Theoretical for m = ' + str(i) for i in self.graphMs ] if self.graphNs[0] != self.graphNs[-1]: legend1 = [ 'Binned for N = ' + str(i) for i in self.graphNs ] legend2 = [ 'Theoretical for N = ' + str(i) for i in self.graphNs ] plotTypes1 = [plotType1 for i in range(len(graphsProbs))] plotTypes2 = [plotType3 for i in range(len(graphsProbs))] plt.figure(figsize=(12, 10)) pt.plot(xData=graphsCentres, yData=graphsProbs, plotType=plotTypes1, xLab=xLab, yLab=yLab, title=title, legend=legend1, multiX=True, multiY=True, loc=1, log=log, figure=False) pt.plot(xData=graphsCentres, yData=theoryData, plotType=plotTypes2, xLab=xLab, yLab=yLab, title=title, legend=legend2, multiX=True, multiY=True, loc=1, log=log, figure=False) plt.grid() elif plotRatio: # Plotting the ratio of raw to theory probs yLab = r"$p_{d}(k)/p_{t}(k)$" legend = ['Binned for m = ' + str(i) for i in self.graphMs] if self.graphNs[0] != self.graphNs[-1]: legend = [ 'Binned for N = ' + str(i) for i in self.graphNs ] plotTypes = [plotType2 for i in range(len(graphsProbs))] ratios = [] for i in range(len(graphsProbs)): ratio = graphsProbs[i] / theoryData[i] ratios.append(ratio) plt.figure(figsize=(12, 10)) plt.axhline(y=1, linewidth=2, color='k') pt.plot(xData=graphsCentres, yData=ratios, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log, figure=False) else: legend = ['Binned for m = ' + str(i) for i in self.graphMs] if self.graphNs[0] != self.graphNs[-1]: legend = [ 'Binned for N = ' + str(i) for i in self.graphNs ] plotTypes = [plotType2 for i in range(len(graphsProbs))] pt.plot(xData=graphsCentres, yData=graphsProbs, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) elif pRaw and pBinned: # Plotting both the raw data and binned data yLab = r"$p(k)$" legend = [ 'Raw for m = ' + str(self.graphMs[-1]), 'Binned for m = ' + str(self.graphMs[-1]) ] plotTypes = [plotType1, plotType2] pt.plot(xData=[K[-1], graphsCentres[-1]], yData=[KProbs[-1], graphsProbs[-1]], plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) return K, KProbs, KCounts, expectedCounts, graphsCentres, graphsProbs, theoryData # Returning both the raw and binned data
def avProb(self, raw=False, binned=False, checkSum=False, plot=False, plotType1='o', plotType2='o-', log=False): """ Calculates the probabilities of getting avalanche sizes s, P(s; L) for all systems. Also log-bins the data.""" S = [] SProbs = [] systemCentres = [] systemProbs = [] t0 = int(self.tMax - self.N) - 1 T = self.tMax - t0 # Setting the number of data points to use for calculating the probabilities lowerLimit = t0 + 1 # The lower index for the heights list to scan upperLimit = t0 + T # The upper index for the heights list to scan if raw: # Raw probs calculated only is specified for i in range(len(self.systems)): s = [] probs = [] avalanches = self.systemAvSizes[ i] # Getting the list of avalanche sizes for the current system size steadyAvs = np.sort( avalanches[lowerLimit:upperLimit + 1] ) # Only need to scan the avalanches in the steady state for x in range(min(steadyAvs), max(steadyAvs) + 1): # Going in ascending order of the avs list s.append(x) count = 0 for y in steadyAvs: if y == x: count += 1 # Counting how many times the current avalanche size occurs in the list elif count >= 1: break # Since the list is sorted in ascending order, if the count is >= 1 and isn't increasing further, there are no more avalanches of this size else: pass # If the avalanche size hasn't been found yet, keep scanning prob = count / float( len(steadyAvs) ) # Dividing the counted occurrences of all s values by the total number of avalanches in the steady state probs.append(prob) s = np.asarray( s, dtype='int64' ) # Converting the lists to numpy arrays for easier manipulation probs = np.asarray(probs, dtype='float64') S.append(s) SProbs.append(probs) # Converting the lists to numpy arrays for easier manipulation S = np.asarray(S) SProbs = np.asarray(SProbs) if checkSum: # Checking to see if all the probabilities add up to 1 ie they are normalised for all system sizes for probs in SProbs: summation = np.sum(probs) print summation if binned: # Binned probs calculated only is specified for i in range(len(self.systems)): avalanches = self.systemAvSizes[ i] # Getting the list of avalanche sizes for the current system size steadyAvs = avalanches[ lowerLimit:upperLimit + 1] # Only need to scan the avalanches in the steady state centres, binProbs = lb.log_bin( steadyAvs, a=self.a ) # Calculating the log-binned probs using the log_bin_CN_2016.py module systemCentres.append(centres) systemProbs.append(binProbs) if plot: # Plotting the probs P(h; L) against avalanche size s on log-log plots (if specified) xLab = r"$s$" title = "Avalanche Size Probability vs Avalanche Size" if raw and not binned: # Plotting just the raw data yLab = r"$P_{N}(s; L)$" legend = ['Raw for L = ' + str(i) for i in self.systemSizes] plotTypes = [plotType1 for i in range(len(SProbs))] pt.plot(xData=S, yData=SProbs, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) elif binned and not raw: # Plotting just the binned data yLab = r"$\tildeP_{N}(s; L)$" legend = ['Binned for L = ' + str(i) for i in self.systemSizes] plotTypes = [plotType2 for i in range(len(systemProbs))] pt.plot(xData=systemCentres, yData=systemProbs, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) elif raw and binned: # Plotting both the raw data and binned data yLab = r"$P_{N}(s; L)$" legend = [ 'Raw for L = ' + str(self.systemSizes[-1]), 'Binned for L = ' + str(self.systemSizes[-1]) ] plotTypes = [plotType1, plotType2] pt.plot(xData=[S[-1], systemCentres[-1]], yData=[SProbs[-1], systemProbs[-1]], plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) return S, SProbs, systemCentres, systemProbs # Returning both the raw and binned data
for i in values: freqs.append(datadict[int(i)]) freqs = np.array(freqs) normalised = freqs / np.float(np.sum(freqs)) return values, normalised b = [] c = [] v = [] freq = [] for i in avalanche_try: vals1, freq1 = calc_freq(i) b1, c1 = lb.log_bin(i, 1., 1., 1.4, debug_mode=False, drop_zeros=False) vals2, counts2 = lb.lin_bin(i, max(i)) b.append(b1) c.append(c1) v.append(vals1) freq.append(freq1) plt.figure(1) plt.loglog(v[2], freq[2], ".", alpha=1, c=tableau20[9], label=r"$P_N, N = 10^6$") plt.loglog(b[2], c[2],