Example #1
0
def vars(inp):
    """
    Calculated experimental errors of gexp for all peaks
    Jette Oddershede, July 2008
    """

    check_input.set_globals(inp)
    # start preparing for volume weighted errors
    volavg = []
    try:
        for i in range(inp.no_grains):
            if len(inp.volume[i]) > 0:
                #                volavg.append(sum(inp.volume[i])/len(inp.volume[i])) #mean
                volavg.append(reject.median(inp.volume[i]))  #median
            else:
                volavg.append(0)
    except:
        volavg = [1.] * inp.no_grains
    volavg_significant = deepcopy(volavg)
    for i in range(inp.no_grains - 1, -1, -1):
        if volavg_significant[i] == 0:
            volavg_significant.pop(i)
    if len(volavg_significant) > 0:
        volmedian = reject.median(volavg_significant)
    else:
        volmedian = 1.
    # end preparing for volume weighted errors

    inp.vars = []
    for i in range(inp.no_grains):
        inp.vars.append([])
        for j in range(inp.nrefl[i]):
            #            if i+1 in inp.fit['skip']:
            #                inp.vars[i].append([1,1,1])
            #            else:
            #                Sgg = error(inp.w[inp.id[i][j]],inp.dety[inp.id[i][j]],inp.detz[inp.id[i][j]],\
            #                            inp.Sww[inp.id[i][j]],inp.Syy[inp.id[i][j]],inp.Szz[inp.id[i][j]],\
            #                            inp.values['wx'],inp.values['wy'],inp.values['tx'],inp.values['ty'],inp.values['tz'],\
            #                            inp.values['py'],inp.values['pz'],inp.values['cy'],inp.values['cz'],inp.values['L'],\
            #                            inp.values['x%s' %i],inp.values['y%s' %i],inp.values['z%s' %i])
            #                inp.vars[i].append([Sgg[0,0],Sgg[1,0],Sgg[2,0]]) # propagated errors
            #                inp.vars[i].append([Sgg[0,0]+1e-9,Sgg[1,0]+1e-9,Sgg[2,0]+1e-9]) # propagated errors + constant contribution
            if volavg[i] != 0:
                inp.vars[i].append([
                    4e-8 * volmedian / volavg[i], 4e-8 * volmedian / volavg[i],
                    1e-8 * volmedian / volavg[i]
                ])  # no error propagation
            else:
                inp.vars[i].append([4., 4., 1.])  # no error propagation
Example #2
0
def vars(inp):
    """
    Calculated experimental errors of gexp for all peaks
    Jette Oddershede, July 2008
    """

    check_input_multidet.set_globals(inp)
    # start preparing for volume weighted errors
    volavg = []
    try:
        for i in range(inp.no_grains):
            if len(inp.volume[i]) > 0:
                #                volavg.append(sum(inp.volume[i])/len(inp.volume[i])) #mean
                volavg.append(reject.median(inp.volume[i]))  #median
            else:
                volavg.append(0)
    except:
        volavg = [1.] * inp.no_grains
    volavg_significant = deepcopy(volavg)
    for i in range(inp.no_grains - 1, -1, -1):
        if volavg_significant[i] == 0:
            volavg_significant.pop(i)
    if len(volavg_significant) > 0:
        volmedian = reject.median(volavg_significant)
    else:
        volmedian = 1.
    # end preparing for volume weighted errors

    inp.vars = []
    for k in range(inp.fit['no_det']):
        vars = []
        for i in range(inp.no_grains):
            vars.append([])
            #            print 'test', inp.nrefl[k][i]
            for j in range(inp.nrefl[k][i]):
                if volavg[i] != 0:
                    vars[i].append([
                        4e-8 * volmedian / volavg[i] *
                        inp.fit['vars_scale%s' % k], 4e-8 * volmedian /
                        volavg[i] * inp.fit['vars_scale%s' % k], 1e-8 *
                        volmedian / volavg[i] * inp.fit['vars_scale%s' % k]
                    ])  # no error propagation
                else:
                    vars[i].append([
                        4. * inp.fit['vars_scale%s' % k],
                        4. * inp.fit['vars_scale%s' % k],
                        1. * inp.fit['vars_scale%s' % k]
                    ])  # no error propagation
        inp.vars.append(vars)
