libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
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>
33
34using namespace pappso;
35
37 : m_halfWindowSize(half_window_size)
38{
39}
44std::size_t
49
57
58void
59pappso::FilterMorphoWindowBase::buildFilterFromString( [[maybe_unused]] const QString &strBuildParams)
60{
61}
62
63QString
65{
66 return "nose";
67}
68
69QString
71{
72 return "nose";
73}
74
75
76Trace &
78{
79
80 qDebug() << " " << m_halfWindowSize << " data_points.size()" << data_points.size();
81 if(m_halfWindowSize == 0)
82 return data_points;
83 if(data_points.size() <= m_halfWindowSize)
84 return data_points;
85 Trace old_trace(data_points);
86 auto it = old_trace.begin();
87 auto itend = old_trace.end() - m_halfWindowSize - 1;
88 auto it_target = data_points.begin();
89
90
91 std::size_t loop_begin = 0;
92 while((it != itend) && (loop_begin < m_halfWindowSize))
93 {
94 // maxYDataPoint(it_begin, it + m_halfWindowSize + 1);
95 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
96 // << it->x << " " << m_halfWindowSize;
97 // it_target->x = it->x;
98 it_target->y = getWindowValue(old_trace.begin(), it + m_halfWindowSize + 1);
99 it++;
100 it_target++;
101 loop_begin++;
102 }
103 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
104 while(it != itend)
105 {
106 // it_target->x = it->x;
107 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
108 // << it->x;
109 it_target->y = getWindowValue(it - m_halfWindowSize, it + m_halfWindowSize + 1);
110 it++;
111 it_target++;
112 }
113 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
114 while(it != old_trace.end())
115 {
116 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
117 // << it->x;
118 // it_target->x = it->x;
119 it_target->y = getWindowValue(it - m_halfWindowSize, old_trace.end());
120 it++;
121 it_target++;
122 }
123 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
124 // problem with move or swap : this lead to segmentation faults in some cases
125 // data_points = std::move(new_trace);
126 qDebug();
127 return data_points;
128}
129
130FilterMorphoSum::FilterMorphoSum(std::size_t half_window_size)
131 : FilterMorphoWindowBase(half_window_size)
132{
133}
138
141{
143
144 return *this;
145}
146
147double
148FilterMorphoSum::getWindowValue(std::vector<DataPoint>::const_iterator begin,
149 std::vector<DataPoint>::const_iterator end) const
150{
151
152 qDebug();
153 return sumYTrace(begin, end, 0);
154}
155
156FilterMorphoMax::FilterMorphoMax(std::size_t half_window_size)
157 : FilterMorphoWindowBase(half_window_size)
158{
159}
164
167{
169
170 return *this;
171}
172
173double
174FilterMorphoMax::getWindowValue(std::vector<DataPoint>::const_iterator begin,
175 std::vector<DataPoint>::const_iterator end) const
176{
177
178 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
179 return maxYDataPoint(begin, end)->y;
180}
181
182FilterMorphoMin::FilterMorphoMin(std::size_t half_window_size)
183 : FilterMorphoWindowBase(half_window_size)
184{
185}
190
193{
195
196 return *this;
197}
198
199double
200FilterMorphoMin::getWindowValue(std::vector<DataPoint>::const_iterator begin,
201 std::vector<DataPoint>::const_iterator end) const
202{
203 return minYDataPoint(begin, end)->y;
204}
205
206FilterMorphoMinMax::FilterMorphoMinMax(std::size_t half_window_size)
207 : m_filterMax(half_window_size), m_filterMin(half_window_size)
208{
209}
214
217{
218 m_filterMax = other.m_filterMax;
219 m_filterMin = other.m_filterMin;
220
221 return *this;
222}
223
224Trace &
226{
227 qDebug();
228 m_filterMax.filter(data_points);
229 m_filterMin.filter(data_points);
230 qDebug();
231 return data_points;
232}
233std::size_t
235{
236 return m_filterMax.getHalfWindowSize();
237}
238
239
240FilterMorphoMaxMin::FilterMorphoMaxMin(std::size_t half_window_size)
241 : m_filterMin(half_window_size), m_filterMax(half_window_size)
242{
243}
248
251{
252 m_filterMin = other.m_filterMin;
253 m_filterMax = other.m_filterMax;
254
255 return *this;
256}
257
258Trace &
260{
261 qDebug();
262 m_filterMin.filter(data_points);
263 m_filterMax.filter(data_points);
264 qDebug();
265 return data_points;
266}
267std::size_t
269{
270 return m_filterMin.getHalfWindowSize();
271}
272
274 : m_halfWindowSize(half_window_size)
275{
276}
277
282
284{
285 buildFilterFromString(strBuildParams);
286}
287
288void
290{
291 //"antiSpike|2"
292 if(strBuildParams.startsWith("antiSpike|"))
293 {
294 QStringList params = strBuildParams.split("|").back().split(";");
295
296 m_halfWindowSize = params.at(0).toUInt();
297 }
298 else
299 {
301 QString("building FilterMorphoAntiSpike from string %1 is not possible")
302 .arg(strBuildParams));
303 }
304}
305
306QString
308{
309 QString strCode = QString("antiSpike|%1").arg(m_halfWindowSize);
310
311 return strCode;
312}
313
314QString
316{
317 return "antiSpike";
318}
319
320
323{
325
326 return *this;
327}
328
329std::size_t
334Trace &
336{
337 // qDebug();
338 if(m_halfWindowSize == 0)
339 return data_points;
340 if(data_points.size() < m_halfWindowSize)
341 return data_points;
342 Trace old_trace(data_points);
343 auto it = old_trace.begin();
344 auto it_target = data_points.begin();
345 auto itw = old_trace.begin();
346
347 auto itend = old_trace.end() - m_halfWindowSize - 1;
348 // new_trace.reserve(data_points.size());
349
350 // qDebug();
351 while((it != old_trace.end()) && (std::distance(old_trace.begin(), it) < (int)m_halfWindowSize))
352 {
353 // no anti spike at the begining of the signal
354 it++;
355 it_target++;
356 }
357 // qDebug();
358 while((it != itend) && (it != old_trace.end()))
359 {
360 // qDebug();
361 auto itwend = it + m_halfWindowSize + 1;
362 itw = findDifferentYvalue(it - m_halfWindowSize, it + 1, 0);
363 if(itw == it)
364 {
365 // qDebug();
366 itw = findDifferentYvalue(it + 1, itwend, 0);
367 if(itw == itwend)
368 {
369 it_target->y = 0;
370 }
371 // qDebug();
372 }
373
374 // qDebug();
375 it++;
376 it_target++;
377 }
378
379 return data_points;
380}
381
382
383FilterMorphoMedian::FilterMorphoMedian(std::size_t half_window_size)
384 : FilterMorphoWindowBase(half_window_size)
385{
386}
391
392void
394{
395 if(strBuildParams.startsWith(QString("%1|").arg(name())))
396 {
397 QStringList params = strBuildParams.split("|").back().split(";");
398
399 m_halfWindowSize = params.at(0).toDouble();
400 }
401 else
402 {
404 QString("Building of FilterMorphoMean from string %1 failed").arg(strBuildParams));
405 }
406}
407
408QString
410{
411 QString strCode = QString("%1|%2").arg(name()).arg(m_halfWindowSize);
412
413 return strCode;
414}
415
416QString
418{
419 return "morphoMean";
420}
421
424{
426
427 return *this;
428}
429
430
431double
432FilterMorphoMedian::getWindowValue(std::vector<DataPoint>::const_iterator begin,
433 std::vector<DataPoint>::const_iterator end) const
434{
435 return medianYTrace(begin, end);
436}
437
438
439FilterMorphoMean::FilterMorphoMean(std::size_t half_window_size)
440 : FilterMorphoWindowBase(half_window_size)
441{
442}
447
450{
452
453 return *this;
454}
455
456double
457FilterMorphoMean::getWindowValue(std::vector<DataPoint>::const_iterator begin,
458 std::vector<DataPoint>::const_iterator end) const
459{
460 return meanYTrace(begin, end);
461}
462
463pappso::FilterMorphoMean::FilterMorphoMean(const QString &strBuildParams)
465{
466 buildFilterFromString(strBuildParams);
467}
468
469
471{
472 buildFilterFromString(strBuildParams);
473}
474
475FilterMorphoBackground::FilterMorphoBackground(std::size_t median_half_window_size,
476 std::size_t minmax_half_window_size)
477
478{
479 mpa_filterMorphoMedian = new FilterMorphoMedian(median_half_window_size);
480 mpa_filterMorphoMinMax = new FilterMorphoMinMax(minmax_half_window_size);
481}
482
490
491
492void
494{
495
496 if(strBuildParams.startsWith(QString("%1|").arg(name())))
497 {
498 QStringList params = strBuildParams.split("|").back().split(";");
499
500 mpa_filterMorphoMedian = new FilterMorphoMedian(params.at(0).toDouble());
501 mpa_filterMorphoMinMax = new FilterMorphoMinMax(params.at(1).toDouble());
502 }
503 else
504 {
506 QString("Building of FilterMorphoBackground from string %1 failed").arg(strBuildParams));
507 }
508}
509
510
511QString
513{
514 QString strCode = QString("morphoBackground|%1;%2")
515 .arg(mpa_filterMorphoMedian->getHalfWindowSize())
516 .arg(mpa_filterMorphoMinMax->getMinMaxHalfEdgeWindows());
517
518 return strCode;
519}
520
521QString
523{
524 return "morphoBackground";
525}
526
538
539Trace &
541{
542 mpa_filterMorphoMedian->filter(data_points);
543 mpa_filterMorphoMinMax->filter(data_points);
544
545 // finally filter negative values
546 for(DataPoint &point : data_points)
547 {
548 if(point.y < 0)
549 {
550 point.y = 0;
551 }
552 }
553 return data_points;
554}
555const FilterMorphoMedian &
560const FilterMorphoMinMax &
565
excetion to use when an item type is not recognized
anti spike filter set to zero alone values inside the window
FilterMorphoAntiSpike & operator=(const FilterMorphoAntiSpike &other)
std::size_t getHalfWindowSize() const
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
QString toString() const override
QString name() 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
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
Trace & filter(Trace &data_points) const override
FilterMorphoMedian * mpa_filterMorphoMedian
QString toString() const override
const FilterMorphoMedian & getFilterMorphoMedian() const
FilterMorphoBackground & operator=(const FilterMorphoBackground &other)
const FilterMorphoMinMax & getFilterMorphoMinMax() const
FilterMorphoBackground(const QString &strBuildParams)
FilterMorphoMinMax * mpa_filterMorphoMinMax
QString name() const override
transform the trace with the maximum of the minimum equivalent of the erode filter for pictures
Trace & filter(Trace &data_points) const override
FilterMorphoMaxMin(std::size_t half_window_size)
FilterMorphoMax m_filterMax
std::size_t getMaxMinHalfEdgeWindows() const
FilterMorphoMaxMin & operator=(const FilterMorphoMaxMin &other)
FilterMorphoMin m_filterMin
transform the trace into its maximum over a window
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
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
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
FilterMorphoMean & operator=(const FilterMorphoMean &other)
QString toString() const override
FilterMorphoMean(const QString &strBuildParams)
double getWindowValue(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end) const override
QString name() const override
median filter apply median of y values inside the window
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
FilterMorphoMax m_filterMax
FilterMorphoMin m_filterMin
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
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
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
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
virtual QString name() const override
FilterMorphoWindowBase(std::size_t half_window_size)
virtual std::size_t getHalfWindowSize() const
virtual QString toString() const override
FilterMorphoWindowBase & operator=(const FilterMorphoWindowBase &other)
A simple container of DataPoint instances.
Definition trace.h:148
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:126
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:174
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:272
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:239
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:231
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:157