Skip to content

Commit e272bb3

Browse files
committedNov 30, 2016
[processing] Optimise basic stats numbers algorithm
1 parent a927d97 commit e272bb3

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed
 

‎python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
from qgis.PyQt.QtGui import QIcon
3333

34-
from qgis.core import QgsStatisticalSummary
34+
from qgis.core import (QgsStatisticalSummary,
35+
QgsFeatureRequest,
36+
NULL)
3537

3638
from processing.core.GeoAlgorithm import GeoAlgorithm
3739
from processing.core.parameters import ParameterVector
@@ -107,36 +109,16 @@ def processAlgorithm(self, progress):
107109

108110
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
109111

110-
cvValue = 0
111-
minValue = 0
112-
maxValue = 0
113-
sumValue = 0
114-
meanValue = 0
115-
medianValue = 0
116-
stdDevValue = 0
117-
minority = 0
118-
majority = 0
119-
firstQuartile = 0
120-
thirdQuartile = 0
121-
nullValues = 0
122-
iqr = 0
123-
124-
values = []
125-
126-
features = vector.features(layer)
112+
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([fieldName], layer.fields())
113+
stat = QgsStatisticalSummary()
114+
features = vector.features(layer, request)
127115
count = len(features)
128116
total = 100.0 / float(count)
129117
for current, ft in enumerate(features):
130-
value = ft[fieldName]
131-
if value or value == 0:
132-
values.append(float(value))
133-
else:
134-
nullValues += 1
135-
118+
stat.addVariant(ft[fieldName])
136119
progress.setPercentage(int(current * total))
137120

138-
stat = QgsStatisticalSummary()
139-
stat.calculate(values)
121+
stat.finalize()
140122

141123
count = stat.count()
142124
uniqueValue = stat.variety()
@@ -147,13 +129,13 @@ def processAlgorithm(self, progress):
147129
meanValue = stat.mean()
148130
medianValue = stat.median()
149131
stdDevValue = stat.stDev()
150-
if meanValue != 0.00:
151-
cvValue = stdDevValue / meanValue
132+
cvValue = stdDevValue / meanValue if meanValue != 0 else 0
152133
minority = stat.minority()
153134
majority = stat.majority()
154135
firstQuartile = stat.firstQuartile()
155136
thirdQuartile = stat.thirdQuartile()
156137
iqr = stat.interQuartileRange()
138+
nullValues = stat.countMissing()
157139

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

0 commit comments

Comments
 (0)
Please sign in to comment.