QGIS API Documentation  3.15.0-Master (ecc6bfcd7a)
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 << 0,
51  CountMissing = 1 << 15,
52  Sum = 1 << 1,
53  Mean = 1 << 2,
54  Median = 1 << 3,
55  StDev = 1 << 4,
56  StDevSample = 1 << 5,
57  Min = 1 << 6,
58  Max = 1 << 7,
59  Range = 1 << 8,
60  Minority = 1 << 9,
61  Majority = 1 << 10,
62  Variety = 1 << 11,
63  FirstQuartile = 1 << 12,
64  ThirdQuartile = 1 << 13,
65  InterQuartileRange = 1 << 14,
66  First = 1 << 16,
67  Last = 1 << 17,
68  All = Count | CountMissing | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange | First | Last
69  };
70  Q_DECLARE_FLAGS( Statistics, Statistic )
71 
72 
76  QgsStatisticalSummary( QgsStatisticalSummary::Statistics stats = QgsStatisticalSummary::All );
77 
78  virtual ~QgsStatisticalSummary() = default;
79 
85  Statistics statistics() const { return mStatistics; }
86 
93  void setStatistics( QgsStatisticalSummary::Statistics stats );
94 
98  void reset();
99 
104  void calculate( const QList<double> &values );
105 
121  void addValue( double value );
122 
138  void addVariant( const QVariant &value );
139 
147  void finalize();
148 
155  double statistic( QgsStatisticalSummary::Statistic stat ) const;
156 
160  int count() const { return mCount; }
161 
166  int countMissing() const { return mMissing; }
167 
171  double sum() const { return mSum; }
172 
177  double mean() const { return mMean; }
178 
184  double median() const { return mMedian; }
185 
190  double min() const { return mMin; }
191 
196  double max() const { return mMax; }
197 
202  double range() const { return std::isnan( mMax ) || std::isnan( mMin ) ? std::numeric_limits<double>::quiet_NaN() : mMax - mMin; }
203 
210  double first() const { return mFirst; }
211 
218  double last() const { return mLast; }
219 
226  double stDev() const { return mStdev; }
227 
234  double sampleStDev() const { return mSampleStdev; }
235 
241  int variety() const { return mValueCount.count(); }
242 
250  double minority() const { return mMinority; }
251 
259  double majority() const { return mMajority; }
260 
268  double firstQuartile() const { return mFirstQuartile; }
269 
277  double thirdQuartile() const { return mThirdQuartile; }
278 
286  double interQuartileRange() const { return std::isnan( mThirdQuartile ) || std::isnan( mFirstQuartile ) ? std::numeric_limits<double>::quiet_NaN() : mThirdQuartile - mFirstQuartile; }
287 
292  static QString displayName( QgsStatisticalSummary::Statistic statistic );
293 
299  static QString shortName( QgsStatisticalSummary::Statistic statistic );
300 
301  private:
302 
303  Statistics mStatistics;
304 
305  int mCount;
306  int mMissing;
307  double mSum;
308  double mMean;
309  double mMedian;
310  double mMin;
311  double mMax;
312  double mStdev;
313  double mSampleStdev;
314  double mMinority;
315  double mMajority;
316  double mFirstQuartile;
317  double mThirdQuartile;
318  double mFirst;
319  double mLast;
320  QMap< double, int > mValueCount;
321  QList< double > mValues;
322  bool mRequiresAllValueStorage = false;
323  bool mRequiresHisto = false;
324 };
325 
326 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsStatisticalSummary::Statistics )
327 
328 #endif // QGSSTATISTICALSUMMARY_H
double max() const
Returns calculated maximum from values.
Statistic
Enumeration of flags that specify statistics to be calculated.
int variety() const
Returns variety of values.
double minority() const
Returns minority of values.
double range() const
Returns calculated range (difference between maximum and minimum values).
double median() const
Returns calculated median of values.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
double sampleStDev() const
Returns sample standard deviation.
double firstQuartile() const
Returns the first quartile of the values.
double stDev() const
Returns population standard deviation.
double sum() const
Returns calculated sum of values.
double mean() const
Returns calculated mean of values.
double majority() const
Returns majority of values.
double first() const
Returns the first value obtained.
double interQuartileRange() const
Returns the inter quartile range of the values.
int countMissing() const
Returns the number of missing (null) values.
Statistics statistics() const
Returns flags which specify which statistics will be calculated.
double last() const
Returns the last value obtained.
int count() const
Returns calculated count of values.
double min() const
Returns calculated minimum from values.
Calculator for summary statistics for a list of doubles.
double thirdQuartile() const
Returns the third quartile of the values.