Skip to content

Commit a29d504

Browse files
committed
Remove HAVE_XXXX from neo.io
1 parent fb7a955 commit a29d504

File tree

9 files changed

+67
-131
lines changed

9 files changed

+67
-131
lines changed

neo/io/igorproio.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
from neo.io.baseio import BaseIO
1818
from neo.core import Block, Segment, AnalogSignal
1919

20-
try:
21-
import igor.binarywave as bw
22-
import igor.packed as pxp
23-
from igor.record.wave import WaveRecord
24-
HAVE_IGOR = True
25-
except ImportError:
26-
HAVE_IGOR = False
2720

2821

2922
class IgorIO(BaseIO):
@@ -83,6 +76,9 @@ def read_block(self, lazy=False):
8376
return block
8477

8578
def read_segment(self, lazy=False):
79+
import igor.packed as pxp
80+
from igor.record.wave import WaveRecord
81+
8682
assert not lazy, 'This IO does not support lazy mode'
8783
segment = Segment(file_origin=str(self.filename))
8884

@@ -104,11 +100,11 @@ def callback(dirpath, key, value):
104100
return segment
105101

106102
def read_analogsignal(self, path=None, lazy=False):
103+
import igor.binarywave as bw
104+
import igor.packed as pxp
105+
107106
assert not lazy, 'This IO does not support lazy mode'
108107

109-
if not HAVE_IGOR:
110-
raise Exception("`igor` package not installed. "
111-
"Try `pip install igor`")
112108
if self.extension == 'ibw':
113109
data = bw.load(str(self.filename))
114110
version = data['version']

neo/io/klustakwikio.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121
# note neo.core need only numpy and quantitie
2222
import numpy as np
2323

24-
try:
25-
import matplotlib.mlab as mlab
26-
except ImportError as err:
27-
HAVE_MLAB = False
28-
MLAB_ERR = err
29-
else:
30-
HAVE_MLAB = True
31-
MLAB_ERR = None
3224

3325
# I need to subclass BaseIO
3426
from neo.io.baseio import BaseIO
@@ -105,8 +97,6 @@ def __init__(self, filename, sampling_rate=30000.):
10597
sampling_rate : in Hz, necessary because the KlustaKwik files
10698
stores data in samples.
10799
"""
108-
if not HAVE_MLAB:
109-
raise MLAB_ERR
110100
BaseIO.__init__(self)
111101
# self.filename = os.path.normpath(filename)
112102
self.filename, self.basename = os.path.split(os.path.abspath(filename))

neo/io/kwikio.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@
1515
import quantities as pq
1616
import os
1717

18-
try:
19-
from scipy import stats
20-
except ImportError as err:
21-
HAVE_SCIPY = False
22-
SCIPY_ERR = err
23-
else:
24-
HAVE_SCIPY = True
25-
SCIPY_ERR = None
26-
27-
try:
28-
from klusta import kwik
29-
except ImportError as err:
30-
HAVE_KWIK = False
31-
KWIK_ERR = err
32-
else:
33-
HAVE_KWIK = True
34-
KWIK_ERR = None
3518

3619
# I need to subclass BaseIO
3720
from neo.io.baseio import BaseIO
@@ -77,8 +60,8 @@ def __init__(self, filename):
7760
Arguments:
7861
filename : the filename
7962
"""
80-
if not HAVE_KWIK:
81-
raise KWIK_ERR
63+
from klusta import kwik
64+
8265
BaseIO.__init__(self)
8366
self.filename = os.path.abspath(filename)
8467
model = kwik.KwikModel(self.filename) # TODO this group is loaded twice

neo/io/neomatlabio.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,8 @@
1818
import numpy as np
1919
import quantities as pq
2020

21-
# check scipy
22-
try:
23-
from packaging.version import Version
24-
import scipy.io
25-
import scipy.version
26-
except ImportError as err:
27-
HAVE_SCIPY = False
28-
SCIPY_ERR = err
29-
else:
30-
if Version(scipy.version.version) < Version('0.12.0'):
31-
HAVE_SCIPY = False
32-
SCIPY_ERR = ImportError("your scipy version is too old to support "
33-
+ "MatlabIO, you need at least 0.12.0. "
34-
+ "You have %s" % scipy.version.version)
35-
else:
36-
HAVE_SCIPY = True
37-
SCIPY_ERR = None
21+
from packaging.version import Version
22+
3823

