def kabsch(mol0, mol1): # translate to align centroids with origin mol0, d0 = setPdistance(mol0, mol0.centersym(), [0, 0, 0], 0) mol1, d1 = setPdistance(mol1, mol1.centersym(), [0, 0, 0], 0) # get coordinates and matrices P,Q P, Q = [], [] for atom0, atom1 in zip(mol0.getAtoms(), mol1.getAtoms()): P.append(atom0.coords()) Q.append(atom1.coords()) # Computation of the covariance matrix C = dot(transpose(P), Q) # Computation of the optimal rotation matrix # This can be done using singular value decomposition (SVD) # Getting the sign of the det(V)*(W) to decide # whether we need to correct our rotation matrix to ensure a # right-handed coordinate system. # And finally calculating the optimal rotation matrix U # see http://en.wikipedia.org/wiki/Kabsch_algorithm V, S, W = svd(C) d = (det(V) * det(W)) < 0.0 # Create Rotation matrix U if d: S[-1] = -S[-1] V[:, -1] = -V[:, -1] U = dot(V, W) # Rotate P P = dot(P, U) # write back coordinates for i, atom in enumerate(mol0.getAtoms()): atom.setcoords(P[i]) return mol0, U.tolist(), d0, d1
def plot_Peierls_Hubbard_green(u=0.0): ''' plot the Det(G) in the plane dt-w''' L = 6 num = 51 d_num = 20 ws = np.linspace(-3,3,num) Gd = np.zeros((num, d_num), dtype=np.complex) for j, d in enumerate(np.linspace(-1,1,d_num)): H = Peierls_Hubbard(L, t=1., dt=d, U=u, pbc=False) evals, evecs = func.solve_hamiltonian(H) Gwk = np.zeros(num, dtype=np.complex) Gij_up = func.green(ws, op.fermion_up(), L, evals, evecs) Gij_down = func.green(ws, op.fermion_down(), L, evals, evecs) for i in range(num): Gwk[i] = det(Gij_up[i])*det(Gij_down[i]) Gd[:,j] = Gwk # plt.figure("Green u=%.2f"%d) # plt.plot(ws, np.real(Gwk),'o-') # plt.ylim([-1,1]) # print np.min( np.imag(Gwk) ), np.max( np.imag(Gwk) ) plt.figure("poles") fig = plt.imshow(np.real(Gd), interpolation='nearest', aspect=0.4) fig.set_clim(-1,1) plt.colorbar(ticks=[-1,0,1],label="Det(G)") plt.xticks([0.5,d_num/2.,d_num-0.5], [-1,0,1], fontsize=20) plt.yticks([0.5,num/2.,num-0.5], [3,0,-3], fontsize=20) plt.xlabel('d', fontsize=20) plt.ylabel('w', fontsize=20)
def ComputeIndexLda(proj, data, dataLabels): # takken from page 31 of book on GGOBI # diane cook "integrative and dynamic graphics for data analysis ndim = proj.shape[0] A = matrix(proj) B = matrix(zeros((ndim, ndim))) W = matrix(zeros((ndim, ndim))) grand_mean = mean(data , axis = 0).astype(matrix) for c in unique(dataLabels): ind = dataLabels ==c group_mean = mean(data[ind, :], axis = 0).astype(matrix) d = group_mean - grand_mean B += ind[ind].size*dot(d[:,newaxis], d[newaxis, :]) # find better: d = data[ind, :] - group_mean for j in range(data[ind, :].shape[0]): W+=dot(d[j,:, newaxis], d[j,newaxis,:]) #~ print 'A', A, A.size #~ print 'B', B, B.size #~ print 'W', W, W.size lda = 1 - linalg.det(A.T*W*A) / linalg.det(A.T*(W+B)*A) return lda
def update(self, x, y): tmp_X = self.X tmp_y = self.y tmp_reg_K = self.reg_K self.X = vstack((self.X, x)) self.y = hstack((self.y, y)) n = self.X.shape[0] if (n <= th): if (n != 1): row = zeros((1, n-1)) col = zeros((n, 1)) self.reg_K = vstack((self.reg_K, row)) self.reg_K = hstack((self.reg_K, col)) self.add_b(x, n) if det(self.reg_K) == 0: self.X = tmp_X self.y = tmp_y self.reg_K = tmp_reg_K return self.reg_K_inv = self.reg_K.getI() else: self.X = delete(self.X, 0, 0) self.y = delete(self.y, 0, 0) self.updateK(x) if det(self.reg_K) is 0: self.X = tmp_X self.y = tmp_y self.reg_K = temp_reg_K return self.updateKInv()
def dkl_wishart(a1,B1,a2,B2): """ returns the KL divergence bteween two Wishart distribution of parameters (a1,B1) and (a2,B2), where a1 and a2 are degrees of freedom B1 and B2 are scale matrices """ from scipy.special import psi,gammaln from numpy.linalg import det,pinv tiny = 1.e-15 # fixme: check size dim = B1.shape[0] d1 = max(det(B1),tiny) d2 = max(det(B2),tiny) lgc = dim*(dim-1)*np.log(np.pi)/4 lg1 = lgc lg2 = lgc lw1 = -np.log(d1) + dim*np.log(2) lw2 = -np.log(d2) + dim*np.log(2) for i in range(dim): lg1 += gammaln((a1-i)/2) lg2 += gammaln((a2-i)/2) lw1 += psi((a1-i)/2) lw2 += psi((a2-i)/2) lz1 = 0.5*a1*dim*np.log(2)-0.5*a1*np.log(d1)+lg1 lz2 = 0.5*a2*dim*np.log(2)-0.5*a2*np.log(d2)+lg2 dkl = (a1-dim-1)*lw1-(a2-dim-1)*lw2-a1*dim dkl += a1*np.trace(np.dot(B2,pinv(B1))) dkl /=2 dkl += (lz2-lz1) return dkl
def genGraph(S_actual, S_est, S_previous, empCov_set, nodeID, e1, e2, e3, e4, display = False): D = np.where(S_est != 0)[0].shape[0]#len(numpy.where(S_est == 0)[0]) T = np.where(S_actual != 0)[0].shape[0] # print np.where(S_actual != 0)[0] TandD = float(np.where(np.logical_and(S_actual,S_est) == True)[0].shape[0]) P = TandD/D R = TandD/T offDiagDiff = S_actual - S_est offDiagDiff = offDiagDiff - np.diag(np.diag(offDiagDiff)) S_diff = (S_est - S_previous) S_diff = S_diff - np.diag(np.diag(S_diff)) ind = (S_diff < 1e-2) & (S_diff > - 1e-2) S_diff[ind] = 0 K = np.count_nonzero(S_diff) e1.append(-np.log(alg.det(S_est)) + np.trace(np.dot(S_est, empCov_set[nodeID])) + K) # e1.append( alg.norm(offDiagDiff, 'fro')) e2.append(2* P*R/(P+R)) K = float(np.where(np.logical_and((S_est>0) != (S_previous>0), S_est>0) == True)[0].shape[0]) e3.append(-np.log(alg.det(S_est)) + np.trace(np.dot(S_est, empCov_set[nodeID])) + K) e4.append(alg.norm(S_est - S_previous, 'fro')) if display == True: if (nodeID >timeShift -10) and (nodeID < timeShift + 10): print 'nodeID = ', nodeID print 'S_true = ', S_actual,'\nS_est', S_est # print 'S_error = ',S_actual - S_est, '\n its Fro error = ', alg.norm(S_actual - S_est, 'fro') print 'D = ',D,'T = ', T,'TandD = ', TandD,'K = ', K,'P = ', P,'R = ', R,'Score = ', 2* P*R/(P+R) return e1, e2, e3, e4
def findmin(M,B,run): #normal routine for varying a single element at a time. '''Finds minimum cost for the lattice by varying the integers of m, in the element that gives steepest descent The 'run' indicates the cost function to use''' maxsteps = 10000 istep = 1 M = minkM(M,B)#;print'Mink reduced M'; print M while istep<maxsteps: # sys.exit('stop') bestindex = changewhich(M,B,run) if bestindex[2]==0:#found minimum # newcost = cost(M,B,run) break else: M[bestindex[0],bestindex[1]] += bestindex[2] # newcost = cost(M,B,run) if run == 'minsvsym' and B.Nmesh/float(det(M))>1.2: M = M*2 # open up search space when when det(M) gets too low # print; print M;print newcost istep += 1 if istep < maxsteps: # print 'Found minimum after %i steps' % istep # print 'Best M'; print M K = lattice();K.vecs = trimSmall(dot(B.vecs,inv(M)));K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det else: print 'Ended without minimum after maximum %i steps' % istep sys.exit('Stop') return [trimSmall(M),K]
def InitializeElectrons(self): if self.N_up == 0 and self.N_down == 0: print 'Error: no electrons to initialize' return [] else: # generate array of electron positions, normally distributed from the origin with Bohr radius n = self.N_up+self.N_down self.e_positions = np.random.randn(n,3) * GSF.a_B # generate array of electron positions print 'init e_pos',self.e_positions # Store displacements and distances self.e_disp = np.zeros((n,n,3)) # store the displacements in a 3D matrix to make indexing easier self.e_dist = np.zeros((n,n)) # the electron matrices should only be upper diagonal self.atom_disp = np.zeros((n,len(self.atom_list),3)) self.atom_dist = np.zeros((n,len(self.atom_list))) index = 0 for i in range(n): self.e_disp[i,i+1:] = self.e_positions[i] - self.e_positions[i+1:] self.e_dist[i,i+1:] = np.sqrt(np.sum(self.e_disp[i,i+1:]**2,1)) self.atom_disp[i] = self.e_positions[i] - self.ion_positions self.atom_dist[i,:] = np.sqrt(np.sum(self.atom_disp[i,:]**2,1)) #Once the e_position is initialize, the slater matrix and its deteriminant and inverse are all initialized. self.slater_matrix_up = SlaterMatrix(self.e_positions[0:self.N_up],self.psi_up) self.slater_matrix_down = SlaterMatrix(self.e_positions[self.N_up:],self.psi_down) print 'slater_matrix',self.slater_matrix_up if self.N_up>0: self.inverse_SD_up = LA.inv(self.slater_matrix_up) self.slater_det_up = LA.det(self.slater_matrix_up) if self.N_down>0: self.inverse_SD_down = LA.inv(self.slater_matrix_down) self.slater_det_down = LA.det(self.slater_matrix_down) self.J = self.Jastrow() print 'slater_inv',self.inverse_SD_up return self.e_positions
def naiveBayes(): global tSRiver, tSLand, im, outimg covRiver, covLand = cov(tSRiver, rowvar=0), cov(tSLand, rowvar=0) icr, icl= inv(covRiver), inv(covLand) meanRiver, meanLand = mean(tSRiver, axis=0), mean(tSLand, axis=0) for i in range(512): for j in range(512): devRiver, devLand = subtract(im[i,j], meanRiver), subtract(im[i,j], meanLand) rivClass = dot(dot(devRiver, icr), devRiver.T) nrivClass = dot(dot(devLand, icl), devLand.T) try: p1 = Decimal(-0.5)/Decimal(m.sqrt(det(covRiver))) * Decimal(m.exp(rivClass)) except OverflowError: outimg[i,j] = 255 continue# as e: print(e, 'Variable in exp: ', rivClass) try: p2 = Decimal(-0.5)/Decimal(m.sqrt(det(covLand))) * Decimal(m.exp(nrivClass)) except OverflowError: continue# as e: # print(e, 'Variable in exp: ', rivClass, ' Setting pixel to 0') class1 = Decimal(P1)*p1 class2 = Decimal(P2)*p2 if class1 >= class2: outimg[i,j] = 255
def diwishart (X,v,S,return_log=False): is_square(X) k = X.ndim if not return_log: return np.exp(-0.5*(S*la.inv(X)).trace()) * pow(la.det(X),(-(v+k+1)/2.0)) * pow(la.det(S),(v/2.0)) / ( pow(2,(v*k/2.0)) * pow(pi,(k*(k-1)/4.0))* gamma((v+1-np.array(range(1,k)))/2.0).prod()) else: return -0.5*(S*la.inv(X)).trace() + np.log(la.det(X))*(-(v+k+1)/2.) +np.log(la.det(S))*(v/2.0) - np.log(2)*(v*k/2.0) -np.log(pi)*(k*(k-1)/4.0) - lgamma((v+1-np.array(range(1,k)))/2.0).sum()
def _calculate_log_likelihood(self): #if self.m == None: # Give error message R = zeros((self.n, self.n)) X,Y = array(self.X), array(self.Y) thetas = 10.**self.thetas for i in range(self.n): for j in arange(i+1,self.n): R[i,j] = (1-self.nugget)*e**(-sum(thetas*(X[i]-X[j])**2.)) #weighted distance formula R = R + R.T + eye(self.n) self.R = R one = ones(self.n) try: self.R_fact = cho_factor(R) rhs = vstack([Y, one]).T R_fact = (self.R_fact[0].T,not self.R_fact[1]) cho = cho_solve(R_fact, rhs).T self.mu = dot(one,cho[0])/dot(one,cho[1]) self.sig2 = dot(Y-dot(one,self.mu),cho_solve(self.R_fact,(Y-dot(one,self.mu))))/self.n #self.log_likelihood = -self.n/2.*log(self.sig2)-1./2.*log(abs(det(self.R)+1.e-16))-sum(thetas) self.log_likelihood = -self.n/2.*log(self.sig2)-1./2.*log(abs(det(self.R)+1.e-16)) except (linalg.LinAlgError,ValueError): #------LSTSQ--------- self.R_fact = None #reset this to none, so we know not to use cholesky #self.R = self.R+diag([10e-6]*self.n) #improve conditioning[Booker et al., 1999] rhs = vstack([Y, one]).T lsq = lstsq(self.R.T,rhs)[0].T self.mu = dot(one,lsq[0])/dot(one,lsq[1]) self.sig2 = dot(Y-dot(one,self.mu),lstsq(self.R,Y-dot(one,self.mu))[0])/self.n self.log_likelihood = -self.n/2.*log(self.sig2)-1./2.*log(abs(det(self.R)+1.e-16))
def from_fsl(cls, xfm, infile, reffile): """ Converts fsl transform xfm (estimated FROM infile TO reffile) to a pycortex COORD transform. """ ## Adapted from dipy.external.fsl.flirt2aff############################# import nibabel import numpy.linalg as npl try: inIm = nibabel.load(infile) except AttributeError: inIm = infile refIm = nibabel.load(reffile) in_hdr = inIm.get_header() ref_hdr = refIm.get_header() # get_zooms gets the positive voxel sizes as returned in the header inspace = np.diag(in_hdr.get_zooms()[:3] + (1,)) refspace = np.diag(ref_hdr.get_zooms()[:3] + (1,)) # Since FSL does not use the full transform info in the nifti header, # determine whether the transform indicates that the X axis should be # flipped; if so, flip the X axis (for both infile and reffile) if npl.det(in_hdr.get_best_affine())>=0: inspace = np.dot(inspace, _x_flipper(in_hdr.get_data_shape()[0])) if npl.det(ref_hdr.get_best_affine())>=0: refspace = np.dot(refspace, _x_flipper(ref_hdr.get_data_shape()[0])) inAffine = inIm.get_affine() inv = np.linalg.inv coord = np.dot(inv(refspace),np.dot(xfm,np.dot(inspace,inv(inAffine)))) return cls(coord, refIm)
def marginal(self, X, a, B, c, m): d = float(X.shape[0]) px1 = (c / (math.pi * (1 + c)))**(d/2) px2 = det(B + c/(1+c) * (X - m) * (X - m).T)**(-(a+1)/2) / det(B)**(-a/2) px3 = math.exp(np.sum( math.log(gamma( (a+1)/2 + (1 - np.arange(1.0, d))/2 )) - math.log(gamma( a/2 + (1 - np.arange(1.0, d))/2 )) )) px = px1 * px2 * px3 return px
def circleFromPoints(p1, p2, p3): """ This function takes three points in the image and returns the center and radius of the circle passing through these three points """ x1, y1 = p1 x2, y2 = p2 x3, y3 = p3 centerx = det(np.array(([x1**2+y1**2,y1,1],[x2**2+y2**2,y2,1],[x3**2+y3**2,y3,1]))) / float(2*det(np.array(([x1,y1,1],[x2,y2,1],[x3,y3,1])))) centery = det(np.array(([x1,x1**2+y1**2,1],[x2,x2**2+y2**2,1],[x3,x3**2+y3**2,1]))) / float(2*det(np.array(([x1,y1,1],[x2,y2,1],[x3,y3,1])))) radius = sqrt( pow(x2 - centerx, 2) + pow(y2-centery, 2)) # Alternative: # offset = pow(x2, 2) +pow(y2, 2) # bc = ( pow(x1, 2) + pow(y1, 2) - offset )/2.0 # cd = (offset - pow(x3, 2) - pow(y3, 2))/2.0 # det = (x1 - x2) * (y2 - y3) - (x2 - x3)* (y1 - y2) # TOL = 0.0000001 # if (abs(det) < TOL): # print "POINTS TOO CLOSE" # return ((0, 0), 0) # idet = 1/det # centerx = (bc * (y2 - y3) - cd * (y1 - y2)) * idet # centery = (cd * (x1 - x2) - bc * (x2 - x3)) * idet # print centerx, centery, radius return ((centerx, centery), radius)
def to_fsl(self, infile): """ Converts a pycortex transform to an FSL transform. The resulting FSL transform goes FROM the space of the "infile" input TO the space of the reference nifti stored in the pycortex transform. This should ONLY be used for "coord" transforms! Will fail hard for "magnet" transforms! """ import nibabel import numpy.linalg as npl try: inIm = nibabel.load(infile) except AttributeError: inIm = infile in_hdr = inIm.get_header() ref_hdr = self.reference.get_header() # get_zooms gets the positive voxel sizes as returned in the header inspace = np.diag(in_hdr.get_zooms()[:3] + (1,)) refspace = np.diag(ref_hdr.get_zooms()[:3] + (1,)) # Since FSL does not use the full transform info in the nifti header, # determine whether the transform indicates that the X axis should be # flipped; if so, flip the X axis (for both infile and reffile) if npl.det(in_hdr.get_best_affine())>=0: print("Determinant is > 0: FLIPPING!") inspace = np.dot(inspace, _x_flipper(in_hdr.get_data_shape()[0])) if npl.det(ref_hdr.get_best_affine())>=0: print("Determinant is > 0: FLIPPING!") refspace = np.dot(refspace, _x_flipper(ref_hdr.get_data_shape()[0])) inAffine = inIm.get_affine() inv = np.linalg.inv fslx = np.dot(refspace,np.dot(self.xfm,np.dot(inAffine,inv(inspace)))) return fslx
def circum_center_radius_plane(xy1, xy2, xy3): x1, y1 = xy1 x2, y2 = xy2 x3, y3 = xy3 xxyy1 = x1**2 + y1**2 xxyy2 = x2**2 + y2**2 xxyy3 = x3**2 + y3**2 a = det( np.array([[x1,y1,1], [x2,y2,1], [x3,y3,1]]) ) if fne(a,0): bx = - det( np.array([[xxyy1,y1,1], [xxyy2,y2,1], [xxyy3,y3,1]]) ) by = det( np.array([[xxyy1,x1,1], [xxyy2,x2,1], [xxyy3,x3,1]]) ) c = - det( np.array([[xxyy1,x1,y1], [xxyy2,x2,y2], [xxyy3,x3,y3]]) ) center = (-bx/(2*a), -by/(2*a)) radius = sqrt(bx**2+by**2-4*a*c)/(2*fabs(a)) return center, radius else: return None, None
def marginal_log_likelihood(y, A,Q,C,R, _x,_P): """ compute the marginal log likelihood given the learned model parameters and the learned expected sufficient statistics C must be invertible (:= square and non-singular) eg aint no inverse to a f*****g projection p(y|params) = ... """ if True: return 0 T,p = y.shape _,d = _x.shape Cy = mul(inv(C), tp(y)) CRC = inv(mul(tp(C),inv(R),C)) Z_swap = (1/2) * (log(det(2*pi*CRC)) - log(det(2*pi*R))) covar = CRC+identity(d) Z_merge = log(2*pi*det(covar))**(-d/2) - (1/2)*mul(tp(Cy), covar, Cy) for t in r(1,T-1): mean = _x[t+1] covar = CRC + _P[t+1] Z_merge += log(2*pi*det(covar))**(-d/2) - (1/2)*mul(tp(Cy-mean), covar, (Cy-mean)) return sum(T*Z_swap + Z_merge)
def read_data_inhomogeneous(iom, blockid=0): r""" :param iom: An :py:class:`IOManager` instance providing the simulation data. :param blockid: The data block from which the values are read. """ parameters = iom.load_parameters() timegrid = iom.load_inhomogwavepacket_timegrid(blockid=blockid) dt = parameters["dt"] if "dt" in parameters else 1.0 # Filter time = timegrid * dt time = where(timegrid < 0, nan, time) Pis = iom.load_inhomogwavepacket_parameters(blockid=blockid) # List with Pi time evolutions Phist = [] Qhist = [] Shist = [] phist = [] qhist = [] for q, p, Q, P, S in Pis: qhist.append(array([norm(q[i, :]) for i in range(q.shape[0])]).reshape(-1)) phist.append(array([norm(p[i, :]) for i in range(p.shape[0])]).reshape(-1)) Qhist.append(array([det(Q[i, :, :]) for i in range(Q.shape[0])]).reshape(-1)) Phist.append(array([det(P[i, :, :]) for i in range(P.shape[0])]).reshape(-1)) Shist.append(S.reshape(-1)) return (time, qhist, phist, Qhist, Phist, Shist)
def Wishart_eval(n,V,W): """ Evaluation of the probability of W under Wishart(n,V) Parameters ---------- n (scalar) = the number of degrees of freedom (dofs) V = array of shape (n,n) the scale matrix W: array of shape (n,n): the Wishart draw Returns ------- (float) the density """ # check that shape(V)==shape(W) from numpy.linalg import det,pinv from scipy.special import gammaln p = V.shape[0] ldW = np.log(det(W))*(n-p-1)/2 ltr = - np.trace(np.dot(pinv(V),W))/2 la = ( n*p*np.log(2) + np.log(det(V))*n )/2 lg = np.log(np.pi)*p*(p-1)/4 for j in range(p): lg += gammaln((n-j)/2) lt = ldW + ltr -la -lg return np.exp(lt)
def nk_bhatta(X1, X2, eta): # Returns the non-kernelized Bhattacharrya #I.e. fits normal distributions in input space and calculates Bhattacharrya overlap between them (n1, d1) = X1.shape (n2, d ) = X2.shape assert d1 == d mu1 = mat.sum(X1,0) / n1 mu2 = mat.sum(X2,0) / n2 X1c = X1 - mat.tile(mu1, (n1,1)) X2c = X2 - mat.tile(mu2, (n2,1)) Eta = mat.eye(d) * eta S1 = X1c.T * X1c / n1 + Eta S2 = X2c.T * X2c / n2 + Eta mu3 = .5 * (S1.I * mu1.T + S2.I * mu2.T).T S3 = 2 * (S1.I + S2.I).I d1 = la.det(S1) ** -.25 d2 = la.det(S2) ** -.25 d3 = la.det(S3) ** .5 dterm = d1 * d2 * d3 e1 = -.25 * mu1 * S1.I * mu1.T e2 = -.25 * mu2 * S2.I * mu2.T e3 = .5 * mu3 * S3 * mu3.T eterm = math.exp(e1 + e2 + e3) return float(dterm * eterm)
def compDklgaussian(mu1, C1, mu2, C2): ''' mu1 and C1 specify a multidimensional gaussian. mu2 and C2 specify another one (of the same dimension). Return is a float giving the KL divergence between the two Gaussians. Inputs have to be matrix instances, and the mu inputs should be in row vector shape (mu.shape[0] = 1, mu.shape[1] > 1) ''' n = mu1.size b = mu2 - mu1 C2inv = la.inv(C2) C1sqrt = np.mat(sqrtPSDm(C1)) Term1 = C1sqrt * C2inv * C1sqrt Term2 = b.transpose() * C2inv * b det1 = la.det(C1) det2 = la.det(C2) tol = 1e8 if (la.cond(C1) > tol) | (la.cond(C2) > tol): print('Determinants not non-zero. Ignoring determinants.') Term3 = 0 else: Term3 = .5 * np.log(det2 / det1) d = .5 * np.trace(Term1) + .5 * Term2 - .5 * n + Term3 return d[0, 0]
def __init__(self, mean, sigma): """ @param mean: mean of Gaussian density @type mean: float or array of type 'd' @param sigma: sigma of Gaussian density @type sigma: float or array of type 'd' """ self.sigma=sigma self.mean=mean if type(mean)==FloatType and type(sigma)==FloatType: # 1D Gaussian self.c=-log(sigma*sqrt(2*pi)) self.c2=2*sigma*sigma self.one_dim=1 else: # Multidimensional Gaussian assert(det(sigma)>0) self.dim=mean.shape[0] assert(sigma.shape==(self.dim,self.dim)) sq_det=sqrt(det(self.sigma)) self.b=1.0/(power(2*pi, self.dim/2.0)*sq_det) self.logb=log(self.b) self.inv_sigma=inv(self.sigma) self.one_dim=0
def eval_g(self, x): Q = self.eval_Q(x) R = self.eval_R(x) g_Q = la.det(Q) g_R = la.det(R) g_dual = self.eval_dual_obj(x) return np.array([g_Q, g_R, g_dual])
def make_orthogonal_cd(wcs): """ Create a perfect (square, orthogonal, undistorted) CD matrix from the input WCS. """ # get determinant of the CD matrix: cd = wcs.celestial.pixel_scale_matrix if hasattr(wcs, 'idcv2ref') and wcs.idcv2ref is not None: # Convert the PA_V3 orientation to the orientation at the aperture # This is for the reference chip only - we use this for the # reference tangent plane definition # It has the same orientation as the reference chip pv = updatewcs.makewcs.troll(wcs.pav3,wcs.wcs.crval[1],wcs.idcv2ref,wcs.idcv3ref) # Add the chip rotation angle if wcs.idctheta: pv += wcs.idctheta cs = np.cos(np.deg2rad(pv)) sn = np.sin(np.deg2rad(pv)) pvmat = np.dot(np.array([[cs,sn],[-sn,cs]]),wcs.parity) rot = np.arctan2(pvmat[0,1],pvmat[1,1]) scale = wcs.idcscale/3600. det = linalg.det(wcs.parity) else: det = linalg.det(cd) # find pixel scale: if hasattr(wcs, 'idcscale'): scale = (wcs.idcscale) / 3600. # HST pixel scale provided else: scale = np.sqrt(np.abs(det)) # find as sqrt(pixel area) # find Y-axis orientation: if hasattr(wcs, 'orientat') and not ignoreHST: rot = np.deg2rad(wcs.orientat) # use HST ORIENTAT else: rot = np.arctan2(wcs.wcs.cd[0,1], wcs.wcs.cd[1,1]) # angle of the Y-axis par = -1 if det < 0.0 else 1 # create a perfectly square, orthogonal WCS sn = np.sin(rot) cs = np.cos(rot) orthogonal_cd = scale * np.array([[par*cs, sn], [-par*sn, cs]]) lin_wcsobj = pywcs.WCS() lin_wcsobj.wcs.cd = orthogonal_cd lin_wcsobj.wcs.set() lin_wcsobj.orientat = arctan2(lin_wcsobj.wcs.cd[0,1],lin_wcsobj.wcs.cd[1,1]) * 180./np.pi lin_wcsobj.pscale = sqrt(lin_wcsobj.wcs.cd[0,0]**2 + lin_wcsobj.wcs.cd[1,0]**2)*3600. lin_wcsobj.wcs.crval = np.array([0.,0.]) lin_wcsobj.wcs.crpix = np.array([0.,0.]) lin_wcsobj.wcs.ctype = ['RA---TAN', 'DEC--TAN'] lin_wcsobj.wcs.set() return lin_wcsobj
def _logdet(matrix, use_tk_adjustment=True): """returns the LogDet from a diversity matrix Arguments: - use_tk_adjustment: when True, unequal state frequencies are allowed """ invalid = None, None, None, None total = matrix.sum() diffs = total - sum(matrix[i,i] for i in range(matrix.shape[0])) if total == 0: return invalid p = diffs / total if diffs == 0: # seqs identical return total, p, 0.0, None # we replace missing diagonal states with a frequency of 0.5, # then normalise frequency = matrix.copy() unobserved = where(frequency.diagonal() == 0)[0] for index in unobserved: frequency[index, index] = 0.5 frequency /= frequency.sum() # the inverse matrix of frequency, every element is squared M_matrix = inv(frequency)**2 freqs_1 = frequency.sum(axis = 0) freqs_2 = frequency.sum(axis = 1) if use_tk_adjustment: mean_state_freqs = (freqs_1 + freqs_2) / 2 coeff = (norm(mean_state_freqs)**2 - 1) / (matrix.shape[0] - 1) else: coeff = -1 / matrix.shape[0] FM_1 = diag(freqs_1) FM_2 = diag(freqs_2) try: d_xy = coeff * log(det(frequency) / sqrt(det(FM_1 * FM_2))) except FloatingPointError: return invalid if det(frequency) <= 0: #if the result is nan return invalid var_term = dot(M_matrix, frequency).transpose()[0].sum() var_denom = 16 * total if use_tk_adjustment: var = (var_term - (1 / sqrt(freqs_1 * freqs_2)).sum()) / var_denom else: # variance formula for TK adjustment is false var = (var_term - 1) / var_denom var = d_xy - 2 * var return total, p, d_xy, var
def crystal(self): """ Output structure, CRYSTAL format. Creates a structure from the output, including any atomic and cell-shape movements. """ from numpy import dot, identity, abs, array, any, all from numpy.linalg import inv, det from ..crystal import into_voronoi from .geometry import DisplaceAtoms, Elastic # Check whether this is a geometry optimization run. if not self._is_optgeom: return self.input_crystal # Structure is not simplified, since that could change the number of # symmetry operations, unless done carefully. Should be done somewhere # else. incrys, instruct = self.input_crystal, self.input_structure # create strain -- CRYSTAL allows pure rotations to creep into the lattice # during optimization. inv_in = inv(instruct.cell) epsilon = dot(self.structure.cell, inv_in) - identity(3, dtype='float64') cell = dot(identity(3) + epsilon, instruct.cell) inv_out = inv(cell) # create field displacement isbreaksym = self.params.optgeom.breaksym field = [ dot(cell, dot(inv_out, a.pos) - dot(inv_in, b.pos)) for a, b in zip(self.structure, instruct) if a.asymmetric or isbreaksym] field = [into_voronoi(u, cell, inv_out) for u in field] field = array(field) # check if changes: if all(abs(epsilon) < 1e-8) and all(abs(field.flatten()) < 1e-8): return self.input_crystal result = incrys.copy() # check whether we need to add breaksym or keepsym if incrys.is_breaksym != isbreaksym: result.append('breaksym' if isbreaksym else 'keepsymm') # check whether we need to add 'angstrom' if not result.is_angstrom: result.append('angstrom') # add cell shape changes if any(abs(epsilon) > 1e-8): a = Elastic() a.matrix = epsilon a.is_epsilon = True a.const_volume = abs(det(cell) - det(instruct.cell)) < 1e-8 result.append(a) # Add displacements if any(abs(field.flatten()) > 1e-8): a = DisplaceAtoms() atoms = [u for u in instruct if u.asymmetric or isbreaksym] for atom, disp in zip(atoms, field): if any(abs(disp) > 1e-8): a.add_atom(type=atom.label, pos=disp) result.append(a) return result
def modMatInv(A, p): # Finds the inverse of matrix A mod p n = len(A) A = matrix(A) adj = numpy.zeros(shape=(n, n)) for i in range(0, n): for j in range(0, n): adj[i][j] = ((-1) ** (i + j) * int(round(linalg.det(minor(A, j, i))))) % p return (modInv(int(round(linalg.det(A))), p) * adj) % p
def true_kldiv(m0, m1, s0, s1): k = m0.shape[0] first = np.trace(np.dot(inv(s1), s0)) mdiff = m1 - m0 second = np.dot(mdiff.T, np.dot(inv(s1), mdiff)) third = np.log(det(s0) / det(s1)) return .5 * (first + second - third - k)
def bentler(X): X2 = X ** 2 M = cross_product(X2, X2) D = np.diag(np.diag(M)) return -L * (L2.dot(la.inv(M) - la.inv(D))), \ -(np.log(la.det(M)) - np.log(la.det(D))) / 4, \ 'Bentler\'s criterion'
def eval_g(self, x): Q = eval_Q(x, self.n, self.y, self.eta) R = eval_R(x, self.n) g_Q = np.array([la.det(Q[:I, :I]) for I in xrange(1, self.n+2)]) g_R = np.array([la.det(R[:I, :I]) for I in xrange(1, self.n+2)]) g_dual = np.array([eval_dual_obj(x, self.mu, self.P, self.s)]) g = np.concatenate([g_Q, g_R, g_dual]) return g
def ln_B(W,nu): ln_num = -0.5*nu*np.log(det(W)) ln_det_1 = 0.5*nu*D*np.log(2) - D*(D-1)*0.25*np.log(np.pi) ln_det_2 = np.sum(gammaln(np.array([0.5*(nu+1-i) for i in range(D)]))) return ln_num - ln_det_1 - ln_det_2
def reset(self, x0): """ Puts the optimizer in its initial state and sets the initial point to be the 1-dimensional array or list *x0*. The length of the array becomes the dimension of the optimization problem (:attr:`ndim` member). The initial simplex is built around *x0* by calling the :meth:`buildSimplex` method with default values for the *rel* and *abs* arguments. If *x0* is a 2-dimensional array or list of size (*ndim*+1) times *ndim* it specifies the initial simplex. The initial value of the natural logarithm of the simplex side vectors determinant is calculated and stored. This value gets updated at every simplex algorithm step. The only time it needs to be reevaluated is at reshape. But that is also quite simple because the reshaped simplex is orthogonal. The only place where a full determinant needs to be calculated is here. """ # Debug message if self.debug: DbgMsgOut("SDNMOPT", "Resetting.") # Make it an array x0 = array(x0) # Is x0 a point or a simplex? if x0.ndim == 1: # Point # Set x now NelderMead.reset(self, x0) if self.debug: DbgMsgOut("SDNMOPT", "Generating initial simplex from initial point.") sim = self.buildSimplex(x0) self._setSimplex(sim) else: # Simplex or error (handled in _setSimplex()) self._setSimplex(x0) if self.debug: DbgMsgOut("SDNMOPT", "Using specified initial simplex.") # Set x to first point in simplex after it was checked in _setSimplex() Optimizer.reset(self, x0[0, :]) # Reset point moves counter self.simplexmoves = zeros(self.ndim + 1) # Calculate log(n! det([v])) where [v] are the n side vectors # arranged as columns of a matrix (v, l) = self.sortedSideVectors() self.logDet = log(abs(det(v))) # Initial h self.h = 1.0 # Make x tolerance an array self.xtol = array(self.xtol)
def three_tri(node1, node2, node3, node4): [x1, y1, z1, d1] = node1 [x2, y2, z2, d2] = node2 [x3, y3, z3, d3] = node3 [x4, y4, z4, d4] = node4 #3-D trilateration. 4 anchor nodes # x1 = x coordinate of anchor 1 # y1 = y coordinate of anchor 1 # z1 = z coordinate of anchor 1 # d1 = distance from point to anchor 1 d11 = 2 * (x2 - x1) d21 = 2 * (x3 - x1) d31 = 2 * (x4 - x1) d12 = 2 * (y2 - y1) d22 = 2 * (y3 - y1) d32 = 2 * (y4 - y1) d13 = 2 * (z2 - z1) d23 = 2 * (z3 - z1) d33 = 2 * (z4 - z1) #bringing M together into [3, 3] matrix d = np.array([[d11, d12, d13], [d21, d22, d23], [d31, d32, d33]]) #x_numerator elements x_n11 = (d1**2 - d2**2) - (x1**2 - x2**2) - (y1**2 - y2**2) - ( z1**2 - z2**2) #sigma x_n21 = (d1**2 - d3**2) - (x1**2 - x3**2) - (y1**2 - y3**2) - ( z1**2 - z3**2) #beta x_n31 = (d1**2 - d4**2) - (x1**2 - x4**2) - (y1**2 - y4**2) - ( z1**2 - z4**2) #phi x_n12 = 2 * (y2 - y1) x_n22 = 2 * (y3 - y1) x_n32 = 2 * (y4 - y1) x_n13 = 2 * (z2 - z1) x_n23 = 2 * (z3 - z1) x_n33 = 2 * (z4 - z1) #bringing numerator together for x x_n = np.array([[x_n11, x_n12, x_n13], [x_n21, x_n22, x_n23], [x_n31, x_n32, x_n33]]) #finding x by dividing matrix operation and then determinant x = r_divide(x_n, d) x = det(x) #individual y elements y_n11 = 2 * (x2 - x1) y_n21 = 2 * (x3 - x1) y_n31 = 2 * (x4 - x1) y_n12 = x_n11 #sigma y_n22 = x_n21 #beta y_n32 = x_n31 #phi y_n13 = 2 * (z2 - z1) y_n23 = 2 * (z3 - z1) y_n33 = 2 * (z4 - z1) #bringing numerator together for y y_n = np.array([[y_n11, y_n12, y_n13], [y_n21, y_n22, y_n23], [y_n31, y_n32, y_n33]]) #finding y by dividing matrix operation and then determinant y = r_divide(y_n, d) y = det(y) #individual z elements z_n11 = 2 * (x2 - x1) z_n21 = 2 * (x3 - x1) z_n31 = 2 * (x4 - x1) z_n12 = 2 * (y2 - y1) z_n22 = 2 * (y3 - y1) z_n32 = 2 * (y4 - y1) z_n13 = x_n11 #sigma z_n23 = x_n21 #beta z_n33 = x_n31 #phi #bringing z numerator together z_n = np.array([[z_n11, z_n12, z_n13], [z_n21, z_n22, z_n23], [z_n31, z_n32, z_n33]]) #finding z by dividing matrix operation and then determinant z = r_divide(z_n, d) z = det(z) return x, y, z
def multivar_regress(self, Y, predef_var='', list_variates=[]): """ The method is the multi-variate-regression that simultaniously fit multiple variates (x1,x2,....) in one single regression to derive the best fitting line to represent the original data (Y) Parameters: Y: np.array the original data value (Vector of responses) list_variates: list, list of variates used for the regression [var1, var2, .....] to form the design matrix Design matrix = X and Vector of response Y / 1 x11 x21 ... xn1 \ / y1 \ | 1 x12 x22 ... xn2 | | y2 | | . . . . | | . | X = | . . . . | Y = | . | | . . . . | | . | \ 1 x1p x2p ... xnp / \ yp / predef_var: string, kwarg to call for predefined variates options - semisea_sea_lin, semisea_sea_lin_quad, fort13_fort14_lin} Returns: beta: the coefficients (dim = num of variates) that relate to each variate It's order is following the order of variates in the list_variates err: the errors that makes the linear combined regression value to deviate from the original data (Y) / beta0 \ / e1 \ | beta1 | | e2 | | . | | . | beta = | . | err = | . | | . | | . | \ betan / \ ep / Mathematical expression: R^2= sum (Y-beta*X) find the minimum of the equation => first order differential = 0 first order diffferential equation is X'Y=(X'X) (beta) so beta = (X'X)^-1 (X'Y) where prime means transpose of matrix, ^-1 means inversion of matrix to solve the weighted system R^2= sum W(Y-beta*X) find the minimum of the equation => first order differential = 0 first order diffferential equation is X'WY=(X'WX) (beta) so beta = (X'WX)^-1 (X'WY) where /1/E^2 0 0 ... 0 \ | 0 1/E^2 0 ... 0 | | 0 0 1/E^2 ... 0 | W = | . . . | | . . . | \ 0 0 0 ...1/E^2 / the matrix representing the reciprical of the error square at each observed value (can be different at each time step) The weighted regression propogate the measurement error in the observation to the regression [Schuckmann et al., 2011, Llovel et al., 2014] """ #-- reshape the Y array to column Y = np.reshape(Y, [Y.shape[0], 1]) # determine the design matrix X = self.def_vars(predef_var=predef_var, list_variates=list_variates) #-- determine to perform weighted regression or not if self.err_weight.upper() in ['Y']: ww = np.zeros( len(time)) + 1. / self.err**2 # the variance of obs. error W = np.diag(ww) term1 = np.dot(np.transpose(X), np.dot(W, X)) #(X'WX) else: term1 = np.dot(np.transpose(X), X) #(X'X) #-- calculate the determinant for matrix inverse detterm1 = la.det(term1) if detterm1 == np.float64(0.): print "Error64: No inverse of (X'X) or (X'WX) " return #-- calculate the matrix inverse term1_1 = la.inv(term1) #(X'X)^-1 or (X'WX)^-1 #-- calculate the right hand side if self.err_weight.upper() in ['Y']: term2 = np.dot(np.transpose(X), np.dot(W, Y)) #(X'WY) else: term2 = np.dot(np.transpose(X), Y) #(X'Y) #-- matrix multiplication of RHS and inverse matrix beta = np.dot(term1_1, term2) #(X'X)^-1(X'Y) or (X'WX)^-1(X'WY) #-- store attribute in the regression self.nvars = X.shape[1] # store number of variates self.dof = len( self.axis) - self.nvars # store degree of freedom in the regress #-- calculate uncertainties # the diag of rmse*sqrt(X_var)^-1 represents the std of uncertainty # in the regression for each variate Yreg = np.dot(X, np.reshape( beta, [beta.shape[0], 1])) # (X) dot (beta) => regression model Yreg = np.reshape(Yreg, [Yreg.shape[0], 1]) misfit = Y - Yreg rmse = np.sqrt(np.sum(misfit**2) / (self.dof)) # RMSE between the model and obs. X_var = np.dot(np.transpose(X), X) se = rmse * np.sqrt(np.diag(la.inv(X_var))) #-- output the design matrix in the form of list_variates if list_variates == []: list_variates = [X[:, i] for i in range(X.shape[1])] return { 'beta': beta[:, 0], 'se': se, 'rmse': rmse, 'model': Yreg[:, 0], 'list_variates': list_variates }
def valid_bin_params(self, vectors, extents, nbins): return (extents[-1, :] - extents[0, :] > 0).all() and sum( nbins > 1) == 1 and not isclose(det(vectors), 0.0)
def numeric(self, values): return np.log(LA.det(values[0]))
def Gaussian_Distribution(x, D, mean, sigma): E = np.exp((-1/2)*dot((x-mean).T,(inv(sigma)),(x-mean))) y = (1/((2*np.pi)**(D/2)))*(1/((det(sigma))**(1/2)))*E return y
def fer(self, combo_name='Combo 1'): ''' Returns the quadrilateral's local fixed end reaction vector (zero's for now until surface loads get added). Parameters ---------- combo_name : string The name of the load combination to get the consistent load vector for. ''' Hw = lambda r, s: 1 / 4 * array([[(1 + r) * (1 + s), 0, 0, (1 - r) * (1 + s), 0, 0, (1 - r) * (1 - s), 0, 0, (1 + r) * (1 - s), 0, 0]]) # Initialize the fixed end reaction vector fer = zeros((12, 1)) # Get the requested load combination combo = self.LoadCombos[combo_name] # Define the gauss point used for numerical integration gp = 1 / 3**0.5 # Loop through each load case and factor in the load combination for case, factor in combo.factors.items(): p = 0 # Sum the pressures for pressure in self.pressures: # Check if the current pressure corresponds to the current load case if pressure[1] == case: # Sum the pressures p -= factor * pressure[0] fer = (Hw(-gp, -gp).T * p * det(self.J(-gp, -gp)) + Hw(-gp, gp).T * p * det(self.J(-gp, gp)) + Hw(gp, gp).T * p * det(self.J(gp, gp)) + Hw(gp, -gp).T * p * det(self.J(gp, -gp))) # At this point `fer` only contains terms for the degrees of freedom # associated with membrane action. Expand `fer` to include zero terms for # the degrees of freedom related to bending action. This will allow # the bending and membrane vectors to be summed directly # later on. `numpy` has an `insert` function that can be used to # insert rows and columns of zeros one at a time, but it is very slow # as it makes a temporary copy of the vector term by term each time # it's called. The algorithm used here accomplishes the same thing # much faster. Terms are copied only once. # Initialize the expanded vector to all zeros fer_exp = zeros((24, 1)) # Step through each term in the unexpanded vector # i = Unexpanded vector row for i in range(12): # Find the corresponding term in the expanded vector # m = Expanded vector row if i in [0, 3, 6, 9]: # indices associated with deflection in z m = 2 * i + 2 if i in [1, 4, 7, 10]: # indices associated with rotation about x m = 2 * i + 1 if i in [2, 5, 8, 11]: # indices associated with rotation about y m = 2 * i # Ensure the index is an integer rather than a float m = round(m) # Add the term from the unexpanded vector into the expanded vector fer_exp[m, 0] = fer[i, 0] return fer_exp
def band_structure(self, path_kc, modes=False, born=False, verbose=True): """Calculate phonon dispersion along a path in the Brillouin zone. The dynamical matrix at arbitrary q-vectors is obtained by Fourier transforming the real-space force constants. In case of negative eigenvalues (squared frequency), the corresponding negative frequency is returned. Eigenvalues and modes are in units of eV and Ang/sqrt(amu), respectively. Parameters ---------- path_kc: ndarray List of k-point coordinates (in units of the reciprocal lattice vectors) specifying the path in the Brillouin zone for which the dynamical matrix will be calculated. modes: bool Returns both frequencies and modes when True. born: bool Include non-analytic part given by the Born effective charges and the static part of the high-frequency dielectric tensor. This contribution to the force constant accounts for the splitting between the LO and TO branches for q -> 0. verbose: bool Print warnings when imaginary frequncies are detected. """ assert self.D_N is not None if born: assert self.Z_avv is not None assert self.eps_vv is not None # Lattice vectors -- ordered as illustrated in class docstring R_cN = self.lattice_vectors() # Dynamical matrix in real-space D_N = self.D_N # Lists for frequencies and modes along path omega_kl = [] u_kl = [] # Reciprocal basis vectors for use in non-analytic contribution reci_vc = 2 * pi * la.inv(self.atoms.cell) # Unit cell volume in Bohr^3 vol = abs(la.det(self.atoms.cell)) / units.Bohr**3 for q_c in path_kc: # Add non-analytic part if born: # q-vector in cartesian coordinates q_v = np.dot(reci_vc, q_c) # Non-analytic contribution to force constants in atomic units qdotZ_av = np.dot(q_v, self.Z_avv).ravel() C_na = 4 * pi * np.outer(qdotZ_av, qdotZ_av) / \ np.dot(q_v, np.dot(self.eps_vv, q_v)) / vol self.C_na = C_na / units.Bohr**2 * units.Hartree # Add mass prefactor and convert to eV / (Ang^2 * amu) M_inv = np.outer(self.m_inv_x, self.m_inv_x) D_na = C_na * M_inv / units.Bohr**2 * units.Hartree self.D_na = D_na D_N = self.D_N + D_na / np.prod(self.N_c) ## if np.prod(self.N_c) == 1: ## ## q_av = np.tile(q_v, len(self.indices)) ## q_xx = np.vstack([q_av]*len(self.indices)*3) ## D_m += q_xx # Evaluate fourier sum phase_N = np.exp(-2.j * pi * np.dot(q_c, R_cN)) D_q = np.sum(phase_N[:, np.newaxis, np.newaxis] * D_N, axis=0) if modes: omega2_l, u_xl = la.eigh(D_q, UPLO='U') # Sort eigenmodes according to eigenvalues (see below) and # multiply with mass prefactor u_lx = (self.m_inv_x[:, np.newaxis] * u_xl[:, omega2_l.argsort()]).T.copy() u_kl.append(u_lx.reshape((-1, len(self.indices), 3))) else: omega2_l = la.eigvalsh(D_q, UPLO='U') # Sort eigenvalues in increasing order omega2_l.sort() # Use dtype=complex to handle negative eigenvalues omega_l = np.sqrt(omega2_l.astype(complex)) # Take care of imaginary frequencies if not np.all(omega2_l >= 0.): indices = np.where(omega2_l < 0)[0] if verbose: print ("WARNING, %i imaginary frequencies at " "q = (% 5.2f, % 5.2f, % 5.2f) ; (omega_q =% 5.3e*i)" % (len(indices), q_c[0], q_c[1], q_c[2], omega_l[indices][0].imag)) omega_l[indices] = -1 * np.sqrt(np.abs(omega2_l[indices].real)) omega_kl.append(omega_l.real) # Conversion factor: sqrt(eV / Ang^2 / amu) -> eV s = units._hbar * 1e10 / sqrt(units._e * units._amu) omega_kl = s * np.asarray(omega_kl) if modes: return omega_kl, np.asarray(u_kl) return omega_kl
outf.tprint('%d %g %d' % (i + 1, Sk[k - 1, 0], k)) marg1d[i] = Sk[k - 1, 0] S1 = marg1d.sum() outf.tprint("# First-order entropy (sum of 1D marginal entropies):") outf.tprint("%f" % (S1)) if a.order in ['1.5', '2']: # first-order plus correlations corrmat = corrcoef(X, rowvar=0) estmi = 0.0 # mutual info estimated from corr coef for i in range(d - 1): for j in range(i + 1, d): estmi = estmi - 0.5 * log(1.0 - corrmat[i, j]**2) outf.tprint('# Estimated mutual info from linear correlations:') outf.tprint('%f' % (estmi * ufac)) # also calculate "quasi-harmonic mutual info" CX = cov(Xfull, rowvar=0) Iqh = 0.5 * (log(diag(CX)).sum() - log(la.det(CX))) outf.tprint('# Quasiharmonic mutual info:') outf.tprint('%f' % (Iqh * ufac)) outf.tprint( '# First-order entropy corrected by estimated / quasiharm. mut.inf' ) outf.tprint('%f %f' % (S1 - estmi * ufac, S1 - Iqh * ufac)) if a.order == '2': I2 = 0.0 outf.tprint("# 2-dimensional joint (marginal) entropies") outf.tprint( "# variable 1, variable 2, joint entropy, number of components, " + "mutual information") for i in range(d - 1): for j in range(i + 1, d): (W, M, R, Tlogl, Sk) = em(X[:, [i, j]],
def em(X, Y, kmax, nr_of_cand, stop, Sdelta, emthresh, ufac, overfit, printout): #function [W,M,R,Tlogl,Sk] = myem(X,T,kmax,nr_of_cand,plo,dia,Sdelta,emthresh) # em - EM algorithm for adaptive multivariate Gaussian mixtures # #[W,M,R,Tlogl] = em(X,T,kmax,nr_of_cand,plo,dia) # X - (n x d) d-dimensional zero-mean unit-variance data # Y - (m x d) test data for cross-validation (optional, set [] if none) # kmax - maximum number of components allowed # nr_of_cand - number of candidates per component, zero gives non-greedy EM # plo - if 1 then plot ellipses for 2-d data # dia - if 1 then print diagnostics # Sdelta - stop if change in entropy is < Sdelta J/K/mol (added by A. Szilagyi) # emthresh - threshold for EM optimization # overfit - nr of components to calculate after stopping crit is met #returns # W - (k x 1) vector of mixing weights # M - (k x d) matrix of components means # R - (k x d^2) matrix of Cholesky submatrices of components covariances # in vector reshaped format. To get the covariance of component k: # Rk = reshape(R(k,:),d,d); S = Rk'*Rk; # Tlogl - average log-likelihood of test data # Sk - entropy history (added by A. Szilagyi) # # Nikos Vlassis & Sjaak Verbeek, oct 2002 # see greedy-EM paper at http://www.science.uva.nl/~vlassis/publications n, d = X.shape converged = False #n1 = ones((n,1)) #d1 = ones((1,d)) Sk = zeros((kmax, 1)) THRESHOLD = emthresh if nr_of_cand > 0: k = 1 logging.debug('Greedy EM initialization') else: k = kmax logging.debug('Non-greedy EM initialization') (W, M, R, P, sigma) = em_init_km(X, k, 0) oldW, oldM, oldR = W, M, R # do autoscaling of the data to prevent numerical overflows/underflows llcorrection = 0.0 if k == 1: Rj = reshape(R, (d, d), order='F').copy() else: Rj = reshape(R[0, :], (d, d), order='F').copy() detrj = la.det(Rj) # determinant of covariance matrix if detrj > 1e20: scalingf = (1.0e20 / detrj)**(1.0 / d) X = scalingf * X Y = scalingf * Y llcorrection = d * log(scalingf) logging.debug('# autoscaling data by a scaling factor: %g' % scalingf) (W, M, R, P, sigma) = em_init_km(X, k, 0) oldW, oldM, oldR = W, M, R elif detrj < 1.0: scalingf = (1.0 / detrj)**(1.0 / d) X = scalingf * X Y = scalingf * Y llcorrection = d * log(scalingf) logging.debug('# autoscaling data by a scaling factor: %g' % scalingf) (W, M, R, P, sigma) = em_init_km(X, k, 0) oldW, oldM, oldR = W, M, R # sigma = sigma**2 oldlogl = -REALMAX oldYlogl = -REALMAX oldentropy = REALMAX oldAIC = REALMAX while 1: # apply EM steps to the complete mixture until convergence logging.debug('EM steps on complete mixture...') while 1: (W, M, R) = em_step(X, W, M, R, P) # likelihoods L (n x k) for all inputs and all components L = em_gauss(X, M, R) # mixture F (n x 1) and average log-likelihood F = L.dot(W) toosmall = len((F < REALMIN).nonzero()[0]) F[F < REALMIN] = REALMIN logl = mean(log(F)) # posteriors P (n x k) and their sums P = L * (ones((n, 1)).dot(W.T)) / F.dot(ones((1, k))) if abs(logl / oldlogl - 1) < THRESHOLD: logging.debug('Logl = %g' % (logl)) break oldlogl = logl # for cross-validation, calculate loglikelihood for Y set if stop == 'cv': Fy = dot(em_gauss(Y, M, R), W) logging.debug('Fy nonzero: %d' % count_nonzero(F < REALMIN)) Fy[Fy < REALMIN] = REALMIN Ylogl = mean(log(Fy)) logging.debug('oldYlogl=%f Ylogl=%f' % (oldYlogl, Ylogl)) if Ylogl < oldYlogl: if not converged: converged = True kconv = k outf.tprint('# Calculation converged') if converged and k >= kconv + overfit: logging.debug('Ylogl decreased, stopping') outf.tprint('# Stopping due to Ylogl decrease') return (oldW, oldM, oldR, oldYlogl, Sk) oldYlogl = Ylogl oldW = W oldM = M oldR = R # newentropy = -logl - llcorrection if stop == 'cv': newentropy2 = -Ylogl - llcorrection logging.debug("with %d components S=%f J/K/mol (%f cal/K/mol)" % (k, 8.314472 * newentropy, 1.9858775 * newentropy)) # calculate Akaike Information Criterion if stop == 'aicsd': npar = k - 1 + d * k + k * d * ( d + 1) / 2 # number of parameters of the mixture AIC = 2 * npar - 2 * n * (logl + llcorrection) logging.debug("AIC = %f" % (AIC)) if stop == 'cv': newentropy2 = -Ylogl - llcorrection #Sk[k-1] = ufac*(newentropy+newentropy2)/2 Sk[k - 1] = ufac * newentropy else: Sk[k - 1] = ufac * newentropy if toosmall: logging.warning(( "%d out of %d likelihoods are too small. The following result may " + "not be valid. Use fewer dimensions.") % (toosmall, len(F))) if stop == 'aicsd': # use AIC+sdelta as stopping criterion if AIC > oldAIC: if not converged: converged = True kconv = k outf.tprint('# Calculation converged by AIC') if converged and k >= kconv + overfit: logging.debug('Stopping as AIC has increased') logging.debug('%d %g' % (k, Sk[k - 1])) outf.tprint('# Stopping due to AIC increase') return (oldW, oldM, oldR, logl, Sk) # return previous values oldW, oldM, oldR = W, M, R if printout: if stop == 'cv': outf.tprint("%d %g %g" % (k, Sk[k - 1], ufac * newentropy2)) else: outf.tprint("%d %g" % (k, Sk[k - 1])) if stop == 'aicsd': # use sdelta as stopping criterion if abs(newentropy - oldentropy) < Sdelta / ufac: if not converged: converged = True kconv = k outf.tprint('# Calculation converged by Sdelta') if converged and k >= kconv + overfit: logging.debug('Stopping %f %f' % (ufac * newentropy, ufac * oldentropy)) outf.tprint('# Stopping due to reaching Sdelta') return (W, M, R, logl, Sk) oldentropy = newentropy if stop == 'aicsd': oldAIC = AIC # stop if kmax is reached if k == kmax: return (W, M, R, logl, Sk) # try to add another component logging.debug('Trying component allocation') ntries = 100 # repeat candidate search this many times maximum for itry in range(ntries): (Mnew, Rnew, alpha, returnstatus) = rand_split(P, X, M, R, sigma, F, W, nr_of_cand) if alpha != 0: break else: if returnstatus == 'split_impossible': break elif itry < ntries - 1: logging.warning( 'No appropriate candidate found. Trying more candidates...' ) if alpha == 0: logging.warning( 'Failed to find candidates. Result has not converged.') if returnstatus == 'split_impossible': logging.warning('A larger sample is needed.') else: logging.warning('Try to use a larger ncand.') return (W, M, R, logl, Sk) K = em_gauss(X, Mnew, Rnew) PP = F * (1 - alpha) + K * alpha LOGL = mean(log(PP)) # optimize new mixture with partial EM steps updating only Mnew,Rnew veryoldlogl = logl oldlogl = LOGL done_here = False Pnew = (K * (ones((n, 1)) * alpha)) / PP while not done_here: (alpha, Mnew, Rnew) = em_step(X, alpha, Mnew, Rnew, Pnew) K = em_gauss(X, Mnew, Rnew) Fnew = F * (1 - alpha) + K * alpha Pnew = K * alpha / Fnew logl = mean(log(Fnew)) if abs(logl / oldlogl - 1) < THRESHOLD: done_here = True oldlogl = logl # check if log-likelihood increases with insertion if logl <= veryoldlogl: logging.debug(( 'Mixture uses only %d components, logl=%g oldlogl=%g veryoldlogl=%g ' + 'llcorrection=%f') % (k, logl, oldlogl, veryoldlogl, llcorrection)) return (W, M, R, logl, Sk) # allocate new component M = vstack((M, Mnew)) R = vstack((R, Rnew)) W = vstack(((1 - alpha) * W, alpha)) k = k + 1 logging.debug(' k = %d' % (k)) logging.debug('LogL = %g (%g after correction)' % (logl, logl + llcorrection)) # prepare next EM step L = em_gauss(X, M, R) F = dot(L, W) F[F < REALMIN] = REALMIN P = L * dot(ones((n, 1)), W.T) / dot(F, ones((1, k)))
def orthdef(latt): #columns as vectors od = norm(latt[:,0])*norm(latt[:,1])*norm(latt[:,2])/det(latt) return od
print() # 2) умножение матрицы на вектор matrix = numpy.arange(2 * 3).reshape((3, 2)) vector = numpy.array([1, -1], dtype=float) print('Матрица:\n', matrix) print('Вектор:\n', second_matrix) print('Умножение матрицы на вектор: ', matrix @ vector) print() # 3) решение произвольной системы линейных уравнений # x - y = 7 # 3x + 2y = 16 matrix = numpy.array([[1., -1.], [3., 2.]]) vector = numpy.array([7., 16.]) print('Система:\nx - y = 7\n3x + 2y = 16') answer = numpy.linalg.solve(matrix, vector) print('x: ', answer[0], 'y: ', answer[1]) print() # 4) расчет определителя матрицы matrix = numpy.arange(5 * 5).reshape((5, 5)) print('Матрица: ', matrix, '\n', 'Определитель: ', det(matrix)) print() # 5) получение обратной и транспонированной матриц a = numpy.array([[0, 4, 2], [4, 6, 6], [7, 9, 10]]) print('Исходная матрица:\n', a) print('Обратаня матрица:\n', inv(a)) print('Транспонированная матрица:\n', a.transpose())
def step(self, action_dict): obs_dict = {} reward_dict = {} done_dict = {'__all__': False} info_dict = {} # Targets move (t -> t+1) for n in range(self.nb_targets): self.targets[n].update() self.belief_targets[n].predict() # Belief state at t+1 # Agents move (t -> t+1) and observe the targets for ii, agent_id in enumerate(action_dict): obs_dict[self.agents[ii].agent_id] = [] reward_dict[self.agents[ii].agent_id] = [] done_dict[self.agents[ii].agent_id] = [] action_vw = self.action_map[action_dict[agent_id]] # Locations of all targets and agents in order to maintain a margin between them margin_pos = [t.state[:2] for t in self.targets[:self.nb_targets]] for p, ids in enumerate(action_dict): if agent_id != ids: margin_pos.append(np.array(self.agents[p].state[:2])) _ = self.agents[ii].update(action_vw, margin_pos) # _ = self.agents[ii].update(action_vw, [t.state[:2] for t in self.targets[:self.nb_targets]]) observed = np.zeros(self.nb_targets, dtype=bool) obstacles_pt = (self.sensor_r, np.pi) # Update beliefs of all targets for jj in range(self.nb_targets): # Observe obs, z_t = self.observation(self.targets[jj], self.agents[ii]) observed[jj] = obs if obs: # if observed, update the target belief. self.belief_targets[jj].update(z_t, self.agents[ii].state) # obstacles_pt = map_utils.get_closest_obstacle(self.MAP, self.agents[ii].state) # if obstacles_pt is None: # Calculate beliefs on only assigned targets # for kk in range(self.nb_targets): r_b, alpha_b = util.relative_distance_polar( self.belief_targets[jj].state[:2], xy_base=self.agents[ii].state[:2], theta_base=self.agents[ii].state[-1]) r_dot_b, alpha_dot_b = util.relative_velocity_polar( self.belief_targets[jj].state[:2], self.belief_targets[jj].state[2:], self.agents[ii].state[:2], self.agents[ii].state[-1], action_vw[0], action_vw[1]) obs_dict[agent_id].append([ r_b, alpha_b, r_dot_b, alpha_dot_b, np.log(LA.det(self.belief_targets[jj].cov)), float(obs), obstacles_pt[0], obstacles_pt[1] ]) obs_dict[agent_id] = np.asarray(obs_dict[agent_id]) # Get all rewards after all agents and targets move (t -> t+1) reward, done, mean_nlogdetcov = self.get_reward( obstacles_pt, observed, self.is_training) reward_dict['__all__'], done_dict['__all__'], info_dict[ 'mean_nlogdetcov'] = reward, done, mean_nlogdetcov return obs_dict, reward_dict, done_dict, info_dict
def rhs(self, t, y, sw): ''' The right-hand-side function (rhs) for the integrator ''' ###################################################################################################### P = y[0] T = y[1] V = y[3] a = (V / (4. * np.pi / 3))**(1. / 3.) self.radius = a eruption = sw[4] # This tells whether eruption is yes or no if t > self.tcurrent: self.dt = t - self.tcurrent self.dt_counter += self.dt self.tcurrent = t self.P_list.update(P - self.plith) self.T_list.update(T - self.param['T_S']) self.times_list.update(t) self.T_out, self.P_out, self.sigma_rr, self.sigma_theta, self.T_der = self.crust_analy_params.Analytical_sol_cavity_T_Use( self.T_list.data[:self.max_count], self.P_list.data[:self.max_count], self.radius, self.times_list.data[:self.max_count], self.T_flux_list.data[:self.max_count], self.P_flux_list.data[:self.max_count]) self.max_count += 1 pdb.set_trace() self.T_out -= self.initial_T_out * np.exp( -t / self.t0 / self.param['relax_press_init']) self.P_out -= self.initial_P_out * np.exp( -t / self.t0 / self.param['relax_press_init']) self.sigma_rr -= self.initial_sigma_rr * np.exp( -t / self.t0 / self.param['relax_press_init']) self.sigma_theta -= self.initial_sigma_theta * np.exp( -t / self.t0 / self.param['relax_press_init']) self.P_out_all = np.vstack([self.P_out_all, self.P_out]) self.T_out_all = np.vstack([self.T_out_all, self.T_out]) self.sigma_rr_all = np.vstack([self.sigma_rr_all, self.sigma_rr]) self.sigma_theta_all = np.vstack( [self.sigma_theta_all, self.sigma_theta]) #self.sigma_eff_rr_all = np.vstack([self.sigma_eff_rr_all,self.sigma_rr + self.P_out]) #self.sigma_eff_theta_all = np.vstack([self.sigma_eff_theta_all,self.sigma_theta+self.P_out]) else: self.dt = 0. eps_g = y[2] dV_dP = V / self.param['beta_r'] dV_dT = -V * self.param['alpha_r'] rho_m = y[4] drho_m_dP = rho_m / self.param['beta_m'] drho_m_dT = -rho_m * self.param['alpha_m'] rho_x = y[5] drho_x_dP = rho_x / self.param['beta_x'] drho_x_dT = -rho_x * self.param['alpha_x'] eps_x, deps_x_dT, deps_x_deps_g = self.input_functions.melting_curve( T, P, eps_g) rho_g, drho_g_dP, drho_g_dT = self.input_functions.gas_density(T, P) rho = (1. - eps_g - eps_x) * rho_m + eps_g * rho_g + eps_x * rho_x drho_dP = (1. - eps_g - eps_x) * drho_m_dP + eps_g * drho_g_dP + eps_x * drho_x_dP drho_dT = (1. - eps_g - eps_x) * drho_m_dT + eps_g * drho_g_dT + eps_x * drho_x_dT drho_deps_g = -rho_m + rho_g drho_deps_x = -rho_m + rho_x # % exsolution m_eq, dm_eq_dP, dm_eq_dT = self.input_functions.solubulity_curve(T, P) c = ((1. - eps_g - eps_x) * rho_m * self.param['c_m'] + eps_g * rho_g * self.param['c_g'] + eps_x * rho_x * self.param['c_x']) / rho dc_dP = (1. / rho) * ( (1 - eps_g - eps_x) * self.param['c_m'] * drho_m_dP + eps_g * self.param['c_g'] * drho_g_dP + eps_x * self.param['c_x'] * drho_x_dP) - (c / rho) * drho_dP dc_dT = (1. / rho) * ( (1 - eps_g - eps_x) * self.param['c_m'] * drho_m_dT + eps_g * self.param['c_g'] * drho_g_dT + eps_x * self.param['c_x'] * drho_x_dT) - (c / rho) * drho_dT dc_deps_g = (1. / rho) * (-rho_m * self.param['c_m'] + rho_g * self.param['c_g']) - (c / rho) * drho_deps_g dc_deps_x = (1. / rho) * (-rho_m * self.param['c_m'] + rho_x * self.param['c_x']) - (c / rho) * drho_deps_x #% boundary conditions eps_x_in, _, _ = self.input_functions.melting_curve( self.param['T_in'], self.plith, self.param['eps_g_in']) rho_g_in, _, _ = self.input_functions.gas_density( self.param['T_in'], P) rho_m_in = self.param['rho_m_inp'] # rho_m rho_x_in = self.param['rho_x_inp'] # rho_x rho_in = ( 1 - self.param['eps_g_in'] - eps_x_in ) * rho_m_in + self.param['eps_g_in'] * rho_g_in + eps_x_in * rho_x_in c_in = ((1 - self.param['eps_g_in'] - eps_x_in) * rho_m_in * self.param['c_m'] + self.param['eps_g_in'] * rho_g_in * self.param['c_g'] + eps_x_in * rho_x_in * self.param['c_x']) / rho_in #;%c; Mdot_in = self.param['Mdot_in'] Mdot_v_in = self.param['m_eq_in'] * rho_m_in * ( 1. - self.param['eps_g_in'] - eps_x_in) * Mdot_in / rho_in + rho_g_in * self.param[ 'eps_g_in'] * Mdot_in / rho_in Hdot_in = c_in * self.param['T_in'] * Mdot_in P_buoyancy = -(rho - self.param['crustal_density'] ) * const.g_earth * a # delta_rho*g*h # tmp_val = (P-self.plith + P_buoyancy) - self.param['delta_Pc']*0.95 # if tmp_val > 0 : # value_diking = True # else : # value_diking = False indx_use_P = np.where( self.R_outside <= (1. + self.param['frac_rad_press']) * a) indx_use_T = np.where( self.R_outside <= (1. + self.param['frac_rad_Temp']) * a) indx_use_visc = np.where( self.R_outside <= (1. + self.param['frac_rad_visc']) * a) mean_T_der_out = np.mean(self.T_der[indx_use_T]) mean_T_out = np.mean(self.T_out[indx_use_T]) + self.param['T_S'] mean_P_out = np.mean(self.P_out[indx_use_P]) + self.plith ############################################################### # self.T_fluid_mean = mean_T_out # self.crust_analy_params.set_viscosity(self.T_fluid_mean,mean_P_out) # self.crust_analy_params.set_constants(self.param['material'],self.permeability) ############################################################### mean_sigma_rr_out = -np.mean(self.sigma_rr[indx_use_visc]) + self.plith visc_gas = self.crust_analy_params.visc # ############################################################# # ########################################################### if self.param['heat_cond'] == 1.: if t < 30e7: # Initially the gradients are kind of large .. so may be unstable .. small_q = -self.param['k_crust'] * (mean_T_out - T) / ( self.param['frac_rad_Temp'] * a) else: small_q = -self.param['k_crust'] * mean_T_der_out small_q2 = -self.param['k_crust'] * (300. - self.param['T_S']) / ( self.param['depth']) # conductive heat loss to the surface surface_area_chamber = 4. * np.pi * a**2. Q_out = small_q * surface_area_chamber + self.param[ 'frac_cond_cool'] * small_q2 * surface_area_chamber elif self.param['heat_cond'] == 0.: Q_out = 0. else: raise NotImplementedError('heat_cond not specified') if np.isnan(Q_out): pdb.set_trace() raise ValueError('Q_out is NaN') # ############################################################# # ############################################################# if eruption == False: if self.param['vol_degass'] == 1.: surface_area_chamber_degassing = 4. * np.pi * a**2. * self.param[ 'degass_frac_chm'] delta_P_grad = (P - mean_P_out) / a / self.param['frac_length'] ################## Flux out of the chamber due to pressure gradient in the crust .. # Note that there is no buoyancy term since the fluid is in equilbrium (Pressure is perturbation oer bkg) U_og2 = (self.permeability / visc_gas) * (delta_P_grad) Mdot_out = eps_g * rho_g * surface_area_chamber_degassing * U_og2 degass_hdot_water = self.param['c_g'] * T * Mdot_out ################## Flux out of the chamber -- given the permeability of the chamber .. # U_og = self.input_functions.func_Uog(eps_g,eps_x,m_eq,rho_m,rho_g,T,delta_P_grad) # Mdot_out1 = eps_g*rho_g*surface_area_chamber_degassing*U_og # degass_hdot_water1 = self.param['c_g']*T*Mdot_out1 # if np.abs(Mdot_out2) > 5 : # pdb.set_trace() # print(Mdot_out2) # tmp1_sign = np.sign(Mdot_out1/Mdot_out2) # if (tmp1_sign == 1.0) : # if (np.abs(Mdot_out2) > np.abs(Mdot_out1)) : # Mdot_out = Mdot_out2 # degass_hdot_water = degass_hdot_water2 # Q_fluid_flux_out = 0. # else : # Mdot_out = Mdot_out1 # degass_hdot_water = degass_hdot_water1 # Q_fluid_flux_out = (Mdot_out - Mdot_out2)/surface_area_chamber_degassing/rho_g # extra term for the pressure equation .., m/s (i.e a velocity ) # else : # Mdot_out = Mdot_out2 + Mdot_out1 # degass_hdot_water = degass_hdot_water2 +degass_hdot_water1 # Q_fluid_flux_out = Mdot_out1/surface_area_chamber_degassing/rho_g # extra term for the pressure equation .., m/s (i.e a velocity ) QH_fluid_flux_out = np.copy( degass_hdot_water ) / surface_area_chamber_degassing # W/m^2 else: Mdot_out = 0. degass_hdot_water = 0. QH_fluid_flux_out = 0. # Q_fluid_flux_out = 0. # QH_fluid_flux_out = 0. Mdot_v_out = np.copy(Mdot_out) # mass loss = water loss rate Hdot_out = Q_out + degass_hdot_water elif eruption == True: ########################## if self.param['vol_degass'] == 1.: # Note that there is no buoyancy term since the fluid is in equilbrium # (Pressure is perturbation over bkg) surface_area_chamber_degassing = 4. * np.pi * a**2. * self.param[ 'degass_frac_chm'] delta_P_grad = (P - mean_P_out) / a / self.param['frac_length'] U_og2 = (self.permeability / visc_gas) * (delta_P_grad) Mdot_out2 = eps_g * rho_g * surface_area_chamber_degassing * U_og2 degass_hdot_water = self.param['c_g'] * T * Mdot_out2 QH_fluid_flux_out = np.copy( degass_hdot_water ) / surface_area_chamber_degassing # W/m^2 else: Mdot_out2 = 0. degass_hdot_water = 0. QH_fluid_flux_out = 0. ########################## # if value_diking : Mdot_out1 = self.input_functions.crit_outflow\ (eps_x,m_eq,T,rho,self.param['depth'],self.param['Area_conduit'], self.param['S'],(P-self.plith + P_buoyancy), additional_model=self.param['outflow_model']) Mdot_v_out = m_eq * rho_m * ( 1. - eps_g - eps_x ) * Mdot_out1 / rho + rho_g * eps_g * Mdot_out1 / rho + Mdot_out2 Mdot_out = Mdot_out1 + Mdot_out2 Hdot_out = c * T * Mdot_out1 + Q_out + degass_hdot_water # else : # surface_area_chamber_degassing = 4. * np.pi * a ** 2. * self.param['degass_frac_chm'] # delta_P_grad = (P - mean_P_out) / a / self.param['frac_length'] # U_og1 = (self.permeability_frac/ visc_gas) * (delta_P_grad) # Mdot_out1 = eps_g * rho_g * surface_area_chamber_degassing * U_og1 # degass_hdot_water1 = self.param['c_g'] * T * Mdot_out2 # Mdot_v_out = Mdot_out1 + Mdot_out2 # Mdot_out = Mdot_out1 + Mdot_out2 # Hdot_out = degass_hdot_water1 + Q_out + degass_hdot_water else: raise NotImplementedError('eruption not specified') ############################################################# self.T_flux_list.update(QH_fluid_flux_out) self.P_flux_list.update(0.) self.flux_in_vol.update(Mdot_v_in) self.flux_out_vol.update(Mdot_v_out) # ############################################################# #% viscous relaxation - #% crustal viscosity (Pa s) ############################################################# eta_r_new = self.input_functions.crustal_viscosity( self.T_out[indx_use_visc], self.R_outside[indx_use_visc]) if self.param['visc_relax'] == 1.: # P_loss1 = (P-self.plith)/eta_r_new P_loss1 = (P - mean_sigma_rr_out) / eta_r_new elif self.param['visc_relax'] == 0.: P_loss1 = 0. else: raise NotImplementedError('visc_relax not specified') # ############################################################# # Pore Pressure relaxation - if self.param['press_relax'] == 1: # Set that the P_loss2 is only when eps_g > 0.02 P_loss2 = np.tanh( (eps_g / self.param['critical_eps_press_relax']) * 2.) * (self.permeability / visc_gas) * (P - mean_P_out) / ( self.param['frac_rad_press'] * a)**2. elif self.param['press_relax'] == 0: P_loss2 = 0 else: raise NotImplementedError('press_relax not specified') # ############################################################# # ############################################################# P_loss = P_loss1 + P_loss2 self.sigma_rr_eff = -(self.sigma_rr + self.P_out) # in Pa self.mean_sigma_rr_eff = np.mean( self.sigma_rr_eff[indx_use_P]) # effective stress total .. self.min_sigma_rr_eff = np.min( self.sigma_rr_eff) # effective stress total .. value4 = ( self.min_sigma_rr_eff + self.param['delta_Pc'] ) ## This is positive if max tensile stress is less than delta_Pc if value4 < 0.: print('Reached min_sigma_rr_eff threshold, t = {}'.format( t / np.pi / 1e7)) # ############################################################# # ############################################################# # coefficients in the system of unknowns Ax = B, here x= [dP/dt dT/dt dphi/dt ...] # values matrix A # conservation of (total) mass a11 = (1 / rho) * drho_dP + (1 / V) * dV_dP a12 = (1. / rho) * drho_dT + (1. / V) * dV_dT + ( 1. / rho) * drho_deps_x * deps_x_dT a13 = (1 / rho) * drho_deps_g + (1 / rho) * drho_deps_x * deps_x_deps_g # conservation of volatile mass a21 = (1/rho_g)*drho_g_dP + (1/V)*dV_dP \ + (m_eq*rho_m*(1-eps_g-eps_x))/(rho_g*eps_g)*((1/m_eq)*dm_eq_dP + (1/rho_m)*drho_m_dP + (1/V)*dV_dP) a22 = (1/rho_g)*drho_g_dT + (1/V)*dV_dT \ + (m_eq*rho_m*(1-eps_g-eps_x))/(rho_g*eps_g)*((1/m_eq)*dm_eq_dT + (1/rho_m)*drho_m_dT + (1/V)*dV_dT \ - deps_x_dT/(1-eps_g-eps_x)) a23 = 1 / eps_g - (1 + deps_x_deps_g) * m_eq * rho_m / (rho_g * eps_g) # conservation of (total) enthalpy a31 = (1/rho)*drho_dP + (1/c)*dc_dP + (1/V)*dV_dP \ + (self.param['L_e']*rho_g*eps_g)/(rho*c*T)*((1/rho_g)*drho_g_dP + (1/V)*dV_dP) \ - (self.param['L_m']*rho_x*eps_x)/(rho*c*T)*((1/rho_x)*drho_x_dP + (1/V)*dV_dP) a32 = (1/rho)*drho_dT + (1/c)*dc_dT + (1/V)*dV_dT + 1/T \ + (self.param['L_e']*rho_g*eps_g)/(rho*c*T)*((1/rho_g)*drho_g_dT + (1/V)*dV_dT) \ - (self.param['L_m']*rho_x*eps_x)/(rho*c*T)*((1/rho_x)*drho_x_dT + (1/V)*dV_dT) \ + ((1/rho)*drho_deps_x + (1/c)*dc_deps_x - (self.param['L_m']*rho_x)/(rho*c*T))*deps_x_dT a33 = (1/rho)*drho_deps_g + (1/c)*dc_deps_g \ + (self.param['L_e']*rho_g)/(rho*c*T) \ + ((1/rho)*drho_deps_x + (1/c)*dc_deps_x - (self.param['L_m']*rho_x)/(rho*c*T))*deps_x_deps_g # values vector B # conservation of (total) mass b1 = (Mdot_in - Mdot_out) / (rho * V) - P_loss # conservation of volatile mass b2 = (Mdot_v_in - Mdot_v_out) / (rho_g * eps_g * V) - P_loss * (1 + (m_eq * rho_m * (1 - eps_g - eps_x)) / (rho_g * eps_g)) # conservation of (total) enthalpy b3 = (Hdot_in - Hdot_out) / (rho * c * T * V) - P_loss * ( 1 - (self.param['L_m'] * rho_x * eps_x) / (rho * c * T) + (self.param['L_e'] * rho_g * eps_g) / (rho * c * T) - P / (rho * c * T)) # set up matrices to solve using Cramer's rule A = np.array([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]]) A_P = np.array([[b1, a12, a13], [b2, a22, a23], [b3, a32, a33]]) A_T = np.array([[a11, b1, a13], [a21, b2, a23], [a31, b3, a33]]) A_eps_g = np.array([[a11, a12, b1], [a21, a22, b2], [a31, a32, b3]]) det_A = det(A) dP_dt = det(A_P) / det_A dT_dt = det(A_T) / det_A deps_g_dt = det(A_eps_g) / det_A dV_dt = dV_dP * dP_dt + dV_dT * dT_dt + V * P_loss drho_m_dt = drho_m_dP * dP_dt + drho_m_dT * dT_dt drho_x_dt = drho_x_dP * dP_dt + drho_x_dT * dT_dt dydz = np.zeros(6) # column vector dydz[0] = dP_dt dydz[1] = dT_dt dydz[2] = deps_g_dt dydz[3] = dV_dt dydz[4] = drho_m_dt dydz[5] = drho_x_dt return dydz
def k_m(self): ''' Returns the local stiffness matrix for membrane (in-plane) stresses. Plane stress is assumed ''' t = self.t C = self.C() # Define the gauss point for numerical integration gp = 1 / 3**0.5 # Get the membrane B matrices for each gauss point # Doing this now will save us from doing it twice below B1 = self.B_m(gp, gp) B2 = self.B_m(-gp, gp) B3 = self.B_m(-gp, -gp) B4 = self.B_m(gp, -gp) # See reference 1 at the bottom of page 353, and reference 2 page 466 k = t * (matmul(B1.T, matmul(C, B1)) * det(self.J(gp, gp)) + matmul(B2.T, matmul(C, B2)) * det(self.J(-gp, gp)) + matmul(B3.T, matmul(C, B3)) * det(self.J(-gp, -gp)) + matmul(B4.T, matmul(C, B4)) * det(self.J(gp, -gp))) # At this point `k` only contains terms for the degrees of freedom # associated with membrane action. Expand `k` to include zero terms for # the degrees of freedom related to bending forces. This will allow # the bending and membrane stiffness matrices to be summed directly # later on. `numpy` has an `insert` function that can be used to # insert rows and columns of zeros one at a time, but it is very slow # as it makes a temporary copy of the matrix term by term each time # it's called. The algorithm used here accomplishes the same thing # much faster. Terms are copied only once. k_exp = zeros((24, 24)) # Step through each term in the unexpanded stiffness matrix # i = Unexpanded matrix row for i in range(8): # j = Unexpanded matrix column for j in range(8): # Find the corresponding term in the expanded stiffness # matrix # m = Expanded matrix row if i in [0, 2, 4, 6]: # indices associated with displacement in x m = i * 3 if i in [1, 3, 5, 7]: # indices associated with displacement in y m = i * 3 - 2 # n = Expanded matrix column if j in [0, 2, 4, 6]: # indices associated with displacement in x n = j * 3 if j in [1, 3, 5, 7]: # indices associated with displacement in y n = i * 3 - 2 # Ensure the indices are integers rather than floats m, n = round(m), round(n) # Add the term from the unexpanded matrix into the expanded # matrix k_exp[m, n] = k[i, j] return k_exp
def neff(line, inname, outname, flux_norm=711.0, skylevel=1020.67, pixel_scale=0.2, width='4096', height='4096', x='0.5', y='0.0', N_exp=1.): """ ARGUMENTS: line: which line to send to galsimcat *NOTE: using 0 for line will draw all galaxies in region inname: [string] the input dat filename to send to galsimcat outname: [string] the output filename flux_norm: total flux in ADU for one exposure of a typical galaxy of AB mag 24 skylevel: the skylevel to send to galsimcat and used in calculating neff, default is i-band NOTE: average sky level is (ADU/pixel in ONE exposure) pixel_scale: the size of each pixel in arcsec height/width: pixel size of image (0.2 arcsec/pixel) x/y: center of image in degrees N_exp: number of exposures (i & r band will have 460 (15 sec. each) exposures in 10 yr. span) """ #""" os.system('./galsimcat.py --stamps --partials -i ' + str(inname) + ' -o' + str(outname) + ' --only-line ' + str(line) + ' --flux-norm ' + str(flux_norm) + ' --sky-level ' + str(skylevel) + ' --pixel-scale ' + str(pixel_scale) + ' --width ' + str(width) + ' --height ' + str(height) + ' --margin 5.0 -x ' + str(x) + ' -y ' + str(y) + ' --no-trim') #""" #first entry in 7x7 (or 6x6 if no bulge) matrix is flux which we #can get from nominal n_eff = 0.0 hdulist = py.open(outname + '_stamps.fits', memmap=False) N_gal = n.shape(hdulist)[0] #make a txt file to write neff to os.system('touch ' + str(outname) + '.txt') f = open(str(outname) + '.txt', 'w') for k in range(1, N_gal): scidata = hdulist[k].data nominal = scidata[0].flatten() xc = scidata[3].flatten() yc = scidata[4].flatten() hlr_d = scidata[5].flatten() hlr_b = scidata[6].flatten() g1 = scidata[7].flatten() g2 = scidata[8].flatten() cube = list() cube.append(nominal) cube.append(xc) cube.append(yc) if n.sum(hlr_d) != 0.0 and n.sum(hlr_b) != 0.0: cube.append(hlr_d) cube.append(hlr_b) mat_size = 7 MAT = matrix(n.zeros((7, 7))) #MAT = matrix([[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.,0.]]) if n.sum(hlr_d) == 0.0 and n.sum(hlr_b) != 0.0: cube.append(hlr_b) mat_size = 6 MAT = matrix(n.zeros((6, 6))) #MAT = matrix([[0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.],[0.,0.,0.,0.,0.,0.]]) if n.sum(hlr_d) != 0.0 and n.sum(hlr_b) == 0.0: cube.append(hlr_d) mat_size = 6 MAT = matrix(n.zeros((6, 6))) cube.append(g1) cube.append(g2) #need to calculate the parameter crosscorelation #the variance in each pixel is equal to the sky_level + pixel flux sigma_SN = 0.25 holder = n.copy(nominal) * 0.0 for i in range(mat_size): for j in range(mat_size): MAT[i, j] = n.sum((cube[i] * cube[j]) / (skylevel + cube[0])) if linalg.det(MAT) != 0.0: #invert the matrix IMAT = MAT.I if linalg.det(MAT) == 0.0: print 'Singular matrix encountered skipping line:', k f.write('0\n') continue #find the determinant of the sub matrix {e1,e2} SUBM = matrix([[ IMAT[(mat_size - 2), (mat_size - 2)], IMAT[(mat_size - 2), (mat_size - 1)] ], [ IMAT[(mat_size - 1), (mat_size - 2)], IMAT[(mat_size - 1), (mat_size - 1)] ]]) variance_m = (SUBM[0, 0] + SUBM[1, 1]) / 2.0 #At this stage I wont bother with writing out the galaxy id, #as it can be found in the matching ..._catalog.dat file #f.write(str(SUBM[0,0])+','+str(SUBM[1,1])+'\n') f.write(str(variance_m) + '\n') # python will convert \n to os.linesep #print 'neff(',k,'):', sigma_SN**2/(sigma_SN**2 + variance_m) n_eff += sigma_SN**2 / (sigma_SN**2 + (variance_m / N_exp)) #on the last line print the total neff f.write(str(n_eff) + '\n') f.close() hdulist.close() return n_eff
def k_b(self): ''' Returns the local stiffness matrix for bending stresses ''' Cb = self.Cb() Cs = self.Cs() # Define the gauss point for numerical integration gp = 1 / 3**0.5 # Get the determinant of the Jacobian matrix for each gauss pointing # Doing this now will save us from doing it twice below J1 = det(self.J(gp, gp)) J2 = det(self.J(-gp, gp)) J3 = det(self.J(-gp, -gp)) J4 = det(self.J(gp, -gp)) # Get the bending B matrices for each gauss point B1 = self.B_kappa(gp, gp) B2 = self.B_kappa(-gp, gp) B3 = self.B_kappa(-gp, -gp) B4 = self.B_kappa(gp, -gp) # Create the stiffness matrix with bending stiffness terms # See Reference 1, Equation 5.94 k = (matmul(B1.T, matmul(Cb, B1)) * J1 + matmul(B2.T, matmul(Cb, B2)) * J2 + matmul(B3.T, matmul(Cb, B3)) * J3 + matmul(B4.T, matmul(Cb, B4)) * J4) # Get the shear B matrices for each gauss point B1 = self.B_gamma_MITC4(gp, gp) B2 = self.B_gamma_MITC4(-gp, gp) B3 = self.B_gamma_MITC4(-gp, -gp) B4 = self.B_gamma_MITC4(gp, -gp) # Add shear stiffness terms to the stiffness matrix k += (matmul(B1.T, matmul(Cs, B1)) * J1 + matmul(B2.T, matmul(Cs, B2)) * J2 + matmul(B3.T, matmul(Cs, B3)) * J3 + matmul(B4.T, matmul(Cs, B4)) * J4) # Calculate the stiffness of a weak spring for the drilling degree of freedom (rotation about local z) k_rz = min(abs(k[1, 1]), abs(k[2, 2]), abs(k[4, 4]), abs(k[5, 5]), abs(k[7, 7]), abs(k[8, 8]), abs(k[10, 10]), abs( k[11, 11])) / 1000 # At this point `k` only contains terms for the degrees of freedom # associated with bending action. Expand `k` to include zero terms for # the degrees of freedom related to membrane forces. This will allow # the bending and membrane stiffness matrices to be summed directly # later on. `numpy` has an `insert` function that can be used to # insert rows and columns of zeros one at a time, but it is very slow # as it makes a temporary copy of the matrix term by term each time # it's called. The algorithm used here accomplishes the same thing # much faster. Terms are copied only once. # Initialize the expanded stiffness matrix to all zeros k_exp = zeros((24, 24)) # Step through each term in the unexpanded stiffness matrix # i = Unexpanded matrix row for i in range(12): # j = Unexpanded matrix column for j in range(12): # Find the corresponding term in the expanded stiffness # matrix # m = Expanded matrix row if i in [0, 3, 6, 9]: # indices associated with deflection in z m = 2 * i + 2 if i in [1, 4, 7, 10]: # indices associated with rotation about x m = 2 * i + 1 if i in [2, 5, 8, 11]: # indices associated with rotation about y m = 2 * i # n = Expanded matrix column if j in [0, 3, 6, 9]: # indices associated with deflection in z n = 2 * j + 2 if j in [1, 4, 7, 10]: # indices associated with rotation about x n = 2 * j + 1 if j in [2, 5, 8, 11]: # indices associated with rotation about y n = 2 * j # Ensure the indices are integers rather than floats m, n = round(m), round(n) # Add the term from the unexpanded matrix into the expanded # matrix k_exp[m, n] = k[i, j] # Instead of the nested loops above, the rows of zeros could have been # inserted directly into `k` as shown in these next lines, but it # would be a slower process. # k = insert(k, 12, 0, axis=0) # k = insert(k, 12, 0, axis=1) # k = insert(k, 9, 0, axis=0) # k = insert(k, 9, 0, axis=1) # k = insert(k, 9, 0, axis=0) # k = insert(k, 9, 0, axis=1) # k = insert(k, 9, 0, axis=0) # k = insert(k, 9, 0, axis=1) # k = insert(k, 6, 0, axis=0) # k = insert(k, 6, 0, axis=1) # k = insert(k, 6, 0, axis=0) # k = insert(k, 6, 0, axis=1) # k = insert(k, 6, 0, axis=0) # k = insert(k, 6, 0, axis=1) # k = insert(k, 3, 0, axis=0) # k = insert(k, 3, 0, axis=1) # k = insert(k, 3, 0, axis=0) # k = insert(k, 3, 0, axis=1) # k = insert(k, 3, 0, axis=0) # k = insert(k, 3, 0, axis=1) # k = insert(k, 0, 0, axis=0) # k = insert(k, 0, 0, axis=1) # k = insert(k, 0, 0, axis=0) # k = insert(k, 0, 0, axis=1) # Add the drilling degree of freedom's weak spring k_exp[5, 5] = k_rz k_exp[11, 11] = k_rz k_exp[17, 17] = k_rz k_exp[23, 23] = k_rz return k_exp
def setLatBase(self, base): """Set matrix of unit cell base vectors and calculate corresponding lattice parameters and stdbase, baserot and metrics tensors. Return self. """ self.base = numpy.array(base) detbase = numalg.det(self.base) if abs(detbase) < 1.0e-8: emsg = "base vectors are degenerate" raise LatticeError(emsg) elif detbase < 0.0: emsg = "base is not right-handed" raise LatticeError(emsg) self.a = numpy.sqrt(numpy.dot(self.base[0, :], self.base[0, :])) self.b = numpy.sqrt(numpy.dot(self.base[1, :], self.base[1, :])) self.c = numpy.sqrt(numpy.dot(self.base[2, :], self.base[2, :])) ca = ca = numpy.dot(self.base[1, :], self.base[2, :]) / (self.b * self.c) cb = cb = numpy.dot(self.base[0, :], self.base[2, :]) / (self.a * self.c) cg = cg = numpy.dot(self.base[0, :], self.base[1, :]) / (self.a * self.b) sa = sa = numpy.sqrt(1.0 - ca**2) sb = sb = numpy.sqrt(1.0 - cb**2) sg = sg = numpy.sqrt(1.0 - cg**2) (self.ca, self.sa) = (ca, sa) (self.cb, self.sb) = (cb, sb) (self.cg, self.sg) = (cg, sg) self.alpha = math.degrees(math.acos(ca)) self.beta = math.degrees(math.acos(cb)) self.gamma = math.degrees(math.acos(cg)) # Vunit is a volume of unit cell with a=b=c=1 Vunit = math.sqrt(1.0 + 2.0 * ca * cb * cg - ca * ca - cb * cb - cg * cg) # reciprocal lattice self.ar = sa / (self.a * Vunit) self.br = sb / (self.b * Vunit) self.cr = sg / (self.c * Vunit) car = (cb * cg - ca) / (sb * sg) sar = math.sqrt(1.0 - car**2) cbr = (ca * cg - cb) / (sa * sg) sbr = math.sqrt(1.0 - cbr**2) cgr = (ca * cb - cg) / (sa * sb) sgr = math.sqrt(1.0 - cgr**2) (self.car, self.sar) = (car, sar) (self.cbr, self.sbr) = (cbr, sbr) (self.cgr, self.sgr) = (cgr, sgr) self.alphar = math.degrees(math.acos(car)) self.betar = math.degrees(math.acos(cbr)) self.gammar = math.degrees(math.acos(cgr)) # standard orientation of lattice vectors self.stdbase = numpy.array( [[1.0 / self.ar, -cgr / sgr / self.ar, cb * self.a], [0.0, self.b * sa, self.b * ca], [0.0, 0.0, self.c]], dtype=float) # calculate unit cell rotation matrix, base = stdbase*baserot self.baserot = numpy.dot(numalg.inv(self.stdbase), self.base) self.recbase = numalg.inv(self.base) # bases normalized to unit reciprocal vectors self.normbase = numpy.array([ self.base[0, :] * self.ar, self.base[1, :] * self.br, self.base[2, :] * self.cr ]) self.recnormbase = numpy.array(self.recbase) self.recnormbase[:, 0] /= self.ar self.recnormbase[:, 1] /= self.br self.recnormbase[:, 2] /= self.cr # update metrics tensor self.metrics = numpy.array( [[self.a * self.a, self.a * self.b * cg, self.a * self.c * cb], [self.b * self.a * cg, self.b * self.b, self.b * self.c * ca], [self.c * self.a * cb, self.c * self.b * ca, self.c * self.c]], dtype=float) return self
def B_gamma_MITC4(self, r, s): ''' Returns the [B] matrix for shear. MITC stands for mixed interpolation tensoral components. MITC elements are used in many programs and are known to perform well for thick and thin plates, and for distorted plate geometries. ''' # Get the local coordinates for the element x1, y1, x2, y2, x3, y3, x4, y4 = self.x1, self.y1, self.x2, self.y2, self.x3, self.y3, self.x4, self.y4 x_axis = array([1, 0, 0]) y_axis = array([0, 1, 0]) # Reference 1, Equations 5.105 Ax = x1 - x2 - x3 + x4 Bx = x1 - x2 + x3 - x4 Cx = x1 + x2 - x3 - x4 Ay = y1 - y2 - y3 + y4 By = y1 - y2 + y3 - y4 Cy = y1 + y2 - y3 - y4 # Find the angles between the axes of the natural coordinate system and # the local x-axis. r_axis = array([(x1 + x4) / 2 - (x2 + x3) / 2, (y1 + y4) / 2 - (y2 + y3) / 2, 0]).T s_axis = array([(x1 + x2) / 2 - (x3 + x4) / 2, (y1 + y2) / 2 - (y3 + y4) / 2, 0]).T r_axis = r_axis / norm(r_axis) s_axis = s_axis / norm(s_axis) alpha = arccos(dot(r_axis, x_axis)) beta = arccos(dot(s_axis, x_axis)) # Reference 1, Equations 5.103 and 5.104 (p. 426) det_J = det(self.J(r, s)) gr = ((Cx + r * Bx)**2 + (Cy + r * By)**2)**0.5 / (8 * det_J) gs = ((Ax + s * Bx)**2 + (Ay + s * By)**2)**0.5 / (8 * det_J) # See Jupyter Notebook derivation for this next part gamma_rz = gr / 4 * array([[ 2 * (s + 1), -s * y1 + s * y2 - y1 + y2, s * x1 - s * x2 + x1 - x2, 2 * (-s - 1), -s * y1 + s * y2 - y1 + y2, s * x1 - s * x2 + x1 - x2, 2 * (s - 1), -s * y3 + s * y4 + y3 - y4, s * x3 - s * x4 - x3 + x4, 2 * (1 - s), -s * y3 + s * y4 + y3 - y4, s * x3 - s * x4 - x3 + x4 ]]) gamma_sz = gs / 4 * array([[ 2 * (r + 1), -r * y1 + r * y4 - y1 + y4, r * x1 - r * x4 + x1 - x4, 2 * (1 - r), r * y2 - r * y3 - y2 + y3, -r * x2 + r * x3 + x2 - x3, 2 * (r - 1), r * y2 - r * y3 - y2 + y3, -r * x2 + r * x3 + x2 - x3, 2 * (-r - 1), -r * y1 + r * y4 - y1 + y4, r * x1 - r * x4 + x1 - x4 ]]) # Reference 1, Equations 5.102 B_gamma_MITC4 = zeros((2, 12)) B_gamma_MITC4[0, :] = gamma_rz * sin(beta) - gamma_sz * sin(alpha) B_gamma_MITC4[1, :] = -gamma_rz * cos(beta) + gamma_sz * cos(alpha) # Return the [B] matrix for shear return B_gamma_MITC4
def func(theta): K = rq_kernel(X_train, X_train, l=theta[0], sigma_f=theta[1],alpha=theta[2]) + noise**2 * np.eye(len(X_train)) return 0.5 * np.log(det(K)) + 0.5 * Y_train.T.dot(inv(K).dot(Y_train)) + 0.5 * len(X_train) * np.log(2*np.pi)
def ppca_mv(Ye, d, dia): N, D = Ye.shape # N observations in D dimensions threshold = 10**( -4) # minimal relative change in objective funciton to continue hidden = np.isnan(Ye) missing = np.count_nonzero(hidden) M = np.zeros((1, D), dtype=np.float64) # compute data mean and center data if missing: for i in range(0, D): M[0, i] = np.mean(Ye[~hidden[:, i], i]) else: M = np.mean(Ye) print Ye.shape print npML.repmat(M, N, 1).shape print N Ye = Ye - npML.repmat(M, N, 1) if missing: Ye[hidden] = 0 # ======= Initialization ====== C = np.random.randn(D, d) CtC = C.transpose().dot(C) X = Ye.dot(C).dot(LA.inv(CtC)) recon = X.dot(C.transpose()) recon[hidden] = 0 ss = np.sum(np.sum(np.square(recon - Ye))) / (N * D - missing) count = 1 old = np.inf while count: # ============ EM iterations ========== Sx = LA.inv(np.eye(d) + CtC / ss) # ====== E-step, (co)variances ===== ss_old = ss if missing: proj = X.dot(C.transpose()) Ye[hidden] = proj[hidden] X = Ye.dot(C.dot(Sx / ss)) # ==== E step: expected values ==== SumXtX = X.transpose().dot(X) # ======= M-step ===== C = Ye.transpose().dot(X).dot(LA.inv(SumXtX + N * Sx)) CtC = C.transpose().dot(C) ss = (np.sum(np.sum(np.square(X.dot(C.transpose()) - Ye))) + N * np.sum(np.sum(np.multiply(CtC, Sx))) + missing * ss_old) / (N * D) objective = N * D + N * (D * np.log(ss) + np.trace(Sx) - np.log( LA.det(Sx))) + np.trace(SumXtX) - missing * np.log(ss_old) rel_ch = np.absolute(1 - objective / old) old = objective count = count + 1 if rel_ch < threshold and count > 5: count = 0 if dia: print('Objective: %.2f relative change: %.5f \n') % (objective, rel_ch) # ============ EM iterations ========== C = sciLA.orth(C) vals, vecs = LA.eig(np.cov(Ye.dot(C).transpose())) order = vals.argsort()[::-1] vals = vals[order] vecs = vecs[:, order] C = C.dot(vecs) X = Ye.dot(C) # add data mean to expected complete data Ye = Ye + npML.repmat(M, N, 1) return C, ss, M, X, Ye
from numpy import * from numpy.linalg import svd,eig,det,inv a=array([[3,2,2],[2,2,1],[2,1,2]]) u,s,v = svd(a) #print u print "singular values,",s #print v #print dot(dot(u,diag(s)),v) #print dot(u,v) print "det", det(a) e,v= eig(a) print "eigen values,",e print "eigen vectos,",v iv=inv(v) print "inv vectors,", iv print "a:", dot(dot(v,diag(e)),iv) print dot(dot(iv,a),v) print dot(dot(dot(dot(v,v),v),v),v) ########################################################################### ########################################################################### ### Codes for graphs and vertex and ants from http://www.boriel.com/?lang=en
def E_ln_lam_k(k, nu, W): return np.sum(digamma(nu[k]+1-np.arange(D)+1)) + D*np.log(2) + np.log(det(W[k]))
norm1 = la.norm(c, 1, axis=0) # 1范数为模之和 norm2 = la.norm(c, 2, axis=0) # 2范数 print(norm1) print(norm2) # 06. # cond(...) # la.cond(A): computes condition number of matrix A. cond = la.cond(c) print(cond) print(np.mean(cond)) # condition number == mean value of matrix C # 07. # det(...) # la.det(A): computes the determinant-- 行列式 of a square matrix A. 方阵 det = la.det(c) print(det) print(np.floor(det)) # 向下取整 # 08. # inv(...) # la.inv(A): computes the inverse of a square matrix A. 方阵 # Be careful about the shapes an sizes. trans = c.T # 方阵(矩阵)转置 inv = la.inv(c) # 方阵求逆矩阵 print(trans) print(inv) # 09. # solve(...) # la.solve(A,b): solves Ax = b for square matrix A.
def post_analyze(self,analyzer): if not self.has_generic_input(): calctypes = self.input.get_output_info('calctypes') opt_run = calctypes!=None and 'opt' in calctypes if opt_run: opt_file = analyzer.results.optimization.optimal_file if opt_file is None: self.failed = True #end if #end if exc_run = 'excitation' in self if exc_run: exc_failure = False edata = self.read_einspline_dat() exc_input = self.excitation exc_spin,exc_type,exc_spins,exc_types,exc1,exc2 = check_excitation_type(exc_input) elns = self.input.get_electron_particle_set() if exc_type==exc_types.band: # Band Index 'tw1 band1 tw2 band2'. Eg., '0 45 3 46' # Check that tw1,band1 is no longer in occupied set tw1,bnd1 = exc2.split()[0:2] tw2,bnd2 = exc2.split()[2:4] if exc1 in ('up','down'): spin_channel = exc1 dsc = edata[spin_channel] for idx,(tw,bnd) in enumerate(zip(dsc.TwistIndex,dsc.BandIndex)): if tw == int(tw1) and bnd == int(bnd1): # This orbital should no longer be in the set of occupied orbitals if idx<elns.groups[spin_channel[0]].size: msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, the first orbital \'{} {}\' is still occupied (see einspline file).\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],tw1,bnd1) exc_failure = True #end if elif tw == int(tw2) and bnd == int(bnd2): # This orbital should be in the set of occupied orbitals if idx>=elns.groups[spin_channel[0]].size: msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, the second orbital \'{} {}\' is not occupied (see einspline file).\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],tw2,bnd2) exc_failure = True #end if #end if #end for else: self.warn('No check for \'{}\' excitation of type \'{}\' was done. When this path is possible, then a check should be written.'.format(exc_input[0],exc_input[1])) #end if elif exc_type in (exc_types.energy,exc_types.lowest): # Lowest or Energy Index '-orbindex1 +orbindex2'. Eg., '-4 +5' if exc_type==exc_types.lowest: if exc_spin==exc_spins.down: orb1 = elns.groups.d.size else: orb1 = elns.groups.u.size #end if orb2 = orb1+1 else: orb1 = int(exc_input[1].split()[0][1:]) orb2 = int(exc_input[1].split()[1][1:]) #end if if exc1 in ('up','down'): spin_channel = exc1 nelec = elns.groups[spin_channel[0]].size eigs_spin = edata[spin_channel].Energy # Construct the correct set of occupied orbitals by hand based on # orb1 and orb2 values that were input by the user excited = eigs_spin order = eigs_spin.argsort() ground = excited[order] # einspline orbital ordering for excited state excited = excited[:nelec] # hand-crafted orbital order for excited state hc_excited = np.array(list(ground[:orb1-1])+[ground[orb2-1]]+list(ground[orb1:nelec])) etol = 1e-6 if np.abs(hc_excited-excited).max() > etol: msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, the second orbital \'{}\' is not occupied (see einspline file).\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],orb1) exc_failure = True #end if elif exc1 in ('singlet','triplet'): wf = self.input.get('wavefunction') occ = wf.determinantset.multideterminant.detlist.csf.occ if occ[int(orb1)-1]!='1': msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, this is inconsistent with the occupations in detlist \'{}\'.\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],occ) exc_failure = True #end if if occ[int(orb2)-1]!='1': msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, this is inconsistent with the occupations in detlist \'{}\'.\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],occ) exc_failure = True #end if #end if else: # The format is: 'gamma vb z cb' if exc1 in ('singlet','triplet'): self.warn('No check for \'{}\' excitation of type \'{}\' was done. When this path is possible, then a check should be written.'.format(exc_input[0],exc_input[1])) else: # assume excitation of form 'gamma vb k cb' or 'gamma vb-1 k cb+1' excitation = exc2.upper().split(' ') k_1, band_1, k_2, band_2 = excitation tilematrix = self.system.structure.tilematrix() wf = self.input.get('wavefunction') if exc_spin==exc_spins.up: sdet = wf.determinantset.get('updet') else: sdet = wf.determinantset.get('downdet') #end if from numpy import linalg,where,isclose vb = int(sdet.size / abs(linalg.det(tilematrix))) -1 # Separate for each spin channel cb = vb+1 # Convert band_1, band_2 to band indexes bands = [band_1, band_2] for bnum, b in enumerate(bands): b = b.lower() if 'cb' in b: if '-' in b: b = b.split('-') bands[bnum] = cb - int(b[1]) elif '+' in b: b = b.split('+') bands[bnum] = cb + int(b[1]) else: bands[bnum] = cb #end if elif 'vb' in b: if '-' in b: b = b.split('-') bands[bnum] = vb - int(b[1]) elif '+' in b: b = b.split('+') bands[bnum] = vb + int(b[1]) else: bands[bnum] = vb #end if else: QmcpackInput.class_error('{0} in excitation has the wrong formatting'.format(b)) #end if #end for band_1, band_2 = bands # Convert k_1 k_2 to wavevector indexes structure = self.system.structure.get_smallest().copy() structure.change_units('A') from structure import get_kpath kpath = get_kpath(structure=structure) kpath_label = array(kpath['explicit_kpoints_labels']) kpath_rel = kpath['explicit_kpoints_rel'] k1_in = k_1 k2_in = k_2 if k_1 in kpath_label and k_2 in kpath_label: k_1 = kpath_rel[where(kpath_label == k_1)][0] k_2 = kpath_rel[where(kpath_label == k_2)][0] kpts = structure.kpoints_unit() found_k1 = False found_k2 = False for knum, k in enumerate(kpts): if isclose(k_1, k).all(): k_1 = knum found_k1 = True #end if if isclose(k_2, k).all(): k_2 = knum found_k2 = True #end if #end for if not found_k1 or not found_k2: QmcpackInput.class_error('Requested special kpoint is not in the tiled cell\nRequested "{}", present={}\nRequested "{}", present={}\nAvailable kpoints: {}'.format(k1_in,found_k1,k2_in,found_k2,sorted(set(kpath_label)))) #end if else: QmcpackInput.class_error('Excitation wavevectors are not found in the kpath\nlabels requested: {} {}\nlabels present: {}'.format(k_1,k_2,sorted(set(kpath_label)))) #end if tw1,bnd1 = (k_1,band_1) tw2,bnd2 = (k_2,band_2) spin_channel = exc1 dsc = edata[spin_channel] for idx,(tw,bnd) in enumerate(zip(dsc.TwistIndex,dsc.BandIndex)): if tw == int(tw1) and bnd == int(bnd1): # This orbital should no longer be in the set of occupied orbitals if idx<elns.groups[spin_channel[0]].size: msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, the first orbital \'{} {}\' is still occupied (see einspline file).\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],tw1,bnd1) exc_failure = True #end if elif tw == int(tw2) and bnd == int(bnd2): # This orbital should be in the set of occupied orbitals if idx>=elns.groups[spin_channel[0]].size: msg = 'WARNING: You requested \'{}\' excitation of type \'{}\',\n' msg += ' however, the second orbital \'{} {}\' is not occupied (see einspline file).\n' msg += ' Please check your input.' msg = msg.format(spin_channel,exc_input[1],tw2,bnd2) exc_failure = True #end if #end if #end for #end if if exc_failure: self.failed = True self.warn(msg) filename = self.identifier+'_errors.txt' open(os.path.join(self.locdir,filename),'w').write(msg)
def integrand(y): VRot = np.array([[-2 * Vup, 0], [0, -2 * Vdown]]) g = np.array([[Dyson(GF(y), Vup)[2, 2], 0], [0, Dyson(GF(y), Vdown)[2, 2]]]) return 1.0 / pi * log(det(np.eye(2) - g.dot(VRot))).real
#natoms = 3 #nkppra = 10000 #nk = int(nkppra/natoms) Nmesh=200 #(a - b) (-b^2 + a c) for BCT #start b = 0 and c = a #M = zeros((3,3),dtype=np_int) print 'Target N kpoints', Nmesh a = int(rint(Nmesh**(1/3.0))); c = a b = 0 params = [a,b] M = Mfill(params) print 'Starting M' print 'Det M', det(M) print M B = lattice() ##############BCT lattice alat = 2*sqrt(2) ca = 4/3. clat = alat*ca B.vecs = matrix(( [ -alat/2, alat/2, alat/2], [ alat/2, -alat/2, alat/2], [ clat/2, clat/2, -clat/2] ), dtype=float) #print 'B vectors before inverse and transpose';print B.vecs #B.vecs = trimSmall(inv(B.vecs)).T #############End BCT lattice
def driftRigid(X, Y, w=0.5): from numpy.linalg import svd, det from numpy import trace, eye, full, asarray, zeros from .geometry import pairwiseDistanceSquared, RigidXform if not (X.ndim == Y.ndim == 2): raise ValueError("Expecting 2D input data, got {}D and {}D".format( X.ndim, Y.ndim)) if X.shape[1] != Y.shape[1]: raise ValueError( "Expecting points with matching dimensionality, got {} and {}". format(X.shape[1:], Y.shape[1:])) D = X.shape[1] N = len(X) M = len(Y) norms = list(map(RigidXform.normalize, [X, Y])) X, Y = map(op.matmul, norms, [X, Y]) R = eye(D) s = 1.0 t = zeros(D) if isinstance(w, float): if not (0 <= w <= 1): raise ValueError("w must be in the range [0..1], got {}".format(w)) prior = full((N, M), (1 - w) / M, dtype='double') else: prior = asarray(w) sigma_squared = pairwiseDistanceSquared( X, RigidXform(R, t, s) @ Y).sum() / (D * M * N) while True: # E-step P = eStep(X, RigidXform(R, t, s) @ Y, prior, sigma_squared) # M-step N_p = P.sum() mu_x = 1 / N_p * X.T @ P.sum(axis=1) mu_y = 1 / N_p * Y.T @ P.sum(axis=0) X_hat = X - mu_x.T Y_hat = Y - mu_y.T # This part is different to driftAffine A = X_hat.T @ P @ Y_hat U, _, VT = svd(A) C = eye(D) C[-1, -1] = det(U @ VT) R = U @ C @ VT s = trace(A.T @ R) / trace( (Y_hat.T * P.sum(axis=0, keepdims=True)) @ Y_hat) t = mu_x - s * R @ mu_y old_sigma_squared = sigma_squared sigma_squared = (trace( (X_hat.T * P.sum(axis=1, keepdims=True).T) @ X_hat) - s * trace(A.T @ R)) / (N_p * D) yield P, norms[0].inverse @ RigidXform(R, t, s) @ norms[1] if abs(sigma_squared) < 1e-12 or abs(old_sigma_squared - sigma_squared) < 1e-12: # Sigma squared == 0 on positive fit, but occasionally ~= -1e17 break
vol_obj = VolumeMetrics.VolumeMetrics() vol_obj.set_image_object(ablation_segmentation=dcm_img) volume_ablation_eav = vol_obj.get_volume_ml(dcm_img) print('Éffective Ablation Volume with spacing into account [ml]:', volume_ablation_eav) rx_outer, ry_outer, rz_outer = get_radii_from_matrix(A_outer) vol_formula_outer = volume_ellipsoid(rx_outer, ry_outer, rz_outer) vol_spacing_outer = volume_ellipsoid_spacing(rx_outer, ry_outer, rz_outer, spacing) print('Outer Ellipsoid volume:', vol_formula_outer) print('Outer Ellipsoid volume with spacing ellipsoid scikit-image:', vol_spacing_outer) # rx_inner, ry_inner, rz_inner = B_inner[0, 0], B_inner[1, 1], B_inner[2,2] vol_formula_inner = np.sqrt(la.det(B_inner) / 1000) * 4.19 # vol_spacing_inner = volume_ellipsoid_spacing(rx_inner, ry_inner, rz_inner, spacing) print('Inner Ellipsoid volume:', vol_formula_inner) #%% PLOT fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='blue', label='Ablation Segmentation') plot_ellipsoid(A_outer, centroid_outer, 'green', ax) # plot_ellipsoid(B_inner, centroid_inner, 'orange', ax) plt.legend(loc='best') plt.show() # timestr = time.strftime("%H%M%S-%Y%m%d") # file_dir = r"C:\develop\segmentation-eval\figures" # filepath = os.path.join(file_dir, 'ellipsoid_' + timestr)