From 38b85e493d3fba8c5c369c5f99797cdbfd8fbfc6 Mon Sep 17 00:00:00 2001 From: Mike Sullivan Date: Tue, 10 Dec 2024 14:57:40 +0000 Subject: [PATCH] Mean is calculated in separate thread, spectrum updated in Main thread --- .../gui/windows/live_viewer/model.py | 6 ---- .../gui/windows/live_viewer/presenter.py | 33 ++++++++++++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mantidimaging/gui/windows/live_viewer/model.py b/mantidimaging/gui/windows/live_viewer/model.py index 97f117c3837..387676661a9 100644 --- a/mantidimaging/gui/windows/live_viewer/model.py +++ b/mantidimaging/gui/windows/live_viewer/model.py @@ -6,7 +6,6 @@ from typing import TYPE_CHECKING from pathlib import Path from logging import getLogger -from threading import Thread import numpy as np from PyQt5.QtCore import QFileSystemWatcher, QObject, pyqtSignal, QTimer @@ -251,14 +250,9 @@ def calc_mean_chunk(self, chunk_size: int) -> None: buffer_mean = np.mean(self.image_cache.load_image(self.images[ind])[top:bottom, left:right]) np.put(self.mean, ind, buffer_mean) - def create_new_calc_mean_all_chunks_thread(self, chunk_size: int) -> None: - self.calc_mean_all_chunks_thread = Thread(target=self.calc_mean_all_chunks, args=[chunk_size]) - def calc_mean_all_chunks(self, chunk_size: int) -> None: while np.isnan(self.mean).any(): self.calc_mean_chunk(chunk_size) - self.presenter.update_spectrum(self.mean) - class ImageWatcher(QObject): diff --git a/mantidimaging/gui/windows/live_viewer/presenter.py b/mantidimaging/gui/windows/live_viewer/presenter.py index 5318b8c2554..3085ca2d156 100644 --- a/mantidimaging/gui/windows/live_viewer/presenter.py +++ b/mantidimaging/gui/windows/live_viewer/presenter.py @@ -8,6 +8,7 @@ from logging import getLogger import numpy as np +from PyQt5.QtCore import pyqtSignal, QObject, QThread from imagecodecs._deflate import DeflateError @@ -23,6 +24,18 @@ logger = getLogger(__name__) +class Worker(QObject): + finished = pyqtSignal() + + def __init__(self, presenter: LiveViewerWindowPresenter): + super().__init__() + self.presenter = presenter + + def run(self): + self.presenter.model.calc_mean_all_chunks(100) + self.finished.emit() + + class LiveViewerWindowPresenter(BasePresenter): """ The presenter for the Live Viewer window. @@ -166,13 +179,25 @@ def handle_roi_moved(self, force_new_spectrums: bool = False): roi = self.view.live_viewer.get_roi() self.model.set_roi(roi) self.model.clear_mean_partial() - if self.model.calc_mean_all_chunks_thread is not None: - self.model.calc_mean_all_chunks_thread.join() - self.model.create_new_calc_mean_all_chunks_thread(100) - self.model.calc_mean_all_chunks_thread.start() + self.run_mean_chunk_calc() self.roi_moving = False + def run_mean_chunk_calc(self): + self.thread = QThread() + self.worker = Worker(self) + self.worker.moveToThread(self.thread) + self.thread.started.connect(self.worker.run) + self.worker.finished.connect(self.update_spectrum_with_mean) + self.worker.finished.connect(self.thread.quit) + self.worker.finished.connect(self.worker.deleteLater) + self.thread.finished.connect(self.thread.deleteLater) + self.thread.start() + def handle_roi_moved_start(self): self.roi_moving = True self.model.clear_mean_partial() self.update_spectrum(self.model.mean) + + def update_spectrum_with_mean(self): + self.view.spectrum.clearPlots() + self.view.spectrum.plot(self.model.mean)