source: fact/tools/pyscripts/sandbox/dneise/spike_studies/drs_spikes_2d.py@ 20115

Last change on this file since 20115 was 13632, checked in by neise, 13 years ago
initial commit of my tests and so on
File size: 5.0 KB
Line 
1class DRSSpikes_2D(object):
2 """ remove spikes (single or double false readings) from DRS4 data
3 Strategy:
4 * filter the data, removing the signal, thus spike(s) are clearly visible
5 * search single and double spikes
6 * replace the spike by a value derived from the neighbors
7
8 """
9
10 def __init__(self, threshold=7.,
11 single_pattern=np.array( [-0.5, 1.0, -0.5]) ,
12 double_pattern=np.array([-1., 1., 1., -1.]),
13 user_action=lambda self, data: None,
14 debug = False):
15 """ initialize spike filter
16 template_single: template of a single slice spike
17 template_double: template of a two slice spike
18 """
19
20 self.threshold = threshold
21 self.single_pattern = single_pattern * threshold
22 self.double_pattern = double_pattern * threshold
23
24 self.remove_signal = fir.RemoveSignal()
25
26 self.user_action = user_action
27 self.debug = debug
28
29 def __call__(self, data):
30 # shortcuts
31 row, col = data.shape
32 thr = self.threshold
33
34 # these are: lists, which will contain positiones of spikes
35 # lets see if this is feasible
36 self.singles = []
37 self.doubles = []
38 singles = self.singles
39 doubles = self.doubles
40
41 # indi means indicator, i.e. a filter output, which indicates, where spikes
42 # are positioned in the unfiltered data
43 # indi is delayed w.r.t. data by 1
44 self.indi = self.remove_signal(data)
45 indi = self.indi
46
47 # cand (candidates), is a tuple of two equal length np.arrays
48 # each pair of array elements can be understood as coordinates, pointing out
49 # where the condition was fullfilled in indi.
50 # e.g. in pixel = cand[0][0] around slice = cand[1][0]
51 # there is probably a spike
52 #
53 # for double spikes, two neighboring slices fulfill the condition
54 # which lead to something like:
55 # cand =( array([ ... 3, 3, ... ]) , array([ ... 102, 103 ...]) )
56 cand = np.where(indi[:,1:-2] > thr)
57 self.cand = cand
58 # in order to verify, that the candidate is really a single or double
59 # spike, we compare the spike with a 3 or 4 slices pattern.
60 # therefor we want to slice out 4 slices out of indi, where ever the
61 # condition was fullfilled
62 #
63 # note: since indi was sliced in the np.where statement,
64 # the resulting cand coordinates are reduced by 1 in the slice coordinate
65 # this is actually what we want, since a spike has a distinctive low-high-low
66 # pattern in the indicator. So we *want* the indicator slices to be shifted 1
67 # to the left
68 # and in addition, by pure chance, the coordinates in cand[1] point directly
69 # to the real spike in data, since indi was delayed by one anyway.
70 cand_slices = np.empty( (len(cand[0]), 4), dtype=np.int )
71 self.cand_slices = cand_slices
72 for i in range(4):
73 cand_slices[i] = indi[ (cand[0], cand[1]+i )
74
75 # search for single spikes
76 sp = self.single_pattern * np.sign( self.single_pattern )
77 for i, can in enumerate(cand_slices[:-1]):
78 can *= np.sign(sp)
79 if np.all( can > sp):
80 singles.append( (cand[0][i],can[1][i]) )
81
82 # I guess in principle it is possible, that a candidate looks like a
83 # single and like a double, ... nut with the current patters
84 # but in case one changes the patterns ... then it might happen.
85 # In addition the treatment of double spikes is maybed not smart:
86 # In case both parts of a double spike fulfill the 1st condition
87 # only the first candidate will fulfill the 2nd condition
88 # In case only the first part fulfilled the 1st conition, then
89 # we are fine
90 # In case only the second part triggered the first time, then
91 # we sliced out the wrong piece and it wouldn't fulfull the 2nd anyway.
92 #
93 # This means, in case there are neighboring hits in cand,
94 # The 2nd neighbor will never fulfill the 2nd condition.
95
96 # search for double spikes
97 dp = self.double_pattern * np.sign( self.double_pattern )
98 for i, can in enumerate( cand_slices ):
99 can *= np.sign(dp)
100 if np.all(can > dp):
101 doubles.append( (cand[0][i],can[1][i]) )
102
103 self.user_action(self, data)
104
105 data = self.remove_single_spikes(singles, data)
106 data = self.remove_double_spikes(doubles, data)
107 return data
108
109 def remove_single_spikes(self, singles, data):
110 for spike in singles:
111 data[spike[0],spike[1]] = (data[spike[0],spike[1]-1] + data[spike[0],spike[1]+1]) / 2.
112 return data
113
114 def remove_double_spikes(self, doubles, data):
115 for spike in doubles:
116 data[spike[0],spike[1]:spike[1]+2] = (data[spike[0],spike[1]-1] + data[spike[0],spike[1]+2]) / 2.
117 return data
118
119
120
Note: See TracBrowser for help on using the repository browser.