libpappsomspp
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 #include "../../exception/exceptionnotrecognized.h"
33 
34 using namespace pappso;
35 
37  : m_halfWindowSize(half_window_size)
38 {
39 }
41  const FilterMorphoWindowBase &other)
42  : m_halfWindowSize(other.m_halfWindowSize)
43 {
44 }
45 std::size_t
47 {
48  return m_halfWindowSize;
49 }
50 
53 {
55 
56  return *this;
57 }
58 
59 
60 Trace &
62 {
63 
64  qDebug() << " " << m_halfWindowSize << " data_points.size()"
65  << data_points.size();
66  if(m_halfWindowSize == 0)
67  return data_points;
68  Trace old_trace(data_points);
69  auto it = old_trace.begin();
70  auto itend = old_trace.end() - m_halfWindowSize - 1;
71  auto it_target = data_points.begin();
72 
73 
74  std::size_t loop_begin = 0;
75  while((it != itend) && (loop_begin < m_halfWindowSize))
76  {
77  // maxYDataPoint(it_begin, it + m_halfWindowSize + 1);
78  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
79  // << it->x << " " << m_halfWindowSize;
80  // it_target->x = it->x;
81  it_target->y =
82  getWindowValue(old_trace.begin(), it + m_halfWindowSize + 1);
83  it++;
84  it_target++;
85  loop_begin++;
86  }
87  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
88  while(it != itend)
89  {
90  // it_target->x = it->x;
91  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
92  // << it->x;
93  it_target->y =
95  it++;
96  it_target++;
97  }
98  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
99  while(it != old_trace.end())
100  {
101  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
102  // << it->x;
103  // it_target->x = it->x;
104  it_target->y = getWindowValue(it - m_halfWindowSize, old_trace.end());
105  it++;
106  it_target++;
107  }
108  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
109  // problem with move or swap : this lead to segmentation faults in some cases
110  // data_points = std::move(new_trace);
111  qDebug();
112  return data_points;
113 }
114 
115 FilterMorphoSum::FilterMorphoSum(std::size_t half_window_size)
116  : FilterMorphoWindowBase(half_window_size)
117 {
118 }
120  : FilterMorphoWindowBase(other.m_halfWindowSize)
121 {
122 }
123 
126 {
128 
129  return *this;
130 }
131 
132 double
134  std::vector<DataPoint>::const_iterator begin,
135  std::vector<DataPoint>::const_iterator end) const
136 {
137 
138  qDebug();
139  return sumYTrace(begin, end, 0);
140 }
141 
142 FilterMorphoMax::FilterMorphoMax(std::size_t half_window_size)
143  : FilterMorphoWindowBase(half_window_size)
144 {
145 }
147  : FilterMorphoWindowBase(other.m_halfWindowSize)
148 {
149 }
150 
153 {
155 
156  return *this;
157 }
158 
159 double
161  std::vector<DataPoint>::const_iterator begin,
162  std::vector<DataPoint>::const_iterator end) const
163 {
164 
165  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
166  return maxYDataPoint(begin, end)->y;
167 }
168 
169 std::size_t
171 {
172  return m_halfWindowSize;
173 }
174 
175 FilterMorphoMin::FilterMorphoMin(std::size_t half_window_size)
176  : FilterMorphoWindowBase(half_window_size)
177 {
178 }
180  : FilterMorphoWindowBase(other.m_halfWindowSize)
181 {
182 }
183 
186 {
188 
189  return *this;
190 }
191 
192 double
194  std::vector<DataPoint>::const_iterator begin,
195  std::vector<DataPoint>::const_iterator end) const
196 {
197  return minYDataPoint(begin, end)->y;
198 }
199 
200 std::size_t
202 {
203  return m_halfWindowSize;
204 }
205 
206 FilterMorphoMinMax::FilterMorphoMinMax(std::size_t half_window_size)
207  : m_filterMax(half_window_size), m_filterMin(half_window_size)
208 {
209 }
211  : m_filterMax(other.m_filterMax), m_filterMin(other.m_filterMin)
212 {
213 }
214 
217 {
218  m_filterMax = other.m_filterMax;
219  m_filterMin = other.m_filterMin;
220 
221  return *this;
222 }
223 
224 Trace &
226 {
227  qDebug();
228  m_filterMax.filter(data_points);
229  m_filterMin.filter(data_points);
230  qDebug();
231  return data_points;
232 }
233 std::size_t
235 {
236  return ((FilterMorphoMax)m_filterMax).getMaxHalfEdgeWindows();
237 }
238 
239 
240 FilterMorphoMaxMin::FilterMorphoMaxMin(std::size_t half_window_size)
241  : m_filterMin(half_window_size), m_filterMax(half_window_size)
242 {
243 }
245  : m_filterMin(other.m_filterMin), m_filterMax(other.m_filterMax)
246 {
247 }
248 
251 {
252  m_filterMin = other.m_filterMin;
253  m_filterMax = other.m_filterMax;
254 
255  return *this;
256 }
257 
258 Trace &
260 {
261  qDebug();
262  m_filterMin.filter(data_points);
263  m_filterMax.filter(data_points);
264  qDebug();
265  return data_points;
266 }
267 std::size_t
269 {
270  return ((FilterMorphoMax)m_filterMax).getMaxHalfEdgeWindows();
271 }
272 
273 FilterMorphoAntiSpike::FilterMorphoAntiSpike(std::size_t half_window_size)
274  : m_halfWindowSize(half_window_size)
275 {
276 }
277 
279  : m_halfWindowSize(other.m_halfWindowSize)
280 {
281 }
282 
283 void
285  const QString &strBuildParams)
286 {
287  //"antiSpike|2"
288  if(strBuildParams.startsWith("antiSpike|"))
289  {
290  QStringList params = strBuildParams.split("|").back().split(";");
291 
292  m_halfWindowSize = params.at(0).toUInt();
293  }
294  else
295  {
297  QString("building FilterMorphoAntiSpike from string %1 is not possible")
298  .arg(strBuildParams));
299  }
300 }
301 
302 QString
304 {
305  QString strCode = QString("antiSpike|%1").arg(m_halfWindowSize);
306 
307  return strCode;
308 }
309 
310 
313 {
315 
316  return *this;
317 }
318 
319 std::size_t
321 {
322  return m_halfWindowSize;
323 }
324 Trace &
326 {
327  if(m_halfWindowSize == 0)
328  return data_points;
329  Trace old_trace(data_points);
330  auto it = old_trace.begin();
331  auto it_target = data_points.begin();
332  auto itw = old_trace.begin();
333 
334  auto itend = old_trace.end() - m_halfWindowSize - 1;
335  // new_trace.reserve(data_points.size());
336 
337  while((it != old_trace.end()) &&
338  (std::distance(old_trace.begin(), it) < (int)m_halfWindowSize))
339  {
340  // no anti spike at the begining of the signal
341  it++;
342  it_target++;
343  }
344  while(it != itend)
345  {
346  auto itwend = it + m_halfWindowSize + 1;
347  itw = findDifferentYvalue(it - m_halfWindowSize, it + 1, 0);
348  if(itw == it)
349  {
350  itw = findDifferentYvalue(it + 1, itwend, 0);
351  if(itw == itwend)
352  {
353  it_target->y = 0;
354  }
355  }
356 
357  it++;
358  it_target++;
359  }
360 
361  return data_points;
362 }
363 
364 
365 FilterMorphoMedian::FilterMorphoMedian(std::size_t half_window_size)
366  : FilterMorphoWindowBase(half_window_size)
367 {
368 }
370  : FilterMorphoWindowBase(other.m_halfWindowSize)
371 {
372 }
373 
376 {
378 
379  return *this;
380 }
381 
382 double
384  std::vector<DataPoint>::const_iterator begin,
385  std::vector<DataPoint>::const_iterator end) const
386 {
387  return medianYTrace(begin, end);
388 }
389 
390 
391 FilterMorphoMean::FilterMorphoMean(std::size_t half_window_size)
392  : FilterMorphoWindowBase(half_window_size)
393 {
394 }
396  : FilterMorphoWindowBase(other.m_halfWindowSize)
397 {
398 }
399 
402 {
404 
405  return *this;
406 }
407 
408 std::size_t
410 {
411  return m_halfWindowSize;
412 }
413 
414 double
416  std::vector<DataPoint>::const_iterator begin,
417  std::vector<DataPoint>::const_iterator end) const
418 {
419  return meanYTrace(begin, end);
420 }
421 
422 
424  std::size_t median_half_window_size, std::size_t minmax_half_window_size)
425  : m_filterMorphoMedian(median_half_window_size),
426  m_filterMorphoMinMax(minmax_half_window_size)
427 {
428 }
429 
431  const FilterMorphoBackground &other)
432  : m_filterMorphoMedian(other.m_filterMorphoMedian),
433  m_filterMorphoMinMax(other.m_filterMorphoMinMax)
434 {
435 }
436 
439 {
442 
443  return *this;
444 }
445 
446 Trace &
448 {
449  m_filterMorphoMedian.filter(data_points);
450  m_filterMorphoMinMax.filter(data_points);
451 
452  // finally filter negative values
453  for(DataPoint &point : data_points)
454  {
455  if(point.y < 0)
456  {
457  point.y = 0;
458  }
459  }
460  return data_points;
461 }
462 const FilterMorphoMedian &
464 {
465  return m_filterMorphoMedian;
466 }
467 const FilterMorphoMinMax &
469 {
470  return m_filterMorphoMinMax;
471 }
excetion to use when an item type is not recognized
anti spike filter set to zero alone values inside the window
Definition: filtermorpho.h:158
FilterMorphoAntiSpike & operator=(const FilterMorphoAntiSpike &other)
std::size_t getHalfWindowSize() const
void buildFilterFromString(const QString &strBuildParams) override
build this filer using a string
QString toString() const override
FilterMorphoAntiSpike(std::size_t half_window_size)
Trace & filter(Trace &data_points) const override
compute background of a trace compute background noise on a trace
Definition: filtermorpho.h:221
Trace & filter(Trace &data_points) const override
FilterMorphoBackground(std::size_t median_half_window_size, std::size_t minmax_half_window_size)
FilterMorphoMedian m_filterMorphoMedian
Definition: filtermorpho.h:223
const FilterMorphoMedian & getFilterMorphoMedian() const
FilterMorphoBackground & operator=(const FilterMorphoBackground &other)
const FilterMorphoMinMax & getFilterMorphoMinMax() const
FilterMorphoMinMax m_filterMorphoMinMax
Definition: filtermorpho.h:224
transform the trace with the maximum of the minimum equivalent of the erode filter for pictures
Definition: filtermorpho.h:138
Trace & filter(Trace &data_points) const override
FilterMorphoMaxMin(std::size_t half_window_size)
FilterMorphoMax m_filterMax
Definition: filtermorpho.h:141
std::size_t getMaxMinHalfEdgeWindows() const
FilterMorphoMaxMin & operator=(const FilterMorphoMaxMin &other)
FilterMorphoMin m_filterMin
Definition: filtermorpho.h:140
transform the trace into its maximum over a window
Definition: filtermorpho.h:81
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
std::size_t getMaxHalfEdgeWindows() const
FilterMorphoMax(std::size_t half_window_size)
FilterMorphoMax & operator=(const FilterMorphoMax &other)
mean filter apply mean of y values inside the window : this results in a kind of smoothing
Definition: filtermorpho.h:202
FilterMorphoMean(std::size_t half_window_size)
FilterMorphoMean & operator=(const FilterMorphoMean &other)
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
std::size_t getMeanHalfEdgeWindows() const
median filter apply median of y values inside the window
Definition: filtermorpho.h:183
FilterMorphoMedian & operator=(const FilterMorphoMedian &other)
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
FilterMorphoMedian(std::size_t half_window_size)
transform the trace with the minimum of the maximum equivalent of the dilate filter for pictures
Definition: filtermorpho.h:118
FilterMorphoMax m_filterMax
Definition: filtermorpho.h:120
FilterMorphoMin m_filterMin
Definition: filtermorpho.h:121
FilterMorphoMinMax(std::size_t half_window_size)
std::size_t getMinMaxHalfEdgeWindows() const
Trace & filter(Trace &data_points) const override
FilterMorphoMinMax & operator=(const FilterMorphoMinMax &other)
transform the trace into its minimum over a window
Definition: filtermorpho.h:99
std::size_t getMinHalfEdgeWindows() const
FilterMorphoMin(std::size_t half_window_size)
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
FilterMorphoMin & operator=(const FilterMorphoMin &other)
FilterMorphoSum & operator=(const FilterMorphoSum &other)
FilterMorphoSum(std::size_t half_window_size)
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
base class that apply a signal treatment based on a window
Definition: filtermorpho.h:41
virtual Trace & filter(Trace &data_points) const override
virtual double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const =0
FilterMorphoWindowBase(std::size_t half_window_size)
virtual std::size_t getHalfWindowSize() const
FilterMorphoWindowBase & operator=(const FilterMorphoWindowBase &other)
A simple container of DataPoint instances.
Definition: trace.h:132
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
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:88
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:139
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:223
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:212
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:203
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:117