2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Declares analysis data modules for calculating histograms.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
44 #define GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
46 #include "../abstractdata.h"
47 #include "../arraydata.h"
48 #include "../datamodule.h"
49 #include "../../utility/uniqueptr.h"
54 class AnalysisHistogramSettings;
57 * Provides "named parameter" idiom for constructing histograms.
59 * \see histogramFromBins()
60 * \see histogramFromRange()
62 * Methods in this class do not throw.
65 * \ingroup module_analysisdata
67 class AnalysisHistogramSettingsInitializer
71 * Creates an empty initializer.
73 * Should not be called directly, but histogramFromRange() or
74 * histogramFromBins() should be used instead.
76 AnalysisHistogramSettingsInitializer();
79 * Sets the first bin location.
81 * Typically should not be called directly, but through
82 * histogramFromBins().
84 AnalysisHistogramSettingsInitializer &start(real min)
85 { min_ = min; return *this; }
87 * Sets the number of bins in the histogram.
89 * If only the first bin location is specified, this value is required
90 * (and automatically provided if histogramFromBins() is used).
91 * If both the first and last bins are specified, either this value or
92 * binWidth() is required.
94 AnalysisHistogramSettingsInitializer &binCount(int binCount)
95 { binCount_ = binCount; return *this; }
97 * Sets the first and last bin locations.
99 * Typically should not be called directly, but through
100 * histogramFromRange().
102 AnalysisHistogramSettingsInitializer &range(real min, real max)
103 { min_ = min; max_ = max; return *this; }
105 * Sets the bin width of the histogram.
107 * If only the first bin location is specified, this value is required
108 * (and automatically provided if histogramFromBins() is used).
109 * If both the first and last bins are specified, either this value or
110 * binCount() is required.
111 * If a bin width is provided with both first and last bin locations,
112 * and the given bin width does not divide the range exactly, the last
113 * bin location is adjusted to match.
115 AnalysisHistogramSettingsInitializer &binWidth(real binWidth)
116 { binWidth_ = binWidth; return *this; }
118 * Indicate that first and last bin locations to specify bin centers.
120 * If set, the first and last bin locations are interpreted as bin
122 * If not set (the default), the first and last bin locations are
123 * interpreted as the edges of the whole histogram.
125 * Cannot be specified together with roundRange().
127 AnalysisHistogramSettingsInitializer &integerBins(bool enabled = true)
128 { bIntegerBins_ = enabled; return *this; }
130 * Round first and last bin locations.
132 * If set, the resulting histogram will cover the range specified, but
133 * the actual bin locations will be rounded such that the edges fall
134 * on multiples of the bin width.
135 * Only implemented when both first and last bin location and bin width
137 * Cannot be specified together with integerBins() or with binCount().
139 AnalysisHistogramSettingsInitializer &roundRange(bool enabled = true)
140 { bRoundRange_ = enabled; return *this; }
142 * Sets the histogram to match all values.
144 * If set, the histogram behaves as if the bins at the ends extended to
147 AnalysisHistogramSettingsInitializer &includeAll(bool enabled = true)
148 { bIncludeAll_ = enabled; return *this; }
159 friend class AnalysisHistogramSettings;
163 * Initializes a histogram using a range and a bin width.
169 inline AnalysisHistogramSettingsInitializer
170 histogramFromRange(real min, real max)
172 return AnalysisHistogramSettingsInitializer().range(min, max);
176 * Initializes a histogram using bin width and the number of bins.
182 inline AnalysisHistogramSettingsInitializer
183 histogramFromBins(real start, int nbins, real binwidth)
185 return AnalysisHistogramSettingsInitializer()
186 .start(start).binCount(nbins).binWidth(binwidth);
191 * Contains parameters that specify histogram bin locations.
193 * Methods in this class do not throw.
196 * \ingroup module_analysisdata
198 class AnalysisHistogramSettings
201 //! Initializes undefined parameters.
202 AnalysisHistogramSettings();
204 * Initializes parameters based on a named parameter object.
206 * This constructor is not explicit to allow initialization of
207 * histograms directly from AnalysisHistogramSettingsInitializer:
209 gmx::AnalysisDataSimpleHistogramModule *hist =
210 new gmx::AnalysisDataSimpleHistogramModule(
211 histogramFromRange(0.0, 5.0).binWidth(0.5));
214 AnalysisHistogramSettings(const AnalysisHistogramSettingsInitializer &settings);
216 //! Returns the left edge of the first bin.
217 real firstEdge() const { return firstEdge_; }
218 //! Returns the right edge of the first bin.
219 real lastEdge() const { return lastEdge_; }
220 //! Returns the number of bins in the histogram.
221 int binCount() const { return binCount_; }
222 //! Returns the width of a bin in the histogram.
223 real binWidth() const { return binWidth_; }
224 //! Whether values beyond the edges are mapped to the edge bins.
225 bool includeAll() const { return bAll_; }
226 //! Returns a zero-based bin index for a value, or -1 if not in range.
227 int findBin(real y) const;
233 real inverseBinWidth_;
242 class BasicHistogramImpl;
244 } // namespace internal
246 class AbstractAverageHistogram;
248 //! Smart pointer to manage an AbstractAverageHistogram object.
249 typedef gmx_unique_ptr<AbstractAverageHistogram>::type
250 AverageHistogramPointer;
253 * Base class for representing histograms averaged over frames.
255 * The averaging module for a per-frame histogram is always created by the
256 * histogram module class (e.g., AnalysisDataSimpleHistogramModule), and can be
257 * accessed using, e.g., AnalysisDataSimpleHistogramModule::averager().
258 * The user can alter some properties of the average histogram directly, but
259 * the main use of the object is to postprocess the histogram once the
260 * calculation is finished.
263 * \ingroup module_analysisdata
265 class AbstractAverageHistogram : public AbstractAnalysisArrayData
268 virtual ~AbstractAverageHistogram();
270 //! Returns bin properties for the histogram.
271 const AnalysisHistogramSettings &settings() const { return settings_; }
274 * Creates a copy of the histogram with double the bin width.
276 * \throws std::bad_alloc if out of memory.
278 * The caller is responsible of deleting the returned object.
280 AverageHistogramPointer resampleDoubleBinWidth(bool bIntegerBins) const;
282 * Creates a deep copy of the histogram.
284 * \throws std::bad_alloc if out of memory.
286 * The returned histogram is not necessarily of the same dynamic type
287 * as the original object, but contains the same data from the point of
288 * view of the AbstractAverageHistogram interface.
290 * The caller is responsible of deleting the returned object.
292 AverageHistogramPointer clone() const;
293 //! Normalizes the histogram such that the integral over it is one.
294 void normalizeProbability();
295 //! Scales the value of each bin by an uniform scaling factor.
296 void scale(real norm);
297 //! Scales the value of each bin by a different scaling factor.
298 void scaleVector(real norm[]);
300 * Notifies attached modules of the histogram data.
302 * After this function has been called, it is no longer possible to
303 * alter the histogram.
305 void done() { AbstractAnalysisArrayData::valuesReady(); }
309 * Creates a histogram module with undefined bins.
311 * Bin parameters must be defined with init() before data input is
314 AbstractAverageHistogram();
315 //! Creates a histogram module with defined bin parameters.
316 explicit AbstractAverageHistogram(const AnalysisHistogramSettings &settings);
319 * (Re)initializes the histogram from settings.
321 void init(const AnalysisHistogramSettings &settings);
324 AnalysisHistogramSettings settings_;
326 // Copy and assign disallowed by base.
331 * Data module for per-frame histograms.
333 * Output data contains the same number of frames as the input data.
334 * Each frame contains the histogram for the points in that frame.
335 * All input columns are averaged into the same histogram.
336 * The number of columns equals the number of bins in the histogram.
339 * \ingroup module_analysisdata
341 class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
342 public AnalysisDataModuleInterface
346 * Creates a histogram module with undefined bins.
348 * Bin parameters must be defined with init() before data input is
351 AnalysisDataSimpleHistogramModule();
352 //! Creates a histogram module with defined bin parameters.
353 explicit AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &settings);
354 virtual ~AnalysisDataSimpleHistogramModule();
357 * (Re)initializes the histogram from settings.
359 void init(const AnalysisHistogramSettings &settings);
362 * Returns the average histogram over all frames.
364 * Can be called already before the histogram is calculated to
365 * customize the way the average histogram is calculated.
367 * \see AbstractAverageHistogram
369 AbstractAverageHistogram &averager();
371 //! Returns bin properties for the histogram.
372 const AnalysisHistogramSettings &settings() const;
374 virtual int flags() const;
376 virtual void dataStarted(AbstractAnalysisData *data);
377 virtual void frameStarted(const AnalysisDataFrameHeader &header);
378 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
379 virtual void frameFinished(const AnalysisDataFrameHeader &header);
380 virtual void dataFinished();
383 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
384 virtual bool requestStorageInternal(int nframes);
386 PrivateImplPointer<internal::BasicHistogramImpl> impl_;
388 // Copy and assign disallowed by base.
393 * Data module for per-frame weighted histograms.
395 * Output data contains the same number of frames as the input data.
396 * Each frame contains the histogram for the points in that frame, interpreted
397 * such that the first column passed to pointsAdded() determines the bin and
398 * the rest give weights to be added to that bin (input data should have at
399 * least two colums, and at least two columns should be added at the same time).
400 * All input columns are averaged into the same histogram.
401 * The number of columns equals the number of bins in the histogram.
404 * \ingroup module_analysisdata
406 class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
407 public AnalysisDataModuleInterface
410 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
411 AnalysisDataWeightedHistogramModule();
412 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &)
413 explicit AnalysisDataWeightedHistogramModule(const AnalysisHistogramSettings &settings);
414 virtual ~AnalysisDataWeightedHistogramModule();
416 //! \copydoc AnalysisDataSimpleHistogramModule::init()
417 void init(const AnalysisHistogramSettings &settings);
419 //! \copydoc AnalysisDataSimpleHistogramModule::averager()
420 AbstractAverageHistogram &averager();
422 //! \copydoc AnalysisDataSimpleHistogramModule::settings()
423 const AnalysisHistogramSettings &settings() const;
425 virtual int flags() const;
427 virtual void dataStarted(AbstractAnalysisData *data);
428 virtual void frameStarted(const AnalysisDataFrameHeader &header);
429 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
430 virtual void frameFinished(const AnalysisDataFrameHeader &header);
431 virtual void dataFinished();
434 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
435 virtual bool requestStorageInternal(int nframes);
437 PrivateImplPointer<internal::BasicHistogramImpl> impl_;
439 // Copy and assign disallowed by base.
444 * Data module for bin averages.
446 * Output data contains one row for each bin; see AbstractAverageHistogram.
447 * Output data contains three columns: the first is the average over all frames
448 * for that bin, the second is the standard deviation of the values, and the
449 * third is the number of samples in that bin.
450 * The input data is interpreted such that the first column passed to
451 * pointsAdded() determines the bin and the rest give values to be added to
452 * that bin (input data should have at least two colums, and at least two
453 * columns should be added at the same time).
454 * All input columns are averaged into the same histogram.
457 * \ingroup module_analysisdata
459 class AnalysisDataBinAverageModule : public AbstractAnalysisArrayData,
460 public AnalysisDataModuleInterface
463 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
464 AnalysisDataBinAverageModule();
465 //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule(const AnalysisHistogramSettings &)
466 explicit AnalysisDataBinAverageModule(const AnalysisHistogramSettings &settings);
467 virtual ~AnalysisDataBinAverageModule();
469 //! \copydoc AnalysisDataSimpleHistogramModule::init()
470 void init(const AnalysisHistogramSettings &settings);
472 //! \copydoc AnalysisDataSimpleHistogramModule::settings()
473 const AnalysisHistogramSettings &settings() const { return settings_; }
475 virtual int flags() const;
477 virtual void dataStarted(AbstractAnalysisData *data);
478 virtual void frameStarted(const AnalysisDataFrameHeader &header);
479 virtual void pointsAdded(const AnalysisDataPointSetRef &points);
480 virtual void frameFinished(const AnalysisDataFrameHeader &header);
481 virtual void dataFinished();
484 AnalysisHistogramSettings settings_;
486 // Copy and assign disallowed by base.
489 //! Smart pointer to manage an AnalysisDataSimpleHistogramModule object.
490 typedef boost::shared_ptr<AnalysisDataSimpleHistogramModule>
491 AnalysisDataSimpleHistogramModulePointer;
492 //! Smart pointer to manage an AnalysisDataWeightedHistogramModule object.
493 typedef boost::shared_ptr<AnalysisDataWeightedHistogramModule>
494 AnalysisDataWeightedHistogramModulePointer;
495 //! Smart pointer to manage an AnalysisDataBinAverageModule object.
496 typedef boost::shared_ptr<AnalysisDataBinAverageModule>
497 AnalysisDataBinAverageModulePointer;