Skip to content

Commit

Permalink
Allow QgsStringStatisticalSummary to calculate mean string length
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent 57f17e3 commit a927d97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsstringstatisticalsummary.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class QgsStringStatisticalSummary
Max, //!< Maximum string value
MinimumLength, //!< Minimum length of string
MaximumLength, //!< Maximum length of string
MeanLength, //!< Mean length of strings
All, //! All statistics
};
typedef QFlags<QgsStringStatisticalSummary::Statistic> Statistics;
Expand Down Expand Up @@ -147,6 +148,12 @@ class QgsStringStatisticalSummary
*/
int maxLength() const;

/**
* Returns the mean length of strings.
* @note added in QGIS 3.0
*/
double meanLength() const;

/** Returns the friendly display name for a statistic
* @param statistic statistic to return name for
*/
Expand Down
11 changes: 9 additions & 2 deletions src/core/qgsstringstatisticalsummary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void QgsStringStatisticalSummary::reset()
mMax.clear();
mMinLength = INT_MAX;
mMaxLength = 0;
mSumLengths = 0;
mMeanLength = 0;
}

void QgsStringStatisticalSummary::calculate( const QStringList& values )
Expand Down Expand Up @@ -71,8 +73,7 @@ void QgsStringStatisticalSummary::addValue( const QVariant& value )

void QgsStringStatisticalSummary::finalize()
{
//nothing to do for now - this method has been added for forward compatibility
//if statistics are implemented which require a post-calculation step
mMeanLength = mSumLengths / static_cast< double >( mCount );
}

void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList& values )
Expand Down Expand Up @@ -121,6 +122,8 @@ void QgsStringStatisticalSummary::testString( const QString& string )
mMax = string;
}
}
if ( mStatistics & MeanLength )
mSumLengths += string.length();
mMinLength = qMin( mMinLength, string.length() );
mMaxLength = qMax( mMaxLength, string.length() );
}
Expand All @@ -143,6 +146,8 @@ QVariant QgsStringStatisticalSummary::statistic( QgsStringStatisticalSummary::St
return mMinLength;
case MaximumLength:
return mMaxLength;
case MeanLength:
return mMeanLength;
case All:
return 0;
}
Expand All @@ -167,6 +172,8 @@ QString QgsStringStatisticalSummary::displayName( QgsStringStatisticalSummary::S
return QObject::tr( "Minimum length" );
case MaximumLength:
return QObject::tr( "Maximum length" );
case MeanLength:
return QObject::tr( "Mean length" );
case All:
return QString();
}
Expand Down
11 changes: 10 additions & 1 deletion src/core/qgsstringstatisticalsummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class CORE_EXPORT QgsStringStatisticalSummary
Max = 16, //!< Maximum string value
MinimumLength = 32, //!< Minimum length of string
MaximumLength = 64, //!< Maximum length of string
All = Count | CountDistinct | CountMissing | Min | Max, //! All statistics
MeanLength = 128, //!< Mean length of strings
All = Count | CountDistinct | CountMissing | Min | Max | MinimumLength | MaximumLength | MeanLength, //! All statistics
};
Q_DECLARE_FLAGS( Statistics, Statistic )

Expand Down Expand Up @@ -167,6 +168,12 @@ class CORE_EXPORT QgsStringStatisticalSummary
*/
int maxLength() const { return mMaxLength; }

/**
* Returns the mean length of strings.
* @note added in QGIS 3.0
*/
double meanLength() const { return mMeanLength; }

/** Returns the friendly display name for a statistic
* @param statistic statistic to return name for
*/
Expand All @@ -183,6 +190,8 @@ class CORE_EXPORT QgsStringStatisticalSummary
QString mMax;
int mMinLength;
int mMaxLength;
long mSumLengths;
double mMeanLength;

void testString( const QString& string );
};
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_qgsstringstatisticalsummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def testStats(self):
self.assertEqual(s2.minLength(), 0)
self.assertEqual(s.maxLength(), 8)
self.assertEqual(s2.maxLength(), 8)
self.assertEqual(s.meanLength(), 3.33333333333333333333333)
self.assertEqual(s2.meanLength(), 3.33333333333333333333333)

#extra check for minLength without empty strings
s.calculate(['1111111', '111', '11111'])
Expand All @@ -63,6 +65,7 @@ def testIndividualStats(self):
{'stat': QgsStringStatisticalSummary.Max, 'expected': 'eeee'},
{'stat': QgsStringStatisticalSummary.MinimumLength, 'expected': 0},
{'stat': QgsStringStatisticalSummary.MaximumLength, 'expected': 8},
{'stat': QgsStringStatisticalSummary.MeanLength, 'expected': 3.3333333333333335},
]

s = QgsStringStatisticalSummary()
Expand Down

0 comments on commit a927d97

Please sign in to comment.