Skip to content

Commit

Permalink
Live view type annotation and pathlib (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEAllen authored Sep 4, 2023
2 parents 1d01652 + 8991e0d commit 9bc5b88
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mantidimaging/gui/widgets/mi_mini_image_view/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def image_item(self) -> ImageItem:
def viewbox(self) -> ViewBox:
return self.vb

def clear(self):
def clear(self) -> None:
self.im.clear()
self.set_auto_color_enabled(False)
self.clear_overlays()
Expand Down
2 changes: 1 addition & 1 deletion mantidimaging/gui/windows/live_viewer/live_view_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def show_image(self, image: np.ndarray) -> None:
"""
self.image.setImage(image)

def handle_deleted(self):
def handle_deleted(self) -> None:
"""
Handle the deletion of the image.
"""
Expand Down
37 changes: 20 additions & 17 deletions mantidimaging/gui/windows/live_viewer/model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Copyright (C) 2023 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from typing import TYPE_CHECKING
from pathlib import Path
from logging import getLogger
from PyQt5.QtCore import QFileSystemWatcher, QObject, pyqtSignal

if TYPE_CHECKING:
from os import stat_result
from mantidimaging.gui.windows.live_viewer.view import LiveViewerWindowPresenter

LOG = getLogger(__name__)
Expand All @@ -20,13 +22,13 @@ class Image_Data:
Attributes
----------
image_path : str
image_path : Path
path to image file
image_name : str
name of image file
image_size : int
size of image file
image_modified_time : int
image_modified_time : float
last modified time of image file
"""

Expand All @@ -44,16 +46,16 @@ def __init__(self, image_path: Path):
self._stat = image_path.stat()

@property
def stat(self):
def stat(self) -> stat_result:
return self._stat

@property
def image_size(self):
def image_size(self) -> int:
"""Return the image size"""
return self._stat.st_size

@property
def image_modified_time(self):
def image_modified_time(self) -> float:
"""Return the image modified time"""
return self._stat.st_mtime

Expand All @@ -68,7 +70,7 @@ class LiveViewerWindowModel:
----------
presenter : LiveViewerWindowPresenter
presenter for the spectrum viewer window
path : str
path : Path
path to dataset
"""

Expand All @@ -83,16 +85,17 @@ def __init__(self, presenter: 'LiveViewerWindowPresenter'):
"""

self.presenter = presenter
self._dataset_path = None
self._dataset_path: Path | None = None
self.image_watcher: ImageWatcher | None = None

@property
def path(self):
def path(self) -> Path | None:
return self._dataset_path

@path.setter
def path(self, path):
def path(self, path: Path) -> None:
self._dataset_path = path
self.image_watcher = ImageWatcher(self.path)
self.image_watcher = ImageWatcher(path)
self.image_watcher.image_changed.connect(self._handle_image_changed_in_list)
self.image_watcher.find_images()
self.image_watcher.get_images()
Expand Down Expand Up @@ -120,7 +123,7 @@ class ImageWatcher(QObject):
Attributes
----------
directory : str
directory : Path
path to directory to watch
watcher : QFileSystemWatcher
file system watcher to watch directory
Expand All @@ -142,13 +145,13 @@ class ImageWatcher(QObject):
"""
image_changed = pyqtSignal(list) # Signal emitted when an image is added or removed

def __init__(self, directory):
def __init__(self, directory: Path):
"""
Constructor for ImageWatcher class which inherits from QObject.
Parameters
----------
directory : str
directory : Path
path to directory to watch
"""

Expand All @@ -157,7 +160,7 @@ def __init__(self, directory):
self.watcher = QFileSystemWatcher()
self.watcher.directoryChanged.connect(self._handle_directory_change)
self.watcher.addPath(str(self.directory))
self.images = []
self.images: list[Image_Data] = []

def find_images(self) -> None:
"""
Expand All @@ -175,11 +178,11 @@ def sort_images_by_modified_time(self, images: list[Image_Data]) -> list[Image_D
"""
return sorted(images, key=lambda x: x.image_modified_time)

def get_images(self):
def get_images(self) -> list[Image_Data]:
"""Return the sorted images"""
return self.images

def _handle_directory_change(self, directory) -> None:
def _handle_directory_change(self, directory: str) -> None:
"""
Handle a directory change event. Update the list of images
to reflect directory changes and emit the image_changed signal
Expand All @@ -193,7 +196,7 @@ def _handle_directory_change(self, directory) -> None:
except FileNotFoundError:
self.image_changed.emit([])

def _get_image_files(self):
def _get_image_files(self) -> list[Image_Data]:
image_files = []
for file_path in Path(self.directory).iterdir():
if self._is_image_file(file_path.name):
Expand Down
12 changes: 7 additions & 5 deletions mantidimaging/gui/windows/live_viewer/presenter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Copyright (C) 2023 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from logging import getLogger

from imagecodecs._deflate import DeflateError
from tifffile import tifffile

from mantidimaging.gui.mvp_base import BasePresenter
from mantidimaging.gui.windows.live_viewer.model import LiveViewerWindowModel
from mantidimaging.gui.windows.live_viewer.model import LiveViewerWindowModel, Image_Data

if TYPE_CHECKING:
from mantidimaging.gui.windows.live_viewer.view import LiveViewerWindowView # pragma: no cover
Expand All @@ -34,20 +36,20 @@ def __init__(self, view: LiveViewerWindowView, main_window: MainWindowView):
self.main_window = main_window
self.model = LiveViewerWindowModel(self)

def set_dataset_path(self, path):
def set_dataset_path(self, path: Path) -> None:
"""Set the path to the dataset."""
self.model.path = path

def clear_label(self):
def clear_label(self) -> None:
"""Clear the label."""
self.view.label_active_filename.setText("")

def handle_deleted(self):
def handle_deleted(self) -> None:
"""Handle the deletion of the image."""
self.view.remove_image()
self.clear_label()

def update_image(self, images_list: list):
def update_image(self, images_list: list[Image_Data]) -> None:
"""Update the image in the view."""
if not images_list:
self.view.remove_image()
Expand Down
4 changes: 2 additions & 2 deletions mantidimaging/gui/windows/live_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def show_most_recent_image(self, image: np.ndarray) -> None:
"""
self.live_viewer.show_image(image)

def watch_directory(self):
def watch_directory(self) -> None:
"""Show the most recent image arrived in the selected directory"""
self.presenter.set_dataset_path(self.path)

def remove_image(self):
def remove_image(self) -> None:
"""Remove the image from the view."""
self.live_viewer.handle_deleted()

0 comments on commit 9bc5b88

Please sign in to comment.