Skip to content

Commit

Permalink
spectrum is updated asyncronously via a Thread (with warnings)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSullivan7 committed Dec 6, 2024
1 parent 5394271 commit 6d7a02c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 12 additions & 1 deletion mantidimaging/gui/windows/live_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 @@ -165,6 +166,7 @@ def __init__(self, presenter: LiveViewerWindowPresenter):
self.roi: SensibleROI | None = None
self.image_cache = ImageCache(max_cache_size=10)
self.mean_cached: np.ndarray = np.empty(0)
self.calc_mean_all_chunks_thread = None

@property
def path(self) -> Path | None:
Expand Down Expand Up @@ -214,7 +216,7 @@ def close(self) -> None:
self.presenter = None # type: ignore # Model instance to be destroyed -type can be inconsistent

def add_mean(self, image_data_obj: Image_Data, image_array: np.ndarray) -> None:
if self.roi:
if self.roi and (self.roi.left, self.roi.top, self.roi.right, self.roi.bottom) != (0, 0, 0, 0):
left, top, right, bottom = self.roi
mean_to_add = np.mean(image_array[top:bottom, left:right])
else:
Expand Down Expand Up @@ -249,6 +251,15 @@ 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
11 changes: 4 additions & 7 deletions mantidimaging/gui/windows/live_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ def handle_deleted(self) -> None:

def update_image_list(self, images_list: list[Image_Data]) -> None:
"""Update the image in the view."""
# TODO: put add_mean in here and check that if images have been deleted,
# these are compared against the mean_dict
# throw away and recalc the mean_dict if needed
if not images_list:
self.handle_deleted()
self.view.set_load_as_dataset_enabled(False)
Expand All @@ -80,8 +77,6 @@ def update_image_list(self, images_list: list[Image_Data]) -> None:
if images_list[-1].image_path not in self.model.mean_dict.keys():
image_data = self.model.image_cache.load_image(images_list[-1])
self.model.add_mean(images_list[-1], image_data)
if not self.roi_moving:
self.model.calc_mean_chunk(50)
self.update_spectrum(self.model.mean)
self.view.set_image_range((0, len(images_list) - 1))
self.view.set_image_index(len(images_list) - 1)
Expand Down Expand Up @@ -171,8 +166,10 @@ 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()
self.model.calc_mean_chunk(100)
self.update_spectrum(self.model.mean)
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.roi_moving = False

def handle_roi_moved_start(self):
Expand Down

0 comments on commit 6d7a02c

Please sign in to comment.