Skip to content

Commit

Permalink
Merge pull request #7363 from alexbruy/fix-sampling
Browse files Browse the repository at this point in the history
[processing] fix Random extract/select within subset algorithms
  • Loading branch information
alexbruy committed Jul 4, 2018
2 parents 6c014b4 + b72f5d1 commit 6d751dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Expand Up @@ -125,8 +125,11 @@ def processAlgorithm(self, parameters, context, feedback):
classes[attrs[index]].append(feature)
feedback.setProgress(int(i * total))

for subset in classes.values():
for k, subset in classes.items():
selValue = value if method != 1 else int(round(value * len(subset), 0))
if selValue > len(subset):
selValue = len(subset)
feedback.reportError(self.tr('Subset "{}" is smaller than requested number of features.'.format(k)))
selran.extend(random.sample(subset, selValue))

total = 100.0 / featureCount if featureCount else 1
Expand Down
Expand Up @@ -133,11 +133,14 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(int(i * total))

selran = []
for subset in classes.values():
for k, subset in classes.items():
if feedback.isCanceled():
break

selValue = value if method != 1 else int(round(value * len(subset), 0))
if selValue > len(subset):
selValue = len(subset)
feedback.reportError(self.tr('Subset "{}" is smaller than requested number of features.'.format(k)))
selran.extend(random.sample(subset, selValue))

layer.selectByIds(selran)
Expand Down
16 changes: 16 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -3923,6 +3923,21 @@ tests:
name: points_weighted.gml
compare: false

- algorithm: qgis:randomextractwithinsubsets
name: Random extract within subset (subset smaller than number)
params:
FIELD: id2
INPUT:
name: points.gml
type: vector
METHOD: 0
NUMBER: 3
results:
OUTPUT:
type: vector
name: points.gml
compare: false

- algorithm: qgis:heatmapkerneldensityestimation
name: Heatmap (Kernel density estimation)
params:
Expand Down Expand Up @@ -5355,6 +5370,7 @@ tests:
compare:
fields:
fid: skip

- algorithm: native:difference
name: Test Difference B - A (basic)
params:
Expand Down

0 comments on commit 6d751dc

Please sign in to comment.