From 3714d2d2ff37d6d60e3fe4c21bd8b77d95650a66 Mon Sep 17 00:00:00 2001 From: Sam Tygier Date: Fri, 13 Oct 2023 16:31:58 +0100 Subject: [PATCH] Switch everywhere to use InstrumentLog --- mantidimaging/core/data/imagestack.py | 8 ++++---- mantidimaging/core/data/test/fake_logfile.py | 11 ++++++----- mantidimaging/core/data/test/image_stack_test.py | 4 ++-- mantidimaging/core/io/loader/loader.py | 6 +++--- mantidimaging/core/io/loader/test/loader_test.py | 4 ++-- .../windows/image_load_dialog/test/presenter_test.py | 6 +++--- mantidimaging/gui/windows/main/model.py | 5 +++-- 7 files changed, 23 insertions(+), 21 deletions(-) diff --git a/mantidimaging/core/data/imagestack.py b/mantidimaging/core/data/imagestack.py index 0e16f084187..d6eb5136e49 100644 --- a/mantidimaging/core/data/imagestack.py +++ b/mantidimaging/core/data/imagestack.py @@ -19,7 +19,7 @@ from mantidimaging.core.utility.leak_tracker import leak_tracker if TYPE_CHECKING: - from mantidimaging.core.utility.imat_log_file_parser import IMATLogFile + from mantidimaging.core.io.instrument_log import InstrumentLog class ImageStack: @@ -55,7 +55,7 @@ def __init__(self, self._is_sinograms = sinograms self._proj180deg: Optional[ImageStack] = None - self._log_file: Optional[IMATLogFile] = None + self._log_file: InstrumentLog | None = None self._projection_angles: Optional[ProjectionAngles] = None if name is None: @@ -286,11 +286,11 @@ def is_sinograms(self) -> bool: return self._is_sinograms @property - def log_file(self): + def log_file(self) -> InstrumentLog | None: return self._log_file @log_file.setter - def log_file(self, value: IMATLogFile): + def log_file(self, value: InstrumentLog | None) -> None: if value is not None: self.metadata[const.LOG_FILE] = str(value.source_file) elif value is None: diff --git a/mantidimaging/core/data/test/fake_logfile.py b/mantidimaging/core/data/test/fake_logfile.py index f0e6b87fa99..9e811654a30 100644 --- a/mantidimaging/core/data/test/fake_logfile.py +++ b/mantidimaging/core/data/test/fake_logfile.py @@ -3,10 +3,11 @@ from __future__ import annotations from pathlib import Path -from mantidimaging.core.utility.imat_log_file_parser import CSVLogParser, IMATLogFile, TextLogParser +from mantidimaging.core.io.instrument_log import InstrumentLog +from mantidimaging.core.utility.imat_log_file_parser import CSVLogParser, TextLogParser -def generate_txt_logfile() -> IMATLogFile: +def generate_txt_logfile() -> InstrumentLog: data = [ TextLogParser.EXPECTED_HEADER_FOR_IMAT_TEXT_LOG_FILE, # checked if exists, but skipped "", # skipped when parsing @@ -22,10 +23,10 @@ def generate_txt_logfile() -> IMATLogFile: "Sun Feb 10 00:26:29 2019 Projection: 8 angle: 2.5216 Monitor 3 before: 5786535 Monitor 3 after: 5929002", # noqa: E501 "Sun Feb 10 00:27:02 2019 Projection: 9 angle: 2.8368 Monitor 3 before: 5938142 Monitor 3 after: 6078866", # noqa: E501 ] - return IMATLogFile(data, Path("/tmp/fake")) + return InstrumentLog(data, Path("/tmp/fake.txt")) -def generate_csv_logfile() -> IMATLogFile: +def generate_csv_logfile() -> InstrumentLog: data = [ CSVLogParser.EXPECTED_HEADER_FOR_IMAT_CSV_LOG_FILE, "Sun Feb 10 00:22:04 2019,Projection,0,angle: 0.0,Monitor 3 before: 4577907,Monitor 3 after: 4720271", @@ -39,4 +40,4 @@ def generate_csv_logfile() -> IMATLogFile: "Sun Feb 10 00:26:29 2019,Projection,8,angle: 2.5216,Monitor 3 before: 5786535,Monitor 3 after: 5929002", "Sun Feb 10 00:27:02 2019,Projection,9,angle: 2.8368,Monitor 3 before: 5938142,Monitor 3 after: 6078866", ] - return IMATLogFile(data, Path("/tmp/fake")) + return InstrumentLog(data, Path("/tmp/fake.csv")) diff --git a/mantidimaging/core/data/test/image_stack_test.py b/mantidimaging/core/data/test/image_stack_test.py index 9b90a45dfe0..567a0483363 100644 --- a/mantidimaging/core/data/test/image_stack_test.py +++ b/mantidimaging/core/data/test/image_stack_test.py @@ -6,6 +6,7 @@ from pathlib import Path from unittest import mock +from mantidimaging.core.io.instrument_log import InstrumentLog from mantidimaging.core.utility.data_containers import ProjectionAngles import unittest @@ -15,7 +16,6 @@ from mantidimaging.core.data.test.fake_logfile import generate_csv_logfile, generate_txt_logfile from mantidimaging.core.operations.crop_coords import CropCoordinatesFilter from mantidimaging.core.operation_history import const -from mantidimaging.core.utility.imat_log_file_parser import IMATLogFile from mantidimaging.core.utility.sensible_roi import SensibleROI from mantidimaging.test_helpers.unit_test_helper import generate_images @@ -73,7 +73,7 @@ def test_loading_metadata_preserves_existing(self): def test_loading_metadata_preserves_existing_log(self): json_file = io.StringIO('{"pixel_size": 30.0, "log_file": "/old/logfile"}') mock_log_path = Path("/aaa/bbb") - mock_log_file = mock.create_autospec(IMATLogFile, source_file=mock_log_path) + mock_log_file = mock.create_autospec(InstrumentLog, source_file=mock_log_path) imgs = ImageStack(np.asarray([1])) self.assertEqual({}, imgs.metadata) diff --git a/mantidimaging/core/io/loader/loader.py b/mantidimaging/core/io/loader/loader.py index 579fd462707..59791e406f5 100644 --- a/mantidimaging/core/io/loader/loader.py +++ b/mantidimaging/core/io/loader/loader.py @@ -11,10 +11,10 @@ import astropy.io.fits as fits from tifffile import tifffile +from mantidimaging.core.io.instrument_log import InstrumentLog from mantidimaging.core.io.loader import img_loader from mantidimaging.core.io.utility import find_first_file_that_is_possibly_a_sample from mantidimaging.core.utility.data_containers import Indices, FILE_TYPES, ProjectionAngles -from mantidimaging.core.utility.imat_log_file_parser import IMATLogFile from mantidimaging.core.io.filenames import FilenameGroup if TYPE_CHECKING: @@ -91,9 +91,9 @@ def read_image_dimensions(file_path: Path) -> Tuple[int, int]: return img.shape -def load_log(log_file: Path) -> IMATLogFile: +def load_log(log_file: Path) -> InstrumentLog: with open(log_file, 'r') as f: - return IMATLogFile(f.readlines(), log_file) + return InstrumentLog(f.readlines(), log_file) def load_stack_from_group(group: FilenameGroup, progress: Optional[Progress] = None) -> ImageStack: diff --git a/mantidimaging/core/io/loader/test/loader_test.py b/mantidimaging/core/io/loader/test/loader_test.py index e462e5e88bb..10cd4a561fb 100644 --- a/mantidimaging/core/io/loader/test/loader_test.py +++ b/mantidimaging/core/io/loader/test/loader_test.py @@ -7,11 +7,11 @@ import numpy as np from mantidimaging.core.io.filenames import FilenameGroup +from mantidimaging.core.io.instrument_log import InstrumentLog from mantidimaging.core.io.loader.loader import (DEFAULT_PIXEL_DEPTH, DEFAULT_PIXEL_SIZE, DEFAULT_IS_SINOGRAM, create_loading_parameters_for_file_path, get_loader, load, _imread) from mantidimaging.core.utility.data_containers import FILE_TYPES, ProjectionAngles -from mantidimaging.core.utility.imat_log_file_parser import IMATLogFile from mantidimaging.test_helpers.unit_test_helper import FakeFSTestCase @@ -81,7 +81,7 @@ def test_load_with_golden_angles(self, mock_execute: mock.Mock, mock_load_log: m mock_filename_group.all_files.return_value = filenames mock_filename_group.first_file.return_value = filenames[0] - mock_log_data = mock.create_autospec(IMATLogFile) + mock_log_data = mock.create_autospec(InstrumentLog) mock_log_data.projection_angles.return_value = ProjectionAngles(np.deg2rad(angles)) mock_load_log.return_value = mock_log_data diff --git a/mantidimaging/gui/windows/image_load_dialog/test/presenter_test.py b/mantidimaging/gui/windows/image_load_dialog/test/presenter_test.py index 1af8acab98c..dd933a7715c 100644 --- a/mantidimaging/gui/windows/image_load_dialog/test/presenter_test.py +++ b/mantidimaging/gui/windows/image_load_dialog/test/presenter_test.py @@ -7,7 +7,7 @@ from unittest import mock from mantidimaging.core.io.filenames import FilenameGroup -from mantidimaging.core.utility.imat_log_file_parser import IMATLogFile +from mantidimaging.core.io.instrument_log import InstrumentLog from mantidimaging.gui.windows.image_load_dialog.field import Field from mantidimaging.gui.windows.image_load_dialog.presenter import LoadPresenter from mantidimaging.core.utility.data_containers import FILE_TYPES, Indices @@ -165,7 +165,7 @@ def test_ensure_sample_log_consistency_matching(self, mock_load_log): """ Test behaviour when the number of projection angles and files matches """ - mock_log = mock.create_autospec(IMATLogFile) + mock_log = mock.create_autospec(InstrumentLog) mock_load_log.return_value = mock_log file_name = "file_name" field = mock.MagicMock() @@ -182,7 +182,7 @@ def test_ensure_sample_log_consistency_matching(self, mock_load_log): @mock.patch("mantidimaging.gui.windows.image_load_dialog.presenter.load_log") def test_ensure_sample_log_consistency_exits_when_none_or_empty_str(self, mock_load_log): - mock_log = mock.create_autospec(IMATLogFile) + mock_log = mock.create_autospec(InstrumentLog) mock_load_log.return_value = mock_log file_name = None field = mock.MagicMock() diff --git a/mantidimaging/gui/windows/main/model.py b/mantidimaging/gui/windows/main/model.py index 55b3d63c508..0612c06cf8b 100644 --- a/mantidimaging/gui/windows/main/model.py +++ b/mantidimaging/gui/windows/main/model.py @@ -139,12 +139,13 @@ def raise_error_when_parent_dataset_not_found(self, images_id: uuid.UUID) -> NoR def raise_error_when_parent_strict_dataset_not_found(self, images_id: uuid.UUID) -> NoReturn: raise RuntimeError(f"Failed to find strict dataset containing ImageStack with ID {images_id}") - def add_log_to_sample(self, images_id: uuid.UUID, log_file: Path): + def add_log_to_sample(self, images_id: uuid.UUID, log_file: Path) -> None: images = self.get_images_by_uuid(images_id) if images is None: raise RuntimeError log = loader.load_log(log_file) - log.raise_if_angle_missing(images.filenames) + if images.filenames is not None: + log.raise_if_angle_missing(images.filenames) images.log_file = log def _remove_dataset(self, dataset_id: uuid.UUID):