QgsStatisticalSummary class

Calculator for summary statistics for a list of doubles.

Statistics are calculated by calling calculate() and passing a list of doubles. The individual statistics can then be retrieved using the associated methods. Note that not all statistics are calculated by default. Statistics which require slower computations are only calculated by specifying the statistic in the constructor or via setStatistics().

Public types

enum Statistic { Count = 1 << 0, CountMissing = 1 << 15, Sum = 1 << 1, Mean = 1 << 2, Median = 1 << 3, StDev = 1 << 4, StDevSample = 1 << 5, Min = 1 << 6, Max = 1 << 7, Range = 1 << 8, Minority = 1 << 9, Majority = 1 << 10, Variety = 1 << 11, FirstQuartile = 1 << 12, ThirdQuartile = 1 << 13, InterQuartileRange = 1 << 14, First = 1 << 16, Last = 1 << 17, All = Count | CountMissing | Sum | Mean | Median | StDev | Max | Min | Range | Minority | Majority | Variety | FirstQuartile | ThirdQuartile | InterQuartileRange | First | Last }
Enumeration of flags that specify statistics to be calculated.

Public static functions

static auto displayName(QgsStatisticalSummary::Statistic statistic) -> QString
Returns the friendly display name for a statistic.
static auto shortName(QgsStatisticalSummary::Statistic statistic) -> QString
Returns a short, friendly display name for a statistic, suitable for use in a field name.

Constructors, destructors, conversion operators

QgsStatisticalSummary(QgsStatisticalSummary::Statistics stats = QgsStatisticalSummary::All)
Constructor for QgsStatisticalSummary.

Public functions

void addValue(double value)
Adds a single value to the statistics calculation.
void addVariant(const QVariant& value)
Adds a single value to the statistics calculation.
void calculate(const QList<double>& values)
Calculates summary statistics for a list of values.
auto count() const -> int
Returns calculated count of values.
auto countMissing() const -> int
Returns the number of missing (null) values.
void finalize()
Must be called after adding all values with addValues() and before retrieving any calculated statistics.
auto first() const -> double
Returns the first value obtained.
auto firstQuartile() const -> double
Returns the first quartile of the values.
auto interQuartileRange() const -> double
Returns the inter quartile range of the values.
auto last() const -> double
Returns the last value obtained.
auto majority() const -> double
Returns majority of values.
auto max() const -> double
Returns calculated maximum from values.
auto mean() const -> double
Returns calculated mean of values.
auto median() const -> double
Returns calculated median of values.
auto min() const -> double
Returns calculated minimum from values.
auto minority() const -> double
Returns minority of values.
auto range() const -> double
Returns calculated range (difference between maximum and minimum values).
void reset()
Resets the calculated values.
auto sampleStDev() const -> double
Returns sample standard deviation.
void setStatistics(QgsStatisticalSummary::Statistics stats)
Sets flags which specify which statistics will be calculated.
auto statistic(QgsStatisticalSummary::Statistic stat) const -> double
Returns the value of a specified statistic.
auto statistics() const -> Statistics
Returns flags which specify which statistics will be calculated.
auto stDev() const -> double
Returns population standard deviation.
auto sum() const -> double
Returns calculated sum of values.
auto thirdQuartile() const -> double
Returns the third quartile of the values.
auto variety() const -> int
Returns variety of values.

Enum documentation

enum QgsStatisticalSummary::Statistic

Enumeration of flags that specify statistics to be calculated.

Enumerators
Count

Count.

CountMissing

Number of missing (null) values.

Sum

Sum of values.

Mean

Mean of values.

Median

Median of values.

StDev

Standard deviation of values.

StDevSample

Sample standard deviation of values.

Min

Min of values.

Max

Max of values.

Range

Range of values (max - min)

Minority

Minority of values.

Majority

Majority of values.

Variety

Variety (count of distinct) values.

FirstQuartile

First quartile.

ThirdQuartile

Third quartile.

InterQuartileRange

Inter quartile range (IQR)

First

First value (since QGIS 3.6)

Last

Last value (since QGIS 3.6)

All

Function documentation

static QString QgsStatisticalSummary::displayName(QgsStatisticalSummary::Statistic statistic)

Returns the friendly display name for a statistic.

static QString QgsStatisticalSummary::shortName(QgsStatisticalSummary::Statistic statistic)

Returns a short, friendly display name for a statistic, suitable for use in a field name.

