def reset_utils(self): self.utility = resize(0.0, (self.num_squares_x, self.num_squares_y)) for e in self.pits: self.utility[e] = -1.0 e = self.goal self.utility[e] = 1.0
def corrcoef(*args): """ corrcoef(X) where X is a matrix returns a matrix of correlation coefficients for each row of X. corrcoef(x,y) where x and y are vectors returns the matrix or correlation coefficients for x and y. Numeric arrays can be real or complex The correlation matrix is defined from the covariance matrix C as r(i,j) = C[i,j] / (C[i,i]*C[j,j]) """ if len(args) == 2: X = transpose(array([args[0]] + [args[1]])) elif len(args == 1): X = args[0] else: raise RuntimeError, 'Only expecting 1 or 2 arguments' C = cov(X) d = resize(diagonal(C), (2, 1)) r = divide(C, sqrt(matrixmultiply(d, transpose(d))))[0, 1] try: return r.real except AttributeError: return r
def makeTreeArray(self, dec_list=None): """Makes an array with nodes in rows and descendants in columns. A value of 1 indicates that the decendant is a descendant of that node/ A value of 0 indicates that it is not also returns a list of nodes in the same order as they are listed in the array""" #get a list of internal nodes node_list = [node for node in self.traverse() if node.Children] node_list.sort() #get a list of TerminalDescendants Data if one is not supplied if not dec_list: dec_list = [dec.Data for dec in self.TerminalDescendants] dec_list.sort() #make a blank array of the right dimensions to alter result = resize(array([0]), (len(node_list), len(dec_list))) #put 1 in the column for each child of each node for i, node in enumerate(node_list): children = [dec.Data for dec in node.TerminalDescendants] for j, dec in enumerate(dec_list): if dec in children: result[i,j] = 1 return result, node_list
def RenderGradient(surf, topcolor, bottomcolor): '''Creates a new 3d vertical gradient array. This code was copied from the vgrade example''' import pygame from Numeric import array, repeat, resize, arange, \ Float, NewAxis, Int, UnsignedInt8 topcolor = array(topcolor, copy=0) bottomcolor = array(bottomcolor, copy=0) diff = bottomcolor - topcolor width, height = surf.get_size() # create array from 0.0 to 1.0 triplets column = arange(height, typecode=Float)/height column = repeat(column[:, NewAxis], [3], 1) # create a single column of gradient column = topcolor + (diff * column).astype(Int) # make the column a 3d image column by adding X column = column.astype(UnsignedInt8)[NewAxis,:,:] #3d array into 2d array column = pygame.surfarray.map_array(surf, column) # stretch the column into a full image return resize(column, (width, height))
def reset_frequencies(self): self.frequencies = resize(0, (self.num_squares_x, self.num_squares_y))
def reset_reward(self): self.reward = resize(0.0, (self.num_squares_x, self.num_squares_y)) self.reward[self.goal] = 100 for e in self.pits: self.reward[e] = -50
def iagaussian(s,mu,sigma): """ o Purpose Generate a 2D Gaussian image. o Synopsis g = iagaussian(s,mu,sigma) o Input s: [rows columns] mu: Mean vector. 2D point (x;y). Point of maximum value. sigma: covariance matrix (square). [ Sx^2 Sxy; Syx Sy^2] o Output g: o Description A 2D Gaussian image is an image with a Gaussian distribution. It can be used to generate test patterns or Gaussian filters both for spatial and frequency domain. The integral of the gaussian function is 1.0. o Examples import Numeric f = iagaussian([8,4], [3,1], [[1,0],[0,1]]) print Numeric.array2string(f, precision=4, suppress_small=1) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) print g f = iagaussian(100, 50, 10*10) g = ianormalize(f, [0,1]) g,d = iaplot(g) showfig(g) f = iagaussian([50,50], [25,10], [[10*10,0],[0,20*20]]) g = ianormalize(f, [0,255]).astype(Numeric.UnsignedInt8) iashow(g) """ from Numeric import asarray,product,arange,NewAxis,transpose,matrixmultiply,reshape,concatenate,resize,sum,zeros,Float,ravel,pi,sqrt,exp from LinearAlgebra import inverse,determinant if type(sigma).__name__ in ['int', 'float', 'complex']: sigma = [sigma] s, mu, sigma = asarray(s), asarray(mu), asarray(sigma) if (product(s) == max(s)): x = arange(product(s)) d = x - mu if len(d.shape) == 1: tmp1 = d[:,NewAxis] tmp3 = d else: tmp1 = transpose(d) tmp3 = tmp1 if len(sigma) == 1: tmp2 = 1./sigma else: tmp2 = inverse(sigma) k = matrixmultiply(tmp1, tmp2) * tmp3 else: aux = arange(product(s)) x, y = iaind2sub(s, aux) xx = reshape(concatenate((x,y)), (2, product(x.shape))) d = transpose(xx) - resize(reshape(mu,(len(mu),1)), (s[0]*s[1],len(mu))) if len(sigma) == 1: tmp = 1./sigma else: tmp = inverse(sigma) k = matrixmultiply(d, tmp) * d k = sum(transpose(k)) g = zeros(s, Float) aux = ravel(g) if len(sigma) == 1: tmp = sigma else: tmp = determinant(sigma) aux[:] = 1./(2*pi*sqrt(tmp)) * exp(-1./2 * k) return g
def psd(x, NFFT=256, Fs=2, detrend=detrend_none, window=window_hamming, noverlap=0): """ The power spectral density by Welches average periodogram method. The vector x is divided into NFFT length segments. Each segment is detrended by function detrend and windowed by function window. noperlap gives the length of the overlap between segments. The absolute(fft(segment))**2 of each segment are averaged to compute Pxx, with a scaling to correct for power loss due to windowing. Fs is the sampling frequency. -- NFFT must be a power of 2 -- detrend and window are functions, unlike in matlab where they are vectors. -- if length x < NFFT, it will be zero padded to NFFT Refs: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) """ if NFFT % 2: raise ValueError, 'NFFT must be a power of 2' # zero pad x up to NFFT if it is shorter than NFFT if len(x) < NFFT: n = len(x) x = resize(x, (NFFT, )) x[n:] = 0 # for real x, ignore the negative frequencies # if x.typecode()==Complex: numFreqs = NFFT if any(numpy.iscomplex(x)): numFreqs = NFFT else: numFreqs = NFFT // 2 + 1 # windowVals = window(ones((NFFT,),x.typecode())) windowVals = window(numpy.ones(NFFT)) step = NFFT - noverlap ind = range(0, len(x) - NFFT + 1, step) n = len(ind) # Pxx = zeros((numFreqs,n), Float) Pxx = numpy.zeros([numFreqs, n]) # do the ffts of the slices for i in range(n): thisX = x[ind[i]:ind[i] + NFFT] thisX = windowVals * detrend(thisX) fx = absolute(fft(thisX))**2 #print("numFreqs={0:f}".format(numFreqs)) #print("len of fx slice={0:d}".format(len(fx[:int(numFreqs)]))) #print("len of destination in Pxx={0:d}") Pxx[:, i] = fx[:int(numFreqs)] # Scale the spectrum by the norm of the window to compensate for # windowing loss; see Bendat & Piersol Sec 11.5.2 if n > 1: Pxx = mean(Pxx, 1) Pxx = divide(Pxx, norm(windowVals)**2) freqs = Fs / NFFT * arange(0, numFreqs) return Pxx, freqs
def csd(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hamming, noverlap=0): """ The cross spectral density Pxy by Welches average periodogram method. The vectors x and y are divided into NFFT length segments. Each segment is detrended by function detrend and windowed by function window. noverlap gives the length of the overlap between segments. The product of the direct FFTs of x and y are averaged over each segment to compute Pxy, with a scaling to correct for power loss due to windowing. Fs is the sampling frequency. NFFT must be a power of 2 Refs: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) """ if NFFT % 2: raise ValueError, 'NFFT must be a power of 2' # zero pad x and y up to NFFT if they are shorter than NFFT if len(x) < NFFT: n = len(x) x = resize(x, (NFFT, )) x[n:] = 0 if len(y) < NFFT: n = len(y) y = resize(y, (NFFT, )) y[n:] = 0 # for real x, ignore the negative frequencies # if x.typecode()==Complex: numFreqs = NFFT if any(numpy.iscomplex(x)): numFreqs = NFFT else: numFreqs = NFFT // 2 + 1 # windowVals = window(ones((NFFT,),x.typecode())) windowVals = window(numpy.ones(NFFT)) step = NFFT - noverlap ind = range(0, len(x) - NFFT + 1, step) n = len(ind) # Pxy = zeros((numFreqs,n), Complex) Pxy = numpy.zeros([numFreqs, n]) # do the ffts of the slices for i in range(n): thisX = x[ind[i]:ind[i] + NFFT] thisX = windowVals * detrend(thisX) thisY = y[ind[i]:ind[i] + NFFT] thisY = windowVals * detrend(thisY) fx = fft(thisX) fy = fft(thisY) Pxy[:, i] = fy[:numFreqs] * conjugate(fx[:numFreqs]) # Scale the spectrum by the norm of the window to compensate for # windowing loss; see Bendat & Piersol Sec 11.5.2 if n > 1: Pxy = mean(Pxy, 1) Pxy = divide(Pxy, norm(windowVals)**2) freqs = Fs / NFFT * arange(0, numFreqs) return Pxy, freqs
def __init__(self, root, width, height): Tkinter.Toplevel.__init__(self, root) self.inaccessible = [(-1,-1),\ ( 4, 2),( 5, 2),( 6, 2),( 4, 3),( 5, 3),( 6, 3),\ ( 9, 2),(10, 2),(11, 2),( 9, 3),(10, 3),(11, 3),( 9, 4),(10, 4),(11, 4),\ (13, 1),(14, 1),(13, 2),(14, 2),(13, 3),(14, 3),(13, 4),(14, 4),\ (3,6),(4,6),(5,6),(3,7),(4,7),(5,7),(3,8),(4,8),(5,8),(3,9),(4,9),(5,9),\ (7,6),(8,6), (7,7),(8,7), (7,8),(8,8), \ (6,9),(7,9),(8,9), (6,10),(7,10),(8,10),(9,10),(10,10),(6,11),(7,11),\ (8,11),(9,11),(10,11),(6,12),(7,12),(8,12),(9,12),(6,13),(7,13),(8,13),\ (11,6),(12,6),(13,6),(11,7),(12,7),(13,7),(11,8),(12,8),(13,8),\ (0,11),(1,11),(2,11),(0,12),(1,12),(2,12),(0,13),(1,13),(2,13),(0,14),(1,14),(2,14)]; self.path_color = "#5ee563" self.visited_color = "#c5c5c5" self.current_pos_color = "#00AF32" self.inaccessible_color= "black" self.gridline_color = "black" self.background_color = "white" self.path = [] self.visited = [] self.pits = [] self.goal = [] self.goal_id = -1 self.pit_ids = [] # how many states ? self.num_squares_x = 15 self.num_squares_y = 15 self.squares = resize( 0,(self.num_squares_x,self.num_squares_y)); # various object members self.done = 0 self.quit = 0 self.root = root self.width = width self.height = height self.complete = 0 # various tk objects self.title("SymbolicSimulator: RLWorld") self.canvas = Tkinter.Canvas(self,width=self.width,height=self.height,bg="black") self.canvas.pack() self.winfo_toplevel().protocol('WM_DELETE_WINDOW',self.destroy) # set height and width of images self.square_height = self.width / self.num_squares_x; self.square_width = self.height / self.num_squares_y; # goal image goldFilename = pyrobotdir() + "/images/rlgoal.gif" goldImage = Image.open(goldFilename) goldImage = goldImage.resize( [self.square_height-2, self.square_width-2] ) self.goldImageTk = ImageTk.PhotoImage(goldImage) # pit image pitFilename = pyrobotdir() + "/images/rlpit.gif" pitImage = Image.open(pitFilename) pitImage = pitImage.resize( [self.square_height-2, self.square_width-2] ) self.pitImageTk = ImageTk.PhotoImage(pitImage, height=self.square_height, width=self.square_width) for i in range(0, self.num_squares_x): for j in range(0, self.num_squares_y): self.squares[i][j] = self.canvas.create_rectangle( i*self.square_width, j*self.square_height, (i+1)*self.square_width - 1, (j+1)*self.square_height - 1, fill= self.background_color, tag = "square-%d-%d" % (i,j)); # initialize the world self.initWorld() self.resetStates() # used by simulator self.properties = ["location", "obstacles", "goal", "home", \ "final", "visited", "complete", \ "pits", "path"] self.movements = ["up", "right", "down", "left"] self.ports = [60000] # start things off self.redraw() self.drawInaccessible()
def __init__(self, root, width, height): Tkinter.Toplevel.__init__(self, root) self.inaccessible = [(-1,-1),\ ( 4, 2),( 5, 2),( 6, 2),( 4, 3),( 5, 3),( 6, 3),\ ( 9, 2),(10, 2),(11, 2),( 9, 3),(10, 3),(11, 3),( 9, 4),(10, 4),(11, 4),\ (13, 1),(14, 1),(13, 2),(14, 2),(13, 3),(14, 3),(13, 4),(14, 4),\ (3,6),(4,6),(5,6),(3,7),(4,7),(5,7),(3,8),(4,8),(5,8),(3,9),(4,9),(5,9),\ (7,6),(8,6), (7,7),(8,7), (7,8),(8,8), \ (6,9),(7,9),(8,9), (6,10),(7,10),(8,10),(9,10),(10,10),(6,11),(7,11),\ (8,11),(9,11),(10,11),(6,12),(7,12),(8,12),(9,12),(6,13),(7,13),(8,13),\ (11,6),(12,6),(13,6),(11,7),(12,7),(13,7),(11,8),(12,8),(13,8),\ (0,11),(1,11),(2,11),(0,12),(1,12),(2,12),(0,13),(1,13),(2,13),(0,14),(1,14),(2,14)] self.path_color = "#5ee563" self.visited_color = "#c5c5c5" self.current_pos_color = "#00AF32" self.inaccessible_color = "black" self.gridline_color = "black" self.background_color = "white" self.path = [] self.visited = [] self.pits = [] self.goal = [] self.goal_id = -1 self.pit_ids = [] # how many states ? self.num_squares_x = 15 self.num_squares_y = 15 self.squares = resize(0, (self.num_squares_x, self.num_squares_y)) # various object members self.done = 0 self.quit = 0 self.root = root self.width = width self.height = height self.complete = 0 # various tk objects self.title("SymbolicSimulator: RLWorld") self.canvas = Tkinter.Canvas(self, width=self.width, height=self.height, bg="black") self.canvas.pack() self.winfo_toplevel().protocol('WM_DELETE_WINDOW', self.destroy) # set height and width of images self.square_height = self.width / self.num_squares_x self.square_width = self.height / self.num_squares_y # goal image goldFilename = os.environ["PYROBOT"] + "/images/rlgoal.gif" goldImage = Image.open(goldFilename) goldImage = goldImage.resize( [self.square_height - 2, self.square_width - 2]) self.goldImageTk = ImageTk.PhotoImage(goldImage) # pit image pitFilename = os.environ["PYROBOT"] + "/images/rlpit.gif" pitImage = Image.open(pitFilename) pitImage = pitImage.resize( [self.square_height - 2, self.square_width - 2]) self.pitImageTk = ImageTk.PhotoImage(pitImage, height=self.square_height, width=self.square_width) for i in range(0, self.num_squares_x): for j in range(0, self.num_squares_y): self.squares[i][j] = self.canvas.create_rectangle( i * self.square_width, j * self.square_height, (i + 1) * self.square_width - 1, (j + 1) * self.square_height - 1, fill=self.background_color, tag="square-%d-%d" % (i, j)) # initialize the world self.initWorld() self.resetStates() # used by simulator self.properties = ["location", "obstacles", "goal", "home", \ "final", "visited", "complete", \ "pits", "path"] self.movements = ["up", "right", "down", "left"] self.ports = [60000] # start things off self.redraw() self.drawInaccessible()