OpenMS
ProductModel.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: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 
10 #pragma once
11 
14 #include <OpenMS/KERNEL/Peak2D.h>
17 
18 namespace OpenMS
19 {
20 
33  template <UInt D>
34  class ProductModel;
35 
37  template <>
38  class OPENMS_DLLAPI ProductModel<2>:
39  public BaseModel<2>
40  {
41 public:
42 
44  enum {D = 2};
45 
46  typedef double IntensityType;
49 
52  BaseModel<D>(),
53  distributions_(D, nullptr)
54  {
55  this->setName(this->getProductName());
56 
57  //Register model info
58  for (UInt dim = 0; dim < D; ++dim)
59  {
61  this->subsections_.push_back(name);
62  this->defaults_.setValue(name, "GaussModel", "Name of the model used for this dimension");
63  }
64 
65  //defaults
66  this->defaults_.setValue("intensity_scaling", 1.0, "Scaling factor used to adjust the model distribution to the intensities of the data");
67  this->defaultsToParam_();
68  }
69 
71  ProductModel(const ProductModel & source) :
72  BaseModel<D>(source),
73  distributions_(D, nullptr),
74  scale_(source.scale_)
75  {
76  for (UInt dim = 0; dim < D; ++dim)
77  {
78  // clone source model
79  if (source.distributions_[dim])
80  {
81  ModelDescription<1> desc(source.distributions_[dim]);
82  setModel(dim, desc.createModel());
83  }
84  }
85  updateMembers_();
86  }
87 
89  ~ProductModel() override
90  {
91  for (Size dim = 0; dim < D; ++dim)
92  {
93  delete distributions_[dim];
94  }
95  }
96 
98  virtual ProductModel & operator=(const ProductModel & source)
99  {
100  if (&source == this) return *this;
101 
102  BaseModel<D>::operator=(source);
103  scale_ = source.scale_;
104 
105  for (UInt dim = 0; dim < D; ++dim)
106  {
107  if (source.distributions_[dim])
108  {
109  // clone source model
110  ModelDescription<1> desc(source.distributions_[dim]);
111  setModel(dim, desc.createModel());
112  }
113  else
114  {
115  distributions_[dim] = nullptr;
116  }
117  }
118  updateMembers_();
119 
120  return *this;
121  }
122 
124  IntensityType getIntensity(const PositionType & pos) const override
125  {
126  IntensityType intens(scale_);
127  for (UInt dim = 0; dim < D; ++dim)
128  {
129  if (distributions_[dim] == nullptr)
130  {
131  throw Exception::BaseException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("ProductModel: model for dimension ") + dim + " not set.", "");
132  }
133  intens *= distributions_[dim]->getIntensity(pos[dim]);
134  }
135  return intens;
136  }
137 
139  static BaseModel<D> * create()
140  {
141  return new ProductModel<D>();
142  }
143 
145  static const String getProductName()
146  {
147  return String("ProductModel") + D + "D";
148  }
149 
158  {
159  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
160  if (dist == nullptr || dist == distributions_[dim])
161  {
162  return *this;
163  }
164 
165  delete distributions_[dim];
166  distributions_[dim] = dist;
167 
168  // Update model info
170  this->param_.removeAll(name + ':');
171  this->param_.insert(name + ':', distributions_[dim]->getParameters());
172  this->param_.setValue(name, distributions_[dim]->getName());
173 
174  return *this;
175  }
176 
178  {
179  OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
180  return distributions_[dim];
181  }
182 
185  {
186  return scale_;
187  }
188 
191  {
192  this->setCutOff(this->getCutOff() / scale_); // remove scaling from cutoff
193  scale_ = scale;
194  this->param_.setValue("intensity_scaling", scale);
195  this->setCutOff(this->getCutOff() * scale_); // scale cutoff
196  }
197 
199  void getSamples(SamplesType & cont) const override
200  {
201  cont.clear();
202  typedef BaseModel<1>::SamplesType Samples1D;
203  std::vector<Samples1D> samples(D);
204  // get samples for each dimension
205  for (Size dim = 0; dim < D; ++dim)
206  {
207  distributions_[dim]->getSamples(samples[dim]);
208  }
209 
211  std::vector<UInt> i(D, 0); // index vector
212 
213  while (i[D - 1] < samples[D - 1].size())
214  {
215  for (UInt dim = 0; dim < D; ++dim)
216  {
217  peak.getPosition()[dim] = samples[dim][i[dim]].getPosition()[0];
218  }
219  fillIntensity(peak);
220  cont.push_back(peak);
221 
222  ++i[0];
223  for (Size dim = 0; dim < D - 1; ++dim)
224  {
225  if (i[dim] >= samples[dim].size())
226  {
227  i[dim] = 0;
228  ++i[dim + 1];
229  }
230  }
231  }
232  return;
233  }
234 
235 protected:
236  void updateMembers_() override
237  {
239  scale_ = (double)(this->param_.getValue("intensity_scaling"));
240  for (UInt dim = 0; dim < D; ++dim)
241  {
243  if (this->param_.exists(name))
244  {
245  delete distributions_[dim];
246  distributions_[dim] = Factory<BaseModel<1> >::create(this->param_.getValue(name).toString());
247  Param copy = this->param_.copy(name + ":", true);
248  distributions_[dim]->setParameters(copy);
249  if (distributions_[dim]->getName().hasSubstring("IsotopeModel"))
250  {
251  static_cast<IsotopeModel *>(distributions_[dim])->setSamples(static_cast<IsotopeModel *>(distributions_[dim])->getFormula());
252  }
253  }
254  }
255  }
256 
257  std::vector<BaseModel<1> *> distributions_;
259  };
260 }
261 
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:65
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct.
Definition: Factory.h:37
Isotope distribution approximated using linear interpolation.
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:184
std::vector< BaseModel< 1 > * > distributions_
Definition: ProductModel.h:257
IntensityType scale_
Definition: ProductModel.h:258
DPosition< D > PositionType
Definition: ProductModel.h:47
BaseModel< D >::SamplesType SamplesType
Definition: ProductModel.h:48
ProductModel()
Default constructor.
Definition: ProductModel.h:51
void setScale(IntensityType scale)
set the intensity scaling factor
Definition: ProductModel.h:190
static BaseModel< D > * create()
create new ProductModel object (needed by Factory)
Definition: ProductModel.h:139
virtual ProductModel & operator=(const ProductModel &source)
assignment operator
Definition: ProductModel.h:98
BaseModel< 1 > * getModel(UInt dim) const
Definition: ProductModel.h:177
IntensityType getIntensity(const PositionType &pos) const override
intensity equals product of intensities in each dimension
Definition: ProductModel.h:124
ProductModel & setModel(UInt dim, BaseModel< 1 > *dist)
set model dist for dimension dim
Definition: ProductModel.h:157
~ProductModel() override
destructor
Definition: ProductModel.h:89
ProductModel(const ProductModel &source)
copy constructor
Definition: ProductModel.h:71
void getSamples(SamplesType &cont) const override
get reasonable set of samples from the model (i.e. for printing)
Definition: ProductModel.h:199
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: ProductModel.h:236
double IntensityType
Definition: ProductModel.h:46
static const String getProductName()
Returns the name of the model.
Definition: ProductModel.h:145
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:34
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:112
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22