def Task354(): pts1 = [scalar_vector_mult(0.5, v) for v in L] pts2 = [scalar_vector_mult(-0.5, v) for v in L] "help(list): __add__ overloading" pts = [x for x in pts1 + pts2] print(pts) plot(pts, 4)
def plot_profiles(prots, eluts, sp='Hs', plot_sums=True, shape=None, min_count=1): """ shape: (m,n) = m rows, n columns eluts: [el.NormElut(f, sp, norm_rows=False, norm_cols=False) for f in fs] """ import plotting as pl gt = seqs.GTrans() use_eluts = elutions_containing_prots(eluts, sp, seqs.names2ids(prots), min_count) shape = shape if shape else ut.sqrt_shape(len(use_eluts)+1) fig = pl.figure() for i,e in enumerate(use_eluts): sp_target = ut.shortname(e.filename)[:2] pl.subplot(shape[0],shape[1],i+1) pl.title(ut.shortname(e.filename)) pids = [gt.name2id[p] for p in prots] protsmax = max([np.max(e.normarr[r]) for p in pids if p in e.baseid2inds for r in e.baseid2inds[p]]) plot_prots(e, pids, e.baseid2inds, protsmax) if plot_sums: # plot total spectral counts normalized to match biggest peak sums = np.sum(e.normarr,axis=0) fmax = np.max(sums) pl.plot(range(sums.shape[1]), np.log2(sums[0,:]).T*np.log2(protsmax)*len(pids)/np.log2(fmax), color='k', linestyle='-', linewidth=.5) # make legend with all prots pl.subplot(shape[0],shape[1],0) for p in prots: pl.plot(0,label=p) pl.legend()
def Task369(): def segment(pt1, pt2): return [add2(scalar_vector_mult(t / 100, pt1), scalar_vector_mult(1 - (t / 100), pt2)) for t in range(100)] pts = segment([3.5, 3], [0.5, 1]) print(pts) plot(pts, 6)
def Task2420(): data = color2gray((file2image('img01.png'))) row = len(data[0]) col = len(data) pts = {(x+y*1j)*e**(pi/4*1j) for x in range(row) for y in range(col) if data[col-y-1][x]<120} avg = sum(pts)/len(pts) pts2 = {(z-avg)/2 for z in pts} plot(pts2, max(row, col), 1)
def plot_inds_together(inds, elut): for rowid in baseid2inds[pid]: row = elut.mat[rowid] if np.max(row) >= min_count: rowmax = np.max(row) protsmax = rowmax if rowmax > protsmax else protsmax pl.plot(range(row.shape[1]),row[0,:].T, label=pname, color=pl.COLORS[i],linewidth=1)
def loadImageAndPlot(imageFile): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) S = [ x+(y*1j) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ] plotting.plot(S, max(row, col), 1)
def loadImageRotateAndPlot(imageFile, theta): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) S = rotate([ x+(y*1j) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ], theta) plotting.plot(S, max(row * 1.5, col * 1.5), 1)
def plot_line(start_x, start_y, end_x, end_y): start = create_point(start_x, start_y) end = create_point(end_x, end_y) equation = end - start line = line_to_point(equation, 100) transformed_line = translate_line(line, start) plot(transformed_line, 3)
def reportLastMazeGrad(self, steps, iii, idxA, positions, density, mazegrad, times, number): import plotting bk = steps line = positions[iii-bk:iii, idxA] posidx = np.array(density * line, dtype=np.int_) mazepoints = mazegrad[:,posidx[:,0],posidx[:,1]] mazepoints = np.sqrt(mazepoints[0]*mazepoints[0] + mazepoints[1]*mazepoints[1]) y = [mazepoints] plotting.plot([range(len(mazepoints))] , y, folder=self.resultsdir, savefile="mazeline_%d.png" % number)
def plot_sums(fs, shape=None): import plotting as pl shape = shape if shape else ut.sqrt_shape(len(fs)) for i,f in enumerate(fs): e = el.load_elution(f) pl.subplot(shape[0],shape[1],i+1) pl.title(ut.shortname(f)) sums = np.sum(e.mat,axis=0) pl.plot(range(sums.shape[1]), sums[0,:].T)
def draw_plot(): v = [-1.5, 2] v_points = [scalar_vector_mult(i/10, v) for i in range(11)] u = [3, 0] u_points = [scalar_vector_mult(i/10, u) for i in range(11)] c_points = [scalar_vector_ext(i/10, u, -4/9, 4/3) for i in range(-11, 11)] plot(u_points + v_points + c_points)
def Task2417(): """ How do i know math modules? >>> import math >>> help(math) """ n=20 w=e**(2*pi*1j/n) pts={w**(x) for x in range(n)} print(pts) plot(pts, 4)
def loadImageAndPlotToCenterRotateAndScale(imageFile): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = rotateAndScaled([ x+(y*1j)+toCenter for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ]) plotting.plot(S, max(yCenter, xCenter), 1)
def loadImageRotateCenterAndPlot(imageFile, theta): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = rotate([ x+(y*1j)+toCenter for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ], theta) plotting.plot(S, max(yCenter * 1.5, xCenter * 1.5), 1)
def loadImageRotateCenterScaleAndPlot(imageFile, theta, scale): data = image.color2gray(image.file2image(imageFile)) row = len(data) col = len(data[0]) yCenter = int(row / 2) xCenter = int(col / 2) toCenter = 0 - xCenter - yCenter * 1j S = [ math.e**(theta*1j)*scale*(x+(y*1j)+toCenter) for y in range(len(data)) for x in range(len(data[y])) if data[row-y-1][x] < 120 ] plotting.plot(S, max(yCenter * 1.5, xCenter * 1.5), 1)
def task1410(filename): # Errata mentions to use color2gray to convert the 3-tuples that file2image gives into a single number data = color2gray(file2image(filename)) # Original Pseudo-code # for y = 0; y < len(data); y++ # for x = 0; x < len(data[y]); x++ # if(data[y][x] < 120) # pts.add(x + yj) # pts = {x + yj for {y,x} in data if data[y][x] < 120} # Fix this by subtracting the y we get from the height (length of data) pts = [x+(len(data)-y)*1j for y, datay in enumerate(data) for x, intensity in enumerate(datay) if data[y][x] < 120] plot(pts, 190) return pts # Not specified in task, but useful for later manipulation
def plotGuess(self,*args,**kwds): self.getGuessParamsFromGui() if self.guessCurve is not None: self.guessCurve.setXY(X = self.X, Y = self.func(self.X,self.packParamsGuess())) else: self.guessCurve = plotting.plot(self.X, self.func(self.X,self.packParamsGuess()) ,parentCurve = self.parentCurve,name = "guess for fit",linestyle = "--",color = "red") self.guessCurve.plot()
def plot(self): ''' if plot option is set, plot ''' if self.has_plot or self.save_png: if self.verbose: print "Start now plotting..." if self.save_png: filename = self.get_name() else: filename = None plotting.plot(self.graph, show=self.has_plot, save_to_file=filename) if self.verbose: print "Closed plot." return
def Task249(): plot({z*(-1j)/2 +2 -1j for z in S}, 4) print("plot({z*(-1j)/2 +2 -1j for z in S}, 4)...")
def Task247(): plot({z/2 for z in S}, 4) print("plot({z/2 for z in S}, 4)...")
from plotting import plot from matutil import coldict2mat a1 = list2vec([1/100, 0, 0]) a2 = list2vec([0, 1/100, 0]) a3 = list2vec([-1/2, -1/2, 1]) camera_basis = [a1, a2, a3] pt = list2vec([1, 1, 8]) coordinate_representation = vec2rep(camera_basis, pt) print(coordinate_representation) print(coordinate_representation/coordinate_representation[2]) i, j = 62.5, 62.5 plot([(i,j)], 80) def line_segment(pt1, pt2, samples=100): return [(i/samples)*pt1 + (1-i/samples)*pt2 for i in range(samples+1)] corners = [list2vec([1,1,8])+list2vec(v) for v in [[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1]]] line_segments = [line_segment(corners[i], corners[j]) for i,j in [(0,1),(2,3),(0,2),(1,3),(4,5),(6,7),(4,6),(5,7),(0,4),(1,5),(2,6),(3,7)]] pts = sum(line_segments, []) reps = [vec2rep(camera_basis, v) for v in pts] def scale_down(u): return list2vec([u[0]/u[2], u[1]/u[2], 1]) in_camera_plane = [scale_down(u) for u in reps] def pixel(u): return (u[0], u[1]) pixels = [pixel(u) for u in in_camera_plane] # in camera plane
def AxE(plt,df_lowpt,df_egamma,df_orig=None) : xaxis_egamma = None xaxis_lowpt = None if df_orig is not None : xaxis_egamma = df_orig[(df_orig.has_gsf) & \ (df_orig.gsf_pt>0.5) & \ (np.abs(df_orig.gsf_eta)<2.4)] xaxis_lowpt = df_lowpt[(df_lowpt.has_gsf) & \ (df_lowpt.gsf_pt>0.5) & \ (np.abs(df_lowpt.gsf_eta)<2.4)] # EGamma GSF print has_gsf = (df_egamma.has_gsf) & (df_egamma.gsf_pt>0.5) & (np.abs(df_egamma.gsf_eta)<2.4) plot( plt=plt, df=df_egamma, string="EGamma GSF tracks, AxE", selection=has_gsf, draw_roc=False, draw_eff=True, label='EGamma GSF ($\mathcal{A}\epsilon$)', color='green', markersize=8, linestyle='solid', df_xaxis=xaxis_egamma ) # EGamma PF ele print has_ele = (df_egamma.has_ele) & (df_egamma.gsf_pt>0.5) & (np.abs(df_egamma.gsf_eta)<2.4) plot( plt=plt, df=df_egamma, string="EGamma PF ele, AxE", selection=has_ele, draw_roc=False, draw_eff=True, label='EGamma PF ele ($\mathcal{A}\epsilon$)', color='purple', markersize=8, linestyle='solid', df_xaxis=xaxis_egamma ) # Low-pT GSF tracks (unbiased + ptbiased) print has_gsf = (df_lowpt.has_gsf) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT GSF tracks + unbiased, AxE", selection=has_gsf, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ GSF + PreId ($\mathcal{A}\epsilon$)', color='red', markersize=8, linestyle='solid', discriminator=df_lowpt.gsf_bdtout1, df_xaxis=xaxis_lowpt ) plot( plt=plt, df=df_lowpt, string="Low pT GSF tracks + ptbiased, AxE", selection=has_gsf, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ GSF + PreId ($\mathcal{A}\epsilon$)', color='red', markersize=8, linestyle='dashed', discriminator=df_lowpt.gsf_bdtout2, df_xaxis=xaxis_lowpt ) # Low-pT GSF electrons (unbiased + ptbiased) print has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele + unbiased, AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + PreId ($\mathcal{A}\epsilon$)', color='blue', markersize=8, linestyle='solid', discriminator=df_lowpt.gsf_bdtout1, df_xaxis=xaxis_lowpt ) plot( plt=plt, df=df_lowpt, string="Low pT ele + ptbiased, AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + PreId ($\mathcal{A}\epsilon$)', color='blue', markersize=8, linestyle='dashed', discriminator=df_lowpt.gsf_bdtout2, df_xaxis=xaxis_lowpt )
def face_by_vectors(): plot(L, 4)
trainbatches = np.split(trainbatch, trainbatch.shape[0] // minibatch_size, axis=0) # subset shuffled datasets return trainbatches test_normal = False test_GD = False test_SGD = True if test_GD: W, b = grad_descent(W, b, trainData, trainTarget, lrs[0], epochs, reg[0], error_tolerance, 'CE') plot(epochs, trainloss_list, valloss_list, testloss_list, train_acc_list, val_acc_list, test_acc_list, True) if test_SGD: #SGD(trainingData, trainingLabels, alpha, iterations, regularization, minibatch_size, beta1, beta2, epsilon, lossType = None) W, b = SGD(trainData, trainTarget, 0.001, epochs, 0, 500, 0.9, 0.999, 1e-4, lossType='CE') plot(epochs, trainloss_list, valloss_list, testloss_list, train_acc_list, val_acc_list, test_acc_list, False)
def face_scaled(): plot([scalar_vector_mult(0.5, l) for l in L], 4) plot([scalar_vector_mult(-0.5, l) for l in L], 4)
def playwithcircle(): plot(e**(t*pi*1j/20) for t in range(20)) print 'test'
from plotting import plot # Task 2.3.2 L = [[2, 2], [3, 2], [1.75, 1], [2, 1], [2.25, 1], [2.5, 1], [2.75, 1], [3, 1], [3.25, 1]] plot(L, 4) # Task 2.4.3 def add2(v, w): return [v[0] + w[0], v[1] + w[1]] plot([add2(v, [1, 2]) for v in L], 4) # Quiz 2.4.4: Suppose we represent n-vectors by n-element lists. Write a # procedure addn to compute the sum of two vectors so represented. def addn(v, w): return [x + y for (x, y) in zip(v, w)] # Quiz 2.5.3: Suppose we represent n-vectors by n-element lists. Write a # procedure scalar_vector_mult(alpha, v) that multiplies the vector v by the # scalar alpha. def scalar_vector_mult(alpha, v): return [alpha * n for n in v] # Task 2.5.4 plot([scalar_vector_mult(0.5, n) for n in L], 4)
def design_model_no_dropout(X, learning_rate): model = Sequential(name="my_first_model") input = layers.InputLayer(input_shape=(X.shape[1], )) model.add(input) model.add(layers.Dense(128, activation='relu')) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(24, activation='relu')) model.add(layers.Dense(1)) opt = tf.keras.optimizers.Adam(learning_rate=learning_rate) model.compile(loss='mse', metrics=['mae'], optimizer=opt) return model #using the early stopping in fit_model learning_rate = 0.001 num_epochs = 200 #train the model without dropout history1 = fit_model(design_model_no_dropout(features_train, learning_rate), features_train, labels_train, learning_rate, num_epochs) #train the model with dropout history2 = fit_model(design_model_dropout(features_train, learning_rate), features_train, labels_train, learning_rate, num_epochs) plot(history1, 'static/images/no_dropout.png') plot(history2, 'static/images/with_dropout.png') import app
#Task 1.4.9: Using a comprehension, create a new plot in which the points of S are rotated by 90 degrees, scaled by #1/2, and then shifted down by one unit and to the right by two units. Use a comprehension in which the points of #S are multiplied by one complex number and added to another S = {(1.75 + 1j), (2 + 1j), (2.25 + 1j), (2.5 + 1j), (2 + 2j), (3 + 2j), (2.75 + 1j), (3 + 1j), (3.25 + 1j)} plot({(1/2) * z * 1j - 1j + 2 for z in S}, 4) #1/2 because scaling by 1/2 #z represents each coordinate in S #*1j represents the rotation by 90 degrees #-1j is shifting down by 1 unit #+2 is shifting to the right by 2 units #Order matters!!!! #Task 1.4.18: Write a comprehension whose value is the set consisting of rotations by pi/4 #of the elements of S. Plot the value of this comprehension. #Solution: from math import e, pi from plotting import plot S = {(1.75 + 1j), (2 + 1j), (2.25 + 1j), (2.5 + 1j), (2 + 2j), (3 + 2j), (2.75 + 1j), (3 + 1j), (3.25 + 1j)} #I will save the comprehension to a variable called coordinates. You don't have to do this, you can put the #comprehension directly in the plot function if you want. coordinates = {x* (**e((pi/4) * 1j)) for x in S} plot(coordinates, 4) #x is each coordinate with S #e is part of Euler's formula
] # List Comprehension. Wird für jeden Punkt betrachtet -> for xi in x: ) closest_centroid_idx = np.argmin( distance_to_centroids ) # Herausfinden des Index im Array. np.argmin() -> Returns the indices of the minimum values along an axis. y_pred[idx] = closest_centroid_idx return y_pred def score( self, x: np.ndarray ): # y wird nicht entgegengenommen, da nichts gemacht wird in Unsupervised Learning pass kmeans = KMeans(n_clusters=3, max_iter=1_000) kmeans.fit(x) y_pred = kmeans.predict(x).astype( int) # y_Pred als Integerwerte ausgeben, da sonst Probleme mit Farben print(kmeans.centroids_) print(y_pred) #Plot der Daten nach Clustering (Farben können tauschen im Vergleich zum ersten Plot da zufällig) for idx, point in enumerate(x): plt.scatter(point[0], point[1], color=colors[y_pred[idx]], marker="o") for centroid in kmeans.centroids_: plt.scatter(centroid[0], centroid[1], color="black", marker="*", s=200) plt.show() plot(x, y, y_pred, kmeans.centroids_, kmeans)
if i_episode % args.log_interval == 0: print(f'Episode {i_episode}\tLast length: {t}\tEpisode reward/length or EMA running_return: {running_return}') #check if task has been solved, exit training. Task solved for this env means that the agent has been able to hold the stick up for 475 steps for sufficient number of different episodes if running_return > env.spec.reward_threshold: print("---> Solved! Running return is now {running_return} and the last episode runs to {t} time steps! \a") break print(f'Running return = {running_return}\tenv.spec.reward_threshold={env.spec.reward_threshold}') if __name__ == '__main__': ## create Env env = gym.make('CartPole-v1') env.seed(args.seed) torch.manual_seed(args.seed) print(f'env = {env}') print(f'env.spec.reward_threshold = {env.spec.reward_threshold}') ## params for training nb_episodes = args.nb_episodes gamma = args.gamma learning_rate = args.learning_rate ema_alpha = args.ema_alpha ## ML stuff policy = Policy(env) print(f'{policy.print_mdl()}') optimizer = optim.Adam(policy.parameters(), lr=learning_rate) ## Start Training print('--- Start Training RL agent') train(policy,optimizer,env,gamma,nb_episodes=nb_episodes,ema_alpha=ema_alpha) print('--- DONE TRAINING') if args.plot: my_plt.plot(policy)
def std(self, optimize=False, plot=False, plotScaled=False, plotType1='o', plotType2='-', log=False): """ Calculates the STD of the height of the pile once in the steady state for all systems. Also produces an optimised fit through the points""" STDs = [] t0 = int(self.tMax - self.N) T = self.tMax - t0 # Setting the number of data points to use for calculating the stds lowerLimit = t0 + 1 # The lower index for the heights list to scan upperLimit = t0 + T # The upper index for the heights list to scan for i in range(len(self.systemHeights)): heights = self.systemHeights[i] steady = heights[lowerLimit:upperLimit + 1] firstTerm = np.sum(steady**2) / float(T) # Calculating <h^2> secondTerm = (np.sum(steady) / float(T))**2 # Calculating <h>^2 STD = np.sqrt( firstTerm - secondTerm) # Substracting the two terms and taking the sqrt STDs.append(STD) # Converting the list to numpy array to easier manipulation STDs = np.asarray(STDs, dtype='float64') if optimize: # The STDs are optimised using a fit function "a0 * (L**D)" (two smalllest system sizes ignored to avoid corrections to scaling) popt, pcov = curve_fit(funcs.func2, self.systemSizes[2:], STDs[2:]) a0, D = popt[0], popt[ 1] # Getting the paramaters from the optimised fit scaledSTDs = np.array( STDs / (self.systemSizes**float(D)), dtype='float64') # Scaling the STDs by dividing by L^D if plot: # Plotting STDs vs L xLab = r"$L$" yLab = r"$\sigma_{h}(L)$" title = "Average Height STD of Pile in Steady State vs System Size" legend = "Average Height STDs" if optimize: # Also plotting the optimised fit curve through the numerical data point legend = [ "Average Height STDs", "Optimized Fit Function " r"$a_0L^D$" ] plotTypes = [plotType1, plotType2] optimizeData = [ i for i in range(self.systemSizes[0], self.systemSizes[-1] + 1) ] pt.plot(xData=[self.systemSizes, optimizeData], yData=[STDs, funcs.func2(optimizeData, a0, D)], plotType=plotTypes, xLab=xLab, yLab=yLab, \ title=title, legend=legend, multiX=True, multiY=True, log=log) else: pt.plot(xData=self.systemSizes, yData=STDs, plotType='o-', xLab=xLab, yLab=yLab, title=title, legend=legend, log=log) if plotScaled: # Plotting scaled STDs vs L xLab = r"$L$" yLab = r"$\sigma_{h}(L)/L^{D}$" title = "(Average Height STD)/" r"$L^{D}$" " vs System Size" legend = "Scaled Average Height STDs" pt.plot(xData=self.systemSizes, yData=scaledSTDs, plotType='o-', xLab=xLab, yLab=yLab, title=title, legend=legend, log=log) if optimize: return STDs, scaledSTDs, a0, D # Also returning fit parameters else: return STDs # Only returning calculated STDs
def particular_segment(): plot(segment([3.5, 3], [0.5, 1]), 4)
def averageHeights(self, optimize=False, plot=False, plotScaled=False, plotType1='o', plotType2='-', log=False): """ Calculates the average height of the pile once in the steady state for all systems. Also computes the corrections to scaling parameters""" averageHeights = [] scaledHeights = [] t0 = int(self.tMax - self.N) T = self.tMax - t0 # Setting the number of data points to use for calculating the average heights lowerLimit = t0 + 1 # The lower index for the heights list to scan upperLimit = t0 + T # The upper index for the heights list to scan for i in range(len(self.systemHeights)): heights = self.systemHeights[i] L = self.systemSizes[i] steady = heights[lowerLimit:upperLimit + 1] # Getting the steady state heights averageHeight = np.sum(steady) / float( T) # Summing the steady state heights and dividing by T scaledHeight = averageHeight / float( L ) # Scaling the average heights as <h>/L to see corrections to scaling averageHeights.append(averageHeight) scaledHeights.append(scaledHeight) # Converting the lists to numpy arrays to easier manipulation averageHeights = np.asarray(averageHeights, dtype='float64') scaledHeights = np.asarray(scaledHeights, dtype='float64') if optimize: # The scaled heights are optimised using a fit function "a0 - b*(L**(-w1))" popt, pcov = curve_fit(funcs.func1, self.systemSizes, scaledHeights) a0, b, w1 = popt[0], popt[1], popt[ 2] # Getting the paramaters for the corrections to scaling for the average heights if plot: # Plotting the average heights vs L xLab = r"$L$" yLab = r"$<h(t; L)>_t$" title = "Average Height of Pile (at Site 1) in Steady State vs System Size" legend = "Steady State Heights" pt.plot(xData=self.systemSizes, yData=averageHeights, plotType='o-', xLab=xLab, yLab=yLab, title=title, legend=legend, log=log) if plotScaled: # Plotting the rescaled heights <h>/L vs L to see signatures of corrections to scaling xLab = r"$L$" yLab = r"$<h(t; L)>_{t}/L$" title = "(Average Height of Pile in Steady State)/" r"$L$" " vs System Size" legend = [ "Scaled Steady State Heights", "Optimized Fit Function " r"$a_0 - bL^{-w_1}$" ] plotTypes = [plotType1, plotType2] optimizeData = [ i for i in range(self.systemSizes[0], self.systemSizes[-1] + 1) ] pt.plot(xData=[self.systemSizes, optimizeData], yData=[scaledHeights, funcs.func1(optimizeData, a0, b, w1)], plotType=plotTypes, \ xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, log=log) if optimize: return averageHeights, scaledHeights, a0, b, w1 # If optimisation was used, the corrections to scaling parameters are also returned else: return averageHeights, scaledHeights # Otherwise just the average and scaled heights are returned
def face_translated(): plot([addn(v, [1, 2]) for v in L], 4)
def AxE_retraining(plt,df_lowpt,df_egamma) : has_gsf = (df_egamma.has_gsf) & (df_egamma.gsf_pt>0.5) & (np.abs(df_egamma.gsf_eta)<2.4) has_ele = (df_egamma.has_ele) & (df_egamma.ele_pt>0.5) & (np.abs(df_egamma.ele_eta)<2.4) # EGamma PF ele print eff1,fr1,_ = plot( plt=plt, df=df_egamma, string="EGamma GSF trk, AxE", selection=has_gsf, draw_roc=False, draw_eff=True, label='EGamma GSF track ($\mathcal{A}\epsilon$)', color='green', markersize=8, linestyle='solid', ) # EGamma PF ele print plot( plt=plt, df=df_egamma, string="EGamma PF ele, AxE", selection=has_ele, draw_roc=False, draw_eff=True, label='EGamma PF ele ($\mathcal{A}\epsilon$)', color='purple', markersize=8, linestyle='solid', ) has_gsf = (df_lowpt.has_gsf) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.ele_pt>0.5) & (np.abs(df_lowpt.ele_eta)<2.4) # Low-pT GSF electrons (PreId unbiased) print plot( plt=plt, df=df_lowpt, string="Low pT GSF trk (PreId), AxE", selection=has_gsf, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ GSF track + unbiased ($\mathcal{A}\epsilon$)', color='red', markersize=8, linestyle='dashed', discriminator=df_lowpt.gsf_bdtout1, ) # Low-pT GSF electrons (CMSSW) print has_ele = (df_lowpt.has_ele) & (df_lowpt.ele_pt>0.5) & (np.abs(df_lowpt.ele_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (CMSSW), AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + 2019Jun28 model ($\mathcal{A}\epsilon$)', color='blue', markersize=8, linestyle='dashdot', discriminator=df_lowpt.ele_mva_value, ) # Low-pT GSF electrons (retraining) print eff2,fr2,roc2 = plot( plt=plt, df=df_lowpt, string="Low pT ele (latest), AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + latest model ($\mathcal{A}\epsilon$)', color='blue', markersize=8, linestyle='solid', discriminator=df_lowpt.training_out, ) roc = (roc2[0]*fr2,roc2[1]*eff2,roc2[2]) idxL = np.abs(roc[0]-fr1).argmin() idxT = np.abs(roc[1]-eff1).argmin() print " PFele: eff/fr/thresh:",\ "{:.3f}/{:.4f}/{:4.2f} ".format(eff1,fr1,np.nan) print " Loose: eff/fr/thresh:",\ "{:.3f}/{:.4f}/{:4.2f} ".format(roc[1][idxL],roc[0][idxL],roc[2][idxL]) print " Tight: eff/fr/thresh:",\ "{:.3f}/{:.4f}/{:4.2f} ".format(roc[1][idxT],roc[0][idxT],roc[2][idxT])
#2-3 #def solve1(a,b,c): return (c-b)/a #print solve1(10+5j, 5, 20) #2-4 #plot(S, 4) #P.48 2.4.2 Task 2.4.3 #plot({1+2j+z for z in S}.union(S), 4) # add original set use union function #P.50 2.4.3 Task 2.4.7 #half #plot({(1+2j+z)/2 for z in S}.union(S), 4) # add original set use union function #180 degree #plot({-(1+2j+z) for z in S}.union(S), 4) # add original set use union function #P.52 2.4.5 Task 2.4.8 #90 degree #plot({-1/2j*(1+2j+z) for z in S}.union(S), 4) # add original set use union function #P.52 2.4.5 Task 2.4.10 data = file2image('img01.png') data = color2gray(data) # if occur error, make comment in "image.py" at line 98 #pts = [[x + y * 1j for x, pixel in enumerate(row) if pixel < 120] for y, row in enumerate(data)] #pts1 = sum(pts, []) #plot(pts1, 200, 1) pts = [[x + y * 1j for x, pixel in enumerate(row) if pixel < 120] for y, row in enumerate(reversed(data))] pts1 = sum(pts, []) plot(pts1, 200, 1)
output = set() for i in reversed(data): for ii in i: NP = NP + 1 if ii < 120: output.add(complex( NP,CP)) NP = 0 CP = CP + 1 return output from plotting import plot S = image2complex("img01.png") plot(S, 200) import image import math def reflectV(S, h): output = set() for i in S: d = abs(i.real - h) if i.real>h: n = i - 2 * d output.add(n) if i.real<h: n = i + 2 * d
def plot2DLine(p1, p2): plot([ addn(scalar_vector_mult(0.01 * i, p1), scalar_vector_mult(1 - 0.01 * i, p2)) for i in range(100) ], 5)
from plotting import plot from math import pi S = { 2 + 2j, 3 + 2j, 1.75 + 1j, 2 + 1j, 2.25 + 1j, 2.5 + 1j, 2.75 + 1j, 3 + 1j, 3.25 + 1j } R = set((2 + 1j) + z for z in S) plot(S, 5) plot(R, 5)
from plotting import plot S = {2+2j,3+2j,1.75+1j,2+1j,2.25+1j,2.5+1j,2.75+1j,3+1j,3.25+1j} plot(S,4) # 7.1 L = [1,2,4,5,7] num = 2 def my_filter(L, num): return [x for x in L if x != num) # 7.2 L = [1,2,4] def my_lists(L): return [[y for y in range(1,x+1)] for x in L] # 7.3 f = {0:'a',1:'b'} g = {'a':'apple', 'b':'banana'} def my_function_composition(f,g): return { key:g[value] for (key,value) in f.items()} # 7.4 def my_sum(L): current = 0 for x in L: current += x return current # 7.5
def AxE_retraining_binned2(plt,df_lowpt,df_egamma) : # EGamma PF ele print has_ele = (df_egamma.has_ele) & (df_egamma.gsf_pt>0.5) & (np.abs(df_egamma.gsf_eta)<2.4) plot( plt=plt, df=df_egamma, string="EGamma PF ele, AxE", selection=has_ele, draw_roc=False, draw_eff=True, label='EGamma PF ele ($\mathcal{A}\epsilon$)', color='purple', markersize=8, linestyle='solid', ) ########## # Low-pT GSF electrons (seeding, pT>2.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (seeding, pT>2.0), AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + seed BDT, $p^{trk}_{T} > 2.0\, GeV$', color='green', markersize=8, linestyle='solid', discriminator=df_lowpt.gsf_bdtout1, mask = (df_lowpt.gsf_pt>2.0), ) # Low-pT GSF electrons (seeding, 1.5<pT<2.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (seeding, 1.5<pT<2.0)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + seed BDT, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='green', markersize=8, linestyle='dashed', discriminator=df_lowpt.gsf_bdtout1, mask = (df_lowpt.gsf_pt>1.5) & (df_lowpt.gsf_pt<2.0), ) # Low-pT GSF electrons (seeding, 1.5<pT<1.5) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (seeding, 1.0<pT<1.5)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + seed BDT, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='green', markersize=8, linestyle='dashdot', discriminator=df_lowpt.gsf_bdtout1, mask = (df_lowpt.gsf_pt>1.0) & (df_lowpt.gsf_pt<1.5), ) # Low-pT GSF electrons (seeding, 0.5<pT<1.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (seeding, 0.5<pT<1.0)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + seed BDT, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='green', markersize=8, linestyle='dotted', discriminator=df_lowpt.gsf_bdtout1, mask = (df_lowpt.gsf_pt>0.5) & (df_lowpt.gsf_pt<1.0), ) ########## # Low-pT GSF electrons (retraining, pT>2.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (retraining, pT>2.0), AxE", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + retrained model, $p^{trk}_{T} > 2.0\, GeV$', color='blue', markersize=8, linestyle='solid', discriminator=df_lowpt.training_out, mask = (df_lowpt.gsf_pt>2.0), ) # Low-pT GSF electrons (retraining, 1.5<pT<2.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (retraining, 1.5<pT<2.0)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + retrained model, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='blue', markersize=8, linestyle='dashed', discriminator=df_lowpt.training_out, mask = (df_lowpt.gsf_pt>1.5) & (df_lowpt.gsf_pt<2.0), ) # Low-pT GSF electrons (retraining, 1.5<pT<1.5) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (retraining, 1.0<pT<1.5)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + retrained model, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='blue', markersize=8, linestyle='dashdot', discriminator=df_lowpt.training_out, mask = (df_lowpt.gsf_pt>1.0) & (df_lowpt.gsf_pt<1.5), ) # Low-pT GSF electrons (retraining, 0.5<pT<1.0) print has_trk = (df_lowpt.has_trk) & (df_lowpt.trk_pt>0.5) & (np.abs(df_lowpt.trk_eta)<2.4) has_ele = (df_lowpt.has_ele) & (df_lowpt.gsf_pt>0.5) & (np.abs(df_lowpt.gsf_eta)<2.4) plot( plt=plt, df=df_lowpt, string="Low pT ele (retraining, 0.5<pT<1.0)", selection=has_ele, draw_roc=True, draw_eff=False, label='Low-$p_{T}$ ele + retrained model, $0.5 < p^{trk}_{T} < 2.0\, GeV$', color='blue', markersize=8, linestyle='dotted', discriminator=df_lowpt.training_out, mask = (df_lowpt.gsf_pt>0.5) & (df_lowpt.gsf_pt<1.0), )
def Task243(): plot({1+2j+z for z in S}, 4) print("plot({1+2j+z for z in S}, 4)...")
def Task241(): plot(S, 4) print("plot(S, 4)...")
def Task248(): plot({z*(-1j)/2 for z in S},4) print("plot({z*(-1j)/2 for z in S},4)...")
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True, log_device_placement=True)) as sess: train_handle = make_handle(sess, train) test_handle = make_handle(sess, test) sess.run(tf.global_variables_initializer()) feed_dict = {handle: train_handle} test_feed_dict = {handle: test_handle} for i in tqdm(range(POPULATION_STEPS), desc="PBT"): # Copy best sess.run([m.copy_from(models[0]) for m in models[-WORST_THRES:]]) # Perturb others sess.run([m.perturb for m in models[BEST_THRES:]]) # Training for _ in tqdm(range(ITERATIONS), desc="Training"): sess.run([m.optimize for m in models], feed_dict) # Evaluate l1_scales = sess.run({m: m.l1_scale for m in models}) accuracies = sess.run({m: m.accuracy for m in models}, test_feed_dict) models.sort(key=lambda m: accuracies[m], reverse=True) # Logging best_accuracy_hist[i] = accuracies[models[0]] best_l1_scale_hist[i] = l1_scales[models[0]] for m in models: l1_scale_hist[m.model_id, i] = l1_scales[m] accuracy_hist[m.model_id, i] = accuracies[m] plot(best_accuracy_hist, nonreg_accuracy_hist, l1_scale_hist)
def Task2411(): def f(z): avg = sum(z)/len(z) return {x-avg for x in z} plot(f(S), 4)
from image import file2image,color2gray from plotting import plot # 2.3.2 L = [[2,2],[3,2],[1.75,1],[2,1],[2.25,1],[2.5,1],[2.75,1],[3,1],[3.25,1]] plot(L,4) def add2(v,w): return [v[0]+w[0],v[1]+w[1]] def addn(v,w): return [v[i] + w[i] for i in range(0,len(v))] # 2.4.3 plot([add2(x,[1,2]) for x in L]) def scalar_vector_mult2(alpha,v): return [alpha*v[x] for x in range(len(v))] def scalar_vector_mult(alpha,v): return [x*alpha for x in v] plot([scalar_vector_mult(-0.5,x) for x in L],16) plot([scalar_vector_mult(i/100,[3,2]) for i in range(101)],5) plot([add2(scalar_vector_mult(i/100,[3,2]),[0.5,1]) for i in range(101)],5)
def Task2418(): pts={x*e**(pi/4*1j) for x in S} plot(pts, 4)
Market_place = "SriKalahasti" state = "Madhya Pradesh" values = dict() for i in folders: globpath = os.path.join(i, '*.xml') #joining the path of only xml files filelist = glob(globpath) f.append(filelist) for i in f: for j in i: #j denotes the file name of the xml file of all the folders tree = ElementTree.parse(str(j)) root = tree.getroot() for k in range(len(root[0][0][0][1][0])): print k xmldict = xmltodict.XmlDictConfig( root[0][0][0][1][0] [k]) #creates an dict with data of kth number print xmldict if xmldict[ "Market"] == Market_place: #sorts according to market place #if xmldict["State"]==state: #sorts according to state values[xmldict["Arrival_Date"]] = xmldict["Modal_x0020_Price"] else: pass List = plotting.dictolist(values) plotting.plot(List)
def Task2419(): data = color2gray((file2image('img01.png'))) row = len(data[0]) col = len(data) pts = {(x+y*1j)*e**(pi/4*1j) for x in range(row) for y in range(col) if data[col-y-1][x]<120} plot(pts, max(row, col), 1)
def gaussians(self, checkSum=False, plot=False, plotType='o-', log=False): """ Calculates the probabilities of getting heights h, P(h; L) for all systems """ allHs = [] allProbs = [] t0 = int(self.tMax - self.N) T = self.tMax - t0 # Setting the number of data points to use for calculating the probabilities lowerLimit = t0 + 1 # The lower index for the heights list to scan upperLimit = t0 + T # The upper index for the heights list to scan for i in range(len(self.systemHeights)): h = [] probs = [] heights = self.systemHeights[i] steady = np.sort( heights[lowerLimit:upperLimit + 1] ) # Only need to scan the heights in the steady state for x in range(min(steady), max(steady) + 1): # Going in ascending order of the heights list h.append(x) count = 0 for y in steady: if y == x: count += 1 # Counting how many times the current height value occurs in the list elif count >= 1: break # Since the list is sorted in ascending order, if the count is >= 1 and isn't increasing further, there are no more heights of this value else: pass # If the height value hasn't been found yet, keep scanning prob = count / float( len(steady) ) # Dividing the counted occurrences of all h values by the total number of heights in the steady state probs.append(prob) h = np.asarray( h, dtype='int64' ) # Converting the lists to numpy arrays for easier manipulation probs = np.asarray(probs, dtype='float64') allHs.append(h) allProbs.append(probs) # Converting the lists to numpy arrays for easier manipulation allHs = np.asarray(allHs) allProbs = np.asarray(allProbs) if checkSum: # Checking to see if all the probabilities add up to 1 ie they are normalised for all system sizes for probs in allProbs: summation = np.sum(probs) print summation if plot: # Plotting the probabilities against h for all systems xLab = r"$h$" yLab = r"$P(h; L)$" title = "Height Probability vs Height Value" legend = ['L = ' + str(i) for i in self.systemSizes] plotTypes = [plotType for value in range(len(allHs))] pt.plot(xData=allHs, yData=allProbs, plotType=plotTypes, xLab=xLab, yLab=yLab, title=title, legend=legend, multiX=True, multiY=True, loc=1, log=log) return allHs, allProbs # Returning the probs with the associated h values
from math import * from plotting import plot def w(n): return e ** (2 * pi * 1j / n) def rotate(z, r): return z * e ** (r * 1j) a = {rotate(w(n), pi/4) for n in range(1, 20)} plot(a, 4)
W_o, v_Wo = update_param(v_Wo, W_o, gamma, lr, dW_o) b_o, v_bo = update_param(v_bo, b_o, gamma, lr, db_o) W_h, v_Wh = update_param(v_Wh, W_h, gamma, lr, dW_h) b_h, v_bh = update_param(v_bh, b_h, gamma, lr, db_h) # calculate losses and accuracies train_loss, train_acc = calculateLoss_Accuracy( forwardPass(trainData, W_o, b_o, W_h, b_h)[3], trainTarget) valid_loss, valid_acc = calculateLoss_Accuracy( forwardPass(validData, W_o, b_o, W_h, b_h)[3], validTarget) test_loss, test_acc = calculateLoss_Accuracy( forwardPass(testData, W_o, b_o, W_h, b_h)[3], testTarget) print( "Epoch: {}, | Training loss: {:.5f} | Validation loss: {:.5f} | Test loss: {:.5f} " "Training Accuracy: {:.5f} | Validation Accuracy: {:.5f} | Test Accuracy: {:.5f} " .format(epoch + 1, train_loss, valid_loss, test_loss, train_acc, valid_acc, test_acc)) train_loss_list.append(train_loss) val_loss_list.append(valid_loss) test_loss_list.append(test_loss) train_acc_list.append(train_acc) val_acc_list.append(valid_acc) test_acc_list.append(test_acc) plot(epochs, train_loss_list, val_loss_list, test_loss_list, train_acc_list, val_acc_list, test_acc_list)