Skip to content
Permalink
Browse files
[sextante] changed statisticByCategories, so now it does not depend o…
…n external libs
  • Loading branch information
volaya committed Feb 24, 2013
1 parent cd0253a commit ffa56d0184e11631f1910b453e88639a474d42ee
@@ -31,7 +31,6 @@
from PyQt4.QtGui import *
from sextante.parameters.ParameterString import ParameterString
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException


class FieldsCalculator(GeoAlgorithm):
@@ -16,9 +16,9 @@
* *
***************************************************************************
"""
from sextante.algs.MeanAndStdDevPlot import MeanAndStdDevPlot
from sextante.algs.BarPlot import BarPlot
from sextante.algs.PolarPlot import PolarPlot
#from sextante.algs.MeanAndStdDevPlot import MeanAndStdDevPlot
#from sextante.algs.BarPlot import BarPlot
#from sextante.algs.PolarPlot import PolarPlot
from sextante.algs.RasterLayerStatistics import RasterLayerStatistics

__author__ = 'Victor Olaya'
@@ -30,7 +30,7 @@
import os
from PyQt4 import QtGui
#from sextante.algs.RasterLayerHistogram import RasterLayerHistogram
#from sextante.algs.StatisticsByCategories import StatisticsByCategories
from sextante.algs.StatisticsByCategories import StatisticsByCategories
from sextante.algs.ftools.PointsInPolygon import PointsInPolygon
from sextante.algs.ftools.PointsInPolygonUnique import PointsInPolygonUnique
from sextante.algs.ftools.PointsInPolygonWeighted import PointsInPolygonWeighted
@@ -97,8 +97,7 @@ def __init__(self):
SumLines(), PointsInPolygon(), PointsInPolygonWeighted(), PointsInPolygonUnique(),
BasicStatisticsStrings(), BasicStatisticsNumbers(), NearestNeighbourAnalysis(),
MeanCoords(), LinesIntersection(), UniqueValues(), PointDistance(), PointsLayerFromTable(),
#StatisticsByCategories(),
ReprojectLayer(),
StatisticsByCategories(),ReprojectLayer(),
ExportGeometryInfo(), Centroids(), Delaunay(), VoronoiPolygons(),
SimplifyGeometries(), DensifyGeometries(), DensifyGeometriesInterval(),
MultipartToSingleparts(), SinglePartsToMultiparts(), PolygonsToLines(),
@@ -16,17 +16,16 @@
* *
***************************************************************************
"""
import math

__author__ = 'Victor Olaya'
__date__ = 'September 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import math
from PyQt4.QtCore import *
from qgis.core import *
from scipy import stats
from sextante.outputs.OutputTable import OutputTable
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
@@ -50,7 +49,7 @@ def defineCharacteristics(self):
self.addParameter(ParameterTableField(self.CATEGORIES_FIELD_NAME, "Field with categories",
self.INPUT_LAYER, ParameterTableField.DATA_TYPE_ANY))

self.addOutput(OutputTable(self.OUTPUT, "Statistics for numeric field"))
self.addOutput(OutputTable(self.OUTPUT, "Statistics"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
@@ -62,21 +61,55 @@ def processAlgorithm(self, progress):
categoriesField = layer.fieldNameIndex(categoriesFieldName)

features = QGisLayers.features(layer)
nFeat = len(features)
nFeats = len(features)
values = {}
nFeat = 0
for feat in features:
nFeat += 1
progress.setPercentage(int((100 * nFeats) / nFeat))
attrs = feat.attributes()
value = float(attrs[valuesField].toDouble()[0])
cat = unicode(attrs[categoriesField].toString())
if cat not in values:
values[cat] = []
values[cat].append(value)
try:
value = float(attrs[valuesField].toDouble()[0])
cat = unicode(attrs[categoriesField].toString())
if cat not in values:
values[cat] = []
values[cat].append(value)
except:
pass

fields = [QgsField("category", QVariant.String), QgsField("mean", QVariant.Double), QgsField("variance", QVariant.Double)]
fields = [QgsField("category", QVariant.String), QgsField("min", QVariant.Double),
QgsField("max", QVariant.Double), QgsField("mean", QVariant.Double),
QgsField("stddev", QVariant.Double)]
writer = output.getTableWriter(fields)
for cat, value in values.items():
n, min_max, mean, var, skew, kurt = stats.describe(value)
record = [cat, mean, math.sqrt(var)]
for cat, v in values.items():
min, max, mean, stddev = calculateStats(v)
record = [cat, min, max, mean, stddev]
writer.addRecord(record)


def calculateStats(values):
n = 0
sum = 0
mean = 0
M2 = 0
minvalue = None
maxvalue = None

for v in values:
sum += v
n = n + 1
delta = v - mean
mean = mean + delta/n
M2 = M2 + delta*(v - mean)
if minvalue is None:
minvalue = v
maxvalue = v
else:
minvalue = min(v, minvalue)
maxvalue = max(v, maxvalue)

if n > 1:
variance = M2/(n - 1)
else:
variance = 0;
stddev = math.sqrt(variance)
return minvalue,maxvalue, mean, stddev

0 comments on commit ffa56d0

Please sign in to comment.