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
60 [[maybe_unused]] const QString &strBuildParams)
61{
62}
63
64QString
66{
67 return "nose";
68}
69
70QString
72{
73 return "nose";
74}
75
76
77Trace &
79{
80
81 qDebug() << " " << m_halfWindowSize << " data_points.size()" << data_points.size();
82 if(m_halfWindowSize == 0)
83 return data_points;
84 if(data_points.size() <= m_halfWindowSize)
85 return data_points;
86 Trace old_trace(data_points);
87 auto it = old_trace.begin();
88 auto itend = old_trace.end() - m_halfWindowSize - 1;
89 auto it_target = data_points.begin();
90
91
92 std::size_t loop_begin = 0;
93 while((it != itend) && (loop_begin < m_halfWindowSize))
94 {
95 // maxYDataPoint(it_begin, it + m_halfWindowSize + 1);
96 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
97 // << it->x << " " << m_halfWindowSize;
98 // it_target->x = it->x;
99 it_target->y = getWindowValue(old_trace.begin(), it + m_halfWindowSize + 1);
100 it++;
101 it_target++;
102 loop_begin++;
103 }
104 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
105 while(it != itend)
106 {
107 // it_target->x = it->x;
108 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
109 // << it->x;
110 it_target->y = getWindowValue(it - m_halfWindowSize, it + m_halfWindowSize + 1);
111 it++;
112 it_target++;
113 }
114 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
115 while(it != old_trace.end())
116 {
117 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
118 // << it->x;
119 // it_target->x = it->x;
120 it_target->y = getWindowValue(it - m_halfWindowSize, old_trace.end());
121 it++;
122 it_target++;
123 }
124 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
125 // problem with move or swap : this lead to segmentation faults in some cases
126 // data_points = std::move(new_trace);
127 qDebug();
128 return data_points;
129}
130
131FilterMorphoSum::FilterMorphoSum(std::size_t half_window_size)
132 : FilterMorphoWindowBase(half_window_size)
133{
134}
139
142{
144
145 return *this;
146}
147
148double
149FilterMorphoSum::getWindowValue(std::vector<DataPoint>::const_iterator begin,
150 std::vector<DataPoint>::const_iterator end) const
151{
152
153 qDebug();
154 return sumYTrace(begin, end, 0);
155}
156
157FilterMorphoMax::FilterMorphoMax(std::size_t half_window_size)
158 : FilterMorphoWindowBase(half_window_size)
159{
160}
165
168{
170
171 return *this;
172}
173
174double
175FilterMorphoMax::getWindowValue(std::vector<DataPoint>::const_iterator begin,
176 std::vector<DataPoint>::const_iterator end) const
177{
178
179 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
180 return maxYDataPoint(begin, end)->y;
181}
182
183FilterMorphoMin::FilterMorphoMin(std::size_t half_window_size)
184 : FilterMorphoWindowBase(half_window_size)
185{
186}
191
194{
196
197 return *this;
198}
199
200double
201FilterMorphoMin::getWindowValue(std::vector<DataPoint>::const_iterator begin,
202 std::vector<DataPoint>::const_iterator end) const
203{
204 return minYDataPoint(begin, end)->y;
205}
206
207FilterMorphoMinMax::FilterMorphoMinMax(std::size_t half_window_size)
208 : m_filterMax(half_window_size), m_filterMin(half_window_size)
209{
210}
215
218{
219 m_filterMax = other.m_filterMax;
220 m_filterMin = other.m_filterMin;
221
222 return *this;
223}
224
225Trace &
227{
228 qDebug();
229 m_filterMax.filter(data_points);
230 m_filterMin.filter(data_points);
231 qDebug();
232 return data_points;
233}
234std::size_t
236{
237 return m_filterMax.getHalfWindowSize();
238}
239
240
241FilterMorphoMaxMin::FilterMorphoMaxMin(std::size_t half_window_size)
242 : m_filterMin(half_window_size), m_filterMax(half_window_size)
243{
244}
249
252{
253 m_filterMin = other.m_filterMin;
254 m_filterMax = other.m_filterMax;
255
256 return *this;
257}
258
259Trace &
261{
262 qDebug();
263 m_filterMin.filter(data_points);
264 m_filterMax.filter(data_points);
265 qDebug();
266 return data_points;
267}
268std::size_t
270{
271 return m_filterMin.getHalfWindowSize();
272}
273
275 : m_halfWindowSize(half_window_size)
276{
277}
278
283
285{
286 buildFilterFromString(strBuildParams);
287}
288
289void
291{
292 //"antiSpike|2"
293 if(strBuildParams.startsWith("antiSpike|"))
294 {
295 QStringList params = strBuildParams.split("|").back().split(";");
296
297 m_halfWindowSize = params.at(0).toUInt();
298 }
299 else
300 {
302 QString("building FilterMorphoAntiSpike from string %1 is not possible")
303 .arg(strBuildParams));
304 }
305}
306
307QString
309{
310 QString strCode = QString("antiSpike|%1").arg(m_halfWindowSize);
311
312 return strCode;
313}
314
315QString
317{
318 return "antiSpike";
319}
320
321
324{
326
327 return *this;
328}
329
330std::size_t
335Trace &
337{
338 // qDebug();
339 if(m_halfWindowSize == 0)
340 return data_points;
341 if(data_points.size() < m_halfWindowSize)
342 return data_points;
343 Trace old_trace(data_points);
344 auto it = old_trace.begin();
345 auto it_target = data_points.begin();
346 auto itw = old_trace.begin();
347
348 auto itend = old_trace.end() - m_halfWindowSize - 1;
349 // new_trace.reserve(data_points.size());
350
351 // qDebug();
352 while((it != old_trace.end()) && (std::distance(old_trace.begin(), it) < (int)m_halfWindowSize))
353 {
354 // no anti spike at the begining of the signal
355 it++;
356 it_target++;
357 }
358 // qDebug();
359 while((it != itend) && (it != old_trace.end()))
360 {
361 // qDebug();
362 auto itwend = it + m_halfWindowSize + 1;
363 itw = findDifferentYvalue(it - m_halfWindowSize, it + 1, 0);
364 if(itw == it)
365 {
366 // qDebug();
367 itw = findDifferentYvalue(it + 1, itwend, 0);
368 if(itw == itwend)
369 {
370 it_target->y = 0;
371 }
372 // qDebug();
373 }
374
375 // qDebug();
376 it++;
377 it_target++;
378 }
379
380 return data_points;
381}
382
383
384FilterMorphoMedian::FilterMorphoMedian(std::size_t half_window_size)
385 : FilterMorphoWindowBase(half_window_size)
386{
387}
392
393void
395{
396 if(strBuildParams.startsWith(QString("%1|").arg(name())))
397 {
398 QStringList params = strBuildParams.split("|").back().split(";");
399
400 m_halfWindowSize = params.at(0).toDouble();
401 }
402 else
403 {
405 QString("Building of FilterMorphoMean from string %1 failed").arg(strBuildParams));
406 }
407}
408
409QString
411{
412 QString strCode = QString("%1|%2").arg(name()).arg(m_halfWindowSize);
413
414 return strCode;
415}
416
417QString
419{
420 return "morphoMean";
421}
422
425{
427
428 return *this;
429}
430
431
432double
433FilterMorphoMedian::getWindowValue(std::vector<DataPoint>::const_iterator begin,
434 std::vector<DataPoint>::const_iterator end) const
435{
436 return medianYTrace(begin, end);
437}
438
439
440FilterMorphoMean::FilterMorphoMean(std::size_t half_window_size)
441 : FilterMorphoWindowBase(half_window_size)
442{
443}
448
451{
453
454 return *this;
455}
456
457double
458FilterMorphoMean::getWindowValue(std::vector<DataPoint>::const_iterator begin,
459 std::vector<DataPoint>::const_iterator end) const
460{
461 return meanYTrace(begin, end);
462}
463
464pappso::FilterMorphoMean::FilterMorphoMean(const QString &strBuildParams)
466{
467 buildFilterFromString(strBuildParams);
468}
469
470
472{
473 buildFilterFromString(strBuildParams);
474}
475
476FilterMorphoBackground::FilterMorphoBackground(std::size_t median_half_window_size,
477 std::size_t minmax_half_window_size)
478
479{
480 mpa_filterMorphoMedian = new FilterMorphoMedian(median_half_window_size);
481 mpa_filterMorphoMinMax = new FilterMorphoMinMax(minmax_half_window_size);
482}
483
491
492
493void
495{
496
497 if(strBuildParams.startsWith(QString("%1|").arg(name())))
498 {
499 QStringList params = strBuildParams.split("|").back().split(";");
500
501 mpa_filterMorphoMedian = new FilterMorphoMedian(params.at(0).toDouble());
502 mpa_filterMorphoMinMax = new FilterMorphoMinMax(params.at(1).toDouble());
503 }
504 else
505 {
507 QString("Building of FilterMorphoBackground from string %1 failed").arg(strBuildParams));
508 }
509}
510
511
512QString
514{
515 QString strCode = QString("morphoBackground|%1;%2")
516 .arg(mpa_filterMorphoMedian->getHalfWindowSize())
517 .arg(mpa_filterMorphoMinMax->getMinMaxHalfEdgeWindows());
518
519 return strCode;
520}
521
522QString
524{
525 return "morphoBackground";
526}
527
539
540Trace &
542{
543 mpa_filterMorphoMedian->filter(data_points);
544 mpa_filterMorphoMinMax->filter(data_points);
545
546 // finally filter negative values
547 for(DataPoint &point : data_points)
548 {
549 if(point.y < 0)
550 {
551 point.y = 0;
552 }
553 }
554 return data_points;
555}
556const FilterMorphoMedian &
561const FilterMorphoMinMax &
566
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:152
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:121
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:169
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:267
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:234
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:226
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:152