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.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.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector 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) outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)


cvValue = 0 request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([fieldName], layer.fields())
minValue = 0 stat = QgsStatisticalSummary()
maxValue = 0 features = vector.features(layer, request)
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)
count = len(features) count = len(features)
total = 100.0 / float(count) total = 100.0 / float(count)
for current, ft in enumerate(features): for current, ft in enumerate(features):
value = ft[fieldName] stat.addVariant(ft[fieldName])
if value or value == 0:
values.append(float(value))
else:
nullValues += 1

progress.setPercentage(int(current * total)) progress.setPercentage(int(current * total))


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


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


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

0 comments on commit e272bb3

Please sign in to comment.