def ansQuest2(maxTime, numTrials, drunkType, title): distLists, locLists = performSim(maxTime, numTrials, drunkType) means = [] for t in range(maxTime + 1): tot = 0.0 for distL in distLists: tot += distL[t] means.append(tot/len(distLists)) pylab.figure() pylab.plot(means) pylab.ylabel('distance') pylab.xlabel('time') pylab.title(title + ' Avg. Distance') lastX = [] lastY = [] for locList in locLists: x, y = locList[-1].getCoords() lastX.append(x) lastY.append(y) pylab.figure() pylab.scatter(lastX, lastY) pylab.xlabel('EW distance') pylab.ylabel('NS distance') pylab.title(title + ' Final locations') pylab.figure() pylab.hist(lastX) pylab.xlabel('EW value') pylab.ylabel('Number of trials') pylab.title(title + ' Distribution of final EW Values')
def Plot2d(self, fignumStart): # Plot xTrue plt.figure(fignumStart) plt.imshow(self.Theta, interpolation='none') plt.colorbar() plt.title('xTrue') # Plot the reconstructed result plt.figure() plt.imshow(np.reshape(self.ThetaEstimated, self.Theta.shape), interpolation='none') plt.colorbar() plt.title('Reconstructed x') # Plot yErr and its histogram yErr = self.NoisyObs - np.reshape(self._reconstructor.hx, self.NoisyObs.shape) plt.figure() plt.imshow(yErr, interpolation='none') plt.colorbar() plt.title('yErr') plt.figure() plt.hist(yErr.flat, 20) plt.title('Histogram of yErr') plt.show()
def shear_plot(env, **kwargs): models = kwargs.pop('models', env.models) obj_index = kwargs.pop('obj_index', None) src_index = kwargs.pop('src_index', None) key = kwargs.pop('key', 'accepted') obj_slice = index_to_slice(obj_index) src_slice = index_to_slice(src_index) s0 = [ [] for o in env.objects ] s1 = [ [] for o in env.objects ] for mi,m in enumerate(models): # For H0 we only have to look at one model because the others are the same for oi, [obj,data] in enumerate(m['obj,data'][obj_slice]): if not data.has_key('shear'): continue #s0[oi].append(90-np.degrees(np.arctan2(*data['shear']))) s0[oi].append(data['shear'][0]) s1[oi].append(data['shear'][1]) #s0,s1 = data['shear'] #Log( 'Model %i Object %i Shear %.4f %.4f' % (mi, oi, s0,s1) ) if s0[0]: pl.hist(s0[0], histtype='step', **kwargs) if s1[0]: pl.hist(s1[0], histtype='step', **kwargs)
def simFlips(numFlips, numTrials): # performs and displays the simulation result diffs = [] # diffs to know if there was a fair Trial. It has the absolute differences of heads and tails in each trial for i in xrange(0, numTrials): heads, tails = flipTrial(numFlips) diffs.append(abs(heads - tails)) diffs = pylab.array(diffs) # create an array of diffs diffMean = sum(diffs)/len(diffs) # average of absolute differences of heads and tails from each trial diffPercent = (diffs/float(numFlips)) * 100 # create an array of percentage of each diffs from its no. of flips. percentMean = sum(diffPercent)/len(diffPercent) # create a percent mean of all diffPercents in the array pylab.hist(diffs) # displays the distribution of elements in diffs array pylab.axvline(diffMean, color = 'r', label = 'Mean') pylab.legend() titleString = str(numFlips) + ' Flips, ' + str(numTrials) + ' Trials' pylab.title(titleString) pylab.xlabel('Difference between heads and tails') pylab.ylabel('Number of Trials') pylab.figure() pylab.plot(diffPercent) pylab.axhline(percentMean, color = 'r', label = 'Mean') pylab.legend() pylab.title(titleString) pylab.xlabel('Trial Number') pylab.ylabel('Percent Difference between heads and tails')
def H0inv_plot(env, **kwargs): _hist(env, '1/H0', xlabel=r'$H_0^{-1}$ (Gyr)') return models = kwargs.pop('models', env.models) obj_index = kwargs.pop('obj_index', 0) key = kwargs.pop('key', 'accepted') xlabel = kwargs.pop('xlabel', r'$H_0^{-1}$ (Gyr)') ylabel = kwargs.pop('ylabel', r'Count') # select a list to append to based on the 'accepted' property. l = [[], [], []] for m in models: obj, data = m['obj,data'][0] # For H0inv we only have to look at one model because the others are the same l[m.get(key,2)].append(data['1/H0']) #l[2].append(data['kappa'][1]) #print amin(l[2]), amax(l[2]) not_accepted, accepted, notag = l #print 'H0inv_plot',H0s for d,s in zip(l, _styles): if d: #print len(d), d #pl.hist(d, bins=20, histtype='step', edgecolor=s['c'], zorder=s['z'], label=s['label']) pl.hist(d, bins=np.ptp(d)//1+1, histtype='step', edgecolor=s['c'], zorder=s['z'], label=s['label'], **kwargs) #if not_accepted or accepted: #pl.legend() pl.axvline(13.7, c='k', ls=':', zorder = 2) pl.xlabel(xlabel) pl.ylabel(ylabel) if accepted or not not_accepted: if accepted: h = np.array(accepted) else: h = np.array(accepted + notag) hs = np.sort(h) l = len(hs) m = hs[l * 0.50] u = hs[l * (0.50 + 0.341)] l = hs[l * (0.50 - 0.341)] #u = hs[l * 0.68] #l = hs[l * 0.32] pl.axvline(m, c='r', ls='-', zorder = 2) pl.axvline(u, c='g', ls='-', zorder = 2) pl.axvline(l, c='g', ls='-', zorder = 2) Log( 'H0inv_plot: ', m, u, l ) Log( 'H0inv_plot: ', m, (u-m), (m-l) ) else: Log( "H0inv_plot: No H0inv values accepted" )
def time_delays_plot(env, **kwargs): models = kwargs.pop('models', env.models) obj_index = kwargs.pop('obj_index', 0) src_index = kwargs.pop('src_index', 0) key = kwargs.pop('key', 'accepted') d = defaultdict(list) for m in models: obj,data = m['obj,data'][obj_index] t0 = data['arrival times'][src_index][0] for i,t in enumerate(data['arrival times'][src_index][1:]): d[i].append( float('%0.6f'%convert('arcsec^2 to days', t-t0, obj.dL, obj.z, data['nu'])) ) t0 = t s = product(range(1,1+len(d)), ['solid', 'dashed', 'dashdot', 'dotted']) for k,v in d.iteritems(): #print 'td plot', k, len(v) #print v lw,ls = s.next() pl.hist(v, bins=25, histtype='step', color='k', ls=ls, lw=lw, label='%s - %s' % (str(k+1),str(k+2)), **kwargs) #pl.xlim(xmin=0) pl.ylim(ymin=0) pl.xlim(xmin=pl.xlim()[0] - 0.01*(pl.xlim()[1] - pl.xlim()[0])) pl.legend() pl.xlabel(_time_delays_xlabel) pl.ylabel(r'Count')
def chisq_plot(env, **kwargs): _hist(env, 'sigp:chisq', xlabel=r'$\chi^2$') return models = kwargs.pop('models', env.models) objects = kwargs.pop('objects', None) key = kwargs.pop('key', 'accepted') # select a list to append to based on the 'accepted' property. l = [[], [], []] for m in models: # For H0 we only have to look at one model because the others are the same obj, data = m['obj,data'][0] l[m.get(key,2)].append(data['sigp:chisq']) not_accepted, accepted, notag = l for d,s in zip(l, _styles): if d: pl.hist(d, histtype='step', edgecolor=s['c'], zorder=s['z'], label=s['label'], log=False, **kwargs) if not_accepted or accepted: pl.legend() pl.xlabel(_chisq_xlabel) pl.ylabel(r'Count')
def swi_histogram(self,dir,name,measure,dpi=80,width=8,height=6,b_left='0.1',b_bot='0.1',b_top='0.1',b_right='0.1',bins='20'): s=ccm.stats.Stats('%s/%s'%(dir,name)) data=s.get_raw(measure) bins=int(bins) pylab.figure(figsize=(float(width),float(height))) try: b_left=float(b_left) except: b_left=0.1 try: b_right=float(b_right) except: b_right=0.1 try: b_top=float(b_top) except: b_top=0.1 try: b_bot=float(b_bot) except: b_bot=0.1 pylab.axes((b_left,b_bot,1.0-b_left-b_right,1.0-b_top-b_bot)) pylab.hist(data,bins=bins) img=StringIO.StringIO() if type(dpi) is list: dpi=dpi[-1] pylab.savefig(img,dpi=int(dpi),format='png') return 'image/png',img.getvalue()
def plotHistogram(data, preTime): pylab.figure(1) pylab.hist(data, bins=10) pylab.xlabel("Virus Population At End of Simulation") pylab.ylabel("Number of Trials") pylab.title("{0} Time Steps Before Treatment Simulation".format(preTime)) pylab.show()
def explore_city_data(city_data): """Calculate the Boston housing statistics.""" # Get the labels and features from the housing data housing_prices = city_data.target housing_features = city_data.data ################################### ### Step 1. YOUR CODE GOES HERE ### ################################### # Please calculate the following values using the Numpy library print "Statistics of the Boston housing dataset." print # Size of data (number of houses)? print "Number of houses: ", housing_features.shape[0] # Number of features? print "Number of features: ", housing_features.shape[1] # Minimum price? print "Minimum price (in $1,000s): ", min(housing_prices) # Maximum price? print "Maximum price (in $1,000s): ", max(housing_prices) # Calculate mean price? print "Mean price (in $1,000s): ", housing_prices.mean() # Calculate median price? print "Median price (in $1,000s): ", np.median(housing_prices) # Calculate standard deviation? print "Standard deviation (in $1,000s): ", housing_prices.std() # Make a plot? pl.figure() pl.hist(housing_prices, bins=15) pl.xlabel('Price in $1000s') pl.ylabel('Counts') pl.show()
def simulationDelayedTreatment(numTrials, condition=75): """ Runs simulations and make histograms for problem 1. Runs numTrials simulations to show the relationship between delayed treatment and patient outcome using a histogram. Histograms of final total virus populations are displayed for delays of 300, 150, 75, 0 timesteps (followed by an additional 150 timesteps of simulation). numTrials: number of simulation runs to execute (an integer) """ trialResults = {trialNum: 0 for trialNum in range(numTrials)} for trial in range(numTrials): viruses = [ResistantVirus(0.1, 0.05, {'guttagonol': False}, 0.005) for x in range(100)] treatedPatient = TreatedPatient(viruses, 1000) for timeStep in range(0,condition+150): treatedPatient.update() if timeStep == condition: treatedPatient.addPrescription('guttagonol') print str(trial) + " Completed" trialResults[trial] = treatedPatient.update() print trialResults pylab.hist(trialResults.values(), bins=20) pylab.title("Final Resistant Population - Prescription Given After " + str(condition) + " Time Steps for " + str(numTrials) + " Trials") pylab.xlabel("Final Total Virus Population") pylab.ylabel("Number of Trials") pylab.legend(loc='best') pylab.show()
def plot(self): rows = np.sqrt(self.M).round() cols = np.ceil(self.M/rows) if self.D==1: xmin = self.X.min() xmax = self.X.max() xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin) Xgrid = np.linspace(xmin,xmax,100)[:,None] zz = self.predict(Xgrid) if self.D==2: xmin,ymin = np.vstack(self.X).min(0) xmax,ymax = np.vstack(self.X).max(0) xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin) ymin,ymax = ymin-0.1*(ymax-ymin), ymax+0.1*(ymax-ymin) xx,yy = np.mgrid[xmin:xmax:100j,ymin:ymax:100j] Xgrid = np.vstack((xx.flatten(),yy.flatten())).T zz = self.predict(Xgrid).reshape(100,100,self.M) for m in range(self.M): pb.subplot(rows,cols,m+1) if self.D==1: pb.hist(self.X[m,:,0],self.N/10.,normed=True) pb.plot(Xgrid,zz[:,m],'r',linewidth=2) elif self.D==2: pb.plot(self.X[m,:,0],self.X[m,:,1],'rx',mew=2) zz_data = self.predict(self.X[m])[:,m] pb.contour(xx,yy,zz[:,:,m],[stats.scoreatpercentile(zz_data,5)],colors='r',linewidths=1.5) pb.imshow(zz[:,:,m].T,extent=[xmin,xmax,ymin,ymax],origin='lower',cmap=pb.cm.binary,vmin=0.,vmax=zz_data.max())
def hist_Ne(sel_coinc_ids, coords, data, n): global histNe2 c_index = data.root.coincidences.c_index observ = data.root.coincidences.observables core_rec = data.root.core_reconstructions.reconstructions histNe = core_rec.readCoordinates(coords, field='reconstructed_shower_size') #histNe = [x for x in histNe if x > 0] # for showersize smaller than 0 d = 10**10.2 histNe2 = [x*d for x in histNe] #histNe *= 10 ** 10.2 pylab.hist(np.log10(histNe2), 100, log=True) # histtype="step" pylab.xlabel('Showerenergy log(eV)') pylab.ylabel('count') pylab.title('showersize bij N==%s' %n) pylab.ylim(ymin=1) pylab.grid(True) pylab.show() return histNe2
def plot_distribution_true_false(prediction_df): """ :param prediction_df: :return: """ mask_well_classified = prediction_df.expected == prediction_df.class_predicted for group_name, g in prediction_df.groupby('expected'): print("group_name %s" % group_name) pylab.figure() try: v = g.confidence[mask_well_classified] pylab.hist(list(v), color='g', alpha=0.3, normed=0, range=(0,1), bins=10) #sns.distplot(g.confidence[mask_well_classified], color='g', bins=11) # TRUE POSITIVE except Exception as e: print(e) mask_wrong = (prediction_df.class_predicted == group_name) & (prediction_df.expected != group_name) # FALSE POSITIVE #v = g.confidence[~mask_well_classified] try: v = prediction_df.confidence[mask_wrong] pylab.hist(list(v), color='r', alpha=0.3, normed=0, range=(0,1), bins=10) #sns.distplot(v, color='r', bins=11) except Exception as e: print(e) #print(len(v)) pass print("FIN figure %s" % group_name) pylab.show() print("")
def stage_plots2(sdsscoimgs=None, coimgs=None, comods=None, comods2=None, resids=None, bands=None, **kwargs): for band,co in zip(bands, sdsscoimgs): print 'co', co.shape plt.clf() plt.hist(co.ravel(), range=(-0.1, 0.1), bins=100) plt.title('SDSS %s band' % band) plt.savefig('sdss-%s.png' % band) print band, 'band 16th and 84th pcts:', np.percentile(co.ravel(), [16,84]) kwa = dict(mnmx=(-2,10), scales=dict(g=(2,0.02), r=(1,0.03), z=(0,0.1))) #z=(0,0.22))) plt.clf() dimshow(get_rgb(sdsscoimgs, bands, **kwa), ticks=False) plt.savefig('sdss2.png') plt.clf() dimshow(get_rgb(coimgs, bands, **kwa), ticks=False) plt.savefig('img2.png')
def fig4(): # Figure 4 # histogram has the ability to plot multiple data in parallel ... # Note the new color kwarg, used to override the default, which # uses the line color cycle. # P.figure() # create a new data-set x = mu + sigma * P.randn(1000, 3) n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', color=['crimson', 'burlywood', 'chartreuse'], label=['Crimson', 'Burlywood', 'Chartreuse']) P.legend() # # ... or we can stack the data # P.figure() n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', stacked=True) P.show() # # we can also stack using the step histtype # P.figure() n, bins, patches = P.hist(x, 10, histtype='step', stacked=True, fill=True)
def plot_function_data_scientific(self): """plot the crap in a more scientific way! more information here: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot """ print '[main] plot data' function_name_to_plot_regex = re.compile('^.*(flip|Critical).*$') #size in inches, come on... pylab.figure(figsize = (32, 18)) #plot data for element in self.__function_name_value_tuple_list: if function_name_to_plot_regex.match(element[0]): #for time_value in element[1]: #counter_dict = collections.Counter(element[1]) pylab.hist(element[1], label = element[0], histtype = 'bar') #pylab.plot(counter_dict.keys(), counter_dict.values(), 'O', label = element[0]) #decorate plot pylab.xlabel('used time in function [ns]') pylab.ylabel('value count') pylab.title('JNI ByteFlipper Performance Test (size of byte array: %d)' % (self.__byte_array_size)) pylab.grid(True) pylab.legend(loc = 'best') plot_file_name = self.log_file_name.strip('.log') + '_scientific.png' print '[main] write simple plot to image (%s)' % plot_file_name pylab.savefig(plot_file_name)
def ShowRawToF(): raw_codes = GetTimecodes_AllFilesInDir(timepix_path_1, xmin, xmax, ymin, ymax, 0, checkerboard_phase = None) offset = 0 tmin = 0 tmax = 11810 fig = pl.figure(figsize=(14,10)) n_codes, bins, patches = pl.hist([11810-i for i in raw_codes], bins = 11810, range = [0,11810]) pl.yscale('log', nonposy='clip') for i,code in enumerate(raw_codes): raw_codes[i] = (11810. - code) - offset raw_codes[i] *= 20 fig = pl.figure(figsize=(14,10)) n_codes, bins, patches = pl.hist(raw_codes, bins = 1000, range = [0,40000]) # n_codes, bins, patches = pl.hist(raw_codes, bins = tmax-tmin, range = [tmin,tmax]) # pl.clf() # n_codes = n_codes[:-1] # pl.plot(bins, n_codes, 'k-.o', label="Timecodes") pl.tight_layout() pl.show()
def fig3(x): # Figure 3 # now we create a cumulative histogram of the data # P.figure() n, bins, patches = P.hist(x, 50, normed=1, histtype='step', cumulative=True) # add a line showing the expected distribution y = P.normpdf(bins, mu, sigma).cumsum() y /= y[-1] l = P.plot(bins, y, 'k--', linewidth=1.5) # create a second data-set with a smaller standard deviation sigma2 = 15. x = mu + sigma2 * P.randn(10000) n, bins, patches = P.hist(x, bins=bins, normed=1, histtype='step', cumulative=True) # add a line showing the expected distribution y = P.normpdf(bins, mu, sigma2).cumsum() y /= y[-1] l = P.plot(bins, y, 'r--', linewidth=1.5) # finally overplot a reverted cumulative histogram n, bins, patches = P.hist(x, bins=bins, normed=1, histtype='step', cumulative=-1) P.grid(True) P.ylim(0, 1.05)
def plotQuizzes(): scores = generateScores(10000) pylab.hist(scores, bins=7) pylab.xlabel("Final Score") pylab.ylabel("Number of Trials") pylab.title("Distribution of Scores") pylab.show()
def pair_plot(data, savefile=None, display=True, **kwargs): chan = data.channels l = len(chan) figure = pylab.figure() pylab.subplot(l, l, 1) for i in range(l): for j in range(i + 1): pylab.subplot(l, l, i * l + j + 1) if i == j: pylab.hist(data[:, i], bins=200, histtype='stepfilled') else: pylab.scatter(data[:, i], data[:, j], **kwargs) if j == 0: pylab.ylabel(chan[i]) if i == l - 1: pylab.xlabel(chan[j]) if display: pylab.show() if savefile: pylab.savefig(savefile) return figure
def simulationTwoDrugsDelayedTreatment(numTrials): """ Runs simulations and make histograms for problem 2. Runs numTrials simulations to show the relationship between administration of multiple drugs and patient outcome. Histograms of final total virus populations are displayed for lag times of 300, 150, 75, 0 timesteps between adding drugs (followed by an additional 150 timesteps of simulation). numTrials: number of simulation runs to execute (an integer) """ # simulation incorrect... wait_time = (300, 150, 75, 0) finals = [[] for i in wait_time] for wait in wait_time: results = [] print "\ntrial (wait time = " + str(wait) + ") working", for i in range(numTrials): tmp = simulationWithDrug(100, 1000, 0.1, 0.05, {'guttagonol': False}, 0.005, 1, before_drug=150, after_drug=wait, plot=False) sec_tmp = simulationWithDrug(int(tmp[-1]), 1000, 0.1, 0.05, {'grimpex': False}, 0.005, 1, before_drug=0, after_drug=150, plot=False) results.append(tmp[-1]) if i%2: print ".", finals[wait_time.index(wait)] += results for results in finals: pylab.hist(results, [x for x in range(1000) if x % 50 == 0]) pylab.title("Wait Time = " + str(wait_time[finals.index(results)])) pylab.xlabel("Final Virus Populations") pylab.ylabel("Trials") pylab.ylim(0, numTrials) pylab.show()
def plotHist(result, title, xLabel, yLabel): pylab.hist(result) pylab.title(title) pylab.xlabel(xLabel) pylab.ylabel(yLabel) pylab.legend(loc = 1) pylab.show()
def plotVowelProportionHistogram(wordList, numBins=15): """ Plots a histogram of the proportion of vowels in each word in wordList using the specified number of bins in numBins """ vowels = 'aeiou' vowelProportions = [] for word in wordList: vowelsCount = 0.0 for letter in word: if letter in vowels: vowelsCount += 1 vowelProportions.append(vowelsCount / len(word)) meanProportions = sum(vowelProportions) / len(vowelProportions) print "Mean proportions: ", meanProportions pylab.figure(1) pylab.hist(vowelProportions, bins=15) pylab.title("Histogram of Proportions of Vowels in Each Word") pylab.ylabel("Count of Words in Each Bucket") pylab.xlabel("Proportions of Vowels in Each Word") ymin, ymax = pylab.ylim() ymid = (ymax - ymin) / 2 pylab.text(0.03, ymid, "Mean = {0}".format( str(round(meanProportions, 4)))) pylab.vlines(0.5, 0, ymax) pylab.text(0.51, ymax - 0.01 * ymax, "0.5", verticalalignment = 'top') pylab.show()
def simulationDelayedTreatment(): """ Runs simulations and make histograms for problem 5. Runs multiple simulations to show the relationship between delayed treatment and patient outcome. Histograms of final total virus populations are displayed for delays of 300, 150, 75, 0 timesteps (followed by an additional 150 timesteps of simulation). """ histBins = [i*50 for i in range(11)] delays = [0, 75, 150, 300] subplot = 1 for d in delays: results = [] for t in xrange(NUM_TRIALS): results.append(runSimulation(d)) pylab.subplot(2, 2, subplot) subplot += 1 pylab.hist(results, bins=histBins, label='delayed ' + str(d)) pylab.xlim(0, 500) pylab.ylim(0, NUM_TRIALS) pylab.ylabel('number of patients') pylab.xlabel('total virus population') pylab.title(str(d) + ' time step delay') popStd = numpy.std(results) popMean = numpy.mean(results) ## print str(d)+' step delay standard deviation: '+str(popStd) ## print str(d)+' step mean: '+str(popMean) ## print str(d)+' step CV: '+str(popStd / popMean) ## print str(d) + ' step delay: ' + str(results) pylab.suptitle('Patient virus populations after 150 time steps when ' +\ 'prescription\n' +\ 'is applied after delays of 0, 75, 150, 300 time steps') pylab.show()
def distribution_plot(item_array, index_num): vals = [x[index_num] for x in item_array] #vals = [] #for db_domain in cur_db.keys(): # vals.append(cur_db[db_domain][item_key]) pylab.hist(vals, 20) pylab.show()
def test_wald_sample(self): acc=ShiftedWaldAccumulator(.2, .2, 2.0) nsamples=100000 x=np.linspace(0,10, nsamples) import pylab as pl samp=acc.sample(nsamples) #dens=scipy.stats.gaussian_kde(samp[samp<10]) pl.hist(acc.sample(nsamples),200, normed=True) h,hx=np.histogram(samp, density=True, bins=1000) hx=hx[:-1]+(hx[1]-hx[0])/2. #assert np.all(np.abs(h-acc.pdf(hx))<1.5) # kolmogoroff smirnov tests whether samples come from CDF D,pv=scipy.stats.kstest(samp, acc.cdf) print D,pv assert pv>.05, "D=%f,p=%f"%(D,pv) if True: pl.clf() #pl.subplot(2,1,1) #pl.hist(samp[samp<10],300, normed=True, alpha=.3) #pl.subplot(2,1,2) pl.bar(hx, h, alpha=.3, width=hx[1]-hx[0]) pl.plot(x,acc.pdf(x), color='red', label='analytical') #pl.plot(x,dens(x), color='green', label='kde') pl.xlim(0,3) pl.legend() self.savefig()
def make_cdf(ham, spam, similarity_name, file_name=None): hist( ham, alpha=0.5, bins=100, normed=True, cumulative=True, histtype='stepfilled', label='Ham (' + str(len(ham)) + ')', color='b') hist( spam, alpha=0.5, bins=100, normed=True, cumulative=True, histtype='stepfilled', label='Spam (' + str(len(spam)) + ')', color='r') legend(loc=2) title("CDF of %s similarity for spam and ham" % (similarity_name)) xlabel("%s similarity" % (similarity_name)) ylabel("proportion") if file_name is None: savefig("%s_cdf.png" % (similarity_name)) else: savefig(file_name) show() clf()
def simulationDelayedTreatment(numTrials): """ Runs simulations and make histograms for problem 1. Runs numTrials simulations to show the relationship between delayed treatment and patient outcome using a histogram. Histograms of final total virus populations are displayed for delays of 300, 150, 75, 0 timesteps (followed by an additional 150 timesteps of simulation). numTrials: number of simulation runs to execute (an integer) """ # TODO patients = [TreatedPatient(viruses=[ResistantVirus(maxBirthProb=0.1, clearProb=0.05, resistances={'guttagonol': False}, mutProb=0.005) for x in xrange(100)], maxPop=1000) for i in xrange(numTrials)] delay = 75 viruspops = [] for p in patients: for i in xrange(delay): p.update() p.addPrescription('guttagonol') for i in xrange(150): p.update() viruspops.append(p.getTotalPop()) pylab.hist(viruspops,10) pylab.show()
def hist_scores(scores, labels, weights=None, **kwargs): if hasattr(scores, "values"): scores = scores.values if hasattr(labels, "values"): labels = labels.values if hasattr(weights, "values"): weights = weights.values if weights != None: s_w = weights[labels == 1] b_w = weights[labels == 0] else: s_w = b_w = None kwargs["bins"] = kwargs.get("bins", 100) kwargs["normed"] = kwargs.get("normed", True) kwargs["histtype"] = kwargs.get("histtype", "stepfilled") if scores.ndim == 2: scores = scores[:, 1] plt.hist(scores[labels == 1], alpha=0.5, label="signal", weights=s_w, color="blue", **kwargs) plt.hist(scores[labels == 0], alpha=0.5, label="bkgd", weights=b_w, color="red", **kwargs) plt.legend(loc="best")
# resultats des simulations print(mu, sigma) ''' 5000.084989964445 223.6842714131764 ''' # resultats en tenant compte de la regle des chiffres significatifs print("Resultats :") print("Moyenne = ", "%.f" % mu) print("deviation standard = ", "%.f" % sigma) ''' Resultats : Moyenne = 5000 deviation standard = 224 ''' # trace de l'histogramme n, bins, patches = pylab.hist(surfaces, 60, normed=1, facecolor='gray', alpha=0.75) y = mlab.normpdf(bins, mu, sigma) l = pylab.plot(bins, y, 'k-', linewidth=2) #plt.xlabel('Smarts') pylab.xlabel('surface') pylab.ylabel(u'probabilite') pylab.title('$\mathrm{histogramme\ de\ la\ surface:}\ \mu=%.f,\ \sigma=%.f$' % (mu, sigma)) pylab.grid(True) pylab.show()
fig, ax = plt.subplots() ax.plot(time, data, 'o', label='data') ax.plot(time, model(time, **injection_parameters), '--r', label='signal') ax.set_xlabel('time') ax.set_ylabel('y') ax.legend() fig.savefig('{}/{}_data.png'.format(outdir, label)) ''' #sigma = 1 #injection_parameters.update(dict(sigma=1)) # Now lets instantiate the built-in GaussianLikelihood, giving it # the time, data and signal model. Note that, because we do not give it the # parameter, sigma is unknown and marginalised over during the sampling E_y, E_x, E_ = hist(T_s, 50, alpha=.3, label='On-Pulse', density=True) E_x = (E_x[1:] + E_x[:-1]) / 2 likelihood = bilby.core.likelihood.GaussianLikelihood(E_x, E_y, model) priors = dict() priors['f'] = bilby.core.prior.Uniform(1e-5, 1 - (1e-5), 'f') #priors['f'] = 1 priors['mu1'] = bilby.core.prior.Uniform(0, 5, 'mu1') priors['sigma1'] = bilby.core.prior.Uniform(0, 5, 'sigma1') #priors['A1'] = bilby.core.prior.Uniform(0, 10000, 'A1') priors['alpha1'] = bilby.core.prior.Uniform(2, 6, 'alpha1') #priors['alpha1'] = 2 priors['mu2'] = bilby.core.prior.Uniform(5, 10, 'mu2') priors['sigma2'] = bilby.core.prior.Uniform(0.5, 5, 'sigma2') #priors['A2'] = bilby.core.prior.Uniform(0, 10000, 'A2')
r'$270^{\circ}$', r'$300^{\circ}$', r'$330^{\circ}$' ]) #,r'$360^{\circ}$' ax.set_xlabel(r'$\alpha$') ax.set_ylabel(r'$\delta$') if save: fname = '%stargets_distribution' % folder_figures figures.savefig(fname, fig, fancy) ### A histogram of the magnitudes fig = plt.figure(dpi=100) ax = fig.add_subplot(111) bins = np.linspace(np.amin(mag_cat), np.amax(mag_cat), 50) n, bins, patches = plt.hist(mag_cat, bins=bins) plt.setp(patches, 'edgecolor', 'black', 'linewidth', 2, 'facecolor', 'blue', 'alpha', 1) ax.xaxis.set_major_locator(MultipleLocator(2)) ax.xaxis.set_minor_locator(MultipleLocator(1)) ax.yaxis.set_major_locator(MultipleLocator(5)) ax.yaxis.set_minor_locator(MultipleLocator(1)) ax.xaxis.grid(True, 'minor') ax.yaxis.grid(True, 'minor') ax.xaxis.grid(True, 'major', linewidth=2) ax.yaxis.grid(True, 'major', linewidth=2) plt.xlim([np.amin(mag_cat) * 0.95, 1.05 * np.amax(mag_cat)])
for [x, y] in L: for delx in v: for dely in u: cir = pylab.Circle((x + delx, y + dely), radius=sigma) pylab.gca().add_patch(cir) pylab.axis([0, Lx, 0, Ly]) pylab.xlabel('x-position') pylab.ylabel('y-position') pylab.title('Snapshot Final Positions', fontsize=14) #--------------------------------------------------------------------------------# #----------------------------Histogram of x positions ---------------------------# if histox != []: plt.subplot(223) pylab.hist(histox, normed=True, bins=100, histtype='step') pylab.xlabel('x-position') pylab.ylabel('Probability Density') pylab.title('Histogram of x-positions with $\eta$ = %.2f' % eta, fontsize=14) else: print 'There was no movement in the x-axis' #--------------------------------------------------------------------------------# #----------------------------Histogram of y positions ---------------------------# if histoy != []: plt.subplot(224) pylab.hist(histoy, normed=True, bins=100, histtype='step') pylab.xlabel('y-position') pylab.ylabel('Probability Density')
import sys import numpy as np import scipy.stats as stats from scipy.stats import variation import pylab as pl from numpy import genfromtxt if len(sys.argv) >= 2: csv_file = sys.argv[1] my_data = genfromtxt(csv_file, delimiter=',') else: print("Please provide the csv file with the data") sys.exit(-1) print("Standard Deviation: %f" % np.std(my_data)) print("Mean: %f" % np.mean(my_data)) print("Coefficient of variation: %f" % variation(my_data)) h = sorted(my_data) fit = stats.norm.pdf(h, np.mean(h), np.std(h)) pl.plot(h, fit, '-o') pl.hist(h, normed=True) pl.show()
pdf = triangular_function(x) cdf = cdf_triangular_function(x) cdf_reverse = reverse_cdf_triangular_function(x) random_x = random_generator(get_seed(), N) sorted_x = np.sort(random_x) pl.plot(x, pdf) pl.grid(True) pl.show() pl.plot(x, cdf) pl.grid(True) pl.show() pl.plot(x, cdf_reverse) pl.axis([0, 1, -2, 2]) pl.grid(True) pl.show() # Plot random numbers pl.plot(x_N, random_x, 'xb') pl.show() # Plot sort random numbers pl.plot(x_N, sorted_x, 'r') pl.show() # Plot histogram pl.hist(random_x, 100, color='g', edgecolor='black') pl.grid(True) pl.show()
def makeHist(data, title, xlabel, ylabel, bins=20): pylab.hist(data, bins=bins) pylab.title(title) pylab.xlabel(xlabel) pylab.ylabel(ylabel)
print(str1) else: spdarr = numpy.asarray(dtlst[j]) dirarr = numpy.asarray(dtlst2[j]) # Data Frame wndfrm = pandas.DataFrame({'WindDir': dirarr, 'WindSpeed': spdarr}) wndsb = wndfrm[(wndfrm['WindDir'].notnull()) & (wndfrm['WindSpeed'].notnull())] print(numpy.amax(wndsb['WindSpeed'])) # Speed Histogram bnsq = numpy.arange(0.0, 54.0, 3.0) fig = pyplot.figure(figsize=(7.5, 6), facecolor='w', edgecolor='w') p1 = pyplot.subplot(1, 1, 1) n, bins, patches = pylab.hist(wndsb['WindSpeed'], bnsq, density=False, histtype='bar', \ rwidth=1.0,color='#3333CC') p1.xaxis.grid(color='#777777', linestyle='dotted') p1.yaxis.grid(color='#777777', linestyle='dotted') p1.set_xlabel('Wind Speed', size=12) p1.set_ylabel('Count', size=12) for lb in p1.xaxis.get_ticklabels(): lb.set_fontsize(11) for lb in p1.yaxis.get_ticklabels(): lb.set_fontsize(11) tstr = '%s Wind Speed' % (stns[j]) pyplot.title(tstr, size=14) pngnm = '%s_WindSpeedHist.png' % (stns[j]) fig.savefig(pngnm, bbox_inches=0) pyplot.close() wndsb = wndsb[(wndsb['WindSpeed'] > 0)]
""" import matplotlib matplotlib.use('Qt4Agg') from numpy import * import pylab as pl from sklearn import datasets from sklearn.utils import shuffle # Load the boston dataset boston = datasets.load_boston() # Randomly shuffle the sample set. This data could be ordered and we want to shuffle # it so that we can divide it into training and testing set X, y = shuffle(boston.data, boston.target) offset = int(0.7 * len(X)) X_train, y_train = X[:offset], y[:offset] X_test, y_test = X[offset:], y[offset:] # Plot the target distribution in training set pl.figure() pl.title('Training Set: MEDV distribution') bins = range(0, 60, 5) n, bins, patches = pl.hist(y_train, bins, normed=1, histtype='bar', rwidth=0.8) # Plot the testing distribution in testing set pl.figure() pl.title('Test Set: MEDV distribution') n, bins, patches = pl.hist(y_test, bins, normed=1, histtype='bar', rwidth=0.8) pl.show()
ec.wstat(x, xerr) # calculate the weighted statistics # assign initial guess as two mixtures alpha = np.array([0.5, 0.5]) mu = np.array([0.3, 0.1]) sigma = np.array([0.05, 0.2]) #aic=ec.aic_ecgmm(x,xerr,alpha,mu,sigma) bic = ec.bic_ecgmm(x, xerr, alpha, mu, sigma) #make plots pl.figure(figsize=(12, 6)) pl.subplot(1, 2, 1) y = np.arange(-1, 1, 0.0001) pl.hist(x, bins=30, alpha=0.3, normed=True, facecolor='green') ec.ecgmmplot(y, alpha, mu, sigma) pl.xlabel = 'x' pl.text(-0.9, 3, r'$\mu=$' + str(mu)) pl.text(-0.9, 2.6, r'$\sigma=$' + str(sigma)) pl.ylim(0, 7) pl.title("ECGMM") # non-error corrected GMM alpha = np.array([0.5, 0.5]) mu = np.array([0.3, 0.1]) sigma = np.array([0.05, 0.2]) xerr[:] = 0 bic = ec.bic_ecgmm(x, xerr, alpha, mu, sigma)
print (n) lmin, lmax = _logicle([0, T], T, m, r) pylab.clf() pylab.figtext(0.5, 0.94, 'Logicle transform with r=%.2f, d=%d and T=%d\nData is normal(0, 50, 50000) + lognormal(8, 1, 50000)' % (r, d, T), va='center', ha='center', fontsize=12) pylab.subplot(4, 1, 1) x = arange(0, m, 0.1) pylab.plot(x, S(x, 0, T, m, w)) locs, labs = pylab.xticks() pylab.xticks([]) pylab.yticks([]) pylab.ylabel('Inverse logicle') pylab.subplot(4, 1, 2) pylab.hist(d3, 1250) locs, labs = pylab.xticks() pylab.xticks([]) pylab.yticks([]) pylab.ylabel('Raw data') pylab.subplot(4, 1, 3) d = 1e5 / lmax * _logicle(d3, T, m, r) d[d < 0] = 0 pylab.hist(d, 1250) locs, labs = pylab.xticks() #pylab.xticks([]) pylab.yticks([]) pylab.ylabel('Data after transform') pylab.subplot(4, 1, 4) d = array(d3, dtype='double')
fig = pl.figure(100, figsize=(18,10)) pl.clf() sp1 = pl.subplot(2,3,1) pl.plot(z_after, (100*np.abs((xp_after-xp_before)-(xp[ii+1, :]-xp[0, :]))/np.std(xp[ii+1, :]-xp[0, :])), '.r', markersize = 2) pl.grid('on') pl.ylabel('''error($\Delta$x')/$\Delta$x' [%]''') pl.subplot(2,3,4, sharex=sp1) pl.plot(z_after, (xp[ii+1, :]-xp[0, :]), '.r', label='HT', markersize = 2) pl.plot(z_after, (xp_after-xp_before), '.b', label='PyHT', markersize = 2) ms.sciy() pl.grid('on') pl.ylabel("$\Delta$x'") pl.xlabel('z[m]') pl.legend(prop = {'size':14}) pl.subplot(2,3,3) pl.hist((100*np.abs((xp_after-xp_before)-(xp[ii+1, :]-xp[0, :]))/np.std(xp[ii+1, :]-xp[0, :])), bins=100, range=(0,100)) pl.xlabel('Error_x [%]') pl.ylabel('Occurrences') pl.xlim(0,100) rms_err_x = np.std(100*np.abs((xp_after-xp_before)-(xp[ii+1, :]-xp[0, :]))/np.std(xp[ii+1, :]-xp[0, :])) sp1 = pl.subplot(2,3,2) pl.plot(z_after, (100*np.abs((yp_after-yp_before)-(yp[ii+1, :]-yp[0, :]))/np.std(yp[ii+1, :]-yp[0, :])), '.r', markersize = 2) pl.grid('on') pl.ylabel('''error($\Delta$y')/$\Delta$y' [%]''') pl.subplot(2,3,5, sharex=sp1) pl.plot(z_after, (yp[ii+1, :]-yp[0, :]), '.r', label='HT', markersize = 2) pl.plot(z_after, (yp_after-yp_before), '.b', label='PyHT', markersize = 2) ms.sciy() pl.grid('on') pl.ylabel("$\Delta$y'")
import random import pylab vals = [] for i in range(100000): num = random.random() vals.append(num) xmin, xmax = pylab.xlim() ymin, ymax = pylab.ylim() print 'x-range =', xmin, '-', xmax print 'y-range =', ymin, '-', ymax pylab.hist(vals, bins = 11) pylab.show() #pylab.xlim(-1.0, 2.0)
objective3 = Minimize(sum(deadzone(A * xdz + b, dz))) p3 = Problem(objective3, []) # Solve the problems p1.solve() p2.solve() p3.solve() # Plot histogram of residuals range_max = 2.0 #rr = np.arange(-range_max, range_max, 1e-2) rr = np.linspace(-2, 3, 20) # l-1 plot subplot(3, 1, 1) n, bins, patches = hist(A * x1.value - b, 50, range=[-2, 2]) plot(bins, np.abs(bins) * 35 / 3, '-') # multiply by scaling factor for plot ylabel('l-1 norm') title('Penalty function approximation') # l-2 plot subplot(3, 1, 2) n, bins, patches = hist(A * x2.value - b, 50, range=[-2, 2]) plot(bins, np.power(bins, 2) * 2, '-') ylabel('l-2 norm') # deadzone plot subplot(3, 1, 3) n, bins, patches = hist(A * xdz.value + b, 50, range=[-2, 2]) zeros = np.array([0 for x in bins]) plot(bins, np.maximum((np.abs(bins) - dz) * 35 / 3, zeros), '-')
pl.yscale('log') pl.xlim((-2.1,1.1)) pl.ylim((0.001,10)) pl.text(-1.8,2,'Global',fontsize=14) pl.savefig('all_scatter_both_'+str(month)+'.pdf') pl.close() ##running time histogram for the three stages month=10 pl.figure(figsize=(6,3)) t1=list(csv.reader(open('times_'+str(month)+'_1.csv','r'))) t2=list(csv.reader(open('times_'+str(month)+'_global.csv','r'))) t3=list(csv.reader(open('times_'+str(month)+'_apply.csv','r'))) n,bins,patches=pl.hist([float(r[5]) for r in t1[1:-1]],histtype='step',normed=True,color='blue',bins=2000,cumulative=True) patches[0].set_xy(patches[0].get_xy()[:-1]) n,bins,patches=pl.hist([float(r[2]) for r in t2[1:-1]],histtype='step',normed=True,color='red',bins=2000,cumulative=True) patches[0].set_xy(patches[0].get_xy()[:-1]) n,bins,patches=pl.hist([float(r[2]) for r in t3[1:-1]],histtype='step',normed=True,color='green',bins=2000,cumulative=True) patches[0].set_xy(patches[0].get_xy()[:-1]) h1,=pl.plot([-1,-1]) h2,=pl.plot([-1,-1]) h3,=pl.plot([-1,-1]) pl.xscale('log') pl.xlabel('Running time (s)') pl.ylabel('Fraction of runs') pl.ylim((0,1.1)) pl.legend((h1,h2,h3),('Meta-parameter optimization','Global otimization','Model application'),loc=4) pl.grid() pl.subplots_adjust(left=0.1,bottom=0.2,right=0.99)
def fit_exp(o_xdata, o_ydata, o_yerr): import scipy, pylab a = scipy.array(o_ydata) a, b, varp = pylab.hist(a, bins=scipy.arange(-2, 2, 0.05)) pylab.xlabel("shear") pylab.ylabel("Number of Galaxies") #pylab.show() o_xdata = scipy.array(o_xdata) o_ydata = scipy.array(o_ydata) o_yerr = scipy.array(o_yerr) print o_yerr #o_xdata = o_xdata[abs(o_ydata) > 0.05] #o_yerr = o_yerr[abs(o_ydata) > 0.05] #o_ydata = o_ydata[abs(o_ydata) > 0.05] both = 0 As = [] for z in range(15): xdata = [] ydata = [] yerr = [] for i in range(len(o_xdata)): rand = int(random.random() * len(o_xdata)) - 1 #print rand, len(o_xdata) xdata.append(o_xdata[rand]) ydata.append(o_ydata[rand]) yerr.append(o_yerr[rand]) xdata = scipy.array(xdata) ydata = scipy.array(ydata) ########## # Fitting the data -- Least Squares Method ########## # Power-law fitting is best done by first converting # to a linear equation and then fitting to a straight line. # # y = a * x^b # log(y) = log(a) + b*log(x) # powerlaw = lambda x, amp, index: amp * (x**index) #print xdata #print ydata ydata = ydata / (scipy.ones(len(ydata)) + abs(ydata)) # define our (line) fitting function #fitfunc = lambda p, x: p[0]*x**p[1] if both: fitfunc = lambda p, x: p[0] * x**p[1] errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err pinit = [1., -1.] else: fitfunc = lambda p, x: p[0] * x**-1. errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err pinit = [1.] #, -1.0] #ydataerr = scipy.ones(len(ydata)) * 0.3 out = scipy.optimize.leastsq(errfunc, pinit, args=(scipy.array(xdata), scipy.array(ydata), scipy.array(yerr)), full_output=1) if both: pfinal = out[0] covar = out[1] print pfinal print covar index = pfinal[1] amp = pfinal[0] ampErr = covar[0][0] indexErr = covar[1][1] else: pfinal = out[0] covar = out[1] print pfinal print covar index = -1. #pfinal[1] amp = pfinal #[0] ampErr = covar[0][0]**0.5 indexErr = 0 #covar #indexErr = scipy.sqrt( covar[0][0] ) #ampErr = scipy.sqrt( covar[1][1] ) * amp ########## # Plotting data ########## import pylab pylab.clf() #pylab.subplot(2, 1, 1) pylab.errorbar(xdata, ydata, yerr=yerr, fmt='k.') # Data from copy import copy xdataline = copy(xdata) xdataline.sort() pylab.plot(xdataline, powerlaw(xdataline, amp, index)) # Fit pylab.text(5, 6.5, 'Ampli = %5.2f +/- %5.2f' % (amp, ampErr)) pylab.text(5, 5.5, 'Index = %5.2f +/- %5.2f' % (index, indexErr)) pylab.title('Best Fit Power Law') pylab.xlabel('X') pylab.ylabel('Y') #pylab.subplot(2, 1, 2) #pylab.loglog(xdataline, powerlaw(xdataline, amp, index)) #pylab.errorbar(xdata, ydata, yerr=ydataerr, fmt='k.') # Data #pylab.xlabel('X (log scale)') #pylab.ylabel('Y (log scale)') #pylab.xlim(1.0, 11) print z if both: pylab.show() #pylab.show() #raw_input() pylab.clf() As.append(amp) As = scipy.array(As) return As.mean(), As.std()
def plot_stats(stats_f, stats_p, hbins=30): stats_f = prep_stats_for_plotting(stats_f) stats_p = prep_stats_for_plotting(stats_p) # data print("plotting data stats") fig = pylab.figure(num=None, facecolor='w', edgecolor='k', frameon=False, tight_layout=True) ax = pylab.subplot(2, 2, 1) _, bins_f, _ = pylab.hist(stats_f['mean'], bins=numpy.linspace(-5, 5), normed=True, histtype='stepfilled') pylab.title('col 0 data mean (forward)') ax = pylab.subplot(2, 2, 3) pylab.hist(stats_p['mean'], bins=bins_f, normed=True, histtype='stepfilled') pylab.title('col 0 data mean (posterior)') ax = pylab.subplot(2, 2, 2) _, bins_f, _ = pylab.hist(stats_f['std'], bins=numpy.linspace(0, 5), normed=True, histtype='stepfilled') pylab.title('col 0 data std (forward)') ax = pylab.subplot(2, 2, 4) pylab.hist(stats_p['std'], bins=bins_f, normed=True, histtype='stepfilled') pylab.title('col 0 data std (posterior)') # hypers pylab.show() print("plotting prior stats") fig = pylab.figure(num=None, facecolor='w', edgecolor='k', frameon=False, tight_layout=True) pylab.clf() ax = pylab.subplot(2, 3, 1) pp_plot(stats_f['hyper_m'], stats_p['hyper_m'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper m') ax = pylab.subplot(2, 3, 2) pp_plot(stats_f['hyper_s'], stats_p['hyper_s'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper s') ax = pylab.subplot(2, 3, 3) pp_plot(stats_f['hyper_r'], stats_p['hyper_r'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper r') ax = pylab.subplot(2, 3, 4) pp_plot(stats_f['hyper_nu'], stats_p['hyper_nu'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper nu') ax = pylab.subplot(2, 3, 5) pp_plot(stats_f['col_alpha'], stats_p['col_alpha'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper col alpha') ax = pylab.subplot(2, 3, 6) pp_plot(stats_f['row_alpha'], stats_p['row_alpha'], hbins) pylab.xlabel('forward') pylab.ylabel('posterior') pylab.title('hyper row alpha') pylab.show() fig = pylab.figure(num=None, facecolor='w', edgecolor='k', frameon=False, tight_layout=True) binned_hist_pair(6, stats_f['hyper_m'], stats_p['hyper_m'], 1, hbins, 'col 0 hyper mu') binned_hist_pair(6, stats_f['hyper_s'], stats_p['hyper_s'], 2, hbins, 'col 0 hyper s', log_scale_x=True) binned_hist_pair(6, stats_f['hyper_r'], stats_p['hyper_r'], 3, hbins, 'col 0 hyper r', log_scale_x=True) binned_hist_pair(6, stats_f['hyper_nu'], stats_p['hyper_nu'], 4, hbins, 'col 0 hyper nu', log_scale_x=True) binned_hist_pair(6, stats_f['col_alpha'], stats_p['col_alpha'], 5, hbins, 'colum crp alpha', log_scale_x=True) binned_hist_pair(6, stats_f['row_alpha'], stats_p['row_alpha'], 6, hbins, 'view_0 row crp alpha', log_scale_x=True) pylab.show()
] sp.baseline.selectregion(exclude=[0] + m_regions + [7680], highlight=True, xtype='pixel') sp.baseline(order=4, highlight_fitregion=True, reset_selection=False, reset=False, subtract=False, selectregion=False, xarr_fit_unit='pixels') sp.plotter.savefig('spw1_baseline_solution.png') pl.figure(2) pl.clf() pl.hist(sp.data[~sp.data.mask], bins=np.linspace(-1, 1)) spn = pyspeckit.Spectrum('sgr_b2_n_SgrB2_12m_spw1_lines.fits') spn.plotter(figure=pl.figure(3)) # baseline is reasonably flat... sp.baseline(order=4, highlight_fitregion=False, reset_selection=False, reset=False, subtract=True, selectregion=False, xarr_fit_unit='pixels') xmin = 90.428 * u.GHz - 244.141 * 7680 * u.kHz xmax = 90.417 * u.GHz
def plot_distribution_matches(data, matches, nbins=31, nbest=5, expand_tails=8, legend=2, plot_cdf=True, p=None, tail='both'): """Plot best matching distributions Parameters ---------- data : np.ndarray Data which was used to obtain the matches matches : list of tuples Sorted matches as provided by match_distribution nbins : int Number of bins in the histogram nbest : int Number of top matches to plot expand_tails : int How many bins away to add to parametrized distributions plots legend : int Either to provide legend and statistics in the legend. 1 -- just lists distributions. 2 -- adds distance measure 3 -- tp/fp/fn in the case if p is provided plot_cdf : bool Either to plot cdf for data using non-parametric distribution p : float or None If not None, visualize null-hypothesis testing (given p). Bars in the histogram which fall under given p are colored in red. False positives and false negatives are marked as triangle up and down symbols correspondingly tail : ('left', 'right', 'any', 'both') If p is not None, the choise of tail for null-hypothesis testing Returns ------- histogram list of lines """ # API changed since v0.99.0-641-ga7c2231 halign = externals.versions['matplotlib'] >= '1.0.0' \ and 'mid' or 'center' hist = pl.hist(data, nbins, normed=1, align=halign) data_range = [np.min(data), np.max(data)] # x's x = hist[1] dx = x[expand_tails] - x[0] # how much to expand tails by x = np.hstack((x[:expand_tails] - dx, x, x[-expand_tails:] + dx)) nonparam = Nonparametric(data) # plot cdf if plot_cdf: pl.plot(x, nonparam.cdf(x), 'k--', linewidth=1) p_thr = p data_p = _pvalue(data, nonparam.cdf, nonparam.rcdf, tail) data_p_thr = (data_p <= p_thr).ravel() npd = Nonparametric(data) x_p = _pvalue(x, npd.cdf, npd.rcdf, tail) x_p_thr = np.abs(x_p) <= p_thr # color bars which pass thresholding in red for thr, bar_ in zip(x_p_thr[expand_tails:], hist[2]): bar_.set_facecolor(('w', 'r')[int(thr)]) if not len(matches): # no matches were provided warning( "No matching distributions were provided -- nothing to plot" ) return (hist, ) lines = [] labels = [] for i in xrange(min(nbest, len(matches))): D, dist_gen, dist_name, params = matches[i] dist = getattr(scipy.stats, dist_gen)(*params) rcdf = _auto_rcdf(dist) label = '%s' % (dist_name) if legend > 1: label += '(D=%.2f)' % (D) xcdf_p = np.abs(_pvalue(x, dist.cdf, rcdf, tail)) xcdf_p_thr = (xcdf_p <= p_thr).ravel() if p is not None and legend > 2: # We need to compare detection under given p data_cdf_p = np.abs(_pvalue(data, dist.cdf, rcdf, tail)) data_cdf_p_thr = (data_cdf_p <= p_thr).ravel() # true positives tp = np.logical_and(data_cdf_p_thr, data_p_thr) # false positives fp = np.logical_and(data_cdf_p_thr, ~data_p_thr) # false negatives fn = np.logical_and(~data_cdf_p_thr, data_p_thr) label += ' tp/fp/fn=%d/%d/%d)' % \ tuple(map(np.sum, [tp, fp, fn])) pdf = dist.pdf(x) line = pl.plot(x, pdf, '-', linewidth=2, label=label)[0] color = line.get_color() if plot_cdf: cdf = dist.cdf(x) pl.plot(x, cdf, ':', linewidth=1, color=color, label=label) # TODO: decide on tp/fp/fn by not centers of the bins but # by the values in data in the ranges covered by # those bins. Then it would correspond to the values # mentioned in the legend if p is not None: # true positives xtp = np.logical_and(xcdf_p_thr, x_p_thr) # false positives xfp = np.logical_and(xcdf_p_thr, ~x_p_thr) # false negatives xfn = np.logical_and(~xcdf_p_thr, x_p_thr) # no need to plot tp explicitely -- marked by color of the bar # pl.plot(x[xtp], pdf[xtp], 'o', color=color) pl.plot(x[xfp], pdf[xfp], '^', color=color) pl.plot(x[xfn], pdf[xfn], 'v', color=color) lines.append(line) labels.append(label) if legend: pl.legend(lines, labels) return (hist, lines)
def explore_ob140613(): data = munge.getdata('ob140613', use_astrom_phot=True) mod_base = '/u/jlu/work/microlens/OB140613/a_2019_06_26/model_fits/' mod_base += '8_fit_multiphot_astrom_parallax2/aa_' mod_fit = model_fitter.PSPL_multiphot_astrom_parallax2_Solver( data, outputfiles_basename=mod_base) mod_fit.summarize_results_modes() tab_all = mod_fit.load_mnest_modes() tab = tab_all[0] tab['mL'] = 10**tab['log_mL'] tab['piE'] = 10**tab['log_piE'] tab['thetaE'] = 10**tab['log_thetaE'] plt.figure(1) plt.clf() plt.hist(tab['log_thetaE'], weights=tab['weights'], bins=50) plt.xlabel('log(thetaE)') plt.figure(3) plt.clf() plt.hist(tab['log_mL'], weights=tab['weights'], bins=50) plt.xlabel('log(mL)') plt.figure(4) plt.clf() plt.hist(tab['log_piE'], weights=tab['weights'], bins=50) plt.xlabel('log(piE)') plt.figure(5) plt.clf() plt.hist(tab['thetaE'], weights=tab['weights'], bins=50) plt.xlabel('thetaE') plt.figure(6) plt.clf() plt.hist(tab['mL'], weights=tab['weights'], bins=50) plt.xlabel('mL') plt.figure(7) plt.clf() plt.hist(tab['piE'], weights=tab['weights'], bins=50) plt.xlabel('piE') plt.figure(8) plt.clf() plt.plot(tab['logLike'], tab['log_mL'], 'k.') summarize_results(tab) return
if(iteration % iterations_per_sample == 0): sample_num += 1 positions = positions_permutations.keys() samples += positions k_cycle_samples[cycle_length - 1] += 1 if(iteration % N == 0): pair_distance_samples += [abs(positions[i] - positions[j]) for i in range(N) for j in range(i + 1, N)] k_cycle_samples = [k_cycle_num / float(sample_num) for k_cycle_num in k_cycle_samples] return samples, k_cycle_samples, pair_distance_samples iteration_num = 100000 iterations_per_sample = 1 #samples = harmonic_distinguishable_directsampling(iteration_num) samples, k_cycle_samples, pair_distance_samples= harmonic_Boson_Metropolis(iteration_num, iterations_per_sample) pylab.hist(samples, normed=True, bins=400, label="Metropolis sampling") xgrid = [0.01 * i for i in range(-500, 501)] distinguishable_analytic_p = [harmonic_distinguishable_analytic_singleparticle_density(x, beta) for x in xgrid] pylab.plot(xgrid, distinguishable_analytic_p, label="Analytic result: distinguishable particles") Boson_analytic_p = [harmonic_Boson_analytic_singleparticle_density(x, beta, N) for x in xgrid] pylab.plot(xgrid, Boson_analytic_p, label="Analytic result: Bosons") pylab.xlabel("x") pylab.ylabel("$p(x) = \\frac{n(x)}{N}$") pylab.title("Normalized single particle density of a system of $N$ non-interacting Bosons in 1-dimensional harmonic trap\n" + "$N$ = %d, $\\beta$ = %s" % (N, beta)) pylab.legend() pylab.show() ################################################################ # The code below computes the probability distribution for the distance of two arbitrary particles in the system of N non-interacting Bosons # in 1-dimensional trap.
error = t['mass_uncertainty'] #error = error[1:len(error)] error = np.asarray(error).astype('float64') error = error[1:len(error)] error = np.trim_zeros(np.sort(error)) error = error[~np.isnan(error)] fit = powerlaw.Fit(masses) alpha = fit.power_law.alpha #determining optimal bin size blocks = astropy.stats.histogram(masses, bins='blocks') pl.hist(masses, log=True, bins=np.logspace(np.log10(0.1),np.log10(np.amax(masses)), 8)) pl.xscale('log') pl.yscale('log') pl.show() #CMF pl.plot(np.log10(np.sort(masses)),np.log10(np.linspace(1,0,len(masses), endpoint=False))) #Fit lines to cmf pl.show() #Estimate error on alpha percent_error = error / masses num_trials = int(1e4)
#!/usr/bin/env python import pylab as P # # The hist() function now has a lot more options # # # first create a single histogram # mu, sigma = 200, 25 x = mu + sigma*P.randn(10000) # the histogram of the data with histtype='step' n, bins, patches = P.hist(x, 50, normed=1, histtype='step') P.setp(patches, 'facecolor', 'g', 'alpha', 0.75) # add a line showing the expected distribution y = P.normpdf( bins, mu, sigma) l = P.plot(bins, y, 'k--', linewidth=1.5) # # create a histogram by providing the bin edges (unequally spaced) # P.figure() bins = [100,125,150,160,170,180,190,200,210,220,230,240,250,275,300] # the histogram of the data with histtype='step' n, bins, patches = P.hist(x, bins, normed=1, histtype='bar', rwidth=0.8)
def plotResults(self): #### calculate stats on collected data and draw some plots #### import matplotlib.mlab as mlab from matplotlib.pyplot import axis, title, xlabel, hist, grid, show, ylabel, plot import pylab results = self.results durations = results[:, 0] flips = results[1:, 2] dmin = durations.min() dmax = durations.max() dmean = durations.mean() dstd = durations.std() fmean = flips.mean() fstd = flips.std() pylab.figure(figsize=[30, 10]) pylab.subplot(1, 3, 1) # the histogram of the delay data n, bins, patches = hist(durations, 50, normed=True, facecolor='blue', alpha=0.75) # add a 'best fit' line y = mlab.normpdf(bins, dmean, dstd) plot(bins, y, 'r--', linewidth=1) xlabel('ioHub getEvents Delay') ylabel('Percentage') title('ioHub Event Delay Histogram (msec.usec):\n' + r'$\ \min={0:.3f},\ \max={1:.3f},\ \mu={2:.3f},\ \sigma={3:.3f}$' .format(dmin, dmax, dmean, dstd)) axis([0, dmax + 1.0, 0, 25.0]) grid(True) # graphs of the retrace data ( taken from retrace example in psychopy demos folder) intervalsMS = flips m = fmean sd = fstd distString = "Mean={0:.1f}ms, s.d.={1:.1f}, 99%CI={2:.1f}-{3:.1f}".format( m, sd, m - 3 * sd, m + 3 * sd) nTotal = len(intervalsMS) nDropped = sum(intervalsMS > (1.5 * m)) droppedString = "Dropped/Frames = {0:d}/{1:d} = {2}%".format( nDropped, nTotal, int(nDropped) / float(nTotal)) pylab.subplot(1, 3, 2) #plot the frameintervals pylab.plot(intervalsMS, '-') pylab.ylabel('t (ms)') pylab.xlabel('frame N') pylab.title(droppedString) pylab.subplot(1, 3, 3) pylab.hist(intervalsMS, 50, normed=0, histtype='stepfilled') pylab.xlabel('t (ms)') pylab.ylabel('n frames') pylab.title(distString) show()
import pylab as plt datas = plt.loadtxt("v2.dat") plt.hist(datas, bins=100, normed=1) plt.show()
#print(arr) final = np.array([]) for i in range(64): new = np.arange(256) final = np.append(final, new) final1 = [i for i in range(256 * 64)] k = np.std(arr) print(k) final1 = np.append(final1, [i for i in range(62)]) print(len(arr)) print(len(final)) print(len(final1)) n, bins, patches = plt.hist(arr, 64, normed=1, facecolor='g', alpha=0.75) #plt.text(60, .025, r'$\mu=100,\ \sigma=15$') #plt.axis([1, 16446, -2, 10]) plt.grid(True) plt.hist(arr, bins=64, normed=True, histtype='step') #plt.show() #plt.hist(arr,final1) plt.bar(final1, arr, align='center') #plt.show() #bar(arr,final1) bar(final1, arr, width=1, align='center', color='brown') #plot(final1, arr, color='purple', lw=1, marker='.') plt.ylabel('volt') #plotting.scatter_matrix(df[['frequency', 'voltage']]) plt.show()
import pylab n, bins, patches = pylab.hist(pylab.randn(1000), 40, normed=1) l, = pylab.plot(bins, pylab.normpdf(bins, 0.0, 1.0), 'r--', label='fit', linewidth=3) pylab.legend([l, patches[0]], ['fit', 'hist']) pylab.show()
L = [(random.uniform(sigma, 1.0 - sigma), random.uniform(sigma, 1.0 - sigma))] for k in range(1, N): a = (random.uniform(sigma, 1.0 - sigma), random.uniform(sigma, 1.0 - sigma)) min_dist_sq = min(((a[0] - b[0])**2 + (a[1] - b[1])**2) for b in L) if min_dist_sq < 4.0 * sigma**2: overlap = True break else: overlap = False L.append(a) return L N = 4 sigma = 0.1197 n_runs = 1000000 histo_data = [] for run in range(n_runs): pos = direct_disks_box(N, sigma) for k in range(N): histo_data.append(pos[k][0]) pylab.hist(histo_data, bins=100, normed=True) pylab.xlabel('x') pylab.ylabel('frequency') pylab.title('Probability distribution - Direct Disks Box') pylab.grid() pylab.savefig('direct_disks_histo.png') pylab.show()
def main(): cm = plt.get_cmap('Blues') try: sns.set_style("white") except: pass #cm.set_gamma(0.2) # name = sys.argv[1] outname = name.split('.')[0] # data = np.loadtxt(name) res = makepdfs(data[:,1], data[:,5]) plt.close('all') plt.hist(res[0], weights=res[1], bins=31, histtype='step', lw=4, color='k') plt.xlim(0.,3.) #plt.plot(res[0], res[1], c='k', lw=2) plt.xlabel('IMF Slope', fontsize=20) plt.ylabel('Probability', fontsize=20) plt.savefig(outname+'_marginal_gamma.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,1], data[:,3], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Log(Age)') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_gamma_age.png', dpi=300, bbox_inches='tight') plt.figure() plt.hist(res2[0], weights = res2[2]) res2 = make2dpdfs(data[:,1], data[:,0], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Av') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_gamma_av.png', dpi=300, bbox_inches='tight') plt.figure() plt.hist(res2[0], weights = res2[2]) res2 = make2dpdfs(data[:,0], data[:,3], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('Log(Age)') plt.xlabel('Av') cb.set_label('Probability') plt.savefig(outname+'_joint_av_age.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,1], data[:,8], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('BF') plt.xlabel('IMF Slope') cb.set_label('Probability') plt.savefig(outname+'_joint_bf_gamma.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,0], data[:,9], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('dAv') plt.xlabel('Av') cb.set_label('Probability') plt.savefig(outname+'_joint_av_dav.png', dpi=300, bbox_inches='tight') res2 = make2dpdfs(data[:,8], data[:,9], data[:,5]) plt.figure() ax = plt.subplot(111) h, xedges, yedges = np.histogram2d(res2[0], res2[1], weights=res2[2]) plt.imshow(h.T, origin='low', interpolation='None', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], cmap=cm, aspect='auto') cb = plt.colorbar() plt.ylabel('dAv') plt.xlabel('BF') cb.set_label('Probability') plt.savefig(outname+'_joint_bf_dav.png', dpi=300, bbox_inches='tight') #print(res[1]) plt.show()
I = np.flatnonzero(Sgrz.class_star_r < 0.1) plt.clf() plt.loglog(S.fluxerr_auto, S.flux_auto, 'b.') plt.xlabel('fluxerr_auto') plt.ylabel('flux_auto') plt.title('SE2') ps.savefig() plt.clf() X = S.flux_auto / S.fluxerr_auto X = X[np.isfinite(X)] mn, mx = [np.percentile(X, p) for p in [1, 99]] print('min,max SNR', mn, mx) mx = 50. plt.hist(X, 50, range=(mn, mx)) plt.xlabel('flux_auto SNR') plt.title('SE2') ps.savefig() plt.clf() plt.loglog(M.chi2_psf, M.chi2_model, 'b.') plt.xlabel('chi2_psf') plt.ylabel('chi2_model') ps.savefig() plt.clf() plt.semilogy(M.class_star, M.chi2_psf / M.chi2_model, 'b.') plt.xlabel('class_star') plt.ylabel('chi2_psf / chi2_model') ps.savefig()