def fitRotationStar_angle_weights(self, Rparams = 'default', Uparams = 'default', report = True, weighting = True, evaluate = False): """ uses F*N = \alpha n to refine rotation """ def _fitR_residual(angles, r0, Fmat, wavelength): Cmat = dot(Fmat.T,Fmat) rI = makeARecipVector(angles,wavelength) n = Unit(rI) N = Unit(r0) alpha_ = sqrt(Inner_Prod(Star(Cmat),N,N)) r_i = Inner_Prod(Star(Fmat),n,N) - alpha_ return r_i def _fitRotation_lsq(Rparams, gI_spots, U,wavelength): out = [] Rmat = Rotations.rotMatOfExpMap(Rparams) Fmat = dot(Rmat,U) for gI,spot in gI_spots: try: angCOM, angCOM_unc = spot.angCOM(useFit=True, getUncertainties=True) except RuntimeError: continue r_i = _fitR_residual(angCOM.flatten(),gI, Fmat, wavelength) if weighting: weight = uncertainty_analysis.propagateUncertainty(_fitR_residual, angCOM_unc, 1e-8,angCOM.flatten(),gI,Fmat,wavelength) #print 'Rotation using weight', weight, r_i, r_i/weight, angCOM_unc else: weight = 1. out.append(r_i/weight) return num.array(out) if Rparams == 'default': angl,axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angl*axxx).flatten() else: Rparams = Rparams if Uparams=='default': Uparams = sym_to_vec(self.uMat) else: Uparams = Uparams self.fitGrainSpotUncertainties() gI_spots = self.associateRecipVector_Spots() U = vec_to_sym(Uparams) wavelength = self.planeData.wavelength if evaluate: return _fitRotation_lsq(Rparams,gI_spots,U,wavelength) optResults = optimize.leastsq(_fitRotation_lsq, x0 = Rparams, args = (gI_spots, 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], ) self.rMat = U1 return optResults
def fitRotation_Angles_(self, Rparams = 'default', Uparams = 'default', report = True, weighting = True, evaluate = False): """ """ if Rparams == 'default': angl,axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angl*axxx).flatten() else: Rparams = Rparams if Uparams=='default': Uparams = sym_to_vec(self.uMat) else: Uparams = Uparams U = vec_to_sym(Uparams) def _lsq_Rotation(Rparams, U, weighting = True): return self.fitAngles_lsq(Rparams, U, weighting) if evaluate: return _lsq_Rotation(Rparams,U,weighting) optResults = optimize.leastsq(_lsq_Rotation, x0 = Rparams, args = (U,weighting), 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], ) # reset pVec in self self.rMat = U1 return optResults
def fitU(self, Rparams = 'default', Uparams = 'default'): if Uparams=='default': Uparams = sym_to_vec(self.uMat) else: Uparams = Uparams if Rparams == 'default': angl, axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angl*axxx).flatten() else: Rparams = Rparams optResults = optimize.leastsq(self._fitU_objFunc_lsq, x0 = Uparams, args = (Rparams, self.planeData.wavelength), xtol=1e-5, ftol=1e-5, full_output = 1) Uparams = optResults[0] U = vec_to_sym(Uparams) 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], ) self.uMat = U
def fitU_Angles_(self, Rparams = 'default', Uparams = 'default', weighting = True, report = True,evaluate = False): if Uparams=='default': Uparams = sym_to_vec(self.uMat) else: Uparams = Uparams if Rparams == 'default': angl, axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angl*axxx).flatten() else: Rparams = Rparams def _lsq_U(Uparams, Rparams): U = vec_to_sym(Uparams) return self.fitAngles_lsq(Rparams, U, weighting) if evaluate: return _lsq_U(Uparams, Rparams, weighting) optResults = optimize.leastsq(_lsq_U, x0 = Uparams, args = Rparams, full_output = 1) r1 = optResults[0] U1 = vec_to_sym(r1) if report: print "refined U 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], ) self.uMat = U1 return optResults
def fitRotation(self, Rparams = 'default', Uparams = 'default', report = True,evaluate = False): """ uses F*N = \alpha n to refine rotation """ if Rparams == 'default': angl,axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angl*axxx).flatten() else: Rparams = Rparams if Uparams=='default': Uparams = sym_to_vec(self.uMat) else: Uparams = Uparams def _fitRotation_lsq(Rparams, vecs, U): out = [] Rmat = Rotations.rotMatOfExpMap(Rparams) Fmat = dot(Rmat,U) Cmat = dot(Fmat.T,Fmat) for r0,rd in vecs: N = Unit(r0) n = Unit(rd) alpha_ = sqrt(Inner_Prod(Star(Cmat),N,N)) r_i = Inner_Prod(Star(Fmat),n,N) - alpha_ out.append(r_i) return num.array(out) gI_rIs = self.associateRecipVectors() U = vec_to_sym(Uparams) if evaluate: return _fitRotation_lsq(Rparams,gI_rIs,U) optResults = optimize.leastsq(_fitRotation_lsq, x0 = Rparams, args = (gI_rIs, U), 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], ) # reset pVec in self self.rMat = U1 return optResults
def _fitRotation_lsq(Rparams, vecs, U): out = [] Rmat = Rotations.rotMatOfExpMap(Rparams) Fmat = dot(Rmat,U) Cmat = dot(Fmat.T,Fmat) for r0,rd in vecs: N = Unit(r0) n = Unit(rd) alpha_ = sqrt(Inner_Prod(Star(Cmat),N,N)) r_i = Inner_Prod(Star(Fmat),n,N) - alpha_ out.append(r_i) return num.array(out)
def fitF(self): def _fitF(FParams, vecs): Rparams = FParams[0:3] Uparams = FParams[3:9] rMat = Rotations.rotMatOfExpMap(Rparams) uMat = vec_to_sym(Uparams) F = dot(rMat,uMat) FinvT = inv(F).T out = [] for r0,rd in vecs: diff = dot(FinvT,r0) - rd out.append(diff[0]) out.append(diff[1]) out.append(diff[2]) return num.array(out) gI_rIs = self.associateRecipVectors() angle,axxx = Rotations.angleAxisOfRotMat(self.rMat) Rparams = (angle*axxx).flatten() Uparams = sym_to_vec(self.uMat) Fparams = [] Fparams.extend(list(Rparams)) Fparams.extend(list(Uparams)) optResults = optimize.leastsq(_fitF, x0 = Fparams, args = gI_rIs, full_output = 1) Rparams = optResults[0][0:3] Uparams = optResults[0][3:9] rMat = Rotations.rotMatOfExpMap(Rparams) uMat = vec_to_sym(Uparams) F = dot(rMat,uMat) print 'finalF' print F R_,U = polarDecomposition(F) print R_,'\n',U return optResults
def _fitF(FParams, vecs): Rparams = FParams[0:3] Uparams = FParams[3:9] rMat = Rotations.rotMatOfExpMap(Rparams) uMat = vec_to_sym(Uparams) F = dot(rMat,uMat) FinvT = inv(F).T out = [] for r0,rd in vecs: diff = dot(FinvT,r0) - rd out.append(diff[0]) out.append(diff[1]) out.append(diff[2]) return num.array(out)
def fitAngles_lsq(self,Rparams,Umat, weighting = True): R_ = Rotations.rotMatOfExpMap(Rparams) angles = self.associateRecipVectorAngles(R_,Umat) out = [] for angleset in angles: measangles, predangles, unc = angleset diff = measangles - predangles #print measangles,predangles if weighting: w1,w2,w3 = unc else: w1=w2=w3=1. out.append(diff[0]/w1) out.append(diff[1]/w2) out.append(diff[2]/w3) return num.array(out)
def _fitRotation_lsq(Rparams, gI_spots, U,wavelength): out = [] Rmat = Rotations.rotMatOfExpMap(Rparams) Fmat = dot(Rmat,U) for gI,spot in gI_spots: try: angCOM, angCOM_unc = spot.angCOM(useFit=True, getUncertainties=True) except RuntimeError: continue r_i = _fitR_residual(angCOM.flatten(),gI, Fmat, wavelength) if weighting: weight = uncertainty_analysis.propagateUncertainty(_fitR_residual, angCOM_unc, 1e-8,angCOM.flatten(),gI,Fmat,wavelength) #print 'Rotation using weight', weight, r_i, r_i/weight, angCOM_unc else: weight = 1. out.append(r_i/weight) return num.array(out)