Пример #1
0
 def __init__(self, x, y, degree):
     Identifier.__init__(self, x, y)
     
     self.poly_fitter = PolyFitter(x, y, degree)
     self.i_points = self.poly_fitter.get_inflec_points()
     
     self.calculate_inflec_point(0)
Пример #2
0
class ITMIdentifier(Identifier):
    '''
    Identifies data with the inflectional tangent method.
    '''
    def __init__(self, x, y, degree):
        Identifier.__init__(self, x, y)
        
        self.poly_fitter = PolyFitter(x, y, degree)
        self.i_points = self.poly_fitter.get_inflec_points()
        
        self.calculate_inflec_point(0)
    
    @property    
    def t_x(self):
        ''' 
        x-values of the tangent.
        '''
        return numpy.arange(self.death_time, self.end_time, 0.1)
    
    @property    
    def t_y(self):
        '''
        y-values of the tangent.
        '''
        m = self.tangent_slope
        b = self.tangent_offset
        return m * self.t_x + b
    
    @property
    def tu(self):
        return self.death_time
    
    @property
    def tg(self):
        return self.end_time - self.death_time
        
    def calculate_inflec_point(self, num):
        '''
        Calculates the point of inflection.
        '''
        # Each tuple in i_points contains the x and y-value
        # plus the slope
        x, y, m = self.i_points[num]
        
        # tangential form: "mx + b = y"
        b = y - m*x
        self.death_time = -b/m
        self.tangent_slope = m
        self.tangent_offset = b
        self.split_point = self._calculate_split_point(x)
        
        i = numpy.nonzero(self.x > self.split_point)
        x, y = self.x[i], self.y[i]
        
        self.model_fitter = ModelFitter(x, y, modellib.exp_approach)
        mf = self.model_fitter
        self.height = mf.params['c']
        self.end_time = (self.height - b) / m
        
    def _calculate_split_point(self, begin):
        '''
        Calculate a useful point, where the tangent should stop
        and the exponential function begin.
        '''
        delta = 0.1
        x = self.poly_fitter.x
        y = self.poly_fitter.get_values(1)
        i = numpy.nonzero(x > begin)
        x, y = x[i], y[i]
        for x_val, y_val in izip(x, y):
            if abs(y_val - self.tangent_slope) > delta:
                return x_val
        
        
    def show_solution(self):
        m = self.tangent_slope
        b = self.tangent_offset
        print 'Tangent: {0}x + {1}'.format(m, b)
        print 'Tu: {0}'.format(self.tu)
        print 'Tg: {0}'.format(self.tg)
        print 'Tu/Tg: {0}'.format(self.tu/self.tg)
        print 'Tg/Tu: {0}'.format(self.tg/self.tu)
        self.plot_solution()
        
    def plot_solution(self):
        c = self.height
        plot(self.x, self.y, label='data')
        plot([self.x[0], self.x[-1]], [c, c], '--', label='limit')
        plot(self.t_x, self.t_y, label='tangent')
        legend()
        grid()
        show()