OpenMS
MZTrafoModel.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 
14 #include <vector>
15 
16 namespace OpenMS
17 {
18 
38  class OPENMS_DLLAPI MZTrafoModel
39  {
40 
41  private:
42  std::vector<double> coeff_;
43  bool use_ppm_;
44  double rt_;
45 
47  static int ransac_seed_;
48  static double limit_offset_;
49  static double limit_scale_;
50  static double limit_power_;
51 
52  public:
53 
58 
69  MZTrafoModel(bool ppm_model);
70 
71  enum MODELTYPE { LINEAR, LINEAR_WEIGHTED, QUADRATIC, QUADRATIC_WEIGHTED, SIZE_OF_MODELTYPE };
72  static const std::string names_of_modeltype[];
80  static MODELTYPE nameToEnum(const std::string& name);
87  static const std::string& enumToName(MODELTYPE mt);
88 
89 
99  static void setRANSACParams(const Math::RANSACParam& p);
100 
104  static void setRANSACSeed(int seed);
105 
114  static void setCoefficientLimits(double offset, double scale, double power);
125  static bool isValidModel(const MZTrafoModel& trafo);
126 
133  bool isTrained() const;
134 
138  double getRT() const;
139 
152  double predict(double mz) const;
153 
165  static Size findNearest(const std::vector<MZTrafoModel>& tms, double rt);
166 
167 
169  struct RTLess
170  {
171  inline bool operator()(const double& left, const MZTrafoModel& right) const
172  {
173  return left < right.rt_;
174  }
175  inline bool operator()(const MZTrafoModel& left, const double& right) const
176  {
177  return left.rt_ < right;
178  }
179  inline bool operator()(const MZTrafoModel& left, const MZTrafoModel& right) const
180  {
181  return left.rt_ < right.rt_;
182  }
183  };
205  bool train(const CalibrationData& cd, MODELTYPE md, bool use_RANSAC,
206  double rt_left = -std::numeric_limits<double>::max(),
207  double rt_right = std::numeric_limits<double>::max()
208  );
209 
234  bool train(std::vector<double> error_mz,
235  std::vector<double> theo_mz,
236  std::vector<double> weights,
237  MODELTYPE md,
238  bool use_RANSAC);
239 
251  void getCoefficients(double& intercept, double& slope, double& power);
252 
256  void setCoefficients(const MZTrafoModel& rhs);
257 
269  void setCoefficients(double intercept, double slope, double power);
270 
277  String toString() const;
278 
279  }; // MZTrafoModel
280 
281 } // namespace OpenMS
282 
A helper class, holding all calibration points.
Definition: CalibrationData.h:39
Create and apply models of a mass recalibration function.
Definition: MZTrafoModel.h:39
MZTrafoModel()
Default constructor.
static void setRANSACParams(const Math::RANSACParam &p)
Set the global (program wide) parameters for RANSAC.
void getCoefficients(double &intercept, double &slope, double &power)
Get model coefficients.
double getRT() const
Get RT associated with the model (training region)
bool use_ppm_
during training, model is build on absolute or relative(ppm) predictions. predict(),...
Definition: MZTrafoModel.h:43
static void setCoefficientLimits(double offset, double scale, double power)
Set coefficient boundaries for which the model coefficient must not exceed to be considered a valid m...
String toString() const
String representation of the model parameters.
bool isTrained() const
Does the model have coefficients (i.e. was trained successfully).
static double limit_offset_
acceptable boundary for the estimated offset; if estimated offset is larger (absolute) the model does...
Definition: MZTrafoModel.h:48
MZTrafoModel(bool ppm_model)
Default constructor.
static double limit_power_
acceptable boundary for the estimated power; if estimated power is larger (absolute) the model does n...
Definition: MZTrafoModel.h:50
static int ransac_seed_
seed used for all RANSAC invocations
Definition: MZTrafoModel.h:47
double predict(double mz) const
Apply the model to an uncalibrated m/z value.
bool train(const CalibrationData &cd, MODELTYPE md, bool use_RANSAC, double rt_left=-std::numeric_limits< double >::max(), double rt_right=std::numeric_limits< double >::max())
Train a model using calibrant data.
void setCoefficients(double intercept, double slope, double power)
Manually set model coefficients.
static bool isValidModel(const MZTrafoModel &trafo)
Predicate to decide if the model has valid parameters, i.e. coefficients.
bool train(std::vector< double > error_mz, std::vector< double > theo_mz, std::vector< double > weights, MODELTYPE md, bool use_RANSAC)
Train a model using calibrant data.
void setCoefficients(const MZTrafoModel &rhs)
Copy model coefficients from another model.
static double limit_scale_
acceptable boundary for the estimated scale; if estimated scale is larger (absolute) the model does n...
Definition: MZTrafoModel.h:49
std::vector< double > coeff_
Model coefficients (for both linear and quadratic models), estimated from the data.
Definition: MZTrafoModel.h:42
static Size findNearest(const std::vector< MZTrafoModel > &tms, double rt)
Binary search for the model nearest to a specific RT.
double rt_
retention time associated to the model (i.e. where the calibrant data was taken from)
Definition: MZTrafoModel.h:44
static const std::string & enumToName(MODELTYPE mt)
Convert enum to string.
static Math::RANSACParam * ransac_params_
global pointer, init to NULL at startup; set class-global RANSAC params
Definition: MZTrafoModel.h:46
static MODELTYPE nameToEnum(const std::string &name)
Convert string to enum.
MODELTYPE
Definition: MZTrafoModel.h:71
@ LINEAR
Definition: MZTrafoModel.h:71
static void setRANSACSeed(int seed)
Set RANSAC seed.
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Comparator by position. As this class has dimension 1, this is basically an alias for MZLess.
Definition: MZTrafoModel.h:170
bool operator()(const double &left, const MZTrafoModel &right) const
Definition: MZTrafoModel.h:171
bool operator()(const MZTrafoModel &left, const double &right) const
Definition: MZTrafoModel.h:175
bool operator()(const MZTrafoModel &left, const MZTrafoModel &right) const
Definition: MZTrafoModel.h:179
A simple struct to carry all the parameters required for a RANSAC run.
Definition: RANSAC.h:33