Пример #1
0
def MC_make_angles_nrb(R,U,pVec,detectorGeom,planeData,omeMin,omeMax,tweak = False, stdev = rad(.1)):
    angl,axxx = Rotations.angleAxisOfRotMat(R)
    Rparams0 = (angl*axxx).flatten()
    #import uncertainty_analysis
    
    tmpDG = detector.DetectorGeomGE(detectorGeom, pVec=pVec)
    #print 'recip vects...',
    recips = planeData.makeRecipVectors(R = R, U = U)
    #print 'made'
    wavelength = planeData.wavelength
    nvecs = recips.shape[1]
    from data_class import inv_dict
    #allangs = inv_dict()
    allangs = []
    #import sys
    #print "nvecs",nvecs
    for j in range(10):
        print j,
    #print "done"
    for i in range(nvecs):
        #print i
        #sys.stdout.write(str(i)+ " ")
        rI = recips[:,i]
        #print "rI",rI,tweak
        if tweak:
            Rparams_tweak = numpy.random.normal([0,0,0],[stdev,stdev,stdev])
            #Rparams = Rparams0 + Rparams_tweak
            Rtweak = Rotations.rotMatOfExpMap(Rparams_tweak)
            #print Rparams0, Rparams
            
            rI = num.dot(Rtweak,rI)
        
        angle_sols  = Get_LLNL_Angles(rI,wavelength)
        #print rI, angle_sols
        for key in angle_sols.keys():
            tth,eta,w = angle_sols[key]
            w = mapOme(w,-num.pi,num.pi)
            if eta!=num.pi and eta!=-num.pi:
                eta = mapOme(eta,-num.pi,num.pi)
            if w<omeMin.getVal('radians') or w>omeMax.getVal('radians'):
                continue
            if w is not None:
                #pred_angle_vector = num.array(detectorGeom.xyoToAng(*(px,py,w)))
                tmpX, tmpY, tmpO = tmpDG.angToXYO([tth,tth],[eta,eta],[w,w])
                tmpX = tmpX[0]
                tmpY = tmpY[0]
                tmpO = tmpO[0]
                
                tmpTTh, tmpEta, tmpOme = detectorGeom.xyoToAng(tmpX, tmpY, tmpO)
                tmpAngs = num.vstack([tmpTTh, tmpEta, tmpOme])
                #allangs.Add((i,key),(num.array((tmpTTh,tmpEta,tmpOme)),(stdev,stdev,stdev)))
                allangs.append([tmpTTh,tmpEta,tmpOme])
    return num.array(allangs)
Пример #2
0
def MC_fitC_angle_weights(Rparams, Uparams, gIs,angles0, wavelength, weighting = False, report = True, evaluate = False):
    """
    alternate fitting approach for U, uses no Rotation information.
    Cparams: [C11,C22,C33,C12,C23,C13]
    """
    from Vector_funcs import Mag
    from numpy import dot,sqrt
    import uncertainty_analysis
    'internal objective function'
    def _fitC_residual(angles, r0, invCmat, wavelength):
        rI = gw.makeARecipVector(angles, wavelength)
        #print rI
        df = 1./Mag(rI)
        d0 = 1./Mag(r0)
        gii_0 = dot(r0,r0)
        gii = dot(dot(invCmat,r0),r0)
        r_i = df/d0 - sqrt(gii_0)/(sqrt(gii))
        return r_i
    def _fitC(Cparams,gIs,angles,wavelength):
        Cmat = gw.vec_to_sym(Cparams)
        invCmat = inv(Cmat)
        out = []
        for i in range(len(gIs)):
            gI = gIs[i]
            angCOM = angles[i] #, spot in gI_spots:
            r_i = _fitC_residual(angCOM,gI, invCmat, wavelength)
            if weighting:
                weight = uncertainty_analysis.propagateUncertainty(_fitC_residual, weighting[i], 1e-8,angCOM,gI,invCmat,wavelength)
            else:
                weight = 1
            out.append(r_i/weight)
        return num.array(out)
    
    if Rparams == 'default':
        angl,axxx = Rotations.angleAxisOfRotMat(num.eye(3))
        Rparams = (angl*axxx).flatten()
    else:
        Rparams = Rparams
    if Uparams=='default':
        Uparams = sym_to_vec(num.eye(3))
    else:
        Uparams = Uparams
        
    U = gw.vec_to_sym(Uparams)
    R = Rotations.rotMatOfExpMap(Rparams)
    F = num.dot(R,U)
    C_ = num.dot(F.T,F)
    Cparams = gw.sym_to_vec(C_)

    if evaluate:
        return _fitC(Cparams,gIs,angles0,wavelength)
    
    optResults = optimize.leastsq(_fitC, x0 = Cparams, args = (gIs,angles0,wavelength), full_output = 1)
    Cparams = optResults[0]    
    C = gw.vec_to_sym(Cparams)
    from scipy.linalg import eig
    'spectral decomposition to get U'
    eval,evec = eig(C)
    l1_sqr,l2_sqr,l3_sqr = num.asarray(eval,dtype = 'float')
    u1,u2,u3 = evec[:,0],evec[:,1],evec[:,2]
    U = sqrt(l1_sqr)*num.outer(u1,u1) + sqrt(l2_sqr)*num.outer(u2,u2) + sqrt(l3_sqr)*num.outer(u3,u3)
    if report:
        print "refined stretch matrix: \n%g, %g, %g\n%g, %g, %g\n%g, %g, %g\n" \
              % (U[0, 0], U[0, 1], U[0, 2], \
                 U[1, 0], U[1, 1], U[1, 2], \
                 U[2, 0], U[2, 1], U[2, 2], )
        

    return optResults
