From a85c20e3349b7756b6ec83129a650462a9387bc6 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Wed, 11 May 2022 12:14:01 +0200 Subject: [PATCH 1/3] delay setting image if algorithm is still running --- .../common_backend/segmentation_thread.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/package/PartSeg/common_backend/segmentation_thread.py b/package/PartSeg/common_backend/segmentation_thread.py index ff36c84ca..4d8fc29eb 100644 --- a/package/PartSeg/common_backend/segmentation_thread.py +++ b/package/PartSeg/common_backend/segmentation_thread.py @@ -32,6 +32,8 @@ def __init__(self, algorithm: ROIExtractionAlgorithm): self.algorithm = algorithm self.clean_later = False self.cache = None + self._image = None + self._mask = None self.mutex = QMutex() self.rerun = False, QThread.InheritPriority @@ -70,6 +72,12 @@ def finished_task(self): self.algorithm.set_parameters(*args, **kwargs) self.cache = None self.clean_later = False + if self._image is not None: + self.algorithm.set_image(self._image) + self._image = None + if self._mask is not None: + self.algorithm.set_mask(self._mask) + self._mask = None if self.rerun[0]: self.rerun = False, QThread.InheritPriority super().start(self.rerun[1]) @@ -102,6 +110,32 @@ def set_parameters(self, *args, **kwargs): self.algorithm.set_parameters(*args, **kwargs) self.mutex.unlock() + def set_image(self, image): + """ + check if calculation is running. + If yes then cache parameters until it finish, otherwise call :py:meth:`.SegmentationAlgorithm.set_image` + :param image: image to be set + """ + self.mutex.lock() + if self.isRunning(): + self._image = image + else: + self.algorithm.set_image(image) + self.mutex.unlock() + + def set_mask(self, mask): + """ + check if calculation is running. + If yes then cache parameters until it finish, otherwise call :py:meth:`.SegmentationAlgorithm.set_mask` + :param mask: mask to be set + """ + self.mutex.lock() + if self.isRunning(): + self._mask = mask + else: + self.algorithm.set_mask(mask) + self.mutex.unlock() + def start(self, priority: "QThread.Priority" = QThread.InheritPriority): """ If calculation is running remember to restart it with new parameters. From 5a41aa3fadcb8fbf12fad73476aeeb46878b36a6 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Wed, 11 May 2022 12:31:40 +0200 Subject: [PATCH 2/3] add new function usage --- package/PartSeg/common_gui/algorithms_description.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/PartSeg/common_gui/algorithms_description.py b/package/PartSeg/common_gui/algorithms_description.py index de65c33fe..cc80d6935 100644 --- a/package/PartSeg/common_gui/algorithms_description.py +++ b/package/PartSeg/common_gui/algorithms_description.py @@ -619,10 +619,10 @@ def show_info(self, text): def image_changed(self, image: Image): self.form_widget.image_changed(image) - self.algorithm_thread.algorithm.set_image(image) + self.algorithm_thread.set_image(image) def set_mask(self, mask): - self.algorithm_thread.algorithm.set_mask(mask) + self.algorithm_thread.set_mask(mask) def mask(self) -> typing.Optional[np.ndarray]: return self.algorithm_thread.algorithm.mask From fb9eab3aaf85302224a03f0911b3362f12351956 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Wed, 11 May 2022 13:33:23 +0200 Subject: [PATCH 3/3] add test --- .../tests/test_PartSeg/test_common_backend.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/package/tests/test_PartSeg/test_common_backend.py b/package/tests/test_PartSeg/test_common_backend.py index 3aecf3bfd..62b71cf4c 100644 --- a/package/tests/test_PartSeg/test_common_backend.py +++ b/package/tests/test_PartSeg/test_common_backend.py @@ -6,6 +6,7 @@ from io import BytesIO from pathlib import Path from typing import Callable, Optional +from unittest.mock import MagicMock import numpy as np import pytest @@ -295,6 +296,26 @@ def test_running_start(self, qtbot, monkeypatch): thr.finished_task() assert start_list == [1, 1] + def test_setting_image(self, qtbot, monkeypatch, image): + algorithm_mock = MagicMock() + thread = segmentation_thread.SegmentationThread(algorithm_mock) + monkeypatch.setattr(thread, "isRunning", lambda: True) + assert thread._image is None + thread.set_image(image) + assert thread._image is image + algorithm_mock.set_image.assert_not_called() + + assert thread._mask is None + thread.set_mask(image.get_channel(0)) + assert thread._mask is not None + algorithm_mock.set_mask.assert_not_called() + + thread.finished_task() + assert thread._image is None + assert thread._mask is None + algorithm_mock.set_image.assert_called_once() + algorithm_mock.set_mask.assert_called_once() + class ROIExtractionAlgorithmForTest(ROIExtractionAlgorithm): def __init__(self, raise_=False, return_none=False):