def interpolateBetweenBinaryObjects(obj1, obj2, slices): """ Takes two binary objects and puts slices slices in-between them, each of which contains a smooth binary transition between the objects. """ # constants temporal_dimension = 3 # flip second returned binary objects along temporal axis slicer = [slice(None) for _ in range(obj1.ndim + 1)] slicer[temporal_dimension] = slice(None, None, -1) # logical-and combination ret = __interpolateBetweenBinaryObjects( obj1, obj2, slices) | __interpolateBetweenBinaryObjects( obj2, obj1, slices)[slicer] # control step: see if last volume corresponds to obj2 slicer[temporal_dimension] = slice(-1, None) if not scipy.all(scipy.squeeze(ret[slicer]) == obj2.astype(scipy.bool_)): raise Exception( 'Last created object does not correspond to obj2. Difference of {} voxels.' .format( len( scipy.nonzero( scipy.squeeze(ret[slicer]) & obj2.astype(scipy.bool_))[0]))) return ret
def __init__(self, output='out', input='in', \ mag=None, phase=None, coh=None, \ freqlim=[], maglim=[], phaselim=[], \ averaged='not specified', \ seedfreq=-1, seedphase=0, labels=[], legloc=-1, compin=[]): self.output = output self.input = input if len(compin) > 0: if mag is None: self.mag = squeeze(colwise(abs(compin))) if phase is None: self.phase = squeeze(colwise(arctan2(imag(compin),real(compin))*180.0/pi)) else: self.mag = squeeze(mag) self.phase = squeeze(phase) self.coh = coh self.averaged = averaged self.seedfreq = seedfreq self.seedphase = seedphase self.freqlim = freqlim self.maglim = maglim self.phaselim = phaselim self.labels = labels self.legloc = legloc
def interpolateBetweenBinaryObjects(obj1, obj2, slices): """ Takes two binary objects and puts slices slices in-between them, each of which contains a smooth binary transition between the objects. """ # constants temporal_dimension = 3 # flip second returned binary objects along temporal axis slicer = [slice(None) for _ in range(obj1.ndim + 1)] slicer[temporal_dimension] = slice(None, None, -1) # logical-and combination ret = ( __interpolateBetweenBinaryObjects(obj1, obj2, slices) | __interpolateBetweenBinaryObjects(obj2, obj1, slices)[slicer] ) # control step: see if last volume corresponds to obj2 slicer[temporal_dimension] = slice(-1, None) if not scipy.all(scipy.squeeze(ret[slicer]) == obj2.astype(scipy.bool_)): raise Exception( "Last created object does not correspond to obj2. Difference of {} voxels.".format( len(scipy.nonzero(scipy.squeeze(ret[slicer]) & obj2.astype(scipy.bool_))[0]) ) ) return ret
def CombinedBodes(bodelist): """This function seeks to make one combined rwkbode instance from a list of them. This may be useful in the case of having several experimental Bode data sets with various targeted frequency ranges that need to be treated as one data set.""" docoh=True for cb in bodelist: if cb.coh is None: docoh=False break first=1 # pdb.set_trace() bigcoh=[] for cb in bodelist: if first: first=0 bigmag=colwise(cb.mag) bigphase=colwise(cb.phase) if docoh: bigcoh=colwise(cb.coh) else: bigmag=r_[bigmag,colwise(cb.mag)] bigphase=r_[bigphase,colwise(cb.phase)] if docoh: bigcoh=r_[bigcoh,colwise(cb.coh)] myoutbode=copy.deepcopy(bodelist[0]) myoutbode.mag=squeeze(bigmag) myoutbode.phase=squeeze(bigphase) myoutbode.coh=squeeze(bigcoh) myoutbode.freqlim=[] return myoutbode
def dB_Phase_Error_vect(self,other): """Subtract two bodes dBmag and phase and return a Bode with dBmag and phase.""" outbode=rwkbode() outbode.phase=squeeze(self.phase)-squeeze(other.phase) outbode.dBmag=self.dBmag()-other.dBmag() return outbode
def plotShotData(shot): temp = sqlite3.connect('SIFdata5.db') b = temp.cursor() c_W = scipy.squeeze((b.execute('SELECT c_W from shots2016 where shot='+str(shot))).fetchall()) c_W_l = scipy.squeeze((b.execute('SELECT c_W_l from shots2016 where shot='+str(shot))).fetchall()) val7 = scipy.squeeze((b.execute('SELECT val7 from shots2016 where shot='+str(shot))).fetchall()) time = scipy.squeeze((b.execute('SELECT time from shots2016 where shot='+str(shot))).fetchall()) plt.legend(loc=2,fontsize=20) ax1 = plt.gca() ax2 = ax1.twinx() ax1.semilogy(time,val7/1e3,color='#0E525A',lw=2,label='CSXR line') ax2.semilogy(time,c_W_l,color='#A02C2C',lw=2,label='$c_W$ high $T_e$') ax2.semilogy(time,c_W,color='#228B22',lw=2,label='$c_W$ low $T_e$') #ax1.set_ylim(1e-6,1e-2) plt.legend(loc=2) ax1.set_ylim(1e-2,1e2) ax1.set_xlabel('time [s]') ax2.set_ylabel('$c_W$') ax1.set_ylabel('line intensity [kCounts]') plt.subplots_adjust(bottom=.12,right=.88) limy = plt.gca().get_ylim() limx = plt.gca().get_xlim() plt.gca().text(.9*(limx[1]-limx[0])+limx[0],1.01*(limy[1]-limy[0])+limy[0],str(shot),rotation=0,fontsize=14) plt.show()
def addGIWdata(conn, shot, name='c_W', name2='c_W_l', table='shots2015'): """pulls all time points associated with shot and places in database""" c = conn.cursor() points = scipy.atleast_1d( scipy.squeeze((c.execute('SELECT id from ' + table + ' where shot=' + str(shot))).fetchall())) time = scipy.atleast_1d( scipy.squeeze((c.execute('SELECT time from ' + table + ' where shot=' + str(shot))).fetchall())) #if the shotfile doesn't exist, or if good data at specific timepoints dont exist, set to 0 try: output = goodGIW(time, shot, name) output2 = goodGIW(time, shot, name2) except dd.PyddError: print(shot) output = scipy.zeros(time.shape) output2 = scipy.zeros(time.shape) for i in xrange(len(points)): c.execute('UPDATE ' + table + ' SET "c_W"=' + str(output[i]) + ' WHERE id=' + str(points[i])) c.execute('UPDATE ' + table + ' SET "c_W_l"=' + str(output2[i]) + ' WHERE id=' + str(points[i])) conn.commit()
def Au(U, GF, EpsArr, NX, NY, NZ): """Returns the result of matrix-vector multiplication by the system matrix A=I-GX """ # reshaping input vector into 4-D array Uarr = sci.reshape(U, (NX, NY, NZ, 3)) # extended zero-padded arrays Uext = sci.zeros((2 * NX, 2 * NY, 2 * NZ, 3), complex) Vext = sci.zeros((2 * NX, 2 * NY, 2 * NZ, 3), complex) Jext = sci.zeros((2 * NX, 2 * NY, 2 * NZ, 3), complex) JFext = sci.zeros((2 * NX, 2 * NY, 2 * NZ, 3), complex) Uext[0:NX, 0:NY, 0:NZ, :] = Uarr # contrast current array for s in range(3): Jext[0:NX, 0:NY, 0:NZ, s] = Uext[0:NX, 0:NY, 0:NZ, s] * (EpsArr[0:NX, 0:NY, 0:NZ] - 1.0) JFext[:, :, :, s] = fft.fftn(sci.squeeze(Jext[:, :, :, s])) Vext[:,:,:,0]=Uext[:,:,:,0]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,0,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,0,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,0,2],JFext[:,:,:,2]))) Vext[:,:,:,1]=Uext[:,:,:,1]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,1,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,1,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,1,2],JFext[:,:,:,2]))) Vext[:,:,:,2]=Uext[:,:,:,2]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,2,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,2,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,2,2],JFext[:,:,:,2]))) # reshaping output into column vector V = sci.reshape(Vext[0:NX, 0:NY, 0:NZ, :], (NX * NY * NZ * 3, 1)) return V
def __load_images(label_image_n, mask_image_n, boundary_image_n, fg_image_n = False, bg_image_n = False): """ Load and return all image data in preprocessed ndarrays. The label image will be relabeled to start from 1. @return label, ground-truth-mask, boundary-result-mask[, fg-markers, bg-markers] """ # load images label_image = load(label_image_n) mask_image = load(mask_image_n) boundary_image = load(boundary_image_n) if fg_image_n: fg_image = load(fg_image_n) if bg_image_n: bg_image = load(bg_image_n) # extract image data label_image_d = scipy.squeeze(label_image.get_data()) mask_image_d = scipy.squeeze(mask_image.get_data()).astype(scipy.bool_) boundary_image_d = scipy.squeeze(boundary_image.get_data()).astype(scipy.bool_) if fg_image_n: fg_image_d = scipy.squeeze(fg_image.get_data()).astype(scipy.bool_) if bg_image_n: bg_image_d = scipy.squeeze(bg_image.get_data()).astype(scipy.bool_) # check if images are of same dimensionality if label_image_d.shape != mask_image_d.shape: raise argparse.ArgumentError('The mask image {} must be of the same dimensionality as the label image {}.'.format(mask_image_d.shape, label_image_d.shape)) if label_image_d.shape != boundary_image_d.shape: raise argparse.ArgumentError('The boundary term image {} must be of the same dimensionality as the label image {}.'.format(boundary_image_d.shape, label_image_d.shape)) if fg_image_n: if label_image_d.shape != fg_image_d.shape: raise argparse.ArgumentError('The foreground markers image {} must be of the same dimensionality as the label image {}.'.format(fg_image_d.shape, label_image_d.shape)) if bg_image_n: if label_image_d.shape != bg_image_d.shape: raise argparse.ArgumentError('The background markers image {} must be of the same dimensionality as the label image {}.'.format(bg_image_d.shape, label_image_d.shape)) # relabel the label image to start from 1 label_image_d = medpy.filter.relabel(label_image_d, 1) if fg_image_n: return label_image_d, mask_image_d, boundary_image_d, fg_image_d, bg_image_d else: return label_image_d, mask_image_d, boundary_image_d
def __mul__(self,other): """Multiply two bodes.""" if type(other)==float or type(other)==int: myoutput=copy.deepcopy(self) myoutput.mag=myoutput.mag*other return myoutput myin='in' myout='out' match=1 if self.output==other.input: first=self second=other elif self.input==other.output: first=other second=self else: warnme=1 if (self.input=='in' and self.output=='out') or (other.input=='in' and other.output=='out'): warnme=0 if warnme: print('Warning: multiplying Bodes without a matching input/output pair:\n'+self.output+'/'+self.input+ ' * ' +other.output+'/'+other.input) match=0 first=self second=other if match: myin=first.input myout=first.output myoutput=copy.deepcopy(self) myoutput.input=myin myoutput.output=myout myoutput.mag=squeeze(colwise(first.mag)*colwise(second.mag)) myoutput.phase=squeeze(colwise(first.phase)+colwise(second.phase)) return myoutput
def modify(name='shots2016'): shots = scipy.array([34401, 34664, 34665, 34666, 34667, 34668, 34669, 34670, 34671, 34672, 34673, 34674]) conn = sqlite3.connect(_tablename) oldC = conn.cursor() for i in shots: # extract necessary idx, time, shot c_w_qc and c_w_l idx = scipy.squeeze((oldC.execute('SELECT id from '+name+' where shot='+str(i))).fetchall()) time = scipy.squeeze((oldC.execute('SELECT time from '+name+' where shot='+str(i))).fetchall()) print(time.shape,idx.shape) reAnalyzeShot2(i, conn, idx, time, bounds=True, serial=False) conn.commit()
def yank(name='shots2016'): conn1 = sqlite3.connect(_olddbname) c = conn1.cursor() temp = scipy.squeeze((c.execute('SELECT * FROM ' + name)).fetchall()) data = temp[:, 1:] idx = temp[:, 0].astype(int) print('large dataset yanked') conn2 = sqlite3.connect(_olddbname2) c = conn2.cursor() shot = scipy.squeeze( (c.execute('SELECT shot FROM ' + name)).fetchall()).astype(int) time = scipy.squeeze((c.execute('SELECT time FROM ' + name)).fetchall()) idx2 = scipy.squeeze( (c.execute('SELECT id FROM ' + name)).fetchall()).astype(int) print('shot and time params yanked') shotlist = scipy.zeros(idx.shape).astype(int) timelist = scipy.zeros(idx.shape) for i in xrange(len(idx)): idxtemp = idx2 == idx[i] shotlist[i] = shot[idxtemp][0] timelist[i] = time[idxtemp][0] conn1.close() conn2.close() return shotlist, timelist, data, idx
def rerun(name='shots2016', startidx=0): conn = sqlite3.connect(_tablename) oldConn = sqlite3.connect(_olddb) oldC = oldConn.cursor() # extract necessary idx, time, shot c_w_qc and c_w_l idx = scipy.squeeze((oldC.execute('SELECT id from ' + name)).fetchall()) newstart = scipy.searchsorted(idx, [startidx])[0] time = scipy.squeeze( (oldC.execute('SELECT time from ' + name)).fetchall())[newstart:] c_w_qc = scipy.squeeze( (oldC.execute('SELECT c_W from ' + name)).fetchall())[newstart:] c_w_l = scipy.squeeze( (oldC.execute('SELECT c_w_l from ' + name)).fetchall())[newstart:] shots = scipy.squeeze( (oldC.execute('SELECT shot from ' + name)).fetchall())[newstart:] idx = idx[newstart:] uniq = scipy.unique(shots) for i in uniq: idxin = shots == i # find index which matches current shot reAnalyzeShot(i, conn, name, idx[idxin], time[idxin], c_w_qc[idxin], c_w_l[idxin], bounds=True, serial=False)
def Au(U,GF,EpsArr,NX,NY,NZ): """Returns the result of matrix-vector multiplication by the system matrix A=I-GX """ # reshaping input vector into 4-D array Uarr=sci.reshape(U,(NX,NY,NZ,3)) # extended zero-padded arrays Uext=sci.zeros((2*NX,2*NY,2*NZ,3),complex) Vext=sci.zeros((2*NX,2*NY,2*NZ,3),complex) Jext=sci.zeros((2*NX,2*NY,2*NZ,3),complex) JFext=sci.zeros((2*NX,2*NY,2*NZ,3),complex) Uext[0:NX,0:NY,0:NZ,:]=Uarr # contrast current array s=0 while s<=2: Jext[0:NX,0:NY,0:NZ,s]=Uext[0:NX,0:NY,0:NZ,s]*(EpsArr[0:NX,0:NY,0:NZ]-1.0) JFext[:,:,:,s]=fft.fftn(sci.squeeze(Jext[:,:,:,s])) s=s+1 Vext[:,:,:,0]=Uext[:,:,:,0]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,0,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,0,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,0,2],JFext[:,:,:,2]))) Vext[:,:,:,1]=Uext[:,:,:,1]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,1,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,1,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,1,2],JFext[:,:,:,2]))) Vext[:,:,:,2]=Uext[:,:,:,2]-\ fft.ifftn(sci.squeeze(sci.multiply(GF[:,:,:,2,0],JFext[:,:,:,0])+\ sci.multiply(GF[:,:,:,2,1],JFext[:,:,:,1])+\ sci.multiply(GF[:,:,:,2,2],JFext[:,:,:,2]))) # reshaping output into column vector V=sci.reshape(Vext[0:NX,0:NY,0:NZ,:],(NX*NY*NZ*3,1)) return V
def dB_Phase_Error_sum(self,other, phaseweight=0.1): """Subtract two bodes dBmag and phase and return the sum of the squared error.""" phaseerror = squeeze(self.phase)-squeeze(other.phase) e_phase = (phaseerror**2).sum() dBmagerror = self.dBmag()-other.dBmag() e_dB = (dBmagerror**2).sum() return e_dB + e_phase*phaseweight
def ToComp(self,ind=None): if ind!=None: x=self.mag[ind]*cos(pi/180.0*self.phase[ind]) y=self.mag[ind]*sin(pi/180.0*self.phase[ind]) return x+y*1.0j else: x=squeeze(self.mag)*squeeze(cos(pi/180.0*self.phase)) y=squeeze(self.mag)*squeeze(sin(pi/180.0*self.phase)) return squeeze(x+y*1.0j)
def CalcSpectra(x,y, input=None, output=None): """Calculate Gxx, Gyy, and Gxy. Note that input and output are just labels. x and y are time domain signals.""" N = max(shape(x)) x_fft = squeeze(fft(x, None, 0)*2/N) y_fft = squeeze(fft(y, None, 0)*2/N) Gxx = norm2(x_fft) Gyy = norm2(y_fft) Gxy = (scipy.conj(x_fft))*y_fft return Spectra(input, output, Gxx, Gyy, Gxy)
def AveBodeFromSpectra(specin): Gxxave=squeeze(specin.Gxx.mean(1)) Gxyave=squeeze(specin.Gxy.mean(1)) Gyyave=squeeze(specin.Gyy.mean(1)) Have=Gxyave/Gxxave cohnum=norm2(Gxyave) cohden=Gxxave*Gyyave mybode=rwkbode(input=specin.input, output=specin.output, \ compin=squeeze(Have), coh=squeeze(cohnum/cohden)) return mybode
def __init__(self, config): Surface.__init__(self, config) # Models are stored as dictionaries in .mat format model_dict = loadmat(config['surface_file']) self.components = list(zip(model_dict['means'], model_dict['covs'])) self.n_comp = len(self.components) self.wl = model_dict['wl'][0] self.n_wl = len(self.wl) # Set up normalization method self.normalize = model_dict['normalize'] if self.normalize == 'Euclidean': self.norm = lambda r: norm(r) elif self.normalize == 'RMS': self.norm = lambda r: s.sqrt(s.mean(pow(r, 2))) elif self.normalize == 'None': self.norm = lambda r: 1.0 else: raise ValueError('Unrecognized Normalization: %s\n' % self.normalize) try: self.selection_metric = config['selection_metric'] except KeyError: self.selection_metric = 'Mahalanobis' # Reference values are used for normalizing the reflectances. # in the VSWIR regime, reflectances are normalized so that the model # is agnostic to absolute magnitude. self.refwl = s.squeeze(model_dict['refwl']) self.idx_ref = [ s.argmin(abs(self.wl - w)) for w in s.squeeze(self.refwl) ] self.idx_ref = s.array(self.idx_ref) # Cache some important computations self.Covs, self.Cinvs, self.mus = [], [], [] for i in range(self.n_comp): Cov = self.components[i][1] self.Covs.append( s.array([Cov[j, self.idx_ref] for j in self.idx_ref])) self.Cinvs.append(svd_inv(self.Covs[-1])) self.mus.append(self.components[i][0][self.idx_ref]) # Variables retrieved: each channel maps to a reflectance model parameter rmin, rmax = 0, 10.0 self.statevec = ['RFL_%04i' % int(w) for w in self.wl] self.bounds = [[rmin, rmax] for w in self.wl] self.scale = [1.0 for w in self.wl] self.init = [0.15 * (rmax - rmin) + rmin for v in self.wl] self.idx_lamb = s.arange(self.n_wl) self.n_state = len(self.statevec)
def CalcBiasEror(dsvect,fds,vect,f,zeroorder=True): dsf = len(f)/len(fds) if zeroorder: stepvect=AveSteps(dsvect,dsf) evect=squeeze(vect-stepvect) return f, evect else: n = int((dsf+0.5)/2.0) fb, intv = InterpVector(dsvect,fds,vect,f) evect=squeeze(vect[n:-n])-squeeze(intv) return fb, evect/squeeze(vect[n:-n])
def RenormalizeFactor(excfile,gsfile,channel=None,Nsamp=1,O=None,q=None): if not type(excfile)==list: excfile=[excfile] if not type(gsfile)==list: gsfile=[gsfile] exat=GetAttr(excfile[0]) gsat=GetAttr(gsfile[0]) L=exat['L'] if q==None: q=sc.array([exat['qx'],exat['qy']]) if 'phasex' in exat.keys(): shift=sc.array([exat['phasex']/2.0,exat['phasey']/2.0]) else: shift=sc.array([exat['phase_shift_x']/2.0,exat['phase_shift_y']/2.0]) phi=exat['phi'] neel=exat['neel'] qx,qy,Sq=GetSq(gsfile) kx,ky=sf.fermisea(L,L,shift) qidx=ml.find((qx==q[0])*(qy==q[1])) if O==None: _,O,_,_=GetEigSys(excfile,Nsamp) pk=None sqq=None if channel==None: channel=exat['channel'] if channel=='trans': pk=sc.squeeze(sf.phiktrans(kx,ky,q[0]/L,q[1]/L,[phi,neel])) sqq=sc.real(0.5*(Sq[0,1,qidx]+Sq[0,2,qidx])) elif channel=='long': pkup=sc.squeeze(sf.phiklong(kx,ky,q[0]/L,q[1]/L,1,[phi,neel])) pkdo=sc.squeeze(sf.phiklong(kx,ky,q[0]/L,q[1]/L,-1,[phi,neel])) if (q[0]/L==0.5 and q[1]/L==0.5) or (q[0]/L==0 and q[1]/L==0): pk=sc.zeros(2*sc.shape(pkup)[0]+1,complex) else: pk=sc.zeros(2*sc.shape(pkup)[0],complex) pk[0:2*sc.shape(pkup)[0]:2]=pkup pk[1:2*sc.shape(pkdo)[0]:2]=pkdo if (qx[0]/L==0.5 and q[1]/L==0.5) or (q[0]/L==0 and q[1]/L==0): if neel==0: pk[-1]=0 else: pk[-1]=sum(neel/sf.omega(kx,ky,[phi,neel])) sqq=Sq[0,0,qidx] else: raise(InputFileError('In file \''+excfile+'\', channel=\''+str(channel)+'\'. Should be \'trans\' or \'long\'')) sqe=sc.einsum('i,jik,k->j',sc.conj(pk),O,pk) out=sc.zeros(Nsamp) for n in range(Nsamp): if abs(sqq)<1e-6 or abs(sqe[n])<1e-6: warnings.warn('Probably ill-defined renormalization, returns 1 for sample {0} out of {1}'.format(n,Nsamp),UserWarning) out[n]=1 else: out[n]=sc.real(sqq/sqe[n]) return out
def GetEigSys(filename,gsfile=None,Nsamp=1,channel=None,wavefile=None,q=None): if type(filename)==str: filename=[filename] hfile=h5py.File(filename[0],'r') attr=GetAttr(filename[0]) if channel==None: channel=attr['channel'] dpath,args=GetStat(filename,Nsamp) dat=sc.array(hfile["/rank-1/data-0"]) hfile.close() N=int(sc.shape(dat)[0]/2) L=attr['L'] shift=None if 'phasex' in attr.keys(): shift=[attr['phasex']/2.0,attr['phasey']/2.0] else: shift=[attr['phase_shift_x']/2.0,attr['phase_shift_y']/2.0] H=sc.zeros([Nsamp,N,N],complex) O=sc.zeros([Nsamp,N,N],complex) E=sc.zeros([Nsamp,N]) V=sc.zeros([Nsamp,N,N],complex) for sample,b in enumerate(args): for d in b: hfile=h5py.File(dpath[d][0],'r') dat=hfile[dpath[d][1]] H[sample,:,:]+=dat[0:N,0:2*N:2]+1j*dat[0:N,1:2*N:2] O[sample,:,:]+=dat[N:2*N,0:2*N:2]+1j*dat[N:2*N,1:2*N:2] hfile.close() H[sample,:,:]=0.5*(H[sample,:,:]+sc.conj(H[sample,:,:].T))/len(b) O[sample,:,:]=0.5*(O[sample,:,:]+sc.conj(O[sample,:,:].T))/len(b) if channel=='groundstate': return H fs=None refstate=sc.zeros(2*L*L) refstate[0::2]=1 if wavefile==None: fs=GetFermiSigns(filename[0],refstate,channel=channel) else: fs=GetFermiSigns(wavefile,refstate,channel=channel) for s in range(sc.shape(H)[0]): H[s,:,:]=sc.dot(sc.diag(fs),sc.dot(H[s,:,:],sc.diag(fs))) O[s,:,:]=sc.dot(sc.diag(fs),sc.dot(O[s,:,:],sc.diag(fs))) ren=sc.ones(Nsamp) if gsfile!=None: ren=RenormalizeFactor(filename,gsfile,Nsamp=1,channel=channel,O=O,q=q) print('{0} pair of (H,O) matrices loaded, now diagonalize'.format(sc.shape(H)[0])) H=sc.einsum('ijk,i->ijk',H,ren) O=sc.einsum('ijk,i->ijk',O,ren) for s in range(sc.shape(H)[0]): E[s,:],V[s,:,:]=vln.geneigh(sc.squeeze(H[s,:,:]),sc.squeeze(O[s,:,:])) print('diagonalization finished') return H,O,E,V
def kmeans(feat_mat, freqs, c1=-1, c2=-1, min_size=50, max_iter=20, spherical=True): if c1 == -1: c1, c2 = sp.random.randint(feat_mat.shape[0]), sp.random.randint( 1, feat_mat.shape[0]) c1, c2 = feat_mat[c1], feat_mat[(c1 + c2) % feat_mat.shape[0]] old_indexer = sp.ones(feat_mat.shape[0]) * -1 for _ in range(max_iter): scores = sp.squeeze(sp.asarray(feat_mat.multiply(c1 - c2).sum(1))) if freqs is None: indexer = get_split_wo_freq(scores=scores, min_size=min_size) else: indexer = get_split_w_freq(scores=scores, min_size=min_size, freqs=freqs) if sp.array_equal(indexer, old_indexer): break old_indexer = indexer c1 = feat_mat[indexer].sum(0) c2 = feat_mat[~indexer].sum(0) if spherical: c1 = normalize(c1) c2 = normalize(c2) return indexer
def coarsen_2d_ti(Dmat,di=2): ''' Takes a 2D trainin image and makes it coarser, by constructing a 3D TI based on all coarsened 2D images ''' from scipy import squeeze nx, ny, nz = Dmat.shape ndim3 = di * di x = np.arange(nx) y = np.arange(ny) ix = x[0:(nx - di):di] iy = y[0:(ny - di):di] nx2 = len(ix) ny2 = len(iy) TI = np.zeros( (nx2, ny2, nz, ndim3)) l = -1; for j in range(di): for k in range(di): l = l + 1 TI_small = Dmat[(0 + j)::di, (0 + k)::di, 0] TI[:, :, 0,l] = TI_small[0:nx2, 0:ny2] TI=squeeze(TI) return TI
def return_results(self, pores=None, throats=None, **kwargs): r""" Send results of simulation out the the appropriate locations. This is a basic version of the update that simply sends out the main result (quantity). More elaborate updates should be subclassed. """ if pores is None: pores = self.Ps if throats is None: throats = self.Ts phase_quantity = self._quantity.replace(self._phase.name + '_', '') if phase_quantity not in self._phase.props(): self._phase[phase_quantity] = sp.nan self._phase[phase_quantity][pores] = self[self._quantity][pores] conn_arr = self._net.find_connected_pores(self.Ts) dx = sp.squeeze(sp.diff(self[self._quantity][conn_arr], n=1, axis=1)) g = self['throat.conductance'] rate = sp.absolute(g * dx) if 'throat.rate' not in self._phase.props(): self._phase['throat.rate'] = sp.nan self._phase['throat.rate'][throats] = rate[throats] logger.debug('Results of ' + self.name + ' algorithm have been added to ' + self._phase.name)
def InterpVector(dsvect,fds,vect,f): dsf = len(f)/len(fds) myint = interpolate.interp1d(fds,squeeze(dsvect)) n = int((dsf+0.5)/2.0) fb = f[n:-n] intv = myint(fb) return fb, intv
def preprocessTrainsFromMatFiles(fileNameList): """ preprocessTrainsFromFiles extracts spike trains from a list of .mat files containing spike times, one train of time-stamps per file. The spike trains are preprocessed by calculating interspike intervals and instantaneous firing rates. The output is a dictionary containing the preprocessed data. Example of use: ctrFiles=getFileList(dataDir, prefix="CntSt", suffix=".mat") ctrData= preprocessTrainsFromMatFiles(ctrFiles) """ dataDir = './trenesBursts/aleSpikeTimes/' spikes = list() isis = list() ifrs = list() n = 0 for f in fileNameList: ff = dataDir + f s = sc.squeeze(io.loadmat(ff)["savevar"][0][0]) spikes.append(s) print("Found %d spikes in %s" % (len(s), ff)) isi = sc.zeros(len(s)) ifr = sc.zeros(len(s)) #Note time stamps are in milliseconds isi[1:] = s[1:] - s[:-1] ifr[1:] = 1 / isi[1:] isis.append(isi) ifrs.append(ifr) n = n + 1 return {"spikeTrains": spikes, "ISIs": isis, "IFRs": ifrs}
def crossover_freq(db, freq): #find where the current db value is greater than 0 and the next one is not t1 = squeeze(db > 0.0) #vector of True and False elements for db > 0.0 t2 = r_[t1[1:], t1[0]] #vector t1 shifted by 1 index t3 = (t1 & ~t2) #current value is > 0.0 and the next is not ind = t3.argmax() return freq[ind], ind
def getArrayFromImage(image): """ Returns a scipy array created from the supplied itk image. @note always use this instead of directly the itk.PyBuffer, as that object transposes the image axes. @param image an instance of itk.Image holding the array's data @type itk.Image (instance) @return an array @rtype scipy.ndarray """ #convert itk_py_converter = itk.PyBuffer[getImageType(image)] arr = itk_py_converter.GetArrayFromImage(image) ############ # !BUG: WarpITK is a very critical itk wrapper. Everything returned / created by it # seems to exist only inside the scope of the current function (at least for # ImageFileReader's image and PyBuffers's scipy array. The returned objects have # therefore to be copied once, to survive outside the current scope. ############ arr = arr.copy() # The itk_py_converter transposes the image dimensions. This has to be countered. return scipy.squeeze(scipy.transpose(arr))
def xd_iterator_pool(arr, view, fun, processes=None): """ Same as xd_iterator, but distributes the execution over 'processes' subprocesses. """ # check parameters for dim in view: if dim >= arr.ndim or 0 > dim or not type(dim) == int: raise AttributeError( 'Invalid dimension {} in view. Must be int between 0 and {}.'. format(dim, arr.ndim - 1)) if len(view) >= arr.ndim: raise AttributeError( 'The view should contain less entries than the array dimensionality.' ) # prepare worker pool worker_pool = multiprocessing.Pool(processes) # create list of iterations iterations = [[None] if dim in view else range(arr.shape[dim]) for dim in range(arr.ndim)] # prepare views on array (subvolumes) slicers = [[ slice(None) if idx is None else slice(idx, idx + 1) for idx in indices ] for indices in itertools.product(*iterations)] reference_shape = arr[slicers[0]].shape subvolumes = [scipy.squeeze(arr[slicer]) for slicer in slicers] # excecute subprocesses result_subvolumes = worker_pool.map(fun, subvolumes) for slicer, result_subvolume in itertools.izip(slicers, result_subvolumes): arr[slicer] = result_subvolume.reshape(reference_shape)
def x(self): """return cartesian coordinate values Returns: numpy array of cartesian coordinates in meters """ return scipy.squeeze([self.x0(), self.x1(), self.x2()])
def fake_dataset(self, nvel, vdisp, fbin, sigvel, mass=1., dates=(0., ), vmean=0.): """Creates a fake single-epoch radial velocity data for Monte Carlo simulations. Note that multiple calls will use the same binary properties. Redraw the binary properties to get a new set. Arguments: - `nvel`: number of velocities to draw. - `vdisp`: velocity dispersion of the cluster in km/s. - `fbin`: binary fraction among observed stars. - `sigvel`: array-like of size `nvel` or single number; measurement uncertainty in km/s (undistinguishable from velocity dispersion for single epoch case). - `mass`: array-like of size `nvel` or single number; mass of observed stars in solar masses. - `dates`: iterable with relative dates of observations in years. Creates a one-dimensional single-epoch dataset if the iterable has a length of one. - `vmean`: mean velocity in km/s. """ v_systematic = sp.randn(nvel) * vdisp v_bin_offset = sp.array( [self[:nvel].velocity(mass, time)[0, :] for time in dates]) v_bin_offset[:, sp.rand(nvel) > fbin] = 0. v_meas_offset = sp.randn(v_bin_offset.size).reshape( v_bin_offset.shape) * sp.atleast_1d(sigvel)[:, sp.newaxis] return sp.squeeze(v_systematic[sp.newaxis, :] + v_bin_offset + v_meas_offset)
def Truncate(self, f, fcut, below=True): """Truncate self.Gxx, self.Gyy, and self.Gxy at the index corresponding to f[ind]=fcut. return the truncated frequency vector. This function makes only one cut and keeps all data either above or below fcut. As of June 10, 2015, I am adding the option to keep above by setting below=False.""" fout = copy.copy(f) ind = thresh(f,fcut) if below: fout = fout[0:ind] else: fout = fout[ind:] keys=['Gxx','Gyy','Gxy'] for curkey in keys: curitem = colwise(getattr(self,curkey)) if below: curitem = curitem[0:ind,:] else: curitem = curitem[ind:,:] setattr(self,curkey,squeeze(curitem)) return fout
def plotVol(volume, pts=15, **kwargs): fluxGrid = scipy.squeeze(volume.getFluxGrid()).T datain = scipy.zeros((fluxGrid.shape[0], pts, fluxGrid.shape[1])) for idx in range(pts): datain[:, idx, :] = fluxGrid temp = genCylGrid(plasma.eq.getRGrid(), scipy.linspace(0, 2 * scipy.pi, pts), plasma.eq.getZGrid()) verticies = genVertsFromPixel(temp) hex_type = tvtk.api.tvtk.Hexahedron().cell_type temp = temp.reshape((temp.size / 3, 3)) verticies = verticies.reshape((verticies.size / 8, 8)) sg = tvtk.api.tvtk.UnstructuredGrid(points=temp) sg.set_cells(hex_type, verticies) sg.point_data.scalars = datain.flatten() sg.point_data.scalars.name = 'temp' psi_0 = plasma.eq.getFluxAxis() psi_LCFS = plasma.eq.getFluxLCFS() psi_min = fluxGrid.min() psi_max = fluxGrid.max() v1 = (psi_0 - psi_min) / (psi_max - psi_min) v2 = (psi_LCFS - psi_min) / (psi_max - psi_min) mlab.pipeline.volume(sg)
def gradients(theta, X, Y): theta = sp.matrix(theta).T m = Y.shape[0] grad = ((1 / m) * X.T * (sigmoid(X * theta) - Y)).T grad = sp.squeeze(sp.asarray(grad)) return grad
def __load(picklefile, label): """ Load a pickled testbed as well as the original and label image for further processing. The label image will be relabeled to start with region id 1. @param picklefile the testbed pickle file name @param label the label image file name @return a tuple containing: label: the label image data as ndarray bounding_boxes: the bounding boxes around the label image regions (Note that the the bounding box of a region with id rid is accessed using bounding_boxes[rid - 1]) model_fg_ids: the region ids of all regions to create the foreground model from model_bg_ids: the region ids of all regions to create the background model from eval_ids: the regions to evaluate the regions term on, represented by their ids truth_fg: subset of regions from the eval_ids that are foreground according to the ground-truth truth_bg: subset of regions from the eval_ids that are background according to the ground-truth """ # load and preprocess images label_image = load(label) label_image_d = scipy.squeeze(label_image.get_data()) # relabel the label image to start from 1 label_image_d = medpy.filter.relabel(label_image_d, 1) # extracting bounding boxes bounding_boxes = find_objects(label_image_d) # load testbed with open(picklefile, 'r') as f: model_fg_ids = cPickle.load(f) model_bg_ids = cPickle.load(f) cPickle.load(f) # eval ids truth_fg = cPickle.load(f) truth_bg = cPickle.load(f) return label_image_d, label_image, bounding_boxes, model_fg_ids, model_bg_ids, truth_fg, truth_bg
def xd_iterator_pool(arr, view, fun, processes = None): """ Same as xd_iterator, but distributes the execution over 'processes' subprocesses. """ # check parameters for dim in view: if dim >= arr.ndim or 0 > dim or not type(dim) == int: raise AttributeError('Invalid dimension {} in view. Must be int between 0 and {}.'.format(dim, arr.ndim - 1)) if len(view) >= arr.ndim: raise AttributeError('The view should contain less entries than the array dimensionality.') # prepare worker pool worker_pool = multiprocessing.Pool(processes) # create list of iterations iterations = [[None] if dim in view else range(arr.shape[dim]) for dim in range(arr.ndim)] # prepare views on array (subvolumes) slicers = [[slice(None) if idx is None else slice(idx, idx + 1) for idx in indices] for indices in itertools.product(*iterations)] reference_shape = arr[slicers[0]].shape subvolumes = [scipy.squeeze(arr[slicer]) for slicer in slicers] # excecute subprocesses result_subvolumes = worker_pool.map(fun, subvolumes) for slicer, result_subvolume in itertools.izip(slicers, result_subvolumes): arr[slicer] = result_subvolume.reshape(reference_shape)
def plotVol(volume,pts=15,**kwargs): fluxGrid = scipy.squeeze(volume.getFluxGrid()).T datain = scipy.zeros((fluxGrid.shape[0], pts, fluxGrid.shape[1])) for idx in range(pts): datain[:,idx,:] = fluxGrid temp = genCylGrid(plasma.eq.getRGrid(), scipy.linspace(0,2*scipy.pi,pts), plasma.eq.getZGrid()) verticies = genVertsFromPixel(temp) hex_type = tvtk.api.tvtk.Hexahedron().cell_type temp = temp.reshape((temp.size/3,3)) verticies = verticies.reshape((verticies.size/8,8)) sg = tvtk.api.tvtk.UnstructuredGrid(points=temp) sg.set_cells(hex_type,verticies) sg.point_data.scalars = datain.flatten() sg.point_data.scalars.name = 'temp' psi_0 = plasma.eq.getFluxAxis() psi_LCFS = plasma.eq.getFluxLCFS() psi_min = fluxGrid.min() psi_max = fluxGrid.max() v1 = (psi_0 - psi_min)/(psi_max - psi_min) v2 = (psi_LCFS - psi_min)/(psi_max - psi_min) mlab.pipeline.volume(sg)
def xd_iterator(arr, view, fun): """ Iterates over arr, slicing it into views over the dimensions provided in view and passes the extracted subvolumes to fun. Fun has to return an array of the same shape as it receives. This return array is then used to update the original array arr in-place. Example: Assuming an array of shape arr.shape = (100, 200, 300), we want to iterate over all 2D slices of the first two dimensions, i.e. iterate over the 3rd dimension and getting 300 slices of shape (100, 200) passed to the function fun. So we have to provide in view the dimensions over which we do not want to iterate, i.e. in this case view = [0,1]. """ # check parameters for dim in view: if dim >= arr.ndim or 0 > dim or not type(dim) == int: raise AttributeError('Invalid dimension {} in view. Must be int between 0 and {}.'.format(dim, arr.ndim - 1)) if len(view) >= arr.ndim: raise AttributeError('The view should contain less entries than the array dimensionality.') # create list of iterations iterations = [[None] if dim in view else range(arr.shape[dim]) for dim in range(arr.ndim)] # iterate, create slicer, execute function and collect results for indices in itertools.product(*iterations): slicer = [slice(None) if idx is None else slice(idx, idx + 1) for idx in indices] arr[slicer] = fun(scipy.squeeze(arr[slicer])).reshape(arr[slicer].shape)
def symm(A): """ symmetric decomposition of n square (m, m) ndarrays. Result is a (squeezed) (n, m, m) ndarray. """ if not isinstance(A, ndarray): raise RuntimeError( "input argument is of incorrect type; should be numpy ndarray.") if A.ndim == 2: m = A.shape[0] n = A.shape[1] if m != n: raise RuntimeError( "this function only works for square arrays; yours is (%d, %d)" % (m, n)) A.resize(1, m, n) elif A.ndim == 3: m = A.shape[1] n = A.shape[2] if m != n: raise RuntimeError("this function only works for square arrays") else: raise RuntimeError("this function only works for square arrays") return squeeze(0.5 * (A + A.transpose(0, 2, 1)))
def func(self, params): G0_w = 0.0624 G1_w = 0.0524 G0_p = 0.0434 G1_p = 0.1406 y = 1.0 s = 0.014 phi = params[0] m = params[1] #d = params[2] g = params[2] b_bp = m * (400.0 / self.bio_optical_parameters.wavelengths)**y a_cdom = g * scipy.exp( -s * (self.bio_optical_parameters.wavelengths - 440.0)) #Bb = (phi * self.b_bphi + m * self.b_bm + d * self.b_bd + self.b_bw) Bb = (b_bp + self.b_bw) A = (phi * self.a_phi + a_cdom + self.aw) k = A + Bb Rrs = (G0_w + G1_w * (self.b_bw / k)) * self.b_bw / k + ( G0_p + G1_p * (self.b_bm / k)) * (self.b_bm / k) return scipy.squeeze(Rrs)
def BodeFromSpectra(specin,calccoh=False): H=specin.Gxy/specin.Gxx if calccoh: cohnum=squeeze(norm2(specin.Gxy)) cohden=squeeze(specin.Gxx*specin.Gyy) coh=cohnum/cohden ave=True else: coh=[] ave=False mybode=rwkbode(input=specin.input, output=specin.output, \ compin=squeeze(H), coh=coh, averaged=ave) mybode.Gxx=specin.Gxx mybode.Gyy=specin.Gyy mybode.Gxy=specin.Gxy return mybode
def func(self, params): g0 = 0.084 g1 = 0.17 #g0 = 0.0949 #g1 = 0.0794 y = 1.0 s = 0.014 phi = params[0] m = params[1] #d = params[2] g = params[2] b_bp = m * (400.0 / self.bio_optical_parameters.wavelengths)**y a_cdom = g * scipy.exp( -s * (self.bio_optical_parameters.wavelengths - 440.0)) b_b = (b_bp + self.b_bw) a = (phi * self.a_phi + a_cdom + self.aw) u = b_b / (a + b_b) r_rs_dp = (g0 + g1 * u) * u Rrs = 0.5 * r_rs_dp / (1.0 - 1.5 * r_rs_dp) #Rrs = 0.5 * r_rs_dp / (0.52 + 1.7 * r_rs_dp) return scipy.squeeze(Rrs)
def crossover_freq(db, freq): #find where the current db value is greater than 0 and the next one is not t1=squeeze(db > 0.0)#vector of True and False elements for db > 0.0 t2=r_[t1[1:],t1[0]]#vector t1 shifted by 1 index t3=(t1 & ~t2)#current value is > 0.0 and the next is not ind = t3.argmax() return freq[ind], ind
def plot_reals(self, nr=25, hardcopy=0, hardcopy_filename='reals', nanval=-997799, filternan=1): import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import numpy as np from scipy import squeeze nx, ny, nz = self.par['simulation_grid_size'] nr = np.min((self.par['n_real'], nr)) nsp = int(np.ceil(np.sqrt(nr))) fig = plt.figure(1) sp = gridspec.GridSpec(nsp, nsp, wspace=0.1, hspace=0.1) plt.set_cmap('hot') for i in range(0, nr): ax1 = plt.Subplot(fig, sp[i]) fig.add_subplot(ax1) if filternan==1: self.sim[i][self.sim[i]==nanval] = np.nan D=squeeze(np.transpose(self.sim[i])) plt.imshow(D, extent=[self.x[0], self.x[-1], self.y[0], self.y[-1]], interpolation='none') plt.title("Real %d" % (i + 1)) fig.suptitle(self.method + ' - ' + self.parameter_filename, fontsize=16) if (hardcopy): plt.savefig(hardcopy_filename) plt.show(block=False)
def poly_features(X, p): X_poly = sp.zeros((X.size, p)) for i in range(0, p): X_poly[:, i] = sp.squeeze(sp.array(X))**(i + 1) return sp.matrix(X_poly)
def xd_iterator_add(arr, view, fun, add): """ Same as xd_iterator, but expects an additional argument which is passed to each call of fun and expected to be returned as second return value. """ # check parameters for dim in view: if dim >= arr.ndim or 0 > dim or not type(dim) == int: raise AttributeError( 'Invalid dimension {} in view. Must be int between 0 and {}.'. format(dim, arr.ndim - 1)) if len(view) >= arr.ndim: raise AttributeError( 'The view should contain less entries than the array dimensionality.' ) # create list of iterations iterations = [[None] if dim in view else range(arr.shape[dim]) for dim in range(arr.ndim)] # iterate, create slicer, execute function and collect results for indices in itertools.product(*iterations): slicer = [ slice(None) if idx is None else slice(idx, idx + 1) for idx in indices ] _tmp, add = fun(scipy.squeeze(arr[slicer]), add) arr[slicer] = _tmp.reshape(arr[slicer].shape)
def read_data(filename_, flag=True): """Reads the odometry and sensor readings from a file. Args: filename_: string containing file location Returns: output: A FburgData class which contains the odometry and/or sensor data Raises: NameError: incorrect filepath """ output = {'sensor':[],'odometry':[]} data = scipy.genfromtxt(filename_, dtype='object') idx = scipy.squeeze(data[:,0] == 'ODOMETRY') for inp in data[idx,1:].astype(float): output['odometry'] += [{'r1':inp[0], 't':inp[1], 'r2':inp[2]}] idxarray = scipy.where(idx) idxarray = scipy.append(idxarray,[len(idx)]) for i in xrange(len(idxarray) - 1): temp = [] for j in scipy.arange(idxarray[i] + 1, idxarray[i + 1]): temp += [{'id':int(data[j,1]) - 1, 'range':float(data[j,2]), 'bearing':float(data[j,3])}] output['sensor'] += [temp] return output
def main(): # prepare logger a = [[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]] b = [[[1, 2, 3, 4, 3], [3, 1, 2, 1, 4], [3, 1, 4, 2, 2], [2, 2, 1, 3, 3], [4, 3, 1, 2, 1]]] c = [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 1, 2, 3]], [[4, 5, 6, 7], [8, 9, 1, 2], [3, 4, 5, 6]]] a = scipy.asarray(a) b = scipy.asarray(b) c = scipy.asarray(c) original_image_n = '/home/omaier/Experiments/Regionsegmentation/Evaluation_Viscous/00originalvolumes/o09.nii' original_image = load(original_image_n) original_image_d = scipy.squeeze(original_image.get_data()) original_image_d = original_image_d[0:50, 0:50, 0:50] dir = scipy.zeros(original_image_d.shape).ravel() oir = original_image_d.ravel() for i in range(len(dir)): dir[i] = oir[i] dir = dir.reshape(original_image_d.shape) coa, con, dir = tamura(dir, 5) for i in range(5): print "k=", i + 1, " with ", len((coa == i).nonzero()[0])
def getArrayFromImage(image): """ Returns a numpy ndarray created from the supplied itk image. Parameters ---------- image : itk.Image (instance) An instance of itk.Image holding the array's data. Returns ------- arr : ndarray The image as numpy array. Notes ----- Always use this instead of directly the itk.PyBuffer, as that object transposes the image axes. """ #convert itk_py_converter = itk.PyBuffer[getImageType(image)] arr = itk_py_converter.GetArrayFromImage(image) ############ # !BUG: WarpITK is a very critical itk wrapper. Everything returned / created by it # seems to exist only inside the scope of the current function (at least for # ImageFileReader's image and PyBuffers's scipy array. The returned objects have # therefore to be copied once, to survive outside the current scope. ############ arr = arr.copy() # The itk_py_converter transposes the image dimensions. This has to be countered. return scipy.squeeze(scipy.transpose(arr))
def func(self, params): """ For doing the forward model """ aphi440 = params[0] ag440 = params[1] bbp555 = params[2] y = params[3] G0_w = 0.0624 G1_w = 0.0524 G0_p = 0.0434 G1_p = 0.1406 self.read_aw_from_file() self.read_bw_from_file() aphi = self.a_phi * aphi440 ag = self.bio_optical_parameters.build_a_cdom(ag440, self.s, wave_const=440) bbp = self.bio_optical_parameters.build_bbp(bbp555, y, wave_const=555) a = self.aw + aphi + ag bb = self.bw + bbp u = bb / (a + bb) rrs = self.g0 * u + self.g1 * u ** 2 rrs = 0.5 * rrs / (1 - 1.5 * rrs) # k = a + bb #Rrs = (G0_w + G1_w * (self.bw / k)) * self.bw / k + (G0_p + G1_p * (self.bb_m / k)) * (self.bb_m / k) return scipy.squeeze(rrs)
def Sy(self, meas, geom): """Calculate measurement error covariance. Input: meas, the instrument measurement Returns: Sy, the measurement error covariance due to instrument noise """ if self.model_type == 'SNR': bad = meas < 1e-5 meas[bad] = 1e-5 nedl = (1.0 / self.snr) * meas return pow(s.diagflat(nedl), 2) elif self.model_type == 'parametric': nedl = abs(self.noise[:, 0] * s.sqrt(self.noise[:, 1] + meas) + self.noise[:, 2]) nedl = nedl / s.sqrt(self.integrations) return pow(s.diagflat(nedl), 2) elif self.model_type == 'pushbroom': if geom.pushbroom_column is None: C = s.squeeze(self.covs.mean(axis=0)) else: C = self.covs[geom.pushbroom_column, :, :] return C / s.sqrt(self.integrations)
def skewMatrixOfVector(w): """ skewMatrixOfVector(w) given a (3, n) ndarray, w, of n hstacked axial vectors, computes the associated skew matrices and stores them in an (n, 3, 3) ndarray. Result is (3, 3) for w.shape = (3, 1) or (3, ). See also: vectorOfSkewMatrix """ dims = w.ndim stackdim = 0 if dims == 1: if len(w) != 3: raise RuntimeError, 'input is not a 3-d vector' else: w = vstack(w) stackdim = 1 elif dims == 2: if w.shape[0] != 3: raise RuntimeError, 'input is of incorrect shape; expecting shape[0] = 3' else: stackdim = w.shape[1] else: raise RuntimeError, 'input is incorrect shape; expecting ndim = 1 or 2' zs = zeros((1, stackdim), dtype='float64') W = vstack( [zs, -w[2, :], w[1, :], w[2, :], zs, -w[0, :], -w[1, :], w[0, :], zs]) return squeeze(reshape(W.T, (stackdim, 3, 3)))
def __init__(self, x_hat, s, flag=False): """ """ s = scipy.array(s) self.unit = unit(x_hat) self.s = scipy.squeeze(s) self.flag = flag
def BodeFromComp(compin, expbode, freq, PhaseMassage=True): """Create an rwkbode instance from a complex vector and an example bode. This would typically be used in conjunction with a symbolic bode function that outputs complex values, where an expbode is known to have the same input and output and desired seedfreq and seedphase. The freq input is used to run PhaseMassage on the created rwkbode instance.""" tbode=rwkbode(expbode.output,expbode.input,compin=compin) tbode.seedfreq=expbode.seedfreq tbode.seedphase=expbode.seedphase tbode.freqlim=expbode.freqlim if (tbode.seedfreq>0) & PhaseMassage: # Pdb().set_trace() tbode.PhaseMassage(freq) tbode.phase=squeeze(tbode.phase) tbode.mag=squeeze(tbode.mag) return tbode
def plot_eas(Deas): ''' Plot data directly form EAS ''' import matplotlib.pyplot as plt import numpy as np from scipy import squeeze # check for matrix/cube if 'Dmat' in Deas: if (Deas['dim']['nz']==1): if (Deas['dim']['ny']==1): plt.plot(np.arange(Deas['dim']['nx']),Deas['Dmat']) elif (Deas['dim']['nx']==1): plt.plot(np.arange(Deas['dim']['ny']),Deas['Dmat']) else: # X-Y plt.imshow(np.transpose(Deas['Dmat'][:,:,0])) plt.xlabel('X') plt.ylabel('Y') elif (Deas['dim']['ny']==1): if (Deas['dim']['nz']==1): plt.plot(np.arange(Deas['dim']['nx']),Deas['Dmat']) elif (Deas['dim']['nx']==1): plt.plot(np.arange(Deas['dim']['nz']),Deas['Dmat']) else: # X-Z plt.imshow(squeeze(Deas['Dmat'][:,0,:])) plt.xlabel('X') plt.xlabel('Z') elif (Deas['dim']['nx']==1): if (Deas['dim']['ny']==1): plt.plot(np.arange(Deas['dim']['nz']),Deas['Dmat']) elif (Deas['dim']['nz']==1): plt.plot(np.arange(Deas['dim']['ny']),Deas['Dmat']) else: # Y-Z plt.imshow(squeeze(Deas['Dmat'][0,:,:])) plt.xlabel('Y') plt.xlabel('Z') else: plot_3d_vtk(Deas['Dmat']) else: # scatter plot print('EAS scatter plot not yet implemented')