Skip to content
Permalink
Browse files

[processing] Optimise basic stats numbers algorithm

  • Loading branch information
nyalldawson committed Nov 30, 2016
1 parent a927d97 commit e272bb3e9c9b80e3a6bd43e36505e9d851f21da5
Showing with 10 additions and 28 deletions.
  1. +10 −28 python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
@@ -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
@@ -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()
@@ -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()))

0 comments on commit e272bb3

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