QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <qnumeric.h>
22 
23 /***************************************************************************
24  * This class is considered CRITICAL and any change MUST be accompanied with
25  * full unit tests in testqgsstatisticalsummary.cpp.
26  * See details in QEP #17
27  ****************************************************************************/
28 
41 class CORE_EXPORT QgsStatisticalSummary
42 {
43  public:
44 
46  enum Statistic
47  {
48  Count = 1,
49  CountMissing = 32770,
50  Sum = 2,
51  Mean = 4,
52  Median = 8,
53  StDev = 16,
54  StDevSample = 32,
55  Min = 64,
56  Max = 128,
57  Range = 256,
58  Minority = 512,
59  Majority = 1024,
60  Variety = 2048,
61  FirstQuartile = 4096,
62  ThirdQuartile = 8192,
63  InterQuartileRange = 16384,
64  All = Count | CountMissing | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange
65  };
66  Q_DECLARE_FLAGS( Statistics, Statistic )
67 
68 
71  QgsStatisticalSummary( const QgsStatisticalSummary::Statistics& stats = All );
72 
73  virtual ~QgsStatisticalSummary();
74 
79  Statistics statistics() const { return mStatistics; }
80 
86  void setStatistics( const Statistics& stats ) { mStatistics = stats; }
87 
90  void reset();
91 
95  void calculate( const QList<double>& values );
96 
111  void addValue( double value );
112 
127  void addVariant( const QVariant& value );
128 
135  void finalize();
136 
142  double statistic( Statistic stat ) const;
143 
146  int count() const { return mCount; }
147 
151  int countMissing() const { return mMissing; }
152 
155  double sum() const { return mSum; }
156 
160  double mean() const { return mMean; }
161 
166  double median() const { return mMedian; }
167 
171  double min() const { return mMin; }
172 
176  double max() const { return mMax; }
177 
181  double range() const { return qIsNaN( mMax ) || qIsNaN( mMin ) ? std::numeric_limits<double>::quiet_NaN() : mMax - mMin; }
182 
188  double stDev() const { return mStdev; }
189 
195  double sampleStDev() const { return mSampleStdev; }
196 
201  int variety() const { return mValueCount.count(); }
202 
209  double minority() const { return mMinority; }
210 
217  double majority() const { return mMajority; }
218 
225  double firstQuartile() const { return mFirstQuartile; }
226 
233  double thirdQuartile() const { return mThirdQuartile; }
234 
241  double interQuartileRange() const { return qIsNaN( mThirdQuartile ) || qIsNaN( mFirstQuartile ) ? std::numeric_limits<double>::quiet_NaN() : mThirdQuartile - mFirstQuartile; }
242 
246  static QString displayName( Statistic statistic );
247 
248  private:
249 
250  Statistics mStatistics;
251 
252  int mCount;
253  int mMissing;
254  double mSum;
255  double mMean;
256  double mMedian;
257  double mMin;
258  double mMax;
259  double mStdev;
260  double mSampleStdev;
261  double mMinority;
262  double mMajority;
263  double mFirstQuartile;
264  double mThirdQuartile;
265  QMap< double, int > mValueCount;
266  QList< double > mValues;
267 };
268 
269 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsStatisticalSummary::Statistics )
270 
271 #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.
double sampleStDev() const
Returns sample standard deviation.
void setStatistics(const Statistics &stats)
Sets flags which specify which statistics will be calculated.
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 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.
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.