From 28b843f1aba356f8df571d862f9c50d8f08b604b Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 15:13:59 -0300 Subject: [PATCH] Improve error message for tiff imaging extractor (#353) * improve error message for tiff imaging extractor * nesting try * test changelog * Apply suggestions from code review Co-authored-by: Paul Adkisson * Update src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py Co-authored-by: Paul Adkisson --------- Co-authored-by: Paul Adkisson --- CHANGELOG.md | 1 + .../tiffimagingextractor.py | 24 +++++++++++++------ tests/test_scan_image_tiff.py | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f95f9b3..66131e81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ * The `Suite2PSegmentationExtractor` now produces an error when a required sub-file is missin: [#330](https://github.com/catalystneuro/roiextractors/pull/330) * Added `_image_mask` initialization in `BaseSegmentationExtractor`; combined `abstractmethod`s into top of file: [#327](https://github.com/catalystneuro/roiextractors/pull/327) * Optimize parsing of xml with `lxml` library for Burker extractors: [#346](https://github.com/catalystneuro/roiextractors/pull/346) +* Improve error message when `TiffImagingExtractor` is not able to form memmap [#353](https://github.com/catalystneuro/roiextractors/pull/353) ### Testing diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index 9ae0a09d..2d77ae49 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -56,13 +56,23 @@ def __init__(self, file_path: PathType, sampling_frequency: FloatType): try: self._video = tifffile.memmap(self.file_path, mode="r") - except ValueError: - warn( - "memmap of TIFF file could not be established. Reading entire matrix into memory. " - "Consider using the ScanImageTiffExtractor for lazy data access." - ) - with tifffile.TiffFile(self.file_path) as tif: - self._video = tif.asarray() + except Exception as e: + + try: + with tifffile.TiffFile(self.file_path) as tif: + self._video = tif.asarray() + warn( + f"memmap of TIFF file could not be established due to the following error: {e}. " + "Reading entire matrix into memory. Consider using the ScanImageTiffSinglePlaneImagingExtractor or ScanImageTiffMultiPlaneImagingExtractor for lazy data access.", + stacklevel=2, + ) + except Exception as e2: + raise RuntimeError( + f"Memory mapping failed: {e}. \n" + f"Attempt to read the TIFF file directly also failed: {e2}. \n" + f"Consider using ScanImageTiffSinglePlaneImagingExtractor or ScanImageTiffMultiPlaneImagingExtractor for lazy data access, check the file integrity. \n" + f"If problems persist, please report an issue at roiextractors/issues." + ) shape = self._video.shape if len(shape) == 3: diff --git a/tests/test_scan_image_tiff.py b/tests/test_scan_image_tiff.py index 589f1811..60853b24 100644 --- a/tests/test_scan_image_tiff.py +++ b/tests/test_scan_image_tiff.py @@ -37,8 +37,8 @@ def test_tiff_non_memmap_warning(self): with self.assertWarnsWith( warn_type=UserWarning, exc_msg=( - "memmap of TIFF file could not be established. Reading entire matrix into memory. " - "Consider using the ScanImageTiffExtractor for lazy data access." + "memmap of TIFF file could not be established due to the following error: image data are not memory-mappable. " + "Reading entire matrix into memory. Consider using the ScanImageTiffSinglePlaneImagingExtractor or ScanImageTiffMultiPlaneImagingExtractor for lazy data access." ), ): TiffImagingExtractor(file_path=self.file_path, sampling_frequency=30.0)