Skip to content

Commit 1c45b94

Browse files
committedMay 17, 2016
Add calculation of number of null values to QgsStatisticalSummary
1 parent 4dea723 commit 1c45b94

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed
 

‎python/core/qgsstatisticalsummary.sip

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class QgsStatisticalSummary
2222
enum Statistic
2323
{
2424
Count, //!< Count
25+
CountMissing, //!< Number of missing (null) values
2526
Sum, //!< Sum of values
2627
Mean, //!< Mean of values
2728
Median, //!< Median of values
@@ -79,15 +80,36 @@ class QgsStatisticalSummary
7980
* @note finalize() must be called after adding the final value and before
8081
* retrieving calculated statistics.
8182
* @see calculate()
83+
* @see addVariant()
8284
* @see finalize()
85+
* @note added in QGIS 2.16
8386
*/
8487
void addValue( double value );
8588

89+
/** Adds a single value to the statistics calculation. Calling this method
90+
* allows values to be added to the calculation one at a time. For large
91+
* quantities of values this may be more efficient then first adding all the
92+
* values to a list and calling calculate().
93+
* @param value variant containing to add. Non-numeric values are treated as null.
94+
* @note call reset() before adding the first value using this method
95+
* to clear the results from any previous calculations
96+
* @note finalize() must be called after adding the final value and before
97+
* retrieving calculated statistics.
98+
* @see addValue()
99+
* @see calculate()
100+
* @see finalize()
101+
* @note added in QGIS 2.16
102+
*/
103+
void addVariant( const QVariant& value );
104+
86105
/** Must be called after adding all values with addValues() and before retrieving
87106
* any calculated statistics.
88107
* @see addValue()
108+
* @see addVariant()
109+
* @note added in QGIS 2.16
89110
*/
90111
void finalize();
112+
91113
/** Returns the value of a specified statistic
92114
* @param stat statistic to return
93115
* @returns calculated value of statistic

‎src/core/qgsstatisticalsummary.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ QgsStatisticalSummary::~QgsStatisticalSummary()
3939
void QgsStatisticalSummary::reset()
4040
{
4141
mCount = 0;
42+
mMissing = 0;
4243
mSum = 0;
4344
mMean = 0;
4445
mMedian = 0;
@@ -88,6 +89,21 @@ void QgsStatisticalSummary::addValue( double value )
8889
mValues << value;
8990
}
9091

92+
void QgsStatisticalSummary::addVariant( const QVariant& value )
93+
{
94+
bool convertOk = false;
95+
if ( !value.isValid() || value.isNull() )
96+
mMissing++;
97+
else
98+
{
99+
double val = value.toDouble( &convertOk );
100+
if ( convertOk )
101+
addValue( val );
102+
else
103+
mMissing++;
104+
}
105+
}
106+
91107
void QgsStatisticalSummary::finalize()
92108
{
93109
if ( mCount == 0 )
@@ -214,6 +230,8 @@ double QgsStatisticalSummary::statistic( QgsStatisticalSummary::Statistic stat )
214230
{
215231
case Count:
216232
return mCount;
233+
case CountMissing:
234+
return mMissing;
217235
case Sum:
218236
return mSum;
219237
case Mean:
@@ -254,6 +272,8 @@ QString QgsStatisticalSummary::displayName( QgsStatisticalSummary::Statistic sta
254272
{
255273
case Count:
256274
return QObject::tr( "Count" );
275+
case CountMissing:
276+
return QObject::tr( "Count (missing)" );
257277
case Sum:
258278
return QObject::tr( "Sum" );
259279
case Mean:

‎src/core/qgsstatisticalsummary.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define QGSSTATISTICALSUMMARY_H
1818

1919
#include <QMap>
20+
#include <QVariant>
2021

2122
/***************************************************************************
2223
* This class is considered CRITICAL and any change MUST be accompanied with
@@ -44,6 +45,7 @@ class CORE_EXPORT QgsStatisticalSummary
4445
enum Statistic
4546
{
4647
Count = 1, //!< Count
48+
CountMissing = 32770, //!< Number of missing (null) values
4749
Sum = 2, //!< Sum of values
4850
Mean = 4, //!< Mean of values
4951
Median = 8, //!< Median of values
@@ -58,7 +60,7 @@ class CORE_EXPORT QgsStatisticalSummary
5860
FirstQuartile = 4096, //!< First quartile
5961
ThirdQuartile = 8192, //!< Third quartile
6062
InterQuartileRange = 16384, //!< Inter quartile range (IQR)
61-
All = Count | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange
63+
All = Count | CountMissing | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange
6264
};
6365
Q_DECLARE_FLAGS( Statistics, Statistic )
6466

@@ -101,13 +103,33 @@ class CORE_EXPORT QgsStatisticalSummary
101103
* @note finalize() must be called after adding the final value and before
102104
* retrieving calculated statistics.
103105
* @see calculate()
106+
* @see addVariant()
104107
* @see finalize()
108+
* @note added in QGIS 2.16
105109
*/
106110
void addValue( double value );
107111

112+
/** Adds a single value to the statistics calculation. Calling this method
113+
* allows values to be added to the calculation one at a time. For large
114+
* quantities of values this may be more efficient then first adding all the
115+
* values to a list and calling calculate().
116+
* @param value variant containing to add. Non-numeric values are treated as null.
117+
* @note call reset() before adding the first value using this method
118+
* to clear the results from any previous calculations
119+
* @note finalize() must be called after adding the final value and before
120+
* retrieving calculated statistics.
121+
* @see addValue()
122+
* @see calculate()
123+
* @see finalize()
124+
* @note added in QGIS 2.16
125+
*/
126+
void addVariant( const QVariant& value );
127+
108128
/** Must be called after adding all values with addValues() and before retrieving
109129
* any calculated statistics.
110130
* @see addValue()
131+
* @see addVariant()
132+
* @note added in QGIS 2.16
111133
*/
112134
void finalize();
113135

@@ -121,6 +143,10 @@ class CORE_EXPORT QgsStatisticalSummary
121143
*/
122144
int count() const { return mCount; }
123145

146+
/** Returns the number of missing (null) values
147+
*/
148+
int countMissing() const { return mMissing; }
149+
124150
/** Returns calculated sum of values
125151
*/
126152
double sum() const { return mSum; }
@@ -209,6 +235,7 @@ class CORE_EXPORT QgsStatisticalSummary
209235
Statistics mStatistics;
210236

211237
int mCount;
238+
int mMissing;
212239
double mSum;
213240
double mMean;
214241
double mMedian;

‎tests/src/core/testqgsstatisticalsummary.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class TestQgsStatisticSummary: public QObject
3535
void individualStatCalculations_data();
3636
void individualStatCalculations();
3737
void maxMin();
38+
void countMissing();
3839

3940
private:
4041

@@ -225,6 +226,7 @@ void TestQgsStatisticSummary::individualStatCalculations_data()
225226
QTest::newRow( "first_quartile" ) << ( int )QgsStatisticalSummary::FirstQuartile << 3.0;
226227
QTest::newRow( "third_quartile" ) << ( int )QgsStatisticalSummary::ThirdQuartile << 5.0;
227228
QTest::newRow( "iqr" ) << ( int )QgsStatisticalSummary::InterQuartileRange << 2.0;
229+
QTest::newRow( "missing" ) << ( int )QgsStatisticalSummary::CountMissing << 0.0;
228230
}
229231

230232
void TestQgsStatisticSummary::individualStatCalculations()
@@ -281,5 +283,21 @@ void TestQgsStatisticSummary::maxMin()
281283
QCOMPARE( s.max(), -5.0 );
282284
}
283285

286+
void TestQgsStatisticSummary::countMissing()
287+
{
288+
QgsStatisticalSummary s( QgsStatisticalSummary::All );
289+
s.addVariant( 5 );
290+
s.addVariant( 6 );
291+
s.addVariant( QVariant() );
292+
s.addVariant( 7 );
293+
s.addVariant( QVariant( QVariant::Double ) );
294+
s.addVariant( 9 );
295+
s.addVariant( "Asdasdsad" );
296+
s.finalize();
297+
298+
QCOMPARE( s.countMissing(), 3 );
299+
QCOMPARE( s.statistic( QgsStatisticalSummary::CountMissing ), 3.0 );
300+
}
301+
284302
QTEST_MAIN( TestQgsStatisticSummary )
285303
#include "testqgsstatisticalsummary.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.