From 86bde5504e6b4d1501daa913a708133060b257df Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 11:25:02 -0300 Subject: [PATCH 1/5] improve error message for tiff imaging extractor --- .../tiffimagingextractors/tiffimagingextractor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index 9ae0a09d..4ff5938e 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -56,10 +56,11 @@ def __init__(self, file_path: PathType, sampling_frequency: FloatType): try: self._video = tifffile.memmap(self.file_path, mode="r") - except ValueError: + except ValueError as e: warn( - "memmap of TIFF file could not be established. Reading entire matrix into memory. " - "Consider using the ScanImageTiffExtractor for lazy data access." + f"memmap of TIFF file could not be established due to the following error: {e}. " + "Reading entire matrix into memory. Consider using the ScanImageTiffExtractor for lazy data access.", + stacklevel=2, ) with tifffile.TiffFile(self.file_path) as tif: self._video = tif.asarray() From 633856daadcd0545ff705aad80550dfab1d43137 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 11:33:13 -0300 Subject: [PATCH 2/5] nesting try --- .../tiffimagingextractor.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index 4ff5938e..a890b87a 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -56,14 +56,23 @@ def __init__(self, file_path: PathType, sampling_frequency: FloatType): try: self._video = tifffile.memmap(self.file_path, mode="r") - except ValueError as e: - warn( - f"memmap of TIFF file could not be established due to the following error: {e}. " - "Reading entire matrix into memory. Consider using the ScanImageTiffExtractor for lazy data access.", - stacklevel=2, - ) - 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 ScanImageTiffExtractor 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 ScanImageTiffExtractor 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: From 8df1c8911c39c474bb41cef496bfa3b55585d921 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 14:12:18 -0300 Subject: [PATCH 3/5] test changelog --- CHANGELOG.md | 1 + tests/test_scan_image_tiff.py | 4 ++-- 2 files changed, 3 insertions(+), 2 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/tests/test_scan_image_tiff.py b/tests/test_scan_image_tiff.py index 589f1811..198e89e5 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 ScanImageTiffExtractor for lazy data access." ), ): TiffImagingExtractor(file_path=self.file_path, sampling_frequency=30.0) From bbba94d1393d771f6e11fd7a73a4aca228e4048a Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 14:59:36 -0300 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Paul Adkisson --- .../extractors/tiffimagingextractors/tiffimagingextractor.py | 2 +- tests/test_scan_image_tiff.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index a890b87a..2e1765f1 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -70,7 +70,7 @@ def __init__(self, file_path: PathType, sampling_frequency: FloatType): raise RuntimeError( f"Memory mapping failed: {e}. \n" f"Attempt to read the TIFF file directly also failed: {e2}. \n" - f"Consider using ScanImageTiffExtractor for lazy data access, check the file integrity. \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." ) diff --git a/tests/test_scan_image_tiff.py b/tests/test_scan_image_tiff.py index 198e89e5..60853b24 100644 --- a/tests/test_scan_image_tiff.py +++ b/tests/test_scan_image_tiff.py @@ -38,7 +38,7 @@ def test_tiff_non_memmap_warning(self): warn_type=UserWarning, exc_msg=( "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 ScanImageTiffExtractor for lazy data access." + "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) From 4e65e06032dec1d639b5d86ba6f90e602dcd2eb2 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Tue, 13 Aug 2024 15:04:35 -0300 Subject: [PATCH 5/5] Update src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py Co-authored-by: Paul Adkisson --- .../extractors/tiffimagingextractors/tiffimagingextractor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index 2e1765f1..2d77ae49 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -63,7 +63,7 @@ def __init__(self, file_path: PathType, sampling_frequency: FloatType): 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 ScanImageTiffExtractor for lazy data access.", + "Reading entire matrix into memory. Consider using the ScanImageTiffSinglePlaneImagingExtractor or ScanImageTiffMultiPlaneImagingExtractor for lazy data access.", stacklevel=2, ) except Exception as e2: