Skip to content

Commit

Permalink
Added stdev
Browse files Browse the repository at this point in the history
  • Loading branch information
huhabla committed Jan 16, 2015
1 parent 98a7242 commit fe37600
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
42 changes: 40 additions & 2 deletions python/plugins/fTools/tools/doPointsInPolygon.py
Expand Up @@ -31,6 +31,7 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ftools_utils
import math
from qgis.core import *
from ui_frmPointsInPolygon import Ui_Dialog

Expand Down Expand Up @@ -207,8 +208,8 @@ def run(self):
columnName = unicode(item.text() + "_" + self.statistics)
index = polyProvider.fieldNameIndex(unicode(columnName))
if index == -1:
if item.type() == typeDouble or self.statistics == "mean":
fieldList.append( QgsField(columnName, QVariant.Double, "double", 10, 2, "Value") )
if item.type() == typeDouble or self.statistics == "mean" or self.statistics == "stddev":
fieldList.append( QgsField(columnName, QVariant.Double, "double", 24, 15, "Value") )
else:
fieldList.append( QgsField(columnName, QVariant.Int, "int", 10, 0, "Value") )

Expand Down Expand Up @@ -268,6 +269,16 @@ def run(self):
# Compute the statistical values for selected vector attributes
for item in selectedItems:
values = valueList[item.text()]
# Check if the input contains non-numeric values
non_numeric_values = False
for value in values:
if (isinstance(value, type(float())) != True) and (isinstance(value, type(int())) != True):
non_numeric_values = True
break
# Jump over invalid values
if non_numeric_values is True:
continue

if values and len(values) > 0:
if self.statistics == "sum":
value = reduce(myAdder, values)
Expand All @@ -279,6 +290,9 @@ def run(self):
elif self.statistics == "max":
values.sort()
value = values[-1]
elif self.statistics == "stddev":
value = two_pass_variance(values)
value = math.sqrt(value)
atMap.append(value)

outFeat.setAttributes(atMap)
Expand Down Expand Up @@ -309,3 +323,27 @@ def stop(self):

def myAdder(x,y):
return x+y

def two_pass_variance(data):
"""
Variance algorithm taken from Wikipedia:
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
"""
n = 0.0
sum1 = 0.0
sum2 = 0.0

for x in data:
n = n + 1.0
sum1 = sum1 + float(x)

if (n < 2):
return 0

mean = sum1 / n

for x in data:
sum2 = sum2 + (x - mean)*(x - mean)

variance = sum2 / (n - 1)
return variance
1 change: 0 additions & 1 deletion python/plugins/fTools/tools/doVectorGrid.py
Expand Up @@ -264,7 +264,6 @@ def compute( self, bound, xOffset, yOffset, polygon ):
pt4 = QgsPoint(x, y - yOffset)
pt5 = QgsPoint(x, y)

print self.angle.value()
if self.angle.value() != 0.0:
self.rotatePoint(pt1)
self.rotatePoint(pt2)
Expand Down
11 changes: 4 additions & 7 deletions python/plugins/fTools/tools/frmVectorGrid.ui
Expand Up @@ -205,7 +205,7 @@
<number>10</number>
</property>
<property name="minimum">
<double>0.000001000000000</double>
<double>0.000100000000000</double>
</property>
<property name="maximum">
<double>1000000000.000000000000000</double>
Expand Down Expand Up @@ -270,7 +270,7 @@
<number>10</number>
</property>
<property name="minimum">
<double>0.000001000000000</double>
<double>0.000100000000000</double>
</property>
<property name="maximum">
<double>1000000000.000000000000000</double>
Expand All @@ -286,7 +286,7 @@
<string>Output grid as polygons</string>
</property>
<property name="checked">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
Expand All @@ -296,7 +296,7 @@
<string>Output grid as lines</string>
</property>
<property name="checked">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
Expand Down Expand Up @@ -369,9 +369,6 @@
<property name="text">
<string>Add result to canvas</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="0">
Expand Down

0 comments on commit fe37600

Please sign in to comment.