PEATDB/Ekin/LM_Fitter.py
def fit(self, rounds=500, error_crit=0.00005, gradient_crit = 1e-6, step=1E-8,
                LM_damper = 0.00001, damper_adjustment_factor = 2.0, silent=False):
        """Do 500 rounds of fitting, or until converged"""
        if silent == False:
            print 'Fitting..'
            print 'start vars:', self.variables
        # Set damper
        self.step=step
        self.LM_damper = LM_damper
        
        # Try to set error_crit using exp_data range
        
        if error_crit == None:
            #get y vals
            y=[]
            for i in self.exp_data:
                y.append(i[1])
            yrange = max(y) - min(y)            
            error_crit = yrange/1e6
            print 'setting error_crit:', error_crit

        # Start iterating
        old_diff=self.get_difference(self.variables)
        status = 'MI'
        count=0
        #print 'Step    Diff   LM_damper'
        for x in range(1,rounds):
            self.fit_LM()
            # check for singluar matrix
            if self.singular_matrix == 1:
                status = 'Stopped - singular matrix'
                if silent == False:
                    print 'Stopped (Singular matrix)'
                break

            now_diff=self.get_difference(self.variables)
            count+=1
            self.callback(difference=now_diff,variables=self.variables,count=count,fitter=self)

            # Check convergence
            s = ''
            if now_diff < old_diff:
                s = 'step accepted'
                self.LM_damper = self.LM_damper / damper_adjustment_factor
                old_diff=now_diff
            else:
                s = 'cancelling'
                self.cancel_step()
                self.LM_damper = self.LM_damper * damper_adjustment_factor

            #print '%5d  %6.4f  %6.4e' %(x, now_diff, self.LM_damper), self.variables, s

            # check error criterium
            #print 'Diff: %5.3e, error_crit: %5.3e,| gradient: %5.3e, grad_crit: %5.3e' %(now_diff,error_crit,self.error_gradient,gradient_crit)
            if abs(now_diff)<=error_crit:
                status = 'Converged - error criterium'
                if silent == False:
                    print 'Converged. (Error criterium) Sum of differences: %7.2e' %(now_diff)
                break

            # check gradient criterium
            if self.error_gradient <= gradient_crit:
                status = 'Stopped - gradient criterium'
                if silent == False:
                    print 'Stopped (Gradient criterium) Error gradient: %7.2e' %(self.error_gradient)
                break

            # check for singluar matrix
            if self.singular_matrix == 1:
                status = 'Stopped - singular matrix'
                if silent == False:
                    print 'Stopped (Singular matrix)'
                break

            # Were we told to stop?
            if self.stop_fit==1:
                break
        return status,self.variables
Do 500 rounds of fitting, or until converged
Do 500 rounds of fitting, or until converged
Do 500 rounds of fitting or until converged
def fit(self, rounds=500, error_crit=0.00005, gradient_crit = 1e-6, step=1E-8,
                LM_damper = 0.00001, damper_adjustment_factor = 2.0, silent=False):
        if silent == False:
            print 'Fitting..'
            print 'start vars:', self.variables
        self.step=step
        self.LM_damper = LM_damper
        if error_crit == None:
            y=[]
            for i in self.exp_data:
                y.append(i[1])
            yrange = max(y) - min(y)            
            error_crit = yrange/1e6
            print 'setting error_crit:', error_crit
        old_diff=self.get_difference(self.variables)
        status = 'MI'
        count=0
        for x in range(1,rounds):
            self.fit_LM()
            if self.singular_matrix == 1:
                status = 'Stopped - singular matrix'
                if silent == False:
                    print 'Stopped (Singular matrix)'
                break
            now_diff=self.get_difference(self.variables)
            count+=1
            self.callback(difference=now_diff,variables=self.variables,count=count,fitter=self)
            s = ''
            if now_diff < old_diff:
                s = 'step accepted'
                self.LM_damper = self.LM_damper / damper_adjustment_factor
                old_diff=now_diff
            else:
                s = 'cancelling'
                self.cancel_step()
                self.LM_damper = self.LM_damper * damper_adjustment_factor
            if abs(now_diff)<=error_crit:
                status = 'Converged - error criterium'
                if silent == False:
                    print 'Converged. (Error criterium) Sum of differences: %7.2e' %(now_diff)
                break
            if self.error_gradient <= gradient_crit:
                status = 'Stopped - gradient criterium'
                if silent == False:
                    print 'Stopped (Gradient criterium) Error gradient: %7.2e' %(self.error_gradient)
                break
            if self.singular_matrix == 1:
                status = 'Stopped - singular matrix'
                if silent == False:
                    print 'Stopped (Singular matrix)'
                break
            if self.stop_fit==1:
                break
        return status,self.variables
def fit ( self , rounds = 500 , error_crit = 0.00005 , gradient_crit = 1e-6 , step = 1E-8 , LM_damper = 0.00001 , damper_adjustment_factor = 2.0 , silent = False ) : if silent == False : print 'Fitting..' print 'start vars:' , self . variables self . step = step self . LM_damper = LM_damper if error_crit == None : y = [ ] for i in self . exp_data : y . append ( i [ 1 ] ) yrange = max ( y ) - min ( y ) error_crit = yrange / 1e6 print 'setting error_crit:' , error_crit old_diff = self . get_difference ( self . variables ) status = 'MI' count = 0 for x in range ( 1 , rounds ) : self . fit_LM ( ) if self . singular_matrix == 1 : status = 'Stopped - singular matrix' if silent == False : print 'Stopped (Singular matrix)' break now_diff = self . get_difference ( self . variables ) count += 1 self . callback ( difference = now_diff , variables = self . variables , count = count , fitter = self ) s = '' if now_diff < old_diff : s = 'step accepted' self . LM_damper = self . LM_damper / damper_adjustment_factor old_diff = now_diff else : s = 'cancelling' self . cancel_step ( ) self . LM_damper = self . LM_damper * damper_adjustment_factor if abs ( now_diff ) <= error_crit : status = 'Converged - error criterium' if silent == False : print 'Converged. (Error criterium) Sum of differences: %7.2e' % ( now_diff ) break if self . error_gradient <= gradient_crit : status = 'Stopped - gradient criterium' if silent == False : print 'Stopped (Gradient criterium) Error gradient: %7.2e' % ( self . error_gradient ) break if self . singular_matrix == 1 : status = 'Stopped - singular matrix' if silent == False : print 'Stopped (Singular matrix)' break if self . stop_fit == 1 : break return status , self . variables
Do 500 rounds of fitting, or until converged
Do 500 rounds of fitting or until converged
"""Do 500 rounds of fitting, or until converged""" # Set damper # Try to set error_crit using exp_data range #get y vals # Start iterating #print 'Step    Diff   LM_damper' # check for singluar matrix # Check convergence #print '%5d  %6.4f  %6.4e' %(x, now_diff, self.LM_damper), self.variables, s # check error criterium #print 'Diff: %5.3e, error_crit: %5.3e,| gradient: %5.3e, grad_crit: %5.3e' %(now_diff,error_crit,self.error_gradient,gradient_crit) # check gradient criterium # check for singluar matrix # Were we told to stop?
self
rounds
error_crit
gradient_crit
step
LM_damper
damper_adjustment_factor
silent