def dist2(x): R, GSIGMAS = pylab.meshgrid(r[r < fit_rcutoff], gsigmas) g = pylab.zeros_like(GSIGMAS) g = evalg(x, GSIGMAS, R) gfit = pylab.reshape(g, len(eta) * len(r[r < fit_rcutoff])) return gfit - pylab.reshape([g[r < fit_rcutoff] for g in ghs], len(gsigmas) * len(r[r < fit_rcutoff]))
def dist2(x): R, GSIGMAS = pylab.meshgrid(r[r<fit_rcutoff], gsigmas) g = pylab.zeros_like(GSIGMAS) g = evalg(x, GSIGMAS, R) gfit = pylab.reshape(g, len(eta)*len(r[r<fit_rcutoff])) return gfit - pylab.reshape([g[r<fit_rcutoff] for g in ghs], len(gsigmas)*len(r[r<fit_rcutoff]))
def loadMNISTImages(filename): f = open(filename, 'rb') # Verify Magic Number s = f.read(4) magic = int(s.encode('hex'),16) assert(magic == 2051) # Get Number of Images s = f.read(4) numImages = int(s.encode('hex'),16) s = f.read(4) numRows = int(s.encode('hex'),16) s = f.read(4) numCols = int(s.encode('hex'),16) # Get Data s = f.read() a = frombuffer(s, uint8) # Use 'F' to ensure that we read by column a = reshape(a, (numCols , numRows, numImages), order='F'); images = transpose(a, (1, 0, 2)) f.close() # Reshape to #pixels * #examples images = reshape(a, (shape(images)[0] * shape(images)[1], numImages), order='F'); images = double(images)/255 return images
def dist2(x): R, ETA = pylab.meshgrid(r[r<fit_rcutoff], eta) g = pylab.zeros_like(ETA) g = evalg(x, ETA, R) gfit = pylab.reshape(g, len(eta)*len(r[r<fit_rcutoff])) return gfit - pylab.reshape([g[r<fit_rcutoff] for g in ghs], len(eta)*len(r[r<fit_rcutoff]))
def getParamCovMat(prefix,dlogpower = 2, theoconstmult = 1.,dlogfilenames = ['dlogpnldloga.dat'],volume=256.**3,startki = 0, endki = 0, veff = [0.]): """ Calculates parameter covariance matrix from the power spectrum covariance matrix and derivative term in the prefix directory """ nparams = len(dlogfilenames) kpnl = M.load(prefix+'pnl.dat') k = kpnl[startki:,0] nk = len(k) if (endki == 0): endki = nk pnl = M.array(kpnl[startki:,1],M.Float64) covarwhole = M.load(prefix+'covar.dat') covar = covarwhole[startki:,startki:] if len(veff) > 1: sqrt_veff = M.sqrt(veff[startki:]) else: sqrt_veff = M.sqrt(volume*M.ones(nk)) dlogs = M.reshape(M.ones(nparams*nk,M.Float64),(nparams,nk)) paramFishMat = M.reshape(M.zeros(nparams*nparams*(endki-startki),M.Float64),(nparams,nparams,endki-startki)) paramCovMat = paramFishMat * 0. # Covariance matrices of dlog's for param in range(nparams): if len(dlogfilenames[param]) > 0: dlogs[param,:] = M.load(prefix+dlogfilenames[param])[startki:,1] normcovar = M.zeros(M.shape(covar),M.Float64) for i in range(nk): normcovar[i,:] = covar[i,:]/(pnl*pnl[i]) M.save(prefix+'normcovar.dat',normcovar) f = k[1]/k[0] if (volume == -1.): volume = (M.pi/k[0])**3 #theoconst = volume * k[1]**3 * f**(-1.5)/(12.*M.pi**2) #1 not 0 since we're starting at 1 for ki in range(1,endki-startki): for p1 in range(nparams): for p2 in range(nparams): paramFishMat[p1,p2,ki] = M.sum(M.sum(\ M.inverse(normcovar[:ki+1,:ki+1]) * M.outerproduct(dlogs[p1,:ki+1]*sqrt_veff[:ki+1],\ dlogs[p2,:ki+1]*sqrt_veff[:ki+1]))) paramCovMat[:,:,ki] = M.inverse(paramFishMat[:,:,ki]) return k[1:],paramCovMat[:,:,1:]
def draw_domain(f, x0, y0, x1, y1): number_of_levels = 41 n = 101 x = pylab.linspace(x0, x1, n) y = pylab.linspace(y0, y1, n) xx = (pylab.reshape(x, (n, 1)) * pylab.ones(n)).transpose() yy = pylab.reshape(y, (n, 1)) * pylab.ones(n) z = f([xx, yy]) pylab.ion() pylab.contour(x, y, z, number_of_levels) pylab.draw()
def readDatDirectory(key, directory): global stats #Don't read data in if it's already read if not key in DATA["mean"]: data = defaultdict(array) #Process the dat files for datfile in glob.glob(directory + "/*.dat"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Process the div files' for datfile in glob.glob(directory + "/*.div"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Iterate through the stats and calculate mean/standard deviation for aKey in stats: if aKey in data: DATA["mean"][key][aKey] = mean(data[aKey], axis=0) DATA["median"][key][aKey] = median(data[aKey], axis=0) DATA["std"][key][aKey] = std(data[aKey], axis=0) DATA["ste"][key][aKey] = std(data[aKey], axis=0) / sqrt( len(data[aKey])) DATA["min"][key][aKey] = mean(data[aKey], axis=0) - amin( data[aKey], axis=0) DATA["max"][key][aKey] = amax(data[aKey], axis=0) - mean( data[aKey], axis=0) DATA["actual"][key][aKey] = data[aKey]
def f_l2_wd(x0, S, I, gamma): M = shape(S)[0] L, batch_size = shape(I) A = matrix(reshape(x0,[L,M])) E = I - A*S f = 0.5*(E.A**2).sum()/batch_size + 0.5*gamma*(A.A**2).sum() return f
def render_network(A): [L, M] = shape(A) sz = int(sqrt(L)) buf = 1 A = asarray(A) if floor(sqrt(M)) ** 2 != M: m = int(sqrt(M / 2)) n = M / m else: m = int(sqrt(M)) n = m array = -ones([buf + m * (sz + buf), buf + n * (sz + buf)], "d") k = 0 for i in range(m): for j in range(n): clim = max(abs(A[:, k])) x_offset = buf + i * (sz + buf) y_offset = buf + j * (sz + buf) array[x_offset : x_offset + sz, y_offset : y_offset + sz] = reshape(A[:, k], [sz, sz]) / clim k += 1 return array
def homog3D (points2d, points3d): """ Compute a matrix relating homogeneous 3D points (4xN) to homogeneous 2D points (3xN) Not sure why anyone would do this. Note that the returned transformation *NOT* an isometry. But it's here... so deal with it. """ numPoints = points2d.shape[1] assert (numPoints >= 4) A = None for i in range (0, numPoints): xiPrime = points2d[:,i] xi = points3d[:,i] Ai_row0 = pl.concatenate ((pl.zeros (4,), -xiPrime[2]*xi, xiPrime[1]*xi)) Ai_row1 = pl.concatenate ((xiPrime[2]*xi, pl.zeros (4,), -xiPrime[0]*xi)) Ai = pl.row_stack ((Ai_row0, Ai_row1)) if A is None: A = Ai else: A = pl.vstack ((A, Ai)) U, S, V = pl.svd (A) V = V.T h = V[:,-1] P = pl.reshape (h, (3, 4)) return P
def homog2D(xPrime, x): """ Compute the 3x3 homography matrix mapping a set of N 2D homogeneous points (3xN) to another set (3xN) """ numPoints = xPrime.shape[1] assert numPoints >= 4 A = None for i in range(0, numPoints): xiPrime = xPrime[:, i] xi = x[:, i] Ai_row0 = pl.concatenate((pl.zeros(3), -xiPrime[2] * xi, xiPrime[1] * xi)) Ai_row1 = pl.concatenate((xiPrime[2] * xi, pl.zeros(3), -xiPrime[0] * xi)) Ai = pl.row_stack((Ai_row0, Ai_row1)) if A is None: A = Ai else: A = pl.vstack((A, Ai)) U, S, V = pl.svd(A) V = V.T h = V[:, -1] H = pl.reshape(h, (3, 3)) return H
def homog3D(points2d, points3d): """ Compute a matrix relating homogeneous 3D points (4xN) to homogeneous 2D points (3xN) Not sure why anyone would do this. Note that the returned transformation *NOT* an isometry. But it's here... so deal with it. """ numPoints = points2d.shape[1] assert numPoints >= 4 A = None for i in range(0, numPoints): xiPrime = points2d[:, i] xi = points3d[:, i] Ai_row0 = pl.concatenate((pl.zeros(4), -xiPrime[2] * xi, xiPrime[1] * xi)) Ai_row1 = pl.concatenate((xiPrime[2] * xi, pl.zeros(4), -xiPrime[0] * xi)) Ai = pl.row_stack((Ai_row0, Ai_row1)) if A is None: A = Ai else: A = pl.vstack((A, Ai)) U, S, V = pl.svd(A) V = V.T h = V[:, -1] P = pl.reshape(h, (3, 4)) return P
def plot_hist(X,Y,title,name): # get list of tracks and list of labels xs = X.values ys = Y.values ys = pl.reshape(ys,[ys.shape[0],]) pl.figure(figsize=(15, 6), dpi=100) for i in range(many_features): if (i==2): counts0, bins0 = pl.histogram(xs[ys==0,i],100,range=(0.,0.08)) counts1, bins1 = pl.histogram(xs[ys==1,i],100,range=(0.,0.08)) elif (i==5): counts0, bins0 = pl.histogram(xs[ys==0,i],100,range=(1,5)) counts1, bins1 = pl.histogram(xs[ys==1,i],100,range=(1,5)) elif (i==6): counts0, bins0 = pl.histogram(xs[ys==0,i],100,range=(0,15)) counts1, bins1 = pl.histogram(xs[ys==1,i],100,range=(0,15)) elif (i==7): counts0, bins0 = pl.histogram(xs[ys==0,i],100,range=(-1.5,1.)) counts1, bins1 = pl.histogram(xs[ys==1,i],100,range=(-1.5,1.)) else: counts0, bins0 = pl.histogram(xs[ys==0,i],100) counts1, bins1 = pl.histogram(xs[ys==1,i],100) pl.hold() pl.subplot(2,4,i+1) pl.plot(bins0[0:100],counts0,'r',bins1[0:100],counts1,'b') pl.title(feature_names[i]) pl.tight_layout() pl.savefig("../out/{0}/{1}".format(WHICH_EXP,name),bbox_inches='tight')
def homog2D (xPrime, x): """ Compute the 3x3 homography matrix mapping a set of N 2D homogeneous points (3xN) to another set (3xN) """ numPoints = xPrime.shape[1] assert (numPoints >= 4) A = None for i in range (0, numPoints): xiPrime = xPrime[:,i] xi = x[:,i] Ai_row0 = pl.concatenate ((pl.zeros (3,), -xiPrime[2]*xi, xiPrime[1]*xi)) Ai_row1 = pl.concatenate ((xiPrime[2]*xi, pl.zeros (3,), -xiPrime[0]*xi)) Ai = pl.row_stack ((Ai_row0, Ai_row1)) if A is None: A = Ai else: A = pl.vstack ((A, Ai)) U, S, V = pl.svd (A) V = V.T h = V[:,-1] H = pl.reshape (h, (3, 3)) return H
def read_big_data(filename, type=None, format='ascii'): if format == 'ascii': data = filename.replace('.cfg', '_' + type + '.dat') elif format == 'hdf5': data = filename.replace('.cfg', '.' + type + '.h5') try: A = plt.np.load(data.replace('.dat', '.npy')) except IOError: if format == 'ascii': n_blocks = get_first_newline(data) A = plt.loadtxt(data).T [n_cols, n_turns, n_blocks] = [A.shape[0], A.shape[1] / n_blocks, n_blocks] A = plt.reshape(A, (n_cols, n_turns, n_blocks)) A = plt.rollaxis(A, 1, 3) plt.np.save(data.replace('.dat', '.npy'), A) elif format == 'hdf5': A = h5py.File(data, 'r') turns = natsort.natsorted(A.keys(), signed=False) cols = ['z0', 'x', 'xp', 'y', 'yp', 'z', 'zp'] try: n_cols, n_particles, n_turns = len(cols), len(A[turns[0]][cols[0]]), len(turns) B = plt.zeros((n_cols, n_particles, n_turns)) for i, d in enumerate(cols): for j, g in enumerate(turns): B[i, :, j] = A[g][d] except KeyError: B = A['Fields']['Data'][:].T A = B return A, data
def g_l2_wd(x0, S, I, gamma): M = shape(S)[0] L, batch_size = shape(I) A = matrix(reshape(x0,[L,M])) E = I - A*S g = -E*S.T/batch_size + gamma*A return g.A1
def retrieve_result(self, filename): """ retrieve_many_result(filename) Import the result of the dynamics as a class variable. Parameters ---------- filename : name of the result file. """ if filename[-3:] == "txt": raw_result = numpy.loadtxt("%s/%s"%(self.path_to_output_dir, filename)) numpy.save("%s/%s"%(self.path_to_output_dir, filename[:-4]), raw_result) else: raw_result = numpy.load("%s/%s"%(self.path_to_output_dir, filename)) self.times = raw_result[:,0] self.field = raw_result[:,1] raw_result = raw_result[:,2:] self.psi = zeros([self.vib_basis_size, self.el_basis_size, raw_result.shape[0]], dtype = complex) for index in range(raw_result.shape[0]): self.psi[:,:,index] = reshape(raw_result[index, :self.basis_size] + 1j * raw_result[index, self.basis_size:], [self.vib_basis_size, self.el_basis_size], order = "F") self.psi_final = self.psi[:,:,-1]
def simulate(self, T): """Simulates the full neural field model Arguments ---------- T: ndarray simulation time instants Returns ---------- V: list of matrix each matrix is the neural field at a time instant Y: list of matrix each matrix is the observation vector corrupted with noise at a time instant """ Y = [] V = [] spatial_location_num = (len(self.field_space)) ** 2 sim_field_space_len = len(self.field_space) # initial field v0 = self.Sigma_e_c * pb.matrix(np.random.randn(spatial_location_num, 1)) v_membrane = pb.reshape(v0, (sim_field_space_len, sim_field_space_len)) firing_rate = self.act_fun.fmax / (1.0 + pb.exp(self.act_fun.varsigma * (self.act_fun.v0 - v_membrane))) for t in T[1:]: v = self.Sigma_varepsilon_c * pb.matrix(np.random.randn(len(self.obs_locns), 1)) w = pb.reshape( self.Sigma_e_c * pb.matrix(np.random.randn(spatial_location_num, 1)), (sim_field_space_len, sim_field_space_len), ) print "simulation at time", t g = signal.fftconvolve(self.K, firing_rate, mode="same") g *= self.spacestep ** 2 v_membrane = self.Ts * pb.matrix(g) + self.xi * v_membrane + w firing_rate = self.act_fun.fmax / (1.0 + pb.exp(self.act_fun.varsigma * (self.act_fun.v0 - v_membrane))) # Observation Y.append((self.spacestep ** 2) * (self.C * pb.reshape(v_membrane, (sim_field_space_len ** 2, 1))) + v) V.append(v_membrane) return V, Y
def simulate(self, T): """Simulates the full neural field model Arguments ---------- T: ndarray simulation time instants Returns ---------- V: list of matrix each matrix is the neural field at a time instant Y: list of matrix each matrix is the observation vector corrupted with noise at a time instant """ Y = [] V = [] spatial_location_num = (len(self.simulation_space_x_y))**2 sim_field_space_len = len(self.simulation_space_x_y) #initial field v0 = pb.dot(self.Sigma_e_c, np.random.randn(spatial_location_num, 1)) v_membrane = pb.reshape(v0, (sim_field_space_len, sim_field_space_len)) for t in T[1:]: v = pb.dot(self.Sigma_varepsilon_c, np.random.randn(len(self.obs_locns), 1)) w = pb.reshape( pb.dot(self.Sigma_e_c, np.random.randn(spatial_location_num, 1)), (sim_field_space_len, sim_field_space_len)) #print "simulation at time",t g = signal.fftconvolve(self.K, v_membrane, mode='same') g *= (self.spacestep**2) v_membrane = g + w #Observation Y.append((self.spacestep**2) * (pb.dot( self.C, pb.reshape(v_membrane, (sim_field_space_len**2, 1)))) + v) V.append(v_membrane) return V, Y
def readDatDirectory(key, directory): global stats #Don't read data in if it's already read if not key in DATA["mean"]: data = defaultdict(array) #Process the dat files for datfile in glob.glob(directory + "/*.dat"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Process the div files' for datfile in glob.glob(directory + "/*.div"): fileHandle = open(datfile, 'rb') keys, dataDict = csvExtractAllCols(fileHandle) stats = union(stats, keys) for aKey in keys: if not aKey in data: data[aKey] = reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))) else: data[aKey] = append(data[aKey], reshape(array(dataDict[aKey]), (1, len(dataDict[aKey]))), axis=0) #Iterate through the stats and calculate mean/standard deviation for aKey in stats: if aKey in data: DATA["mean"][key][aKey] = mean(data[aKey], axis=0) DATA["median"][key][aKey] = median(data[aKey], axis=0) DATA["std"][key][aKey] = std(data[aKey], axis=0) DATA["ste"][key][aKey] = std(data[aKey], axis=0)/ sqrt(len(data[aKey])) DATA["min"][key][aKey] = mean(data[aKey], axis=0)-amin(data[aKey], axis=0) DATA["max"][key][aKey] = amax(data[aKey], axis=0)-mean(data[aKey], axis=0) DATA["actual"][key][aKey] = data[aKey]
def objfun_l2_wd(x0, S, I, gamma): M = shape(S)[0] L, batch_size = shape(I) A = matrix(reshape(x0,[L,M])) E = I - A*S f = 0.5*(E.A**2).sum()/batch_size + 0.5*gamma*(A.A**2).sum() g = -E*S.T/batch_size + gamma*A return (f,g.A1)
def load_data(filename): state="end" dat=Data() valdep=[] infile=open(filename) for line in infile.readlines(): tagfnd=re.search("\<([^<>]*)\>",line) if tagfnd: tag=tagfnd.group(1) tag.strip() if re.search("Qucs Dataset ",line): continue if tag[0]=="/": state="end" print "Number of Dimensions:",len(valdep) if len(valdep)>1: shape=[] for i in range(len(valdep),0,-1): shape.append(dat.__dict__[valdep[i-1]].len) val=pylab.array(val) val=pylab.reshape(val,shape) dat.__dict__[name]=val else: state="start" words=tag.split() type=words[0] name=words[1].replace(".","_") name=name.replace(",","") name=name.replace("[","") name=name.replace("]","") if type=="indep": val=Val("f") val.len=int(words[2]) else: val=[] valdep=words[2:] else: if state=="start": if "j" in line: print line line=line.replace("j","") line="%sj"%line.strip() try: val.append(complex(line)) except: traceback.print_exc() print line # add nan check print name print len(val) else: val.append(float(line)) else: print "Parser Error:",line return dat
def load_data(filename): state="end" dat=Data() valdep=[] infile=open(filename) for line in infile.readlines(): tagfnd=re.search("\<([^<>]*)\>",line) if tagfnd: tag=tagfnd.group(1) tag.strip() if re.search("Qucs Dataset ",line): continue if tag[0]=="/": state="end" print("Number of Dimensions:",len(valdep)) if len(valdep)>1: shape=[] for i in range(len(valdep),0,-1): shape.append(dat.__dict__[valdep[i-1]].len) val=pylab.array(val) val=pylab.reshape(val,shape) dat.__dict__[name]=val else: state="start" words=tag.split() type=words[0] name=words[1].replace(".","_") name=name.replace(",","") name=name.replace("[","") name=name.replace("]","") if type=="indep": val=Val("f") val.len=int(words[2]) else: val=[] valdep=words[2:] else: if state=="start": if "j" in line: print(line) line=line.replace("j","") line="%sj"%line.strip() try: val.append(complex(line)) except: traceback.print_exc() print(line) # add nan check print(name) print(len(val)) else: val.append(float(line)) else: print("Parser Error:",line) return dat
def simulate(self,T): """Simulates the full neural field model Arguments ---------- T: ndarray simulation time instants Returns ---------- V: list of matrix each matrix is the neural field at a time instant Y: list of matrix each matrix is the observation vector corrupted with noise at a time instant """ Y=[] V=[] spatial_location_num=(len(self.simulation_space_x_y))**2 sim_field_space_len=len(self.simulation_space_x_y) #initial field v0=pb.dot(self.Sigma_e_c,np.random.randn(spatial_location_num,1)) v_membrane=pb.reshape(v0,(sim_field_space_len,sim_field_space_len)) for t in T[1:]: v = pb.dot(self.Sigma_varepsilon_c,np.random.randn(len(self.obs_locns),1)) w = pb.reshape(pb.dot(self.Sigma_e_c,np.random.randn(spatial_location_num,1)),(sim_field_space_len,sim_field_space_len)) #print "simulation at time",t g=signal.fftconvolve(self.K,v_membrane,mode='same') g*=(self.spacestep**2) v_membrane=g+w #Observation Y.append((self.spacestep**2)*(pb.dot(self.C,pb.reshape(v_membrane,(sim_field_space_len**2,1))))+v) V.append(v_membrane) return V,Y
def excitation_emission_cubes_to_spectra(arr): new = [] for im in range(len(arr)): a = arr[im] x = a.shape[1] y = a.shape[2] z = a.shape[0] r = pl.reshape(a, (z, x * y)) new.append(r) new = pl.array(new) return (new)
def __call__(self, lon, lat, inverse=False): """ Calling a Proj class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y native map projection coordinates (in meters). If optional keyword 'inverse' is True (default is False), the inverse transformation from x/y to lon/lat is performed. For cylindrical equidistant projection ('cyl'), this does nothing (i.e. x,y == lon,lat). lon,lat can be either scalar floats or N arrays. """ if self.projection == "cyl": # for cyl x,y == lon,lat return lon, lat # if inputs are numarray arrays, get shape and typecode. try: shapein = lon.shape lontypein = lon.typecode() lattypein = lat.typecode() except: shapein = False # make sure inputs have same shape. if shapein and lat.shape != shapein: raise ValueError, "lon, lat must be scalars or numarrays with the same shape" if shapein: x = N.ravel(lon).tolist() y = N.ravel(lat).tolist() if inverse: outx, outy = self._inv(x, y) else: outx, outy = self._fwd(x, y) outx = N.reshape(N.array(outx, lontypein), shapein) outy = N.reshape(N.array(outy, lattypein), shapein) else: if inverse: outx, outy = self._inv(lon, lat) else: outx, outy = self._fwd(lon, lat) return outx, outy
def __call__(self, lon, lat, inverse=False): """ Calling a Proj class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y native map projection coordinates (in meters). If optional keyword 'inverse' is True (default is False), the inverse transformation from x/y to lon/lat is performed. For cylindrical equidistant projection ('cyl'), this does nothing (i.e. x,y == lon,lat). lon,lat can be either scalar floats or N arrays. """ if self.projection == 'cyl': # for cyl x,y == lon,lat return lon, lat # if inputs are numarray arrays, get shape and typecode. try: shapein = lon.shape lontypein = lon.typecode() lattypein = lat.typecode() except: shapein = False # make sure inputs have same shape. if shapein and lat.shape != shapein: raise ValueError, 'lon, lat must be scalars or numarrays with the same shape' if shapein: x = N.ravel(lon).tolist() y = N.ravel(lat).tolist() if inverse: outx, outy = self._inv(x, y) else: outx, outy = self._fwd(x, y) outx = N.reshape(N.array(outx, lontypein), shapein) outy = N.reshape(N.array(outy, lattypein), shapein) else: if inverse: outx, outy = self._inv(lon, lat) else: outx, outy = self._fwd(lon, lat) return outx, outy
def choose_patches(IMAGES, L, batch_size=1000): sz = int(sqrt(L)) imsz = shape(IMAGES)[0] num_images = shape(IMAGES)[2] BUFF = 4 X = matrix(zeros([L,batch_size],'d')) for i in range(batch_size): j = int(floor(num_images * rand())) r = sz/2+BUFF+int(floor((imsz-sz-2*BUFF)*rand())) c = sz/2+BUFF+int(floor((imsz-sz-2*BUFF)*rand())) X[:,i] = reshape(IMAGES[r-sz/2:r+sz/2,c-sz/2:c+sz/2,j],[L,1]) return X
def getDissim(data, atype, vbose=0, minRank=0, maxRank=50, NPOZ=50): ks = data.keys() matr = pylab.ones(len(ks)**2) matr = pylab.reshape(matr, (len(ks), len(ks))) scs = [] names = [] for ik, k_con in enumerate(ks): name = ik if not k_con in names: names.append(k_con) for jk, k_pl in enumerate(ks): ss1 = computeSimSc(data, k_con, k_pl, vbose=vbose, minRank=minRank, maxRank=maxRank, NPOZ=NPOZ) ss2 = computeSimSc(data, k_pl, k_con, vbose=vbose, minRank=minRank, maxRank=maxRank, NPOZ=NPOZ) if atype=='abs': sc1 = sum(ss1) sc2 = sum(ss2) elif atype=='rms': sc1 = pylab.sqrt(pylab.sum(ss1**2)) sc2 = pylab.sqrt(pylab.sum(ss2**2)) elif atype=='met': sc1 = sum(pylab.logical_and(ss1!=0, True)) sc2 = sum(pylab.logical_and(ss2!=0, True)) if vbose>=1: print 'score for ', k_con, k_pl, ss1, sc1, ss2, sc2 oldsc = sc1 + sc2 oldsc *= 0.5 l1 = len(data[k_con]) l2 = len(data[k_pl]) iscale = min(l1, l2) nsc = oldsc/(1.0*iscale) if vbose>=1: print k_con, k_pl, 'oldsc', oldsc, l1, l2, iscale, 'nsc', nsc matr[ik][jk] = nsc if jk<=ik: continue print nsc, 'xx', ik, k_con, jk, k_pl scs.append(nsc) return names, pylab.array(scs), matr
def plotit(x, y, z, title): plb.figure() try: X = list() Y = list() [X.append(i) for i in x if i not in X] [Y.append(i) for i in y if i not in Y] Z = plb.reshape(z, (len(Y), len(X))) plb.contourf(X, Y, Z, 10) except: plb.scatter(x, y, c=z) plb.title(title) plb.colorbar() plb.show()
def sumDotProd(self, start1, end1, start2, end2, k, cosTheta): """ Calculates (k_start1 + ... + k_end1) . (k_start2 + ... + k_end2) k: [|k0|,|k1|,|k2| ...] thetarr: [[cos(theta_k0,k0),cos(theta_k0,k1),...] [cos(theta_k1,k0), ...]] ...] """ start1 -= 1 start2 -= 1 # Account for Python non-inclusive last element of array nentries = (end1 - start1) * (end2 - start2) # print start1,end1,start2,end2 # print M.outer(k[start1:end1],k[start2:end2]) ans = M.sum(M.reshape(M.outer(k[start1:end1], k[start2:end2]) * cosTheta[start1:end1, start2:end2], nentries)) return ans
def getHaloBiasCoef(h): """ Halo bias parameters (Mo et al (1997), Cooray & Sheth, sect. 3.4) """ a1 = 1. a2 = -17./21. a3 = 341./567. a4 = -55805./130977. delta2 = h.delta*h.delta sigma2 = h.sigma*h.sigma nu2 = delta2/sigma2 # notation as in Cooray & Sheth, sect. 3.4 # ST a -> CS q # ST q -> CS p # ST A -> CS A cs_q = h.p.st_little_a cs_p = h.p.stq epsilon1 = (cs_q*nu2 - 1.)/h.deltaz0 epsilon2 = cs_q*nu2*(cs_q*nu2-3.)/h.deltaz0**2 e1 = 2.*cs_p/h.deltaz0/(1.+(cs_q*nu2)**cs_p) e2 = e1 * ((1.+2.*cs_p)/h.deltaz0 + 2.*epsilon1) # use h.deltaz0, not h.delta here (bug fix from Sebastien Heinis) b = M.reshape(1.*M.ones(3*len(h.sigma)),(3,len(h.sigma))) # already took care of b[0,:] b[1,:] = 1. + epsilon1 + e1 b[2,:] = 2.*(1.+a2)*(epsilon1+e1) + epsilon2 + e2 # This is what's in Mo et all (1997); this is just for the Press-Schechter case #b[1,:] = (nu2-1.)/h.delta + 1. #b[2,:] = 2.*(1.+a2)*(nu2-1)/h.delta + (nu2-3.)/sigma2 #b[3,:] = 6.*(a2+a3)*(nu2-1)/h.delta + 3.*(1.+2.*a2)*(nu2-3.)/sigma2 + \ # (nu2**2-6.*nu2+3.)/(h.delta*sigma2) # b[4,:] = 24.*(a3+a4)*(nu2-1.)/h.delta + \ # 12.*(a2**2 + 2.*(a2+a3))*(nu2-3.)/sigma2 + \ # 4.*(1.+3.*a2)*(nu2**2-6*nu2+3)/(h.delta*sigma2) + \ # (nu2**2-10.*nu2+15.)/sigma2 return b
def findspectralLines(data, cutLevel =0.866, bPlot=True) :#0.02 is good up to cad 4000->0.01 # moving average 3, smooth data data = 1/3.0*(data + pylab.roll(data,1) + pylab.roll(data,2)) #normalise ndata = data/data.max() #where values are abover cut assign value to -1 #same basic idea as findcallibration except we define the area of interest as below rather than abover cut value lines=numpy.where(ndata>cutLevel,-1,ndata) coords = [] #find start and end values of each line for index, item in enumerate(lines): if lines[index-1]==-1 and item !=-1: coords.append(index) elif index==764: break elif lines[index+1]==-1 and item !=-1: coords.append(index) coordPairs = pylab.reshape(coords,(len(coords)/2,2)) if bPlot: pylab.figure(10) pylab.clf() pylab.plot(ndata,label='3 point moving average, 1/3.0*(data + pylab.roll(data,1) + pylab.roll(data,2))') pylab.xlabel('Pixels') pylab.ylabel('Normalised "Smoothed" yValues') pylab.axhline(cutLevel,color='r',label='Threshold=%s'%(cutLevel)) pylab.title('Spectral Lines Found') pylab.legend(loc='best',prop={'size':8}) for lineRange in coordPairs : pylab.axvline(lineRange[0],color='r',ls='--') pylab.axvline(lineRange[1],color='r',ls='--') print 'findCalibrationLines> line coordinates :',coords print 'findCalibratoinLines> length of list :',len(coords) print 'findCalibrationLines> pairs of coordinates :\n',coordPairs return[coordPairs]
def dispims(M, height, width, border=0, bordercolor=0.0, **kwargs): """ Display a whole stack (colunmwise) of vectorized matrices. Useful eg. to display the weights of a neural network layer. """ from pylab import cm, ceil numimages = M.shape[1] n0 = int(ceil(sqrt(numimages))) n1 = int(ceil(sqrt(numimages))) im = bordercolor*\ ones(((height+border)*n1+border,(width+border)*n0+border),dtype='<f8') for i in range(n0): for j in range(n1): if i*n1+j < M.shape[1]: im[j*(height+border)+border:(j+1)*(height+border)+border,\ i*(width+border)+border:(i+1)*(width+border)+border] = \ vstack((\ hstack((reshape(M[:,i*n1+j],(width,height)).T,\ bordercolor*ones((height,border),dtype=float))),\ bordercolor*ones((border,width+border),dtype=float)\ )) imshow(im.T,cmap=cm.gray,interpolation='nearest', **kwargs) show()
def dispims(M, height, width, border=0, bordercolor=0.0, **kwargs): """ Display a whole stack (colunmwise) of vectorized matrices. Useful eg. to display the weights of a neural network layer. """ from pylab import cm, ceil numimages = M.shape[1] n0 = int(ceil(sqrt(numimages))) n1 = int(ceil(sqrt(numimages))) im = bordercolor*\ ones(((height+border)*n1+border,(width+border)*n0+border),dtype='<f8') for i in range(n0): for j in range(n1): if i * n1 + j < M.shape[1]: im[j*(height+border)+border:(j+1)*(height+border)+border,\ i*(width+border)+border:(i+1)*(width+border)+border] = \ vstack((\ hstack((reshape(M[:,i*n1+j],(width,height)).T,\ bordercolor*ones((height,border),dtype=float))),\ bordercolor*ones((border,width+border),dtype=float)\ )) imshow(im.T, cmap=cm.gray, interpolation='nearest', **kwargs) show()
def create_dep(this,ldef,infile): #Create the dependent variable with the name defined in the first field vnames=ldef.split() #Calculate the dimensions dims=[] vsize=1 for iname in vnames[1:]: vs=len(this.indeps[iname]) dims.append(vs) vsize*=vs #Reserve the data buffer dta = pylab.zeros(vsize,complex) #Read the data for i in xrange(0,vsize): l=infile.readline().strip() dta[i]=this.conv_dta(l) #Now make sure, that the last line is "<indep>" l=infile.readline().strip() if l != "</dep>": raise("Wrong syntax in line: "+l) #Reshape the data buffer into the multi-dimensional array dta=pylab.reshape(dta,dims,'FORTRAN') this.deps[vnames[0]]=qucs_dep_var(dta,vnames[1:])
def create_dep(this, ldef, infile): #Create the dependent variable with the name defined in the first field vnames = ldef.split() #Calculate the dimensions dims = [] vsize = 1 for iname in vnames[1:]: vs = len(this.indeps[iname]) dims.append(vs) vsize *= vs #Reserve the data buffer dta = pylab.zeros(vsize, complex) #Read the data for i in xrange(0, vsize): l = infile.readline().strip() dta[i] = this.conv_dta(l) #Now make sure, that the last line is "<indep>" l = infile.readline().strip() if l != "</dep>": raise ("Wrong syntax in line: " + l) #Reshape the data buffer into the multi-dimensional array dta = pylab.reshape(dta, dims, 'FORTRAN') this.deps[vnames[0]] = qucs_dep_var(dta, vnames[1:])
def interp(datain,lonsin,latsin,lonsout,latsout,checkbounds=False,mode='nearest',cval=0.0,order=3): """ dataout = interp(datain,lonsin,latsin,lonsout,latsout,mode='constant',cval=0.0,order=3) interpolate data (datain) on a rectilinear lat/lon grid (with lons=lonsin lats=latsin) to a grid with lons=lonsout, lats=latsout. datain is a rank-2 array with 1st dimension corresponding to longitude, 2nd dimension latitude. lonsin, latsin are rank-1 arrays containing longitudes and latitudes of datain grid in increasing order (i.e. from Greenwich meridian eastward, and South Pole northward) lonsout, latsout are rank-2 arrays containing lons and lats of desired output grid (typically a native map projection grid). If checkbounds=True, values of lonsout and latsout are checked to see that they lie within the range specified by lonsin and latsin. Default is False, and values outside the borders are handled in the manner described by the 'mode' parameter (default mode='nearest', which means the nearest boundary value is used). See section 20.2 of the numarray docs for information on the 'mode' keyword. See numarray.nd_image.map_coordinates documentation for information on the other optional keyword parameters. The order keyword can be 0 for nearest neighbor interpolation (nd_image only allows 1-6) - if order=0 bounds checking is done even if checkbounds=False. """ # lonsin and latsin must be monotonically increasing. if lonsin[-1]-lonsin[0] < 0 or latsin[-1]-latsin[0] < 0: raise ValueError, 'lonsin and latsin must be increasing!' # optionally, check that lonsout,latsout are # within region defined by lonsin,latsin. # (this check is always done if nearest neighbor # interpolation (order=0) requested). if checkbounds or order == 0: if min(pylab.ravel(lonsout)) < min(lonsin) or \ max(pylab.ravel(lonsout)) > max(lonsin) or \ min(pylab.ravel(latsout)) < min(latsin) or \ max(pylab.ravel(latsout)) > max(latsin): raise ValueError, 'latsout or lonsout outside range of latsin or lonsin' # compute grid coordinates of output grid. delon = lonsin[1:]-lonsin[0:-1] delat = latsin[1:]-latsin[0:-1] if max(delat)-min(delat) < 1.e-4 and max(delon)-min(delon) < 1.e-4: # regular input grid. xcoords = (len(lonsin)-1)*(lonsout-lonsin[0])/(lonsin[-1]-lonsin[0]) ycoords = (len(latsin)-1)*(latsout-latsin[0])/(latsin[-1]-latsin[0]) else: # irregular (but still rectilinear) input grid. lonsoutflat = pylab.ravel(lonsout) latsoutflat = pylab.ravel(latsout) ix = pylab.searchsorted(lonsin,lonsoutflat)-1 iy = pylab.searchsorted(latsin,latsoutflat)-1 xcoords = pylab.zeros(ix.shape,'f') ycoords = pylab.zeros(iy.shape,'f') for n,i in enumerate(ix): if i < 0: xcoords[n] = -1 # outside of range on lonsin (lower end) elif i >= len(lonsin)-1: xcoords[n] = len(lonsin) # outside range on upper end. else: xcoords[n] = float(i)+(lonsoutflat[n]-lonsin[i])/(lonsin[i+1]-lonsin[i]) xcoords = pylab.reshape(xcoords,lonsout.shape) for m,j in enumerate(iy): if j < 0: ycoords[m] = -1 # outside of range of latsin (on lower end) elif j >= len(latsin)-1: ycoords[m] = len(latsin) # outside range on upper end else: ycoords[m] = float(j)+(latsoutflat[m]-latsin[j])/(latsin[j+1]-latsin[j]) ycoords = pylab.reshape(ycoords,latsout.shape) coords = [ycoords,xcoords] # interpolate to output grid using numarray.nd_image spline filter. if order: return nd_image.map_coordinates(datain,coords,mode=mode,cval=cval,order=order) else: # nearest neighbor interpolation if order=0. # uses index arrays, so first convert to numarray. datatmp = pylab.array(datain,datain.typecode()) xi = pylab.around(xcoords).astype('i') yi = pylab.around(ycoords).astype('i') return datatmp[yi,xi]
filename_TpRelay = dirname + 'TpRelay' filename_Rp = dirname + 'Rp' filename_Vp_h_L23pyr = dirname + 'Vp_h_L23pyr' filename_Vp_h_L4pyr = dirname + 'Vp_h_L4pyr' filename_Vp_v_L23pyr = dirname + 'Vp_v_L23pyr' filename_Vp_v_L4pyr = dirname + 'Vp_v_L4pyr' # Save spike data with pickle format for name, r in detectors.items(): rec = r[0] senders = nest.GetStatus(rec)[0]['events']['senders'] times = nest.GetStatus(rec)[0]['events']['times'] #--- reshape and connect data times_column = pylab.reshape(times, (len(times), 1)) senders_column = pylab.reshape(senders, (len(senders), 1)) spike_data = [] spike_data = np.hstack((times_column, senders_column)) if name == 'TpRelay': pickle.dump(spike_data, open(filename_TpRelay + '_spike.p', 'w')) elif name == 'Rp': pickle.dump(spike_data, open(filename_Rp + '_spike.p', 'w')) elif name == 'Vp_h L23pyr': pickle.dump(spike_data, open(filename_Vp_h_L23pyr + '_spike.p', 'w')) elif name == 'Vp_h L4pyr': pickle.dump(spike_data, open(filename_Vp_h_L4pyr + '_spike.p', 'w')) elif name == 'Vp_v L23pyr': pickle.dump(spike_data, open(filename_Vp_v_L23pyr + '_spike.p', 'w')) elif name == 'Vp_v L4pyr':
# read in data. file = open('fcover.dat', 'r') ul = [] vl = [] pl = [] nlons = 73 nlats = 73 dellat = 2.5 dellon = 5. for line in file.readlines(): l = line.replace('\n', '').split() ul.append(float(l[0])) vl.append(float(l[1])) pl.append(float(l[2])) u = reshape(array(ul, Float32), (nlats, nlons)) v = reshape(array(vl, Float32), (nlats, nlons)) p = reshape(array(pl, Float32), (nlats, nlons)) lats1 = -90. + dellat * arange(nlats) lons1 = -180. + dellon * arange(nlons) lons, lats = meshgrid(lons1, lats1) # plot vectors in geographical (lat/lon) coordinates. # north polar projection. m = Basemap(lon_0=-135, boundinglat=25, resolution='c', area_thresh=10000., projection='npstere') # create a figure, add an axes.
def pi(alpha=alpha): pi = pl.zeros((T, J)) for t in range(T): pi[t] = pl.reshape(pl.exp(alpha[t]), J) / pl.sum(pl.exp(alpha[t])) return pi
""" Plot the columns of the output files """ import sys import pylab from mpl_toolkits.basemap import Basemap # Set up a projection bm = Basemap(projection='ortho', lon_0=-80, lat_0=-40, resolution='l', area_thresh=10000) data = pylab.loadtxt(sys.argv[1], unpack=True) shape = (int(sys.argv[2]), int(sys.argv[3])) lon = pylab.reshape(data[0], shape) lat = pylab.reshape(data[1], shape) glon, glat = bm(lon, lat) for i, value in enumerate(data[3:]): value = pylab.reshape(value, shape) pylab.figure(figsize=(4, 3)) pylab.title("Column %d" % (i + 4)) bm.drawcoastlines() #bm.fillcontinents(color='coral',lake_color='aqua') #bm.drawmapboundary(fill_color='aqua') bm.drawmapboundary() bm.drawparallels(pylab.arange(-90.,120.,30.)) bm.drawmeridians(pylab.arange(0.,420.,60.)) #bm.bluemarble() bm.pcolor(glon, glat, value) pylab.colorbar() #bm.contour(glon, glat, value, 12, linewidth=3)
#----------------------------------------------------------------------------- #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Example of how the nelder_mead() function can be used. In this case it # is used to perform nonlinear least-squares curvefitting. if __name__ == "__main__": def f(x): return (x[0] - 1.0)**2 + (x[1] - 2.0)**2 + 0.8 * np.cos(x[0] * x[1]) n = 101 x = pylab.linspace(-2.0, 4.0, n) y = pylab.linspace(-1.0, 5.0, n) xx = (pylab.reshape(x, (n, 1)) * pylab.ones(n)).transpose() yy = pylab.reshape(y, (n, 1)) * pylab.ones(n) z = f([xx, yy]) # x0 = [3.8, 3.5] # dx = 0.25 # x0 = [3.8, 0.0] # dx = 0.75 x0 = [-1.5, 3.5] dx = 0.25 pylab.ion() pylab.contour(x, y, z, 41) a, err, iter = nelder_mead(f, x0, dx, 1e-3, 1, True) pylab.draw()
from matplotlib.toolkits.basemap import Basemap from pylab import show, title, arange, meshgrid, cm, figure, sqrt, \ colorbar, axes, gca, reshape, array, Float32, quiverkey # read in data. file = open('fcover.dat','r') ul=[];vl=[];pl=[] nlons=73; nlats=73 dellat = 2.5; dellon = 5. for line in file.readlines(): l = line.replace('\n','').split() ul.append(float(l[0])) vl.append(float(l[1])) pl.append(float(l[2])) u = reshape(array(ul,Float32),(nlats,nlons)) v = reshape(array(vl,Float32),(nlats,nlons)) p = reshape(array(pl,Float32),(nlats,nlons)) lats1 = -90.+dellat*arange(nlats) lons1 = -180.+dellon*arange(nlons) lons, lats = meshgrid(lons1, lats1) # plot vectors in geographical (lat/lon) coordinates. # north polar projection. m = Basemap(lon_0=-135,boundinglat=25, resolution='c',area_thresh=10000.,projection='npstere') # create a figure, add an axes. fig=figure(figsize=(8,8)) ax = fig.add_axes([0.1,0.1,0.7,0.7]) # rotate wind vectors to map projection coordinates. # (also compute native map projections coordinates of lat/lon grid)
def gen_nrl(self): """ generation of nrl figures :param hh: :type hh: :param z1: :type z1: :param z2: :type z2: :param fg: :type fg: :param fig: :type fig: """ figures = [] # figure(55) # matshow(reshape(sigma_epsilone,(height,width))) # colorbar() for m in range(0, self.M): hh = self.m_H z1 = self.m_A[:, m] z2 = reshape(z1, (self.height, self.width)) fg = figure((self.nf + 1) * 110) fig, ax = subplots() # figure Nrl ########,cmap=get_cmap('gray') data = ax.matshow(z2, cmap=get_cmap('gray')) fig.colorbar(data) title("Niveau de reponse", fontsize='xx-large') figtext(0.2, 0.04, 'bande = ' + str(self.bande) + ' beta =' + str(self.beta) + ' sigma = ' + str(self.sigmaH) + ' pl = ' + str(self.pl) + ' dt = ' + str(self.dt) + ' thrf = ' + str(self.Thrf), fontsize='x-large') figures.append(fig) # title("Est: m = " + str(m)) if self.save == 1: savefig(self.output_dir + 'nrl bande =' + str(self.bande) + 'beta=' + str(self.beta) + 'sigma= ' + str(self.sigmaH) + 'pl=' + str(self.pl) + 'dt=' + str(self.dt) + 'thrf' + str(self.Thrf) + '.png') q = self.q_Z[m, 1, :] q2 = reshape(q, (self.height, self.width)) # q2 = seuillage(q2,0.5) fig, ax = subplots() data = ax.matshow(q2, cmap=get_cmap('gray')) fig.colorbar(data) title("Label d'activation", fontsize='xx-large') figtext(0.2, 0.04, 'bande = ' + str(self.bande) + ' beta =' + str(self.beta) + ' sigma = ' + str(self.sigmaH) + ' pl = ' + str(self.pl) + ' dt = ' + str(self.dt) + ' thrf = ' + str(self.Thrf), fontsize='x-large') figures.append(fig) # for k in range(0,1): # q = q_Z[m,k,:] # q2 = reshape(q,(height,width)) # figure ((nf+1) *38) # matshow(q2,cmap=get_cmap('gray')) # colorbar() # # figure Q_sans seuil ########### # if save == 1 : # savefig(outDir+'Q_Sans_seuil bande =' +str(bande)+'beta='+str(beta)+'sigma= '+str(sigmaH)+'pl='+str(pl)+ 'dt='+str(dt)+'thrf'+str(Thrf)+'.png') # q2=seuillage(q2,0.5) # figure((nf+1) *102) # # figure Q_z avec seuil ########### # # imshow(q2,cmap=cm.gray) # colorbar() # if save == 1 : # savefig(outDir+'Q_avec_seuil bande =' +str(bande)+'beta='+str(beta)+'sigma= '+str(sigmaH)+'pl='+str(pl)+ 'dt='+str(dt)+'thrf'+str(Thrf)+'.png') if self.shower == 1: show() return figures
""" Plot the columns of the output files """ import sys import pylab data = pylab.loadtxt(sys.argv[1], unpack=True) shape = (int(sys.argv[2]), int(sys.argv[3])) lon = pylab.reshape(data[0], shape) * 0.001 lat = pylab.reshape(data[1], shape) * 0.001 xmin, xmax = lon.min(), lon.max() ymin, ymax = lat.min(), lat.max() for i, value in enumerate(data[3:]): value = pylab.reshape(value, shape) pylab.figure(figsize=(4, 3)) pylab.title("Column %d" % (i + 4)) pylab.axis('scaled') pylab.pcolor(lon, lat, value) pylab.colorbar() pylab.contour(lon, lat, value, 12, color='k') #pylab.xlabel("Longitude") #pylab.ylabel("Latitude") pylab.xlim(xmin, xmax) pylab.ylim(ymin, ymax) pylab.savefig('column%d.png' % (i + 4))
def dist(x): # function with x[i] as constants to be determined R, ETA = pylab.meshgrid(r, eta) g = pylab.zeros_like(ETA) g = evalg(x, ETA, R) return pylab.reshape(g, len(eta)*len(r))
def fitLines(coordPairs,lineBasicData, CutLinesValues, bPlot = False) : fitData = [] j = 1 for i in range(0,len(coordPairs),1) : print i print coordPairs[i] print lineBasicData[i] for Values, basicData in itertools.izip(CutLinesValues[1:],lineBasicData[1:]): #chnage slicer values to skip lines which cannot be minimised i.e edge effects or detected false lines x = Values[0] f = Values[1] Err = Values[2] #chi2 = lambda bg, a, mu, sigma, R: (((f-convolutionfuncG(a, mu, sigma, x, R)+bg)/Err)**2).sum() #chi2 = lambda bg,a,mu,sigma: ((f -(a/sigma*pylab.sqrt(2*math.pi))*pylab.exp(-(x-mu)**2/sigma**2)+bg)**2/Err**2).sum() #just gauss #chi2 = lambda bg, a, mu, gamma,R: ((f-convolutionfuncL(a, mu, gamma, x, R)+bg)**2/Err**2).sum() #circleLorentz chi2 = lambda bg, a, mu, gamma, sigma, R:(((f-convolutionfuncvoi(a, x, mu, sigma, gamma, R)+bg)/Err)**2).sum() #circlevoigt #chi2 = lambda bg, a, mu, gamma, sigma:(((f-voiSb((x-mu), sigma, gamma,a)+bg)/Err)**2).sum() #voigt #m=minuit.Minuit(chi2, bg=-505864.835856, a = -408608, mu= 666.961236213, gamma=11.1507399367, sigma=0.750795227728, R=4.87) #m=minuit.Minuit(chi2, bg=-510099.995856, a = -890000, mu= 668.961236213, gamma=11.1507399367, sigma=0.750795227728)#voigt beta extended #m=minuit.Minuit(chi2, bg=-505864.835856, a = -408608, mu= 666.961236213, gamma=11.1507399367, sigma=0.750795227728)#voigt shallow #m=minuit.Minuit(chi2, bg = -500000, a = -basicData[1], mu = basicData[0], gamma = basicData[2], sigma = basicData[2], R=4.87)#tester #m=minuit.Minuit(chi2, bg = -603807.71721, a = -801908.440558, mu = 389.77133122, gamma = 10.133591096, sigma = 10.161872412, R=4.255771982) m=minuit.Minuit(chi2, bg = -494532.214627, a = -275192.356194, mu = 389.755898794, gamma = 9.06574437224, sigma = 2, R=11.0398160531) m.tol = 10000000000 m.printMode = 1 m.migrad() m.hesse() m.values bg = m.values['bg'] a = m.values['a'] mu = m.values['mu'] gamma = m.values['gamma'] sigma = m.values['sigma'] R = m.values['R'] #fit =(a/sigma*pylab.sqrt(2*math.pi))*pylab.exp(-((x-mu)**2)/sigma**2)-bg #fit =convolutionfuncL(a, mu, gamma, x, R)-bg #fit = convolutionfuncG(a, mu, sigma, x, R)-bg fit = convolutionfuncvoi(a, x, mu, sigma, gamma,R)-bg #fit = voiSb((x-mu), sigma, gamma,a)-bg #fitData.append([a,mu,bg,sigma,m.errors]) fitData.append([a,mu,bg,gamma,sigma,R,m.errors]) print'fitLines> Covariance Matrix:\n', m.covariance if bPlot: pylab.figure() pylab.errorbar(x,f,Err,label='Data') pylab.xlabel('Pixels') pylab.ylabel('no. Photo electrons') pylab.plot(x,fit,label='Fitted Voigt X Circle') pylab.legend(loc='best') pylab.figure(108) pylab.subplot(5,2,j) pylab.tight_layout() ax = pylab.gca() for tick in ax.xaxis.get_major_ticks(): tick.label1.set_fontsize(9) for tick in ax.yaxis.get_major_ticks(): tick.label1.set_fontsize(9) pylab.errorbar(x,f,Err) pylab.plot(x,fit,label='line:%s'%(j)) pylab.legend(loc='best',prop={'size':8}) j+=1 fitData = pylab.reshape(fitData,(len(fitData),7)) #out_txt(fitData,'') print 'fitLines> fitData:\n',fitData return[fitData]
def dist(x): # function with x[i] as constants to be determined R, GSIGMA = pylab.meshgrid(r, gsigmas) g = pylab.zeros_like(GSIGMA) g = evalg(x, GSIGMA, R) return pylab.reshape(g, len(gsigmas) * len(r))
def interp(datain,lonsin,latsin,lonsout,latsout,checkbounds=False,mode='nearest',cval=0.0,order=3): """ dataout = interp(datain,lonsin,latsin,lonsout,latsout,mode='constant',cval=0.0,order=3) interpolate data (datain) on a rectilinear lat/lon grid (with lons=lonsin lats=latsin) to a grid with lons=lonsout, lats=latsout. datain is a rank-2 array with 1st dimension corresponding to longitude, 2nd dimension latitude. lonsin, latsin are rank-1 Numeric arrays containing longitudes and latitudes of datain grid in increasing order (i.e. from Greenwich meridian eastward, and South Pole northward) lonsout, latsout are rank-2 Numeric arrays containing lons and lats of desired output grid (typically a native map projection grid). If checkbounds=True, values of lonsout and latsout are checked to see that they lie within the range specified by lonsin and latsin. Default is False, and values outside the borders are handled in the manner described by the 'mode' parameter (default mode='nearest', which means the nearest boundary value is used). See section 20.2 of the numarray docs for information on the 'mode' keyword. See numarray.nd_image.map_coordinates documentation for information on the other optional keyword parameters. The order keyword can be 0 for nearest neighbor interpolation (nd_image only allows 1-6) - if order=0 bounds checking is done even if checkbounds=False. """ # lonsin and latsin must be monotonically increasing. if lonsin[-1]-lonsin[0] < 0 or latsin[-1]-latsin[0] < 0: raise ValueError, 'lonsin and latsin must be increasing!' # optionally, check that lonsout,latsout are # within region defined by lonsin,latsin. # (this check is always done if nearest neighbor # interpolation (order=0) requested). if checkbounds or order == 0: if min(pylab.ravel(lonsout)) < min(lonsin) or \ max(pylab.ravel(lonsout)) > max(lonsin) or \ min(pylab.ravel(latsout)) < min(latsin) or \ max(pylab.ravel(latsout)) > max(latsin): raise ValueError, 'latsout or lonsout outside range of latsin or lonsin' # compute grid coordinates of output grid. delon = lonsin[1:]-lonsin[0:-1] delat = latsin[1:]-latsin[0:-1] if max(delat)-min(delat) < 1.e-4 and max(delon)-min(delon) < 1.e-4: # regular input grid. xcoords = (len(lonsin)-1)*(lonsout-lonsin[0])/(lonsin[-1]-lonsin[0]) ycoords = (len(latsin)-1)*(latsout-latsin[0])/(latsin[-1]-latsin[0]) else: # irregular (but still rectilinear) input grid. lonsoutflat = pylab.ravel(lonsout) latsoutflat = pylab.ravel(latsout) ix = pylab.searchsorted(lonsin,lonsoutflat)-1 iy = pylab.searchsorted(latsin,latsoutflat)-1 xcoords = pylab.zeros(ix.shape,'f') ycoords = pylab.zeros(iy.shape,'f') for n,i in enumerate(ix): if i < 0: xcoords[n] = -1 # outside of range on lonsin (lower end) elif i >= len(lonsin)-1: xcoords[n] = len(lonsin) # outside range on upper end. else: xcoords[n] = float(i)+(lonsoutflat[n]-lonsin[i])/(lonsin[i+1]-lonsin[i]) xcoords = pylab.reshape(xcoords,lonsout.shape) for m,j in enumerate(iy): if j < 0: ycoords[m] = -1 # outside of range of latsin (on lower end) elif j >= len(latsin)-1: ycoords[m] = len(latsin) # outside range on upper end else: ycoords[m] = float(j)+(latsoutflat[m]-latsin[j])/(latsin[j+1]-latsin[j]) ycoords = pylab.reshape(ycoords,latsout.shape) coords = [ycoords,xcoords] # interpolate to output grid using numarray.nd_image spline filter. if order: return nd_image.map_coordinates(datain,coords,mode=mode,cval=cval,order=order) else: # nearest neighbor interpolation if order=0. # uses index arrays, so first convert to numarray. datatmp = pylab.array(datain,datain.typecode()) xi = pylab.around(xcoords).astype('i') yi = pylab.around(ycoords).astype('i') return datatmp[yi,xi]
pylab.clf() pylab.jet() # now plot data from each recorder in turn, assume four recorders for name, r in recorders.items(): rec = r[0] sp = r[1] pylab.subplot(2,2,sp) d = nest.GetStatus(rec)[0]['events']['V_m'] if len(d) != Params['N']**2: # cortical layer with two neurons in each location, take average d = 0.5 * ( d[::2] + d[1::2] ) # clear data from multimeter nest.SetStatus(rec, {'n_events': 0}) pylab.imshow(pylab.reshape(d, (Params['N'],Params['N'])), aspect='equal', interpolation='nearest', extent=(0,Params['N']+1,0,Params['N']+1), vmin=vmn[sp-1], vmax=vmx[sp-1]) pylab.colorbar() pylab.title(name + ', t = %6.1f ms' % nest.GetKernelStatus()['time']) pylab.draw() # force drawing inside loop pylab.show() # required by ``pyreport`` #! just for some information at the end print(nest.GetKernelStatus())
pylab.clf() pylab.jet() # now plot data from each recorder in turn, assume four recorders for name, r in recorders.iteritems(): rec = r[0] sp = r[1] pylab.subplot(2, 2, sp) d = nest.GetStatus(rec)[0]['events']['V_m'] if len(d) != Params['N']**2: # cortical layer with two neurons in each location, take average d = 0.5 * (d[::2] + d[1::2]) # clear data from multimeter nest.SetStatus(rec, {'n_events': 0}) pylab.imshow(pylab.reshape(d, (Params['N'], Params['N'])), aspect='equal', interpolation='nearest', extent=(0, Params['N'] + 1, 0, Params['N'] + 1), vmin=vmn[sp - 1], vmax=vmx[sp - 1]) pylab.colorbar() pylab.title(name + ', t = %6.1f ms' % nest.GetKernelStatus()['time']) pylab.draw() # force drawing inside loop pylab.show() # required by ``pyreport`` #! just for some information at the end print nest.GetKernelStatus()