QgsStatisticalSummary::QgsStatisticalSummary(QgsStatisticalSummary::Statistics stats = QgsStatisticalSummary::All)

Constructor for QgsStatisticalSummary.

Parameters
stats flags for statistics to calculate

void QgsStatisticalSummary::addValue(double value)

Adds a single value to the statistics calculation.

Parameters
value value to add

Calling this method allows values to be added to the calculation one at a time. For large quantities of values this may be more efficient then first adding all the values to a list and calling calculate().

void QgsStatisticalSummary::addVariant(const QVariant& value)

Adds a single value to the statistics calculation.

Parameters
value variant containing to add. Non-numeric values are treated as null.

Calling this method allows values to be added to the calculation one at a time. For large quantities of values this may be more efficient then first adding all the values to a list and calling calculate().

void QgsStatisticalSummary::calculate(const QList<double>& values)

Calculates summary statistics for a list of values.

Parameters
values list of doubles

int QgsStatisticalSummary::countMissing() const

Returns the number of missing (null) values.

void QgsStatisticalSummary::finalize()

Must be called after adding all values with addValues() and before retrieving any calculated statistics.

double QgsStatisticalSummary::first() const

Returns the first value obtained.

A NaN value may be returned if no values were encountered.

double QgsStatisticalSummary::firstQuartile() const

Returns the first quartile of the values.

The quartile is calculated using the "Tukey's hinges" method. A NaN value may be returned if the first quartile cannot be calculated.

double QgsStatisticalSummary::interQuartileRange() const

Returns the inter quartile range of the values.

The quartiles are calculated using the "Tukey's hinges" method. A NaN value may be returned if the IQR cannot be calculated.

double QgsStatisticalSummary::last() const

Returns the last value obtained.

A NaN value may be returned if no values were encountered.

double QgsStatisticalSummary::majority() const

Returns majority of values.

The majority is the value with most occurrences in the list This is only calculated if Statistic::Majority has been specified in the constructor or via setStatistics. A NaN value may be returned if the majority cannot be calculated.

double QgsStatisticalSummary::max() const

Returns calculated maximum from values.

A NaN value may be returned if the maximum cannot be calculated.

double QgsStatisticalSummary::mean() const

Returns calculated mean of values.

A NaN value may be returned if the mean cannot be calculated.

double QgsStatisticalSummary::median() const

Returns calculated median of values.

This is only calculated if Statistic::Median has been specified in the constructor or via setStatistics. A NaN value may be returned if the median cannot be calculated.

double QgsStatisticalSummary::min() const

Returns calculated minimum from values.

A NaN value may be returned if the minimum cannot be calculated.

double QgsStatisticalSummary::minority() const

Returns minority of values.

The minority is the value with least occurrences in the list This is only calculated if Statistic::Minority has been specified in the constructor or via setStatistics. A NaN value may be returned if the minority cannot be calculated.

double QgsStatisticalSummary::range() const

Returns calculated range (difference between maximum and minimum values).

A NaN value may be returned if the range cannot be calculated.

double QgsStatisticalSummary::sampleStDev() const

Returns sample standard deviation.

This is only calculated if Statistic::StDev has been specified in the constructor or via setStatistics. A NaN value may be returned if the standard deviation cannot be calculated.

void QgsStatisticalSummary::setStatistics(QgsStatisticalSummary::Statistics stats)

Sets flags which specify which statistics will be calculated.

Parameters
stats flags for statistics to calculate

Some statistics are always calculated (e.g., sum, min and max).

double QgsStatisticalSummary::statistic(QgsStatisticalSummary::Statistic stat) const

Returns the value of a specified statistic.

Parameters
stat statistic to return
Returns calculated value of statistic. A NaN value may be returned for invalid statistics.

Statistics QgsStatisticalSummary::statistics() const

Returns flags which specify which statistics will be calculated.

Some statistics are always calculated (e.g., sum, min and max).

double QgsStatisticalSummary::stDev() const

Returns population standard deviation.

This is only calculated if Statistic::StDev has been specified in the constructor or via setStatistics. A NaN value may be returned if the standard deviation cannot be calculated.

double QgsStatisticalSummary::thirdQuartile() const

Returns the third quartile of the values.

The quartile is calculated using the "Tukey's hinges" method. A NaN value may be returned if the third quartile cannot be calculated.

int QgsStatisticalSummary::variety() const

Returns variety of values.

The variety is the count of unique values from the list. This is only calculated if Statistic::Variety has been specified in the constructor or via setStatistics.