QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsstatisticalsummary.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstatisticalsummary.h
3  --------------------------------------
4  Date : May 2015
5  Copyright : (C) 2015 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSSTATISTICALSUMMARY_H
17 #define QGSSTATISTICALSUMMARY_H
18 
19 #include <QMap>
20 #include <QVariant>
21 #include <cmath>
22 #include "qgis_core.h"
23 
24 /***************************************************************************
25  * This class is considered CRITICAL and any change MUST be accompanied with
26  * full unit tests in testqgsstatisticalsummary.cpp.
27  * See details in QEP #17
28  ****************************************************************************/
29 
43 class CORE_EXPORT QgsStatisticalSummary
44 {
45  public:
46 
48  enum Statistic
49  {
50  Count = 1,
51  CountMissing = 32770,
52  Sum = 2,
53  Mean = 4,
54  Median = 8,
55  StDev = 16,
56  StDevSample = 32,
57  Min = 64,
58  Max = 128,
59  Range = 256,
60  Minority = 512,
61  Majority = 1024,
62  Variety = 2048,
63  FirstQuartile = 4096,
64  ThirdQuartile = 8192,
65  InterQuartileRange = 16384,
66  All = Count | CountMissing | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange
67  };
68  Q_DECLARE_FLAGS( Statistics, Statistic )
69 
70 
75 
76  virtual ~QgsStatisticalSummary() = default;
77 
83  Statistics statistics() const { return mStatistics; }
84 
91  void setStatistics( QgsStatisticalSummary::Statistics stats );
92 
96  void reset();
97 
102  void calculate( const QList<double> &values );
103 
119  void addValue( double value );
120 
136  void addVariant( const QVariant &value );
137 
145  void finalize();
146 
153  double statistic( QgsStatisticalSummary::Statistic stat ) const;
154 
158  int count() const { return mCount; }
159 
164  int countMissing() const { return mMissing; }
165 
169  double sum() const { return mSum; }
170 
175  double mean() const { return mMean; }
176 
182  double median() const { return mMedian; }
183 
188  double min() const { return mMin; }
189 
194  double max() const { return mMax; }
195 
200  double range() const { return std::isnan( mMax ) || std::isnan( mMin ) ? std::numeric_limits<double>::quiet_NaN() : mMax - mMin; }
201 
208  double stDev() const { return mStdev; }
209 
216  double sampleStDev() const { return mSampleStdev; }
217 
223  int variety() const { return mValueCount.count(); }
224 
232  double minority() const { return mMinority; }
233 
241  double majority() const { return mMajority; }
242 
250  double firstQuartile() const { return mFirstQuartile; }
251 
259  double thirdQuartile() const { return mThirdQuartile; }
260 
268  double interQuartileRange() const { return std::isnan( mThirdQuartile ) || std::isnan( mFirstQuartile ) ? std::numeric_limits<double>::quiet_NaN() : mThirdQuartile - mFirstQuartile; }
269 
274  static QString displayName( QgsStatisticalSummary::Statistic statistic );
275 
276  private:
277 
278  Statistics mStatistics;
279 
280  int mCount;
281  int mMissing;
282  double mSum;
283  double mMean;
284  double mMedian;
285  double mMin;
286  double mMax;
287  double mStdev;
288  double mSampleStdev;
289  double mMinority;
290  double mMajority;
291  double mFirstQuartile;
292  double mThirdQuartile;
293  QMap< double, int > mValueCount;
294  QList< double > mValues;
295  bool mRequiresAllValueStorage = false;
296  bool mRequiresHisto = false;
297 };
298 
299 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsStatisticalSummary::Statistics )
300 
301 #endif // QGSSTATISTICALSUMMARY_H
Statistic
Enumeration of flags that specify statistics to be calculated.
double min() const
Returns calculated minimum from values.
int variety() const
Returns variety of values.
double mean() const
Returns calculated mean of values.
double firstQuartile() const
Returns the first quartile of the values.
double range() const
Returns calculated range (difference between maximum and minimum values).
int count() const
Returns calculated count of values.
double minority() const
Returns minority of values.
double stDev() const
Returns population standard deviation.
double sampleStDev() const
Returns sample standard deviation.
int countMissing() const
Returns the number of missing (null) values.
double median() const
Returns calculated median of values.
double majority() const
Returns majority of values.
double sum() const
Returns calculated sum of values.
double thirdQuartile() const
Returns the third quartile of the values.
double max() const
Returns calculated maximum from values.
Calculator for summary statistics for a list of doubles.
double interQuartileRange() const
Returns the inter quartile range of the values.