def good2n(nmax, coefs_list, ref_coefs, threshold=0.90, outfile=''): ## cc=0.90 is equivalent to 5% mutation in real space at nmax<=10 max_indx = math.nlm_array(nmax).nlm().size() for nn in range(nmax, 1, -1): min_indx = math.nlm_array(nn - 1).nlm().size() #coef_0 = ref_coefs[min_indx:max_indx] coef_0 = ref_coefs[0:max_indx] mean_0 = abs(ref_coefs[0]) sigma_0 = flex.sum(flex.norm(coef_0)) - mean_0**2 sigma_0 = smath.sqrt(sigma_0) cc_array = flex.double() #out = open(outfile,"w") for coef in coefs_list: #coef_1 = coef[min_indx:max_indx] coef_1 = coef[0:max_indx] mean_1 = abs(coef[0]) sigma_1 = flex.sum(flex.norm(coef_1)) - mean_1**2 sigma_1 = smath.sqrt(sigma_1) cov_01 = abs(flex.sum(coef_0 * flex.conj(coef_1))) cov_01 = cov_01 - mean_0 * mean_1 this_cc = cov_01 / sigma_1 / sigma_0 cc_array.append(this_cc) out = open(outfile, "a") print >> out, this_cc out.close() print this_cc mean_cc = flex.mean(cc_array) out = open(outfile, "a") print >> out, "level n: ", nn, mean_cc out.close() print "level n: ", nn, mean_cc if (mean_cc >= threshold): return nn max_indx = min_indx return nn
def nonbonded_deviations(self): if (self.n_nonbonded_proxies is not None): nonbonded_deltas = self.nonbonded_distances() r_sq = nonbonded_deltas * nonbonded_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def nonbonded_deviations(self): if(self.n_nonbonded_proxies is not None): nonbonded_deltas = self.nonbonded_distances() r_sq = nonbonded_deltas * nonbonded_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def isotropic_adp_deviation(self): if (self.n_isotropic_adp_proxies is not None): isotropic_adp_deltas_rms = adp_restraints.isotropic_adp_deltas_rms( u_cart=self.u_cart, proxies=self.isotropic_adp_proxies) i_sq = isotropic_adp_deltas_rms * isotropic_adp_deltas_rms i_ave = math.sqrt(flex.mean_default(i_sq, 0)) i_max = math.sqrt(flex.max_default(i_sq, 0)) i_min = math.sqrt(flex.min_default(i_sq, 0)) return i_min, i_max, i_ave
def chirality_deviations(self): if (self.n_chirality_proxies is not None): chirality_deltas = geometry_restraints.chirality_deltas( sites_cart=self.sites_cart, proxies=self.chirality_proxies) c_sq = chirality_deltas * chirality_deltas c_ave = math.sqrt(flex.mean_default(c_sq, 0)) c_max = math.sqrt(flex.max_default(c_sq, 0)) c_min = math.sqrt(flex.min_default(c_sq, 0)) return c_min, c_max, c_ave
def parallelity_deviations(self): if self.n_parallelity_proxies is not None: parallelity_deltas = geometry_restraints.parallelity_deltas( sites_cart=self.sites_cart, proxies=self.parallelity_proxies) p_sq = parallelity_deltas * parallelity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def rosca(m=9, hemisphere=True): """ Regular grid on the unit sphere, Rosca (2010). """ def truncate(x): if(abs(x)<1.e-6): return 0 else: return x def add(result, new): foud = False for s in result: d = math.sqrt((s[0]-new[0])**2+(s[1]-new[1])**2+(s[2]-new[2])**2) if(d<1.e-3): return result.append(new) return d_l = math.sqrt(2)/m result = flex.vec3_double() for m_ in xrange(m+1): if(m_==0): add(result, [0,0,1]) else: l_m = m_ * d_l d_phi = math.pi/(4*m_) assert l_m>=0 and l_m<=math.sqrt(2) for k in xrange(m_+1): arg1 = k*d_phi arg2 = l_m*math.sqrt(1-l_m**2/4) x_km = truncate(math.cos(arg1)*arg2) y_km = truncate(math.sin(arg1)*arg2) z_m = truncate(1.-l_m**2/2 ) add(result, [ x_km, y_km,z_m]) add(result, [ y_km, x_km,z_m]) add(result, [-y_km, x_km,z_m]) add(result, [-x_km, y_km,z_m]) add(result, [ x_km,-y_km,z_m]) add(result, [ y_km,-x_km,z_m]) add(result, [-x_km,-y_km,z_m]) add(result, [-y_km,-x_km,z_m]) if(not hemisphere): add(result, [ x_km, y_km,-z_m]) add(result, [ y_km, x_km,-z_m]) add(result, [-y_km, x_km,-z_m]) add(result, [-x_km, y_km,-z_m]) add(result, [ x_km,-y_km,-z_m]) add(result, [ y_km,-x_km,-z_m]) add(result, [-x_km,-y_km,-z_m]) add(result, [-y_km,-x_km,-z_m]) for r in result: assert abs(1.-math.sqrt(r[0]**2+r[1]**2+r[2]**2))<1.e-6 # XXX for debugging if(0): f = "HETATM%5d O HOH A%4d %8.3f%8.3f%8.3f 1.00 23.99 O " o = open("junk.pdb", "w") for i, r in enumerate(result): print >> o, f%(i, i, r[0],r[1],r[2]) o.close() return result
def tst_2d_zernike_mom(n, l, filename, h5file, flag): rebuilt = open(filename, 'w') # image = generate_image() # image=ImageToDat("/Users/wyf/Desktop/test11.png") image = preprocess_image(h5file, flag) # image = ImageToDat('cha.png') NP = int(smath.sqrt(image.size())) N = int(NP / 2) print "=====", NP, N grid_2d = math.two_d_grid(N, nmax) grid_2d.clean_space(image) grid_2d.construct_space_sum() zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax) moments = zernike_2d_mom.moments() coefs = flex.real(moments) nl_array = math.nl_array(nmax) nls = nl_array.nl() nl_array.load_coefs(nls, coefs) lfg = math.log_factorial_generator(nmax) # print nl_array.get_coef(n,l)*2 for nl, c in zip(nls, moments): if (abs(c) < 1e-3): c = 0 print nl, c reconst = flex.complex_double(NP**2, 0) for nl, c in zip(nls, moments): n = nl[0] l = nl[1] if (l > 0): c = c * 2 #rzfa = math.zernike_2d_radial(n,l,lfg) rap = math.zernike_2d_polynome(n, l) #,rzfa) i = 0 for x in range(0, NP): x = x - N for y in range(0, NP): y = y - N rr = smath.sqrt(x * x + y * y) / N if rr > 1.0: value = 0.0 else: tt = smath.atan2(y, x) value = rap.f(rr, tt) reconst[i] = reconst[i] + value * c i = i + 1 i = 0 for x in range(0, NP): for y in range(0, NP): value = reconst[i].real print >> rebuilt, x, y, value i = i + 1 rebuilt.close()
def dihedral_deviations(self): # !!!XXX!!! Warning! this works wrong because it is not aware of origin_id! if (self.n_dihedral_proxies is not None): dihedral_deltas = geometry_restraints.dihedral_deltas( sites_cart=self.sites_cart, proxies=self.dihedral_proxies) d_sq = dihedral_deltas * dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def parallelity_deviations(self): if self.n_parallelity_proxies is not None: parallelity_deltas = geometry_restraints.parallelity_deltas( sites_cart = self.sites_cart, proxies = self.parallelity_proxies) p_sq = parallelity_deltas * parallelity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def chirality_deviations(self): if(self.n_chirality_proxies is not None): chirality_deltas = geometry_restraints.chirality_deltas( sites_cart = self.sites_cart, proxies = self.chirality_proxies) c_sq = chirality_deltas * chirality_deltas c_ave = math.sqrt(flex.mean_default(c_sq, 0)) c_max = math.sqrt(flex.max_default(c_sq, 0)) c_min = math.sqrt(flex.min_default(c_sq, 0)) return c_min, c_max, c_ave
def rigid_bond_deviation(self): if (self.n_rigid_bond_proxies is not None): rigid_bond_deltas = adp_restraints.rigid_bond_deltas( sites_cart=self.sites_cart, u_cart=self.u_cart, proxies=self.rigid_bond_proxies) r_sq = rigid_bond_deltas * rigid_bond_deltas r_ave = math.sqrt(flex.mean_default(r_sq, 0)) r_max = math.sqrt(flex.max_default(r_sq, 0)) r_min = math.sqrt(flex.min_default(r_sq, 0)) return r_min, r_max, r_ave
def planarity_deviations(self): # XXX Need update, does not respect origin_id # assert 0, "Not counting for origin_id" if (self.n_planarity_proxies is not None): planarity_deltas = geometry_restraints.planarity_deltas_rms( sites_cart=self.sites_cart, proxies=self.planarity_proxies) p_sq = planarity_deltas * planarity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def reference_dihedral_deviations(self): assert 0, "Not working" if (self.n_reference_dihedral_proxies is not None): reference_dihedral_deltas = geometry_restraints.reference_dihedral_deltas( sites_cart=self.sites_cart, proxies=self.reference_dihedral_proxies) d_sq = reference_dihedral_deltas * reference_dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def dihedral_deviations(self): # !!!XXX!!! Warnign! this works wrong because it is not aware of origin_id! if(self.n_dihedral_proxies is not None): dihedral_deltas = geometry_restraints.dihedral_deltas( sites_cart = self.sites_cart, proxies = self.dihedral_proxies) d_sq = dihedral_deltas * dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def reference_dihedral_deviations(self): assert 0, "Not working" if(self.n_reference_dihedral_proxies is not None): reference_dihedral_deltas = geometry_restraints.reference_dihedral_deltas( sites_cart = self.sites_cart, proxies = self.reference_dihedral_proxies) d_sq = reference_dihedral_deltas * reference_dihedral_deltas d_ave = math.sqrt(flex.mean_default(d_sq, 0)) d_max = math.sqrt(flex.max_default(d_sq, 0)) d_min = math.sqrt(flex.min_default(d_sq, 0)) return d_min, d_max, d_ave
def planarity_deviations(self): # XXX Need update, does not respect origin_id # assert 0, "Not counting for origin_id" if(self.n_planarity_proxies is not None): planarity_deltas = geometry_restraints.planarity_deltas_rms( sites_cart = self.sites_cart, proxies = self.planarity_proxies) p_sq = planarity_deltas * planarity_deltas p_ave = math.sqrt(flex.mean_default(p_sq, 0)) p_max = math.sqrt(flex.max_default(p_sq, 0)) p_min = math.sqrt(flex.min_default(p_sq, 0)) return p_min, p_max, p_ave
def adp_similarity_deviation(self): if (self.n_adp_similarity_proxies is not None): adp_similarity_deltas_rms = adp_restraints.adp_similarity_deltas_rms( u_cart=self.u_cart, u_iso=self.u_iso, use_u_aniso=self.use_u_aniso, proxies=self.adp_similarity_proxies) a_sq = adp_similarity_deltas_rms * adp_similarity_deltas_rms a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave
def angle_deviations(self): if (self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: a_sq = angle_deltas * angle_deltas a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave else: return 0, 0, 0
def bond_deviations(self): if (self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas(sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) > 0: b_sq = bond_deltas * bond_deltas b_ave = math.sqrt(flex.mean_default(b_sq, 0)) b_max = math.sqrt(flex.max_default(b_sq, 0)) b_min = math.sqrt(flex.min_default(b_sq, 0)) return b_min, b_max, b_ave else: return 0, 0, 0
def angle_deviations(self): if(self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: a_sq = angle_deltas * angle_deltas a_ave = math.sqrt(flex.mean_default(a_sq, 0)) a_max = math.sqrt(flex.max_default(a_sq, 0)) a_min = math.sqrt(flex.min_default(a_sq, 0)) return a_min, a_max, a_ave else: return 0,0,0
def bond_deviations(self): if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: b_sq = bond_deltas * bond_deltas b_ave = math.sqrt(flex.mean_default(b_sq, 0)) b_max = math.sqrt(flex.max_default(b_sq, 0)) b_min = math.sqrt(flex.min_default(b_sq, 0)) return b_min, b_max, b_ave else: return 0,0,0
def refine(self, trial): print "--------------Trial %d-----------------"%trial, time.ctime() self.working_model = self.start_model.deep_copy() self.nlm_coefs = self.start_nlm_coefs.deep_copy() self.best_nlm_coefs = self.start_nlm_coefs.deep_copy() self.best_blq = self.start_blq.deep_copy() self.lowest_score = self.start_score init_scores = flex.double() while( init_scores.size() < 10 ): if(self.modify()): init_scores.append( self.target() ) mean = flex.mean( init_scores ) self.deltaS = smath.sqrt( flex.sum(flex.pow2(init_scores-mean) )/init_scores.size() ) self.T = self.deltaS * 20 self.nsteps = 500 self.score = mean self.working_model = self.start_model.deep_copy() while( True ): #self.T > self.deltaS/10.0): self.n_reject_this_round = 0 self.n_accept_this_round = 0 for ii in range( self.nsteps ): self.move() self.n_moves_this_round = self.n_reject_this_round + self.n_accept_this_round print "Number of moves: %d(out of %d)"%(self.n_moves_this_round, self.nsteps) print "Number of Accept/Reject: %d/%d"%(self.n_accept_this_round, self.n_reject_this_round) if( self.n_reject_this_round >= self.n_moves_this_round*0.9 ): print "Too Many rejections (%d), quit at temperature (%f)"%(self.n_reject_this_round, self.T) break self.T = self.T*0.9 self.nlm_array.load_coefs( self.nlm, self.best_nlm_coefs ) best_blq = self.blq_calculator.get_all_blq( self.nlm_array ) best_blq = best_blq/best_blq[0] out = open(self.prefix+str(trial)+'_final.blq', 'w') self.data.print_out(data=best_blq,out=out) out.close() print "total number of moves %d"%self.counter print "total number of accepted moves %d"%self.n_accept if (self.pdb_nlm is not None): align_obj = fft_align.align(self.pdb_nlm, self.nlm_array, nmax=self.nmax, refine=True) mean = abs( self.best_nlm_coefs[0] ) var = flex.sum( flex.norm( self.best_nlm_coefs ) ) sigma = smath.sqrt( var - mean*mean ) cc = align_obj.best_score cc = ( cc - mean*self.pdb_m ) / ( sigma*self.pdb_s ) print "C.C. (PDB, trial%6d) = %8.5f, Score = %8.5f"%(trial, cc, self.lowest_score) self.best_nlm_coefs = align_obj.moving_nlm.coefs() reconst_model = self.reconst_model( self.best_nlm_coefs ) xplor_map_type( reconst_model, self.np_on_grid, self.rmax, file_name=self.prefix+str(trial)+'_final_rbt.xplor') xplor_map_type( self.best_model, self.np_on_grid, self.rmax, file_name=self.prefix+str(trial)+'_final.xplor') print "-----------End of Trial %d--------------"%trial, time.ctime()
def wR2(self, cutoff_factor=None): if cutoff_factor is None: return math.sqrt(2*self.objective_data_only) fo_sq = self.observations.fo_sq strong = fo_sq.data() >= cutoff_factor*fo_sq.sigmas() fo_sq = fo_sq.select(strong) fc_sq = self.fc_sq.select(strong) wght = self.weights.select(strong) fc_sq = fc_sq.data() fo_sq = fo_sq.data() fc_sq *= self.scale_factor() wR2 = flex.sum(wght*flex.pow2((fo_sq-fc_sq)))/flex.sum(wght*flex.pow2(fo_sq)) return math.sqrt(wR2)
def refine(self, trial): print "--------------Trial %d-----------------"%trial, time.ctime() self.working_model = self.start_model.deep_copy() self.nlm_coefs = self.start_nlm_coefs.deep_copy() self.best_nlm_coefs = self.start_nlm_coefs.deep_copy() self.best_i = self.start_i.deep_copy() self.lowest_score = self.start_score init_scores = flex.double() for ii in range(10): self.modify() init_scores.append( self.target() ) mean = flex.mean( init_scores ) self.deltaS = smath.sqrt( flex.sum(flex.pow2(init_scores-mean) )/10.0 ) self.T = self.deltaS * 100 self.nsteps = 200 self.score = mean self.working_model = self.start_model.deep_copy() while( self.T > self.deltaS/2.0): self.n_reject = 0 for ii in range( self.nsteps ): self.move() print "Number of Accept/Reject: %d/%d"%(self.nsteps-self.n_reject, self.n_reject) if( self.n_reject > self.nsteps*0.9 ): print "Too Many rejections (%d), quit at temperature (%f)"%(self.n_reject, self.T) break self.T = self.T*0.9 out = open(self.prefix+str(trial)+'_final.iq', 'w') self.nlm_array.load_coefs( self.nlm, self.best_nlm_coefs ) best_i = self.zm.calc_intensity_nlm( self.nlm_array ) best_i = best_i/best_i[0]*self.scale_2_expt for qq,ic,io in zip( self.data.q, best_i, self.data.i*self.scale_2_expt): print>>out, qq, ic, io out.close() print "total number of moves %d"%self.counter print "total number of accepted moves %d"%self.n_accept if (self.pdb_nlm is not None): align_obj = fft_align.align(self.pdb_nlm, self.nlm_array, nmax=self.nmax, refine=True) mean = abs( self.best_nlm_coefs[0] ) var = flex.sum( flex.norm( self.best_nlm_coefs ) ) sigma = smath.sqrt( var - mean*mean ) cc = align_obj.best_score cc = ( cc - mean*self.pdb_m ) / ( sigma*self.pdb_s ) print "C.C. (PDB, trial%6d) = %8.5f, Score = %8.5f"%(trial, cc, self.lowest_score) self.best_nlm_coefs = align_obj.moving_nlm.coefs() reconst_model = self.reconst_model( self.best_nlm_coefs ) xplor_map_type( reconst_model, self.np_on_grid, self.rmax, file_name=self.prefix+str(trial)+'_final_rbt.xplor') xplor_map_type( self.best_model, self.np_on_grid, self.rmax, file_name=self.prefix+str(trial)+'_final.xplor') print "-----------End of Trial %d--------------"%trial, time.ctime()
def enlarge(data,factor,sigma=0.1,full=False): n,n = data.focus() m = int(n*factor) x = flex.double() y = flex.double() vals = flex.double() sigma=sigma*factor new_data = flex.double( flex.grid(m,m), -9 ) visited = flex.bool( flex.grid(m,m), False ) oo = n/2.0 for ii in range(n): for jj in range(n): dd = smath.sqrt( (ii-oo)**2.0 + (jj-oo)**2.0 ) if dd <= oo: nx = ii*factor ny = jj*factor x.append( nx ) y.append( ny ) vals.append( data[ (ii,jj) ] ) new_data[ (int(nx), int(ny)) ] = data[ (ii,jj) ] if not full: visited[ (int(nx), int(ny)) ] = True not_visited = ~visited not_visited = not_visited.iselection() # now we need to loop over all non-visited pixel values for pixel in not_visited: nv = -9 index = n_dim_index_from_one_dim(pixel, [m,m] ) nvx = index[1] nvy = index[0] dx = x-nvx dy = y-nvy dd = (dx*dx+dy*dy) ss = flex.exp( -dd/(sigma*sigma) ) nv = flex.sum(ss*vals)/(1e-12+flex.sum(ss)) new_data[ (nvx,nvy) ] = nv visited[ (nvx,nvy) ] = True #print nvx, nvy, nv not_visited = ~visited not_visited = not_visited.iselection() #print not_visited.size() return new_data oo=m/2.0 for ii in range(m): for jj in range(m): dd = smath.sqrt( (ii-oo)**2.0 + (jj-oo)**2.0 ) new_data[ (ii,jj) ] = new_data[ (ii,jj) ]/(1+smath.sqrt(dd)) return new_data
def wR2(self, cutoff_factor=None): if cutoff_factor is None: return math.sqrt(2 * self.objective_data_only) fo_sq = self.observations.fo_sq strong = fo_sq.data() >= cutoff_factor * fo_sq.sigmas() fo_sq = fo_sq.select(strong) fc_sq = self.fc_sq.select(strong) wght = self.weights.select(strong) fc_sq = fc_sq.data() fo_sq = fo_sq.data() fc_sq *= self.scale_factor() wR2 = flex.sum(wght * flex.pow2( (fo_sq - fc_sq))) / flex.sum(wght * flex.pow2(fo_sq)) return math.sqrt(wR2)
def tst_dmatrix(): # expected values are the d_jmn at beta=1.0 and j=2, m=-2, -2<=n<=2 expect = [0.593133, 0.64806, 0.433605, 0.193411, 0.0528305] eps = 1e-4 d2 = dmatrix(2, 1.0) # dmatrix( max_L, beta ) for n in range(-2, 3): assert abs(expect[n + 2] - d2.djmn(2, -2, n)) < eps d4 = dmatrix(4, 1.0) for n in range(-2, 3): assert abs(expect[n + 2] - d4.djmn(2, -2, n)) < eps d7 = dmatrix(2, 1.0) for n in range(-2, 3): assert abs(expect[n + 2] - d7.djmn(2, -2, n)) < eps # check agains d(2,2,1) = -(1+cos(beta))*sin(beta)/2.0 for ii in range(10): expt = -(1.0 + smath.cos(ii)) * smath.sin(ii) / 2.0 assert abs(dmatrix(2, ii).djmn(2, 2, 1) - expt) < eps # check beta= multiple of pi/2 assert abs(dmatrix(20, smath.pi).djmn(2, 2, 0)) < eps assert abs(dmatrix(2, smath.pi * 2).djmn(2, 2, 0)) < eps assert abs( dmatrix(20, smath.pi * 0.5).djmn(2, 2, 0) - smath.sqrt(6.0) / 4.0) < eps
def generate_image(n,l, N=100): nmax = max(20,n) lfg = math.log_factorial_generator(nmax) #rzfa = math.zernike_2d_radial(n,l,lfg) #rap = math.zernike_2d_polynome(n,l,rzfa) rap = math.zernike_2d_polynome(n,l)#,rzfa) image = flex.vec3_double() original=open('original.dat','w') count = 0 for x in range(-N, N+1): for y in range(-N, N+1): rr = smath.sqrt(x*x+y*y)/N if rr>1.0: value=0.0 else: tt = smath.atan2(y,x) value = rap.f(rr,tt) value = value.real count = count + 1 image.append([x+N,y+N,value]) print>>original, x+N,y+N, value original.close() return image
def pair_align(self): ms = flex.double() ss = flex.double() tmp_nlm_array = math.nlm_array( self.nmax ) for coef in self.finals: mean = abs( coef[0] ) var = flex.sum( flex.norm( coef ) ) sigma = smath.sqrt( var - mean*mean ) ms.append( mean ) ss.append( sigma) grids = flex.grid(self.n_trial, self.n_trial) self.cc_array=flex.double( grids, 1.0 ) for ii in range( self.n_trial ): self.nlm_array.load_coefs( self.nlm, self.finals[ii] ) for jj in range( ii ): tmp_nlm_array.load_coefs( self.nlm, self.finals[jj] ) cc = fft_align.align( self.nlm_array, tmp_nlm_array, nmax=self.nmax, refine=True ).best_score cc = (cc-ms[ii]*ms[jj])/(ss[ii]*ss[jj]) self.cc_array[(ii,jj)]=cc self.cc_array[(jj,ii)]=cc outfile = self.prefix+"pair.cc" comment = "# electron density correlation coefficient, < rho_1(r)*rho_2(r) >" out=open(outfile, 'w') print>>out, comment for ii in range(1,self.n_trial+1): print>>out,"%6d"%ii, print>>out, " average" for ii in range(self.n_trial): for jj in range(self.n_trial): print>>out,"%6.3f"%self.cc_array[(ii,jj)], print>>out, flex.mean( self.cc_array[ii*self.n_trial:(ii+1)*self.n_trial] ) out.close()
def add(result, new): foud = False for s in result: d = math.sqrt((s[0]-new[0])**2+(s[1]-new[1])**2+(s[2]-new[2])**2) if(d<1.e-3): return result.append(new) return
def tst(): M=10 xy = [] for ii in range(M): r=10.0 phi = ii*(smath.pi*2/M) x = r*smath.cos(phi) y = r*smath.sin(phi) xy.append( flex.double([x,y]) ) # build distance matrix dmat = [] for ii in range(M): tmp = [] for jj in range(M): x1=xy[ii][0] x2=xy[jj][0] y1=xy[ii][1] y2=xy[jj][1] dd = smath.sqrt( (x1-x2)**2.0 +(y1-y2)**2.0 ) if jj != ii: tmp.append( (jj,dd) ) dmat.append( tmp ) spee=classic_spe_engine( dmat, l=1.0,max_cycle=5000 ) x,s = spee.embed(2,M) assert s<1e-4 print "OK"
def embed(self,n_dimensions,n_points): x = [] for ii in range(n_points): x.append( flex.random_double(n_dimensions)*100 ) l = float(self.l) for mm in range(self.max_cycle): atom_order = flex.sort_permutation( flex.random_double(len(x)) ) strain = 0.0 for ii in atom_order: n_contacts = len(self.dmat[ii]) jj_index = flex.sort_permutation( flex.random_double( n_contacts ) )[0] jj_info = self.dmat[ii][jj_index] jj = jj_info[0] td = jj_info[1] xi = x[ii] xj = x[jj] cd = smath.sqrt( flex.sum( (xi-xj)*(xi-xj) ) ) new_xi = xi + l*0.5*(td-cd)/(cd+self.eps)*(xi-xj) new_xj = xj + l*0.5*(td-cd)/(cd+self.eps)*(xj-xi) strain += abs(cd-td) x[ii] = new_xi x[jj] = new_xj l = l-self.dl return x,strain/len(x)
def tst(): M = 10 xy = [] for ii in range(M): r = 10.0 phi = ii * (smath.pi * 2 / M) x = r * smath.cos(phi) y = r * smath.sin(phi) xy.append(flex.double([x, y])) # build distance matrix dmat = [] for ii in range(M): tmp = [] for jj in range(M): x1 = xy[ii][0] x2 = xy[jj][0] y1 = xy[ii][1] y2 = xy[jj][1] dd = smath.sqrt((x1 - x2)**2.0 + (y1 - y2)**2.0) if jj != ii: tmp.append((jj, dd)) dmat.append(tmp) spee = classic_spe_engine(dmat, l=1.0, max_cycle=5000) x, s = spee.embed(2, M) assert s < 1e-4 print "OK"
def embed(self, n_dimensions, n_points): x = [] for ii in range(n_points): x.append(flex.random_double(n_dimensions) * 100) l = float(self.l) for mm in range(self.max_cycle): atom_order = flex.sort_permutation(flex.random_double(len(x))) strain = 0.0 for ii in atom_order: n_contacts = len(self.dmat[ii]) jj_index = flex.sort_permutation( flex.random_double(n_contacts))[0] jj_info = self.dmat[ii][jj_index] jj = jj_info[0] td = jj_info[1] xi = x[ii] xj = x[jj] cd = smath.sqrt(flex.sum((xi - xj) * (xi - xj))) new_xi = xi + l * 0.5 * (td - cd) / (cd + self.eps) * (xi - xj) new_xj = xj + l * 0.5 * (td - cd) / (cd + self.eps) * (xj - xi) strain += abs(cd - td) x[ii] = new_xi x[jj] = new_xj l = l - self.dl return x, strain / len(x)
def kabsch_rotation(reference_sites, other_sites): """ Kabsch, W. (1976). Acta Cryst. A32, 922-923. A solution for the best rotation to relate two sets of vectors Based on a prototype by Erik McKee and Reetal K. Pai. This implementation does not handle degenerate situations correctly (e.g. if all atoms are on a line or plane) and should therefore not be used in applications. It is retained here for development purposes only. """ assert reference_sites.size() == other_sites.size() sts = matrix.sqr(other_sites.transpose_multiply(reference_sites)) eigs = eigensystem.real_symmetric((sts * sts.transpose()).as_sym_mat3()) vals = list(eigs.values()) vecs = list(eigs.vectors()) a3 = list(matrix.col(vecs[:3]).cross(matrix.col(vecs[3:6]))) a = matrix.sqr(list(vecs[:6])+a3) b = list(a * sts) for i in xrange(3): d = math.sqrt(math.fabs(vals[i])) if (d > 0): for j in xrange(3): b[i*3+j] /= d b3 = list(matrix.col(b[:3]).cross(matrix.col(b[3:6]))) b = matrix.sqr(b[:6]+b3) return b.transpose() * a
def build_grid(Nq, Nphi, nls, moments): two_pi = smath.pi * 2.0 dq = 1 dphi = two_pi / Nphi qps = flex.vec2_double() xys = flex.vec2_double() image = flex.vec3_double() for xx in range(0, Nq + 1): for yy in range(0, Nq + 1): r = smath.sqrt(xx * xx + yy * yy) / Nq if (r <= 1): phi = smath.atan2(yy, xx) if (phi < 0): phi = phi + two_pi qps.append([r, phi]) xys.append([xx, yy]) c2 = compute_c2(nls, moments, r, phi) else: c2 = 0 image.append([Nq + xx, Nq + yy, c2]) if (xx > 0 and yy > 0): image.append([Nq - xx, Nq - yy, c2]) if (yy > 0): image.append([Nq + xx, Nq - yy, c2]) if (xx > 0): image.append([Nq - xx, Nq + yy, c2]) return image
def tst_dmatrix(): # expected values are the d_jmn at beta=1.0 and j=2, m=-2, -2<=n<=2 expect = [0.593133, 0.64806, 0.433605, 0.193411, 0.0528305] eps = 1e-4 d2 = dmatrix( 2, 1.0) # dmatrix( max_L, beta ) for n in range(-2, 3): assert abs(expect[n+2] - d2.djmn(2, -2, n) ) < eps d4 = dmatrix( 4, 1.0) for n in range(-2, 3): assert abs(expect[n+2] - d4.djmn(2, -2, n) ) < eps d7 = dmatrix( 2, 1.0) for n in range(-2, 3): assert abs(expect[n+2] - d7.djmn(2, -2, n) ) < eps # check agains d(2,2,1) = -(1+cos(beta))*sin(beta)/2.0 for ii in range(10): expt = -(1.0+smath.cos(ii))*smath.sin(ii)/2.0 assert abs( dmatrix(2,ii).djmn(2,2,1) - expt ) < eps # check beta= multiple of pi/2 assert abs( dmatrix( 20, smath.pi ).djmn(2,2,0) )< eps assert abs( dmatrix( 2, smath.pi*2 ).djmn(2,2,0) )< eps assert abs( dmatrix( 20, smath.pi*0.5 ).djmn(2,2,0)-smath.sqrt(6.0)/4.0 )< eps
def compute_functional_and_gradients(self): self.apply_shifts() self.compute_target(compute_gradients = True) if(self.verbose > 1): print "xray.minimization line search: f,rms(g):", print self.f, math.sqrt(flex.mean_sq(self.g)) return self.f, self.g
def angle_deviations_z(self): ''' Calculate rmsz of angles deviations Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) x_i: atcual bond angle mu: geometry restraints mean sigma: geometry restraints standard deviation z_i: z-score for bond i z: array of z_i The sigma and the (x_i - mu) are model constrains, geometry restraints. They function extracts from self, not calculated from data. :returns: a_rmsz: rmsz, root mean square of the z-scors of all angles a_z_min/max: min/max values of z-scors ''' if(self.n_angle_proxies is not None): angle_deltas = self.angle_proxies.proxy_select(origin_id=0).deltas( sites_cart=self.sites_cart) if len(angle_deltas) > 0: sigmas = [geometry_restraints.weight_as_sigma(x.weight) for x in self.angle_proxies] z_scores = flex.double([(angle_delta/sigma) for angle_delta,sigma in zip(angle_deltas,sigmas)]) a_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) a_z_max = flex.max_default(flex.abs(z_scores), 0) a_z_min = flex.min_default(flex.abs(z_scores), 0) return a_z_min, a_z_max, a_rmsz else: return 0,0,0
def build_images(self): for ii in range(self.np): for jj in range(self.np): rr = smath.sqrt( (ii+0.5-self.ox)**2.0 + (jj+0.5-self.oy)**2.0 ) self.r_image[ (ii,jj) ] = rr tt = smath.atan2( jj+0.5-self.oy,ii+0.5-self.ox ) self.t_image[ (ii,jj) ] = tt
def bond_deviations_z(self): ''' Calculate rmsz of bond deviations Compute rmsz, the Root-Mean-Square of the z-scors for a set of data using z_i = {x_i - mu / sigma} and rmsz = sqrt(mean(z*z)) x_i: atcual bond length mu: geometry restraints mean sigma: geometry restraints standard deviation z_i: z-score for bond i z: array of z_i The sigma and the (x_i - mu) are model constrains, geometry restraints. They function extracts from self, not calculated from data. :returns: b_rmsz: rmsz, root mean square of the z-scors of all bonds b_z_min/max: min/max abolute values of z-scors ''' if(self.n_bond_proxies is not None): bond_deltas = self.bond_proxies.deltas( sites_cart=self.sites_cart, origin_id=0) if len(bond_deltas) >0: sigmas = [geometry_restraints.weight_as_sigma(x.weight) for x in self.bond_proxies.simple] z_scores = flex.double([(bond_delta/sigma) for bond_delta,sigma in zip(bond_deltas,sigmas)]) b_rmsz = math.sqrt(flex.mean_default(z_scores*z_scores,0)) b_z_max = flex.max_default(flex.abs(z_scores), 0) b_z_min = flex.min_default(flex.abs(z_scores), 0) return b_z_min, b_z_max, b_rmsz else: return 0,0,0
def compute_functional_and_gradients(self): u_iso_refinable_params = self.apply_shifts() self.compute_target(compute_gradients = True, u_iso_refinable_params = u_iso_refinable_params) if (self.verbose > 1): print "xray.minimization line search: f,rms(g):", print self.f, math.sqrt(flex.mean_sq(self.g)) return self.f, self.g
def get_mean_sigma(nlm_array): t1 = time.time() coef = nlm_array.coefs() mean = abs(coef[0]) var = flex.sum(flex.norm(coef)) sigma = smath.sqrt(var - mean * mean) t2 = time.time() print("get_mean_sigma time used:", t2 - t1) return mean, sigma
def __init__(self, fixed, moving): self.fixed = fixed self.moving = moving self.nsde = nsd_engine(self.fixed) self.d_fixed = smath.sqrt(self.nsde.d_fixed) self.d_moving = smath.sqrt(self.nsde.get_mean_distance( self.moving ) ) self.m_com = self.moving.mean() self.f_com = self.fixed.mean() self.n_mov = self.moving-self.m_com self.d = (self.d_fixed+self.d_moving)/12 self.n = 6 pi = smath.pi self.domain = [ (-pi,pi),(-pi,pi), (-pi,pi), (-self.d,self.d),(-self.d,self.d), (-self.d,self.d) ] self.x = None self.optimizer = de.differential_evolution_optimizer(self, population_size=12,f=0.85,cr=0.95, n_cross=2,eps=1e-2, show_progress=False,show_progress_nth_cycle=20)
def r1_factor(self, cutoff_factor=None): fo_sq = self.observations.fo_sq if cutoff_factor is not None: strong = fo_sq.data() >= cutoff_factor*fo_sq.sigmas() fo_sq = fo_sq.select(strong) fc_sq = self.fc_sq.select(strong) else: fc_sq = self.fc_sq f_obs = fo_sq.f_sq_as_f() f_calc = fc_sq.f_sq_as_f() R1 = f_obs.r1_factor(f_calc, scale_factor=math.sqrt(self.scale_factor()), assume_index_matching=True) return R1, f_obs.size()
def tst_2d_poly(n,l): nmax=max(n,20) np=50 x,y=0.1,0.9 r,t=smath.sqrt(x*x+y*y),smath.atan2(y,x) lfg = math.log_factorial_generator(nmax) #rzfa = math.zernike_2d_radial(n,l,lfg) #rap = math.zernike_2d_polynome(n,l,rzfa) rap = math.zernike_2d_polynome(n,l)#,rzfa) rt_value=rap.f(r,t) grid = math.two_d_grid(np, nmax) zm2d = math.two_d_zernike_moments(grid, nmax) xy_value=zm2d.zernike_poly(n,l,x,y) print rt_value, xy_value, abs(rt_value), abs(xy_value)
def equally_spaced_points_on_vector(start, end, n=None, step=None): assert [n, step].count(None) == 1 vec = [end[0]-start[0],end[1]-start[1],end[2]-start[2]] r = flex.vec3_double([vec]) if(n is not None): assert n > 0 else: assert step > 0 vec_length = math.sqrt(vec[0]**2+vec[1]**2+vec[2]**2) n = int(vec_length/step)-1 dr = r*(1/float(n+1)) points = flex.vec3_double() for i in xrange(n+1): points.extend(dr * i + start) points.append(end) return points
def tst_2d_zm(n,l): nmax=max(n,20) np=100 points=flex.double(range(-np,np+1))/np grid = math.two_d_grid(np, nmax) zm2d = math.two_d_zernike_moments(grid, nmax) image = flex.vec3_double() output=file('testmap.dat','w') for x in points: for y in points: r=smath.sqrt(x*x+y*y) if(r>1.0): value=0.0 else: value=zm2d.zernike_poly(n,l,x,y).real image.append([x*np+np,y*np+np, value]) grid.clean_space( image ) grid.construct_space_sum() zernike_2d_mom = math.two_d_zernike_moments( grid, nmax ) moments = zernike_2d_mom.moments() coefs = flex.real( moments ) nl_array = math.nl_array( nmax ) nls = nl_array.nl() for nl, c in zip( nls, moments): if(abs(c)<1e-3): c=0 print nl, c NP=np*2+1 reconst=zernike_2d_mom.zernike_map(nmax, np) i = 0 for x in range(0,NP): for y in range(0,NP): value=reconst[i].real if(value>0): print>>output, x,y,image[i][2],value i=i+1 output.close()
def build_edge_list(sites_cart, elements, slop=0.2): result = [] for ii in range(len(sites_cart)): x1, y1, z1 = sites_cart[ii] for jj in range(ii + 1, len(sites_cart)): x2, y2, z2 = sites_cart[jj] x2 = x2 - x1 y2 = y2 - y1 z2 = z2 - z1 dd = smath.sqrt(x2 * x2 + y2 * y2 + z2 * z2) expected_dist = expected_bond_lengths_by_element_pair.get((elements[ii], elements[jj]), False) if not expected_dist: expected_dist = expected_bond_lengths_by_element_pair.get((elements[jj], elements[ii]), False) if not expected_dist: expected_dist = 1.7 if dd <= expected_dist + slop: result.append((ii, jj)) return result
def __init__(self, points, epsilon=None, radius_if_one_or_no_points=1, center_if_no_points=(0,0,0)): assert len(points) > 0 or radius_if_one_or_no_points >= 0 if (epsilon is None): epsilon = 1.e-6 self._n_iterations = 0 if (len(points) == 0): self._center = center_if_no_points self._radius = radius_if_one_or_no_points return if (len(points) == 1): self._center = tuple(points[0]) self._radius = radius_if_one_or_no_points return n_dim = len(points[0].elems) w = 1./len(points) weights = matrix.row([w for i in xrange(len(points))]) while 1: x = matrix.col([0]*n_dim) for w,t in zip(weights.elems,points): x += w * t radii = matrix.col([abs(x-t) for t in points]) sigma = 0 for w,r in zip(weights.elems,radii.elems): sigma += w * r**2 sigma = math.sqrt(sigma) tau = radii.max() if (tau - sigma < tau * epsilon): break w_r = [] for w,r in zip(weights.elems,radii.elems): w_r.append(w * r) w_r = matrix.col(w_r) sum_w_r = w_r.sum() assert sum_w_r != 0 weights = w_r / sum_w_r self._n_iterations += 1 self._center = x.elems self._radius = tau
def nsd(self,moving,d_moving=None): if self.d_moving is None: self.d_moving = self.get_mean_distance(moving) if d_moving is not None: self.d_moving = d_moving # loop over all sites in fixed, find the minimum for each site tot_rho_mf = 0 tot_rho_fm = 0 for site in moving: dd = self.fixed-site dd = flex.min( dd.norms() ) tot_rho_mf+=dd*dd for site in self.fixed: dd = moving-site dd = flex.min( dd.norms() ) tot_rho_fm+=dd tot_rho_fm = tot_rho_fm / (self.fixed.size()*self.d_fixed ) tot_rho_mf = tot_rho_mf / (moving.size()*self.d_moving ) result = smath.sqrt((tot_rho_fm+tot_rho_mf)/2.0) return result