27
27
28
28
import math
29
29
30
+ from qgis .core import QgsStatisticalSummary
30
31
from processing .core .GeoAlgorithm import GeoAlgorithm
31
32
from processing .core .parameters import ParameterVector
32
33
from processing .core .parameters import ParameterTableField
@@ -51,6 +52,11 @@ class BasicStatisticsNumbers(GeoAlgorithm):
51
52
RANGE = 'RANGE'
52
53
MEDIAN = 'MEDIAN'
53
54
UNIQUE = 'UNIQUE'
55
+ MINORITY = 'MINORITY'
56
+ MAJORITY = 'MAJORITY'
57
+ FIRSTQUARTILE = 'FIRSTQUARTILE'
58
+ THIRDQUARTILE = 'THIRDQUARTILE'
59
+ IQR = 'IQR'
54
60
55
61
def defineCharacteristics (self ):
56
62
self .name , self .i18n_name = self .trAlgorithm ('Basic statistics for numeric fields' )
@@ -70,11 +76,16 @@ def defineCharacteristics(self):
70
76
self .addOutput (OutputNumber (self .MAX , self .tr ('Maximum value' )))
71
77
self .addOutput (OutputNumber (self .SUM , self .tr ('Sum' )))
72
78
self .addOutput (OutputNumber (self .MEAN , self .tr ('Mean value' )))
79
+ self .addOutput (OutputNumber (self .STD_DEV , self .tr ('Standard deviation' )))
73
80
self .addOutput (OutputNumber (self .COUNT , self .tr ('Count' )))
74
81
self .addOutput (OutputNumber (self .RANGE , self .tr ('Range' )))
75
82
self .addOutput (OutputNumber (self .MEDIAN , self .tr ('Median' )))
76
83
self .addOutput (OutputNumber (self .UNIQUE , self .tr ('Number of unique values' )))
77
- self .addOutput (OutputNumber (self .STD_DEV , self .tr ('Standard deviation' )))
84
+ self .addOutput (OutputNumber (self .MINORITY , self .tr ('Minority (rarest occurring value)' )))
85
+ self .addOutput (OutputNumber (self .MAJORITY , self .tr ('Majority (most frequently occurring value)' )))
86
+ self .addOutput (OutputNumber (self .FIRSTQUARTILE , self .tr ('First quartile' )))
87
+ self .addOutput (OutputNumber (self .THIRDQUARTILE , self .tr ('Third quartile' )))
88
+ self .addOutput (OutputNumber (self .IQR , self .tr ('Interquartile Range (IQR)' )))
78
89
79
90
def processAlgorithm (self , progress ):
80
91
layer = dataobjects .getObjectFromUri (
@@ -92,6 +103,11 @@ def processAlgorithm(self, progress):
92
103
meanValue = 0
93
104
medianValue = 0
94
105
stdDevValue = 0
106
+ minority = 0
107
+ majority = 0
108
+ firstQuartile = 0
109
+ thirdQuartile = 0
110
+ iqr = 0
95
111
96
112
isFirst = True
97
113
values = []
@@ -102,43 +118,30 @@ def processAlgorithm(self, progress):
102
118
current = 0
103
119
for ft in features :
104
120
if ft .attributes ()[index ]:
105
- value = float (ft .attributes ()[index ])
106
- if isFirst :
107
- minValue = value
108
- maxValue = value
109
- isFirst = False
110
- else :
111
- if value < minValue :
112
- minValue = value
113
- if value > maxValue :
114
- maxValue = value
115
-
116
- values .append (value )
117
- sumValue += value
121
+ values .append (float (ft .attributes ()[index ]))
118
122
119
123
current += 1
120
124
progress .setPercentage (int (current * total ))
121
125
122
- # Calculate additional values
123
- rValue = maxValue - minValue
124
- uniqueValue = vector .getUniqueValuesCount (layer , index )
125
-
126
- if count > 0 :
127
- meanValue = sumValue / count
128
- if meanValue != 0.00 :
129
- for v in values :
130
- stdDevValue += (v - meanValue ) * (v - meanValue )
131
- stdDevValue = math .sqrt (stdDevValue / count )
132
- cvValue = stdDevValue / meanValue
133
-
134
- if count > 1 :
135
- tmp = sorted (values )
136
-
137
- # Calculate median
138
- if count % 2 == 0 :
139
- medianValue = 0.5 * (tmp [(count - 1 ) / 2 ] + tmp [count / 2 ])
140
- else :
141
- medianValue = tmp [(count + 1 ) / 2 - 1 ]
126
+ stat = QgsStatisticalSummary ()
127
+ stat .calculate (values )
128
+
129
+ count = stat .count ()
130
+ uniqueValue = stat .variety ()
131
+ minValue = stat .min ()
132
+ maxValue = stat .max ()
133
+ rValue = stat .range ()
134
+ sumValue = stat .sum ()
135
+ meanValue = stat .mean ()
136
+ medianValue = stat .median ()
137
+ stdDevValue = stat .stDev ()
138
+ if meanValue != 0.00 :
139
+ cvValue = stdDevValue / meanValue
140
+ minority = stat .minority ()
141
+ majority = stat .majority ()
142
+ firstQuartile = stat .firstQuartile ()
143
+ thirdQuartile = stat .thirdQuartile ()
144
+ iqr = stat .interQuartileRange ()
142
145
143
146
data = []
144
147
data .append ('Count: ' + unicode (count ))
@@ -151,6 +154,11 @@ def processAlgorithm(self, progress):
151
154
data .append ('Median value: ' + unicode (medianValue ))
152
155
data .append ('Standard deviation: ' + unicode (stdDevValue ))
153
156
data .append ('Coefficient of Variation: ' + unicode (cvValue ))
157
+ data .append ('Minority (rarest occurring value): ' + unicode (minority ))
158
+ data .append ('Majority (most frequently occurring value): ' + unicode (majority ))
159
+ data .append ('First quartile: ' + unicode (firstQuartile ))
160
+ data .append ('Third quartile: ' + unicode (thirdQuartile ))
161
+ data .append ('Interquartile Range (IQR): ' + unicode (iqr ))
154
162
155
163
self .createHTML (outputFile , data )
156
164
@@ -163,7 +171,11 @@ def processAlgorithm(self, progress):
163
171
self .setOutputValue (self .MEAN , meanValue )
164
172
self .setOutputValue (self .MEDIAN , medianValue )
165
173
self .setOutputValue (self .STD_DEV , stdDevValue )
166
- self .setOutputValue (self .CV , cvValue )
174
+ self .setOutputValue (self .MINORITY , minority )
175
+ self .setOutputValue (self .MAJORITY , majority )
176
+ self .setOutputValue (self .FIRSTQUARTILE , firstQuartile )
177
+ self .setOutputValue (self .THIRDQUARTILE , thirdQuartile )
178
+ self .setOutputValue (self .IQR , iqr )
167
179
168
180
def createHTML (self , outputFile , algData ):
169
181
f = open (outputFile , 'w' )
0 commit comments