def main(): SAMPLE_NUM = 10 degree = 9 x, y = sin_wgn_sample(SAMPLE_NUM) fig = pylab.figure(1) pylab.grid(True) pylab.xlabel('x') pylab.ylabel('y') pylab.axis([-0.1,1.1,-1.5,1.5]) # sin(x) + noise # markeredgewidth mew # markeredgecolor mec # markerfacecolor mfc # markersize ms # linewidth lw # linestyle ls pylab.plot(x, y,'bo',mew=2,mec='b',mfc='none',ms=8) # sin(x) x2 = linspace(0, 1, 1000) pylab.plot(x2,sin(2*x2*pi),'#00FF00',lw=2,label='$y = \sin(x)$') # polynomial fit reg = exp(-18) w = curve_poly_fit(x, y, degree,reg) #w = polyfit(x, y, 3) po = poly1d(w) xx = linspace(0, 1, 1000) pylab.plot(xx, po(xx),'-r',label='$M = 9, \ln\lambda = -18$',lw=2) pylab.legend() pylab.show() fig.savefig("poly_fit9_10_reg.pdf")
def InitializePlot(self, goal_config): self.fig = pl.figure() lower_limits, upper_limits = self.boundary_limits pl.xlim([lower_limits[0], upper_limits[0]]) pl.ylim([lower_limits[1], upper_limits[1]]) pl.plot(goal_config[0], goal_config[1], 'gx') # Show all obstacles in environment for b in self.robot.GetEnv().GetBodies(): if b.GetName() == self.robot.GetName(): continue bb = b.ComputeAABB() pl.plot([bb.pos()[0] - bb.extents()[0], bb.pos()[0] + bb.extents()[0], bb.pos()[0] + bb.extents()[0], bb.pos()[0] - bb.extents()[0], bb.pos()[0] - bb.extents()[0]], [bb.pos()[1] - bb.extents()[1], bb.pos()[1] - bb.extents()[1], bb.pos()[1] + bb.extents()[1], bb.pos()[1] + bb.extents()[1], bb.pos()[1] - bb.extents()[1]], 'r') pl.ion() pl.show()
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 simulationWithoutDrugNick(numViruses, maxPop, maxBirthProb, clearProb, numTrials): """ Run the simulation and plot the graph for problem 3 (no drugs are used, viruses do not have any drug resistance). For each of numTrials trial, instantiates a patient, runs a simulation for 300 timesteps, and plots the average virus population size as a function of time. numViruses: number of SimpleVirus to create for patient (an integer) maxPop: maximum virus population for patient (an integer) maxBirthProb: Maximum reproduction probability (a float between 0-1) clearProb: Maximum clearance probability (a float between 0-1) numTrials: number of simulation runs to execute (an integer) """ #Instantiate the viruses first, the patient second viruses= [ SimpleVirus(maxBirthProb, clearProb) for i in range(numViruses) ] patient = Patient(viruses, maxPop) #Execute the patient.update method 300 times for 100 trials steps = 300 countList = [0 for i in range(300)] for trial in range(numTrials): for timeStep in range(steps): countList[timeStep] += patient.update() avgList = [ countList[i]/float(numTrials) for i in range(steps) ] #Plot a diagram with xAxis=timeSteps, yAxis=average virus population xAxis = [ x for x in range(steps) ] pylab.figure(2) pylab.plot(xAxis, avgList, 'ro', label='Simple Virus') pylab.xlabel('Number of elapsed time steps') pylab.ylabel('Average size of the virus population') pylab.title('Virus growth in a patient without the aid of any drag') pylab.legend() pylab.show()
def simulationWithDrug(): """ Runs simulations and plots graphs for problem 4. Instantiates a patient, runs a simulation for 150 timesteps, adds guttagonol, and runs the simulation for an additional 150 timesteps. total virus population vs. time and guttagonol-resistant virus population vs. time are plotted """ maxBirthProb = .1 clearProb = .05 resistances = {'guttagonal': False} mutProb = .005 total = [100] g = [0] badVirus = ResistantVirus(maxBirthProb, clearProb, resistances, mutProb) viruses = [badVirus]*total[0] maxPop = 1000 Bob = Patient(viruses, maxPop) for i in range(150): Bob.update() gVirus = 0 for v in Bob.viruses: if v.isResistantTo('guttagonal'): gVirus += 1 #print "g = ", gVirus #print "t = ", len(Bob.viruses) #print g += [gVirus] total += [len(Bob.viruses)] Bob.addPrescription('guttagonal') for i in range(150): Bob.update() gVirus = 0 for v in Bob.viruses: if v.isResistantTo('guttagonal'): gVirus += 1 g += [gVirus] total += [len(Bob.viruses)] pylab.title("Number of Viruses with Different Resistances to Guttagonal") pylab.xlabel("Number of Timesteps") pylab.ylabel("Number of Viruses") pylab.plot(g, '-r', label = 'Resistant') pylab.plot(total, '-b', label = 'Total') pylab.legend(loc = 'lower right') pylab.show()
def embed_two_dimensions(data, vectorizer, size=10, n_components=5, colormap='YlOrRd'): if hasattr(data, '__iter__'): iterable = data else: raise Exception('ERROR: Input must be iterable') import itertools iterable_1, iterable_2 = itertools.tee(iterable) # get labels labels = [] for graph in iterable_2: label = graph.graph.get('id', None) if label: labels.append(label) # transform iterable into sparse vectors data_matrix = vectorizer.transform(iterable_1) # embed high dimensional sparse vectors in 2D from sklearn import metrics distance_matrix = metrics.pairwise.pairwise_distances(data_matrix) from sklearn.manifold import MDS feature_map = MDS(n_components=n_components, dissimilarity='precomputed') explicit_data_matrix = feature_map.fit_transform(distance_matrix) from sklearn.decomposition import TruncatedSVD pca = TruncatedSVD(n_components=2) low_dimension_data_matrix = pca.fit_transform(explicit_data_matrix) plt.figure(figsize=(size, size)) embed_dat_matrix_two_dimensions(low_dimension_data_matrix, labels=labels, density_colormap=colormap) plt.show()
def Xtest2(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This passes. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_sampledata_path() + '/clt.nc' a=cdms.open(filename) data=a('clt')[0,...] print data.mask #verify this data is not masked GRID= data.getGrid() # input = output grid, passes test_data=data.regrid(GRID,regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def Xtest3(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This is similar to the original test but we construct our own uniform grid. This should passes. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_sampledata_path() + '/clt.nc' a=cdms.open(filename) data=a('clt')[0,...] print data.mask #verify this data is not masked GRID = cdms.grid.createUniformGrid(-90.0, 23, 8.0, -180.0, 36, 10.0, order="yx", mask=None) test_data=data.regrid(GRID,regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def plotLists(xList, xLabel=None, eListTitle=None, eList=None, eLabel=None, fListTitle=None, fList=None, fLabel=None): if h2o.python_username!='kevin': return import pylab as plt print "xList", xList print "eList", eList print "fList", fList font = {'family' : 'normal', 'weight' : 'normal', 'size' : 26} ### plt.rc('font', **font) plt.rcdefaults() if eList: if eListTitle: plt.title(eListTitle) plt.figure() plt.plot (xList, eList) plt.xlabel(xLabel) plt.ylabel(eLabel) plt.draw() if fList: if fListTitle: plt.title(fListTitle) plt.figure() plt.plot (xList, fList) plt.xlabel(xLabel) plt.ylabel(fLabel) plt.draw() if eList or fList: plt.show()
def plotear(xi,yi,zi): # mask inner circle interior1 = sqrt(((xi+1.5)**2) + (yi**2)) < 1.0 interior2 = sqrt(((xi-1.5)**2) + (yi**2)) < 1.0 zi[interior1] = ma.masked zi[interior2] = ma.masked p.figure(figsize=(16,10)) pyplot.jet() max=2.8 min=0.4 steps = 50 levels=list() labels=list() for i in range(0,steps): levels.append(int((max-min)/steps*100*i)*0.01+min) for i in range(0,steps/2): labels.append(levels[2*i]) CSF = p.contourf(xi,yi,zi,levels,norm=colors.LogNorm()) CS = p.contour(xi,yi,zi,levels, format='%.3f', labelsize='18') p.clabel(CS,labels,inline=1,fontsize=9) p.title('electrostatic potential of two spherical colloids, R=lambda/3',fontsize=24) p.xlabel('z-coordinate (3*lambda)',fontsize=18) p.ylabel('radial coordinate r (3*lambda)',fontsize=18) # add a vertical bar with the color values cbar = p.colorbar(CSF,ticks=labels,format='%.3f') cbar.ax.set_ylabel('potential (reduced units)',fontsize=18) cbar.add_lines(CS) p.show()
def posterior(kpl, pk, err, pkfold=None, errfold=None): k0 = n.abs(kpl).argmin() kpl = kpl[k0:] if pkfold is None: print 'Folding for posterior' pkfold = pk[k0:].copy() errfold = err[k0:].copy() pkpos,errpos = pk[k0+1:].copy(), err[k0+1:].copy() pkneg,errneg = pk[k0-1:0:-1].copy(), err[k0-1:0:-1].copy() pkfold[1:] = (pkpos/errpos**2 + pkneg/errneg**2) / (1./errpos**2 + 1./errneg**2) errfold[1:] = n.sqrt(1./(1./errpos**2 + 1./errneg**2)) #ind = n.logical_and(kpl>.2, kpl<.5) ind = n.logical_and(kpl>.15, kpl<.5) #ind = n.logical_and(kpl>.12, kpl<.5) #print kpl,pk.real,err kpl = kpl[ind] pk= kpl**3 * pkfold[ind]/(2*n.pi**2) err = kpl**3 * errfold[ind]/(2*n.pi**2) s = n.logspace(5.,6.5,100) data = [] for ss in s: data.append(n.exp(-.5*n.sum((pk.real - ss)**2 / err**2))) # print data[-1] data = n.array(data) #print data #print s #data/=n.sum(data) data /= n.max(data) p.figure(5) p.plot(s, data) p.plot(s, n.exp(-.5)*n.ones_like(s)) p.plot(s, n.exp(-.5*2**2)*n.ones_like(s)) p.show()
def main(): pylab.ion(); ind = [0,]; ldft = [0,]; lfft = [0,]; lpfft = [0,] # plot a graph Dft vs Fft, lists just support size until 2**9 for i in range(1, 9, 1): t_before = time.clock(); dsprocessing.dspDft(rand(2**i).tolist()); dt = time.clock() - t_before; ldft.append(dt); print ("dft ", 2**i, dt); #pylab.plot([2**i,], [time.clock()-t_before,]); t_before = time.clock(); dsprocessing.dspFft(rand(2**i).tolist()); dt = time.clock() - t_before; print ("fft ", 2**i, dt); lfft.append(dt); #pylab.plot([2**i,], [time.clock()-t_before,]); ind.append(2**i); # python fft just to compare t_before = time.clock(); pylab.fft(rand(2**i).tolist()); dt = time.clock() - t_before; lpfft.append(dt); pylab.plot(ind, ldft); pylab.plot(ind, lfft); pylab.plot(ind, lpfft); pylab.show(); return [ind, ldft, lfft, lpfft];
def blobFinder(img): shape = img.shape[0:2] scales = numpy.arange(0.5,7.0,0.5) nScales = len(scales) bank = numpy.zeros(shape+(nScales,)) for si,scale in enumerate(scales): print scale log = numpy.abs(scale*vigra.filters.laplacianOfGaussian(img,float(scale))) log = numpy.sum(log,axis=2) bank[:,:,si]=log #f = pylab.figure() #for n, iterImg in enumerate([log,img]): # f.add_subplot(1,2, n) # this line outputs images side-by-side # pylab.imshow(numpy.swapaxes(norm01(iterImg),0,1)) #pylab.show() maxScale = numpy.argmax(bank,axis=2) print "mima",maxScale.min(),maxScale.max() print maxScale f = pylab.figure() for n, iterImg in enumerate([maxScale]): f.add_subplot(1,1, n) # this line outputs images side-by-side pylab.imshow(numpy.swapaxes(norm01(iterImg),0,1)) pylab.show()
def param_set_averages_plot(results): averages_ocr = [ a[1] for a in sorted( param_set_averages(results, metric='ocr').items(), key=lambda x: int(x[0].split('-')[1])) ] averages_q = [ a[1] for a in sorted( param_set_averages(results, metric='q').items(), key=lambda x: int(x[0].split('-')[1])) ] averages_mse = [ a[1] for a in sorted( param_set_averages(results, metric='mse').items(), key=lambda x: int(x[0].split('-')[1])) ] fig = plt.figure(figsize=(6, 4)) # plt.tight_layout() plt.plot(averages_ocr, label='OCR', linewidth=2.0) plt.plot(averages_q, label='Q', linewidth=2.0) plt.plot(averages_mse, label='MSE', linewidth=2.0) plt.ylim([0, 1]) plt.xlabel(u'Paslėptų neuronų skaičius') plt.ylabel(u'Vidurinė Q įverčio pokyčio reikšmė') plt.grid(True) plt.tight_layout() plt.legend(loc='lower right') plt.show()
def generateKineticsModel(reaction, tunneling='', plot=False): logging.info('Calculating rate coefficient for {0}...'.format(reaction)) if len(reaction.reactants) == 1: kunits = 's^-1' elif len(reaction.reactants) == 2: kunits = 'm^3/(mol*s)' elif len(reaction.reactants) == 3: kunits = 'm^6/(mol^2*s)' else: kunits = '' Tlist = 1000.0/numpy.arange(0.4, 3.35, 0.05) klist = reaction.calculateTSTRateCoefficients(Tlist, tunneling) arrhenius = Arrhenius().fitToData(Tlist, klist, kunits) klist2 = arrhenius.getRateCoefficients(Tlist) reaction.kinetics = arrhenius if plot: logging.info('Plotting kinetics model for {0}...'.format(reaction)) import pylab pylab.semilogy(1000.0 / Tlist, klist * reaction.degeneracy, 'ok') pylab.semilogy(1000.0 / Tlist, klist2 * reaction.degeneracy, '-k') pylab.xlabel('1000 / Temperature (1000/K)') pylab.ylabel('Rate coefficient (SI units)') pylab.show()
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 drawPr(tp,fp,tot,show=True): """ draw the precision recall curve """ det=numpy.array(sorted(tp+fp)) atp=numpy.array(tp) afp=numpy.array(fp) #pylab.figure() #pylab.clf() rc=numpy.zeros(len(det)) pr=numpy.zeros(len(det)) #prc=0 #ppr=1 for i,p in enumerate(det): pr[i]=float(numpy.sum(atp>=p))/numpy.sum(det>=p) rc[i]=float(numpy.sum(atp>=p))/tot #print pr,rc,p ap=0 for c in numpy.linspace(0,1,num=11): if len(pr[rc>=c])>0: p=numpy.max(pr[rc>=c]) else: p=0 ap=ap+p/11 if show: pylab.plot(rc,pr,'-g') pylab.title("AP=%.3f"%(ap)) pylab.xlabel("Recall") pylab.ylabel("Precision") pylab.grid() pylab.show() pylab.draw() return rc,pr,ap
def createPlot(dataY, dataX, ticksX, annotations, axisY, axisX, dostep, doannotate): if not ticksX: ticksX = dataX if dostep: py.step(dataX, dataY, where='post', linestyle='-', label=axisY) # where=post steps after point else: py.plot(dataX, dataY, marker='o', ms=5.0, linestyle='-', label=axisY) if annotations and doannotate: for note, x, y in zip(annotations, dataX, dataY): py.annotate(note, (x, y), xytext=(2,2), xycoords='data', textcoords='offset points') py.xticks(np.arange(1, len(dataX)+1), ticksX, horizontalalignment='left', rotation=30) leg = py.legend() leg.draggable() py.xlabel(axisX) py.ylabel('time (s)') # Set X axis tick labels as rungs #print zip(dataX, dataY) py.draw() py.show() return
def yfromx(self, newtimeaxis, doplot=False, debug=False): if debug: print('fastresampler: yfromx called with following parameters') print(' padvalue:, ', self.padvalue) print(' initstep, hiresstep:', self.initstep, self.hiresstep) print(' initial axis limits:', self.initstart, self.initend) print(' hires axis limits:', self.hiresstart, self.hiresend) print(' requested axis limits:', newtimeaxis[0], newtimeaxis[-1]) outindices = ((newtimeaxis - self.hiresstart) // self.hiresstep).astype(int) if debug: print('len(self.hires_y):', len(self.hires_y)) try: out_y = self.hires_y[outindices] except IndexError: print('') print('indexing out of bounds in fastresampler') print(' padvalue:, ', self.padvalue) print(' initstep, hiresstep:', self.initstep, self.hiresstep) print(' initial axis limits:', self.initstart, self.initend) print(' hires axis limits:', self.hiresstart, self.hiresend) print(' requested axis limits:', newtimeaxis[0], newtimeaxis[-1]) sys.exit() if doplot: fig = pl.figure() ax = fig.add_subplot(111) ax.set_title('fastresampler timecourses') pl.plot(self.hires_x, self.hires_y, newtimeaxis, out_y) pl.legend(('hires', 'output')) pl.show() return out_y
def drawPrfastscore(tp,fp,scr,tot,show=True): tp=numpy.cumsum(tp) fp=numpy.cumsum(fp) rec=tp/tot prec=tp/(fp+tp) #dif=numpy.abs(prec[1:]-rec[1:]) dif=numpy.abs(prec[::-1]-rec[::-1]) pos=dif.argmin() pos=len(dif)-pos-1 ap=0 for t in numpy.linspace(0,1,11): pr=prec[rec>=t] if pr.size==0: pr=0 p=numpy.max(pr); ap=ap+p/11; if show: pylab.plot(rec,prec,'-g') pylab.title("AP=%.3f EPRthr=%.3f"%(ap,scr[pos])) pylab.xlabel("Recall") pylab.ylabel("Precision") pylab.grid() pylab.show() pylab.draw() return rec,prec,scr,ap,scr[pos]
def plotEventFlop(library, num, eventNames, sizes, times, events, filename = None): from pylab import legend, plot, savefig, semilogy, show, title, xlabel, ylabel import numpy as np arches = sizes.keys() bs = events[arches[0]].keys()[0] data = [] names = [] for event, color in zip(eventNames, ['b', 'g', 'r', 'y']): for arch, style in zip(arches, ['-', ':']): if event in events[arch][bs]: names.append(arch+'-'+str(bs)+' '+event) data.append(sizes[arch][bs]) data.append(1e-3*np.array(events[arch][bs][event])[:,1]) data.append(color+style) else: print 'Could not find %s in %s-%d events' % (event, arch, bs) semilogy(*data) title('Performance on '+library+' Example '+str(num)) xlabel('Number of Dof') ylabel('Computation Rate (GF/s)') legend(names, 'upper left', shadow = True) if filename is None: show() else: savefig(filename) return
def getOptCandGamma(cv_train, cv_label): print "Finding optimal C and gamma for SVM with RBF Kernel" C_range = 10.0 ** np.arange(-2, 9) gamma_range = 10.0 ** np.arange(-5, 4) param_grid = dict(gamma=gamma_range, C=C_range) cv = StratifiedKFold(y=cv_label, n_folds=40) # Use the svm.SVC() as the cost function to evaluate parameter choices # NOTE: Perhaps we should run computations in parallel if needed. Does it # do that already within the class? grid = GridSearchCV(svm.SVC(), param_grid=param_grid, cv=cv) grid.fit(cv_train, cv_label) score_dict = grid.grid_scores_ scores = [x[1] for x in score_dict] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) pl.figure(figsize=(8,6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show() print "The best classifier is: ", grid.best_estimator_
def plot(self, GRID=10, ax=None, **kwargs): NEWAXIS = False if ax == None: from pylab import figure, show fig = figure() ax = fig.add_subplot(111) NEWAXIS = True if "period" in kwargs: period = kwargs.pop("period") else: period = tl.PI2 phase = tl.PI2*np.arange(GRID)/float(GRID-1) phase += phase[1]/2. UV = np.asarray([ [self([phase[i], phase[j]]) for i in xrange(GRID)] # Spalten for j in xrange(GRID)]) # Zeilen X, Y = np.meshgrid(phase, phase) U, V = UV[:, :, 0], UV[:, :, 1] Q = ax.quiver(period*X/tl.PI2, period*Y/tl.PI2, U, V, units='width') for fp in self.fixedPoints: fp.plot(axis=ax, period=period) if NEWAXIS: ax.set_xlim(0., period) ax.set_ylim(0., period) fig.tight_layout() show() return Q
def plotAllWarmJumps(): jumpAddrs = np.array(getAllWarmJumpsAddr()).reshape((8, 18)) figure() pcolor(jumpAddrs) for (x, y), v in np.ndenumerate(jumpAddrs): text(y + 0.125, x + 0.5, "0x%03x" % v) show()
def cmap_plot(cmdLine): pylab.figure(figsize=[5,10]) a=outer(ones(10),arange(0,1,0.01)) subplots_adjust(top=0.99,bottom=0.00,left=0.01,right=0.8) maps=[m for m in cm.datad if not m.endswith("_r")] maps.sort() l=len(maps)+1 for i, m in enumerate(maps): print m subplot(l,1,i+1) pylab.setp(pylab.gca(),xticklabels=[],xticks=[],yticklabels=[],yticks=[]) imshow(a,aspect='auto',cmap=get_cmap(m),origin="lower") pylab.text(100.85,0.5,m,fontsize=10) # render plot if cmdLine: pylab.show(block=True) else: pylab.ion() pylab.plot([]) pylab.ioff() status = 1 return status
def evaluate_result(data, target, result): assert(data.shape[0] == target.shape[0]) assert(target.shape[0] == result.shape[0]) correct = np.where( result == target ) miss = np.where( result != target ) class_rate = float(correct[0].shape[0]) / target.shape[0] print "Correct classification rate:", class_rate #get the 3s mask = np.where(target == wanted[0]) data_3_correct = data[np.intersect1d(mask[0],correct[0])] data_3_miss = data[np.intersect1d(mask[0],miss[0])] #get the 8s mask = np.where(target == wanted[1]) data_8_correct = data[np.intersect1d(mask[0],correct[0])] data_8_miss = data[np.intersect1d(mask[0],miss[0])] #plot plot.title("Scatter") plot.xlabel("x_0") plot.ylabel("x_1") size = 20 plot.scatter(data_3_correct[:,0], data_3_correct[:,1], marker = "x", c = "r", s = size ) plot.scatter( data_3_miss[:,0], data_3_miss[:,1], marker = "x", c = "b", s = size ) plot.scatter(data_8_correct[:,0], data_8_correct[:,1], marker = "o", c = "r", s = size ) plot.scatter( data_8_miss[:,0], data_8_miss[:,1], marker = "o", c = "b", s = size ) plot.show()
def plot_heatingrate(data_dict, filename, do_show=True): pl.figure(201) color_list = ['b','r','g','k','y','r','g','b','k','y','r',] fmtlist = ['s','d','o','s','d','o','s','d','o','s','d','o'] result_dict = {} for key in data_dict.keys(): x = data_dict[key][0] y = data_dict[key][1][:,0] y_err = data_dict[key][1][:,1] p0 = np.polyfit(x,y,1) fit = LinFit(np.array([x,y,y_err]).transpose(), show_graph=False) p1 = [0,0] p1[0] = fit.param_dict[0]['Slope'][0] p1[1] = fit.param_dict[0]['Offset'][0] print fit x0 = np.linspace(0,max(x)) cstr = color_list.pop(0) fstr = fmtlist.pop(0) lstr = key + " heating: {0:.2f} ph/ms".format((p1[0]*1e3)) pl.errorbar(x/1e3,y,y_err,fmt=fstr + cstr,label=lstr) pl.plot(x0/1e3,np.polyval(p0,x0),cstr) pl.plot(x0/1e3,np.polyval(p1,x0),cstr) result_dict[key] = 1e3*np.array(fit.param_dict[0]['Slope']) pl.xlabel('Heating time (ms)') pl.ylabel('nbar') if do_show: pl.legend() pl.show() if filename != None: pl.savefig(filename) return result_dict
def _test_graph(): i = 10000 x = np.linspace(0,3.7*pi,i) y = (0.3*np.sin(x) + np.sin(1.3 * x) + 0.9 * np.sin(4.2 * x) + 0.06 * np.random.randn(i)) y *= -1 x = range(i) _max, _min = peakdetect(y,x,750, 0.30) xm = [p[0] for p in _max] ym = [p[1] for p in _max] xn = [p[0] for p in _min] yn = [p[1] for p in _min] plot = pylab.plot(x,y) pylab.hold(True) pylab.plot(xm, ym, 'r+') pylab.plot(xn, yn, 'g+') _max, _min = peak_det_bad.peakdetect(y, 0.7, x) xm = [p[0] for p in _max] ym = [p[1] for p in _max] xn = [p[0] for p in _min] yn = [p[1] for p in _min] pylab.plot(xm, ym, 'y*') pylab.plot(xn, yn, 'k*') pylab.show()
def qinit(x,y): """ Gaussian hump: """ from numpy import where from pylab import plot,show nxpoints = 2001 nypoints = 2001 #import sympy #def sech(x): #return sympy.cosh(x)**(-1) zmin = 1000.0 zmin1 = 1000 g = 9.81 A = 10.0 # wave height k = sqrt(3*A/(4*zmin**3)) z = zeros(shape=shape(y)) u = zeros(shape=shape(y)) hu = zeros(shape=shape(y)) y0 = 180000 c = sqrt(g*(A+zmin)) rho = 1.0e3 for i in range(nxpoints): for j in range(nypoints): z[i,j] = A*cosh(k*(y[i,j]-y0))**(-2) u[i,j] = -sqrt(g/zmin)*z[i,j] hu[i,j] =(z[i,j]+zmin)*u[i,j] plot(y,u,'-') show() #ze = -((y+0e0)**2)/10. #z = where(ze>-400000., 400.e0*exp(ze), 0.) return hu
def test_mask_LUT(self): """ The masked image has a masked ring around 1.5deg with value -10 without mask the pixels should be at -10 ; with mask they are at 0 """ x1 = self.ai.xrpd_LUT(self.data, 1000) # print self.ai._lut_integrator.lut_checksum x2 = self.ai.xrpd_LUT(self.data, 1000, mask=self.mask) # print self.ai._lut_integrator.lut_checksum x3 = self.ai.xrpd_LUT(self.data, 1000, mask=numpy.zeros(shape=self.mask.shape, dtype="uint8"), dummy= -20.0, delta_dummy=19.5) # print self.ai._lut_integrator.lut_checksum res1 = numpy.interp(1.5, *x1) res2 = numpy.interp(1.5, *x2) res3 = numpy.interp(1.5, *x3) if logger.getEffectiveLevel() == logging.DEBUG: pylab.plot(*x1, label="nomask") pylab.plot(*x2, label="mask") pylab.plot(*x3, label="dummy") pylab.legend() pylab.show() raw_input() self.assertAlmostEqual(res1, -10., 1, msg="Without mask the bad pixels are around -10 (got %.4f)" % res1) self.assertAlmostEqual(res2, 0., 4, msg="With mask the bad pixels are actually at 0 (got %.4f)" % res2) self.assertAlmostEqual(res3, -20., 4, msg="Without mask but dummy=-20 the dummy pixels are actually at -20 (got % .4f)" % res3)
def main(): minutes = 0 seconds = 30 milliseconds = 0 run_time = minutes * 60 * 1000 + seconds * 1000 + milliseconds weight_to_spike = 4. model = sim.IF_curr_exp cell_params = { 'cm': 0.25, # nF 'i_offset': 0.0, 'tau_m': 10.0, 'tau_refrac': 2.0, 'tau_syn_E': 2.5, 'tau_syn_I': 2.5, 'v_reset': -70.0, 'v_rest': -65.0, 'v_thresh': -55.4 } # Available resolutions # 16, 32, 64, 128 mode = ExternalDvsEmulatorDevice.MODE_64 cam_res = int(mode) cam_fps = 90 frames_per_saccade = cam_fps / 3 - 1 polarity = ExternalDvsEmulatorDevice.MERGED_POLARITY output_type = ExternalDvsEmulatorDevice.OUTPUT_TIME history_weight = 1.0 behaviour = VirtualCam.BEHAVE_ATTENTION vcam = VirtualCam("./mnist", behaviour=behaviour, fps=cam_fps, resolution=cam_res, frames_per_saccade=frames_per_saccade) cam_params = { 'mode': mode, 'polarity': polarity, 'threshold': 12, 'adaptive_threshold': False, 'fps': cam_fps, 'inhibition': False, 'output_type': output_type, 'save_spikes': "./spikes_from_cam.pickle", 'history_weight': history_weight, #'device_id': 0, # for an OpenCV webcam device #'device_id': 'path/to/video/file', # to encode pre-recorded video 'device_id': vcam, } if polarity == ExternalDvsEmulatorDevice.MERGED_POLARITY: num_neurons = 2 * (cam_res**2) else: num_neurons = cam_res**2 sim.setup(timestep=1.0, min_delay=1.0, max_delay=10.0) target = sim.Population(num_neurons, model, cell_params) stimulation = sim.Population(num_neurons, DvsEmulatorDevice, cam_params, label="Webcam population") connector = sim.OneToOneConnector(weights=weight_to_spike) projection = sim.Projection(stimulation, target, connector) target.record() sim.run(run_time) spikes = target.getSpikes(compatible_output=True) sim.end() #stimulation._vertex.stop() print("Raster plot of the spikes that Spinnaker echoed back") fig = pylab.figure() spike_times = [spike_time for (neuron_id, spike_time) in spikes] spike_ids = [neuron_id for (neuron_id, spike_time) in spikes] pylab.plot(spike_times, spike_ids, ".", markerfacecolor="None", markeredgecolor="Blue", markersize=3) pylab.show()
def eval_gwas(pv, i_causal, out_fn=None, plot=False): """ """ pv_thresholds = [1e-3, 5e-4, 1e-4, 5e-5, 1e-5, 5e-6, 1e-6, 5e-7, 1e-7, 5e-8, 1e-8] # compute lambda on all p-values? #lambda_gc = estimate_lambda(p_values) lambda_gc = estimate_lambda(pv) n_causal = i_causal.sum() #compute power and Type-1 error power = np.zeros_like(pv_thresholds) t1err = np.zeros_like(pv_thresholds) power_corr = np.zeros_like(pv_thresholds) t1err_corr = np.zeros_like(pv_thresholds) pvcorr = stats.chi2.sf(stats.chi2.isf(pv,1)/lambda_gc,1) for i_t, t in enumerate(pv_thresholds): #compute uncorrected power and T1 i_lower = pv<t power[i_t] = i_causal[i_lower].sum()/(1.0*(n_causal)) t1err[i_t] = (~i_causal[i_lower]).sum()/(1.0*(len(i_causal)-n_causal)) #compute GC corrected Power and T1 i_lower_corr = pvcorr<t power_corr[i_t] = i_causal[i_lower_corr].sum()/(1.0*(n_causal)) t1err_corr[i_t] = (~i_causal[i_lower_corr]).sum()/(1.0*(len(i_causal)-n_causal)) if plot == True: import pylab pylab.figure() pylab.title("lambda_gc=%f" % lambda_gc) pylab.plot(pv_thresholds, power, "-o") pylab.yscale("log") pylab.xscale("log") pylab.xlabel("pv threshold") pylab.ylabel("power") pylab.grid(True) pylab.plot(pv_thresholds, power_corr, "-o") if not out_fn is None: pow_fn = out_fn.replace(".pickle", "_pow.pdf") pylab.savefig(pow_fn) else: pylab.show() pylab.figure() pylab.title("lambda_gc=%f" % lambda_gc) pylab.plot(pv_thresholds, t1err, "-o", label="t1err") pylab.plot(pv_thresholds, t1err_corr, "-o", label="t1err_gc") pylab.yscale("log") pylab.xscale("log") pylab.xlabel("pv threshold") pylab.ylabel("t1err") pylab.grid(True) pylab.plot(pv_thresholds, pv_thresholds, "-", label="thres") pylab.legend(loc="upper left") if not out_fn is None: t1err_fn = out_fn.replace(".pickle", "_t1err.pdf") pylab.savefig(t1err_fn) else: pylab.show() # plot auROC if out_fn is None: roc_fn = None else: roc_fn = out_fn.replace(".pickle", "_roc.pdf") plot_roc(i_causal, -pv, label='lambda_gc=%0.4f' % (lambda_gc), out_fn=roc_fn) # plot auPRC if out_fn is None: prc_fn = None else: prc_fn = out_fn.replace(".pickle", "_prc.pdf") plot_prc(i_causal, -pv, label='lambda_gc=%0.4f' % (lambda_gc), out_fn=prc_fn) # wrap up metrics res = {} res["lambda"] = lambda_gc res["pv_thresholds"] = pv_thresholds res["power"] = power res["power_corr"] = power_corr res["t1err"] = t1err res["t1err_corr"] = t1err_corr return res
def simulationTwoDrugsVirusPopulations(): """ Run simulations and plot graphs examining the relationship between administration of multiple drugs and patient outcome. Plots of total and drug-resistant viruses vs. time are made for a simulation with a 300 time step delay between administering the 2 drugs and a simulations for which drugs are administered simultaneously. """ #TODO virusList = [] popList = [] againstList1 = [] againstList2 = [] againstListall = [] SimNum = 30 for i in range(100): virusList.append( ResistantVirus(0.1, 0.05, { 'guttagonol': False, 'grimpex': False, 'ac': False }, 0.005)) for i in range(300): popList.append(0) againstList1.append(0) againstList2.append(0) againstListall.append(0) for i in range(SimNum): patient = Patient(virusList, 1000) for i in range(150): virusNum = patient.update() against1 = patient.getResistPop(['guttagonol']) against2 = patient.getResistPop(['grimpex']) againstall = patient.getResistPop(['guttagonol', 'grimpex']) popList[i] += virusNum againstList1[i] += against1 againstList2[i] += against2 againstListall[i] += againstall patient.addPrescription('guttagonol') patient.addPrescription('grimpex') patient.addPrescription('ac') for i in range(150, 300): virusNum = patient.update() against1 = patient.getResistPop(['guttagonol']) against2 = patient.getResistPop(['grimpex']) againstall = patient.getResistPop(['guttagonol', 'grimpex']) popList[i] += virusNum againstList1[i] += against1 againstList2[i] += against2 againstListall[i] += againstall for i in range(300): popList[i] /= float(SimNum) againstList1[i] /= float(SimNum) againstList2[i] /= float(SimNum) againstListall[i] /= float(SimNum) pylab.plot(popList, 'r') pylab.plot(againstList1, 'b') pylab.plot(againstList2, 'g') pylab.plot(againstListall, 'y') pylab.show()
influence = 0 # Run algo with settings from above result = thresholding_algo(y, lag=lag, threshold=threshold, influence=influence) # Plot result pylab.subplot(211) pylab.plot(np.arange(1, len(y) + 1), y) pylab.plot(np.arange(1, len(y) + 1), result["avgFilter"], color="cyan", lw=2) pylab.plot(np.arange(1, len(y) + 1), result["avgFilter"] + threshold * result["stdFilter"], color="green", lw=2) pylab.plot(np.arange(1, len(y) + 1), result["avgFilter"] - threshold * result["stdFilter"], color="green", lw=2) pylab.subplot(212) pylab.step(np.arange(1, len(y) + 1), result["signals"], color="red", lw=2) pylab.ylim(-1.5, 1.5) pylab.show()
llpv = epics.PV(BYTES2STR('15IDC:cyberAmp2k0ScaLL.VAL')) llvpRBK = epics.PV(BYTES2STR('15IDC:cyberAmp2k0ScaLLRbv.VAL')) ulpv = epics.PV(BYTES2STR('15IDC:cyberAmp2k0ScaUL.VAL')) IDC15_scaler_start = epics.PV(BYTES2STR('15IDC:scaler1.CNT')) IDC15_scaler_mode = epics.PV(BYTES2STR('15IDC:scaler1.CONT')) IDC15_scaler_count_time = epics.PV(BYTES2STR('15IDC:scaler1.TP')) IDC15_scaler_mode.put(0) # for one shot yap_pv = epics.PV(BYTES2STR('15IDC:scaler1.S8')) llpv_ip = llpv.value ulpv_ip = ulpv.value data = [] positions = pl.linspace(start, stop, steps) i = 0 for pos in positions: llpv.put(str(pos), wait=True) llpv.put(str(pos) + win, wait=True) IDC15_scaler_start.put(1, wait=True) yap_counts = yap_pv.value data.append([i, float(llRBK.value), yap_counts]) print(i, llpv.value, yap_counts) i += 1 data = pl.array(data) pl.figure() pl.plot(data[:, 1], data[:, 2], 'r.-') pl.show() llpv.put(llpv_ip, wait=True) ulpv.put(ulpv_ip, wait=True) IDC15_scaler_mode.put(1) # for autocount back again
skip_footer=num_lines - (nsamples + 3)) f.close() xs = samples[:, 2] ys = samples[:, 1] ax.plot(xs, ys, 'ro', markersize=10) ax.plot(xs[0], ys[0], 'bo', markersize=20) xpairs = [] ypairs = [] data = np.genfromtxt("list_segments.txt") for I in range(len(data[:, 1])): xends = [data[I, 0], data[I, 2]] yends = [data[I, 1], data[I, 3]] xpairs.append(xends) ypairs.append(yends) segs = list(zip(zip(data[:, 1], data[:, 0]), zip(data[:, 3], data[:, 2]))) line_segments = LineCollection(segs, linewidths=1) ax.add_collection(line_segments) if (output == "file"): string = filename string2 = string.split(sep=".")[0] plt.savefig(string2 + ".png") if (output == "screen"): plt.show()
def Cluster(): #=========Load Data========= InputFileName = "Aggregation" OutputFileName = InputFileName + "_out" suffix = ".txt" Fin = open(InputFileName + suffix, "r") Fout = open(OutputFileName + suffix, "w") points = [] for line in Fin.readlines(): data = line.split() if len(data) == 3: a = float(data[0]) b = float(data[1]) points.append((a, b)) #=========Calculating========= #-----choose dc----- dc_percent = 0.02 dis = [] distance = {} dc = ChooseDc(dc_percent, points, dis, distance) print("dc:" + str(dc)) #-----cal rho:"Cut off" kernel rho = [0 for i in range(len(points))] for i in range(0, len(points)): for j in range(i + 1, len(points)): dij = getDistance(points[i], points[j]) if dij < dc: rho[i] += 1 rho[j] += 1 rho_list = [(rho[i], i) for i in range(len(rho))] rho_sorted = sorted(rho_list, reverse=1) print("Highest rho:", rho_sorted[0]) maxd = dis[-1] delta = [maxd for i in range(len(points))] nneigh = [-1 for i in range(len(points))] for ii in range(1, len(rho_sorted)): for jj in range(0, ii): id_p1 = rho_sorted[ii][1] #get point1's id id_p2 = rho_sorted[jj][1] #get point2's id if (distance[id_p1, id_p2] < delta[id_p1]): delta[id_p1] = distance[id_p1, id_p2] nneigh[id_p1] = id_p2 #assignment cl = [-1 for i in range(len(points))] colorNum = 0 for ii in range(len(rho_sorted)): id_p = rho_sorted[ii][1] if (cl[id_p] == -1 and delta[id_p] > 4.0): cl[id_p] = colorNum colorNum += 1 else: if (cl[id_p] == -1 and cl[nneigh[id_p] != -1]): cl[id_p] = cl[nneigh[id_p]] print(colorNum) import pylab as pl fig1 = pl.figure(1) pl.subplot(121) drawOriginGraph(pl, points, cl, colorNum) pl.subplot(122) drawDecisionGraph(pl, rho, delta, cl, colorNum) pl.show() for i in range(len(points)): Fout.write(str(i) + "," + str(rho[i]) + "," + str(delta[i]) + "\n") #Assign Cluster Fin.close() Fout.close()
from pylab import plotfile, show import matplotlib.cbook as cbook fname = cbook.get_sample_data('/Users/MacBook/Downloads/Book_code/Chapter4/amzn.csv', asfileobj=False) plotfile(fname, (0,1,5), plotfuncs={5:'bar'}) show()
N = npy.linspace(0, Nmax, 20) * RPM Tb = Motor.Tb(0, N) Pb = Motor.Pb(0, N) eta = Motor.Efficiency(N=N) N = N / RPM Tb = Tb / (IN * OZF) Pb = Pb / Motor.PowerUnit pyl.figure(1) pyl.subplot(131) pyl.plot(N, Tb) pyl.xlabel('RPM') pyl.ylabel('Tb (IN*OZF)') pyl.subplot(132) pyl.plot(N, Pb) pyl.xlabel('RPM') pyl.ylabel('Pb (' + Motor.PowerUnitName + ')') pyl.ylim((0, max(Pb) * 1.1)) pyl.subplot(133) pyl.plot(N, eta) pyl.xlabel('RPM') pyl.ylabel('Efficiency') pyl.ylim((0, max(eta) * 1.1)) pyl.show()
def merge_results(results_dir, fn_filter_list, mindist): """ visualize gwas results based on results file names """ files = [fn for fn in os.listdir(results_dir) if fn.endswith("pickle")] import pylab pylab.figure() for fn_idx, fn_filter in enumerate(fn_filter_list): method_files = [fn for fn in files if fn.find(fn_filter) != -1] p_values = [] p_values_lin = [] i_causal = [] for method_fn in method_files: tmp_fn = results_dir + "/" + method_fn print tmp_fn dat = load(tmp_fn) pv_m, i_causal_m = cut_snps_close_to_causals(dat["p_values_uncut"], dat["pos"], dat["causal_idx"], mindist=mindist) pv_lin_m, i_causal_m2 = cut_snps_close_to_causals(dat["p_values_lin_uncut"], dat["pos"], dat["causal_idx"], mindist=mindist) np.testing.assert_array_equal(i_causal_m, i_causal_m2) p_values.extend(pv_m) p_values_lin.extend(pv_lin_m) i_causal.extend(i_causal_m) p_values = np.array(p_values) p_values_lin = np.array(p_values_lin) i_causal = np.array(i_causal) method_label = fn_filter.replace("_", "")# underscore prefix hides label pylab.subplot(221) plot_prc_noshow(i_causal, -p_values, label=method_label) if fn_idx == 0: plot_prc_noshow(i_causal, -p_values_lin, label="lin") pylab.subplot(222) plot_roc_noshow(i_causal, -p_values, label=method_label) if fn_idx == 0: plot_roc_noshow(i_causal, -p_values_lin, label="lin") pylab.subplot(223) plot_t1err_noshow(p_values, i_causal, label=method_label) if fn_idx == 0: plot_t1err_noshow(p_values_lin, i_causal, label="lin") pylab.subplot(224) plot_power_noshow(p_values, i_causal, label=method_label) if fn_idx == 0: plot_power_noshow(p_values_lin, i_causal, label="lin") print p_values print i_causal pylab.show()
def calc_comp(cluster, DETECT_FILTER, AP_TYPE, spectra, magtype='APER1', verbose=True, type='rand', plot=False): from useful import * from coeio import loaddata, loadfile, params_cl, str2num, loaddict, findmatch1, pause #, prange, plotconfig if type == 'rand': suffix = 'rand' elif type == 'spec': suffix = 'spec' else: suffix = 'all' import os run_name = os.environ[ 'subdir'] + '/' + cluster + '/PHOTOMETRY_' + DETECT_FILTER + '' + AP_TYPE + '/' + cluster + '.' + magtype + '.1.' + spectra + '.' + suffix print run_name, 'run_name' cat = None if cat == None: cat = run_name + '.bpz' else: cat = bpz bpzstr = loadfile(cat) bpzparams = {} i = 0 import string while bpzstr[i][:2] == '##': line = bpzstr[i][2:] if '=' in line: [key, value] = string.split(line, '=') bpzparams[key] = value i = i + 1 bpzcols = [] while bpzstr[i][:2] == '# ': line = bpzstr[i][2:] [col, key] = string.split(line) bpzcols.append(key) i = i + 1 bpzparams['FLUX_COMPARISON'] = run_name + '.flux_comparison' bpzparams['OUTPUT'] = run_name + '.bpz' bpzparams['INPUT'] = run_name + '.cat' bpzparams['PROBS_LITE'] = run_name + '.probs' print bpzparams print run_name, 'run_name' spectra = bpzparams.get('SPECTRA', 'CWWSB_fuv_f_f.list') columns = bpzparams.get('COLUMNS', run_name + '.columns') flux_comparison = bpzparams.get('FLUX_COMPARISON', run_name + '.flux_comparison') interp = str2num(bpzparams.get('INTERP', '2')) output = bpzparams.get('OUTPUT') output_f = get_2Darray(output) #Read the whole file all = get_2Darray(flux_comparison) #Read the whole file print len(all[:, 0]) ncols = len(all[0, :]) nf = (ncols - 5) / 3 ''' need to get the number of filters ''' ''' need to retrieve the flux predicted, flux observed, and flux_error ''' import scipy ft = scipy.array(all[:, 5:5 + nf]) # FLUX (from spectrum for that TYPE) fo = scipy.array(all[:, 5 + nf:5 + 2 * nf]) # FLUX (OBSERVED) efo = scipy.array(all[:, 5 + 2 * nf:5 + 3 * nf]) # FLUX_ERROR (OBSERVED) all_num = len(ft) ''' only use predictions with good ODDS ''' if 1: #good_odds: odds = scipy.array(output_f[:, 5]) redshift = scipy.array(output_f[:, 1]) if 1: mask = (odds == 1) * (redshift > 0.4) ft = ft[mask] fo = fo[mask] efo = efo[mask] odds_num = len(ft) print odds #print ft/fo[0] #print flux_comparison #print columns ''' read in BPZ ODDS parameters and filter catalog ''' print 'Reading filter information from %s' % columns filters = get_str( columns, 0, nrows=nf, ) print filters print filters, nf corrections = [] for i in range((nf)): import scipy print filters[i] ''' get rid of zero flux objects in the training band ''' mask1 = (fo[:, i] != 0) ft_temp_1 = ft[mask1] fo_temp_1 = fo[mask1] efo_temp_1 = efo[mask1] if len(ft_temp_1): ''' get rid of poorly constrained objects in training band ''' mask2 = (efo_temp_1[:, i] / fo_temp_1[:, i] < 0.2) ft_temp = ft_temp_1 #[mask2] fo_temp = fo_temp_1 #[mask2] zero_num = len(ft) print ft_temp[:100, i], fo_temp[:100, i] vec = (ft_temp / fo_temp)[:, i] #print vec #print ft[:,i], fo[:,i] #print ft_temp,fo_temp median = scipy.median(vec) print median title = filters[i] + ' all=' + str(all_num) + ' odds_num=' + str( odds_num) + ' zero_num=' + str(zero_num) #print median, filters[i] if 0: import pylab o = pylab.hist((ft_temp / fo_temp)[:, i], bins=40, range=[0, 3]) print o pylab.suptitle(filters[i] + ' all=' + str(all_num) + ' odds_num=' + str(odds_num) + ' zero_num=' + str(zero_num)) if verbose: pylab.show() #print (ft/fo)[:,0] corrections.append([median, filters[i], len(vec), vec, title]) print filters[i] corrections.sort() correction_dict = {} plot_dict = {} title_dict = {} for c in corrections: print c correction_dict[c[1]] = -2.5 * math.log10(float(c[0])) plot_dict[c[1]] = c[3] title_dict[c[1]] = c[4] print correction_dict if not plot: return correction_dict else: return correction_dict, plot_dict, title_dict
origin='lower') # plot contours p.contour(H, levels, extent=extent, origin='lower') # disable numerical labelling a = p.gca() for tick in a.yaxis.get_major_ticks(): tick.label1On = False tick.label2On = False for tick in a.xaxis.get_major_ticks(): tick.label1On = False tick.label2On = False # label plots if along x or y axis #print i, j,labels[i-1], labels[j-1] if i == 1: p.xlabel(labels[j - 1]) if j == npars: rhAxis = p.twinx() for tick in a.yaxis.get_major_ticks(): tick.label1On = False tick.label2On = False for tick in rhAxis.yaxis.get_major_ticks(): tick.label1On = False tick.label2On = False p.ylabel(labels[i - 1], rotation='horizontal') # if i==1: # rhAxis.set_xlabel(labels[j-1]) p.show()
print(data) som = MiniSom(7,7,4,sigma=1.0,learning_rate=0.5) som.random_weights_init(data) print("Training...") som.train_random(data,50000) # training with 100 iterations print("\n...ready!") bone() pcolor(som.distance_map().T) # distance map as background colorbar() # loading the labels target = genfromtxt('iris.csv', delimiter=',',usecols=(4),dtype=str) t = zeros(len(target),dtype=int) t[target == 'setosa'] = 0 t[target == 'versicolor'] = 1 t[target == 'virginica'] = 2 # use different colors and markers for each label markers = ['o','s','D'] colors = ['r','g','b'] for cnt,xx in enumerate(data): w = som.winner(xx) # getting the winner # palce a marker on the winning position for the sample xx plot(w[0]+.5,w[1]+.5,markers[t[cnt]],markerfacecolor='None', markeredgecolor=colors[t[cnt]],markersize=12,markeredgewidth=2) axis([0,som._weights.shape[0],0,som._weights.shape[1]]) show() # show the figure
def plotClassifiers(datasets, classifiers, names): figure = pl.figure(figsize=(27, 9)) i = 1 h = .02 # step size in the mesh for ds in datasets: # preprocessing datasets, split into train and test X, y = ds X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4) x_min = X[:, 0].min() - .5 x_max = X[:, 0].max() + .5 y_min = X[:, 1].min() - .5 y_max = X[:, 1].max() + .5 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), \ np.arange(y_min, y_max, h)) # plot the dataset cm = pl.cm.RdBu cm_bright = ListedColormap(['#FF0000', '#0000FF']) ax = pl.subplot(len(datasets), len(classifiers) + 1, i) # plot training / testing points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright) ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6) ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) i += 1 # iterate over classifiers for name, clf in zip(names, classifiers): ax = pl.subplot(len(datasets), len(classifiers) + 1, i) clf.fit(X_train, y_train) score = clf.score(X_test, y_test) # plot the decision boundary # assign a color to each point in the mesh # [x_min, x_max] x [y_min, y_max] if hasattr(clf, "decision_function"): Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) else: Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1] # put the result into a color plot Z = Z.reshape(xx.shape) ax.contourf(xx, yy, Z, cmap=cm, alpha=.8) # plot training and testing points ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright) ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6) ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_xticks(()) ax.set_yticks(()) ax.set_title(name) ax.text(xx.max() - .3, \ yy.min() + .3, \ ('%.2f' % score).lstrip('0'), \ size=15, \ horizontalalignment='right') i += 1 figure.subplots_adjust(left=.02, right=.98) pl.show()
def spline(folder_mat, nt, nz, verbose, index_b0=[], graph=0): # get path of the toolbox status, path_sct = commands.getstatusoutput('echo $SCT_DIR') # append path that contains scripts, to be able to load modules sys.path.append(path_sct + '/scripts') import sct_utils as sct sct.printv( '\n\n\n------------------------------------------------------------------------------', verbose) sct.printv('Spline Regularization along T: Smoothing Patient Motion...', verbose) file_mat = [[[] for i in range(nz)] for i in range(nt)] for it in range(nt): for iz in range(nz): file_mat[it][iz] = folder_mat + 'mat.T' + str(it) + '_Z' + str( iz) + '.txt' # Copying the existing Matrices to another folder old_mat = folder_mat + 'old/' if not os.path.exists(old_mat): os.makedirs(old_mat) cmd = 'cp ' + folder_mat + '*.txt ' + old_mat status, output = sct.run(cmd, verbose) sct.printv('\nloading matrices...', verbose) X = [[[] for i in range(nt)] for i in range(nz)] Y = [[[] for i in range(nt)] for i in range(nz)] X_smooth = [[[] for i in range(nt)] for i in range(nz)] Y_smooth = [[[] for i in range(nt)] for i in range(nz)] for iz in range(nz): for it in range(nt): file = open(file_mat[it][iz]) Matrix = np.loadtxt(file) file.close() X[iz][it] = Matrix[0, 3] Y[iz][it] = Matrix[1, 3] # Generate motion splines sct.printv('\nGenerate motion splines...', verbose) T = np.arange(nt) if graph: import pylab as pl for iz in range(nz): # frequency = scipy.fftpack.fftfreq(len(X[iz][:]), d=1) # spectrum = np.abs(scipy.fftpack.fft(X[iz][:], n=None, axis=-1, overwrite_x=False)) # Wn = np.amax(frequency)/10 # N = 5 #Order of the filter # b, a = scipy.signal.iirfilter(N, Wn, rp=None, rs=None, btype='low', analog=False, ftype='butter', output='ba') # X_smooth[iz][:] = scipy.signal.filtfilt(b, a, X[iz][:], axis=-1, padtype=None) spline = scipy.interpolate.UnivariateSpline(T, X[iz][:], w=None, bbox=[None, None], k=3, s=None) X_smooth[iz][:] = spline(T) if graph: pl.plot(T, X_smooth[iz][:], label='spline_smoothing') pl.plot(T, X[iz][:], marker='*', linestyle='None', label='original_val') if len(index_b0) != 0: T_b0 = [T[i_b0] for i_b0 in index_b0] X_b0 = [X[iz][i_b0] for i_b0 in index_b0] pl.plot(T_b0, X_b0, marker='D', linestyle='None', color='k', label='b=0') pl.title('X') pl.grid() pl.legend() pl.show() # frequency = scipy.fftpack.fftfreq(len(Y[iz][:]), d=1) # spectrum = np.abs(scipy.fftpack.fft(Y[iz][:], n=None, axis=-1, overwrite_x=False)) # Wn = np.amax(frequency)/10 # N = 5 #Order of the filter # b, a = scipy.signal.iirfilter(N, Wn, rp=None, rs=None, btype='low', analog=False, ftype='butter', output='ba') # Y_smooth[iz][:] = scipy.signal.filtfilt(b, a, Y[iz][:], axis=-1, padtype=None) spline = scipy.interpolate.UnivariateSpline(T, Y[iz][:], w=None, bbox=[None, None], k=3, s=None) Y_smooth[iz][:] = spline(T) if graph: pl.plot(T, Y_smooth[iz][:], label='spline_smoothing') pl.plot(T, Y[iz][:], marker='*', linestyle='None', label='original_val') if len(index_b0) != 0: T_b0 = [T[i_b0] for i_b0 in index_b0] Y_b0 = [Y[iz][i_b0] for i_b0 in index_b0] pl.plot(T_b0, Y_b0, marker='D', linestyle='None', color='k', label='b=0') pl.title('Y') pl.grid() pl.legend() pl.show() # Storing the final Matrices sct.printv('\nStoring the final Matrices...', verbose) for iz in range(nz): for it in range(nt): file = open(file_mat[it][iz]) Matrix = np.loadtxt(file) file.close() Matrix[0, 3] = X_smooth[iz][it] Matrix[1, 3] = Y_smooth[iz][it] file = open(file_mat[it][iz], 'w') np.savetxt(file_mat[it][iz], Matrix, fmt="%s", delimiter=' ', newline='\n') file.close() sct.printv('\n...Done. Patient motion has been smoothed', verbose) sct.printv( '------------------------------------------------------------------------------\n', verbose)
def plot_uvj_vs_icd(): galaxies=mk_galaxy_struc() #Take out the UDF data galaxies = filter(lambda galaxy: galaxy.field == 1, galaxies) galaxies = filter(lambda galaxy: -0.05 < galaxy.ICD_IH < 0.25, galaxies) #f,(ax1, ax2) = pyl.subplots(1,2,sharex=True, sharey=True,figsize=(6.25,4.5)) F = pyl.figure(1,figsize=(6,3.1)) grid = AxesGrid(F, 111, nrows_ncols=(1,2), axes_pad = 0.1, add_all=True, label_mode = 'L', share_all=True, cbar_location = 'top', cbar_mode = 'single') ax1 = grid[0] ax2 = grid[1] uv =[] vj =[] icd = [] uv_all =[] vj_all =[] icd_all = [] appenduv = uv.append appendvj = vj.append appendicd = icd.append appenduv_all = uv_all.append appendvj_all = vj_all.append appendicd_all = icd_all.append #Build Arrays for galaxy in galaxies: if galaxy.ston_I >30.: appenduv(-2.5*pyl.log10(galaxy.Uflux_rest/galaxy.Vflux_rest)) appendvj(-2.5*pyl.log10(galaxy.Vflux_rest/galaxy.Jflux_rest)) appendicd(galaxy.ICD_IH) appenduv_all(-2.5*pyl.log10(galaxy.Uflux_rest/galaxy.Vflux_rest)) appendvj_all(-2.5*pyl.log10(galaxy.Vflux_rest/galaxy.Jflux_rest)) appendicd_all(galaxy.ICD_IH) #Convert to Numpy Array uv = pyl.asarray(uv) vj = pyl.asarray(vj) icd = pyl.asarray(icd) uv_all = pyl.asarray(uv_all) vj_all = pyl.asarray(vj_all) icd_all = pyl.asarray(icd_all) #Build Plotting Matrices cut = pyl.column_stack((uv,vj,icd)) total = pyl.column_stack((uv_all,vj_all,icd_all)) cut = colsort(cut,3) total = colsort(total,3) #plot! sc1 = ax1.scatter(total[:,1], total[:,0], c=total[:,2], s=50, cmap='spectral') sc2 = ax2.scatter(cut[:,1], cut[:,0], c=cut[:,2], s=50, cmap='spectral') #Add Lines ax1.set_ylim(-0.5,2.5) ax1.set_xlim(-1.5,2.5) limits = ax1.axis() ax1.axis x = [limits[0], 0.69, 1.4,1.4] y = [1.2, 1.2, 1.82, limits[3]] ax1.plot(x,y,lw=2,c='k') ax2.plot(x,y,lw=2,c='k') #Add the color bar and labels and stuff grid.cbar_axes[0].colorbar(sc2) ax = grid.cbar_axes[0] ax.axis["top"].toggle(ticks=True, ticklabels=True, label=True) ax.set_xlabel(r'$\xi[I,H]$') grid.axes_llc.set_xticks([-1,0,1,2]) grid.axes_llc.set_yticks([0,1,2]) ax1.set_ylabel('$U-V_{Rest}$') ax1.set_xlabel('$V-J_{Rest}$') ax2.set_xlabel('$V-J_{Rest}$') pyl.savefig('UVJ_vs_icd.eps',bbox='tight') pyl.show()
def show(self, *args, **kw): self.plot(*args, **kw) pl.show() return self
nbr_samples = 10000 #epsilon = 0.5 state_params = state_params_factory.scrape_params_from_problem( problem, S = 10 ) mcmc_params = mcmc_params_factory.scrape_params_from_problem( problem, type="mh", is_marginal = True, nbr_samples = nbr_samples ) algo_params = { "modeling_approach" : "kernel", "observation_groups" : problem.get_obs_groups(), "state_params" : state_params, "mcmc_params" : mcmc_params, "algorithm" : "model_mcmc" } recorder_params = {} algo, model, state = algo_factory.create_algo_and_state( algo_params ) recorder = recorder_factory.create_recorder( recorder_params ) state.theta = problem.theta_prior_rand() model.set_current_state( state ) model.set_recorder( recorder ) verbose = True print "*************** RUNNING MODEL ABC MCMC ***************" thetas, LL, acceptances,sim_calls = algo( nbr_samples, \ model, \ verbose = verbose, \ verbose_rate = 100 ) print " ACCEPT RATE = %0.3f"%(acceptances.mean()) print "*************** DONE ABC MCMC ***************" print "*************** VIEW RESULTS ***************" problem.view_results( recorder, burnin = nbr_samples/2 ) pp.show() print "*************** DONE VIEW ***************"
def animate_interactive(data, t = [], dimOrder = (0,1,2), fps = 10.0, title = '', xlabel = 'x', ylabel = 'y', fontsize = 24, cBar = 0, sloppy = True, rangeMin = [], rangeMax = [], extent = [-1,1,-1,1], shade = False, azdeg = 0, altdeg = 65, arrowsX = np.array(0), arrowsY = np.array(0), arrowsRes = 10, arrowsPivot = 'mid', arrowsWidth = 0.002, arrowsScale = 5, arrowsColor = 'black', plotArrowsGrid = False, movieFile = '', bitrate = 1800, keepImages = False, figsize = (8, 7), dpi = None, **kwimshow): """ Assemble a 2D animation from a 3D array. call signature:: animate_interactive(data, t = [], dimOrder = (0,1,2), fps = 10.0, title = '', xlabel = 'x', ylabel = 'y', fontsize = 24, cBar = 0, sloppy = True, rangeMin = [], rangeMax = [], extent = [-1,1,-1,1], shade = False, azdeg = 0, altdeg = 65, arrowsX = np.array(0), arrowsY = np.array(0), arrowsRes = 10, arrowsPivot = 'mid', arrowsWidth = 0.002, arrowsScale = 5, arrowsColor = 'black', plotArrowsGrid = False, movieFile = '', bitrate = 1800, keepImages = False, figsize = (8, 7), dpi = None, **kwimshow) Assemble a 2D animation from a 3D array. *data* has to be a 3D array who's time index has the same dimension as *t*. The time index of *data* as well as its x and y indices can be changed via *dimOrder*. Keyword arguments: *dimOrder*: [ (i,j,k) ] Ordering of the dimensions in the data array (t,x,y). *fps*: Frames per second of the animation. *title*: Title of the plot. *xlabel*: Label of the x-axis. *ylabel*: Label of the y-axis. *fontsize*: Font size of the title, x and y label. The size of the x- and y-ticks is 0.7*fontsize and the colorbar ticks's font size is 0.5*fontsize. *cBar*: [ 0 | 1 | 2 ] Determines how the colorbar changes: (0 - no cahnge; 1 - keep extreme values constant; 2 - change extreme values). *sloppy*: [ True | False ] If True the update of the plot lags one frame behind. This speeds up the plotting. *rangeMin*, *rangeMax*: Range of the colortable. *extent*: [ None | scalars (left, right, bottom, top) ] Data limits for the axes. The default assigns zero-based row, column indices to the *x*, *y* centers of the pixels. *shade*: [ False | True ] If True plot a shaded relief plot instead of the usual colormap. Note that with this option cmap has to be specified like cmap = plt.cm.hot instead of cmap = 'hot'. Shading cannot be used with the cBar = 0 option. *azdeg*, *altdeg*: Azimuth and altitude of the light source for the shading. *arrowsX*: Data containing the x-component of the arrows. *arrowsy*: Data containing the y-component of the arrows. *arrowsRes*: Plot every arrowRes arrow. *arrowsPivot*: [ 'tail' | 'middle' | 'tip' ] The part of the arrow that is at the grid point; the arrow rotates about this point. *arrowsWidth*: Width of the arrows. *arrowsScale*: Scaling of the arrows. *arrowsColor*: Color of the arrows. *plotArrowsGrid*: [ False | True ] If 'True' the grid where the arrows are aligned to is shown. *movieFile*: [ None | string ] The movie file where the animation should be saved to. If 'None' no movie file is written. Requires 'mencoder' to be installed. *bitrate*: Bitrate of the movie file. Set to higher value for higher quality. *keepImages*: [ False | True ] If 'True' the images for the movie creation are not deleted. *figsize*: Size of the figure in inches. *dpi*: Dots per inch of the frame. **kwimshow: Remaining arguments are identical to those of pylab.imshow. Refer to that help. """ import pylab as plt import time import os # for making the movie try: import thread # for GUI except: import _thread as thread from matplotlib.colors import LightSource global tStep, sliderTime, pause # plot the current frame def plotFrame(): global tStep, sliderTime if movieFile: ax.set_title(title+r'$\quad$'+r'$t={0}$'.format(t[tStep]), fontsize = fontsize) if shade == False: image.set_data(data[tStep,:,:]) else: image.set_data(rgb[tStep,:,:,:]) if (cBar == 0): pass if (cBar == 1): colorbar.set_clim(vmin=data[tStep,:,:].min(), vmax=data[tStep,:,:].max()) if (cBar == 2): colorbar.set_clim(vmin=data[tStep,:,:].min(), vmax=data[tStep,:,:].max()) colorbar.update_bruteforce(data[tStep,:,:]) if plotArrows: arrows.set_UVC(U = arrowsX[tStep,::arrowsRes,::arrowsRes], V = arrowsY[tStep,::arrowsRes,::arrowsRes]) if (sloppy == False) or (movieFile): manager.canvas.draw() # play the movie def play(threadName): global tStep, sliderTime, pause pause = False while (tStep < nT) & (pause == False): # write the image files for the movie if movieFile: plotFrame() frameName = movieFile + '%06d.png'%tStep fig.savefig(frameName, dpi = dpi) movieFiles.append(frameName) else: start = time.clock() # time slider sliderTime.set_val(t[tStep]) # wait for the next frame (fps) while (time.clock() - start < 1.0/fps): pass # do nothing tStep += 1 tStep -= 1 # call the play function as a separate thread (for GUI) def play_thread(event): global pause if pause == True: try: thread.start_new_thread(play, ("playThread", )) except: print("Error: unable to start play thread") def pausing(event): global pause pause = True def reverse(event): global tStep, sliderTime tStep -= 1 if tStep < 0: tStep = 0 # plot the frame and update the time slider sliderTime.set_val(t[tStep]) def forward(event): global tStep, sliderTime tStep += 1 if tStep > len(t)-1: tStep = len(t)-1 # plot the frame and update the time slider sliderTime.set_val(t[tStep]) pause = True plotArrows = False # check if the data has the right dimensions if (len(data.shape) != 3 and len(data.shape) != 4): print("error: data dimensions are invalid: {0} instead of 3".format(len(data.shape))) return -1 # transpose the data according to dimOrder unOrdered = data data = np.transpose(unOrdered, dimOrder) unOrdered = [] # check if arrows should be plotted if len(arrowsX.shape) == 3: # transpose the data according to dimOrder unOrdered = arrowsX arrowsX = np.transpose(unOrdered, dimOrder) unOrdered = [] if len(arrowsY.shape) == 3: # transpose the data according to dimOrder unOrdered = arrowsY arrowsY = np.transpose(unOrdered, dimOrder) unOrdered = [] # check if the dimensions of the arrow arrays match each other if ((len(arrowsX[:,0,0]) != len(arrowsY[:,0,0])) or (len(arrowsX[0,:,0]) != len(arrowsY[0,:,0])) or (len(arrowsX[0,0,:]) != len(arrowsY[0,0,:]))): print("error: dimensions of arrowX do not match with dimensions of arrowY") return -1 else: plotArrows = True # check if time array has the right length nT = len(t) if (nT != len(data[:,0,0])): print("error: length of time array doesn\'t match length of data array") return -1 if plotArrows: if (nT != len(arrowsX[:,0,0]) or nT != len(arrowsX[:,0,0])): print("error: length of time array doesn\'t match length of arrows array") return -1 # check if fps is positive if (fps < 0.0): print("error: fps is not positive, fps = {0}".format(fps)) return -1 # determine the size of the array nX = len(data[0,:,0]) nY = len(data[0,0,:]) # determine the minimum and maximum values of the data set if not(rangeMin): rangeMin = np.min(data) if not(rangeMax): rangeMax = np.max(data) # setup the plot if movieFile: plt.rc("figure.subplot", bottom=0.15) plt.rc("figure.subplot", top=0.95) plt.rc("figure.subplot", right=0.95) plt.rc("figure.subplot", left=0.15) fig = plt.figure(figsize = figsize) ax = plt.axes([0.1, 0.1, .90, .85]) else: plt.rc("figure.subplot", bottom=0.05) plt.rc("figure.subplot", top=0.95) plt.rc("figure.subplot", right=0.95) plt.rc("figure.subplot", left=0.15) fig = plt.figure(figsize = figsize) ax = plt.axes([0.1, 0.25, .85, .70]) ax.set_title(title, fontsize = fontsize) ax.set_xlabel(xlabel, fontsize = fontsize) ax.set_ylabel(ylabel, fontsize = fontsize) plt.xticks(fontsize = 0.7*fontsize) plt.yticks(fontsize = 0.7*fontsize) if shade: plane = np.zeros((nX,nY,3)) else: plane = np.zeros((nX,nY)) # apply shading if True if shade: ls = LightSource(azdeg = azdeg, altdeg = altdeg) rgb = [] # shading can be only used with cBar = 1 or cBar = 2 at the moment if cBar == 0: cBar = 1 # check if colormap is set, if not set it to 'copper' if not 'cmap' in kwimshow.keys(): kwimshow['cmap'] = plt.cm.copper for i in range(len(data[:,0,0])): tmp = ls.shade(data[i,:,:], kwimshow['cmap']) rgb.append(tmp.tolist()) rgb = np.array(rgb) tmp = [] # calibrate the displayed colors for the data range image = ax.imshow(plane, vmin=rangeMin, vmax=rangeMax, origin='lower', extent = extent, **kwimshow) colorbar = fig.colorbar(image) # change the font size of the colorbar's ytickslabels cbytick_obj = plt.getp(colorbar.ax.axes, 'yticklabels') plt.setp(cbytick_obj, fontsize = 0.5*fontsize) # plot the arrows # TODO: add some more options if plotArrows: # prepare the mash grid where the arrows will be drawn arrowGridX, arrowGridY = np.meshgrid(np.arange(extent[0], extent[1], float(extent[1]-extent[0])*arrowsRes/len(data[0,:,0])), np.arange(extent[2], extent[3], float(extent[3]-extent[2])*arrowsRes/len(data[0,0,:]))) arrows = ax.quiver(arrowGridX, arrowGridY, arrowsX[0,::arrowsRes,::arrowsRes], arrowsY[0,::arrowsRes,::arrowsRes], units = 'width', pivot = arrowsPivot, width = arrowsWidth, scale = arrowsScale, color = arrowsColor) # plot the grid for the arrows if plotArrowsGrid == True: ax.plot(arrowGridX, arrowGridY, 'k.') # for real-time image display if (sloppy == False) or (movieFile): manager = plt.get_current_fig_manager() manager.show() tStep = 0 if movieFile: movieFiles = [] # start the animation play('noThread') # write the movie file mencodeCommand = "mencoder 'mf://"+movieFile+"*.png' -mf type=png:fps="+np.str(fps)+" -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate="+np.str(bitrate)+" -ffourcc MP4S -oac copy -o "+movieFile+".mpg" os.system(mencodeCommand) # clean up the image files if (keepImages == False): print("cleaning up files") for fname in movieFiles: os.remove(fname) else: # set up the gui plt.ion() axPlay = plt.axes([0.1, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonPlay = plt.Button(axPlay, 'play', color='lightgoldenrodyellow', hovercolor='0.975') buttonPlay.on_clicked(play_thread) axPause = plt.axes([0.3, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonPause = plt.Button(axPause, 'pause', color='lightgoldenrodyellow', hovercolor='0.975') buttonPause.on_clicked(pausing) axReverse = plt.axes([0.5, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonReverse = plt.Button(axReverse, 'reverse', color='lightgoldenrodyellow', hovercolor='0.975') buttonReverse.on_clicked(reverse) axForward = plt.axes([0.7, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonForward = plt.Button(axForward, 'forward', color='lightgoldenrodyellow', hovercolor='0.975') buttonForward.on_clicked(forward) # create the time slider fig.subplots_adjust(bottom=0.2) sliderTimeAxes = plt.axes([0.2, 0.12, 0.6, 0.03], axisbg='lightgoldenrodyellow') sliderTime = plt.Slider(sliderTimeAxes, 'time', t[0], t[-1], valinit = 0.0) def update(val): global tStep # find the closest time step to the slider time value for i in range(len(t)): if t[i] < sliderTime.val: tStep = i if (tStep != len(t)-1): if (t[tStep+1] - sliderTime.val) < (sliderTime.val - t[tStep]): tStep += 1 plotFrame() sliderTime.on_changed(update) plt.show() print("done")
eor1[ch] = n.convolve(eor1[ch], fringe_filter, mode='same') else: # this one is the exact one ij = a.miriad.bl2ij(bls_master[0]) #beam_w_fr = capo.frf_conv.get_beam_w_fr(aa, bl) #t, firs, frbins,frspace = capo.frf_conv.get_fringe_rate_kernels(beam_w_fr, inttime, FRF_WIDTH) frp, bins = fringe.aa_to_fr_profile(aa, ij, 100) timebins, firs = fringe.frp_to_firs(frp, bins, aa.get_freqs(), fq0=aa.get_freqs()[100]) for cnt,ch in enumerate(chans): eor1[cnt] = n.convolve(eor1[cnt], firs[ch], mode='same') #eor2 = eor.values()[0] * INJECT_SIG eor = eor1 * INJECT_SIG for k in days: for bl in x[k]: x[k][bl] += eor if False and PLOT: p.subplot(211); capo.arp.waterfall(eor1, mode='real'); p.colorbar() p.subplot(212); capo.arp.waterfall(eor2, mode='real'); p.colorbar(); p.show() #Q = {} # Create the Q's that extract power spectrum modes #for i in xrange(nchan): # Q[i] = get_Q(i, nchan) Q = [get_Q(i,nchan) for i in xrange(nchan)] # Compute baseline auto-covariances and apply inverse to data I,_I,_Ix = {},{},{} C,_C,_Cx = {},{},{} for k in days: I[k],_I[k],_Ix[k] = {},{},{} C[k],_C[k],_Cx[k] = {},{},{} for bl in x[k]: C[k][bl] = cov(x[k][bl]) I[k][bl] = n.identity(C[k][bl].shape[0])
def find_triangle_minimum(self, alpha, beta, gamma, max_iteration_number=40, x_start=20, y_start=20, show=True): ''' find minimum of functional on our model parameters with triangle deformation method ''' # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time # Z_ = self.vector_calc_fuctional(X_, Y_, t) z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) # np array for quadratic errors of each iteration error = np.array((0,)) # set start triangle triangles = [] x01 = np.array((x_start, y_start)) x02 = np.array((x_start + 2, y_start)) x03 = np.array((x_start, y_start + 2)) triangles.append([x01, x02, x03]) for i in range(0, max_iteration_number): print("step number {}".format(i)) # main cycle for optimization iterations print("vector calc") ZZ_ = self.noise_vector_calc_fuctional(X_, Y_, t) print("end vector calc") # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # triangle step print("triangle step") x1, x2, x3, x_mid = self.triangle_step(triangles[i][0], triangles[i][1], triangles[i][2], t, alpha, beta, gamma) print("triangle step done") triangles.append([np.array(x1), np.array(x2), np.array(x3)]) print("scalar calc") F_now = self.scalar_calc_functional(x_mid[0], x_mid[1], t) print(" end scalar calc") # F_min = self.scalar_calc_functional(X_[n_min], Y_[n_min], t) F_min = np.min(ZZ_) er = (F_now - F_min) error = np.append(error, er) if (i % 2 == 0 and show == True): # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting triangles axes = pl.gca() for tr in triangles: draw_triangle(axes, tr[0], tr[1], tr[2]) # draw last triangle draw_triangle(axes, triangles[i + 1][0], triangles[i + 1][1], triangles[i + 1][2], color_='g') # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() print(triangles[i][0], triangles[i][1], triangles[i][2], t) print(x_min[i], y_min[i]) print(F_now, F_min) t = t + 5 * self.param['dt'] # at the end of all iterations: if (show == True): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") # plotting triangles axes = pl.gca() for tr in triangles: draw_triangle(axes, tr[0], tr[1], tr[2]) pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("triangles.png") pl.show() return error
def main(): # Training data and training labels trainX, trainY = load_data('data_batch_1') print(trainX.shape, trainY.shape) # Test data and labels testX, testY = load_data('test_batch_trim') print(testX.shape, testY.shape) # Scale Training and Test Data scaler = preprocessing.StandardScaler() trainX = scaler.fit_transform(trainX) testX = scaler.transform(testX) # Create the model x = tf.placeholder(tf.float32, [None, IMG_SIZE * IMG_SIZE * NUM_CHANNELS]) y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES]) # Build the graph for the deep net logits, conv_1_out, pool_1, conv_2_out, pool_2, keep_prob = cnn(x, conv_window, conv_window2, pool_window, pool_strides, fully_conn_layer) # calculate loss cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=logits) loss = tf.reduce_mean(cross_entropy) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) train_step1 = tf.train.MomentumOptimizer(learning_rate, 0.1).minimize(loss) train_step2 = tf.train.RMSPropOptimizer(learning_rate).minimize(cross_entropy) train_step3 = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) # calculate accuracy correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y_, 1)) correct_prediction = tf.cast(correct_prediction, tf.float32) accuracy = tf.reduce_mean(correct_prediction) N = len(trainX) idx = np.arange(N) # Start a new Session with tf.Session() as sess: # Initialise Session sess.run(tf.global_variables_initializer()) test_acc = [] err = [] batch_err = [] for i in range(epochs): # Randomly shuffle the data np.random.shuffle(idx) trainX, trainY = trainX[idx], trainY[idx] # Use mini-batch Gradient Descent for start, end in zip(range(0, N, batch_size), range(batch_size, N, batch_size)): train_step.run(feed_dict={x: trainX[start:end], y_: trainY[start: end], keep_prob: 1.0}) batch_err.append(loss.eval(feed_dict={x: trainX, y_: trainY, keep_prob: 1.0})) # Training error for one epoch: Mean batch error err.append(sum(batch_err) / len(batch_err)) # Test accuracy for one epoch test_acc.append(accuracy.eval(feed_dict={x: testX, y_: testY, keep_prob: 1.0})) print('iter', i, 'entropy', err[i]) print ('iter', i, 'accuracy', test_acc[i]) # Plot Test Accuracy #plt.figure('Test Accuracy') plt.figure('Normal-Test Accuracy') plt.plot(np.arange(epochs), test_acc, label='Normal Gradient Descent') plt.xlabel('epochs') plt.ylabel('test accuracy') plt.legend(loc='lower right') # Plot Training Error #plt.figure('Error') plt.figure('Normal- Error') plt.plot(np.arange(epochs), err, label = "Normal Gradient Descent") plt.xlabel('epochs') plt.ylabel('err') plt.legend(loc='lower right') # With Momentum print('momentum...') # Reinitialise the variables sess.run(tf.global_variables_initializer()) # Reinitialise test accuracy and error matrices test_acc = [] err=[] batch_err=[] for i in range(epochs): #Randomly shuffle the data np.random.shuffle(idx) trainX, trainY = trainX[idx], trainY[idx] # Use mini-batch Gradient Descent for start, end in zip(range(0,N,batch_size),range(batch_size,N, batch_size)): train_step1.run(feed_dict={x:trainX[start:end], y_:trainY[start: end], keep_prob:1.0}) batch_err.append(loss.eval(feed_dict={x:trainX, y_:trainY, keep_prob: 1.0})) # Training error for one epoch: Mean batch error err.append(sum(batch_err)/len(batch_err)) # Test accuracy for one epoch test_acc.append(accuracy.eval(feed_dict={x:testX, y_: testY, keep_prob: 1.0})) print('iter', i, 'entropy', err[i]) print ('iter',i, 'accuracy',test_acc[i]) # Plot Test Accuracy #plt.figure('Test Accuracy') plt.figure('With Momentum- Test Accuracy') plt.plot(np.arange(epochs), test_acc, label = "momentum") plt.xlabel('epochs') plt.ylabel('test accuracy') plt.legend(loc='lower right') # Plot the Error #plt.figure('Error') plt.figure('With Momentum-Error') plt.plot(np.arange(epochs), err, label = "momentum") plt.xlabel('epochs') plt.ylabel('err') plt.legend(loc='lower right') # Using RMSProp algorithm print('RMS Prop...') # Reinitialise the variables sess.run(tf.global_variables_initializer()) # Reinitialise test accuracy and error matrices test_acc=[] err=[] batch_err=[] for i in range(epochs): # Randomly shuffle the data np.random.shuffle(idx) trainX, trainY = trainX[idx], trainY[idx] # Use mini-batch Gradient Decsent for start, end in zip(range(0,N,batch_size),range(batch_size,N, batch_size)): train_step2.run(feed_dict={x:trainX[start:end], y_:trainY[start: end], keep_prob:1.0}) batch_err.append(loss.eval(feed_dict={x:trainX, y_:trainY, keep_prob:1.0})) # Training error for one epoch: Mean batch error err.append(sum(batch_err)/len(batch_err)) # Testing accuracy for one epoch test_acc.append(accuracy.eval(feed_dict={x:testX, y_: testY, keep_prob: 1.0})) print('iter', i, 'entropy', err[i]) print ('iter',i, 'accuracy',test_acc[i]) # Plot the Test Accuracy #plt.figure('Test Accuracy') plt.figure('RMS Prop-Test Accuracy') plt.plot(np.arange(epochs), test_acc, label = "RMS Prop") plt.xlabel('epochs') plt.ylabel('test acc') plt.legend(loc='lower right') # Plot the Error #plt.figure('Error') plt.figure('RMS Prop-Error') plt.plot(np.arange(epochs), err, label="RMS Prop") plt.xlabel('epochs') plt.ylabel('err') plt.legend(loc='lower right') # Using Adam Optimizer print('Adam...') # Reinitialise the variables sess.run(tf.global_variables_initializer()) # Reinitialise the test accuracy and error matrices test_acc=[] err=[] batch_err=[] for i in range(epochs): # Randomly shuffle the data np.random.shuffle(idx) trainX, trainY = trainX[idx], trainY[idx] # Use mini-batch Gradient Descent for start, end in zip(range(0,N,batch_size),range(batch_size,N, batch_size)): train_step3.run(feed_dict={x:trainX[start:end], y_:trainY[start: end], keep_prob:1.0}) batch_err.append(loss.eval(feed_dict={x:trainX, y_:trainY, keep_prob: 1.0})) # Training error for one epoch: Mean batch error err.append(sum(batch_err)/len(batch_err)) # Test accuracy for one epoch test_acc.append(accuracy.eval(feed_dict={x:testX, y_: testY, keep_prob: 1.0})) print('iter', i, 'entropy', err[i]) print ('iter',i, 'accuracy',test_acc[i]) # Plot Test Accuracy #plt.figure('Test Accuracy') plt.figure('Adam-Test Accuracy') plt.plot(np.arange(epochs), test_acc, label = "Adam") plt.xlabel('epochs') plt.ylabel('test acc') plt.legend(loc='lower right') # Plot Error #plt.figure('Error') plt.figure('Adam-Error') plt.plot(np.arange(epochs), err, label="Adam") plt.xlabel('epochs') plt.ylabel('err') plt.legend(loc='lower right') # Normal Gradient Descent with Dropout of 0.8 print('gd with dropout...') # Reinitialise the variables sess.run(tf.global_variables_initializer()) # Reinitialise the test accuracy and error matrices test_acc=[] err=[] batch_err=[] for i in range(epochs): # Randomly shuffle the data np.random.shuffle(idx) trainX, trainY = trainX[idx], trainY[idx] # Use mini-batch Gradient Descent for start, end in zip(range(0,N,batch_size),range(batch_size,N, batch_size)): train_step.run(feed_dict={x:trainX[start:end], y_:trainY[start: end], keep_prob:0.8}) batch_err.append(loss.eval(feed_dict={x:trainX, y_:trainY, keep_prob: 0.8})) # Training error for one epoch: Mean batch error err.append(sum(batch_err)/len(batch_err)) # Test accuracy for one epoch test_acc.append(accuracy.eval(feed_dict={x:testX, y_: testY, keep_prob: 1.0})) print('iter', i, 'entropy', err[i]) print ('iter',i, 'accuracy',test_acc[i]) # Plot Test Acccuracy #plt.figure('Test Accuracy') plt.figure('With Dropout-Test Accuracy') plt.plot(np.arange(epochs), test_acc, label='With Dropout') plt.xlabel('epochs') plt.ylabel('test accuracy') plt.legend(loc='lower right') # Plot Error #plt.figure('Error') plt.figure('With Dropout-Error') plt.plot(np.arange(epochs), err, label= "With Dropout") plt.xlabel('epochs') plt.ylabel('error') plt.legend(loc='lower right') plt.show()
def find_gradient_minimum(self, max_iteration_number=100, x_start=20, y_start=20, show=True, gamma=0.09): ''' find minimum of functional on our model parameters with simple gradient method ''' # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time # Z_ = self.vector_calc_fuctional(X_, Y_, t) z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) x_grad = np.array([x_start]) y_grad = np.array([y_start]) error = np.array((0,)) # gradient parameters dx1 = 0.1 dx2 = 0.1 for i in range(0, max_iteration_number): # main cycle for gradient iterations ZZ_ = self.vector_calc_fuctional(X_, Y_, t) # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # gradient step xg, yg = self.gradient_step(x_grad[i], y_grad[i], t, dx1, dx2, gamma) x_grad = np.append(x_grad, xg) y_grad = np.append(y_grad, yg) # squared error calculation Fmin = np.min(ZZ_) Fnow = self.scalar_calc_functional(xg, yg, t) er = (Fmin - Fnow) error = np.append(error, er) if (i % 5 == 0 and show): # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting gradient steps trajectory pl.plot(x_grad, y_grad, "-or") # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() # print(x_grad[i], y_grad[i], t) # print(x_min[i], y_min[i]) t = t + 3 * self.param['dt'] # at the end of all iterations: if (show): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") pl.plot(x_grad, y_grad, "-or") pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("growing.png") pl.show() return error
def check_C_testset(mss_id): import pylab import expenv import numpy from helper import Options from method_hierarchy_svm_new import Method #from method_augmented_svm_new import Method #costs = 10000 #[float(c) for c in numpy.exp(numpy.linspace(numpy.log(10), numpy.log(20000), 6))] costs = [ float(c) for c in numpy.exp(numpy.linspace(numpy.log(0.4), numpy.log(10), 6)) ] print costs mss = expenv.MultiSplitSet.get(mss_id) train = mss.get_train_data(-1) test = mss.get_eval_data(-1) au_roc = [] au_prc = [] for cost in costs: #create mock param object by freezable struct param = Options() param.kernel = "WeightedDegreeStringKernel" param.wdk_degree = 10 param.transform = cost param.base_similarity = 1.0 param.taxonomy = mss.taxonomy param.id = 666 #param.cost = cost param.cost = 10000 param.freeze() # train mymethod = Method(param) mymethod.train(train) assessment = mymethod.evaluate(test) au_roc.append(assessment.auROC) au_prc.append(assessment.auPRC) print assessment assessment.destroySelf() pylab.title("auROC") pylab.semilogx(costs, au_roc, "-o") pylab.show() pylab.figure() pylab.title("auPRC") pylab.semilogx(costs, au_prc, "-o") pylab.show() return (costs, au_roc, au_prc)
def plot_response_function(enu, depth, cos_theta, kind): emu = minimum_muon_energy(overburden(cos_theta, depth)) response, muyield, energy_per_nucleon = response_function( enu, emu, cos_theta, kind) import pylab from matplotlib.gridspec import GridSpec from matplotlib.ticker import NullFormatter fig = pylab.figure() grid = GridSpec(3, 1, height_ratios=[3, 1, 1], hspace=0) ax = pylab.subplot(grid[0]) elements = ['H', 'He', 'C, N, O', 'Mg, Si, Al', 'Fe'] for i, color, label in zip(range(5), colors, elements): pylab.plot(energy_per_nucleon, response[i, :], color=color, lw=0.5, label=label) pylab.plot(energy_per_nucleon, numpy.exp(-muyield[i, :]) * response[i, :], color=color, ls='--', lw=0.5) ax.plot(energy_per_nucleon, response.sum(axis=0), color='k', lw=1, label='Total') ax.plot(energy_per_nucleon, (numpy.exp(-muyield) * response).sum(axis=-2), color='k', ls='--', lw=1, label='(no muons)') pylab.loglog() ax.set_ylabel('Partial flux $[GeV^{-1}\, m^{-2}\, sr^{-1}\, s^{-1}]$') ax.legend(loc='best', prop=dict(size='small'), title='Flux contributions') passrate = (numpy.exp(-muyield) * response).sum(axis=(-2, -1)) / response.sum(axis=(-2, -1)) ax.set_title('%s %s from %d deg at %d m depth: %d%% passing rate' % (format_energy('%d', enu), kind, numpy.round(numpy.degrees( numpy.arccos(cos_theta))), depth, passrate * 100)) logmax = numpy.ceil(numpy.log10(response.max())) ax.set_ylim(10**(logmax - 8), 10**(logmax)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_ticks(ax.yaxis.get_ticklocs()[2:-1]) ax = pylab.subplot(grid[1]) for i, color, label in zip(range(5), colors, elements): pylab.plot(energy_per_nucleon, muyield[i, :], color=color, lw=0.5, label=label) pylab.loglog() logmax = numpy.ceil(numpy.log10(muyield.max())) ax.set_ylim(10**(logmax - 4), 10**(logmax)) ax.xaxis.set_major_formatter(NullFormatter()) ax.set_ylabel(r'$\left<N_{\mu} > 1\, TeV \right>$') ax.yaxis.set_ticks(ax.yaxis.get_ticklocs()[2:-1]) ax = pylab.subplot(grid[2]) for i, color, label in zip(range(5), colors, elements): pylab.plot(energy_per_nucleon, numpy.exp(-muyield[i, :]), color=color, lw=0.5, label=label) pylab.semilogx() ax.set_ylabel(r'$P(N_{\mu} = 0)$') pylab.xlabel('Energy per nucleon [GeV]') for ax in fig.axes: ax.grid(color='grey', ls='-', lw=0.1) ax.yaxis.set_label_coords(-.07, .5) help = """\ The solid lines in the upper panel of the displayed plot show the contributions of cosmic ray primaries to the differential neutrino flux at %s and %d degrees. The center panel shows the mean number of muons from each shower that reach a vertical depth of %d meters with more than 1 TeV of kinetic energy. The bottom panel shows the Poisson probability of observing 0 muons at depth given the mean in the center panel. These probabilities are multiplied with the flux contributions to obtain the effective contributions that can be observed if accompanying muons are rejected, shown as dashed lines in the upper panel. The passing rate is the ratio of observable to the total flux, which is the ratio of the integrals of the solid and dashed black curves (%d%%). """ % (format_energy('%d', enu), numpy.round(numpy.degrees( numpy.arccos(cos_theta))), depth, passrate * 100) import textwrap for line in textwrap.wrap(textwrap.dedent(help), 78): print(line) pylab.show()
def find_conj_gradient_minimum(self, max_iteration_number=100, x_start=20, y_start=20, show=True): ''' find minimum of functional on our model parameters with conjugate gradient method ''' # we calling step function depending on type_ # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) x_grad = np.array([x_start]) y_grad = np.array([y_start]) error = np.array((0,)) # conjugate gradient parameters dx1 = 0.1 dx2 = 0.1 lam = 0.001 S1 = 0 S2 = 0 df1 = 0 df2 = 0 for i in range(0, max_iteration_number): # main cycle for gradient iterations ZZ_ = self.vector_calc_fuctional(X_, Y_, t) # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # gradient step xg, yg, df1_n, df2_n, S1_n, S2_n = self.conj_gradient_step((i == 0), x_grad[i], \ y_grad[i], dx1, dx2, df1, df2, S1, S2, lam, t) x_grad = np.append(x_grad, xg) y_grad = np.append(y_grad, yg) # remember new parameters S1 = S1_n S2 = S2_n df1 = df1_n df2 = df2_n # error F_now = self.scalar_calc_functional(xg, yg, t) F_min = np.min(ZZ_) er = (F_now - F_min) error = np.append(error, er) if (i % 10 == 0 and show): print(i) # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting gradient steps trajectory pl.plot(x_grad, y_grad, "-or") # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() # print(x_grad[i], y_grad[i], t) # print(x_min[i], y_min[i]) t = t + 5 * self.param['dt'] # at the end of all iterations: if (show): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") pl.plot(x_grad, y_grad, "-or") pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("growing.png") pl.show() return error
def simulationWithDrug(numViruses, maxPop, maxBirthProb, clearProb, resistances, mutProb, numTrials): """ For each of numTrials trials, instantiates a patient, runs a simulation for 150 timesteps, adds guttagonol, and runs the simulation for an additional 150 timesteps. At the end plots the average virus population size (for both the total virus population and the guttagonol-resistant virus population) as a function of time. numViruses: number of ResistantVirus to create for patient (an integer) maxPop: maximum virus population for patient (an integer) maxBirthProb: Maximum reproduction probability (a float between 0-1) clearProb: maximum clearance probability (a float between 0-1) resistances: a dictionary of drugs that each ResistantVirus is resistant to (e.g., {'guttagonol': False}) mutProb: mutation probability for each ResistantVirus particle (a float between 0-1). numTrials: number of simulation runs to execute (an integer) """ numTimesteps = 300 virusPopulationPerTrial = {x: () for x in range(numTimesteps)} resistantVirusPopulationPerTrial = dict(virusPopulationPerTrial) for trial in range(numTrials): # Line bellow broken to comply with pep8 viruses = [ResistantVirus(maxBirthProb, clearProb, resistances, mutProb) for x in range(numViruses)] patient = TreatedPatient(viruses, maxPop) # Runs the first half of the simulation without drugs for timestep in range(numTimesteps//2): patient.update() virusPopulationPerTrial[timestep] += (patient.getTotalPop(),) # Line bellow broken to comply with pep8 resistantVirusPopulationPerTrial[timestep] += ( patient.getResistPop(['guttagonol']), ) patient.addPrescription('guttagonol') # Runs the second half of the simulation after adding the drug for timestep in range(numTimesteps//2, numTimesteps): patient.update() virusPopulationPerTrial[timestep] += (patient.getTotalPop(),) # Line bellow broken to comply with pep8 resistantVirusPopulationPerTrial[timestep] += ( patient.getResistPop(['guttagonol']), ) avgVirusPopulation = {} avgResistantVirusPopulation = {} # Runs the trials for timestep in virusPopulationPerTrial: # Line bellow broken to comply with pep8 avgVirusPopulation[timestep] = ( sum(virusPopulationPerTrial[timestep]) / len(virusPopulationPerTrial[timestep]) ) # Line bellow broken to comply with pep8 avgResistantVirusPopulation[timestep] = ( sum(resistantVirusPopulationPerTrial[timestep]) / len(resistantVirusPopulationPerTrial[timestep]) ) # Plots the results # Line bellow broken to comply with pep8 pylab.plot(list(avgVirusPopulation.values()), label="Average total virus population") # Line bellow broken to comply with pep8 pylab.plot(list(avgResistantVirusPopulation.values()), label="Average population of guttagonol-resistant virus") pylab.title("ResistantVirus simulation") pylab.xlabel("time step") pylab.ylabel("# viruses") pylab.legend(loc="best") pylab.show()
nz * vol)) # Plot dn/dz in arcmin^-2 P.subplot(111) P.plot(_z, dndz_expt(_z) / 60.**2., 'b-', lw=1.8) P.tick_params(axis='both', which='major', labelsize=18, size=8., width=1.5, pad=5.) P.ylabel(r"$N(z)$ $[{\rm amin}^{-2}]$", fontsize=18.) P.xlabel("$z$", fontsize=18.) P.xlim((0., 0.6)) P.tight_layout() P.show() exit() # Comparison plot of dndz, bias, and fitting function if DEBUG_PLOT: P.subplot(211) P.plot(_z, dndz_expt(_z)) P.plot(_z, p[0] * _z**p[1] * np.exp(-p[2] * _z)) P.subplot(212) P.plot(_z, bias_expt(_z)) P.plot(_z, pb[0] * np.exp(pb[1] * _z)) P.show()
def kepfilter(infile, outfile, datacol, function, cutoff, passband, plot, plotlab, clobber, verbose, logfile, status, cmdLine=False): ## startup parameters status = 0 numpy.seterr(all="ignore") labelsize = 24 ticksize = 16 xsize = 16 ysize = 6 lcolor = '#0000ff' lwidth = 1.0 fcolor = '#ffff00' falpha = 0.2 ## log the call hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile, hashline, verbose) call = 'KEPFILTER -- ' call += 'infile=' + infile + ' ' call += 'outfile=' + outfile + ' ' call += 'datacol=' + str(datacol) + ' ' call += 'function=' + str(function) + ' ' call += 'cutoff=' + str(cutoff) + ' ' call += 'passband=' + str(passband) + ' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot=' + plotit + ' ' call += 'plotlab=' + str(plotlab) + ' ' overwrite = 'n' if (clobber): overwrite = 'y' call += 'clobber=' + overwrite + ' ' chatter = 'n' if (verbose): chatter = 'y' call += 'verbose=' + chatter + ' ' call += 'logfile=' + logfile kepmsg.log(logfile, call + '\n', verbose) ## start time kepmsg.clock('KEPFILTER started at', logfile, verbose) ## test log file logfile = kepmsg.test(logfile) ## clobber output file if clobber: status = kepio.clobber(outfile, logfile, verbose) if kepio.fileexists(outfile): message = 'ERROR -- KEPFILTER: ' + outfile + ' exists. Use clobber=yes' status = kepmsg.err(logfile, message, verbose) ## open input file if status == 0: instr, status = kepio.openfits(infile, 'readonly', logfile, verbose) tstart, tstop, bjdref, cadence, status = kepio.timekeys( instr, infile, logfile, verbose, status) if status == 0: try: work = instr[0].header['FILEVER'] cadenom = 1.0 except: cadenom = cadence ## fudge non-compliant FITS keywords with no values if status == 0: instr = kepkey.emptykeys(instr, file, logfile, verbose) ## read table structure if status == 0: table, status = kepio.readfitstab(infile, instr[1], logfile, verbose) # read time and flux columns if status == 0: barytime, status = kepio.readtimecol(infile, table, logfile, verbose) flux, status = kepio.readsapcol(infile, table, logfile, verbose) # filter input data table if status == 0: try: nanclean = instr[1].header['NANCLEAN'] except: naxis2 = 0 for i in range(len(table.field(0))): if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0): table[naxis2] = table[i] naxis2 += 1 instr[1].data = table[:naxis2] comment = 'NaN cadences removed from data' status = kepkey.new('NANCLEAN', True, comment, instr[1], outfile, logfile, verbose) ## read table columns if status == 0: intime, status = kepio.readtimecol(infile, instr[1].data, logfile, verbose) if status == 0: indata, status = kepio.readfitscol(infile, instr[1].data, datacol, logfile, verbose) if status == 0: intime = intime + bjdref indata = indata / cadenom ## define data sampling if status == 0: tr = 1.0 / (cadence / 86400) timescale = 1.0 / (cutoff / tr) ## define convolution function if status == 0: if function == 'boxcar': filtfunc = numpy.ones(numpy.ceil(timescale)) elif function == 'gauss': timescale /= 2 dx = numpy.ceil(timescale * 10 + 1) filtfunc = kepfunc.gauss() filtfunc = filtfunc([1.0, dx / 2 - 1.0, timescale], linspace(0, dx - 1, dx)) elif function == 'sinc': dx = numpy.ceil(timescale * 12 + 1) fx = linspace(0, dx - 1, dx) fx = fx - dx / 2 + 0.5 fx /= timescale filtfunc = numpy.sinc(fx) filtfunc /= numpy.sum(filtfunc) ## pad time series at both ends with noise model if status == 0: ave, sigma = kepstat.stdev(indata[:len(filtfunc)]) padded = append( kepstat.randarray( np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma), indata) ave, sigma = kepstat.stdev(indata[-len(filtfunc):]) padded = append( padded, kepstat.randarray( np.ones(len(filtfunc)) * ave, np.ones(len(filtfunc)) * sigma)) ## convolve data if status == 0: convolved = convolve(padded, filtfunc, 'same') ## remove padding from the output array if status == 0: if function == 'boxcar': outdata = convolved[len(filtfunc):-len(filtfunc)] else: outdata = convolved[len(filtfunc):-len(filtfunc)] ## subtract low frequencies if status == 0 and passband == 'high': outmedian = median(outdata) outdata = indata - outdata + outmedian ## comment keyword in output file if status == 0: status = kepkey.history(call, instr[0], outfile, logfile, verbose) ## clean up x-axis unit if status == 0: intime0 = float(int(tstart / 100) * 100.0) if intime0 < 2.4e6: intime0 += 2.4e6 ptime = intime - intime0 xlab = 'BJD $-$ %d' % intime0 ## clean up y-axis units if status == 0: pout = indata * 1.0 pout2 = outdata * 1.0 nrm = len(str(int(numpy.nanmax(pout)))) - 1 pout = pout / 10**nrm pout2 = pout2 / 10**nrm ylab = '10$^%d$ %s' % (nrm, plotlab) ## data limits xmin = ptime.min() xmax = ptime.max() ymin = numpy.nanmin(pout) ymax = numpy.nanmax(pout) xr = xmax - xmin yr = ymax - ymin ptime = insert(ptime, [0], [ptime[0]]) ptime = append(ptime, [ptime[-1]]) pout = insert(pout, [0], [0.0]) pout = append(pout, 0.0) pout2 = insert(pout2, [0], [0.0]) pout2 = append(pout2, 0.0) ## plot light curve if status == 0 and plot: try: params = { 'backend': 'png', 'axes.linewidth': 2.5, 'axes.labelsize': labelsize, 'axes.font': 'sans-serif', 'axes.fontweight': 'bold', 'text.fontsize': 12, 'legend.fontsize': 12, 'xtick.labelsize': ticksize, 'ytick.labelsize': ticksize } rcParams.update(params) except: print 'ERROR -- KEPFILTER: install latex for scientific plotting' status = 1 if status == 0 and plot: pylab.figure(figsize=[xsize, ysize]) pylab.clf() ## plot filtered data ax = pylab.axes([0.06, 0.1, 0.93, 0.87]) pylab.gca().xaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) pylab.gca().yaxis.set_major_formatter( pylab.ScalarFormatter(useOffset=False)) labels = ax.get_yticklabels() setp(labels, 'rotation', 90, fontsize=12) pylab.plot(ptime, pout, color='#ff9900', linestyle='-', linewidth=lwidth) fill(ptime, pout, color=fcolor, linewidth=0.0, alpha=falpha) if passband == 'low': pylab.plot(ptime[1:-1], pout2[1:-1], color=lcolor, linestyle='-', linewidth=lwidth) else: pylab.plot(ptime, pout2, color=lcolor, linestyle='-', linewidth=lwidth) fill(ptime, pout2, color=lcolor, linewidth=0.0, alpha=falpha) xlabel(xlab, {'color': 'k'}) ylabel(ylab, {'color': 'k'}) xlim(xmin - xr * 0.01, xmax + xr * 0.01) if ymin >= 0.0: ylim(ymin - yr * 0.01, ymax + yr * 0.01) else: ylim(1.0e-10, ymax + yr * 0.01) pylab.grid() # render plot if cmdLine: pylab.show() else: pylab.ion() pylab.plot([]) pylab.ioff() ## write output file if status == 0: for i in range(len(outdata)): instr[1].data.field(datacol)[i] = outdata[i] instr.writeto(outfile) ## close input file if status == 0: status = kepio.closefits(instr, logfile, verbose) ## end time if (status == 0): message = 'KEPFILTER completed at' else: message = '\nKEPFILTER aborted at' kepmsg.clock(message, logfile, verbose)
value_name='Judet') plt.figure(figsize=(10,10)) sns.violinplot(X="Judet", y="Judet", hue="Judet", data=data,split=True, inner="quart") plt.xticks(rotation=90) """ # df = pd.read_csv(filecsv, delimiter="^", na_values="Not Available", nrows=30, usecols=[9],dtype={'DataPublicare': np.str, 'Judet': np.str, 'TipContract': np.str, 'ValoareEstimata': np.float} ,skiprows = 1 ) # # df = pd.read_csv(filecsv, delimiter="^", usecols=[0,1,2,3,4,5,6], na_values="Not Available", nrows=20) # df = pd.read_csv(filecsv, skiprows = 1, index_col=0, na_values = ['no info', '.'],dtype={'speed':int, 'period':str, 'warning':str, 'pair':int, "revenues" : "float64"}, header = None) #rad_viz = pd.plotting.radviz(df2, 'Judet') # doctest: +SKIP #df3 = pd.DataFrame(df2, columns=['Judet','ValoareEstimata']) #scatter_matrix(df2, alpha=0.2) #plt.close('all') #plt.show() """ #pylab.clf() axs = scatter_matrix(df2, alpha=100, diagonal='hist') #axs = scatter_matrix(df, alpha=0.2, ax=None, grid=False, diagonal='hist', marker='.', density_kwds=None, hist_kwds=None, range_padding=0.05) #axs = scatter_matrix(df, alpha=20, ax=None, grid=False, diagonal='hist', marker='.', density_kwds=None, hist_kwds=None) for ax in axs[:,0]: # the left boundary ax.grid('off', axis='both') ax.set_yticks([0, .5]) for ax in axs[-1,:]: # the lower boundary ax.grid('off', axis='both') #pylab.savefig( "111.png") pylab.show() """ """ plt.hist(X, bins=20, normed=1) # matplotlib version (plot) plt.show()