3924
from neo.io.baseio import BaseIO
4025
from neo.core import (Block, Segment, AnalogSignal, IrregularlySampledSignal,
@@ -212,8 +197,13 @@ def __init__(self, filename=None):
212197
Arguments:
213198
filename : the filename to read
214199
"""
215-
if not HAVE_SCIPY:
216-
raise SCIPY_ERR
200+
import scipy
201+
202+
if Version(scipy.version.version) < Version('0.12.0'):
203+
raise ImportError("your scipy version is too old to support "
204+
+ "MatlabIO, you need at least 0.12.0. "
205+
+ "You have %s" % scipy.version.version)
206+
217207
BaseIO.__init__(self)
218208
self.filename = filename
219209

@@ -222,6 +212,7 @@ def read_block(self, lazy=False):
222212
Arguments:
223213
224214
"""
215+
import scipy.io
225216
assert not lazy, 'Do not support lazy'
226217

227218
d = scipy.io.loadmat(self.filename, struct_as_record=False,
@@ -241,7 +232,7 @@ def write_block(self, bl, **kargs):
241232
Arguments:
242233
bl: the block to b saved
243234
"""
244-
235+
import scipy.io
245236
bl_struct = self.create_struct_from_obj(bl)
246237

247238
for seg in bl.segments:

neo/io/nixio.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939
from ..io.proxyobjects import BaseProxy
4040
from ..version import version as neover
4141

42-
try:
43-
import nixio as nix
44-
45-
HAVE_NIX = True
46-
except ImportError:
47-
HAVE_NIX = False
48-
4942

5043
datetime_types = (date, time, datetime)
5144

@@ -121,16 +114,17 @@ def dt_from_nix(nixdt, annotype):
121114

122115

123116
def check_nix_version():
124-
if not HAVE_NIX:
117+
try:
118+
import nixio
119+
except ImportError:
125120
raise Exception(
126121
"Failed to import NIX. "
127122
"The NixIO requires the Python package for NIX "
128123
"(nixio on PyPi). Try `pip install nixio`."
129-
130124
)
131125

132126
# nixio version numbers have a 'v' prefix which breaks the comparison
133-
nixverstr = nix.__version__.lstrip("v")
127+
nixverstr = nixio.__version__.lstrip("v")
134128
try:
135129
nixver = Version(nixverstr)
136130
except ValueError:
@@ -174,25 +168,27 @@ def __init__(self, filename, mode="rw"):
174168
:param filename: Full path to the file
175169
"""
176170
check_nix_version()
171+
import nixio
172+
177173
BaseIO.__init__(self, filename)
178174
self.filename = str(filename)
179175
if mode == "ro":
180-
filemode = nix.FileMode.ReadOnly
176+
filemode = nixio.FileMode.ReadOnly
181177
elif mode == "rw":
182-
filemode = nix.FileMode.ReadWrite
178+
filemode = nixio.FileMode.ReadWrite
183179
elif mode == "ow":
184-
filemode = nix.FileMode.Overwrite
180+
filemode = nixio.FileMode.Overwrite
185181
else:
186182
raise ValueError(f"Invalid mode specified '{mode}'. "
187183
"Valid modes: 'ro' (ReadOnly)', 'rw' (ReadWrite),"
188184
" 'ow' (Overwrite).")
189-
self.nix_file = nix.File.open(self.filename, filemode)
185+
self.nix_file = nixio.File.open(self.filename, filemode)
190186

191-
if self.nix_file.mode == nix.FileMode.ReadOnly:
187+
if self.nix_file.mode == nixio.FileMode.ReadOnly:
192188
self._file_version = '0.5.2'
193189
if "neo" in self.nix_file.sections:
194190
self._file_version = self.nix_file.sections["neo"]["version"]
195-
elif self.nix_file.mode == nix.FileMode.ReadWrite:
191+
elif self.nix_file.mode == nixio.FileMode.ReadWrite:
196192
if "neo" in self.nix_file.sections:
197193
self._file_version = self.nix_file.sections["neo"]["version"]
198194
else:
@@ -1154,6 +1150,8 @@ def _write_spiketrain(self, spiketrain, nixblock, nixgroup):
11541150
:param nixblock: NIX Block where the MultiTag will be created
11551151
:param nixgroup: NIX Group where the MultiTag will be attached
11561152
"""
1153+
import nixio
1154+
11571155
if "nix_name" in spiketrain.annotations:
11581156
nix_name = spiketrain.annotations["nix_name"]
11591157
else:
@@ -1208,7 +1206,7 @@ def _write_spiketrain(self, spiketrain, nixblock, nixgroup):
12081206
data=wfdata)
12091207
wfda.unit = wfunits
12101208
wfda.metadata = nixmt.metadata.create_section(wfda.name, "neo.waveforms.metadata")
1211-
nixmt.create_feature(wfda, nix.LinkType.Indexed)
1209+
nixmt.create_feature(wfda, nixio.LinkType.Indexed)
12121210
# TODO: Move time dimension first for PR #457
12131211
# https://github.com/NeuralEnsemble/python-neo/pull/457
12141212
wfda.append_set_dimension()
@@ -1234,6 +1232,7 @@ def _write_property(self, section, name, v):
12341232
:param v: The value to write
12351233
:return: The newly created property
12361234
"""
1235+
import nixio
12371236

12381237
if isinstance(v, datetime_types):
12391238
value, annotype = dt_to_nix(v)
@@ -1243,7 +1242,7 @@ def _write_property(self, section, name, v):
12431242
if len(v):
12441243
section.create_property(name, v)
12451244
else:
1246-
section.create_property(name, nix.DataType.String)
1245+
section.create_property(name, nixio.DataType.String)
12471246
elif isinstance(v, bytes):
12481247
section.create_property(name, v.decode())
12491248
elif isinstance(v, Iterable):
@@ -1258,7 +1257,7 @@ def _write_property(self, section, name, v):
12581257
# NIX supports empty properties but dtype must be specified
12591258
# Defaulting to String and using definition to signify empty
12601259
# iterable as opposed to empty string
1261-
values = nix.DataType.String
1260+
values = nixio.DataType.String
12621261
definition = EMPTYANNOTATION
12631262
else:
12641263
for item in v:
@@ -1303,6 +1302,8 @@ def _nix_attr_to_neo(nix_obj):
13031302
Metadata: For properties that specify a 'unit', a Quantity object is
13041303
created.
13051304
"""
1305+
import nixio
1306+
13061307
neo_attrs = dict()
13071308
neo_attrs["nix_name"] = nix_obj.name
13081309
neo_attrs["description"] = stringify(nix_obj.definition)
@@ -1312,7 +1313,7 @@ def _nix_attr_to_neo(nix_obj):
13121313
if not len(values):
13131314
if prop.definition == EMPTYANNOTATION:
13141315
values = list()
1315-
elif prop.data_type == nix.DataType.String:
1316+
elif prop.data_type == nixio.DataType.String:
13161317
values = ""
13171318
elif len(values) == 1:
13181319
values = values[0]

0 commit comments

Comments
 (0)