Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary import checks for scipy, h5py, and zarr #364

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 2 additions & 18 deletions src/roiextractors/extraction_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 2 additions & 16 deletions src/roiextractors/extractors/caiman/caimansegmentationextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 2 additions & 15 deletions src/roiextractors/extractors/nwbextractors/nwbextractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down
Loading