Skip to content

Commit

Permalink
Live view open dataset (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEAllen authored Oct 30, 2024
2 parents b1f5312 + 8abc62e commit 8914c75
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/release_notes/next/feature-2351-live-view-load-dataset
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#2351: Option in live view menu to load current images as dataset

14 changes: 10 additions & 4 deletions mantidimaging/gui/windows/live_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ def update_image_list(self, images_list: list[Image_Data]) -> None:
"""Update the image in the view."""
if not images_list:
self.handle_deleted()
return

self.view.set_image_range((0, len(images_list) - 1))
self.view.set_image_index(len(images_list) - 1)
self.view.set_load_as_dataset_enabled(False)
else:
self.view.set_image_range((0, len(images_list) - 1))
self.view.set_image_index(len(images_list) - 1)
self.view.set_load_as_dataset_enabled(True)

def select_image(self, index: int) -> None:
if not self.model.images:
Expand Down Expand Up @@ -153,3 +154,8 @@ def perform_operations(self, image_data) -> np.ndarray:
op_params = self.view.filter_params[operation]["params"]
op_func(image_stack, **op_params)
return image_stack.slice_as_array(0)[0]

def load_as_dataset(self) -> None:
if self.model.images:
image_dir = self.model.images[0].image_path.parent
self.main_window.show_image_load_dialog_with_path(str(image_dir))
51 changes: 51 additions & 0 deletions mantidimaging/gui/windows/live_viewer/test/presenter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

import unittest
from pathlib import Path
from unittest import mock

from parameterized import parameterized

from mantidimaging.gui.windows.live_viewer import LiveViewerWindowView, LiveViewerWindowModel, LiveViewerWindowPresenter
from mantidimaging.gui.windows.live_viewer.model import Image_Data
from mantidimaging.gui.windows.main import MainWindowView


class MainWindowPresenterTest(unittest.TestCase):

def setUp(self):
self.view = mock.create_autospec(LiveViewerWindowView)
self.main_window = mock.create_autospec(MainWindowView)
self.model = mock.create_autospec(LiveViewerWindowModel)

with mock.patch("mantidimaging.gui.windows.live_viewer.presenter.LiveViewerWindowModel") as mock_model:
mock_model.return_value = self.model
self.presenter = LiveViewerWindowPresenter(self.view, self.main_window)

def test_load_as_dataset(self):
image_dir = Path("/path/to/dir")
image_paths = [image_dir / f"image_{i:03d}.tif" for i in range(5)]
self.model.images = [mock.create_autospec(Image_Data, image_path=p) for p in image_paths]

self.presenter.load_as_dataset()

self.main_window.show_image_load_dialog_with_path.assert_called_once_with(str(image_dir))

def test_load_as_dataset_empty_dir(self):
self.model.images = []

self.presenter.load_as_dataset()

self.main_window.show_image_load_dialog_with_path.assert_not_called()

@parameterized.expand([
([], False),
([mock.Mock()], True),
])
def test_load_as_dataset_enabled_when_images(self, image_list, action_enabled):
with mock.patch.object(self.presenter, "handle_deleted"):
self.presenter.update_image_list(image_list)

self.view.set_load_as_dataset_enabled.assert_called_once_with(action_enabled)
6 changes: 6 additions & 0 deletions mantidimaging/gui/windows/live_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __init__(self, main_window: MainWindowView, live_dir_path: Path) -> None:
if angle == 0:
action.setChecked(True)

self.load_as_dataset_action = self.right_click_menu.addAction("Load as dataset")
self.load_as_dataset_action.triggered.connect(self.presenter.load_as_dataset)

def show(self) -> None:
"""Show the window"""
super().show()
Expand Down Expand Up @@ -100,3 +103,6 @@ def set_image_rotation_angle(self) -> None:
image_rotation_angle = int(self.rotate_angles_group.checkedAction().text().replace('°', ''))
self.filter_params["Rotate Stack"] = {"params": {"angle": image_rotation_angle}}
self.presenter.update_image_operation()

def set_load_as_dataset_enabled(self, enabled: bool):
self.load_as_dataset_action.setEnabled(enabled)
2 changes: 2 additions & 0 deletions mantidimaging/gui/windows/main/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def show_image_load_dialog_with_path(self, file_path: str) -> bool:
Open the dataset loading dialog with a given file_path preset as the sample
"""
sample_file = find_first_file_that_is_possibly_a_sample(file_path)
if sample_file is None:
sample_file = find_first_file_that_is_possibly_a_sample(os.path.dirname(file_path))
if sample_file is not None:
self.image_load_dialog = ImageLoadDialog(self)
self.image_load_dialog.presenter.do_update_sample(sample_file)
Expand Down

0 comments on commit 8914c75

Please sign in to comment.