Example #3
0
def write_values(lsqr):
    """
    Save the fitted grain parameters, pos, U and eps

    INPUT: The parameter set from the final fitting
    OUTPUT: grainno mean_IA grain_volume x y z rodx rody rodz 
            U11 U12 U13 U21 U22 U23 U31 U32 U33 
            eps11   eps22   eps33   eps23   eps13   eps12
            eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s
            sig11   sig22   sig33   sig23   sig13   sig12
            sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s
            sig_tth sig_eta
 
    Jette Oddershede, Risoe DTU, April 21 2008
    """

    filename = '%s/%s_%s.gff' % (lsqr.inp.fit['direc'], lsqr.inp.fit['stem'],
                                 lsqr.inp.fit['goon'])
    f = open(filename, 'w')
    format = "%d " * 1 + "%f " * 8 + "%0.12f " * 9 + "%e " * 24 + "%f " * 2 + "\n"
    out = "# grainno mean_IA grainvolume x y z rodx rody rodz U11 U12 U13 U21 U22 U23 U31 U32 U33 eps11 eps22 eps33 eps23 eps13 eps12 "
    out = out + "eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s sig11 sig22 sig33 sig23 sig13 sig12 sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s sig_tth sig_eta\n"
    f.write(out)
    for i in range(lsqr.inp.no_grains):
        if i + 1 in lsqr.inp.fit['skip']:
            pass
        else:
            U = tools.rod_to_u([
                lsqr.inp.rod[i][0] + lsqr.m.values['rodx%s' % i],
                lsqr.inp.rod[i][1] + lsqr.m.values['rody%s' % i],
                lsqr.inp.rod[i][2] + lsqr.m.values['rodz%s' % i],
            ])
            eps = n.array([[
                lsqr.m.values['epsaa%s' % i], lsqr.m.values['epsab%s' % i],
                lsqr.m.values['epsac%s' % i]
            ],
                           [
                               lsqr.m.values['epsab%s' % i],
                               lsqr.m.values['epsbb%s' % i],
                               lsqr.m.values['epsbc%s' % i]
                           ],
                           [
                               lsqr.m.values['epsac%s' % i],
                               lsqr.m.values['epsbc%s' % i],
                               lsqr.m.values['epscc%s' % i]
                           ]])
            eps_s = conversion.grain2sample(eps, U)
            sig = conversion.strain2stress(eps, lsqr.inp.C)
            sig_s = conversion.grain2sample(sig, U)
            out = format % (
                i + 1,
                n.sum(lsqr.inp.mean_ia[i]) / len(lsqr.inp.mean_ia[i]),  #0,
                #                          sum(lsqr.inp.volume[i])/lsqr.inp.nrefl[i],
                reject.median(lsqr.inp.volume[i]),
                lsqr.m.values['x%s' % i] / 1000,
                lsqr.m.values['y%s' % i] / 1000,
                lsqr.m.values['z%s' % i] / 1000,
                lsqr.inp.rod[i][0] + lsqr.m.values['rodx%s' % i],
                lsqr.inp.rod[i][1] + lsqr.m.values['rody%s' % i],
                lsqr.inp.rod[i][2] + lsqr.m.values['rodz%s' % i],
                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],
                eps[0][0],
                eps[1][1],
                eps[2][2],
                eps[1][2],
                eps[0][2],
                eps[0][1],
                eps_s[0][0],
                eps_s[1][1],
                eps_s[2][2],
                eps_s[1][2],
                eps_s[0][2],
                eps_s[0][1],
                sig[0][0],
                sig[1][1],
                sig[2][2],
                sig[1][2],
                sig[0][2],
                sig[0][1],
                sig_s[0][0],
                sig_s[1][1],
                sig_s[2][2],
                sig_s[1][2],
                sig_s[0][2],
                sig_s[0][1],
                reject.median(lsqr.inp.spr_tth[i]),
                reject.median(lsqr.inp.spr_eta[i]))
            f.write(out)
    f.close()
