Skip to content

Commit

Permalink
[processing] correctly handle zero in Basic statistics alg (fix #14331)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 18, 2016
1 parent a0da85f commit d8a5e0f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py
Expand Up @@ -57,6 +57,7 @@ class BasicStatisticsNumbers(GeoAlgorithm):
MAJORITY = 'MAJORITY'
FIRSTQUARTILE = 'FIRSTQUARTILE'
THIRDQUARTILE = 'THIRDQUARTILE'
NULLVALUES = 'NULLVALUES'
IQR = 'IQR'

def defineCharacteristics(self):
Expand Down Expand Up @@ -86,6 +87,7 @@ def defineCharacteristics(self):
self.addOutput(OutputNumber(self.MAJORITY, self.tr('Majority (most frequently occurring value)')))
self.addOutput(OutputNumber(self.FIRSTQUARTILE, self.tr('First quartile')))
self.addOutput(OutputNumber(self.THIRDQUARTILE, self.tr('Third quartile')))
self.addOutput(OutputNumber(self.NULLVALUES, self.tr('NULL (missed) values')))
self.addOutput(OutputNumber(self.IQR, self.tr('Interquartile Range (IQR)')))

def processAlgorithm(self, progress):
Expand All @@ -95,8 +97,6 @@ def processAlgorithm(self, progress):

outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)

index = layer.fieldNameIndex(fieldName)

cvValue = 0
minValue = 0
maxValue = 0
Expand All @@ -108,6 +108,7 @@ def processAlgorithm(self, progress):
majority = 0
firstQuartile = 0
thirdQuartile = 0
nullValues = 0
iqr = 0

isFirst = True
Expand All @@ -117,8 +118,11 @@ def processAlgorithm(self, progress):
count = len(features)
total = 100.0 / float(count)
for current, ft in enumerate(features):
if ft.attributes()[index]:
values.append(float(ft.attributes()[index]))
value = ft[fieldName]
if value or value == 0:
values.append(float(value))
else:
nullValues += 1

progress.setPercentage(int(current * total))

Expand Down Expand Up @@ -159,6 +163,7 @@ def processAlgorithm(self, progress):
data.append('Majority (most frequently occurring value): ' + unicode(majority))
data.append('First quartile: ' + unicode(firstQuartile))
data.append('Third quartile: ' + unicode(thirdQuartile))
data.append('NULL (missed) values: ' + unicode(nullValues))
data.append('Interquartile Range (IQR): ' + unicode(iqr))

self.createHTML(outputFile, data)
Expand All @@ -176,6 +181,7 @@ def processAlgorithm(self, progress):
self.setOutputValue(self.MAJORITY, majority)
self.setOutputValue(self.FIRSTQUARTILE, firstQuartile)
self.setOutputValue(self.THIRDQUARTILE, thirdQuartile)
self.setOutputValue(self.NULLVALUES, nullValues)
self.setOutputValue(self.IQR, iqr)

def createHTML(self, outputFile, algData):
Expand Down

0 comments on commit d8a5e0f

Please sign in to comment.