diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bb0edca..a846c99e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ * 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) * Protect sima and dill export [#351](https://github.com/catalystneuro/roiextractors/pull/351) +* 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)