Skip to content
Permalink
Browse files

Allow QgsStringStatisticalSummary to calculate mean string length

  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent 57f17e3 commit a927d9743f2b9adee284dee0704fee8572311ed7
@@ -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;
@@ -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
*/
@@ -42,6 +42,8 @@ void QgsStringStatisticalSummary::reset()
mMax.clear();
mMinLength = INT_MAX;
mMaxLength = 0;
mSumLengths = 0;
mMeanLength = 0;
}

void QgsStringStatisticalSummary::calculate( const QStringList& values )
@@ -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 )
@@ -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() );
}
@@ -143,6 +146,8 @@ QVariant QgsStringStatisticalSummary::statistic( QgsStringStatisticalSummary::St
return mMinLength;
case MaximumLength:
return mMaxLength;
case MeanLength:
return mMeanLength;
case All:
return 0;
}
@@ -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();
}
@@ -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 )

@@ -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
*/
@@ -183,6 +190,8 @@ class CORE_EXPORT QgsStringStatisticalSummary
QString mMax;
int mMinLength;
int mMaxLength;
long mSumLengths;
double mMeanLength;

void testString( const QString& string );
};
@@ -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'])
@@ -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()

0 comments on commit a927d97

Please sign in to comment.
You can’t perform that action at this time.