Example #4
0
    def refine(self):
        """
	Carry out one refinement cycle according to the order given by self.inp.fit['reforder']
	Reject reflection according to self.inp.fit['rej_resmean']
	Print and save refinement and rejection info and parameters
	
	Jette Oddershede, Risoe DTU, May 15 2008
	"""

        # initialise
        self.poor_value = []
        self.poor_nrefl = []

        # create lists of parameters, global and for each grain
        self.globals = ["a", "b", "c", "alpha", "beta", "gamma", "wx", "wy"]
        for i in range(self.inp.fit['no_det']):
            self.globals.append("tx%s" % i)
            self.globals.append("ty%s" % i)
            self.globals.append("tz%s" % i)
            self.globals.append("py%s" % i)
            self.globals.append("pz%s" % i)
            self.globals.append("cy%s" % i)
            self.globals.append("cz%s" % i)
            self.globals.append("L%s" % i)

        self.grains = []
        for i in range(self.inp.no_grains):
            self.grains.append([
                "x%s" % i,
                "y%s" % i,
                "z%s" % i,
                "rodx%s" % i,
                "rody%s" % i,
                "rodz%s" % i,
                "epsaa%s" % i,
                "epsbb%s" % i,
                "epscc%s" % i,
                "epsbc%s" % i,
                "epsac%s" % i,
                "epsab%s" % i
            ])

    # correct for z-offset
        xcom = 0
        ycom = 0
        zcom = 0
        vol = 0
        for i in range(self.inp.no_grains):
            if i + 1 not in self.inp.fit['skip']:
                vol = vol + reject.median(self.inp.volume[i])
                xcom = xcom + self.inp.values['x%s' % i] * reject.median(
                    self.inp.volume[i])
                ycom = ycom + self.inp.values['y%s' % i] * reject.median(
                    self.inp.volume[i])
                zcom = zcom + self.inp.values['z%s' % i] * reject.median(
                    self.inp.volume[i])
        xcom = xcom / vol
        ycom = ycom / vol
        zcom = zcom / vol

        for i in range(self.inp.no_grains):
            #            self.inp.values['x%s' %i] = self.inp.values['x%s' %i] - xcom
            #            self.inp.values['y%s' %i] = self.inp.values['y%s' %i] - ycom
            self.inp.values['z%s' % i] = self.inp.values['z%s' % i] - zcom

        for i in range(self.inp.fit['no_det']):
            self.inp.values['cz%s' % i] = self.inp.values[
                'cz%s' % i] + zcom / self.inp.values['pz%s' % i]

    #refinement update
        reload(fcn)
        self.m = Minuit(fcn.FCN)
        self.m.values = self.inp.values
        self.m.errors = self.inp.errors
        for entries in self.m.fixed:
            self.m.fixed[entries] = True
        for entries in self.globals:
            self.m.fixed[entries] = True
            print entries, self.inp.values[entries]

        # determine whether to refine
        self.ref = False
        if 'globals' in self.inp.fit['goon']:
            self.ref = True

        # carry out refinement
        if self.ref == True:
            self.mg = Minuit(fcn.FCNgrain)
            self.mg.values = self.m.values
            self.mg.errors = self.m.errors
            self.mg.fixed = self.m.fixed

            print '\n\n*****Now fitting %s*****' % self.inp.fit['goon']
            print 'newreject_grain', self.inp.fit['newreject_grain']
            # calculate starting values
            g = fit_multidet.grain_values(self)
            self.fval = sum(g)
            print '\n%s starting value %e' % (self.inp.fit['goon'], self.fval)
            t1 = time.clock()
            self.fitglobals()
            print 'Fit %s tolerance %e' % (self.inp.fit['goon'], self.m.tol)
            self.m.errors = self.inp.errors
            self.m.migrad()
            fit_multidet.scale_errors(self)
            self.inp.values = self.m.values
            self.inp.errors = self.m.errors
            write_output_multidet.write_global(self)

            self.time = time.clock() - t1
            print 'Fit %s time %i s' % (self.inp.fit['goon'], self.time)
            print 'Fit %s value %e \n' % (self.inp.fit['goon'], self.m.fval)

            # apply crystal_system restraints to unit cell parameters
            if 'hex' in self.inp.fit[
                    'crystal_system'] or 'trigonal' in self.inp.fit[
                        'crystal_system'] or 'tetra' in self.inp.fit[
                            'crystal_system']:
                self.m.values['b'] = self.m.values['a']
            elif 'cubic' in self.inp.fit[
                    'crystal_system'] or 'isotropic' in self.inp.fit[
                        'crystal_system']:
                self.m.values['b'] = self.m.values['a']
                self.m.values['c'] = self.m.values['a']
        #constrain pixels to be square
            if self.inp.fit['pixel'] == 1:
                for k in range(self.inp.fit['no_det']):
                    self.m.values['pz%s' % k] = self.m.values['py%s' % k]

            # reject outliers and save cycle info
            fit_multidet.reject_outliers(self)
            write_output_multidet.write_rej(self.inp,
                                            message=self.inp.fit['goon'])
            write_output_multidet.write_log(self)
            write_output_multidet.write_par(self)

        # move onto next refinement given by the reforder list
        write_output_multidet.write_values(self)
        self.inp.fit['goon'] = self.inp.fit['reforder'][
            self.inp.fit['reforder'].index(self.inp.fit['goon']) + 1]

        return
