Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress bar for binned RITS export #2033

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from mantidimaging.core.io import saver
from mantidimaging.core.io.instrument_log import LogColumn
from mantidimaging.core.utility.sensible_roi import SensibleROI
from mantidimaging.core.utility.progress_reporting import Progress

if TYPE_CHECKING:
from mantidimaging.gui.windows.spectrum_viewer.presenter import SpectrumViewerWindowPresenter
Expand Down Expand Up @@ -61,8 +62,6 @@ def __init__(self, presenter: 'SpectrumViewerWindowPresenter'):
self._roi_id_counter = 0
self._roi_ranges = {}
self.special_roi_list = [ROI_ALL]
self.bin_size: int = 10
self.step_size: int = 1

def roi_name_generator(self) -> str:
"""
Expand Down Expand Up @@ -315,7 +314,12 @@ def validate_bin_and_step_size(self, roi, bin_size: int, step_size: int) -> None
if bin_size and step_size > min(roi.width, roi.height):
raise ValueError("Both bin size and step size must be less than or equal to the ROI size")

def save_rits_images(self, directory: Path, error_mode: ErrorMode, bin_size, step) -> None:
def save_rits_images(self,
directory: Path,
error_mode: ErrorMode,
bin_size: int,
step: int,
progress: Progress | None = None) -> None:
"""
Saves multiple Region of Interest (ROI) images to RITS files.

Expand All @@ -337,11 +341,11 @@ def save_rits_images(self, directory: Path, error_mode: ErrorMode, bin_size, ste
Returns:
None
"""

roi = self.get_roi(ROI_RITS)
left, top, right, bottom = roi
x_iterations = min(ceil((right - left) / step), ceil((right - left - bin_size) / step) + 1)
y_iterations = min(ceil((bottom - top) / step), ceil((bottom - top - bin_size) / step) + 1)
progress = Progress.ensure_instance(progress, num_steps=x_iterations * y_iterations)

self.validate_bin_and_step_size(roi, bin_size, step)
for y in range(y_iterations):
Expand All @@ -353,6 +357,7 @@ def save_rits_images(self, directory: Path, error_mode: ErrorMode, bin_size, ste
sub_roi = SensibleROI.from_list([sub_left, sub_top, sub_right, sub_bottom])
path = directory / f"rits_image_{x}_{y}.dat"
self.save_rits_roi(path, error_mode, sub_roi)
progress.update()
if sub_right == right:
break
if sub_bottom == bottom:
Expand Down
12 changes: 11 additions & 1 deletion mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from __future__ import annotations

from enum import Enum
from functools import partial
from typing import TYPE_CHECKING, Optional

from logging import getLogger
from mantidimaging.core.data.dataset import StrictDataset
from mantidimaging.gui.dialogs.async_task import start_async_task_view, TaskWorkerThread
from mantidimaging.gui.mvp_base import BasePresenter
from mantidimaging.gui.windows.spectrum_viewer.model import SpectrumViewerWindowModel, SpecType, ROI_RITS, ErrorMode

Expand Down Expand Up @@ -178,7 +180,11 @@ def handle_rits_export(self) -> None:
if path is None:
LOG.debug("No path selected, aborting export")
return
self.model.save_rits_images(path, error_mode, self.view.bin_size, self.view.bin_step)
run_function = partial(self.model.save_rits_images, path, error_mode, self.view.bin_size,
self.view.bin_step)

start_async_task_view(self.view, run_function, self._async_save_done)

else:
path = self.view.get_rits_export_filename()
if path is None:
Expand All @@ -188,6 +194,10 @@ def handle_rits_export(self) -> None:
path = path.with_suffix(".dat")
self.model.save_single_rits_spectrum(path, error_mode)

def _async_save_done(self, task: TaskWorkerThread) -> None:
if task.error is not None:
self.view.show_error_dialog(f"Operation failed: {task.error}")

def handle_enable_normalised(self, enabled: bool) -> None:
if enabled:
self.spectrum_mode = SpecType.SAMPLE_NORMED
Expand Down
Loading