|
4 | 4 | from typing import List, Optional, Union |
5 | 5 | from contextlib import redirect_stderr |
6 | 6 | from pathlib import Path |
| 7 | +import importlib.util |
7 | 8 |
|
8 | 9 | import numpy as np |
9 | 10 | import probeinterface |
|
13 | 14 | from spikeinterface.extractors.alfsortingextractor import ALFSortingSegment |
14 | 15 |
|
15 | 16 |
|
| 17 | +if importlib.util.find_spec("one") is not None and importlib.util.find_spec("one.api") is not None: |
| 18 | + HAVE_ONE = True |
| 19 | +else: |
| 20 | + HAVE_ONE = False |
| 21 | +if importlib.util.find_spec("brainbox") is not None and importlib.util.find_spec("brainbox.io.one") is not None: |
| 22 | + HAVE_BRAINBOX = True |
| 23 | +else: |
| 24 | + HAVE_BRAINBOX = False |
| 25 | + |
| 26 | + |
16 | 27 | class IblRecordingExtractor(BaseRecording): |
17 | 28 | """ |
18 | 29 | Stream IBL data as an extractor object. |
@@ -69,10 +80,9 @@ class IblRecordingExtractor(BaseRecording): |
69 | 80 |
|
70 | 81 | @staticmethod |
71 | 82 | def _get_default_one(cache_folder: Optional[Union[Path, str]] = None): |
72 | | - try: |
| 83 | + if HAVE_ONE and HAVE_BRAINBOX: |
73 | 84 | from one.api import ONE |
74 | | - from brainbox.io.one import EphysSessionLoader |
75 | | - except ImportError: |
| 85 | + else: |
76 | 86 | raise ImportError(IblRecordingExtractor.installation_mesg) |
77 | 87 | one = ONE( |
78 | 88 | base_url="https://openalyx.internationalbrainlab.org", |
@@ -113,10 +123,9 @@ def get_stream_names(eid: str, cache_folder: Optional[Union[Path, str]] = None, |
113 | 123 | stream_names : list of str |
114 | 124 | List of stream names as expected by the `stream_name` argument for the class initialization. |
115 | 125 | """ |
116 | | - try: |
117 | | - from one.api import ONE |
| 126 | + if HAVE_ONE and HAVE_BRAINBOX: |
118 | 127 | from brainbox.io.one import EphysSessionLoader |
119 | | - except ImportError: |
| 128 | + else: |
120 | 129 | raise ImportError(IblRecordingExtractor.installation_mesg) |
121 | 130 |
|
122 | 131 | cache_folder = Path(cache_folder) if cache_folder is not None else cache_folder |
@@ -144,9 +153,9 @@ def __init__( |
144 | 153 | one: "one.api.OneAlyx" = None, |
145 | 154 | stream_type: str | None = None, |
146 | 155 | ): |
147 | | - try: |
| 156 | + if HAVE_BRAINBOX: |
148 | 157 | from brainbox.io.one import SpikeSortingLoader |
149 | | - except ImportError: |
| 158 | + else: |
150 | 159 | raise ImportError(self.installation_mesg) |
151 | 160 |
|
152 | 161 | from neo.rawio.spikeglxrawio import read_meta_file, extract_stream_info |
@@ -320,15 +329,15 @@ class IblSortingExtractor(BaseSorting): |
320 | 329 | def __init__( |
321 | 330 | self, pid: str, good_clusters_only: bool = False, load_unit_properties: bool = True, one=None, **kwargs |
322 | 331 | ): |
323 | | - try: |
| 332 | + if HAVE_ONE and HAVE_BRAINBOX: |
324 | 333 | from one.api import ONE |
325 | 334 | from brainbox.io.one import SpikeSortingLoader |
326 | 335 |
|
327 | 336 | if isinstance(one, dict): |
328 | 337 | one = ONE(**one) |
329 | 338 | elif one is None: |
330 | 339 | one = IblRecordingExtractor._get_default_one() |
331 | | - except ImportError: |
| 340 | + else: |
332 | 341 | raise ImportError(self.installation_mesg) |
333 | 342 | self.ssl = SpikeSortingLoader(one=one, pid=pid) |
334 | 343 | sr = self.ssl.raw_electrophysiology(band="ap", stream=True) |
|
0 commit comments