Skip to content

Commit

Permalink
[processing] Optimise basic stats numbers algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent a927d97 commit e272bb3
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
Expand Up @@ -31,7 +31,9 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsStatisticalSummary
from qgis.core import (QgsStatisticalSummary,
QgsFeatureRequest,
NULL)

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
Expand Down Expand Up @@ -107,36 +109,16 @@ def processAlgorithm(self, progress):

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

cvValue = 0
minValue = 0
maxValue = 0
sumValue = 0
meanValue = 0
medianValue = 0
stdDevValue = 0
minority = 0
majority = 0
firstQuartile = 0
thirdQuartile = 0
nullValues = 0
iqr = 0

values = []

features = vector.features(layer)
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([fieldName], layer.fields())
stat = QgsStatisticalSummary()
features = vector.features(layer, request)
count = len(features)
total = 100.0 / float(count)
for current, ft in enumerate(features):
value = ft[fieldName]
if value or value == 0:
values.append(float(value))
else:
nullValues += 1

stat.addVariant(ft[fieldName])
progress.setPercentage(int(current * total))

stat = QgsStatisticalSummary()
stat.calculate(values)
stat.finalize()

count = stat.count()
uniqueValue = stat.variety()
Expand All @@ -147,13 +129,13 @@ def processAlgorithm(self, progress):
meanValue = stat.mean()
medianValue = stat.median()
stdDevValue = stat.stDev()
if meanValue != 0.00:
cvValue = stdDevValue / meanValue
cvValue = stdDevValue / meanValue if meanValue != 0 else 0
minority = stat.minority()
majority = stat.majority()
firstQuartile = stat.firstQuartile()
thirdQuartile = stat.thirdQuartile()
iqr = stat.interQuartileRange()
nullValues = stat.countMissing()

data = []
data.append(self.tr('Analyzed layer: {}').format(layer.name()))
Expand Down

0 comments on commit e272bb3

Please sign in to comment.