Bug report #14090

Frequency Analysis tool contains errors in code

Added by Joseph Liam almost 9 years ago. Updated almost 9 years ago.

Status:Closed
Priority:Normal
Assignee:Victor Olaya
Category:Processing/Core
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

Description

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:

##Table=group
##Input=vector
##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)
    fieldIdxs.append(idx)
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 almost 9 years ago

[processing] fixed frequency analysis algorithm

code provided by Joseph Liam

fixes #14090

History

#1 Updated by Jürgen Fischer almost 9 years ago

#2 Updated by Victor Olaya almost 9 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF