source: fact/tools/pyscripts/pyfact/drs_spikes.py@ 13012

Last change on this file since 13012 was 13012, checked in by lusterma, 13 years ago
improved algorithm, now only loops previously defined candidates, now uses all function and sign of the pattern
  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/python
2#
3# Werner Lustermann
4# ETH Zurich
5#
6import numpy as np
7
8import fir_filter as fir
9
10class DRSSpikes(object):
11 """ remove spikes (single or double false readings) from DRS4 data
12 Strategy:
13 * filter the data, removing the signal, thus spike(s) are clearly visible
14 * search single and double spikes
15 * replace the spike by a value derived from the neighbors
16
17 """
18
19 def __init__(self, threshold=10.,
20 single_pattern=np.array( [-0.5, 1.0, -0.5]) ,
21 double_pattern=np.array([-1., 1., 1., -1.]),
22 debug = False):
23 """ initialize spike filter
24 template_single: template of a single slice spike
25 template_double: template of a two slice spike
26
27 """
28
29 self.threshold = threshold
30 self.single_pattern = single_pattern * threshold
31 self.double_pattern = double_pattern * threshold
32
33 self.remove_signal = fir.RemoveSignal()
34 self.debug = debug
35
36 def __call__(self, data):
37
38 self.row, self.col = data.shape
39 indicator = self.remove_signal(data)
40 a = indicator.flatten()
41 singles = []
42 doubles = []
43
44 cc = candidates = np.where(a > self.threshold)
45 print 'candidates: ', candidates[0]-1
46 #: find single spikes
47 p = self.single_pattern * np.sign( self.single_pattern )
48 for i, can in enumerate( zip(a[cc[0]-1], a[cc[0]], a[cc[0]+1]) ):
49 print 'can : p', can, p
50 can = can * np.sign(self.single_pattern)
51 if all(can > p):
52 singles.append(candidates[0][i] - 1)
53
54 #: find double spikes
55 p = self.double_pattern * np.sign( self.double_pattern )
56 for i, can in enumerate( zip(a[cc[0]-1], a[cc[0]],
57 a[cc[0]+1], a[cc[0]+2]) ):
58 print 'can : p', can, p
59 can = can * np.sign(self.double_pattern)
60 if all(can > p):
61 doubles.append(candidates[0][i] - 1)
62
63 if self.debug:
64 print 'singles: ', singles
65 print 'doubles: ', doubles
66
67 data = self.remove_single_spikes(singles, data)
68 data = self.remove_double_spikes(doubles, data)
69 return data
70
71 def remove_single_spikes(self, singles, data):
72 data = data.flatten()
73 for spike in singles:
74 data[spike] = (data[spike-1] + data[spike+1]) / 2.
75 return data.reshape(self.row, self.col)
76
77 def remove_double_spikes(self, doubles, data):
78 data = data.flatten()
79 for spike in doubles:
80 data[spike:spike+2] = (data[spike-1] + data[spike+2]) / 2.
81 return data.reshape(self.row, self.col)
82
83
84def _test():
85
86 a = np.ones((3,12)) * 3.
87 a[0,3] = 7.
88 a[1,7] = 14.
89 a[1,8] = 14.
90 a[2,4] = 50.
91 a[2,5] = 45.
92 a[2,8] = 20.
93
94 print a
95
96 SpikeRemover = DRSSpikes(3., debug=True)
97 print 'single spike pattern ', SpikeRemover.single_pattern
98 print 'double spike pattern ', SpikeRemover.double_pattern
99 afilt = SpikeRemover(a)
100 print afilt
101
102if __name__ == '__main__':
103 """ test the class """
104 _test()
105
106
Note: See TracBrowser for help on using the repository browser.