From 796dfb2b507ef1496558d1e0740bb3a62d310c7e Mon Sep 17 00:00:00 2001 From: Dominik Kutra Date: Fri, 27 Sep 2024 10:15:12 +0100 Subject: [PATCH] Fix flaky ObjectClassificationGui test (#2911) this test was flaky due to so many features in training - fixing the seed for the RF training should fix this. In the test class the following comparisons against of exported data to reference data would fail occasionally. Also removed some unused imports. --- tests/conftest.py | 24 ++++++++++++++++--- .../testObjectClassificationGui.py | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index caef90924..a0a023ce8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,8 @@ from pathlib import Path import os -import shutil import tempfile import threading -import time import queue -import sys import warnings import platform import itertools @@ -358,3 +355,24 @@ def cacheMemoryManager(monkeypatch): mem_manager = _CacheMemoryManager() monkeypatch.setattr(lazyflow.operators.cacheMemoryManager, "_cache_memory_manager", mem_manager) return mem_manager + + +@pytest.fixture() +def reliable_vigra_train_rf_seed(monkeypatch): + """ + Can be used in test functions that involve invoking + `vigra.learning.RandomForest.learnRF` along the way. This will set + the randomSeed parameter in that function to 42. + Relies on `learnRF` being called only with parameters trainData and trainLabels. + """ + import vigra + + _learn_orig = vigra.learning.RandomForest.learnRF + randomSeed = 42 + + # Note: can't use a partial there, the function doesn't like kwargs + monkeypatch.setattr( + vigra.learning.RandomForest, + "learnRF", + lambda self, trainData, trainLabels: _learn_orig(self, trainData, trainLabels, randomSeed), + ) diff --git a/tests/test_ilastik/test_workflows/testObjectClassificationGui.py b/tests/test_ilastik/test_workflows/testObjectClassificationGui.py index 93957dddd..27c840ea6 100644 --- a/tests/test_ilastik/test_workflows/testObjectClassificationGui.py +++ b/tests/test_ilastik/test_workflows/testObjectClassificationGui.py @@ -31,6 +31,7 @@ import h5py import numpy +import pytest from ilastik.workflows import ObjectClassificationWorkflowPrediction from ilastik.applets.dataSelection.opDataSelection import FilesystemDatasetInfo @@ -364,6 +365,7 @@ def impl(): # Run this test from within the shell event loop self.exec_in_shell(impl) + @pytest.mark.usefixtures("reliable_vigra_train_rf_seed") def test_05_live_update_mode(self): def impl(): shell = self.shell