Loading...
Searching...
No Matches
maptrace.cpp
Go to the documentation of this file.
#include <numeric>
#include <limits>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
/////////////////////// Qt includes
#include <QDebug>
#include <QObject>
#include "../exception/exceptionnotpossible.h"
#include "maptrace.h"
#include "../processing/combiners/tracepluscombiner.h"
#include "../processing/combiners/traceminuscombiner.h"
#include "../types.h"
int mapTraceMetaTypeId =
qRegisterMetaType<pappso::MapTrace>("pappso::MapTrace");
int mapTracePtrMetaTypeId =
qRegisterMetaType<pappso::MapTrace *>("pappso::MapTrace *");
namespace pappso
{
MapTrace::MapTrace()
{
}
MapTrace::MapTrace(
const std::vector<std::pair<pappso_double, pappso_double>> &dataPoints)
{
for(auto &dataPoint : dataPoints)
{
insert(dataPoint);
}
}
MapTrace::MapTrace(const std::vector<DataPoint> &dataPoints)
{
for(auto &dataPoint : dataPoints)
{
insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
}
}
MapTrace::MapTrace(const MapTrace &other)
: std::map<pappso_double, pappso_double>(other)
{
}
MapTrace::MapTrace(const Trace &trace)
{
// qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()";
for(auto &dataPoint : trace)
{
// std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << "
// () "
//<< std::setprecision(15)
//<< "Current data point: " << dataPoint.toString().toStdString() <<
// std::endl;
insert(std::pair<pappso_double, pappso_double>(dataPoint.x, dataPoint.y));
}
// qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()"
//<< "After construction, map size: " << size();
}
MapTrace::~MapTrace()
{
// Calls the destructor for each DataPoint object in the vector.
clear();
}
size_t
MapTrace::initialize(const std::vector<pappso_double> &xVector,
const std::vector<pappso_double> &yVector)
{
// Clear *this, because initialize supposes that *this will contain only the
// data in the vectors.
clear();
// Sanity check
if(xVector.size() != yVector.size())
throw ExceptionNotPossible(
QObject::tr("Fatal error at msrundatasettreenode.cpp "
"-- ERROR xVector and yVector must have the same size."
"Program aborted."));
for(std::size_t iter = 0; iter < xVector.size(); ++iter)
{
insert(std::pair<pappso_double, pappso_double>(xVector.at(iter),
yVector.at(iter)));
}
return size();
}
size_t
MapTrace::initialize(const std::map<pappso_double, pappso_double> &map)
{
// Clear *this, because initialize supposes that *this will be identical to
// map.
clear();
for(auto &&pair : map)
{
insert(pair);
}
return size();
}
MapTrace &
MapTrace::operator=(const MapTrace &other)
{
if(&other == this)
return *this;
// Clear *this, because initialize supposes that *this will be identical to
// other.
clear();
for(auto &pair : other)
{
insert(pair);
}
return *this;
}
MapTraceSPtr
MapTrace::makeMapTraceSPtr() const
{
return std::make_shared<MapTrace>(*this);
}
MapTraceCstSPtr
MapTrace::makeMapTraceCstSPtr() const
{
return std::make_shared<const MapTrace>(*this);
}
std::vector<pappso_double>
MapTrace::xValues()
{
std::vector<pappso_double> vector;
for(auto &&pair : *this)
vector.push_back(pair.first);
return vector;
}
std::vector<pappso_double>
MapTrace::yValues()
{
std::vector<pappso_double> vector;
for(auto &&pair : *this)
vector.push_back(pair.second);
return vector;
}
void
MapTrace::insertOrUpdate(const DataPoint &data_point)
{
// Try to insert the data point into the map. Check if that was done or
// not.
std::pair<std::map<double, double>::iterator, bool> res =
insert(std::pair<double, double>(data_point.x, data_point.y));
if(!res.second)
{
// One other same (x,y) value pair was seen already. Only increment the y
// value.
res.first->second += data_point.y;
}
}
void
MapTrace::insertOrUpdate(const Trace &trace)
{
for(const DataPoint &data_point : trace)
insertOrUpdate(data_point);
}
Trace
MapTrace::toTrace() const
{
Trace trace;
for(auto &&pair : *this)
trace.push_back(DataPoint(pair.first, pair.second));
return trace;
}
QString
MapTrace::toString() const
{
// Even if the spectrum is empty, we should return an empty string.
QString text;
for(auto &&pair : *this)
{
// For debugging
#if 0
QString new_data_point_text = QString("%1 %2\n")
.arg(pair.first, 0, 'f', 10)
.arg(pair.second, 0, 'f', 10);
qDebug() << "new data point text:" << new_data_point_text;
text.append(new_data_point_text);
#endif
text.append(QString("%1 %2\n")
.arg(pair.first, 0, 'f', 10)
.arg(pair.second, 0, 'f', 10));
}
return text;
}
} // namespace pappso
pappso::ExceptionNotPossible
Definition exceptionnotpossible.h:29
pappso::MapTrace
Definition maptrace.h:34
pappso::MapTrace::toString
QString toString() const
Definition maptrace.cpp:231
pappso::MapTrace::makeMapTraceSPtr
MapTraceSPtr makeMapTraceSPtr() const
Definition maptrace.cpp:154
pappso::MapTrace::~MapTrace
virtual ~MapTrace()
Definition maptrace.cpp:81
pappso::MapTrace::MapTrace
MapTrace()
Definition maptrace.cpp:31
pappso::MapTrace::operator=
virtual MapTrace & operator=(const MapTrace &other)
Definition maptrace.cpp:133
pappso::MapTrace::toTrace
Trace toTrace() const
Definition maptrace.cpp:219
pappso::MapTrace::xValues
std::vector< pappso_double > xValues()
Definition maptrace.cpp:168
pappso::MapTrace::makeMapTraceCstSPtr
MapTraceCstSPtr makeMapTraceCstSPtr() const
Definition maptrace.cpp:161
pappso::MapTrace::initialize
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition maptrace.cpp:89
pappso::MapTrace::insertOrUpdate
void insertOrUpdate(const DataPoint &data_point)
Definition maptrace.cpp:192
pappso::MapTrace::yValues
std::vector< pappso_double > yValues()
Definition maptrace.cpp:180
pappso::Trace
A simple container of DataPoint instances.
Definition trace.h:148
pappso::Trace::append
size_t append(const DataPoint &data_point)
appends a datapoint and return new size
Definition trace.cpp:648
mapTracePtrMetaTypeId
int mapTracePtrMetaTypeId
Definition maptrace.cpp:24
mapTraceMetaTypeId
int mapTraceMetaTypeId
Definition maptrace.cpp:22
maptrace.h
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::PrecisionUnit::res
@ res
pappso::MapTraceSPtr
std::shared_ptr< MapTrace > MapTraceSPtr
Definition maptrace.h:26
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition types.h:50
pappso::MapTraceCstSPtr
std::shared_ptr< const MapTrace > MapTraceCstSPtr
Definition maptrace.h:27
pappso::DataPoint
Definition datapoint.h:22
pappso::DataPoint::x
pappso_double x
Definition datapoint.h:23
pappso::DataPoint::y
pappso_double y
Definition datapoint.h:24