Пример #3
0
def MC_fitRotation_angles(Rparams, Uparams, gIs,angles0, wavelength, weighting = False, report = True,evaluate = False):
    assert len(gIs)==len(angles0), 'different lengths'
    def _fitR_residual(angles, r0, Fmat, wavelength):
        Cmat = dot(Fmat.T,Fmat)
        
        rI = gw.makeARecipVector(angles,wavelength)
        n = Unit(rI)
        N = Unit(r0)
        #print Fmat,Cmat
        
        alpha_ = sqrt(Inner_Prod(Star(Cmat),N,N))
        #print alpha_
        r_i = Inner_Prod(Star(Fmat),n,N) - alpha_
        #print 'r_i'
        return r_i        
    def _fitRotation_lsq(Rparams, gIs,angles0, U,wavelength):
        out = []
        Rmat = Rotations.rotMatOfExpMap(Rparams)
        Fmat = dot(Rmat,U)
        for i in range(len(gIs)):
            gI,angles = gIs[i], angles0[i]
            r_i = _fitR_residual(angles,gI, Fmat, wavelength)
            if weighting:
                weight = uncertainty_analysis.propagateUncertainty(_fitR_residual, weighting[i], 1e-8,angles,gI,Fmat,wavelength)
                maxiter = 100
                ct = 0
                while weight==0:
                    weight = uncertainty_analysis.propagateUncertainty(_fitR_residual, weighting[i], 1e-8,angles,gI,Fmat,wavelength)
                    ct+=1
                    if ct>=maxiter:
                        print 'zero weight error, i = ',i
                        weight = 1
                        break
                    
            else:
                weight = 1.
            out.append(r_i/weight)
            if weight == 0:
                print 'wefiht0',i,weighting[i],Fmat
            

        out = num.array(out)

    #    print out
        return out
    if Rparams == 'default':
        angl,axxx = Rotations.angleAxisOfRotMat(num.eye(3))
        Rparams = (angl*axxx).flatten()
    else:
        Rparams = Rparams
    if Uparams=='default':
        Uparams = sym_to_vec(num.eye(3))
    else:
        Uparams = Uparams
        
    U = gw.vec_to_sym(Uparams)
    if evaluate:
        return _fitRotation_lsq(Rparams,gIs,angles0,U,wavelength)
    
    optResults = optimize.leastsq(_fitRotation_lsq, x0 = Rparams, args = (gIs,angles0, U, wavelength), full_output = 1)
    r1 = optResults[0]
    U1 = Rotations.rotMatOfExpMap(r1)
    if report:
        print "refined orientation matrix: \n%g, %g, %g\n%g, %g, %g\n%g, %g, %g\n" \
              % (U1[0, 0], U1[0, 1], U1[0, 2], \
                 U1[1, 0], U1[1, 1], U1[1, 2], \
                 U1[2, 0], U1[2, 1], U1[2, 2], )
        
    return optResults