3939from ..io .proxyobjects import BaseProxy
4040from ..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
5043datetime_types = (date , time , datetime )
5144
@@ -121,16 +114,17 @@ def dt_from_nix(nixdt, annotype):
121114
122115
123116def 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