Esempio n. 1
0
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
Esempio n. 2
0
    def orient(self, ring1, g1, ring2, g2, verbose=0, all=True):
        """
        Compute an orientation matrix using cell parameters and the indexing
        of two reflections

        Orientation matrix:
        A matrix such that h = A^1 g
        define 3 vectors t1,t2,t3
        t1 is parallel to first peak (unit vector along g1)
        t2 is in the plane of both   (unit vector along g1x(g1xg2))
        t3 is perpendicular to both  (unit vector along g1xg2)
        """
        costheta = np.dot(g1, g2) / np.sqrt((g1 * g1).sum() * (g2 * g2).sum())
        hab, c2ab, matrs = self.getanglehkls(ring1, ring2)
        if all:
            lo = min(np.searchsorted(c2ab, costheta - 1e-5, side='left'),
                     len(c2ab) - 1)
            hi = min(np.searchsorted(c2ab, costheta + 1e-5, side='right'),
                     len(c2ab))
            best = range(lo, hi)
            if verbose == 1:
                print("lo hi best", lo, hi, best, len(c2ab))
        else:
            i = np.searchsorted(c2ab, costheta, side='left')
            if i > 0 and (
                    i == len(c2ab) or
                (fabs(costheta - c2ab[i - 1]) < fabs(costheta - c2ab[i]))):
                best = [
                    i - 1,
                ]
            else:
                best = [
                    i,
                ]
        if verbose == 1:
            print("g1, g2", g1, g2)
            print("observed cos2theta", costheta)
            print("hab, c2ab", hab, c2ab)
            print("best", best)
        self.UBIlist = []
        UBlist = []
        for b in best:
            h1, h2 = hab[b]
            if verbose == 1:
                print("Assigning h1",h1,g1,self.ds(h1),\
                    math.sqrt(np.dot(g1,g1)),\
                    self.ds(h1)-math.sqrt(np.dot(g1,g1)))
                print("Assigning h2",h2,g2,self.ds(h2),\
                    math.sqrt(np.dot(g2,g2)),\
                    self.ds(h1)-math.sqrt(np.dot(g1,g1)))
                print("Cos angle calc", self.anglehkls(h1, h2), "obs",
                      costheta, "c2ab", c2ab[b])
            BT = matrs[b]
            UBI = np.empty((3, 3), float)
            UBI[0] = g1
            UBI[1] = g2
            cImageD11.quickorient(UBI, BT)
            if verbose == 1:
                print("UBI")
                print(UBI)
                h = np.dot(UBI, g1)
                print("(%9.3f, %9.3f, %9.3f)" % (h[0], h[1], h[2]))
                h = np.dot(UBI, g2)
                print("(%9.3f, %9.3f, %9.3f)" % (h[0], h[1], h[2]))
            self.UBI = UBI
            self.UB = np.linalg.inv(UBI)
            self.UBIlist.append(UBI)
            UBlist.append(self.UB)
        # trim to uniq list? What about small distortions...
        self.UBIlist = ubi_equiv(self.UBIlist, UBlist)
Esempio n. 3
0
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