Changeset 12981


Ignore:
Timestamp:
03/01/12 12:32:48 (13 years ago)
Author:
neise
Message:
included test functions in __main__ and debugged a lot
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/pyscripts/pyfact/extractor.py

    r12952 r12981  
    1  #!/usr/bin/python
     1#!/usr/bin/python
    22#
    33# Dominik Neise, Werner Lustermann
     
    66import numpy as np
    77import matplotlib.pyplot as plt
    8 #import * from fir_filter
     8from generator import *
     9from fir_filter import *
    910
    1011class GlobalMaxFinder(object):
     
    2324       
    2425    def __call__(self, data):
    25         time = np.argmax( data[ : , self.min:self.max ], 1)
    26         amplitude = np.max( data[ : , self.min:self.max], 1)
    27         return amplitude, time
     26        if data.ndim > 1:
     27            time = np.argmax( data[ : , self.min:self.max ], 1)
     28            amplitude = np.max( data[ : , self.min:self.max], 1)
     29        else:
     30            time = np.argmax( data[self.min:self.max])
     31            amplitude = np.max( data[self.min:self.max])
     32        return amplitude, time+self.min
    2833
    2934    def __str__(self):
    3035        s = self.name + '\n'
    3136        s += 'window:\n'
    32         s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')\n'
     37        s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')'
    3338        return s
     39       
     40    def test(self):
     41        pass
    3442
    3543
     
    4957        for pixel in range( data.shape[0] ):
    5058            integral[pixel] = data[pixel, self.min:self.max].sum()
    51         return integtral
     59        return integral
    5260
    5361    def __str__(self):
    5462        s = self.name + '\n'
    5563        s += 'window:\n'
    56         s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')\n'
     64        s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')'
    5765        return s   
    5866
     
    7482        for pix_data in data:
    7583            hits = []
    76             for i in range( data.shape[1] ):
    77                 if ( slope > 0 ):
     84            for i in range( data.shape[1]-1 ):
     85                if ( self.slope > 0 ):
    7886                    if ( pix_data[i] > 0 ):
    7987                        continue
     
    99107
    100108
     109def plotter(signal, text):
     110    x=range(len(signal))
     111    ax = plt.plot(x, signal, 'b.', label='signal')
     112    plt.title(text)
     113    plt.xlabel('sample')
     114    plt.legend()
     115    plt.grid(True)
     116    plt.show()
     117   
     118def histplotter(signal, text):
     119    plt.xlabel('time of zero crossing')
     120    plt.title(text)
     121    plt.axis([0, 300,0,1440])
     122    plt.grid(True)
     123    n, bins, patches = plt.hist(signal, 3000, facecolor='r', alpha=0.75)
     124    plt.show()
     125
    101126
    102127if __name__ == '__main__':
    103128    """ test the extractors """
     129    sg = SignalGenerator()
     130    pulse_str = 'len 300 bsl -0.5 noise 0.5 triangle 10 65 8 100'
     131    pulse = sg(pulse_str)
     132    event = []
     133    for i in range(1440):
     134        event.append(sg(pulse_str))
     135    event = np.array(event)
     136    print 'test event with 1000 pixel generated, like this:'
     137    print pulse_str
     138    print
    104139   
    105     gmf = GlobalMaxFinder((12,40))
     140    # Test GlobalMaxFinder
     141    gmf = GlobalMaxFinder(30,250)
    106142    print gmf
    107     fwi = FixedWindowIntegrator(1,3)
     143    amplitude, time = gmf(event)
     144    if abs(amplitude.mean() - 10) < 0.5:
     145        print "Test 1: OK GlobalMaxFinder found amplitude correctly", amplitude.mean()
     146    if abs(time.mean() - 65) < 2:
     147        print "Test 1: OK GlobalMaxFinder found time correctly", time.mean()
     148    else:
     149        print "BAD: time mean:", time.mean()
     150   
     151    print
     152   
     153    # Test FixedWindowIntegrator
     154    fwi = FixedWindowIntegrator(50,200)
    108155    print fwi
     156    integral = fwi(event)
     157    #value of integral should be: 150*bsl + 8*10/2 + 100*10/2 = 465
     158    if abs( integral.mean() - 465) < 2:
     159        print "Test 2: OK FixedWindowIntegrator found integral correctly", integral.mean()
     160    else:
     161        print "Test 2:  X FixedWindowIntegrator integral.mean failed:", integral.mean()
     162   
     163    print
     164    cfd = CFD()
     165    sa = SlidingAverage(8)
     166    print sa
     167    cfd_out = sa(event)
     168    cfd_out = cfd(cfd_out   )
     169    cfd_out = sa(cfd_out)
    109170    zx = ZeroXing()
    110171    print zx
     172    list_of_list_of_times = zx(cfd_out)
     173    times = []
     174    for list_of_times in list_of_list_of_times:
     175        times.extend(list_of_times)
     176    times = np.array(times)
     177   
     178    hist,bins = np.histogram(times,3000,(0,300))
     179    most_probable_time = np.argmax(hist)
     180    print 'most probable time of zero-crossing', most_probable_time/10.
     181    print 'this includes filter delays ... for average filter setting 8 this turns out to be 78.8 most of the time'
     182   
     183    #histplotter(times.flatten(), 'times.flatten()')
     184    #plotter(cfd_out[0], 'cfd_out')
     185    #plotter (pulse, pulse_str)
     186   
     187
     188
Note: See TracChangeset for help on using the changeset viewer.