Skip to content

Commit

Permalink
Mean is calculated in separate thread, spectrum updated in Main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSullivan7 committed Dec 10, 2024
1 parent 6d7a02c commit 38b85e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
6 changes: 0 additions & 6 deletions mantidimaging/gui/windows/live_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
33 changes: 29 additions & 4 deletions mantidimaging/gui/windows/live_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from logging import getLogger

import numpy as np
from PyQt5.QtCore import pyqtSignal, QObject, QThread

from imagecodecs._deflate import DeflateError

Expand All @@ -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.
Expand Down Expand Up @@ -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)

0 comments on commit 38b85e4

Please sign in to comment.