def score(self, UBI, tol=None): """ Decide which are the best orientation matrices """ # t0=time.time() if tol == None: return closest.score(UBI, self.gvflat, self.hkl_tol) else: return closest.score(UBI, self.gvflat, tol)
def pairs(self, hkl1, hkl2, cos_tol=0.02, hkl_tol=0.05): """ 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) npks = closest.score(ubi, gvf, 0.1) pairs.append((ind1[i], ind2[k], U, ubi)) print(npks, ubi) self.pairs = pairs print(time.time() - start, "for", len(pairs), n1.shape, n2.shape) return pairs
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
def scorethem(self): start = time.time() ts = 0 tor = 0 ng = 0 tol = float(self.hkl_tol) gv = self.gvflat all = len(self.hits) print "Scoring", all, "potential orientations" prog = 0 ng = 0 nuniq = 0 while len(self.hits) > 0 and ng < self.max_grains: sys.stdout.write( "Tested %8d Found %8d Rejected %8d as not being unique\r" % (prog, ng, nuniq)) prog = prog + 1 diff, i, j = self.hits.pop() if self.ga[i] > -1 or self.ga[j] > -1: # skip things which are already assigned continue if i == j: continue try: self.unitcell.orient(self.ring_1, self.gv[i, :], self.ring_2, self.gv[j, :], verbose=0, all=False) except: print i, j, self.ring_1, self.ring_2 print self.gv[i] print self.gv[j] raise npk = closest.score(self.unitcell.UBI, gv, tol) if npk > self.minpks: self.unitcell.orient(self.ring_1, self.gv[i, :], self.ring_2, self.gv[j, :], verbose=0, all=True) npks = [ closest.score(UBItest, gv, tol) for UBItest in self.unitcell.UBIlist ] choice = np.argmax(npks) UBI = self.unitcell.UBIlist[choice] npk = npks[choice] # See if we already have this grain... try: ubio = self.refine(UBI.copy()) # refine the orientation ind = self.getind(ubio) # indices of peaks indexed ga = self.ga[ind] # previous grain assignments uniqueness = np.sum(np.where(ga == -1, 1, 0)) * 1.0 / ga.shape[0] if uniqueness > self.uniqueness: # print "Writing in self.ga" np.put(self.ga, ind, len(self.scores) + 1) self.ubis.append(ubio) self.scores.append(npk) ng = ng + 1 else: nuniq = nuniq + 1 # put(self.ga,ind,ng) except: raise print print "Number of orientations with more than", self.minpks, "peaks is", len( self.ubis) print "Time taken", time.time() - start if len(self.ubis) > 0: bestfitting = np.argmax(self.scores) print "UBI for best fitting\n", self.ubis[bestfitting] print "Unit cell\n", ubitocellpars(self.ubis[bestfitting]) print "Indexes", self.scorelastrefined, "peaks, with <drlv2>=", self.fitlastrefined print "That was the best thing I found so far" notaccountedfor = np.sum( np.where(np.logical_and(self.ga == -1, self.ra != -1), 1, 0)) print "Number of peaks assigned to rings but not indexed = ",\ notaccountedfor #self.histogram(self.ubis[bestfitting]) else: print "Try again, either with larger tolerance or fewer minimum peaks"
] #print len(pks) hkl = np.array(pks, float).T uniq_ubis = [] names = list(ubi_all.keys()) tol = 0.25 for name in names: this_list = ubi_all[name] for ubi, i in zip(this_list, list(range(len(this_list)))): gv = np.dot(np.linalg.inv(ubi), hkl) seen = 0 for j in range(len(uniq_ubis)): u = uniq_ubis[j][0] npk = closest.score(u, gv.T, tol) if npk == len(pks): # seen you before uniq_ubis[j][1].append((name, i)) uniq_ubis[j][2].append(ubi) uniq_ubis[j][0] = np.add.reduce( uniq_ubis[j][2] )/ \ len(uniq_ubis[j][2]) seen += 1 #if npk > 50 and npk < len(pks): # print "close...",npk, if seen == 0: uniq_ubis.append([ubi, [(name, i)], [ubi]]) if seen > 1: print("uniq seen more than once", ubi, i, name) print("# Found", len(uniq_ubis), "unique ubi matrices")
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 = closest.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():