OpenMS
ProductModel.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 
10 #pragma once
11 
14 #include <OpenMS/KERNEL/Peak2D.h>
17 
18 namespace OpenMS
19 {
20 
30  template <UInt D>
31  class ProductModel;
32 
34  template <>
35  class OPENMS_DLLAPI ProductModel<2>:
36  public BaseModel<2>
37  {
38 public:
39 
41  enum {D = 2};
42 
43  typedef double IntensityType;
46 
49  BaseModel<D>(),
50  distributions_(D, nullptr)
51  {
52  this->setName(this->getProductName());
53 
54  //Register model info
55  for (UInt dim = 0; dim < D; ++dim)
56  {
58  this->subsections_.push_back(name);
59  this->defaults_.setValue(name, "GaussModel", "Name of the model used for this dimension");
60  }
61 
62  //defaults
63  this->defaults_.setValue("intensity_scaling", 1.0, "Scaling factor used to adjust the model distribution to the intensities of the data");
64  this->defaultsToParam_();
65  }
66 
68  ProductModel(const ProductModel & source) :
69  BaseModel<D>(source),
70  distributions_(D, nullptr),
71  scale_(source.scale_)
72  {
73  for (UInt dim = 0; dim < D; ++dim)
74  {
75  // clone source model
76  if (source.distributions_[dim])
77  {
78  ModelDescription<1> desc(source.distributions_[dim]);
79  setModel(dim, desc.createModel());
80  }
81  }
82  updateMembers_();
83  }
84 
86  ~ProductModel() override
87  {
88  for (Size dim = 0; dim < D; ++dim)
89  {
90  delete distributions_[dim];
91  }
92  }
93 
95  virtual ProductModel & operator=(const ProductModel & source)
96  {
97  if (&source == this) return *this;
98 
100  scale_ = source.scale_;
101 
102  for (UInt dim = 0; dim < D; ++dim)
103  {
104  if (source.distributions_[dim])
105  {
106  // clone source model
107  ModelDescription<1> desc(source.distributions_[dim]);
108  setModel(dim, desc.createModel());
109  }
110  else
111  {
112  distributions_[dim] = nullptr;
113  }
114  }
115  updateMembers_();
116 
117  return *this;
118  }
119 
121  IntensityType getIntensity(const PositionType & pos) const override
122  {
123  IntensityType intens(scale_);
124  for (UInt dim = 0; dim < D; ++dim)
125  {
126  if (distributions_[dim] == nullptr)
127  {
128  throw Exception::BaseException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("ProductModel: model for dimension ") + dim + " not set.", "");
129  }
130  intens *= distributions_[dim]->getIntensity(pos[dim]);
131  }
132  return intens;
133  }
134 
136  static BaseModel<D> * create()
137  {
138  return new ProductModel<D>();
139  }
140 
142  static const String getProductName()
143  {
144  return String("ProductModel") + D + "D";
145  }
146 
155  {
156  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
157  if (dist == nullptr || dist == distributions_[dim])
158  {
159  return *this;
160  }
161 
162  delete distributions_[dim];
163  distributions_[dim] = dist;
164 
165  // Update model info
167  this->param_.removeAll(name + ':');
168  this->param_.insert(name + ':', distributions_[dim]->getParameters());
169  this->param_.setValue(name, distributions_[dim]->getName());
170 
171  return *this;
172  }
173 
175  {
176  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
177  return distributions_[dim];
178  }
179 
182  {
183  return scale_;
184  }
185 
188  {
189  this->setCutOff(this->getCutOff() / scale_); // remove scaling from cutoff
190  scale_ = scale;
191  this->param_.setValue("intensity_scaling", scale);
192  this->setCutOff(this->getCutOff() * scale_); // scale cutoff
193  }
194 
196  void getSamples(SamplesType & cont) const override
197  {
198  cont.clear();
199  typedef BaseModel<1>::SamplesType Samples1D;
200  std::vector<Samples1D> samples(D);
201  // get samples for each dimension
202  for (Size dim = 0; dim < D; ++dim)
203  {
204  distributions_[dim]->getSamples(samples[dim]);
205  }
206 
208  std::vector<UInt> i(D, 0); // index vector
209 
210  while (i[D - 1] < samples[D - 1].size())
211  {
212  for (UInt dim = 0; dim < D; ++dim)
213  {
214  peak.getPosition()[dim] = samples[dim][i[dim]].getPosition()[0];
215  }
216  fillIntensity(peak);
217  cont.push_back(peak);
218 
219  ++i[0];
220  for (Size dim = 0; dim < D - 1; ++dim)
221  {
222  if (i[dim] >= samples[dim].size())
223  {
224  i[dim] = 0;
225  ++i[dim + 1];
226  }
227  }
228  }
229  return;
230  }
231 
232 protected:
233  void updateMembers_() override
234  {
236  scale_ = (double)(this->param_.getValue("intensity_scaling"));
237  for (UInt dim = 0; dim < D; ++dim)
238  {
240  if (this->param_.exists(name))
241  {
242  delete distributions_[dim];
243  distributions_[dim] = Factory<BaseModel<1> >::create(this->param_.getValue(name).toString());
244  Param copy = this->param_.copy(name + ":", true);
245  distributions_[dim]->setParameters(copy);
246  if (distributions_[dim]->getName().hasSubstring("IsotopeModel"))
247  {
248  static_cast<IsotopeModel *>(distributions_[dim])->setSamples(static_cast<IsotopeModel *>(distributions_[dim])->getFormula());
249  }
250  }
251  }
252  }
253 
254  std::vector<BaseModel<1> *> distributions_;
256  };
257 }
258 
Abstract base class for all D-dimensional models.
Definition: BaseModel.h:25
DPeak< D >::Type PeakType
Definition: BaseModel.h:30
BaseModel & operator=(const BaseModel &source)
assignment operator
Definition: BaseModel.h:51
std::vector< PeakType > SamplesType
Definition: BaseModel.h:31
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: BaseModel.h:126
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:29
Exception base class.
Definition: Exception.h:63
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:37
Definition: IsotopeModel.h:34
EmpiricalFormula getFormula()
return the Averagine peptide formula (mass calculated from mean mass and charge – use ....
Stores the name and parameters of a model.
Definition: ModelDescription.h:28
BaseModel< D > * createModel()
Definition: ModelDescription.h:70
Management and storage of parameters / INI files.
Definition: Param.h:44
Param copy(const std::string &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
static char const * shortDimensionName(UInt const dim)
Short name of the dimension (abbreviated form)
IntensityType getScale() const
return the intensity scaling factor
Definition: ProductModel.h:181
std::vector< BaseModel< 1 > * > distributions_
Definition: ProductModel.h:254
IntensityType scale_
Definition: ProductModel.h:255
DPosition< D > PositionType
Definition: ProductModel.h:44
BaseModel< D >::SamplesType SamplesType
Definition: ProductModel.h:45
ProductModel()
Default constructor.
Definition: ProductModel.h:48
void setScale(IntensityType scale)
set the intensity scaling factor
Definition: ProductModel.h:187
static BaseModel< D > * create()
create new ProductModel object (needed by Factory)
Definition: ProductModel.h:136
virtual ProductModel & operator=(const ProductModel &source)
assignment operator
Definition: ProductModel.h:95
BaseModel< 1 > * getModel(UInt dim) const
Definition: ProductModel.h:174
IntensityType getIntensity(const PositionType &pos) const override
intensity equals product of intensities in each dimension
Definition: ProductModel.h:121
ProductModel & setModel(UInt dim, BaseModel< 1 > *dist)
set model dist for dimension dim
Definition: ProductModel.h:154
~ProductModel() override
destructor
Definition: ProductModel.h:86
ProductModel(const ProductModel &source)
copy constructor
Definition: ProductModel.h:68
void getSamples(SamplesType &cont) const override
get reasonable set of samples from the model (i.e. for printing)
Definition: ProductModel.h:196
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: ProductModel.h:233
double IntensityType
Definition: ProductModel.h:43
static const String getProductName()
Returns the name of the model.
Definition: ProductModel.h:142
A more convenient string class.
Definition: String.h:34
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Class for product models i.e. models with D independent dimensions.
Definition: ProductModel.h:31
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:112
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19