Skip to content

Commit

Permalink
Switch everywhere to use InstrumentLog
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed Oct 13, 2023
1 parent 9d3cd36 commit 3714d2d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions mantidimaging/core/data/imagestack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions mantidimaging/core/data/test/fake_logfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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"))
4 changes: 2 additions & 2 deletions mantidimaging/core/data/test/image_stack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions mantidimaging/core/io/loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions mantidimaging/core/io/loader/test/loader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions mantidimaging/gui/windows/main/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3714d2d

Please sign in to comment.