def process_line(line, separator, collist, atype, missing): global _not_warned strlist = [] line = _obj.sub(r"\1\3\5",line) # remove spaces between real # and imaginary parts of complex numbers if _not_warned: warn = 0 if (_obj.search(line) is not None): warn = 1 for k in range(len(atype)): if atype[k] in numpy.typecodes['Complex']: warn = 0 if warn: numpy.disp("Warning: Complex data detected, but no requested typecode was complex.") _not_warned = 0 for mysep in separator[:-1]: if mysep is None: newline, ind = move_past_spaces(line) strlist.append(line[:ind]) line = newline else: ind = line.find(mysep) strlist.append(line[:ind]) line = line[ind+len(mysep):] strlist.extend(line.split(separator[-1])) arlist = array(strlist,'O') N = len(atype) vals = [None]*N for k in range(len(atype)): vals[k] = extract_columns(arlist, collist[k], atype[k], missing) return vals
def residual(x): T = x[-1] x = x[:-1] omega0 = 2 * np.pi / T Omega = omega0 * n t = np.linspace(0, T, N + 1) t = t[0:-1] # # Check derivatives a = 0.1 x_ = np.sin(a * t) dxdt_ = a * np.cos(a * t) X_ = np.fft.fft(x_) DxDt_ = np.fft.ifft(1j * np.multiply(Omega, X_)) X = np.fft.fft(x) dx = np.fft.ifft(np.multiply(1j * Omega, X)) ddx = np.fft.ifft(np.multiply(-Omega**2, X)) f = np.zeros(N) for ix in range(0, len(x)): if np.imag(x[ix]) > 1e-3 or np.imag(dx[ix]) > 1e-3: np.disp('You have a problem with imaginary numbers!') np.disp([np.imag(x[ix]), np.imag(dx[ix])]) gs.wedge(theta=np.real(x[ix]), thetaDot=np.real(dx[ix])) [Fx, Fy, Mz] = aero.calcLoad() f[ix] = Mz Residual = ddx + 100 * x - Mz Residual = Residual Residual = np.sum(np.abs((Residual**2))) # Residual = np.sum(np.abs((Residual**1))) # return Residual + np.max(np.abs(np.real(DxDt_ - dxdt_))) return Residual
def residual(x): T = x[-1] x = x[:-1] omega0 = 2*np.pi / T Omega = omega0 * n t = np.linspace(0, T, N+1) t = t[0:-1] # # Check derivatives a = 0.1 x_ = np.sin(a*t) dxdt_ = a*np.cos(a*t) X_ = np.fft.fft(x_) DxDt_ = np.fft.ifft(1j * np.multiply(Omega, X_)) X = np.fft.fft(x) dx = np.fft.ifft(np.multiply(1j * Omega, X)) ddx = np.fft.ifft(np.multiply(-Omega**2, X)) f = np.zeros(N) for ix in range(0, len(x)): if np.imag(x[ix]) > 1e-3 or np.imag(dx[ix]) > 1e-3: np.disp('You have a problem with imaginary numbers!') np.disp([np.imag(x[ix]), np.imag(dx[ix])]) gs.wedge(theta=np.real(x[ix]), thetaDot=np.real(dx[ix])) [Fx, Fy, Mz] = aero.calcLoad() f[ix] = Mz Residual = ddx + 100 * x - Mz Residual = Residual Residual = np.sum(np.abs((Residual**2))) # Residual = np.sum(np.abs((Residual**1))) # return Residual + np.max(np.abs(np.real(DxDt_ - dxdt_))) return Residual
def reduction2D(self, max_norm_err=1e-4, pod=1): #ne marche pas si dim>2 if self.dim == 2: if pod == 0: if self.shape[0] < self.shape[1]: CC = np.dot(self.data[1], self.data[0].T) self.data[1] = CC self.data[0] = np.identity(np.shape(CC)[1]) else: CC = np.dot(self.data[0], self.data[1].T) self.data[0] = CC self.data[1] = np.identity(np.shape(CC)[1]) else: CC = np.dot(self.data[0], self.data[1].T) U, S, V = linalg.svd(CC, False) # nombre de valeurs propres retenues i = 1 while S[i] / S[0] > max_norm_err: i += 1 if i == len(S): break nbeig = i + 1 self.data[0] = U[:, 0:nbeig] * S[0:nbeig] self.data[1] = V[0:nbeig, :].T else: np.disp('Warning: reduction_POD dont work if dim != 2') return self
def process_line(line, separator, collist, atype, missing): global _not_warned strlist = [] line = _obj.sub(r"\1\3\5", line) # remove spaces between real # and imaginary parts of complex numbers if _not_warned: warn = 0 if _obj.search(line) is not None: warn = 1 for k in range(len(atype)): if atype[k] in numpy.typecodes["Complex"]: warn = 0 if warn: numpy.disp("Warning: Complex data detected, but no requested typecode was complex.") _not_warned = 0 for mysep in separator[:-1]: if mysep is None: newline, ind = move_past_spaces(line) strlist.append(line[:ind]) line = newline else: ind = line.find(mysep) strlist.append(line[:ind]) line = line[ind + len(mysep) :] strlist.extend(line.split(separator[-1])) arlist = array(strlist, "O") N = len(atype) vals = [None] * N for k in range(len(atype)): vals[k] = extract_columns(arlist, collist[k], atype[k], missing) return vals
def topo_subset(llcrnrlon=-42, urcrnrlon=-35, llcrnrlat=-23, urcrnrlat=-14, tfile='topo30.grd'): """ Get a subset from an etopo1, etopo2 or Smith and Sandwell topography file. OBS: Modified from oceans.datasets.etopo_subset() function by Filipe Fernandes ([email protected]). """ topo = Dataset(tfile, 'r') if 'smith_sandwell' in tfile.lower() or 'etopo1' in tfile.lower(): lons = topo.variables["lon"][:] lats = topo.variables["lat"][:] lons = lon360to180(lons) elif 'etopo2' in tfile.lower(): lons = topo.variables["x"][:] lats = topo.variables["y"][:] else: np.disp('Unknown topography file.') return res = get_indices(llcrnrlat, urcrnrlat, llcrnrlon, urcrnrlon, lons, lats) lon, lat = np.meshgrid(lons[res[0]:res[1]], lats[res[2]:res[3]]) bathy = topo.variables["z"][int(res[2]):int(res[3]), int(res[0]):int(res[1])] return lon, lat, bathy
def readfield(self, fieldname, ind=0): np.disp('''Reading field {}, index {}'''.format(fieldname, ind)) field = np.array(self.fid.variables[fieldname])[ind, :, :, :] field = np.ma.filled(field, np.nan)[:, :, self.lonsort] self.CurrentField = field self.CurrentFieldName = fieldname return field
def cohconf(n, level, unbias, c): # Local Variables: A, c, f, level, i1, k, cl, n, F, unbias, z, Fz, pl # Function calls: disp, cohconf, cohbias, fliplr, interp1, sum, sqrt, nargin, length, diff, pi, cumsum, find, gamma if nargin<1.: help(cohconf) return [] if nargin<2.: level = .95 if nargin<3.: unbias = 0. if nargin<4.: c = 0. if n<2.: np.disp('Warning: degress of freedom must be greater or equal to two.') return [] if np.logical_or(level<=0., level >= 1.): np.disp('Warning: confidence level should be between zero and one.') return [] #%Calculated according to: Amos and Koopmans, "Tables of the distribution of the #%coefficient of coherence for stationary bivariate Gaussian processes", Sandia #%Corporation, 1963 #%Also see Priestly, 1981 z = np.arange(0., 1.0005, .0005) for i1 in np.arange(1., (length(z))+1): A[0] = 1. for k in np.arange(1., (n-1.)+1): A[int((k+1.))-1] = np.dot(matdiv(np.dot(np.dot(A[int(k)-1], n-k), 2.*k-1.), np.dot(2.*n-2.*k+1., k)), matdiv(1.-np.dot(c, z[int(i1)-1]), 1.+np.dot(c, z[int(i1)-1]))**2.) f[int(i1)-1] = np.dot(matdiv(np.dot(matdiv(np.dot(np.dot(np.dot(2.*(n-1.), matixpower(1.-c**2., n)), z[int(i1)-1]), matixpower(1.-z[int(i1)-1]**2., n-2.)), np.dot(1.+np.dot(c, z[int(i1)-1]), matixpower(1.-np.dot(c, z[int(i1)-1]), 2.*n-1.))), plt.gamma((n-.5))), np.dot(np.sqrt(np.pi), plt.gamma(n))), np.sum(A)) #%Use a quadratic Newton-Cotes methods to determine the cumulative sum for i1 in np.arange(2., (length(f)/2.)+1): F[int(i1)-1] = f[int((2.*(i0.)+1.))-1]+4.*f[int((2.*i1))-1]+f[int((2.*i1+1.))-1] F = matdiv(F, 6.*length(F)) F = np.array(np.hstack((np.fliplr((1.-matdiv(np.cumsum(np.fliplr(F)), np.sum(F)))), 1.))) Fz = np.array(np.hstack((z[0:0-2.:2.], 1.))) pl = nonzero((np.diff(F) > 0.)) pl = np.array(np.hstack((1., pl+1.))) cl = interp1(F[int(pl)-1], Fz[int(pl)-1], level) if unbias == 1.: cl = cohbias(n, cl) return [cl]
def __delete(self, x): self.message("Delete " + x) i = np.argwhere(self._open_list[0, :] == x) if len(i) != 1: Error("D*:Delete: state " + x + " doesn't exist.") if len(i) > 1: disp("Del") self._open_list = np.delete(self._open_list, i - 1, 1) self._t = self._closed
def coeflege(n): if n <= 1: np.disp(' n must be > 1 ') return None a = np.zeros(n) b = np.zeros(n) b[0] = 2. k = np.arange(2, n+1) b[k-1] = 1. / (4. - 1. / (k-1)**2.) return [a, b]
def main(): import pylab g = TrHermite(skew=0.1, kurt=3.01) g.dist2gauss() # g = TrOchi(skew=0.56) x = np.linspace(-5, 5) y = g(x) pylab.plot(np.abs(x - g.gauss2dat(y))) # pylab.plot(x,y,x,x,':',g.gauss2dat(y),y,'r') pylab.show() np.disp('finito')
def pad_diffPatterns( Nx, Ny ): #Kan dessa tex heta Nx och Ny när det finns glabala parameterar som heter det? padded_diffPatterns = np.zeros((nbr_scans, Ny, Nx)) x = (Nx - diffSet.shape[2]) / 2 y = (Ny - diffSet.shape[1]) / 2 for i in range(0, nbr_scans): padded_diffPatterns[i, y:y + diffSet.shape[1], x:x + diffSet.shape[2]] = diffSet[i] np.disp(Nx) return padded_diffPatterns
def start(self): # Start the MATLAB server print "Starting MATLAB on http://%s:%s" % (self.host, str(self.port)) print " visit http://%s:%s/exit.m to shut down same" % (self.host, str(self.port)) self.server_process = Process(target=_run_matlab_server, args=(self.matlab, self.port, self.log, self.id, self.startup_options)) self.server_process.daemon = True self.server_process.start() while not self.is_connected(): np.disp(".", linefeed=False) time.sleep(1) print "MATLAB started and connected!" return True
def main(): import pylab g = TrHermite(skew=0.1, kurt=3.01) g.dist2gauss() #g = TrOchi(skew=0.56) x = np.linspace(-5, 5) y = g(x) pylab.plot(np.abs(x - g.gauss2dat(y))) #pylab.plot(x,y,x,x,':',g.gauss2dat(y),y,'r') pylab.show() np.disp('finito')
def get_colors(num_colors): colors = [] if num_colors == 0: num_colors = 1 np.disp('num_colors equal to 0, consider resetting parameters') for i in np.arange(0., 360., 360. / num_colors): hue = i / 360. lightness = (50 + np.random.rand() * 10) / 100. saturation = (90 + np.random.rand() * 10) / 100. colors.append(colorsys.hls_to_rgb(hue, lightness, saturation)) return colors
def mfreq_simulate2(frequency): # Local Variables: nfreq, remainder2, i, frequencies, j, stri, sampling, nsampl, cam_fps, frequency, prime1, freq, remainder, sampling_option # Function calls: disp, rand, floor, fprintf, min, fclose, sort, s, num2str, mfreq_simulate2, input, mod nfreq = 4. nsampl = 33. sampling_option = 0. cam_fps = 15. prime1 freq = np.random.rand(1., nfreq) #% frequencies=100+ceil(freq*900); frequencies = np.array(np.hstack((70., 100., 170., 230.))) if sampling_option == 1.: for i in np.arange(1., (nsampl)+1): stri = np.array(np.hstack(('give ', num2str(i), 'th frequency of strobe'))) np.disp(stri) sampling[int(i)-1] = input(\') else: sampling[0:nsampl] = prime1[0:nsampl] sampling for j in np.arange(1., (nsampl)+1): for i in np.arange(1., (nfreq)+1): if np.mod(np.floor(matdiv(matcompat.max(np.mod(frequencies[int(i)-1], sampling[int(j)-1]), (sampling[int(j)-1]-np.mod(frequencies[int(i)-1], sampling[int(j)-1]))), cam_fps)), 2.) == 0.: remainder[int(i)-1,int(j)-1] = np.mod(matcompat.max(np.mod(frequencies[int(i)-1], sampling[int(j)-1]), (sampling[int(j)-1]-np.mod(frequencies[int(i)-1], sampling[int(j)-1]))), cam_fps) else: remainder[int(i)-1,int(j)-1] = 15.-np.mod(matcompat.max(np.mod(frequencies[int(i)-1], sampling[int(j)-1]), (sampling[int(j)-1]-np.mod(frequencies[int(i)-1], sampling[int(j)-1]))), cam_fps) remainder2[:,int(j)-1] = np.sort(remainder[:,int(j)-1]) #%remainder #%remainder2 fprintf(s, '%f\n', nfreq) fprintf(s, '%f\n', nsampl) for j in np.arange(1., (nsampl)+1): fprintf(s, '%f ', sampling[int(j)-1]) fprintf(s, '\n') for i in np.arange(1., (nfreq)+1): for j in np.arange(1., (nsampl)+1): fprintf(s, '%8.2f ', remainder2[int(i)-1,int(j)-1]) fprintf(s, '\n') fclose(s) #%status=0; return [frequencies, sampling]
def check_forward(self, x): if not (self._x_limit is None): x00 = self._x_limit txt2 = 'for the given interval x = [%g, %g]' % (x[0], x[-1]) if any(np.logical_and(x[0] <= x00, x00 <= x[-1])): cdef = 1 else: cdef = sum(np.logical_xor(x00 <= x[0] , x00 <= x[-1])) if np.mod(cdef, 2): errtxt = 'Unable to invert the polynomial \n %s' % txt2 raise ValueError(errtxt) np.disp('However, successfully inverted the polynomial\n %s' % txt2)
def interp2(A1,A2,A3,B1,B2): "2D Interpolation" # Get input size # height, width = A3.shape height, width = np.float64(A3.shape) # Get output size # heighto, widtho = B1.shape heighto, widtho = np.float64(B1.shape) # Flatten input arrays, just in case... A1 = A1.flatten('F') A2 = A2.flatten('F') A3 = A3.flatten('F') B1 = B1.flatten('F') B2 = B2.flatten('F') # Compute interpolation parameters s = ((B1-A1[0])/(A1[-1]-A1[0]))*(width-1) t = ((B2-A2[0])/(A2[-1]-A2[0]))*(height-1) # s = 1+(B1-A1[0])/(A1[-1]-A1[0])*(width-1) # t = 1+(B2-A2[0])/(A2[-1]-A2[0])*(height-1) # Compute interpolation parameters pruebas # s = (B1-A1[0])/(A1[-1]-A1[0])*(width-1) # t = (B2-A2[0])/(A2[-1]-A2[0])*(height-1) print np.min(s),np.max(s),np.min(t),np.max(t) # Check for out of range values of s and t and set to 1 # sout = np.nonzero(np.logical_or((s<1),(s>width))) # s[sout] = 1 # tout = np.nonzero(np.logical_or((t<1),(t>width))) # t[tout] = 1 # Check for out of range values of s and t and set to 0 # sout = np.nonzero(np.logical_or((s<0),(s>width))) # s[sout] = 0 s[s<0]=0; s[s>width]=0 t[t<0]=0; t[t>width]=0 # Matrix element indexing ndx = np.floor(t)+np.floor(s-1)*height ndx = np.intp(ndx) print ndx.shape, height, width, np.max(ndx) # Compute interpolation parameters s[:] = s-np.floor(s) t[:] = t-np.floor(t) np.disp(str(t)) onemt = 1-t B3 = (A3[ndx-1]*onemt+A3[ndx]*t)*(1-s)+(A3[ndx+int(height)-1]*onemt+A3[ndx+int(height)])*s # B3 = (A3[ndx]*onemt+A3[ndx+1]*t)*(1-s)+(A3[ndx+height]*onemt+A3[ndx+height+1])*s B3 = B3.reshape((heighto, widtho),order='F') B3[B3<0]=0. B3[B3>255]=255. return B3
def process_event_emg(task, event, runIndex, totalTaskTime, sessionNumber): # Local Variables: runIndex, task, val, totalTaskTime, sessionNumber, record_length, ix0, event # Function calls: disp, set, process_event_emg, min, char, str2double, strfind, strcmp #% handle events for this paradigm _switch_val = task.cue_type[int(event) - 1] if False: # switch pass elif _switch_val == 'start_trial': set((task.bar), 'Visible', 'off') #% set(task.text,'String',task.cue{event}); set((task.text), 'String', 'Prepare') if strcmp(np.char((task.cue_type[1])), 'text:bar'): record_length = 10. else: record_length = totalTaskTime #%[status,cmdout] = system('record_length'); #% [status,cmdout] = system(char(trial_data)); #% current_time = num2str(clock); #% for ii = length(current_time) #% ending = strcat(ending,'-',current_time(ii)) #% end #% dataFileName = ['emg_trial_' num2str(trialIndex) '_' ending '.dat']; #% [status,cmdout] = system(dataFileName); elif _switch_val == 'ISI': set((task.bar), 'Visible', 'off') set((task.text), 'String', 'ISI') elif _switch_val == 'text': set((task.text), 'String', (task.cue.cell[int(event) - 1])) set((task.bar), 'Visible', 'off') elif _switch_val == 'text:bar': ix0 = strfind((task.cue.cell[int(event) - 1]), ':') val = str2double( (task.cue.cell[int(event) - 1, int(ix0 + 1.) - 1:]())) / 100. val = matcompat.max(val, 0.9) set((task.text), 'String', (task.cue.cell[int(event) - 1])) if strfind((task.cue.cell[int(event) - 1]), 'Left'): set((task.bar), 'Position', np.array(np.hstack((-val - .1, -.1, val, .2))), 'FaceColor', 'g', 'Visible', 'on') if strfind((task.cue.cell[int(event) - 1]), 'Right'): set((task.bar), 'Position', np.array(np.hstack( (.1, -.1, val, .2))), 'FaceColor', 'r', 'Visible', 'on') elif _switch_val == 'audio': np.disp('play audio here') return
def residual(x): X = np.fft.fft(x) dx = np.fft.ifft(np.multiply(1j * Omega, X)) ddx = np.fft.ifft(np.multiply(-Omega**2, X)) f = np.zeros(N) for ix in range(0, len(x)): if np.imag(x[ix]) > 1e-3 or np.imag(dx[ix]) > 1e-3: np.disp('You have a problem with imaginary numbers!') np.disp([np.imag(x[ix]), np.imag(dx[ix])]) gs.wedge(theta=np.real(x[ix]), thetaDot=np.real(dx[ix])) [Fx, Fy, Mz] = aero.calcLoad() f[ix] = Mz Residual = ddx + 100 * x - Mz Residual = np.sum(np.abs((Residual**2))) return Residual
def SenalDiscreta(amp): n = np.arange( 50 ) # arrange :retorna espacios iguales de a acuerdo a un intervalo 0- 50 dt = 0.07 / 50 x = np.sin(2 * np.pi * amp * n * dt) plt.xlabel('n') plt.ylabel('x[n]') plt.title(r'senal discreta $x[n] = 12 \sin(2\pi 50 n \Delta t)$') plt.stem(n, x) np.disp(x)
def check_forward(self, x): if not (self._x_limit is None): x00 = self._x_limit txt2 = 'for the given interval x = [%g, %g]' % (x[0], x[-1]) if any(np.logical_and(x[0] <= x00, x00 <= x[-1])): cdef = 1 else: cdef = sum(np.logical_xor(x00 <= x[0], x00 <= x[-1])) if np.mod(cdef, 2): errtxt = 'Unable to invert the polynomial \n %s' % txt2 raise ValueError(errtxt) np.disp('However, successfully inverted the polynomial\n %s' % txt2)
def dl_goes(time=datetime(2013, 9, 13), dt=24, dest_dir='./'): """ USAGE ----- dl_goes(time=datetime(2013,9,13)) Downloads full GOES SST images from the PODAAC FTP (12 MB each). Uses wget. ftp://podaac-ftp.jpl.nasa.gov/allData/goes/L3/goes_6km_nrt/americas/. * `time` is a datetime object or a list of datetime objects containing the desired times. * `dt` is an integer representing the time resolution desired. Choose from `24` (default), `3` or `1`. If `dt` is either 1 or 3, all images for each day in `time` will be downloaded. * `dest_dir` is the directory in which the downloaded data will be saved. TODO ---- Find an openDAP link for this dataset (WITH the bayesian cloud filter). """ if type(time) != list: time = [time] original_dir = os.getcwd() # Store original working directory. if os.path.isdir(dest_dir): # If dest_dir already exists. os.chdir(dest_dir) else: # Create it if it does not exist. os.makedirs(dest_dir) os.chdir(dest_dir) for date in time: # Getting files for each day in the list. yyyy = str(date.year) dd = date.timetuple().tm_yday # Get the julian day. dd = str(dd).zfill(3) head = 'ftp://podaac-ftp.jpl.nasa.gov/OceanTemperature/goes/L3/goes_6km_nrt/americas/%s/%s/' % ( yyyy, dd) filename = 'sst%s?_%s_%s' % ( str(dt), yyyy, dd ) # dt can be 1, 3 or 24 (hourly, 3-hourly or daily). url = head + filename # The 'b' character is only for 2008-present data. cmd = "wget --tries=inf %s" % url os.system(cmd) # Download file. os.chdir(original_dir) # Return to the original working directory. np.disp("Done downloading all files.") return None
def dl_goes(time=datetime(2013,9,13), dt=24, dest_dir='./'): """ USAGE ----- dl_goes(time=datetime(2013,9,13)) Downloads full GOES SST images from the PODAAC FTP (12 MB each). Uses wget. ftp://podaac-ftp.jpl.nasa.gov/allData/goes/L3/goes_6km_nrt/americas/. * `time` is a datetime object or a list of datetime objects containing the desired times. * `dt` is an integer representing the time resolution desired. Choose from `24` (default), `3` or `1`. If `dt` is either 1 or 3, all images for each day in `time` will be downloaded. * `dest_dir` is the directory in which the downloaded data will be saved. TODO ---- Find an openDAP link for this dataset (WITH the bayesian cloud filter). """ if type(time)!=list: time = [time] original_dir = os.getcwd() # Store original working directory. if os.path.isdir(dest_dir): # If dest_dir already exists. os.chdir(dest_dir) else: # Create it if it does not exist. os.makedirs(dest_dir) os.chdir(dest_dir) for date in time: # Getting files for each day in the list. yyyy = str(date.year) dd = int(datetime2doy(date)) # Get the julian day. dd = str(dd).zfill(3) head = 'ftp://podaac-ftp.jpl.nasa.gov/OceanTemperature/goes/L3/goes_6km_nrt/americas/%s/%s/' %(yyyy,dd) filename = 'sst%s?_%s_%s' % (str(dt),yyyy,dd) # dt can be 1, 3 or 24 (hourly, 3-hourly or daily). url = head + filename # The 'b' character is only for 2008-present data. cmd = "wget --tries=inf %s" %url os.system(cmd) # Download file. os.chdir(original_dir) # Return to the original working directory. np.disp("Done downloading all files.") return None
def doplot(): f = open("data", "rb") data = cPickle.load(f) f.close() inttypes = [ Huayno.inttypes.HOLD_DKD, Huayno.inttypes.CC_KEPLER, Huayno.inttypes.CC, Huayno.inttypes.EXTRAPOLATE ] for inttype in inttypes: fdims = [1.6, 2.0, 2.3, 2.6, 2.8, 3.0] time = [] time_disp = [] eerr = [] eerr_disp = [] for fd in fdims: t = numpy.array(map(lambda x: x[0], data[inttype][fd])) de = numpy.array(map(lambda x: abs(x[1]), data[inttype][fd])) time.append(numpy.average(t)) time_disp.append(numpy.disp(t)) eerr.append(numpy.average(de)) eerr_disp.append(numpy.disp(de)) data[inttype]["fdims"] = fdims data[inttype]["time"] = time data[inttype]["timedisp"] = time_disp data[inttype]["eerr"] = eerr data[inttype]["eerrdisp"] = eerr_disp linestyles = ["-.", "-", ":", "--"] f = pyplot.figure(figsize=(8, 10)) ax = f.add_subplot(211) for i, inttype in enumerate(inttypes): fdims = data[inttype]['fdims'] eerr = data[inttype]['eerr'] ax.semilogy(fdims, eerr, 'r' + linestyles[i]) ax.set_ylabel("|E-E0|/E0") ax = f.add_subplot(212) for i, inttype in enumerate(inttypes): fdims = data[inttype]['fdims'] eerr = data[inttype]['time'] ax.semilogy(fdims, eerr, 'r' + linestyles[i]) ax.set_xlabel("fractal dimension") ax.set_ylabel("wallclock time (s)") pyplot.savefig('fractal_dimension_dE_time.eps')
def vert_interp_3dvar(pvar3d, z_parent, z_child): global zp, fp1, zc print("Vertically interpolating 3D variable...") cvar3d = np.zeros((z_child.shape[0], ) + pvar3d.shape[1:]) zz_parent = np.unique(z_parent) etamax, ximax = pvar3d.shape[1:] for ieta in range(etamax): # print ("") np.disp("Row %s of %s" % (str(ieta + 1), str(etamax))) # print ("") for ixi in range(ximax): # if ixi%20==0: # np.disp("Col %s of %s"%(str(ixi+1),str(ximax))) # np.disp("Col %s of %s"%(str(ixi+1),str(ximax))) zp = z_parent[:, ieta, ixi] zc = z_child[:, ieta, ixi] fp = pvar3d[:, ieta, ixi] if noextra: fi = np.interp(zc, zp, fp) else: # If no parent variable deeper than the first child s-level, # Search horizontally on the parent grid for the nearest value. I = interp1d(zp, fp, kind='linear', bounds_error=False, assume_sorted=False) I = extrap1d(I) if ixi == 6 and ieta == 6: print('ZP', fp) fi = I(zc) cvar3d[:, ieta, ixi] = fi return cvar3d
def COM_variation(j, nbr_iter): for i in range(j, nbr_iter): xindex = np.argmax(np.sum(one_position[i], axis=0)) yindex = np.argmax(np.sum(one_position[i], axis=1)) reddot = np.zeros((512, 512)) # Make a centred line in x and y intersection at COM reddot[:, xindex] = 500000 reddot[yindex, :] = 500000 np.disp(xindex) plt.figure() noes = ['spring', 'autumn'] plt.imshow(np.log10(one_position[i]), cmap=noes[1], interpolation='none') plt.imshow(np.log10(reddot)) #plt.imshow(np.log10(one_position[1]), cmap = 'hot', interpolation = 'none') #plt.colorbar() funkar ej med flera imshows plt.title('Scan_nbr_%d' % (first_scan_nbr + i))
def is_connected(self): if not self.started: time.sleep(2) return False req = json.dumps(dict(cmd="connect"), cls=ComplexEncoder) self.socket.send(req) start_time = time.time() while(True): try: resp = self.socket.recv_string(flags=zmq.NOBLOCK) if resp == "connected": return True else: return False except zmq.ZMQError: np.disp(".", linefeed=False) time.sleep(1) if (time.time() - start_time > self.maxtime) : print "Matlab session timed out after %d seconds" % (self.maxtime) return False
def is_connected(self): if not self.started: time.sleep(2) return False req = json.dumps(dict(cmd="connect")) self.socket.send(req) start_time = time.time() while(True): try: resp = self.socket.recv_string(flags=zmq.NOBLOCK) if resp == "connected": return True else: return False except zmq.Again: np.disp(".", linefeed=False) time.sleep(1) if (time.time() - start_time > self.maxtime) : print "Matlab session timed out after %d seconds" % (self.maxtime) return False
def DispRMSEStats(Err,Truth,Prior,E): #Quick Error stats Err.RelErrA0=mean(E.A0hat.T-Truth.A0)/mean(Truth.A0) disp(['Relative Error in A0:' +str(Err.RelErrA0)]) disp(['Relative Uncertainty in A0: ' +str(mean(E.stdA0Post.T/E.A0hat.T))]) RMSQPost=sqrt(mean( (E.QhatPostf.T-Truth.Q.T)**2,0 )) RMSQPrior=sqrt(mean( (E.QhatPrior.T-Truth.Q.T)**2,0 )) # ratio=((RMSQPrior-RMSQPost)/RMSQPrior)*100 nR=len(E.A0hat) print('RMS for Q prior: ') for i in range(0,nR): print('%.1f' %RMSQPrior[i]) print('RMS for Q posterior: ') for i in range(0,nR): print('%.1f' %RMSQPost[i]) Err.QRelErrPrior=RMSQPrior/mean(Truth.Q.T,0) Err.QRelErrPost=RMSQPost/mean(Truth.Q.T,0) print('Average RMS for Q posterior: %.3f' %mean(Err.QRelErrPost,0)) print('Average relative Q uncertainty: %.3f' %mean(mean(E.QstdPost/E.QhatPost,0)) ) print('For entire timeseries, rRMSE= %.2f' %Err.Stats.rRMSE, 'and relative bias= %.2f' %Err.Stats.meanRelRes) RMSEQbart=Err.Stats.RMSE/Err.Stats.Qbart biasQbart=Err.Stats.bias/Err.Stats.Qbart print('For entire timeseries, RMSE/Qbart=%.2f' %RMSEQbart, \ 'and bias/Qbart=%.2f' %biasQbart) return Err
def start(self): def _run_matlab_server(): cmd_str = '%s -nodesktop -nosplash -nodisplay -r "'%self.matlab cmd_str += "addpath(genpath(" cmd_str += "'%s'"%MATLAB_FOLDER cmd_str += ')), webserver(%s),exit"'%self.port if self.log: cmd_str += ' -logfile ./pymatbridge/logs/matlablog_%s.txt > ./pymatbridge/logs/bashlog_%s.txt' % (self.id, self.id) os.system(cmd_str) return True # Start the MATLAB server print "Starting MATLAB on http://%s:%s" % (self.host, str(self.port)) self.server_process = Process(target=_run_matlab_server) self.server_process.daemon = True self.server_process.start() while not self.is_connected(): np.disp(".", linefeed=False) time.sleep(1) print "MATLAB started and connected!" return True
def plan(self, goal=None, animate=False, progress=True): if goal is not None: self.goal = goal self.reset() assert (goal is not None and goal is not np.array([])) goal = self._goal self._g = sub2ind(np.shape(self._occ_grid_nav), goal[1], goal[0]) self.__insert(self._g, 0, 'goalset') self._h[self._g] = 0 self._niter = 0 h_prog = None if progress: h_prog = self.progress_init('D* Planning') n_free = np.prod(np.shape(self._occ_grid_nav)) - np.sum( np.sum(self._occ_grid_nav > 0)) n_update = np.round(n_free / 100) while True: self._niter = self._niter + 1 if progress and np.mod(self._niter, n_update) == 0: self.progress(h_prog, self._niter / n_free) if animate: self.show_distance(self._h) if self.process_state() < 0: break if progress: self.progress_delete(h_prog) self._valid_plan = True disp(self._niter + " iterations\n")
def wind2stress(u, v, formula='large_pond1981-modified'): """ USAGE ----- taux,tauy = wind2stress(u, v, formula='mellor2004') Converts u,v wind vector components to taux,tauy wind stress vector components. """ rho_air = 1.226 # kg/m3 mag = np.sqrt(u**2 + v**2) # m/s Cd = np.zeros(mag.shape) # Drag coefficient. if formula == 'large_pond1981-modified': # Large and Pond (1981) formula # modified for light winds, as # in Trenberth et al. (1990). f = mag <= 1. Cd[f] = 2.18e-3 f = np.logical_and(mag > 1., mag < 3.) Cd[f] = (0.62 + 1.56 / mag[f]) * 1e-3 f = np.logical_and(mag >= 3., mag < 10.) Cd[f] = 1.14e-3 f = mag >= 10. Cd[f] = (0.49 + 0.065 * mag[f]) * 1e-3 elif formula == 'mellor2004': Cd = 7.5e-4 + 6.7e-5 * mag else: np.disp('Unknown formula for Cd.') pass # Computing wind stress [N/m2] taux = rho_air * Cd * mag * u tauy = rho_air * Cd * mag * v return taux, tauy
def wind2stress(u, v, formula='large_pond1981-modified'): """ USAGE ----- taux,tauy = wind2stress(u, v, formula='mellor2004') Converts u,v wind vector components to taux,tauy wind stress vector components. """ rho_air = 1.226 # kg/m3 mag = np.sqrt(u**2+v**2) # m/s Cd = np.zeros( mag.shape ) # Drag coefficient. if formula=='large_pond1981-modified': # Large and Pond (1981) formula # modified for light winds, as # in Trenberth et al. (1990). f=mag<=1. Cd[f] = 2.18e-3 f=np.logical_and(mag>1.,mag<3.) Cd[f] = (0.62+1.56/mag[f])*1e-3 f=np.logical_and(mag>=3.,mag<10.) Cd[f] = 1.14e-3 f=mag>=10. Cd[f] = (0.49 + 0.065*mag[f])*1e-3 elif formula=='mellor2004': Cd = 7.5e-4 + 6.7e-5*mag else: np.disp('Unknown formula for Cd.') pass # Computing wind stress [N/m2] taux = rho_air*Cd*mag*u tauy = rho_air*Cd*mag*v return taux,tauy
#% pattern input x = np.dot(C, np.tanh((np.dot(W, x)+np.dot(Win, u)+Wbias))) #%x = tanh(Wstar * x + Win * u + Wbias); if n > washoutLength: xCollector[:,int((n-washoutLength))-1] = x pCollector[0,int((n-washoutLength))-1] = u allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error NRMSE_readout = nrmse(np.dot(Wout, allTrainArgs), allTrainOuts) np.disp(sprintf('NRMSE readout relearn: %g', NRMSE_readout)) #% % test with C x_TestPL = np.zeros(5., testLength, Np) p_TestPL = np.zeros(1., testLength, Np) for p in np.arange(1., (Np)+1): C = PHI(nativeCs.cell[0,int(p)-1], aperture) #%x = startxs(:,p); x = plt.randn(Netsize, 1.) for n in np.arange(1., (testWashout+testLength)+1): x = np.dot(C, np.tanh((np.dot(W, x)+np.dot(D, x)+Wbias))) if n > testWashout: x_TestPL[:,int((n-testWashout))-1,int(p)-1] = x[0:5.,0] p_TestPL[:,int((n-testWashout))-1,int(p)-1] = np.dot(Wout, x)
def __init__(self, file_name, data_path, IDs): # Session information. np.disp(file_name) self.file_name = file_name self.subject_ID = int(file_name.split("-", 1)[0][1:]) self.date = file_name.split("-", 1)[1].split(".")[0] self.IDs = IDs # Import data. data_file = open(os.path.join(data_path, file_name), "r") self.pyControl_file = "Run started at:" in data_file.read() # File is from pyControl system. data_file.seek(0) data_lines = [ line.strip() for line in data_file if line[0].isdigit() and len(line.split(" ")) == (2 + self.pyControl_file) ] data_file.seek(0) reward_prob_lines = [line.strip() for line in data_file if line[0:3] == "Rew"] data_file.seek(0) block_start_lines = [ line.strip() for line in data_file if line[0].isdigit() and len(line.split(" ")) > 1 and line.split(" ")[1 + self.pyControl_file] == "-1" ] data_file.close() # Convert lines to numpy arrays of timestamps (in seconds) and event IDs. time_stamps = np.array([int(line.split(" ")[0]) for line in data_lines]) event_codes = np.array([int(line.split(" ")[-1]) for line in data_lines]) if self.pyControl_file: # self.time_stamps = time_stamps / 1000.0 self.duration = time_stamps[-1] self.event_codes = event_codes start_time_stamp = 0 else: self._raw_time_stamps = time_stamps if "start_stop" in IDs.keys(): start_stop_inds = np.where(event_codes == IDs["start_stop"])[0] start_ind = start_stop_inds[0] stop_ind = start_stop_inds[1] else: start_ind = np.where(event_codes == IDs["session_start"])[0][0] stop_ind = np.where(event_codes == IDs["session_stop"])[0][0] start_time_stamp = time_stamps[start_ind] time_stamps = (time_stamps - start_time_stamp) / 1000.0 self.duration = time_stamps[stop_ind] # Extract reward probs if available. if len(reward_prob_lines) > 0: self.reward_probs = np.array( [(float(line.split()[2]), float(line.split()[4])) for line in reward_prob_lines] ) # Store data and summary info. self.time_stamps = time_stamps[start_ind:stop_ind] self.event_codes = event_codes[start_ind:stop_ind] self.rewards = sum((self.event_codes == IDs["left_reward"]) | (self.event_codes == IDs["right_reward"])) self.actions = sum( (self.event_codes == IDs["left_poke"]) | (self.event_codes == IDs["right_poke"]) | (self.event_codes == IDs["high_poke"]) | (self.event_codes == IDs["low_poke"]) ) self.make_CTSO_representation() # Boolean arrays of choice, transition, outcome information. self.fraction_rewarded = float(self.rewards) / self.n_trials self.trial_start_times = self.time_stamps[self.event_codes == IDs["trial_start"]] # Extract block tranistion information if available: if len(block_start_lines) > 0: start_times, start_trials, reward_states, transition_states = ([], [], [], []) for line in block_start_lines: line = line.split(" ") start_times.append((int(line[0]) - start_time_stamp) / 1000.0) start_trials.append(np.argmax(self.trial_start_times >= start_times[-1])) start_trials[ 0 ] = 0 # Timestamp for first block info follows first trial start, subsequent block info time stamps # preceed first trial of new block. reward_states.append(int(line[-2])) transition_states.append(int(line[-1])) reward_states = np.array(reward_states) transition_states = np.array(transition_states) trial_trans_state = np.zeros( self.n_trials, dtype=bool ) # Boolean array indicating state of tranistion matrix for each trial. trial_rew_state = np.zeros( self.n_trials, dtype=int ) # Integer array indicating state of rewared probabilities for each trial. end_trials = start_trials[1:] + [self.n_trials] for start_trial, end_trial, trans_state, reward_state in zip( start_trials, end_trials, transition_states, reward_states ): trial_trans_state[start_trial:end_trial] = trans_state trial_rew_state[start_trial:end_trial] = reward_state if self.pyControl_file: # Invert reward states for consistency with animal data. # Note this hack is here because in the original datasets # recorded on the arduino setups the reward states where in fact the opposite of that intended and so the analysis # currently is set up for inverted reward states. trial_rew_state = 2 - trial_rew_state reward_states = 2 - reward_states self.blocks = { "start_times": start_times, "start_trials": start_trials, # index of first trial of blocks, first trial of session is trial 0. "end_trials": start_trials[1:] + [self.n_trials], "reward_states": reward_states, # 0 for left good, 1 for neutral, 2 for right good. "transition_states": transition_states, # 1 for A blocks, 0 for B blocks. "trial_trans_state": trial_trans_state, "trial_rew_state": trial_rew_state, }
def ePIE( n, diffSet, probe, objectFuncNy, objectFuncNx, ypixel, xpixel, positiony, positionx, nbr_scans ): # size of probe and diffraction patterns ysize = diffSet.shape[1] xsize = diffSet.shape[2] # object får den nog inte heta object. # make sure it can hold complex numbers # intensitet ettor phase 0 är bra guess objectFunc = np.ones((objectFuncNy, objectFuncNx), dtype=np.complex64) objectIlluminated = np.ones(shape=(ysize, xsize),dtype=np.complex64) # allocating g = np.zeros((ysize, xsize),dtype=np.complex64) gprime = np.zeros(( ysize, xsize),dtype=np.complex64) G = np.zeros((ysize, xsize),dtype=np.complex64) Gprime = np.zeros((ysize, xsize),dtype=np.complex64) # define iteration counter for outer loop k = 0 #figure for animation # fig = plt.figure() # plt.gca().invert_yaxis() # plt.ylabel(' [µm]') # plt.xlabel(' [µm]') # Initialize vector for animation data # ims = [] # initialize vector for error calculation sse = np.zeros(shape=(n,1)) gprimesum = 0 # Start of ePIE iterations while k < n: # Start of inner loop: (where you iterate through all probe positions R) for u in range(0,nbr_scans): # define xposition in matrix from motorposition yposition = int(np.round(positiony[u]/ypixel)) xposition = int(np.round(positionx[u]/xpixel)) # Cut out the part of the image that is illuminated at R(=(ypos,xpos) objectIlluminated = objectFunc[yposition : yposition + ysize, xposition : xposition + xsize ] # get the guessed wave field out from the object at position R (only at position R) g = objectIlluminated * probe # fft the wave field at position R to Fourier space G = (fft.fftshift(fft.fft2(g))) # np.disp('G shape:') # print(G.shape) # make |PSI| confirm with the diffraction pattern from R Gprime = diffSet[u]*np.exp(1j*np.angle(G)) # inverse Fourier transform gprime = ( fft.ifft2(fft.ifftshift(Gprime))) # update the total object function with the illuminated part objectFunc[yposition : yposition + ysize, xposition : xposition + xsize ] = objectIlluminated + (gprime-g) * np.conj(probe) / (np.max(abs(probe))**2)# probe* annars blir det att man delar med massa nollor #update probe function # if k%4==0: # probe = probe + 1 *(gprime-g) * np.conj(objectIlluminated)/ (np.max(abs(objectIlluminated))**2) beta = 0.9 probe = probe + beta *(gprime-g) * np.conj(objectIlluminated)/ (np.max(abs(objectIlluminated))**2) ######################## # Further constraints: ######################## # constrain object amplitude to 1 temp_Oamp = abs(objectFunc) temp_Oamp[temp_Oamp>1] = 1 temp = np.angle(objectFunc) objectFunc = temp_Oamp * np.exp(1j* temp) ##constraint object phase to negative or 0 temp_Ophase = np.angle(objectFunc) temp_Ophase[temp_Ophase>0] = 0 objectFunc = abs(objectFunc) * np.exp(1j* temp_Ophase) # This is for the PRTF (Absolut men du ska ju bara göra det efter att akka iteration är klara, alltså för k=n # Antingen gör detta eller jämför med stara G (men då skippar man ju en iteration) # if k==n: # gprimesum = gprimesum + fft.fft(fft.fft2(objectFunc*probe)) # anim # im = plt.imshow(abs(objectFunc), animated=True, interpolation='none', extent=[0,6.837770297837617,0,6.825238081022181]) ## Error estimate (sse) Nu är alla diff mönster viktade på samma sätt. Inte så bra när de scans som är utanför provet är oviktiga/ger ingen information if u == int(nbr_scans/2): save_G_for_sse = abs(G) # sse[k] = sse[k] + sum(sum( )**2 # va är det här det är ju inte rätt:!: #sse[k] = sse[k] + sum(sum( (diffSet[k]**2 - abs(G)**2) / 65536 ))**2 # ims.append([im]) # tittar bara på sista scanet? sse[k] = sum(sum( (diffSet[int(nbr_scans/2)]**2 - save_G_for_sse**2 ) / 65536 ))**2 #dela innanför k = k+1 np.disp(k) # End of ePIE iterations # calculate PRTF: PRTF = (gprimesum/nbr_scans) / ( sum(diffSet) / nbr_scans) #te = np.fft.fftfreq(PRTF) # np.disp(PRTF) # define exit wave psi = np.zeros((nbr_scans,probe.shape[0],probe.shape[1]),dtype=np.complex64) #transmis = np.zeros # iterate over all probe positions # for lu in range(0,nbr_scans): # # define xposition in matrix from motorposition # yposition = int(np.round(positiony[lu]/ypixel)) # xposition = int(np.round(positionx[lu]/xpixel)) # # # kolla om detta är rätt #psi[lu] = probe * objectFunc[yposition : yposition + ysize, xposition : xposition + xsize ] # ani = animation.ArtistAnimation(fig, ims, interval=150, blit=True,repeat_delay=200) ani = 1 return (objectFunc, probe, ani, sse, psi, PRTF)
1:np.dot(p, learnLength)] = pCollector allTrainTargs[:, int(np.dot(p - 1., learnLength) + 1.) - 1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot( np.dot( linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T) + np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error NRMSE_readout = nrmse(np.dot(Wout, allTrainArgs), allTrainOuts) np.disp(sprintf('NRMSE readout: %g', NRMSE_readout)) #%%% compute W Wtargets = atanh(allTrainArgs) - matcompat.repmat(Wbias, 1., np.dot(Np, learnLength)) W = np.dot( np.dot( linalg.inv((np.dot(allTrainOldArgs, allTrainOldArgs.conj().T) + np.dot(TychonovAlpha, np.eye(Netsize)))), allTrainOldArgs), Wtargets.conj().T).conj().T #% training errors per neuron NRMSE_W = nrmse(np.dot(W, allTrainOldArgs), Wtargets) np.disp(sprintf('mean NRMSE W: %g', np.mean(NRMSE_W))) #%%% run loaded reservoir to observe a messy output. Do this with starting #%%% from four states originally obtained in the four driving conditions #%%
def InterpolateCrossCorrelation(FirstSignal, SecondSignal, Delays, SamplingPeriod, MaxDelay): # Local Variables: SecondPolynomialCoefficients, FirstPolynomialCoefficients, MaxDelay, SecondSignal, MaxLag, PCCC, Delays, Curvature, SamplingPeriod, CrossCorrelation, FirstSignal, Derivative, dd # Function calls: disp, PolynomialCoefficientsCrossCorrelation, numel, PolynomialInterpolationCoefficients, ceil, nargin, zeros, InterpolateCrossCorrelation, error, size #%InterpolatieCrossCorrelation interpolates the cross-correlation function #%of two discrete signals #% #% USAGE: [CrossCorrelation Derivative Curvature] = #% InterpolateCrossCorrelation(FirstSignal,SecondSignal,Delays,SamplingPeriod) #% #% PARAMETERS: #% FirstSignal ~ values of the first signal #% SecondSignal ~ values of the second signal #% Delays ~ delays in which we want to evaluate the cross-correlation function #% SamplingPeriod ~ sampling period of the signals #% #% RETURN VALUE: #% CrossCorrelation ~ values of the cross-correlation function at delays #% Derivatives ~ values of its derivative at delays #% Curvature ~ values of its second derivative at delays #% #% DESCRIPTION: #% This function interpolates the cross-correlation function at times Delays #% of the signals FirstSignal and SecondSignal (sampled at SamplingPeriod). #% It does that assuming some polynomial interpolation at each time interval. #% #% This function will then compute: #% 1) The sequence of coefficients of each polynomial interpolation. #% 2) The cross-correlation of these sequences of coefficients. #% 3) The interpolation of the cross-correlation function at each of the Delays. #% #% REFERENCES: #% X. Alameda-Pineda and R. Horaud. Geometrically-constrained time delay #% estimation-based sound source localisation (gTDESSL). Research Report #% RR-7988, INRIA, June 2012. #% #% see also PolynomialInterpolationCoefficients, PolynomialCoefficientsCrossCorrelation and performCCInterpolation #% Copyright 2012, Xavier Alameda-Pineda #% INRIA Grenoble Rhone-Alpes #% E-mail: [email protected] #% #% This is part of the gtde program. #% #% gtde is free software: you can redistribute it and/or modify #% it under the terms of the GNU General Public License as published by #% the Free Software Foundation, either version 3 of the License, or #% (at your option) any later version. #% #% This program is distributed in the hope that it will be useful, #% but WITHOUT ANY WARRANTY; without even the implied warranty of #% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #% GNU General Public License for more details. #% #% You should have received a copy of the GNU General Public License #% along with this program. If not, see <http://www.gnu.org/licenses/>. #%%% Input check if nargin<4.: matcompat.error('Usage: [CrossCorrelation Derivative] = Interpolation(FirstSignal,SecondSignal,Delays,SamplingPeriod[,MaxDelay]') #%%% Compute the interpolation polynomial coefficients of the signals [FirstPolynomialCoefficients] = PolynomialInterpolationCoefficients(FirstSignal, SamplingPeriod) [SecondPolynomialCoefficients] = PolynomialInterpolationCoefficients(SecondSignal, SamplingPeriod) #%%% Compute the cross-correlation of the coefficients if nargin<5.: [PCCC] = PolynomialCoefficientsCrossCorrelation(FirstPolynomialCoefficients, SecondPolynomialCoefficients) else: MaxLag = np.ceil(matdiv(MaxDelay, SamplingPeriod)) [PCCC] = PolynomialCoefficientsCrossCorrelation(FirstPolynomialCoefficients, SecondPolynomialCoefficients, MaxLag) #%%% Interpolate the cross-correlation at "Delays" CrossCorrelation = np.zeros(matcompat.size(Delays)) Derivative = np.zeros(matcompat.size(Delays)) Curvature = np.zeros(matcompat.size(Delays)) for dd in np.arange(1., (numel(Delays))+1): if dd == 2651.: np.disp('stop') Delays[int(dd)-1] return [CrossCorrelation, Derivative, Curvature]
predictions_hard = (predictions_false > cut).astype(int) elapsed2 = (time.time() - t2) # Making the Confusion Matrix from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, predictions_hard) # Calculating acc prec recall f1 score_acc = np.append(score_acc, accuracy_score(y_test, predictions_hard)) tn, fp, fn, tp = confusion_matrix(y_test, predictions_hard).ravel() specificity1 = np.append(specificity1, tn / (tn + fp)) sensitivity1 = np.append(sensitivity1, tp / (tp + fn)) roc_auc = np.append( roc_auc, skl.metrics.roc_auc_score(y_true=y_test, y_score=predictions_false)) np.disp(score_acc) np.disp(np.mean(score_acc)) np.disp(np.mean(specificity1)) np.disp(np.mean(sensitivity1)) np.disp(np.std(score_acc)) fpr_rf, tpr_rf, _ = skl.metrics.roc_curve(y_test_neg, predictions_true, pos_label=1) roc_auc = skl.metrics.roc_auc_score(y_true=y_test, y_score=predictions_false) print("ROC AUC:", np.mean(roc_auc)) plt.figure(figsize=(15, 15)) plt.title('Receiver Operating Characteristic for LOOCV', fontsize=45) plt.plot(fpr_rf, tpr_rf, linewidth=4, label='Random Forest') plt.plot(fpr_knn, tpr_knn, linewidth=4, label='KNN')
def ekman(r=4., plot=True, savefig=False): """ Plots the solution of the Ekman problem in a vertically finite ocean of depth H. Wind is purely meridional, f-plane, linearized steady-state linearized solutions. The wind is imposed impulsively over the initially at rest water column. """ tau0y = 0.08 # Wind stress, meridional-only [Pa] rho = 1025. # Seawater density [kg m^{-3}] A = 5e-2 # Eddy viscosity coefficient [m^{2} s^{-1}] theta0 = -30. # (Reference) latitude. [ ] omega = 2*np.pi/86400. # Planetary angular frequency. [s^{-1}] f = 2*omega*np.sin(theta0*np.pi/180.) # Coriolis parameter. [s^{-1}] d = np.sqrt(2*A/np.abs(f)) # Ekman layer depth. [m] H = r*d # Bottom depth. [m] st = "H, d, H/d = %s, %s, %s"%(str(H),str(d),str(r)) np.disp(st) z = np.linspace(0, H, 1000.) # Solutions for u and v. C = tau0y/(rho*A) Denom = np.sinh(r)*np.sin(r) + np.cosh(r)*np.cos(r) u = C*np.sinh(z/d)*np.cos(z/d)/Denom v = C*np.cosh(z/d)*np.sin(z/d)/Denom # Veering angle at the surface. ang = np.arctan(v[-1]/u[-1])*180/np.pi st = "Veering angle at the surface: %s"%str(ang) np.disp(st) if plot: # u and v profiles. plt.close('all') fig,ax = plt.subplots() ax.hold(True) ax.plot(u,z,'k-',label=ur'Zonal velocity, u$_{Ek}$') ax.plot(v,z,'k--',label=ur'Meridional velocity, v$_{Ek}$') ax.set_xlabel(ur'Velocity [cm s$^{-1}$]') ax.set_ylabel(ur'Depth [m]') ax.legend(loc='best',fontsize=13) ax.hold(False) try: rstyle(ax) except: pass if savefig: plt.savefig('uv.png',bbox='tight') # Hodograph plot. fig,ax = plt.subplots() ax.hold(True) ax.plot(u,v,'k-',linewidth=1.5) # sx = ax.scatter(u,v,c=z,cmap=plt.cm.binary,linewidths=0.,marker='o') # cb = plt.colorbar(mappable=sx) # cb.set_label(ur'Depth [m]') ax.set_xlabel(ur'Zonal velocity [cm s$^{-1}$]') ax.set_ylabel(ur'Meridional velocity [cm s$^{-1}$]') ax.hold(False) try: rstyle(ax) except: pass if savefig: plt.savefig('hodo.png',bbox='tight') return z,u,v
yearlocation = fullfilename_i.find('year'); year = fullfilename_i[yearlocation+4:yearlocation+8] monthlocation = fullfilename_i.find('month'); month = fullfilename_i[monthlocation+5:monthlocation+7] daylocation = fullfilename_i.find('day'); day = fullfilename_i[daylocation+3:daylocation+5] filenameonly = fullfilename_i[yearlocation-2:yearlocation+24]; alldates[indfile] = [year+month+day] matfile = io.loadmat(dirdotmat+'s%i'%ihc+'/%s'%filenameonly); signals[indfile] = matfile['signals_centered'] ss = array(signals[0]) for indfile in range(1,nbrandomconcat): ss = concatenate((ss,array(signals[indfile])),axis=0) disp('************************************************') sortalldates = sort(alldates,0); disp('Station %i:'%ihc); disp(sortalldates) ## disp('************* start process ********************') #=============================================================== #=============================================================== #=============== processing function call ====================== #=============================================================== Rsup, freqslin, STDmoduleR, STDphaseR, nboverTH = \ estimSUT(ss, filtercharact, frequencylist_Hz, \ Fs_Hz, MSCthreshold, trimpercent); ##
def dith_unsure(y, centres): #def dith(y, centres): -> is it renamed? #print y.shape #print "============" # Local Variables: overall_mean, HOW_MANY_STANDARD_DEVIATIONS, d, dims, i, v, M, sample, DEBUG, ii, edges, m1, edges1, overall_sigma, centres, y, z, remainder, DITHINFO, implied # Function calls: disp, max, unique, isscalar, Inf, arreq, min, mean, nargout, sqrt, whichbin, length, zeros, sprintf, error, var, diff, edges_centres, prod, dith, size #% usage: z=dith(y,M); #% usage: z=dith(y,centers); %centres must be an array of scalars #% size(y)= (trials x length) #% size(y) = trials #% z will not contain any 0 (indices start with 1) DEBUG = 1. #%turn off the slow run-time tests #%usage: dith(y,M) %M=arg centres #if len(centres) == 1: # M = centres # if not type(M) is int: # #if np.prod(matcompat.size(M)) != 1.: # rise Exception('usage: z=dith(y,M);') if type(centres) is int: M = centres #dims = np.arange(1., (length(matcompat.size(y)))+1) #dims = np.arange(1., (len(matcompat.size(y)))+1) #dims = np.arange(1, (len(matcompat.size(y)))+1) #not used anymore #%the default order is 2,1,3,4,5,6,7 ... #if length(dims) > 1.: # dims[0:2.] = np.array(np.hstack((2., 1.))) #v = np.var(y, np.array([]), dims[0]) #if length(dims) > 1.: # for d in dims[1:]: # v = np.mean(v, d) v=np.var(y.flatten()) overall_sigma = np.sqrt(v) #%assert(overall_sigma ~= 0.0, 'var is 0.0'); #m1 = np.mean(y, dims[0]) #if length(dims) > 1.: # for d in dims[1:]: # m1 = np.mean(m1, d) m1 = np.mean(y.flatten()) overall_mean = m1 HOW_MANY_STANDARD_DEVIATIONS = 2. #%just a scale of edges & centres value np.disp('using %d standard deviations, M=%d levels'%(HOW_MANY_STANDARD_DEVIATIONS, M)) #%edges = "values used for discretisation" #%centres2 = "implied" value by each discretized level (used in dithering)" (edges, centres) = edges_centres(M, HOW_MANY_STANDARD_DEVIATIONS, overall_sigma, overall_mean) assert(M>1) else: #%usage: dith(y,centres) #%ds=centres(2)-centres(1); #edges1=centres(1:end-1) + diff(centres)/2; edges1 = centres[0:-1]+np.diff(centres) / 2. #edges1 = centres[0:0-1]+np.diff(centres)/2. # edges=[-Inf,edges1,Inf]; edges = np.array(np.hstack((-np.Inf, edges1, np.Inf))) M = len(centres) #%return extra information about discritization if True: #if nargout == 2.: DITHINFO = {} #np.array([]) DITHINFO['edges'] = edges DITHINFO['centres'] = centres #%DITHINFO.z2= regular_discretize(y,M, ds, edges(2)); #% if simple/regular agorithm #% z= regular_discretize(y,M, ds, edges(2)); #% #%the actual slow calculations #print type(y) sz=y.shape #%both edges & centres are needed. #z = np.zeros(y.shape, int) z = np.zeros((y.size,), int) y1=y.flatten(1) remainder = 0. #%initial remainder = 0 (i.e., last error, or remainder error) for i0 in range(y1.size): #np.arange(1., (length(z.flatten(1)))+1): sample = y1[i0] i=i0+1 #%ii=find(histc(sample + remainder,edges)); %, 'first' ii = whichbin((sample+remainder), edges) implied = centres[ii-1] remainder = remainder+sample-implied #%remainder = remainder * 0.999; #print 'ii,i',(ii,i), ' z:',z.shape,' y1.size',y1.shape z[i-1] = ii if DEBUG: #%if isempty(ii) warning('error in discretization'); end #%if ii==0 error('find(histc()) not found'); end #%assert(~isempty(ii), 'error in discretization' ); #%%assert(ii>0, 'find(histc(value)) did not fit into any interval'); #%assert(length(ii)==1, 'histc porblem'); pass #%warning: uses the last remainder for the next trial if DEBUG: #%slow ones here #%unique(z(:))' #%slow pass z=z.reshape(sz) #print z.shape #print sz #print "***********************" #print type(z[0,0]) assert is_any_int_type(z[0,0]) #%inner function return z, DITHINFO
URCollectors.cell[0,int(p)-1] = Ux patternRs.cell[int(p)-1] = R startXs[:,int(p)-1] = x train_xPL.cell[0,int(p)-1] = xCollector[:,0:signalPlotLength] train_pPL.cell[0,int(p)-1] = pCollector[0,0:signalPlotLength] patternCollectors.cell[0,int(p)-1] = pCollector allTrainArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xCollector allTrainOldArgs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = xOldCollector allTrainOuts[0,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = pCollector allTrainTargs[:,int(np.dot(p-1., learnLength)+1.)-1:np.dot(p, learnLength)] = np.dot(Win, pCollector) #%%% compute readout Wout = np.dot(np.dot(linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T)+np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs), allTrainOuts.conj().T).conj().T #% training error NRMSE_readout = nrmse(np.dot(Wout, allTrainArgs), allTrainOuts) np.disp(sprintf('NRMSE readout: %g', NRMSE_readout)) #%%% compute W Wtargets = atanh(allTrainArgs)-matcompat.repmat(Wbias, 1., np.dot(Np, learnLength)) W = np.dot(np.dot(linalg.inv((np.dot(allTrainOldArgs, allTrainOldArgs.conj().T)+np.dot(TychonovAlpha, np.eye(Netsize)))), allTrainOldArgs), Wtargets.conj().T).conj().T #% training errors per neuron NRMSE_W = nrmse(np.dot(W, allTrainOldArgs), Wtargets) np.disp(sprintf('mean NRMSE W: %g', np.mean(NRMSE_W))) #%%% run loaded reservoir to observe a messy output. Do this with starting #%%% from four states originally obtained in the four driving conditions #%% plt.figure(10.) plt.clf #% initialize network state for p in np.arange(1., 5.0): x = startXs[:,int(p)-1] messyOutPL = np.zeros(1., CtestLength)
def helloworld(): np.disp('Hello there, Mr. World.')
allTrainOuts[0, int(np.dot(p - 1.0, learnLength) + 1.0) - 1 : np.dot(p, learnLength)] = pCollector Wout = ( np.dot( np.dot( linalg.inv((np.dot(allTrainArgs, allTrainArgs.conj().T) + np.dot(TychonovAlphaReadout, np.eye(Netsize)))), allTrainArgs, ), allTrainOuts.conj().T, ) .conj() .T ) #% training error NRMSE_readout = nrmse(np.dot(Wout, allTrainArgs), allTrainOuts) np.disp(sprintf("NRMSE readout relearn: %g", NRMSE_readout)) #% % test with C x_TestPL = np.zeros(5.0, testLength, Np) p_TestPL = np.zeros(1.0, testLength, Np) for p in np.arange(1.0, (Np) + 1): C = PHI(nativeCs.cell[0, int(p) - 1], aperture) #%x = startxs(:,p); x = plt.randn(Netsize, 1.0) for n in np.arange(1.0, (testWashout + testLength) + 1): x = np.dot(C, np.tanh((np.dot(W, x) + np.dot(D, x) + Wbias))) if n > testWashout: x_TestPL[:, int((n - testWashout)) - 1, int(p) - 1] = x[0:5.0, 0] p_TestPL[:, int((n - testWashout)) - 1, int(p) - 1] = np.dot(Wout, x) #%% plot
res[0][1] = video_process.replaceGrayArea(res[0][1], vg.mouseRoi[0], vg.mouseRoi[2]) myImageIn.SetData(res[0][1]) vg.addK() # ---------------------------------------------------------- # Acumulo imagen si estoy en la etapa inicial elif vg.process_mode == 1: # Adquiero cuadros if len(vg.vidAcq) < vg.intAcqFrames: vg.vidAcq.append(res[0][1]) np.disp('Guardado '+str(vg.k)+'/'+str(vg.intAcqFrames)) vg.addK() # -------------------------------------------------------------- # Multithreading # Si tengo nucleos sin hacer nada if (len(vg.mt.pending) < vg.mt.threadn and vg.process_mode > 0): # Descargo la imagen vg.captura.get() # Duermo en función del delay setteado time.sleep(1 - vg.pv.playSpeed)
def dynmodes(n=6, lat0=5., plot=False, model='Fratantoni_etal1995'): """ Computes the discrete eigenmodes (dynamical modes) for a quasi-geostrophic ocean with n isopycnal layers. Rigid lids are placed at the surface and the bottom. Inputs: ------- n: Number of layers. lat0: Reference latitude. H: List of rest depths of each layer. S: List of potential density values for each layer. """ omega = 2*np.pi/86400. # [rad s^{-1}] f0 = 2*omega*np.sin(lat0*np.pi/180) # [s^{-1}] f02 = f0**2 # [s^{-2}] g = 9.81 # [m s^{-1}] rho0 = 1027. # [kg m^{-3}] if model=='Fratantoni_etal1995': H = np.array([80.,170.,175.,250.,325.,3000.]) S = np.array([24.97,26.30,26.83,27.12,27.32,27.77]) tit = ur'Modelo de seis camadas para a CNB de Fratantoni \textit{et al.} (1995)' figname = 'vmodes_fratantoni_etal1995' elif model=='Bub_Brown1996': H = np.array([150.,440.,240.,445.,225.,2500.]) S = np.array([24.13,26.97,27.28,27.48,27.74,27.87]) tit = ur'Modelo de seis camadas para a CNB de Bub e Brown (1996)' figname = 'vmodes_bub_brown1996' # Normalized density jumps. E = (S[1:]-S[:-1])/rho0 # Rigid lids at the surface and the bottom, # meaning infinite density jumps. E = np.hstack( (np.inf,E,np.inf) ) # Building the tridiagonal matrix. A = np.zeros( (n,n) ) for i in xrange(n): A[i,i] = -f02/(E[i+1]*g*H[i]) -f02/(E[i]*g*H[i]) # The main diagonal. if i>0: A[i,i-1] = f02/(E[i]*g*H[i]) if i<(n-1): A[i,i+1] = f02/(E[i+1]*g*H[i]) # get eigenvalues and convert them # to internal deformation radii lam,v = eig(A) lam = np.abs(lam) # Baroclinic def. radii in km: uno = np.ones( (lam.size,lam.size) ) Rd = 1e-3*uno/np.sqrt(lam) Rd = np.unique(Rd) Rd = np.flipud(Rd) np.disp("Deformation radii [km]:") [np.disp(int(r)) for r in Rd] # orthonormalize eigenvectors, i.e., # find the dynamical mode vertical structure functions. F = np.zeros( (n,n) ) for i in xrange(n): mi = v[:,i] # The vertical structure of the i-th vertical mode. fac = np.sqrt(np.sum(H*mi*mi)/np.sum(H)) F[:,i] = 1/fac*mi F=-F F[:,0] = np.abs(F[:,1]) F = np.fliplr(F) Fi = np.vstack( (F[0,:],F) ) Fi = np.flipud(Fi) for i in xrange(n-1): Fi[i,:] = F[i+1,:] Fi = np.flipud(Fi) # Plot the vertical modes. if plot: plt.close('all') kw = dict(fontsize=15, fontweight='black') fig,ax = plt.subplots() ax.hold(True) Hc = np.sum(H) z = np.flipud(np.linspace(-Hc,0,1000)) Hp = np.append(0,H) Hp = -np.cumsum(Hp) # build modes for plotting purposes Fp = np.zeros( (z.size,n) ) fo = 0 for i in xrange(n): f1 = near(z,Hp[i])[0][0] for j in xrange(fo,f1): Fp[j,:] = F[i,:] fo=f1 for i in xrange(n): l = 'Modo %s'%str(i) ax.plot(Fp[:,i], z, label=l) xl,xr = ax.set_xlim(-5,5) ax.hlines(Hp,xl,xr,linestyle='dashed') ax.hold(False) ax.set_title(tit, **kw) ax.set_xlabel(ur'Autofunção [adimensional]', **kw) ax.set_ylabel(ur'Profundidade [m]', **kw) try: rstyle(ax) except: pass ax.legend(loc='lower left', fontsize=20, fancybox=True, shadow=True) fmt='png' fig.savefig(figname+'.'+fmt, format=fmt, bbox='tight')
def solvemembrane(nodes, elements, properties, loads, fixities ): """Finds node displacements in a 2-dimensional linear elastic membrane.""" # element's stiffness matrix def Ke(k, A): """Returns a membrane element's stiffness matrix.""" #np.disp(A) invA = np.linalg.inv(A) area = np.linalg.det(A)*0.5 K=np.zeros((3,3)) for i in range(3): for j in range(3): K[i,j] = area*k*(invA[0,i]*invA[0,j]+invA[1,i]*invA[1,j]) return K # degrees of freedom per node dof = 1 # number of elements noe = len(elements) # nodes per element nne = len(elements[0])-1 KGsize = np.size(nodes,0)*dof KG = np.zeros((KGsize,KGsize)) # list containing all elements' stiffness in global coordinates kg = [] for i in range(noe): k = properties[elements[i,3],0] coord = np.array([ [ nodes[elements[i,0],0], nodes[elements[i,0],1], 1 ], [ nodes[elements[i,1],0], nodes[elements[i,1],1], 1 ], [ nodes[elements[i,2],0], nodes[elements[i,2],1], 1 ] ]) #np.disp(coord) kg.append( Ke(k,coord) ) #for i in range(noe): #np.disp(kg[i]) # assemble global stiffness matrix # for every element kint=0 for i in range(noe): jint=0 # for every line for j in elements[i][0:-1]: # and every column for k in elements[i][0:-1]: KG[j,k]+=kg[i][jint,kint] kint += 1 jint += 1 kint=0 # assemble loads vector F = np.zeros((KGsize,1)) for i in range(len(loads)): for j in range(dof): F[loads[i,0]*dof + j ] = loads[i, 1+j] #np.disp(KG) # applies fixity conditions to global stiffness matrix for i in range(len(fixities)): for j in range(dof): if not np.isnan(fixities[i,j+1]): for k in range(KGsize): KG[ fixities[i,0]*dof+j, k] = 0 KG[fixities[i,0]*dof+j, fixities[i,0]*dof+j ] = 1 F[fixities[i,0]*dof+j ] = fixities[i,j+1] print("\n== GLOBAL STIFFNESS MATRIX ==\n") np.disp(KG) #print("\n== GLOBAL LOADS VECTOR ==\n") #np.disp(F) # solves system X = np.linalg.solve(KG,F) return X
def wind_subset(times=(datetime(2009,12,28), datetime(2010,1,10)), dt='daily', llcrnrlon=-45, urcrnrlon=-35, llcrnrlat=-25, urcrnrlat=-18): """ USAGE ----- u,v = wind_subset(...) Gets wind vectors from the NCDC/NOAA Blended Seawinds L4 product, given a given lon, lat, time bounding box. Input ----- * times: A tuple with 2 datetime objects marking the start and end of the desired subset. If dt=='clim', this should be an integer indicating the desired month, from 1 (January) through 12 (December). This climatology is built from the 1995-2005 data. * dt: Time resolution wanted. Choose `six-hourly`, `daily` (default) or `monthly`. * llcrnrlon, urcrnrlon: Longitude band wanted. * llcrnrlat, urcrnrlat: Latitude band wanted. Returns ------- u,v: pandas Panels containing the data subsets. Example ------- TODO. """ head = 'http://www.ncdc.noaa.gov/thredds/dodsC/oceanwinds' tail = {'six-hourly':'6hr','daily':'dly','monthly':'mon','clim':'clm-agg'} url = head + tail[dt] nc = Dataset(url) lon = nc.variables.pop('lon')[:] lon = lon360to180(lon) # Longitude is in the [0,360] range, BB is in the [-180,+180] range. lat = nc.variables.pop('lat')[:] time = nc.variables.pop('time') time = num2date(time[:],time.units,calendar='standard') # Subsetting the data. maskx = np.logical_and(lon >= llcrnrlon, lon <= urcrnrlon) masky = np.logical_and(lat >= llcrnrlat, lat <= urcrnrlat) # Ignoring more precise time units than # the resolution in the desired dataset. nt = time.size if dt=='six-hourly': for i in xrange(nt): time[i] = time[i].replace(minute=0,second=0) elif dt=='daily': for i in xrange(nt): time[i] = time[i].replace(hour=0,minute=0,second=0) elif dt=='monthly': for i in xrange(nt): time[i] = time[i].replace(day=1,hour=0,minute=0,second=0) elif dt=='clim': time[1] = time[1].replace(month=2) # Fixing February. aux = []; [aux.append(Time.month) for Time in time] time = np.copy(aux); del aux else: pass # If the time wanted is a single month/day/hour, # get the nearest time available in the dataset. if dt=='clim': maskt=time==times else: if np.size(times)<2: idxt = np.abs(time-times).argmin() maskt=time==time[idxt] else: maskt = np.logical_and(time >= times[0], time <= times[1]) np.disp('Downloading subset...') lon, lat, time = lon[maskx], lat[masky], time[maskt] u = nc.variables['u'][maskt,0,masky,maskx] v = nc.variables['v'][maskt,0,masky,maskx] np.disp('Downloaded.') lat,lon,time,u,v = map(np.atleast_1d, [lat,lon,time,u,v]) # Sets the panels. U = Panel(data=u, items=time, major_axis=lat, minor_axis=lon) V = Panel(data=v, items=time, major_axis=lat, minor_axis=lon) # Sorting major axis (longitude) to fix the crossing of the 0º meridian. U = U.sort_index(axis=2) V = V.sort_index(axis=2) # Retrieves the u,v arrays to fix masked values. u,v,lon,lat = map(np.atleast_1d, (U.values,V.values,U.minor_axis.values,U.major_axis.values)) fill_value = -9999. u = np.ma.masked_equal(u, fill_value) v = np.ma.masked_equal(v, fill_value) # Resets the Panels with the sorted and masked u,v arrays. U = Panel(data=u, items=time, major_axis=lat, minor_axis=lon) V = Panel(data=v, items=time, major_axis=lat, minor_axis=lon) return U,V
def invfreqs(g, w, varargin): # Local Variables: realFlag, cg, realStr, D31, gndir, t1, cw, nk, kom, nm, na, nb, Vcap, V1, rg, pf, rw, tol, maxiter, varargin, cwf, wf, ll, D, rwf, D32, Dva, Dvb, gaussFlag, verb, GC, e, th, a, OM, b, Vd, g, k, l, st, indg, R, inda, t, w, indb, D3 # Function calls: disp, all, invfreqs, deal, ischar, int2str, warning, apolystab, home, message, size, getString, sqrt, clc, zeros, norm, real, nargchk, max, nargin, ones, isempty, lower, length, num2str, error, strcmp #%INVFREQS Analog filter least squares fit to frequency response data. #% [B,A] = INVFREQS(H,W,nb,na) gives real numerator and denominator #% coefficients B and A of orders nb and na respectively, where #% H is the desired complex frequency response of the system at frequency #% points W, and W contains the frequency values in radians/s. #% INVFREQS yields a filter with real coefficients. This means that it is #% sufficient to specify positive frequencies only; the filter fits the data #% conj(H) at -W, ensuring the proper frequency domain symmetry for a real #% filter. #% #% [B,A]=INVFREQS(H,W,nb,na,Wt) allows the fit-errors to the weighted #% versus frequency. LENGTH(Wt)=LENGTH(W)=LENGTH(H). #% Determined by minimization of sum |B-H*A|^2*Wt over the freqs in W. #% #% [B,A] = INVFREQS(H,W,nb,na,Wt,ITER) does another type of fit: #% Sum |B/A-H|^2*Wt is minimized with respect to the coefficients in B and #% A by numerical search in at most ITER iterations. The A-polynomial is #% then constrained to be stable. [B,A]=INVFREQS(H,W,nb,na,Wt,ITER,TOL) #% stops the iterations when the norm of the gradient is less than TOL. #% The default value of TOL is 0.01. The default value of Wt is all ones. #% This default value is also obtained by Wt=[]. #% #% [B,A]=INVFREQS(H,W,nb,na,Wt,ITER,TOL,'trace') provides a textual #% progress report of the iteration. #% #% [B,A] = INVFREQS(H,W,'complex',NB,NA,...) creates a complex filter. In #% this case, no symmetry is enforced. #% #% % Example: #% % Convert a simple transfer function to frequency response data and #% % then back to the original filter coefficients. If the system is #% % unstable, use invfreqs's iterative algorithm to find a stable #% % approximation to the system. #% #% b = [1 2 3 2 3]; % Numerator coefficients #% a = [1 2 3 2 1 4]; % Denominator coefficients #% [h,w] = freqs(b,a,64); #% [bb,aa] = invfreqs(h,w,4,5) % aa has poles in the right half-plane. #% fprintf('Stable Approximation to the system:') #% [bbb,aaa] = invfreqs(h,w,4,5,[],30) % stable approximation to system #% #% See also FREQZ, FREQS, INVFREQZ. #% Author(s): J.O. Smith and J.N. Little, 4-23-86 #% J.N. Little, 4-27-88, revised #% Lennart Ljung, 9-21-92, rewritten #% T. Krauss, 10-22-92, trace mode made optional #% Copyright 1988-2011 The MathWorks, Inc. #% #% calling sequence is #%function [b,a]=invfreqs(g,w,nb,na,wf,maxiter,tol,pf) #% OR #%function [b,a]=invfreqs(g,w,'complex',nb,na,wf,maxiter,tol,pf) matcompat.error(nargchk(4., 9., nargin, 'struct')) if ischar(varargin.cell[0]): realStr = lower(varargin.cell[0]) varargin[0] = np.array([]) else: realStr = 'real' gaussFlag = length(varargin) > 3. #% run Gauss-Newton algorithm or not? if length(varargin)<6.: varargin.cell[5] = np.array([]) #% pad varargin with []'s [nb, na, wf, maxiter, tol, pf] = deal(varargin.cell[:]) _switch_val=realStr if False: # switch pass elif _switch_val == 'real': realFlag = 1. elif _switch_val == 'complex': realFlag = 0. else: matcompat.warning(message('signal:invfreqs:InvalidParam', realStr)) realFlag = 0. nk = 0. #% The code is prepared for constraining the numerator to #% begin with nk zeros. nb = nb+nk+1. if isempty(pf): verb = 0. elif strcmp(pf, 'trace'): verb = 1. else: matcompat.error(message('signal:invfreqs:NotSupported', pf)) if isempty(wf): wf = np.ones(length(w), 1.) wf = np.sqrt(wf) if length(g) != length(w): matcompat.error(message('signal:invfreqs:UnmatchedLengths', 'H', 'W')) if length(wf) != length(w): matcompat.error(message('signal:invfreqs:UnmatchedLengths', 'Wt', 'W')) #% if any( w(:)<0 ) && realFlag #% warning(message('signal:invfreqs:InvalidWParam', 'W', 'INVFREQS', 'complex')) #% end [rw, cw] = matcompat.size(w) if rw > cw: w = w.conj().T [rg, cg] = matcompat.size(g) if cg > rg: g = g.T [rwf, cwf] = matcompat.size(wf) if cwf > rwf: wf = wf.conj().T nm = matcompat.max((na+1.), (nb+nk)) indb = np.arange(nb, (1.)+(-1.), -1.) indg = np.arange(na+1., (1.)+(-1.), -1.) inda = np.arange(na, (1.)+(-1.), -1.) OM = np.ones(1., length(w)) for kom in np.arange(1., (nm-1.)+1): OM = np.array(np.vstack((np.hstack((OM)), np.hstack(((1i.*w)**kom))))) #% #% Estimation in the least squares case: #% Dva = OM[int(inda)-1,:].T*np.dot(g, np.ones(1., na)) Dvb = -OM[int(indb)-1,:].T D = np.array(np.hstack((Dva, Dvb)))*np.dot(wf, np.ones(1., (na+nb))) R = np.dot(D.conj().T, D) Vd = np.dot(D.conj().T, -g*OM[int((na+1.))-1,:].T*wf) if realFlag: R = np.real(R) Vd = np.real(Vd) th = linalg.solve(R, Vd) a = np.array(np.hstack((1., th[0:na].T))) b = np.array(np.hstack((np.zeros(1., nk), th[int(na+1.)-1:na+nb].T))) if not gaussFlag: return [] #% Now for the iterative minimization if isempty(maxiter): maxiter = 30. if isempty(tol): tol = 0.01 #% Stabilizing the denominator: a = apolystab(a, realFlag) #% The initial estimate: GC = (np.dot(b, OM[int(indb)-1,:])/np.dot(a, OM[int(indg)-1,:])).T e = (GC-g)*wf Vcap = np.dot(e.conj().T, e) t = np.array(np.hstack((a[1:na+1.], b[int(nk+1.)-1:nk+nb]))).T if verb: #% invfreqz using same messages clc np.disp(np.array(np.hstack((' ', getString(message('signal:invfreqs:INITIALESTIMATE')))))) np.disp(np.array(np.hstack((getString(message('signal:invfreqs:CurrentFit')), num2str(Vcap))))) np.disp(getString(message('signal:invfreqs:Parvector'))) np.disp(t) #% % #% ** the minimization loop ** #% gndir = 2.*tol+1. l = 0. st = 0. while np.all(np.array(np.hstack((linalg.norm(gndir) > tol, l<maxiter, st != 1.)))): l = l+1. #% * compute gradient * D31 = OM[int(inda)-1,:].T*np.dot(-GC/np.dot(a, OM[int(indg)-1,:]).T, np.ones(1., na)) D32 = OM[int(indb)-1,:].T/np.dot(np.dot(a, OM[int(indg)-1,:]).T, np.ones(1., nb)) D3 = np.array(np.hstack((D31, D32)))*np.dot(wf, np.ones(1., (na+nb))) #% * compute Gauss-Newton search direction e = (GC-g)*wf R = np.dot(D3.conj().T, D3) Vd = np.dot(D3.conj().T, e) if realFlag: R = np.real(R) Vd = np.real(Vd) gndir = linalg.solve(R, Vd) #% * search along the gndir-direction * ll = 0. k = 1. V1 = Vcap+1. t1 = t while np.all(np.array(np.hstack((V1, ll<20.)))): t1 = t-np.dot(k, gndir) if ll == 19.: t1 = t a = np.array(np.hstack((1., t1[0:na].T))) b = np.array(np.hstack((np.zeros(1., nk), t1[int(na+1.)-1:na+nb].T))) a = apolystab(a, realFlag) #% Stabilizing the denominator t1[0:na] = a[1:na+1.].T GC = (np.dot(b, OM[int(indb)-1,:])/np.dot(a, OM[int(indg)-1,:])).T V1 = np.dot(((GC-g)*wf).conj().T, (GC-g)*wf) if verb: home np.disp(int2str(ll)) k = k/2. ll = ll+1. if ll == 10.: gndir = np.dot(matdiv(Vd, linalg.norm(R)), length(R)) k = 1. if ll == 20.: st = 1. if verb: home np.disp(np.array(np.hstack((' ', getString(message('signal:invfreqs:ITERATION')), int2str(l))))) np.disp(np.array(np.hstack((getString(message('signal:invfreqs:CurrentFit')), num2str(V1), getString(message('signal:invfreqs:PreviousFit')), num2str(Vcap))))) np.disp(getString(message('signal:invfreqs:CurrentParPrevparGNdir'))) np.disp(np.array(np.hstack((t1, t, gndir)))) np.disp(np.array(np.hstack((getString(message('signal:invfreqs:NormOfGNvector')), num2str(linalg.norm(gndir)))))) if st == 1.: np.disp(getString(message('signal:invfreqs:NoImprovement'))) np.disp(getString(message('signal:invfreqs:IterationsThereforeTerminated'))) t = t1 Vcap = V1 return [b, a] def apolystab(a, realFlag): # Local Variables: a, realFlag, vind, v # Function calls: real, poly, length, apolystab, find, roots #%APOLYSTAB Stabilize filter, analog #% inputs: a - denominator polynomial #% realFlag - 1 for real, 0 for complex #% returns stabilized denoninator polynomial if length(a) > 0.: v = np.roots(a) vind = nonzero((np.real(v) > 0.)) v[int(vind)-1] = -v[int(vind)-1] a = np.poly(v) if realFlag: a = np.real(a) return [a]
def calcPsd(self): self.psd = np.zeros((self.numbPoints)) self.Ls_K = np.zeros((self.numbPoints)) self.Lval_K = np.zeros((self.numbPoints)) #psd and L* final vectors self.E_mu_spec = np.zeros( (self.numbPoints)) #energy radial coverage for the chosen MU for iit in range(self.numbPoints): ############################################################################# #1ST STEP, getting alphaK and E_mu (energy respective to desired K and MU) K_temp = self.Kvalues[iit, :] K_temp[K_temp == -1.00000000e+31] = np.nan temp_nan0 = np.isnan( K_temp) #removing nan values in K prior to the interpolation pos0 = np.where(temp_nan0 == False)[0] if len(pos0) < 2 or self.Kd < np.nanmin( K_temp) or self.Kd > np.nanmax(K_temp): self.psd[iit] = np.nan self.Ls_K[iit] = np.nan self.Lval_K[iit] = np.nan self.E_mu_spec[iit] = np.nan #Kd not defined means that that population is in a region of B with opened field lines #For the same reason, Lstar is no longer defined else: pa_st0 = pos0[0] pa_end0 = pos0[-1] + 1 K_temp2 = K_temp[pa_st0:pa_end0] pae2 = self.pae[pa_st0:pa_end0] fK = interp1d(K_temp2, pae2) #linear fit self.aK = fK(self.Kd) #Alpha for desired K at instant iit #disp(aK) ok, no nan values generated for aK during interpolation ################################################################# #LSTAR step, Linear interpolation over L*(t,alpha) to get L* at desired K, that is, at calculated alphaK #With the assumption that K provided was calculated for local alpha #that is, along the satellite's orbit # Ls_temp=self.Ls#[iit,:] Ls_temp = self.Ls[iit, :] Ls_temp[Ls_temp == -1.00000000e+31] = np.nan temp_nan2 = np.isnan( Ls_temp ) #removing nan values of Ls vector before interpolation pos2 = np.where(temp_nan2 == False)[0] if len(pos2) < 2: self.Ls_K[iit] = np.nan self.Lval_K[iit] = np.nan else: Ls_temp2 = Ls_temp[pos2] pae2 = self.pae[pos2] fLs = interp1d( pae2, Ls_temp2) #linear fit on L*-->y in respect to PA-->x, if self.aK < min(pae2) or self.aK > max(pae2): #ok self.Ls_K[iit] = np.nan else: self.Ls_K[iit] = fLs(self.aK) m0 = 9.11e-31 # electron rest mass Kg c = 3e8 #speed of light in m/s #mc_sq=1e-6*(m0*c**2)/(1.6022e-19)=.511 MeV #electron energy rest in units of MeV p2 = 2 * (.511 / c**2) * (self.B2[iit] * 1e-5) * self.MUd / ( np.sin(np.radians( self.aK))**2) #relativistic momentum squared #B is converted from nT to Gauss delta = (2 * .511)**2 + (4 * p2 * c**2) # print('delta', delta) # print(p2) #solving the second degree equation in respect to E to get the kinetic energy En_mu at desired MU and K E_mu = (-2 * .511 + np.sqrt(delta)) / 2 # self.E_mu_spec[iit] = E_mu np.disp(E_mu) if E_mu < np.nanmin(self.Enm) or E_mu > np.nanmax(self.Enm): self.psd[iit] = np.nan #disp([E_mu,iit]) else: ############################################################################# #2ND STEP, Linear fitting of the pitch angle distribution #to get the flux at aK for different energy channels (all saved in jK) jK = np.zeros((21)) #12 for en in range(21): #12 temp_nan = np.isnan(self.fedu_m2[ iit, :, en]) #finding nan values for flux in funtion of PA pos = np.where(temp_nan == False)[0] if len(pos) == 0: jK[en] = np.nan else: pa_st = pos[0] pa_end = pos[-1] + 1 #disp([pa_st,pa_end]) x = self.pAngle_mageis[pa_st:pa_end] y = self.fedu_m2[iit, pa_st:pa_end, en] if self.aK < x[0] or self.aK > x[-1]: self.psd[iit] = np.nan else: fPAD = interp1d(x, y) jK[en] = fPAD(self.aK) ############################################################################ #3RD STEP, exponential fit between fluxes (jK) and the energy channels #to get j(aK) at E_mu. temp_nan1 = np.isnan( jK ) #removing nan values of jK vector before interpolation pos1 = np.where(temp_nan1 == False)[0] if len(pos1) == 0: self.psd[iit] = np.nan else: jK2 = jK[pos1] Enm2 = self.Enm[pos1] fE = interp1d( Enm2, np.log(jK2) ) #linear fit on ln(jK)-->y in respect to E-->x, VALIDATED!!! if E_mu < min(Enm2) or E_mu > max(Enm2): #ok self.psd[iit] = np.nan else: lnj = fE(E_mu) #log(jK) for E_mu jd = np.e**lnj #interpolated j at alphaK and E_mu ############################################################################# #4TH STEP, interpolated j is finally converted to psd self.psd[iit] = 3.325 * (1e-8) * (jd) / ( E_mu * (E_mu + 2 * .511))
excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/Hopf/Hopf-divided/Hopfstochoutput-pt3-dwnspl1-thresh1.4146 1.4451 1.4912 1.5793-biftype1.mat", 1.0, 1.0, ) excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/Hopf/Hopf-divided/Hopfstochoutput-pt4-dwnspl1-thresh1.4146 1.4451 1.4912 1.5793-biftype1.mat", 1.0, 1.0, ) excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/Hopf/Hopf-divided/Hopfstochoutput-pt5-dwnspl1-thresh1.4146 1.4451 1.4912 1.5793-biftype1.mat", 1.0, 1.0, ) np.disp("Hopf complete") excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/hopfoffset/hopfstochoffsetoutput-pt1-dwnspl1-thresh0.7844 0.9044 0.9691 1.1123-biftype5.mat", 5.0, 1.0, ) excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/hopfoffset/hopfstochoffsetoutput-pt2-dwnspl1-thresh0.7844 0.9044 0.9691 1.1123-biftype5.mat", 5.0, 1.0, ) excitsimcalcamplfreq( "/Volumes/LaCie JDS/simulation data/hopfoffset/hopfstochoffsetoutput-pt3-dwnspl1-thresh0.7844 0.9044 0.9691 1.1123-biftype5.mat", 5.0, 1.0, )