| OpenMS 2.5.0

ProductModel.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34
35
36 #pragma once
37
38 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/BaseModel.h>
39 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/ModelDescription.h>
40 #include <OpenMS/KERNEL/Peak2D.h>
41 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/IsotopeModel.h>
42 #include <OpenMS/CHEMISTRY/EmpiricalFormula.h>
43
44 namespace OpenMS
45 {
46
59 template <UInt D>
60 class ProductModel;
61
63 template <>
64 class OPENMS_DLLAPI ProductModel<2>:
65 public BaseModel<2>
66 {
67 public:
68
70 enum {D = 2};
71
72 typedef double IntensityType;
73 typedef DPosition<D> PositionType;
74 typedef BaseModel<D>::SamplesType SamplesType;
75
77 ProductModel() :
78 BaseModel<D>(),
79 distributions_(D, nullptr)
80 {
81 this->setName(this->getProductName());
82
83 //Register model info
84 for (UInt dim = 0; dim < D; ++dim)
85 {
86 String name = Peak2D::shortDimensionName(dim);
87 this->subsections_.push_back(name);
88 this->defaults_.setValue(name, "GaussModel", "Name of the model used for this dimension");
89 }
90
91 //defaults
92 this->defaults_.setValue("intensity_scaling", 1.0, "Scaling factor used to adjust the model distribution to the intensities of the data");
93 this->defaultsToParam_();
94 }
95
97 ProductModel(const ProductModel & source) :
98 BaseModel<D>(source),
99 distributions_(D, nullptr),
100 scale_(source.scale_)
101 {
102 for (UInt dim = 0; dim < D; ++dim)
103 {
104 // clone source model
105 if (source.distributions_[dim])
106 {
107 ModelDescription<1> desc(source.distributions_[dim]);
108 setModel(dim, desc.createModel());
109 }
110 }
111 updateMembers_();
112 }
113
115 ~ProductModel() override
116 {
117 for (Size dim = 0; dim < D; ++dim)
118 {
119 delete distributions_[dim];
120 }
121 }
122
124 virtual ProductModel & operator=(const ProductModel & source)
125 {
126 if (&source == this) return *this;
127
128 BaseModel<D>::operator=(source);
129 scale_ = source.scale_;
130
131 for (UInt dim = 0; dim < D; ++dim)
132 {
133 if (source.distributions_[dim])
134 {
135 // clone source model
136 ModelDescription<1> desc(source.distributions_[dim]);
137 setModel(dim, desc.createModel());
138 }
139 else
140 {
141 distributions_[dim] = nullptr;
142 }
143 }
144 updateMembers_();
145
146 return *this;
147 }
148
150 IntensityType getIntensity(const PositionType & pos) const override
151 {
152 IntensityType intens(scale_);
153 for (UInt dim = 0; dim < D; ++dim)
154 {
155 if (distributions_[dim] == nullptr)
156 {
157 throw Exception::BaseException(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("ProductModel: model for dimension ") + dim + " not set.", "");
158 }
159 intens *= distributions_[dim]->getIntensity(pos[dim]);
160 }
161 return intens;
162 }
163
165 static BaseModel<D> * create()
166 {
167 return new ProductModel<D>();
168 }
169
171 static const String getProductName()
172 {
173 return String("ProductModel") + D + "D";
174 }
175
183 ProductModel & setModel(UInt dim, BaseModel<1> * dist)
184 {
185 OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
186 if (dist == nullptr || dist == distributions_[dim])
187 {
188 return *this;
189 }
190
191 delete distributions_[dim];
192 distributions_[dim] = dist;
193
194 // Update model info
195 String name = Peak2D::shortDimensionName(dim);
196 this->param_.removeAll(name + ':');
197 this->param_.insert(name + ':', distributions_[dim]->getParameters());
198 this->param_.setValue(name, distributions_[dim]->getName());
199
200 return *this;
201 }
202
203 BaseModel<1> * getModel(UInt dim) const
204 {
205 OPENMS_PRECONDITION(dim < D, "ProductModel<D>:getModel(Position): index overflow!");
206 return distributions_[dim];
207 }
208
210 IntensityType getScale() const
211 {
212 return scale_;
213 }
214
216 void setScale(IntensityType scale)
217 {
218 this->setCutOff(this->getCutOff() / scale_); // remove scaling from cutoff
219 scale_ = scale;
220 this->param_.setValue("intensity_scaling", scale);
221 this->setCutOff(this->getCutOff() * scale_); // scale cutoff
222 }
223
225 void getSamples(SamplesType & cont) const override
226 {
227 cont.clear();
228 typedef BaseModel<1>::SamplesType Samples1D;
229 std::vector<Samples1D> samples(D);
230 // get samples for each dimension
231 for (Size dim = 0; dim < D; ++dim)
232 {
233 distributions_[dim]->getSamples(samples[dim]);
234 }
235
236 BaseModel<D>::PeakType peak;
237 std::vector<UInt> i(D, 0); // index vector
238
239 while (i[D - 1] < samples[D - 1].size())
240 {
241 for (UInt dim = 0; dim < D; ++dim)
242 {
243 peak.getPosition()[dim] = samples[dim][i[dim]].getPosition()[0];
244 }
245 fillIntensity(peak);
246 cont.push_back(peak);
247
248 ++i[0];
249 for (Size dim = 0; dim < D - 1; ++dim)
250 {
251 if (i[dim] >= samples[dim].size())
252 {
253 i[dim] = 0;
254 ++i[dim + 1];
255 }
256 }
257 }
258 return;
259 }
260
261 protected:
262 void updateMembers_() override
263 {
264 BaseModel<D>::updateMembers_();
265 scale_ = (double)(this->param_.getValue("intensity_scaling"));
266 for (UInt dim = 0; dim < D; ++dim)
267 {
268 String name = Peak2D::shortDimensionName(dim);
269 if (this->param_.exists(name))
270 {
271 delete distributions_[dim];
272 distributions_[dim] = Factory<BaseModel<1> >::create(this->param_.getValue(name));
273 Param copy = this->param_.copy(name + ":", true);
274 distributions_[dim]->setParameters(copy);
275 if (distributions_[dim]->getName().hasSubstring("IsotopeModel"))
276 {
277 static_cast<IsotopeModel *>(distributions_[dim])->setSamples(static_cast<IsotopeModel *>(distributions_[dim])->getFormula());
278 }
279 }
280 }
281 }
282
283 std::vector<BaseModel<1> *> distributions_;
284 IntensityType scale_;
285 };
286 }
287
OpenMS::ProductModel< 2 >::ProductModel
ProductModel(const ProductModel &source)
copy constructor
Definition: ProductModel.h:97
OpenMS::Exception::BaseException
Exception base class.
Definition: Exception.h:89
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::BaseModel::PeakType
DPeak< D >::Type PeakType
Definition: BaseModel.h:59
OpenMS::ProductModel< 2 >::SamplesType
BaseModel< D >::SamplesType SamplesType
Definition: ProductModel.h:74
OpenMS::ProductModel< 2 >::scale_
IntensityType scale_
Definition: ProductModel.h:284
OpenMS::BaseModel::updateMembers_
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method...
Definition: BaseModel.h:157
IsotopeModel.h
OpenMS::ProductModel
Class for product models i.e. models with D independent dimensions.
Definition: ProductModel.h:60
OpenMS::ProductModel< 2 >::ProductModel
ProductModel()
Default constructor.
Definition: ProductModel.h:77
OpenMS::ProductModel< 2 >::~ProductModel
~ProductModel() override
destructor
Definition: ProductModel.h:115
OpenMS::ProductModel< 2 >::getSamples
void getSamples(SamplesType &cont) const override
get reasonable set of samples from the model (i.e. for printing)
Definition: ProductModel.h:225
OpenMS::BaseModel
Abstract base class for all D-dimensional models.
Definition: BaseModel.h:50
OpenMS::ProductModel< 2 >::getModel
BaseModel< 1 > * getModel(UInt dim) const
Definition: ProductModel.h:203
OpenMS::IsotopeModel
Isotope distribution approximated using linear interpolation.
Definition: IsotopeModel.h:58
OpenMS::ProductModel< 2 >::getIntensity
IntensityType getIntensity(const PositionType &pos) const override
intensity equals product of intensities in each dimension
Definition: ProductModel.h:150
OpenMS::ProductModel< 2 >::IntensityType
double IntensityType
Definition: ProductModel.h:72
OpenMS::ProductModel< 2 >::getScale
IntensityType getScale() const
return the intensity scaling factor
Definition: ProductModel.h:210
OpenMS::ProductModel< 2 >::setModel
ProductModel & setModel(UInt dim, BaseModel< 1 > *dist)
set model dist for dimension dim
Definition: ProductModel.h:183
OPENMS_PRECONDITION
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:136
OpenMS::ProductModel< 2 >::setScale
void setScale(IntensityType scale)
set the intensity scaling factor
Definition: ProductModel.h:216
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Peak2D.h
OpenMS::Peak2D::shortDimensionName
static char const * shortDimensionName(UInt const dim)
Short name of the dimension (abbreviated form)
OpenMS::ProductModel< 2 >::distributions_
std::vector< BaseModel< 1 > * > distributions_
Definition: ProductModel.h:283
OpenMS::BaseModel::SamplesType
std::vector< PeakType > SamplesType
Definition: BaseModel.h:60
OpenMS::ProductModel< 2 >::create
static BaseModel< D > * create()
create new ProductModel object (needed by Factory)
Definition: ProductModel.h:165
OpenMS::BaseModel::operator=
virtual BaseModel & operator=(const BaseModel &source)
assignment operator
Definition: BaseModel.h:83
OpenMS::ProductModel< 2 >::PositionType
DPosition< D > PositionType
Definition: ProductModel.h:73
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::DPosition< D >
OpenMS::ModelDescription
Stores the name and parameters of a model.
Definition: ModelDescription.h:53
double
OpenMS::ProductModel< 2 >::updateMembers_
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method...
Definition: ProductModel.h:262
EmpiricalFormula.h
OpenMS::ProductModel< 2 >::getProductName
static const String getProductName()
Returns the name of the model.
Definition: ProductModel.h:171
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::ProductModel< 2 >::operator=
virtual ProductModel & operator=(const ProductModel &source)
assignment operator
Definition: ProductModel.h:124
BaseModel.h
OpenMS::ModelDescription::createModel
BaseModel< D > * createModel()
Definition: ModelDescription.h:96
OpenMS::Param::copy
Param copy(const String &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
OpenMS::Factory
Returns FactoryProduct* based on the name of the desired concrete FactoryProduct. ...
Definition: Factory.h:61
ModelDescription.h
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127