def MC_fitAll_Angles_wrap(allparams,agrain,wmin,wmax,angles, weighting = True): pVec = allparams[0:3] Rparams = allparams[3:6] Uparams = allparams[6:12] U = gw.vec_to_sym(Uparams) R = Rotations.rotMatOfExpMap(Rparams) #angles = MC_make_angles_from_spots_grain(R,U,agrain,pVec,wmin,wmax,tweak = False) return MC_Angle_Fit(R,U,agrain,pVec,wmin,wmax,angles,weighting)
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)
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
def MC_fitU_Angle_wrap(Uparams,R,agrain,pVec,wmin,wmax,angles,weighting = True): uMat = gw.vec_to_sym(Uparams) #angles = MC_make_angles_from_spots_grain(R,U,agrain,pVec,wmin,wmax,tweak = False) return MC_Angle_Fit(R,uMat,agrain,pVec,wmin,wmax,angles, weighting)
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