pappsomspp
Library for mass spectrometry
filtermorpho.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/filers/filtermorpho.cpp
3  * \date 02/05/2019
4  * \author Olivier Langella
5  * \brief collection of morphological filters
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.fr>.
10  *
11  * This file is part of the PAPPSOms++ library.
12  *
13  * PAPPSOms++ is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * PAPPSOms++ is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25  *
26  ******************************************************************************/
27 
28 #include "filtermorpho.h"
29 #include "../../trace/trace.h"
30 #include <QDebug>
31 #include "../../exception/exceptionoutofrange.h"
32 
33 using namespace pappso;
34 
35 FilterMorphoWindowBase::FilterMorphoWindowBase(std::size_t half_window_size)
36  : m_half_window_size(half_window_size)
37 {
38 }
40  const FilterMorphoWindowBase &other)
41  : m_half_window_size(other.m_half_window_size)
42 {
43 }
44 std::size_t
46 {
47  return m_half_window_size;
48 }
50 FilterMorphoWindowBase::filter(Trace &data_points) const
51 {
52 
53  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
54  << m_half_window_size << " data_points.size()" << data_points.size();
55  if(m_half_window_size == 0)
56  return data_points;
57  Trace old_trace(data_points);
58  auto it = old_trace.begin();
59  auto itend = old_trace.end() - m_half_window_size - 1;
60  auto it_target = data_points.begin();
61 
62 
63  std::size_t loop_begin = 0;
64  while((it != itend) && (loop_begin < m_half_window_size))
65  {
66  // maxYDataPoint(it_begin, it + m_half_window_size + 1);
67  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
68  // << it->x << " " << m_half_window_size;
69  // it_target->x = it->x;
70  it_target->y =
71  getWindowValue(old_trace.begin(), it + m_half_window_size + 1);
72  it++;
73  it_target++;
74  loop_begin++;
75  }
76  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
77  while(it != itend)
78  {
79  // it_target->x = it->x;
80  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
81  // << it->x;
82  it_target->y =
84  it++;
85  it_target++;
86  }
87  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
88  while(it != old_trace.end())
89  {
90  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
91  // << it->x;
92  // it_target->x = it->x;
93  it_target->y = getWindowValue(it - m_half_window_size, old_trace.end());
94  it++;
95  it_target++;
96  }
97  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
98  // problem with move or swap : this lead to segmentation faults in some cases
99  // data_points = std::move(new_trace);
100  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
101  return data_points;
102 }
103 
104 FilterMorphoSum::FilterMorphoSum(std::size_t half_window_size)
105  : FilterMorphoWindowBase(half_window_size)
106 {
107 }
109  : FilterMorphoWindowBase(other.m_half_window_size)
110 {
111 }
112 double
114  std::vector<DataPoint>::const_iterator begin,
115  std::vector<DataPoint>::const_iterator end) const
116 {
117 
118  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
119  return sumYTrace(begin, end, 0);
120 }
121 
122 FilterMorphoMax::FilterMorphoMax(std::size_t half_window_size)
123  : FilterMorphoWindowBase(half_window_size)
124 {
125 }
127  : FilterMorphoWindowBase(other.m_half_window_size)
128 {
129 }
130 double
132  std::vector<DataPoint>::const_iterator begin,
133  std::vector<DataPoint>::const_iterator end) const
134 {
135 
136  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
137  return maxYDataPoint(begin, end)->y;
138 }
139 
140 std::size_t
142 {
143  return m_half_window_size;
144 }
145 
146 FilterMorphoMin::FilterMorphoMin(std::size_t half_window_size)
147  : FilterMorphoWindowBase(half_window_size)
148 {
149 }
151  : FilterMorphoWindowBase(other.m_half_window_size)
152 {
153 }
154 
155 
156 double
158  std::vector<DataPoint>::const_iterator begin,
159  std::vector<DataPoint>::const_iterator end) const
160 {
161  return minYDataPoint(begin, end)->y;
162 }
163 
164 std::size_t
166 {
167  return m_half_window_size;
168 }
169 
170 FilterMorphoMinMax::FilterMorphoMinMax(std::size_t half_window_size)
171  : m_filter_max(half_window_size), m_filter_min(half_window_size)
172 {
173 }
175  : m_filter_max(other.m_filter_max), m_filter_min(other.m_filter_min)
176 {
177 }
179 FilterMorphoMinMax::filter(Trace &data_points) const
180 {
181  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
182  m_filter_max.filter(data_points);
183  m_filter_min.filter(data_points);
184  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
185  return data_points;
186 }
187 std::size_t
189 {
190  return ((FilterMorphoMax)m_filter_max).getMaxHalfEdgeWindows();
191 }
192 
193 
194 FilterMorphoMaxMin::FilterMorphoMaxMin(std::size_t half_window_size)
195  : m_filter_min(half_window_size), m_filter_max(half_window_size)
196 {
197 }
199  : m_filter_min(other.m_filter_min), m_filter_max(other.m_filter_max)
200 {
201 }
203 FilterMorphoMaxMin::filter(Trace &data_points) const
204 {
205  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
206  m_filter_min.filter(data_points);
207  m_filter_max.filter(data_points);
208  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
209  return data_points;
210 }
211 std::size_t
213 {
214  return ((FilterMorphoMax)m_filter_max).getMaxHalfEdgeWindows();
215 }
216 
217 FilterMorphoAntiSpike::FilterMorphoAntiSpike(std::size_t half_window_size)
218  : m_half_window_size(half_window_size)
219 {
220 }
222  : m_half_window_size(other.m_half_window_size)
223 {
224 }
225 
226 std::size_t
228 {
229  return m_half_window_size;
230 }
232 FilterMorphoAntiSpike::filter(Trace &data_points) const
233 {
234  if(m_half_window_size == 0)
235  return data_points;
236  Trace old_trace(data_points);
237  auto it = old_trace.begin();
238  auto it_target = data_points.begin();
239  auto itw = old_trace.begin();
240 
241  auto itend = old_trace.end() - m_half_window_size - 1;
242  // new_trace.reserve(data_points.size());
243 
244  while((it != old_trace.end()) &&
245  (std::distance(old_trace.begin(), it) < (int)m_half_window_size))
246  {
247  // no anti spike at the begining of the signal
248  it++;
249  it_target++;
250  }
251  while(it != itend)
252  {
253  auto itwend = it + m_half_window_size + 1;
254  itw = findDifferentYvalue(it - m_half_window_size, it + 1, 0);
255  if(itw == it)
256  {
257  itw = findDifferentYvalue(it + 1, itwend, 0);
258  if(itw == itwend)
259  {
260  it_target->y = 0;
261  }
262  }
263 
264  it++;
265  it_target++;
266  }
267 
268  return data_points;
269 }
270 
271 
272 FilterMorphoMedian::FilterMorphoMedian(std::size_t half_window_size)
273  : FilterMorphoWindowBase(half_window_size)
274 {
275 }
277  : FilterMorphoWindowBase(other.m_half_window_size)
278 {
279 }
280 double
282  std::vector<DataPoint>::const_iterator begin,
283  std::vector<DataPoint>::const_iterator end) const
284 {
285  return medianYTrace(begin, end);
286 }
287 
288 
289 FilterMorphoMean::FilterMorphoMean(std::size_t half_window_size)
290  : FilterMorphoWindowBase(half_window_size)
291 {
292 }
294  : FilterMorphoWindowBase(other.m_half_window_size)
295 {
296 }
297 std::size_t
299 {
300  return m_half_window_size;
301 }
302 
303 double
305  std::vector<DataPoint>::const_iterator begin,
306  std::vector<DataPoint>::const_iterator end) const
307 {
308  return meanYTrace(begin, end);
309 }
310 
311 
313  std::size_t median_half_window_size, std::size_t minmax_half_window_size)
314  : m_filter_morpho_median(median_half_window_size),
315  m_filter_morpho_minmax(minmax_half_window_size)
316 {
317 }
318 
320  const FilterMorphoBackground &other)
321  : m_filter_morpho_median(other.m_filter_morpho_median),
322  m_filter_morpho_minmax(other.m_filter_morpho_minmax)
323 {
324 }
326 FilterMorphoBackground::filter(Trace &data_points) const
327 {
328  m_filter_morpho_median.filter(data_points);
329  m_filter_morpho_minmax.filter(data_points);
330 
331  // finally filter negative values
332  for(DataPoint &point : data_points)
333  {
334  if(point.y < 0)
335  {
336  point.y = 0;
337  }
338  }
339  return data_points;
340 }
343 {
344  return m_filter_morpho_median;
345 }
348 {
349  return m_filter_morpho_minmax;
350 }
pappso::FilterMorphoMinMax::FilterMorphoMinMax
FilterMorphoMinMax(std::size_t half_window_size)
Definition: filtermorpho.cpp:169
pappso::FilterMorphoWindowBase::FilterMorphoWindowBase
FilterMorphoWindowBase(std::size_t half_window_size)
Definition: filtermorpho.cpp:34
pappso::FilterMorphoMean::FilterMorphoMean
FilterMorphoMean(std::size_t half_window_size)
Definition: filtermorpho.cpp:288
pappso::FilterMorphoSum::getWindowValue
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
Definition: filtermorpho.cpp:112
pappso::FilterMorphoWindowBase::getWindowValue
virtual double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const =0
pappso::FilterMorphoMaxMin::FilterMorphoMaxMin
FilterMorphoMaxMin(std::size_t half_window_size)
Definition: filtermorpho.cpp:193
pappso::FilterMorphoBackground::filter
Trace & filter(Trace &data_points) const override
Definition: filtermorpho.cpp:325
pappso::FilterMorphoMedian
median filter apply median of y values inside the window
Definition: filtermorpho.h:177
pappso::FilterMorphoWindowBase
base class that apply a signal treatment based on a window
Definition: filtermorpho.h:56
pappso::FilterMorphoMinMax::m_filter_max
FilterMorphoMax m_filter_max
Definition: filtermorpho.h:126
pappso::FilterMorphoSum::FilterMorphoSum
FilterMorphoSum(std::size_t half_window_size)
Definition: filtermorpho.cpp:103
pappso::FilterMorphoAntiSpike::FilterMorphoAntiSpike
FilterMorphoAntiSpike(std::size_t half_window_size)
Definition: filtermorpho.cpp:216
pappso
Definition: aa.cpp:38
pappso::FilterMorphoMax::getWindowValue
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
Definition: filtermorpho.cpp:130
pappso::FilterMorphoBackground::m_filter_morpho_minmax
FilterMorphoMinMax m_filter_morpho_minmax
Definition: filtermorpho.h:215
pappso::FilterMorphoWindowBase::filter
virtual Trace & filter(Trace &data_points) const override
Definition: filtermorpho.cpp:49
pappso::FilterMorphoMedian::FilterMorphoMedian
FilterMorphoMedian(std::size_t half_window_size)
Definition: filtermorpho.cpp:271
pappso::FilterMorphoMin
transform the trace into its minimum over a window
Definition: filtermorpho.h:106
pappso::DataPoint
Definition: datapoint.h:20
pappso::FilterMorphoMax
transform the trace into its maximum over a window
Definition: filtermorpho.h:90
pappso::FilterMorphoMin::getWindowValue
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
Definition: filtermorpho.cpp:156
pappso::FilterMorphoAntiSpike::getHalfWindowSize
std::size_t getHalfWindowSize() const
Definition: filtermorpho.cpp:226
pappso::FilterMorphoMean::getWindowValue
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
Definition: filtermorpho.cpp:303
pappso::FilterMorphoMinMax::filter
Trace & filter(Trace &data_points) const override
Definition: filtermorpho.cpp:178
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:125
pappso::sumYTrace
double sumYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double init)
calculate the sum of y value of a trace
Definition: trace.cpp:181
pappso::FilterMorphoMedian::getWindowValue
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
Definition: filtermorpho.cpp:280
pappso::FilterMorphoWindowBase::getHalfWindowSize
virtual std::size_t getHalfWindowSize() const
Definition: filtermorpho.cpp:44
pappso::FilterMorphoMax::getMaxHalfEdgeWindows
std::size_t getMaxHalfEdgeWindows() const
Definition: filtermorpho.cpp:140
pappso::FilterMorphoMin::getMinHalfEdgeWindows
std::size_t getMinHalfEdgeWindows() const
Definition: filtermorpho.cpp:164
pappso::FilterMorphoMin::FilterMorphoMin
FilterMorphoMin(std::size_t half_window_size)
Definition: filtermorpho.cpp:145
pappso::maxYDataPoint
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:126
pappso::FilterMorphoMaxMin::filter
Trace & filter(Trace &data_points) const override
Definition: filtermorpho.cpp:202
pappso::FilterMorphoMaxMin::m_filter_min
FilterMorphoMin m_filter_min
Definition: filtermorpho.h:144
pappso::FilterMorphoMax::FilterMorphoMax
FilterMorphoMax(std::size_t half_window_size)
Definition: filtermorpho.cpp:121
pappso::FilterMorphoMinMax::getMinMaxHalfEdgeWindows
std::size_t getMinMaxHalfEdgeWindows() const
Definition: filtermorpho.cpp:187
pappso::FilterMorphoMean
mean filter apply mean of y values inside the window : this results in a kind of smoothing
Definition: filtermorpho.h:194
pappso::findDifferentYvalue
std::vector< DataPoint >::iterator findDifferentYvalue(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &y_value)
find the first element in which Y is different of value
Definition: trace.cpp:86
pappso::FilterMorphoBackground::m_filter_morpho_median
FilterMorphoMedian m_filter_morpho_median
Definition: filtermorpho.h:214
pappso::FilterMorphoMinMax
transform the trace with the minimum of the maximum equivalent of the dilate filter for pictures
Definition: filtermorpho.h:123
filtermorpho.h
pappso::FilterMorphoAntiSpike::m_half_window_size
std::size_t m_half_window_size
Definition: filtermorpho.h:162
pappso::FilterMorphoBackground::FilterMorphoBackground
FilterMorphoBackground(std::size_t median_half_window_size, std::size_t minmax_half_window_size)
Definition: filtermorpho.cpp:311
pappso::FilterMorphoMaxMin::getMaxMinHalfEdgeWindows
std::size_t getMaxMinHalfEdgeWindows() const
Definition: filtermorpho.cpp:211
pappso::FilterMorphoAntiSpike::filter
Trace & filter(Trace &data_points) const override
Definition: filtermorpho.cpp:231
pappso::FilterMorphoWindowBase::m_half_window_size
std::size_t m_half_window_size
Definition: filtermorpho.h:59
pappso::FilterMorphoSum
test purpose
Definition: filtermorpho.h:76
pappso::medianYTrace
double medianYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the median of y value of a trace
Definition: trace.cpp:201
pappso::FilterMorphoBackground::getFilterMorphoMedian
const FilterMorphoMedian & getFilterMorphoMedian() const
Definition: filtermorpho.cpp:341
pappso::meanYTrace
double meanYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
calculate the mean of y value of a trace
Definition: trace.cpp:190
pappso::FilterMorphoMean::getMeanHalfEdgeWindows
std::size_t getMeanHalfEdgeWindows() const
Definition: filtermorpho.cpp:297
pappso::FilterMorphoMaxMin::m_filter_max
FilterMorphoMax m_filter_max
Definition: filtermorpho.h:145
pappso::FilterMorphoMinMax::m_filter_min
FilterMorphoMin m_filter_min
Definition: filtermorpho.h:127
pappso::minYDataPoint
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
find the element with the smallest Y value (intensity)
Definition: trace.cpp:115
pappso::FilterMorphoMaxMin
transform the trace with the maximum of the minimum equivalent of the erode filter for pictures
Definition: filtermorpho.h:141
pappso::FilterMorphoBackground
compute background of a trace compute background noise on a trace
Definition: filtermorpho.h:211
pappso::FilterMorphoBackground::getFilterMorphoMinMax
const FilterMorphoMinMax & getFilterMorphoMinMax() const
Definition: filtermorpho.cpp:346
pappso::FilterMorphoAntiSpike
anti spike filter set to zero alone values inside the window
Definition: filtermorpho.h:159