diff --git a/CHANGELOG.md b/CHANGELOG.md index 46a83438..21f9cb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added specific error message for single-frame scanimage data [PR #360](https://github.com/catalystneuro/roiextractors/pull/360) ### Improvements +* Removed unnecessary import checks for scipy, h5py, and zarr [PR #364](https://github.com/catalystneuro/roiextractors/pull/364) ### Testing diff --git a/src/roiextractors/extraction_tools.py b/src/roiextractors/extraction_tools.py index c7866f2d..e3f4b9ee 100644 --- a/src/roiextractors/extraction_tools.py +++ b/src/roiextractors/extraction_tools.py @@ -22,21 +22,8 @@ from packaging import version -try: - import h5py - - HAVE_H5 = True -except ImportError: - HAVE_H5 = False - - -try: - import zarr - - HAVE_ZARR = True -except ImportError: - HAVE_ZARR = False - +import h5py +import zarr ArrayType = ArrayLike PathType = Union[str, Path] @@ -485,12 +472,9 @@ def write_to_h5_dataset_format( Raises ------ - AssertionError - If h5py is not installed. AssertionError If neither 'save_path' nor 'file_handle' are given. """ - assert HAVE_H5, "To write to h5 you need to install h5py: pip install h5py" assert save_path is not None or file_handle is not None, "Provide 'save_path' or 'file handle'" if save_path is not None: diff --git a/src/roiextractors/extractors/caiman/caimansegmentationextractor.py b/src/roiextractors/extractors/caiman/caimansegmentationextractor.py index d88dabd4..387eac26 100644 --- a/src/roiextractors/extractors/caiman/caimansegmentationextractor.py +++ b/src/roiextractors/extractors/caiman/caimansegmentationextractor.py @@ -8,20 +8,9 @@ from pathlib import Path -try: - import h5py - - HAVE_H5PY = True -except ImportError: - HAVE_H5PY = False - -try: - from scipy.sparse import csc_matrix - - HAVE_SCIPY = True -except ImportError: - HAVE_SCIPY = False +import h5py +from scipy.sparse import csc_matrix import numpy as np from ...extraction_tools import PathType, get_package @@ -38,11 +27,8 @@ class CaimanSegmentationExtractor(SegmentationExtractor): """ extractor_name = "CaimanSegmentation" - installed = HAVE_H5PY and HAVE_SCIPY # check at class level if installed or not is_writable = True mode = "file" - # error message when not installed - installation_mesg = "To use the CaimanSegmentationExtractor install h5py and scipy: \n\n pip install scipy/h5py\n\n" def __init__(self, file_path: PathType): """Initialize a CaimanSegmentationExtractor instance. diff --git a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py index 5ce5dbf7..16e165c6 100644 --- a/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py +++ b/src/roiextractors/extractors/hdf5imagingextractor/hdf5imagingextractor.py @@ -18,22 +18,15 @@ from lazy_ops import DatasetView -try: - import h5py - - HAVE_H5 = True -except ImportError: - HAVE_H5 = False +import h5py class Hdf5ImagingExtractor(ImagingExtractor): """An imaging extractor for HDF5.""" extractor_name = "Hdf5Imaging" - installed = HAVE_H5 # check at class level if installed or not is_writable = True mode = "file" - installation_mesg = "To use the Hdf5 Extractor run:\n\n pip install h5py\n\n" # error message when not installed def __init__( self, diff --git a/src/roiextractors/extractors/nwbextractors/nwbextractors.py b/src/roiextractors/extractors/nwbextractors/nwbextractors.py index 61ff9f44..4f6ec2ca 100644 --- a/src/roiextractors/extractors/nwbextractors/nwbextractors.py +++ b/src/roiextractors/extractors/nwbextractors/nwbextractors.py @@ -14,13 +14,8 @@ import numpy as np from lazy_ops import DatasetView -try: - from pynwb import NWBHDF5IO - from pynwb.ophys import TwoPhotonSeries, OnePhotonSeries - - HAVE_NWB = True -except ImportError: - HAVE_NWB = False +from pynwb import NWBHDF5IO +from pynwb.ophys import TwoPhotonSeries, OnePhotonSeries from ...extraction_tools import ( PathType, FloatType, @@ -42,11 +37,6 @@ def temporary_deprecation_message(): ) -def check_nwb_install(): - """Check if pynwb is installed.""" - assert HAVE_NWB, "To use the Nwb extractors, install pynwb: \n\n pip install pynwb\n\n" - - class NwbImagingExtractor(ImagingExtractor): """An imaging extractor for NWB files. @@ -55,10 +45,8 @@ class NwbImagingExtractor(ImagingExtractor): """ extractor_name = "NwbImaging" - installed = HAVE_NWB # check at class level if installed or not is_writable = True mode = "file" - installation_mesg = "To use the Nwb Extractor run:\n\n pip install pynwb\n\n" # error message when not installed def __init__(self, file_path: PathType, optical_series_name: Optional[str] = "TwoPhotonSeries"): """Create ImagingExtractor object from NWB file. @@ -264,7 +252,6 @@ def __init__(self, file_path: PathType): file_path: PathType .nwb file location """ - check_nwb_install() super().__init__() file_path = Path(file_path) if not file_path.is_file(): diff --git a/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py b/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py index e013fb75..9b85dd37 100644 --- a/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py +++ b/src/roiextractors/extractors/sbximagingextractor/sbximagingextractor.py @@ -16,22 +16,15 @@ from ...extraction_tools import PathType, ArrayType, raise_multi_channel_or_depth_not_implemented, check_keys from ...imagingextractor import ImagingExtractor -try: - import scipy.io as spio - - HAVE_Scipy = True -except ImportError: - HAVE_Scipy = False +import scipy.io as spio class SbxImagingExtractor(ImagingExtractor): """Imaging extractor for the Scanbox image format.""" extractor_name = "SbxImaging" - installed = HAVE_Scipy # check at class level if installed or not is_writable = True mode = "folder" - installation_mesg = "To use the Sbx Extractor run:\n\n pip install scipy\n\n" # error message when not installed def __init__(self, file_path: PathType, sampling_frequency: Optional[float] = None): """Create a SbxImagingExtractor from .mat or .sbx files. diff --git a/src/roiextractors/extractors/schnitzerextractor/cnmfesegmentationextractor.py b/src/roiextractors/extractors/schnitzerextractor/cnmfesegmentationextractor.py index 4deb3cae..ae5fc9ba 100644 --- a/src/roiextractors/extractors/schnitzerextractor/cnmfesegmentationextractor.py +++ b/src/roiextractors/extractors/schnitzerextractor/cnmfesegmentationextractor.py @@ -8,22 +8,10 @@ from pathlib import Path -try: - import h5py - - HAVE_H5PY = True -except ImportError: - HAVE_H5PY = False - +import h5py import numpy as np from lazy_ops import DatasetView - -try: - from scipy.sparse import csc_matrix - - HAVE_SCIPY = True -except ImportError: - HAVE_SCIPY = False +from scipy.sparse import csc_matrix from ...extraction_tools import PathType from ...multisegmentationextractor import MultiSegmentationExtractor @@ -39,10 +27,8 @@ class CnmfeSegmentationExtractor(SegmentationExtractor): """ extractor_name = "CnmfeSegmentation" - installed = HAVE_H5PY # check at class level if installed or not is_writable = False mode = "file" - installation_mesg = "To use Cnmfe install h5py: \n\n pip install h5py \n\n" # error message when not installed def __init__(self, file_path: PathType): """Create a CnmfeSegmentationExtractor from a .mat file. @@ -161,7 +147,6 @@ def write_segmentation(segmentation_object: SegmentationExtractor, save_path: Pa AssertionError If save_path is not a *.mat file. """ - assert HAVE_SCIPY and HAVE_H5PY, "To use Cnmfe install scipy/h5py: \n\n pip install scipy/h5py \n\n" save_path = Path(save_path) assert save_path.suffix == ".mat", "'save_path' must be a *.mat file" if save_path.is_file(): diff --git a/src/roiextractors/extractors/schnitzerextractor/extractsegmentationextractor.py b/src/roiextractors/extractors/schnitzerextractor/extractsegmentationextractor.py index 3923df06..53e63f8c 100644 --- a/src/roiextractors/extractors/schnitzerextractor/extractsegmentationextractor.py +++ b/src/roiextractors/extractors/schnitzerextractor/extractsegmentationextractor.py @@ -18,13 +18,7 @@ from lazy_ops import DatasetView from packaging import version -try: - import h5py - - HAVE_H5PY = True -except ImportError: - HAVE_H5PY = False - +import h5py from ...extraction_tools import PathType, ArrayType from ...segmentationextractor import SegmentationExtractor @@ -39,8 +33,6 @@ class ExtractSegmentationExtractor(ABC): """Abstract class that defines which extractor class to use for a given file.""" extractor_name = "ExtractSegmentation" - installed = HAVE_H5PY # check at class level if installed or not - installation_mesg = "To use ExtractSegmentationExtractor install h5py: \n\n pip install h5py \n\n" # error message when not installed def __new__( cls, @@ -159,11 +151,6 @@ class NewExtractSegmentationExtractor( """ extractor_name = "NewExtractSegmentation" - installed = HAVE_H5PY # check at class level if installed or not - installation_mesg = ( - "To use NewExtractSegmentation install h5py: \n\n pip install h5py \n\n" - # error message when not installed - ) is_writable = False mode = "file" @@ -296,10 +283,8 @@ class LegacyExtractSegmentationExtractor(SegmentationExtractor): """ extractor_name = "LegacyExtractSegmentation" - installed = HAVE_H5PY # check at class level if installed or not is_writable = False mode = "file" - installation_mesg = "To use extract install h5py: \n\n pip install h5py \n\n" # error message when not installed def __init__( self,