Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Delay setting image if an algorithm is still running #627

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions package/PartSeg/common_backend/segmentation_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions package/PartSeg/common_gui/algorithms_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions package/tests/test_PartSeg/test_common_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down