Bug report #14090

Frequency Analysis tool contains errors in code

Added by Joseph Liam about 5 years ago. Updated about 5 years ago.

Assignee:Victor Olaya
Affected QGIS version:2.12.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:22098


In the latest versions of the Processing plugin (from 2.10 onwards), there are minor errors in the code. The following code should probably be used to replace the existing code:

##Fields=Field Input
##Frequency=output table

from processing.tools.vector import TableWriter
from collections import defaultdict
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

layer = processing.getObject(Input)    # Changed '(input)' to '(Input)'
inputFields = layer.pendingFields()
fieldIdxs = []
fields = Fields.split(',')    # Changed 'fields.split' to 'Fields.split'
for f in fields:
    idx = inputFields.indexFromName(f)
    if idx == -1:
        raise GeoAlgorithmExecutionException('Field not found:' + f)
writer = TableWriter(Frequency, None, fields + ['FREQ'])    # Changed 'output' to 'Frequency'

counts = {}
feats = processing.features(layer)
nFeats = len(feats)
counts = defaultdict(int)
for i, feat in enumerate(feats):
    progress.setPercentage(int(100 * i / nFeats))
    attrs = feat.attributes()
    clazz = tuple([attrs[i] for i in fieldIdxs])
    print clazz
    counts[clazz] += 1

for c in counts:
    writer.addRecord(list(c) + [counts[c]])

This was reported from the GIS:Stack Exchange forum (http://gis.stackexchange.com/questions/171002/proper-use-of-qgis-tool-frequency-analysis)

diff Magnifier (1.11 KB) Jürgen Fischer, 2016-01-12 05:32 AM

Associated revisions

Revision 7a879994
Added by Victor Olaya about 5 years ago

[processing] fixed frequency analysis algorithm

code provided by Joseph Liam

fixes #14090


#1 Updated by Jürgen Fischer about 5 years ago

#2 Updated by Victor Olaya about 5 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF