def filter_pairs( h1h2, c2a, B, BI, tol = 1e-5): order = np.argsort( c2a ) # increasing h1, h2 = h1h2[order[0]] BT = BTmat( h1, h2, B, BI ) UBI = np.array( (np.dot(B, h1), np.dot(B,h2), (0,0,0)), float ) cImageD11.quickorient( UBI, BT ) UB = np.linalg.inv( UBI ) pairs = [ (h1, h2), ] cangs = [ c2a[order[0]], ] matrs = [ BT, ] gvecs = [ np.dot( UB, HKL0 ).T.copy(), ] nhkl = len(HKL0[0]) #import pdb;pdb.set_trace() for i in order[1:]: h1, h2 = h1h2[i] BT = BTmat( h1, h2, B, BI ) UBI = np.array( (np.dot(B, h1), np.dot(B,h2), (0,0,0)), float ) cImageD11.quickorient( UBI, BT ) j = len(pairs)-1 while j>=0 and fabs(c2a[order[i]] - cangs[j]) < 1e-6: if cImageD11.score( UBI, gvecs[j], 1e-8 ) < nhkl+1: pairs += [ (h1, h2), ] cangs += [ c2a[i], ] matrs += [ BT, ] gvecs += [ np.dot( np.linalg.inv(UBI) , HKL0 ).T.copy(), ] j = -1 # break else: j -= 1 return pairs, cangs, matrs
def gridgrains( ul, flt, pars, minx=-750, maxx=750, stepx=25, miny=-750, maxy=750, stepy=25, tol=0.05, ): trn = transformer.transformer() trn.loadfiltered(flt) trn.parameterobj = parameters.parameters(**pars) peaks = [trn.getcolumn(trn.xname), trn.getcolumn(trn.yname)] peaks_xyz = transform.compute_xyz_lab(peaks, **trn.parameterobj.get_parameters()) omega = trn.getcolumn(trn.omeganame) trn.updateparameters() tx = minx - stepx n = 0 sys.stderr.write("Using tol = %f\n" % (tol)) while tx <= maxx: tx = tx + stepx ty = miny - stepy while ty <= maxy: ty = ty + stepy trn.parameterobj.set_parameters({'t_x': tx, 't_y': ty}) pars = trn.parameterobj.get_parameters() tth, eta = transform.compute_tth_eta_from_xyz( peaks_xyz, omega, **pars) if 'omegasign' in pars: om_sgn = float(pars["omegasign"]) else: om_sgn = 1.0 gv = transform.compute_g_vectors(tth, eta, omega * om_sgn, **pars) print(tx, ty, end=' ') for ubi in ul: ns = score(ubi, gv.T, tol) print(ns, end=' ') n += ns print() return n
uniq_ubis = [] names = ubi_all.keys() #dsu = [ (int( np.split(".")[0].split("_")[-1] ), n) for n in names ] #dsu.sort() #names = [d[1] for d in dsu] tol = 0.05 for name in names[15:]: this_list = ubi_all[name] for ubi, i in zip(this_list, range(len(this_list))): gv = np.dot(np.linal.inv(ubi), hkl) seen = 0 for j in range(len(uniq_ubis)): u = uniq_ubis[j][0] npk = cImageD11.score(u, gv.T, tol) if npk == len(pks): # seen you before uniq_ubis[j][1].append((name, i)) seen += 1 if npk > 12 and npk < len(pks): print "close...", npk, if seen == 0: uniq_ubis.append([ubi, [(name, i)]]) if seen > 1: print "uniq seen more than once", ubi, i, name print "Found", len(uniq_ubis), "unique ubi matrices" z = {} for line in open("job_z.txt", "r").readlines():
def pairs(self, hkl1, hkl2, cos_tol=0.02, hkl_tol=0.1): """ We only look for reflection pairs matching a single hkl pairing """ import time start = time.time() w = self.transformpars.get("wavelength") tth1 = get_tth(self.unitcell.ds(hkl1), w) tth2 = get_tth(self.unitcell.ds(hkl2), w) tthtol = self.transformpars.get("fit_tolerance") allinds = np.arange(self.cf.nrows) ind1 = allinds[abs(self.cf.tth - tth1) < tthtol] ind2 = allinds[abs(self.cf.tth - tth2) < tthtol] angle, cosangle = self.unitcell.anglehkls(hkl1, hkl2) print("Angle, cosangle", angle, cosangle, hkl1, hkl2) assert angle > 1 and angle < 179, "hkls are parallel" g = np.array((self.cf.gx, self.cf.gy, self.cf.gz), np.float) n = g / self.cf.modg gvf = g.T.copy() n1 = n[:, ind1] n2 = n[:, ind2] pairs = [] j = np.arange(n2.shape[1]) # from unitcell.orient h1c = unit(np.dot(self.unitcell.B, hkl1)) h2c = unit(np.dot(self.unitcell.B, hkl2)) t1c = unit(h1c) t3c = unit(np.cross(h1c, h2c)) t2c = unit(np.cross(h1c, t3c)) T_c = np.array([t1c, t2c, t3c]) T_g = np.zeros((3, 3)) for i, this_n1 in enumerate(n1.T): cosa = np.dot(this_n1, n2) goodones = j[abs(cosa - cosangle) < cos_tol] # t1 is along g1 # t2 is plane of both: g1x(g1xg2) # t3 is perpendicular to both #if i%100==0: # print i,time.time()-start,len(n1.T) for k in goodones: this_n2 = n2[:, k] T_g[0] = this_n1 T_g[2] = unit(np.cross(this_n1, this_n2)) T_g[1] = unit(np.cross(this_n1, T_g[2])) U = np.dot(T_g.T, T_c) ub = np.dot(U, self.unitcell.B) ubi = np.linalg.inv(ub) ubio = ubi.copy() npks = cImageD11.score(ubi, gvf, hkl_tol) pairs.append((ind1[i], ind2[k], U, ubi)) print(npks, end=' ') ubi, trans = self.refine(ubi, np.zeros(3, np.float), tol=hkl_tol) inds, hkls = self.assign(ubi, trans, hkl_tol) ubi, trans = self.refine(ubi, trans, inds=inds, hkls=hkls, tol=hkl_tol) print(npks, ubi) print("cell: ", 6 * "%.6f " % (indexing.ubitocellpars(ubi))) print("position: ", trans) print() self.pairscache = pairs print(time.time() - start, "for", len(pairs), n1.shape, n2.shape) return pairs
def filter_pairs( h1, h2, c2a, B, BI, tol = 1e-5): """ remove duplicate pairs for orientation searches h1 = reflections of ring1, N1 peaks h2 = reflections of ring2, N2 peaks c2a = cos angle between them, N1xN2 B = B matrix in reciprocal space BI = inverse in real space """ assert c2a.shape == (len(h1), len(h2)) order = np.argsort( c2a.ravel() ) # increasing in cosine of angle c2as = c2a.flat[order] hi, hj= np.mgrid[0:len(h1),0:len(h2)] hi = hi.ravel()[order] # to get back the peaks hj = hj.ravel()[order] # Results holders: pairs = [ ] cangs = [ ] matrs = [ ] # cluster the cangs assuming a sensible threshold dc = (c2as[1:] - c2as[:-1]) > 1e-8 # differences inds = list(np.arange( 1, len(dc) + 1, dtype=int )[dc]) + [len(c2as)-1,] p = 0 # previous for i in inds: c = c2as[p:i] # block is p:i if abs(c2as[p]) < 0.98: # always keep the first one ha = h1[hi[p]] hb = h2[hj[p]] pairs.append( (ha, hb) ) cangs.append( c2as[p] ) BT = BTmat( ha, hb, B, BI) matrs.append( BT ) else: p = i continue if len(c) == 1: p = i continue assert (c.max()-c.min()) < 2.1e-8, "Angles blocking error in filter_pairs" # here we have a bunch of hkl pairs which all give the same angle # between them. They are not all the same. We generate a pair of peaks # from the first one and see which other pairs index differently ga = np.dot(B, ha ) gb = np.dot(B, hb ) assert abs( np.dot(ga,gb)/np.sqrt(np.dot(ga,ga)*np.dot(gb,gb)) - c2as[p] ) < 2e-8, "mixup in filter_pairs" gobs = np.array( (ga, gb, (0,0,0)), float) UBI = gobs.copy() cImageD11.quickorient( UBI, BT ) gtest = [ np.dot( np.linalg.inv(UBI), HKL0 ).T, ] for j in range(p+1,i): ha = h1[hi[j]] hb = h2[hj[j]] BT = BTmat( ha, hb, B, BI) newpair = True for gt in gtest: UBI = gobs.copy() cImageD11.quickorient( UBI, BT ) npk = cImageD11.score(UBI, gt, 1e-6) if npk == len(HKL0[0]): newpair = False break if newpair: pairs.append( (ha, hb) ) cangs.append( c2as[j] ) matrs.append( BT ) gtest.append( np.dot( np.linalg.inv(UBI), HKL0 ).T ) p = i return pairs, cangs, matrs
# ubi0 = np.linalg.inv( ub0 ) hkl0 = np.array((h0,k0,l0),float) unitH = hkl0 / np.sqrt( np.dot(hkl0,hkl0) ) unitG = gtest / np.sqrt( np.dot(gtest,gtest) ) angles_to_test = np.linspace( -np.pi, np.pi, 360 ) testmats = [ np.dot( ubi0, Rotation.from_rotvec( unitG*x ).as_dcm() ) for x in angles_to_test ] rmats = [ Rotation.from_rotvec( unitG*x ).as_dcm() for x in angles_to_test ] print(np.dot(testmats[0],gtest)) gobs = gcalc.T.copy() import timeit start = timeit.default_timer() npk = [cImageD11.score( m, gobs, 0.05) for m in testmats] print("Scoring takes", timeit.default_timer()-start) pl.figure() pl.plot( angles_to_test, npk,"-") # Conclusion: This appears to work. It takes a while to test all the potential orientations however. # Perhaps we can expand the mathematics to try to figure out some contributions a bit quicker? # # Our equation is: # hkl = integer = UBI0 . rotation_on_g0 . gvobs # # Each g-vector has a component along the g0 axis which will be invariant. # # The other components could be found in an axis/angle expansion. # - gaxis + gs.sin(angle) + gc.cos(angle)