From 43a8849c7cb25e6beb7e8e33c2e2bc92a0f7a7d4 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Sun, 19 Jun 2022 19:44:00 -0400 Subject: [PATCH 1/4] swap remaining imaging extractor suffix assertions to warnings --- .../hdf5imagingextractor.py | 25 ++++----------- .../sbximagingextractor.py | 6 +++- .../tiffimagingextractor.py | 32 +++++++------------ 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py index 5113c928..c7ada60b 100644 --- a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py +++ b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py @@ -1,4 +1,5 @@ from pathlib import Path +from warnings import warn import numpy as np @@ -39,10 +40,8 @@ def __init__( self.filepath = Path(file_path) self._sampling_frequency = sampling_frequency self._mov_field = mov_field - assert self.filepath.suffix in [ - ".h5", - ".hdf5", - ], "'file_path' file is not an .hdf5 or .h5 file" + if self.filepath.suffix not in [".h5", ".hdf5"]: + warn("'file_path' file is not an .hdf5 or .h5 file") self._channel_names = channel_names self._file = h5py.File(file_path, "r") @@ -68,12 +67,7 @@ def __init__( else: self.metadata = metadata - ( - self._num_channels, - self._num_frames, - self._size_x, - self._size_y, - ) = get_video_shape(self._video) + (self._num_channels, self._num_frames, self._size_x, self._size_y,) = get_video_shape(self._video) if len(self._video.shape) == 3: # check if this converts to np.ndarray @@ -125,17 +119,10 @@ def get_num_channels(self): @staticmethod def write_imaging( - imaging: ImagingExtractor, - save_path, - overwrite: bool = False, - mov_field="mov", - **kwargs, + imaging: ImagingExtractor, save_path, overwrite: bool = False, mov_field="mov", **kwargs, ): save_path = Path(save_path) - assert save_path.suffix in [ - ".h5", - ".hdf5", - ], "'save_path' file is not an .hdf5 or .h5 file" + assert save_path.suffix in [".h5", ".hdf5",], "'save_path' file is not an .hdf5 or .h5 file" if save_path.is_file(): if not overwrite: diff --git a/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py b/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py index 8e647348..3fe79e1a 100644 --- a/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py +++ b/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py @@ -1,5 +1,6 @@ import os from pathlib import Path +from warnings import warn import numpy as np @@ -47,7 +48,10 @@ def _check_file_path(file_path): file_path = Path(file_path) assertion_msg = "for file_path arg, provide a path to one .sbx / .mat file" file_type = file_path.suffix - assert file_type in [".mat", ".sbx"], assertion_msg + if file_type not in [".mat", ".sbx"]: + warn( + "File suffix ({file_type}) is not one of .mat or .sbx - the SbxImagingExtractor may not be appropriate!" + ) if file_type == ".mat": mat_file_path = file_path sbx_file_path = file_path.with_suffix(".sbx") diff --git a/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py index 0eba49f1..ec8edaa5 100644 --- a/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py @@ -1,5 +1,5 @@ from pathlib import Path -import warnings +from warnings import warn import numpy as np from tqdm import tqdm @@ -31,17 +31,18 @@ class TiffImagingExtractor(ImagingExtractor): installation_mesg = "To use the TiffImagingExtractor install tifffile: \n\n pip install tifffile\n\n" def __init__( - self, - file_path: PathType, - sampling_frequency: FloatType, - channel_names: ArrayType = None, + self, file_path: PathType, sampling_frequency: FloatType, channel_names: ArrayType = None, ): assert HAVE_TIFF, self.installation_mesg ImagingExtractor.__init__(self) self.file_path = Path(file_path) self._sampling_frequency = sampling_frequency self._channel_names = channel_names - assert self.file_path.suffix in [".tiff", ".tif", ".TIFF", ".TIF"] + if self.file_path.suffix not in [".tiff", ".tif", ".TIFF", ".TIF"]: + warn( + "File suffix ({self.file_path.suffix}) is not one of .tiff, .tif, .TIFF, or .TIF! " + "The TiffImagingExtracto may not be appropriate." + ) with tifffile.TiffFile(self.file_path) as tif: self._num_channels = len(tif.series) @@ -50,16 +51,11 @@ def __init__( try: self._video = tifffile.memmap(self.file_path, mode="r") except ValueError: - warnings.warn("memmap of TIFF file could not be established. Reading entire matrix into memory.") + warn("memmap of TIFF file could not be established. Reading entire matrix into memory.") with tifffile.TiffFile(self.file_path) as tif: self._video = tif.asarray() - ( - self._num_channels, - self._num_frames, - self._size_x, - self._size_y, - ) = get_video_shape(self._video) + (self._num_channels, self._num_frames, self._size_x, self._size_y,) = get_video_shape(self._video) if len(self._video.shape) == 3: # check if this converts to np.ndarray @@ -114,12 +110,7 @@ def get_num_channels(self): @staticmethod def write_imaging(imaging, save_path, overwrite: bool = False, chunk_size=None, verbose=True): save_path = Path(save_path) - assert save_path.suffix in [ - ".tiff", - ".tif", - ".TIFF", - ".TIF", - ], "'save_path' file is not an .tiff file" + assert save_path.suffix in [".tiff", ".tif", ".TIFF", ".TIF",], "'save_path' file is not an .tiff file" if save_path.is_file(): if not overwrite: @@ -142,8 +133,7 @@ def write_imaging(imaging, save_path, overwrite: bool = False, chunk_size=None, with tifffile.TiffWriter(save_path) as tif: for i in chunks: video = imaging.get_video( - start_frame=i * chunk_size, - end_frame=min((i + 1) * chunk_size, num_frames), + start_frame=i * chunk_size, end_frame=min((i + 1) * chunk_size, num_frames), ) chunk_frames = np.squeeze(video) tif.save(chunk_frames, contiguous=True, metadata=None) From 344ffc2a91140e9826ecf063a6bc892fd0a0412e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Jun 2022 00:28:55 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../hdf5imagingextractor.py | 18 ++++++++++++--- .../tiffimagingextractor.py | 22 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py index c7ada60b..ab53628b 100644 --- a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py +++ b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py @@ -67,7 +67,12 @@ def __init__( else: self.metadata = metadata - (self._num_channels, self._num_frames, self._size_x, self._size_y,) = get_video_shape(self._video) + ( + self._num_channels, + self._num_frames, + self._size_x, + self._size_y, + ) = get_video_shape(self._video) if len(self._video.shape) == 3: # check if this converts to np.ndarray @@ -119,10 +124,17 @@ def get_num_channels(self): @staticmethod def write_imaging( - imaging: ImagingExtractor, save_path, overwrite: bool = False, mov_field="mov", **kwargs, + imaging: ImagingExtractor, + save_path, + overwrite: bool = False, + mov_field="mov", + **kwargs, ): save_path = Path(save_path) - assert save_path.suffix in [".h5", ".hdf5",], "'save_path' file is not an .hdf5 or .h5 file" + assert save_path.suffix in [ + ".h5", + ".hdf5", + ], "'save_path' file is not an .hdf5 or .h5 file" if save_path.is_file(): if not overwrite: diff --git a/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py index ec8edaa5..bb9e544f 100644 --- a/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py @@ -31,7 +31,10 @@ class TiffImagingExtractor(ImagingExtractor): installation_mesg = "To use the TiffImagingExtractor install tifffile: \n\n pip install tifffile\n\n" def __init__( - self, file_path: PathType, sampling_frequency: FloatType, channel_names: ArrayType = None, + self, + file_path: PathType, + sampling_frequency: FloatType, + channel_names: ArrayType = None, ): assert HAVE_TIFF, self.installation_mesg ImagingExtractor.__init__(self) @@ -55,7 +58,12 @@ def __init__( with tifffile.TiffFile(self.file_path) as tif: self._video = tif.asarray() - (self._num_channels, self._num_frames, self._size_x, self._size_y,) = get_video_shape(self._video) + ( + self._num_channels, + self._num_frames, + self._size_x, + self._size_y, + ) = get_video_shape(self._video) if len(self._video.shape) == 3: # check if this converts to np.ndarray @@ -110,7 +118,12 @@ def get_num_channels(self): @staticmethod def write_imaging(imaging, save_path, overwrite: bool = False, chunk_size=None, verbose=True): save_path = Path(save_path) - assert save_path.suffix in [".tiff", ".tif", ".TIFF", ".TIF",], "'save_path' file is not an .tiff file" + assert save_path.suffix in [ + ".tiff", + ".tif", + ".TIFF", + ".TIF", + ], "'save_path' file is not an .tiff file" if save_path.is_file(): if not overwrite: @@ -133,7 +146,8 @@ def write_imaging(imaging, save_path, overwrite: bool = False, chunk_size=None, with tifffile.TiffWriter(save_path) as tif: for i in chunks: video = imaging.get_video( - start_frame=i * chunk_size, end_frame=min((i + 1) * chunk_size, num_frames), + start_frame=i * chunk_size, + end_frame=min((i + 1) * chunk_size, num_frames), ) chunk_frames = np.squeeze(video) tif.save(chunk_frames, contiguous=True, metadata=None) From 9b67c15bebf6b86c3f7e41fcb9ee9c58940355b6 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Mon, 20 Jun 2022 10:01:36 -0400 Subject: [PATCH 3/4] fixed conflict --- .../extractors/tiffimagingextractors/tiffimagingextractor.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py index da6ed963..c4c5e60d 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -54,14 +54,10 @@ def __init__( try: self._video = tifffile.memmap(self.file_path, mode="r") except ValueError: -<<<<<<< HEAD:src/roiextractors/extractors/tiffimagingextractor/tiffimagingextractor.py - warn("memmap of TIFF file could not be established. Reading entire matrix into memory.") -======= warnings.warn( "memmap of TIFF file could not be established. Reading entire matrix into memory. " "Consider using the ScanImageTiffExtractor for lazy data access." ) ->>>>>>> master:src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py with tifffile.TiffFile(self.file_path) as tif: self._video = tif.asarray() From 6c44fd65b3649497fa1142c24544f66cc4959af8 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Mon, 20 Jun 2022 10:12:59 -0400 Subject: [PATCH 4/4] fix warnings --- .../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 c4c5e60d..8f9d5502 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py +++ b/src/roiextractors/extractors/tiffimagingextractors/tiffimagingextractor.py @@ -54,7 +54,7 @@ def __init__( try: self._video = tifffile.memmap(self.file_path, mode="r") except ValueError: - warnings.warn( + warn( "memmap of TIFF file could not be established. Reading entire matrix into memory. " "Consider using the ScanImageTiffExtractor for lazy data access." )