From 9e96c848df5dab1175197e79317a4de6b7636e20 Mon Sep 17 00:00:00 2001 From: Mike Sullivan Date: Mon, 11 Dec 2023 15:48:39 +0000 Subject: [PATCH 1/2] added fits compatibility for live viewer --- mantidimaging/gui/windows/live_viewer/model.py | 2 +- mantidimaging/gui/windows/live_viewer/presenter.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/mantidimaging/gui/windows/live_viewer/model.py b/mantidimaging/gui/windows/live_viewer/model.py index 4a89dcf4ae8..9e2e50e188e 100644 --- a/mantidimaging/gui/windows/live_viewer/model.py +++ b/mantidimaging/gui/windows/live_viewer/model.py @@ -259,7 +259,7 @@ def _is_image_file(file_name: str) -> bool: :param file_name: name of file :return: True if file is an image file """ - image_extensions = ['.tif', '.tiff'] + image_extensions = ['.tif', '.tiff', '.fits'] file_names = any(file_name.lower().endswith(ext) for ext in image_extensions) return file_names diff --git a/mantidimaging/gui/windows/live_viewer/presenter.py b/mantidimaging/gui/windows/live_viewer/presenter.py index bd28b87ee0a..5a63e7303ed 100644 --- a/mantidimaging/gui/windows/live_viewer/presenter.py +++ b/mantidimaging/gui/windows/live_viewer/presenter.py @@ -8,6 +8,7 @@ from imagecodecs._deflate import DeflateError from tifffile import tifffile, TiffFileError +from astropy.io import fits from mantidimaging.gui.mvp_base import BasePresenter from mantidimaging.gui.windows.live_viewer.model import LiveViewerWindowModel, Image_Data @@ -77,8 +78,12 @@ def select_image(self, index: int) -> None: def load_and_display_image(self, image_path: Path): try: - with tifffile.TiffFile(image_path) as tif: - image_data = tif.asarray() + if image_path.__str__().lower().endswith('.tif') or image_path.__str__().lower().endswith('.tiff'): + with tifffile.TiffFile(image_path) as tif: + image_data = tif.asarray() + elif image_path.__str__().lower().endswith('.fits'): + with fits.open(image_path.__str__()) as fit: + image_data = fit[0].data except (IOError, KeyError, ValueError, TiffFileError, DeflateError) as error: message = f"{type(error).__name__} reading image: {image_path}: {error}" logger.error(message) From 17d74e5ef97bdea2ed9c115c72dce158d2f44b0e Mon Sep 17 00:00:00 2001 From: Mike Sullivan Date: Wed, 13 Dec 2023 11:04:16 +0000 Subject: [PATCH 2/2] Suggested changes made --- docs/release_notes/next/feature-1996-fits-compatibility | 1 + mantidimaging/gui/windows/live_viewer/model.py | 5 ++--- mantidimaging/gui/windows/live_viewer/presenter.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 docs/release_notes/next/feature-1996-fits-compatibility diff --git a/docs/release_notes/next/feature-1996-fits-compatibility b/docs/release_notes/next/feature-1996-fits-compatibility new file mode 100644 index 00000000000..d77ae6675c9 --- /dev/null +++ b/docs/release_notes/next/feature-1996-fits-compatibility @@ -0,0 +1 @@ +#1996 : Added .fits file compatibility for Live Viewer diff --git a/mantidimaging/gui/windows/live_viewer/model.py b/mantidimaging/gui/windows/live_viewer/model.py index 9e2e50e188e..0e6ee23d960 100644 --- a/mantidimaging/gui/windows/live_viewer/model.py +++ b/mantidimaging/gui/windows/live_viewer/model.py @@ -259,9 +259,8 @@ def _is_image_file(file_name: str) -> bool: :param file_name: name of file :return: True if file is an image file """ - image_extensions = ['.tif', '.tiff', '.fits'] - file_names = any(file_name.lower().endswith(ext) for ext in image_extensions) - return file_names + image_extensions = ('tif', 'tiff', 'fits') + return file_name.rpartition(".")[2].lower() in image_extensions def remove_path(self): """ diff --git a/mantidimaging/gui/windows/live_viewer/presenter.py b/mantidimaging/gui/windows/live_viewer/presenter.py index 5a63e7303ed..f6b9168b214 100644 --- a/mantidimaging/gui/windows/live_viewer/presenter.py +++ b/mantidimaging/gui/windows/live_viewer/presenter.py @@ -78,10 +78,10 @@ def select_image(self, index: int) -> None: def load_and_display_image(self, image_path: Path): try: - if image_path.__str__().lower().endswith('.tif') or image_path.__str__().lower().endswith('.tiff'): + if image_path.suffix.lower() in [".tif", ".tiff"]: with tifffile.TiffFile(image_path) as tif: image_data = tif.asarray() - elif image_path.__str__().lower().endswith('.fits'): + elif image_path.suffix.lower() == ".fits": with fits.open(image_path.__str__()) as fit: image_data = fit[0].data except (IOError, KeyError, ValueError, TiffFileError, DeflateError) as error: