def __init__(self, data, nmax=20, rmax=None, scan=True, fraction=0.9, smear=True, prefix=None, weight='i', delta_q=None): self.data = data self.smear = smear self.delta_q = delta_q self.setup_weighting_scheme(weight) if (self.smear): self.set_up_smear() self.int_scale = flex.max(self.data.i) self.nmax = nmax if (rmax is None): rmax = int(get_rg(data) * 3.0 / 2.0) self.rmax = rmax self.scan = scan self.prefix = prefix self.fraction = fraction self.nlm_array = math.nlm_array(nmax) self.nlm_total = self.nlm_array.coefs().size() self.nn_array = math.nl_array(nmax) self.nn = self.nn_array.nl() self.nn_total = self.nn_array.coefs().size()
def __init__(self, nmax, Cnm, coef_table,m): self.nmax=nmax self.Cnm = Cnm self.coef_table = coef_table self.Inm = math.nl_array(nmax) self.m=m ## testing the most populated cases self.coef_table_m = self.coef_table[self.m] self.x = None self.ndim = (self.nmax-self.m)/2+1 self.n = self.ndim self.domain =[(-1.0,1.0)]*self.ndim self.target_data=flex.double() self.n_list=range(self.m,nmax+1,2) self.n_indx_list=range(self.ndim) for nn in self.n_list: self.target_data.append( self.Cnm.get_coef(nn,self.m) ) # self.solution=flex.random_double(self.n)*2-1 # self.target_data=self.calc_Cnm_from_Inm(self.solution) self.scale = self.target_data.norm()**2 # self.optimizer = de.differential_evolution_optimizer(self, population_size=self.ndim,eps=1e-8,n_cross=self.n/2) # self.run_simplex() # self.run_dssa() lowest_score=1e8 for ii in range(10): self.run_lbfgs() score=self.target(self.x) if(score < lowest_score): lowest_score=score self.best_solution=self.x.deep_copy() print list(self.x), score print lowest_score
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 tst_solving_Inm(nmax): Inm=math.nl_array(nmax) nls = Inm.nl() size = nls.size() coefs = flex.random_double(size) Inm.load_coefs( nls, coefs ) coef_table = tst_integral_triple_zernike2d(nmax) Cnm = calc_Cnm_from_Inm( Inm, coef_table, nmax ) new_Inm=math.nl_array(nmax) for n in range( nmax+1 ): for m in range(n,-1,-2): Cnm_value = Cnm.get_coef( n, m ) coef = coef_table[m].get_coef(n,n,n).real # value = smath.sqrt( Cnm_value/coef ) if(coef != 0): value = ( Cnm_value/coef ) print n,m,Inm.get_coef(n,m)**2, value
def get_nn_array_for_code(code, nns, codes, rmaxs, nmax=10): this_one = codes.index(code) coefs = nns[this_one] nn_array = math.nl_array(nmax) nn = nn_array.nl() nn_size = nn.size() coefs = coefs[0:nn_size] nn_array.load_coefs(nn, coefs) print code, rmaxs[this_one] for c in coefs: print c return nn_array
def calc_Cnm_from_Inm( Inm, coef_table, nmax ): Cnm=math.nl_array(nmax) for n in range( 0,nmax+1,2 ): # only even number (n,m) for m in range(0,n+1,2 ): temp = 0 for n1 in range( m,nmax+1,2 ): for n2 in range( m,n1,2 ): i,j,k=sorted([n,n1,n2],reverse=True) temp = temp + coef_table[m].get_coef(i,j,k).real*Inm.get_coef(n1,m)*Inm.get_coef(n2,m) i,j,k=sorted([n,n1,n1],reverse=True) temp = temp + coef_table[m].get_coef(i,j,k).real*Inm.get_coef(n1,m)**2.0/2.0 Cnm.set_coef(n,m,temp*2.0) return Cnm
def generate_image(n, moments, N=100): # Generate images from the zernike moments, N is the number of points from 0 to 1 nmax = n image = flex.vec3_double() NP = 2 * N + 1 reconst = flex.complex_double(NP**2, 0) nl_array = math.nl_array(nmax) nls = nl_array.nl() r_theta = flex.vec2_double() 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: tt = 0.0 else: tt = smath.atan2(y, x) r_theta.append([rr, tt]) for nl, c in zip(nls, moments): n = nl[0] l = nl[1] #if(l/2*2 != l): continue if (l > 0): c = c * 2.0 rap = math.zernike_2d_polynome(n, l) i = 0 for pt in r_theta: rr = pt[0] if rr > 1.0: value = 0.0 else: tt = pt[1] value = rap.f(rr, tt) reconst[i] = reconst[i] + value * c i = i + 1 return reconst i = 0 for x in range(0, NP): for y in range(0, NP): value = reconst[i].real image.append([x, y, value]) i = i + 1 return image
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 calc_Cnm_from_Inm(Inm, coef_table, nmax): two_pi = smath.pi * 2.0 four_pi = 2.0 * two_pi Cnm = math.nl_array(nmax) for n in range(2, nmax + 1, 2): # only even number (n,m) for m in range(2, n + 1, 2): # when m=0, the In0 is subtracted temp = 0 for n1 in range(m, nmax + 1, 2): for n2 in range(m, nmax + 1, 2): i, j, k = sorted([n, n1, n2], reverse=True) temp = temp + coef_table[m].get_coef( i, j, k) * (Inm.get_coef(n1, m).conjugate() * Inm.get_coef(n2, m)).real # i,j,k=sorted([n,n1,n1],reverse=True) # temp = temp + coef_table[m].get_coef(i,j,k)*abs(Inm.get_coef(n1,m))**2.0/2.0 Cnm.set_coef(n, m, temp * four_pi * (n + 1)) return Cnm
def generate_image2(n, moments, template_image, np): nmax = n image = flex.vec3_double() np_tot = template_image.size() reconst = flex.complex_double(np_tot, 0) nl_array = math.nl_array(nmax) nls = nl_array.nl() r_theta = flex.vec2_double() for pt in template_image: x = pt[0] - np y = pt[1] - np rr = smath.sqrt(x * x + y * y) / np if rr > 1.0: tt = 0.0 else: tt = smath.atan2(y, x) r_theta.append([rr, tt]) for nl, c in zip(nls, moments): n = nl[0] l = nl[1] #if(l/2*2 != l): continue if (l > 0): c = c * 2.0 rap = math.zernike_2d_polynome(n, l) i = 0 for pt in r_theta: rr = pt[0] if rr > 1.0: value = 0.0 else: tt = pt[1] value = rap.f(rr, tt) reconst[i] = reconst[i] + value * c i = i + 1 for i in range(np_tot): x = template_image[i][0] y = template_image[i][1] value = reconst[i].real image.append([x, y, value]) return image
def test_solving(nmax, m): Inm=math.nl_array(nmax) nls = Inm.nl() size = nls.size() coefs = flex.random_double(size) Inm.load_coefs( nls, coefs ) coef_table = tst_integral_triple_zernike2d(nmax) Cnm = calc_Cnm_from_Inm( Inm, coef_table, nmax ) t1 = time.time() refine_de_obj = inm_refine( nmax, Cnm, coef_table, m ) solution=flex.double() for nn in range(m,nmax+1,2): solution.append( Inm.get_coef(nn,m) ) for ss,xx in zip(solution, refine_de_obj.best_solution): print ss,xx print "score=",refine_de_obj.target(refine_de_obj.x) print "score=",refine_de_obj.target(solution) t2 = time.time() print "nmax=",nmax, "time used:", t2-t1
def comp_Cnm_calculations(nmax): Inm=math.nl_array(nmax) nls = Inm.nl() size = nls.size() coefs = flex.random_double(size) Inm.load_coefs( nls, coefs ) coef_table = tst_integral_triple_zernike2d(nmax) Cnm = calc_Cnm_from_Inm(Inm, coef_table, nmax ) for mm in range(0,nmax+1,2): coef_table_m = coef_table[mm] Inm_m=flex.double() Cnm_m=flex.double() for nn in range(mm,nmax+1,2): Inm_m.append( Inm.get_coef(nn,mm) ) Cnm_m.append( Cnm.get_coef(nn,mm) ) Cnm_m_new = calc_Cnm_4m_from_Inm(mm,nmax,Inm_m,coef_table_m) for ii,jj in zip(Cnm_m, Cnm_m_new): assert(abs(ii-jj)<1e-6)
def __init__(self, data, rg, io, nmax=20, rmax=None, scan=True, fraction=0.9, smear=True, prefix=None, weight='i', delta_q=None, scale_power=2.0): global stdfile global outfilelog self.stdfile = stdfile self.outfilelog = outfilelog self.data = data self.smear = smear self.delta_q = delta_q self.setup_weighting_scheme(weight) if (self.smear): self.set_up_smear() self.rg = rg self.io = io self.int_scale = self.io self.nmax = nmax if (rmax is None): rmax = int(self.rg * 3.0 / 2.0) self.rmax = rmax self.scan = scan self.prefix = prefix self.fraction = fraction self.nlm_array = math.nlm_array(nmax) self.nlm_total = self.nlm_array.coefs().size() self.nn_array = math.nl_array(nmax) self.nn = self.nn_array.nl() self.nn_total = self.nn_array.coefs().size() self.scale_power = scale_power
def tst_2d_zernike_mom(n, l, file): # image = preprocess_image(h5file) image = ImageToDat(file) feature_maxtix = [] 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) for nl, c in zip( nls, moments): if(abs(c)<1e-3): c=0 feature_maxtix.append(c.real) # print nl,c return feature_maxtix
def tst_nl(): this_nl_array = math.nl_array(5) nl = this_nl_array.nl() result= """""" for tnl in nl: result +=str(tnl)+"\n" assert (tnl[0]-tnl[1])%2==0 expected_result = """(0, 0) (1, 1) (2, 0) (2, 2) (3, 1) (3, 3) (4, 0) (4, 2) (4, 4) (5, 1) (5, 3) (5, 5) """ assert result == expected_result coefs = this_nl_array.coefs() assert coefs.size() == nl.size() new_coefs = flex.double( range(nl.size() ) ) assert this_nl_array.load_coefs( nl, new_coefs ) new_nl = shared.tiny_int_2( [ (10,10) ] ) new_coef = flex.double( [10.0] ) assert not this_nl_array.load_coefs( new_nl, new_coef ) coefs = this_nl_array.coefs() for x,y in zip(coefs,new_coefs): assert abs(x-y)<1e-5 for tnl, cc in zip(nl,coefs): thc = this_nl_array.get_coef(tnl[0], tnl[1]) assert abs(thc-cc)<1e-5 this_nl_array.set_coef(tnl[0], tnl[1], 300 ) thc = this_nl_array.get_coef(tnl[0], tnl[1]) assert abs(thc-300)<1e-5
def tst_line(nmax=20, width=2, N=201): nl_array = math.nl_array(nmax) nls = nl_array.nl() moments = flex.random_double(nls.size()) * 0 nl_array.load_coefs(nls, moments) nl_array.set_coef(8, 6, 1.0) nl_array.set_coef(6, 2, 1.0) nl_array.set_coef(4, 4, 1.0) moments = nl_array.coefs() this_nls = [[8, 6], [6, 2], [4, 4]] # this_nls = [[6,2]] this_moments = [1.0, 1.0, 1.0] N = N half_N = N / 2 Nq = half_N Nphi = Nq ## The following two methods should give consistent results ## #c2_image = build_grid(Nq, Nphi, this_nls, this_moments ) c2_image = analytical_c2(Nq, Nphi, this_nls, this_moments) c2_output = open('c2_raw.dat', 'w') for pt in c2_image: print >> c2_output, pt[0], pt[1], pt[2] c2_output.close() #sp_out = open('sp_image.dat', 'w') #for pt in raw_image: # print>>sp_out, pt[0], pt[1], pt[2] #sp_out.close() ## get cnm from sp_moments ## sp_moments = flex.complex_double(moments, moments * 0) #sp_moments[0]=0 nmax4c2 = nmax * 2 Inm = math.nl_c_array(nmax4c2) coef_table = image_tools.calc_integral_triple_zernike2d(nmax4c2) Inm.load_coefs(nls, sp_moments) cnm = image_tools.calc_Cnm_from_Inm(Inm, coef_table, nmax4c2) cnm_coef = cnm.coefs() c2_reconst = image_tools.generate_image2(nmax4c2, cnm_coef, c2_image, half_N) c2_output = open('c2_reconst.dat', 'w') for pt in c2_reconst: print >> c2_output, pt[0], pt[1], pt[2] c2_output.close() ## get zm for sp_image ## image = c2_image NP = int(smath.sqrt(image.size())) N = NP / 2 sp_n = 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) sp_moments = zernike_2d_mom.moments() print "#C2" for nl, m1, m2 in zip(nls, cnm_coef, sp_moments): print nl, m1, m2.real, m1 / (m2.real + 1e-23)
def find_relatives(ids, cc_min, cc_max, rmax, codes, moments, nmax=10): indices = flex.int() idlist = open('id_list.txt', 'r') for id in idlist: id = id[0:4] indices.append(flex.first_index(codes, id)) r_max = easy_pickle.load(prefix + 'pisa.rmax') nns = easy_pickle.load(prefix + 'pisa.nn') nn_array = math.nl_array(nmax) nn_indx = nn_array.nl() nn_total = nn_indx.size() q_array = flex.double(range(501)) / 2000.0 ref_nlm_array = math.nlm_array(nmax) target_nlm_array = math.nlm_array(nmax) nlm = ref_nlm_array.nlm() coef_size = nlm.size() all_indices = range(codes.size()) small_q_array = flex.double(range(51)) / 300.0 mean = [] sig = [] for indx in indices: print indx #rmax = 50.0 #r_max[indx] ref_coef = moments[indx] ref_nlm_array.load_coefs(nlm, ref_coef[0:coef_size]) z_model = zernike_model(ref_nlm_array, q_array, rmax, nmax) out_name = codes[indx] + "_.qi" nn_array.load_coefs(nn_indx, nns[indx][0:nn_total]) ref_int = put_intensity(z_model, q_array, nn_array, out_name) mean_r = ref_int * 0.0 sig_r = ref_int * 0.0 small_z_model = zernike_model(ref_nlm_array, small_q_array, rmax, nmax) small_ref_int = small_z_model.calc_intensity(nn_array) small_ref_int = small_ref_int / small_ref_int[0] N = 0.0 for coef, ii in zip(moments, all_indices): if N > 25: break target_nlm_array.load_coefs(nlm, coef[0:coef_size]) align_obj = fft_align.align(ref_nlm_array, target_nlm_array, nmax=nmax, topn=10, refine=False) cc = align_obj.get_cc() if (cc >= cc_min and cc <= cc_max): N += 1 nn_array.load_coefs(nn_indx, nns[ii][0:nn_total]) opt_r_obj = optimize_r(nn_array, small_ref_int, small_q_array, nmax) opt_r = gss(opt_r_obj.target, rmax * 0.8, rmax * 1.2) z_model = zernike_model(ref_nlm_array, q_array, opt_r, nmax) out_name = codes[indx] + "_" + codes[ii] + ".qi.rel" mod_int = put_intensity(z_model, q_array, nn_array, out_name, ref_int) out_name = codes[indx] + "_" + codes[ii] + ".qi" put_intensity(z_model, q_array, nn_array, out_name) mod_int = mod_int - 1.0 mean_r += mod_int sig_r += mod_int * mod_int print ii, cc, codes[ii], opt_r if N > 3: mean_r /= N sig_r = sig_r / N - mean_r * mean_r mean.append(mean_r) sig.append(sig_r) N = len(mean) if N > 0: mean_r = mean[0] * 0.0 s_r = mean[0] * 0.0 for uu in range(N): mean_r += mean[uu] s_r += sig[uu] mean_r /= N s_r /= N s_r = flex.sqrt(s_r) f = open('q_m_s_%s.dat' % rmax, 'w') for q, m, s in zip(q_array, mean_r, s_r): print >> f, q, m, s
def tst_2d_zernike_mom(n,l, N=100, filename=None): nmax = max(20,n) rebuilt=open('rebuilt.dat','w') tt1=time.time() if(filename is not None): image=read_data(filename) else: image=generate_image(n,l) NP=int(smath.sqrt( image.size() )) N=NP/2 grid_2d = math.two_d_grid(N, nmax) grid_2d.clean_space( image ) grid_2d.construct_space_sum() tt2=time.time() print "time used: ", tt2-tt1 zernike_2d_mom = math.two_d_zernike_moments( grid_2d, nmax ) moments = zernike_2d_mom.moments() tt2=time.time() print "time used: ", tt2-tt1 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 print 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 if(value>0): print>>rebuilt, x,y,image[i][2],value i=i+1 rebuilt.close()
def tst_fft_proj(pdbfile, nmax=20): dx = 1.0 projection_obj = projection.projection(pdbfile, dx=dx, fraction=0.5) dq = projection_obj.get_dq() image = projection_obj.project() output = open('prj1.dat', 'w') for pt in image: print >> output, pt[0], pt[1], pt[2] output.close() values = projection_obj.get_value() np = projection_obj.get_np() flex_grid = flex.grid(np, np) fft_input = flex.complex_double(flex_grid) for ii in range(fft_input.size()): fft_input[ii] = complex(values[ii], 0) fft_obj = fftpack.complex_to_complex_2d((np, np)) result = fft_obj.forward(fft_input) sp_image = flex.vec3_double() for ii in range(np): for jj in range(np): kk = ii ll = jj if kk > np / 2: kk = kk - np if ll > np / 2: ll = ll - np sp_image.append( [kk + np / 2, ll + np / 2, abs(result[(ii, jj)])**2.0]) Nq = np / 2 Nphi = Nq c2_image, normalized_sp = from_I_to_C2(sp_image, Nq, Nphi, N=np, further_reduct=True) image = normalized_sp NP = int(smath.sqrt(image.size())) N = NP / 2 sp_n = 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) sp_moments = zernike_2d_mom.moments() # sp_reconst = image_tools.generate_image2(nmax, sp_moments, normalized_sp, sp_n) # c2_image, normalized_sp = from_I_to_C2(sp_reconst, Nq, Nphi, N=np) sp_out = open('sp_image.dat', 'w') for pt in sp_image: print >> sp_out, (pt[0] - sp_n) * dq, (pt[1] - sp_n) * dq, pt[2] sp_out.close() c2_output = open('c2.dat', 'w') for pt in c2_image: print >> c2_output, pt[0], pt[1], pt[2] c2_output.close() image = c2_image NP = int(smath.sqrt(image.size())) N = NP / 2 c2_n = 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 ) # c2_moments = zernike_2d_mom.moments() # coefs = ( c2_moments ) nls = math.nl_array(nmax).nl() # for nl, c2m in zip( nls, coefs ): # print nl, c2m sp_moments[0] = 0 nmax4c2 = 2 * nmax Inm = math.nl_c_array(nmax4c2) coef_table = image_tools.calc_integral_triple_zernike2d(nmax4c2) Inm.load_coefs(nls, sp_moments) cnm = image_tools.calc_Cnm_from_Inm(Inm, coef_table, nmax4c2) cnm_coef = cnm.coefs() # for nl, mm,mmm in zip( nls, c2_moments, cnm_coef ): # print abs(mm), abs(mmm) #cnm_coef = c2_moments c2_reconst = image_tools.generate_image2(nmax, cnm_coef, c2_image, c2_n) c2_r = open('c2r.dat', 'w') for pt in c2_reconst: print >> c2_r, pt[0], pt[1], pt[2] c2_r.close() exit()
def tst_image(n, N=100, filename=None): nmax = n nmax0 = 8 nl_array0 = math.nl_array(nmax0) nls0 = nl_array0.nl() nl_array = math.nl_array(nmax) nls = nl_array.nl() if (filename is not None): image = read_data(filename) else: moments = flex.random_double(nls0.size()) # moments=flex.double(nls.size(),0 ) # moments[3] = 1.0 # moments[7] = 1.0 image = generate_image(n, moments) orig = open('original.dat', 'w') for pt in image: print >> orig, pt[0], pt[1], pt[2] orig.close() Nq = 100 Nphi = 100 c2_image = from_I_to_C2(nmax, image, Nq, Nphi) c2_output = open('c2.dat', 'w') for pt in c2_image: print >> c2_output, pt[0], pt[1], pt[2] c2_output.close() coef_table = calc_integral_triple_zernike2d(nmax) moments[0] = 0 new_mom = moments.concatenate(flex.double(nls.size() - nls0.size(), 0)) nmax4c2 = 2 * nmax Inm = math.nl_c_array(nmax4c2) Inm.load_coefs(nls, new_mom) cnm = calc_Cnm_from_Inm(Inm, coef_table, nmax4c2) cnm_coef = cnm.coefs() print "#cnm[0]", cnm_coef[0] # cnm_coef[0]=0 ### calculate 2d zernike moments for C2_image ### image = c2_image NP = int(smath.sqrt(image.size())) N = NP / 2 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) c2_moments = zernike_2d_mom.moments() #coefs = flex.real( c2_moments ) #cnm_coef = flex.real( c2_moments ) cnm_coef = c2_moments c2_reconst = generate_image2(n, cnm_coef, c2_image, Nq) c2_r = open('c2r.dat', 'w') for pt in c2_reconst: print >> c2_r, pt[0], pt[1], pt[2] c2_r.close() ls_score = 0 np_tot = c2_image.size() for p1, p2 in zip(c2_image, c2_reconst): ls_score += (p1[2] - p2[2])**2.0 print nmax, nls.size(), ls_score, np_tot * smath.log( ls_score / np_tot), "SUM" for nl, c2m in zip(nls, cnm_coef): print nl, c2m exit() # ### calculate 2d zernike moments for C2_image ### image = c2_image NP = int(smath.sqrt(image.size())) N = NP / 2 grid_2d = math.two_d_grid(N, nmax) grid_2d.clean_space(image) grid_2d.construct_space_sum() tt2 = time.time() zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax) c2_moments = zernike_2d_mom.moments() #coefs = flex.real( c2_moments ) coefs = (c2_moments) for nl, c2m, c2mm in zip(nls, cnm_coef, coefs): if (nl[0] / 2 * 2 == nl[0]): print c2m, c2mm coefs = flex.real(moments) nl_array.load_coefs(nls, coefs) for nl, c in zip(nls, moments): if (abs(c) < 1e-3): c = 0 print nl, c print 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.0 rap = math.zernike_2d_polynome(n, l) 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 rebuilt = open('rebuilt.dat', 'w') i = 0 for x in range(0, NP): for y in range(0, NP): value = reconst[i].real print >> rebuilt, x, y, image[i][2], value i = i + 1 rebuilt.close()