Example #5
0
    def refine(self):   
#    """
#    Carry out one refinement cycle according to the order given by self.inp.fit['reforder']
#    Reject reflection according to self.inp.fit['rej_resmean']
#    Print and save refinement and rejection info and parameters
#    
#    Jette Oddershede, Risoe DTU, May 15 2008
#    """
        
        # initialise
        self.poor_value = []
        self.poor_nrefl = []

        # create lists of parameters, global and for each grain
        self.globals = ["a","b","c","alpha","beta","gamma","wx","wy","tx","ty","tz","py","pz","cy","cz","L"]
        self.grains = []
        for i in range(self.inp.no_grains):
            self.grains.append(["x%s" %i,"y%s" %i,"z%s" %i,"rodx%s" %i,"rody%s" %i,"rodz%s" %i,
                                "epsaa%s" %i,"epsbb%s" %i,"epscc%s" %i,"epsbc%s" %i,"epsac%s" %i,"epsab%s" %i])

        # correct for z-offset
        xcom = 0
        ycom = 0
        zcom = 0
        vol = 0
        for i in range(self.inp.no_grains):
            if i+1 not in self.inp.fit['skip']:
                vol = vol + reject.median(self.inp.volume[i])
                xcom = xcom + self.inp.values['x%s' %i]*reject.median(self.inp.volume[i])
                ycom = ycom + self.inp.values['y%s' %i]*reject.median(self.inp.volume[i])
                zcom = zcom + self.inp.values['z%s' %i]*reject.median(self.inp.volume[i])
        xcom = xcom / vol
        ycom = ycom / vol
        zcom = zcom / vol
        
        for i in range(self.inp.no_grains):
                self.inp.fitarg['z%s' %i] = self.inp.fitarg['z%s' %i] - zcom
                self.inp.values['z%s' %i] = self.inp.values['z%s' %i] - zcom 
           
        self.inp.values['cz'] = self.inp.values['cz'] + zcom/self.inp.values['pz']
        self.inp.fitarg['cz'] = self.inp.fitarg['cz'] + zcom/self.inp.values['pz']  
        

        #refinement update
        reload(fcn)
        self.m = Minuit(fcn.FCN_fitga,errordef=1,pedantic=False,print_level=-1,**self.inp.fitarg)
        try:
            self.m.values = self.inp.values
            self.m.errors = self.inp.errors
            for entries in self.m.fixed:
                self.m.fixed[entries] = True
        except:
            for entries in self.m.fitarg:
                if "fix_" in entries:
                    self.m.fitarg[entries] = True
            

        # determine whether to refine
        self.ref = False
        if 'globals' in self.inp.fit['goon']:
            self.ref = True
            

        # carry out refinement
        if self.ref == True:
            try:
                self.mg = Minuit(fcn.FCNgrain,errordef=1,pedantic=False,print_level=-1,**self.m.fitarg)
            except:
                self.mg = Minuit(fcn.FCNgrain,errordef=1)
                self.mg.values = self.m.values
                self.mg.errors = self.m.errors
                self.mg.fixed = self.m.fixed

            print '\n\n*****Now fitting %s*****' %self.inp.fit['goon']
            print 'newreject_grain', self.inp.fit['newreject_grain']
            # calculate starting values
            g = fit.grain_values(self)
            self.g_old = deepcopy(g)
            self.fval = sum(g)
            print '\n%s starting value %e' %(self.inp.fit['goon'],self.fval)
            t1 = time.clock()
            self.fitglobals()
            print 'Fit %s tolerance %e' %(self.inp.fit['goon'],self.m.tol)
            try:
                self.m.errors = self.inp.errors
            except:
                for entries in self.m.fitarg:
                    if "error_" in entries:
                        self.m.fitarg[entries] = self.inp.fitarg[entries]
                observations = 0
                for j in range(self.inp.no_grains):
                    if j+1 in self.inp.fit['skip']:
                        pass
                    else:
                        observations = observations + self.inp.nrefl[j]
                errordef1 = self.fval/(3*observations-self.m.fitarg.values().count(False))
                self.m = Minuit(fcn.FCN_fitga,errordef=errordef1,pedantic=False,print_level=-1,**self.m.fitarg)
                self.m.tol = self.m.tol/errordef1
            self.m.migrad()
            try:
                fit.scale_errors(self)
            except:
                pass
            self.inp.values = self.m.values
            self.inp.errors = self.m.errors
            write_output.write_global(self)
                
            self.time = time.clock()-t1
            print 'Fit %s time %i s' %(self.inp.fit['goon'],self.time)
            print 'Fit %s value %e \n' %(self.inp.fit['goon'],self.m.fval)
                
            # apply crystal_system restraints to unit cell parameters
            if 'hex' in self.inp.fit['crystal_system'] or 'trigonal' in self.inp.fit['crystal_system'] or 'tetra' in self.inp.fit['crystal_system'] :
                try:
                    self.m.values['b'] = self.m.values['a'] 
                except:
                    self.m.fitarg['b'] = self.m.fitarg['a']
            elif 'cubic' in self.inp.fit['crystal_system'] or 'isotropic' in self.inp.fit['crystal_system']:
                try:
                    self.m.values['b'] = self.m.values['a']
                    self.m.values['c'] = self.m.values['a']
                except:
                    self.m.fitarg['b'] = self.m.fitarg['a']
                    self.m.fitarg['c'] = self.m.fitarg['a']
             
            #unit cell constraints
            if self.inp.fit['constrx'] != 0:
                for i in range(self.inp.no_grains): 
                    try:
                        self.m.values['x%i' %i] = self.m.values['x0'] 
                        self.m.errors['x%i' %i] = self.m.errors['x0'] 
                    except:
                        self.m.fitarg['x%i' %i] = self.m.fitarg['x0'] 
                        self.m.fitarg['error_x%i' %i] = self.m.fitarg['error_x0'] 
            if self.inp.fit['constry'] != 0:
                for i in range(self.inp.no_grains): 
                    try:
                        self.m.values['y%i' %i] = self.m.values['y0'] 
                        self.m.errors['y%i' %i] = self.m.errors['y0'] 
                    except:
                        self.m.fitarg['y%i' %i] = self.m.fitarg['y0'] 
                        self.m.fitarg['error_y%i' %i] = self.m.fitarg['error_y0'] 
            if self.inp.fit['constrz'] != 0:
                for i in range(self.inp.no_grains): 
                    try:
                        self.m.values['z%i' %i] = self.m.values['z0'] 
                        self.m.errors['z%i' %i] = self.m.errors['z0'] 
                    except:
                        self.m.fitarg['z%i' %i] = self.m.fitarg['z0'] 
                        self.m.fitarg['error_z%i' %i] = self.m.fitarg['error_z0'] 
            
            # reject outliers and save cycle info   
            fit.reject_outliers(self)
            write_output.write_rej(self.inp,message=self.inp.fit['goon'])
            write_output.write_log(self)
            write_output.write_par(self)
            try:    
                self.inp.fitarg = self.m.fitarg
            except:
                pass

        # move onto next refinement given by the reforder list  
        write_output.write_values(self)
        self.inp.fit['goon'] = self.inp.fit['reforder'][self.inp.fit['reforder'].index(self.inp.fit['goon'])+1]

        return