Skip to content

Commit

Permalink
[processing] fix handling of NULLs is Basic statistics for text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 22, 2016
1 parent 847f1c0 commit d3852e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
26 changes: 14 additions & 12 deletions python/plugins/processing/algs/qgis/BasicStatisticsStrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def processAlgorithm(self, progress):
minValue = 0
maxValue = 0
meanValue = 0
countEmpty = 0
countFilled = 0
nullValues = 0
filledValues = 0

isFirst = True
values = []
Expand All @@ -94,7 +94,14 @@ def processAlgorithm(self, progress):
count = len(features)
total = 100.0 / count
for current, ft in enumerate(features):
length = float(len(ft.attributes()[index]))
value = ft[fieldName]
if value:
length = float(len(value))
filledValues += 1
else:
nullValues += 1
progress.setPercentage(int(current * total))
continue

if isFirst:
minValue = length
Expand All @@ -106,11 +113,6 @@ def processAlgorithm(self, progress):
if length > maxValue:
maxValue = length

if length != 0.00:
countFilled += 1
else:
countEmpty += 1

values.append(length)
sumValue += length

Expand All @@ -128,8 +130,8 @@ def processAlgorithm(self, progress):
data.append('Minimum length: ' + unicode(minValue))
data.append('Maximum length: ' + unicode(maxValue))
data.append('Mean length: ' + unicode(meanValue))
data.append('Filled: ' + unicode(countFilled))
data.append('Empty: ' + unicode(countEmpty))
data.append('Filled values: ' + unicode(filledValues))
data.append('NULL (missed) values: ' + unicode(nullValues))
data.append('Count: ' + unicode(count))
data.append('Unique: ' + unicode(uniqueValues))

Expand All @@ -138,8 +140,8 @@ def processAlgorithm(self, progress):
self.setOutputValue(self.MIN_LEN, minValue)
self.setOutputValue(self.MAX_LEN, maxValue)
self.setOutputValue(self.MEAN_LEN, meanValue)
self.setOutputValue(self.FILLED, countFilled)
self.setOutputValue(self.EMPTY, countEmpty)
self.setOutputValue(self.FILLED, filledValues)
self.setOutputValue(self.EMPTY, nullValues)
self.setOutputValue(self.COUNT, count)
self.setOutputValue(self.UNIQUE, uniqueValues)

Expand Down
12 changes: 12 additions & 0 deletions python/plugins/processing/tests/testdata/algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ tests:
OUTPUT:
name: expected/polys_to_lines.gml
type: vector

- algorithm: qgis:basicstatisticsfornumericfields
name: Test (qgis:basicstatisticsfornumericfields)
params:
Expand All @@ -73,3 +74,14 @@ tests:
OUTPUT_HTML_FILE:
name: expected/basic_statistics_numeric_float.html
type: file

- algorithm: qgis:basicstatisticsfortextfields
name: Test (qgis:basicstatisticsfortextfields)
params:
- name: multipolys.gml
type: vector
- 'Bname'
results:
OUTPUT_HTML_FILE:
name: expected/basic_statistics_string.html
type: file
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
<p>Analyzed layer: multipolys.gml</p>
<p>Analyzed field: Bname</p>
<p>Minimum length: 4.0</p>
<p>Maximum length: 4.0</p>
<p>Mean length: 4.0</p>
<p>Filled values: 3</p>
<p>NULL (missed) values: 1</p>
<p>Count: 4</p>
<p>Unique: 2</p>
</body></html>

2 comments on commit d3852e4

@m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented on d3852e4 Feb 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Alex.

Any thoughts from your side about the testing infrastructure?

@alexbruy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great. And in the same time easy to use and understand. Hope we will be able to cover with test most important parts of Processing soon.

Thanks for your hard work!

Please sign in to comment.