forked from NeuralEnsemble/python-neo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
501 lines (368 loc) · 12.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
:mod:`neo.io` provides classes for reading and/or writing
electrophysiological data files.
Note that if the package dependency is not satisfied for one io, it does not
raise an error but a warning.
:attr:`neo.io.iolist` provides a list of successfully imported io classes.
Functions:
.. autofunction:: neo.io.get_io
.. autofunction:: neo.io.list_candidate_ios
Classes:
* :attr:`AlphaOmegaIO`
* :attr:`AsciiImageIO`
* :attr:`AsciiSignalIO`
* :attr:`AsciiSpikeTrainIO`
* :attr:`AxographIO`
* :attr:`AxonaIO`
* :attr:`AxonIO`
* :attr:`BCI2000IO`
* :attr:`BiocamIO`
* :attr:`BlackrockIO`
* :attr:`BlkIO`
* :attr:`BrainVisionIO`
* :attr:`BrainwareDamIO`
* :attr:`BrainwareF32IO`
* :attr:`BrainwareSrcIO`
* :attr:`CedIO`
* :attr:`EDFIO`
* :attr:`ElanIO`
* :attr:`IgorIO`
* :attr:`IntanIO`
* :attr:`MEArecIO`
* :attr:`KlustaKwikIO`
* :attr:`KwikIO`
* :attr:`MaxwellIO`
* :attr:`MedIO`
* :attr:`MicromedIO`
* :attr:`NeoMatlabIO`
* :attr:`NestIO`
* :attr:`NeuralynxIO`
* :attr:`NeuroExplorerIO`
* :attr:`NeuroScopeIO`
* :attr:`NeuroshareIO`
* :attr:`NixIO`
* :attr:`NWBIO`
* :attr:`OpenEphysIO`
* :attr:`OpenEphysBinaryIO`
* :attr:`PhyIO`
* :attr:`PickleIO`
* :attr:`PlexonIO`
* :attr:`Plexon2IO`
* :attr:`RawBinarySignalIO`
* :attr:`RawMCSIO`
* :attr:`Spike2IO`
* :attr:`SpikeGadgetsIO`
* :attr:`SpikeGLXIO`
* :attr:`StimfitIO`
* :attr:`TdtIO`
* :attr:`TiffIO`
* :attr:`WinEdrIO`
* :attr:`WinWcpIO`
.. autoclass:: neo.io.AlphaOmegaIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AsciiImageIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AsciiSignalIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AsciiSpikeTrainIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AxographIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AxonaIO
.. autoattribute:: extensions
.. autoclass:: neo.io.AxonIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BCI2000IO
.. autoattribute:: extensions
.. autoclass:: neo.io.BiocamIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BlackrockIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BlkIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BrainVisionIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BrainwareDamIO
.. autoattribute:: extensions
.. autoclass:: neo.io.BrainwareF32IO
.. autoattribute:: extensions
.. autoclass:: neo.io.BrainwareSrcIO
.. autoattribute:: extensions
.. autoclass:: neo.io.CedIO
.. autoattribute:: extensions
.. autoclass:: neo.io.EDFIO
.. autoattribute:: extensions
.. autoclass:: neo.io.ElanIO
.. autoattribute:: extensions
.. .. autoclass:: neo.io.ElphyIO
.. autoattribute:: extensions
.. autoclass:: neo.io.IgorIO
.. autoattribute:: extensions
.. autoclass:: neo.io.IntanIO
.. autoattribute:: extensions
.. autoclass:: neo.io.KlustaKwikIO
.. autoattribute:: extensions
.. autoclass:: neo.io.KwikIO
.. autoattribute:: extensions
.. autoclass:: neo.io.MEArecIO
.. autoattribute:: extensions
.. autoclass:: neo.io.MaxwellIO
.. autoattribute:: extensions
.. autoclass:: neo.io.MedIO
.. autoattribute:: extensions
.. autoclass:: neo.io.MicromedIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NeoMatlabIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NestIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NeuralynxIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NeuroExplorerIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NeuroScopeIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NeuroshareIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NixIO
.. autoattribute:: extensions
.. autoclass:: neo.io.NWBIO
.. autoattribute:: extensions
.. autoclass:: neo.io.OpenEphysIO
.. autoattribute:: extensions
.. autoclass:: neo.io.OpenEphysBinaryIO
.. autoattribute:: extensions
.. autoclass:: neo.io.PhyIO
.. autoattribute:: extensions
.. autoclass:: neo.io.PickleIO
.. autoattribute:: extensions
.. autoclass:: neo.io.PlexonIO
.. autoattribute:: extensions
.. autoclass:: neo.io.Plexon2IO
.. autoattribute:: extensions
.. autoclass:: neo.io.RawBinarySignalIO
.. autoattribute:: extensions
.. autoclass:: neo.io.RawMCSIO
.. autoattribute:: extensions
.. autoclass:: Spike2IO
.. autoattribute:: extensions
.. autoclass:: SpikeGadgetsIO
.. autoattribute:: extensions
.. autoclass:: SpikeGLXIO
.. autoattribute:: extensions
.. autoclass:: neo.io.StimfitIO
.. autoattribute:: extensions
.. autoclass:: neo.io.TdtIO
.. autoattribute:: extensions
.. autoclass:: neo.io.TiffIO
.. autoattribute:: extensions
.. autoclass:: neo.io.WinEdrIO
.. autoattribute:: extensions
.. autoclass:: neo.io.WinWcpIO
.. autoattribute:: extensions
"""
import pathlib
from collections import Counter
# try to import the neuroshare library.
# if it is present, use the neuroshareapiio to load neuroshare files
# if it is not present, use the neurosharectypesio to load files
try:
import neuroshare as ns
except ImportError as err:
from neo.io.neurosharectypesio import NeurosharectypesIO as NeuroshareIO
# print("\n neuroshare library not found, loading data with ctypes" )
# print("\n to use the API be sure to install the library found at:")
# print("\n www.http://pythonhosted.org/neuroshare/")
else:
from neo.io.neuroshareapiio import NeuroshareapiIO as NeuroshareIO
# print("neuroshare library successfully imported")
# print("\n loading with API...")
from neo.io.alphaomegaio import AlphaOmegaIO
from neo.io.asciiimageio import AsciiImageIO
from neo.io.asciisignalio import AsciiSignalIO
from neo.io.asciispiketrainio import AsciiSpikeTrainIO
from neo.io.axographio import AxographIO
from neo.io.axonaio import AxonaIO
from neo.io.axonio import AxonIO
from neo.io.biocamio import BiocamIO
from neo.io.blackrockio import BlackrockIO
from neo.io.blkio import BlkIO
from neo.io.bci2000io import BCI2000IO
from neo.io.brainvisionio import BrainVisionIO
from neo.io.brainwaredamio import BrainwareDamIO
from neo.io.brainwaref32io import BrainwareF32IO
from neo.io.brainwaresrcio import BrainwareSrcIO
from neo.io.cedio import CedIO
from neo.io.edfio import EDFIO
from neo.io.elanio import ElanIO
from neo.io.elphyio import ElphyIO
from neo.io.exampleio import ExampleIO
from neo.io.igorproio import IgorIO
from neo.io.intanio import IntanIO
from neo.io.klustakwikio import KlustaKwikIO
from neo.io.kwikio import KwikIO
from neo.io.mearecio import MEArecIO
from neo.io.maxwellio import MaxwellIO
from neo.io.medio import MedIO
from neo.io.micromedio import MicromedIO
from neo.io.neomatlabio import NeoMatlabIO
from neo.io.nestio import NestIO
from neo.io.neuralynxio import NeuralynxIO
from neo.io.neuroexplorerio import NeuroExplorerIO
from neo.io.neuroscopeio import NeuroScopeIO
from neo.io.nixio import NixIO
from neo.io.nixio_fr import NixIO as NixIOFr
from neo.io.nwbio import NWBIO
from neo.io.openephysio import OpenEphysIO
from neo.io.openephysbinaryio import OpenEphysBinaryIO
from neo.io.phyio import PhyIO
from neo.io.pickleio import PickleIO
from neo.io.plexonio import PlexonIO
from neo.io.plexon2io import Plexon2IO
from neo.io.rawbinarysignalio import RawBinarySignalIO
from neo.io.rawmcsio import RawMCSIO
from neo.io.spike2io import Spike2IO
from neo.io.spikegadgetsio import SpikeGadgetsIO
from neo.io.spikeglxio import SpikeGLXIO
from neo.io.stimfitio import StimfitIO
from neo.io.tdtio import TdtIO
from neo.io.tiffio import TiffIO
from neo.io.winedrio import WinEdrIO
from neo.io.winwcpio import WinWcpIO
iolist = [
AlphaOmegaIO,
AsciiImageIO,
AsciiSignalIO,
AsciiSpikeTrainIO,
AxographIO,
AxonaIO,
AxonIO,
BCI2000IO,
BiocamIO,
BlackrockIO,
BlkIO,
BrainVisionIO,
BrainwareDamIO,
BrainwareF32IO,
BrainwareSrcIO,
CedIO,
EDFIO,
ElanIO,
ElphyIO,
ExampleIO,
IgorIO,
IntanIO,
KlustaKwikIO,
KwikIO,
MEArecIO,
MaxwellIO,
MedIO,
MicromedIO,
NixIO,
NixIOFr,
NeoMatlabIO,
NestIO,
NeuralynxIO,
NeuroExplorerIO,
NeuroScopeIO,
NeuroshareIO,
NWBIO,
OpenEphysIO,
OpenEphysBinaryIO,
PhyIO,
PickleIO,
PlexonIO,
Plexon2IO,
RawBinarySignalIO,
RawMCSIO,
Spike2IO,
SpikeGadgetsIO,
SpikeGLXIO,
StimfitIO,
TdtIO,
TiffIO,
WinEdrIO,
WinWcpIO
]
# for each supported extension list the ios supporting it
io_by_extension = {}
for current_io in iolist: # do not use `io` as variable name here as this overwrites the module io
for extension in current_io.extensions:
extension = extension.lower()
# extension handling should not be case sensitive
io_by_extension.setdefault(extension, []).append(current_io)
def get_io(file_or_folder, *args, **kwargs):
"""
Return a Neo IO instance, guessing the type based on the filename suffix.
"""
ios = list_candidate_ios(file_or_folder)
for io in ios:
try:
return io(file_or_folder, *args, **kwargs)
except:
continue
raise IOError(f"Could not identify IO for {file_or_folder}")
def list_candidate_ios(file_or_folder, ignore_patterns=['*.ini', 'README.txt', 'README.md']):
"""
Identify neo IO that can potentially load data in the file or folder
Parameters
----------
file_or_folder (str, pathlib.Path)
Path to the file or folder to load
ignore_patterns (list)
List of patterns to ignore when scanning for known formats. See pathlib.PurePath.match().
Default: ['ini']
Returns
-------
list
List of neo io classes that are associated with the file extensions detected
"""
file_or_folder = pathlib.Path(file_or_folder)
if file_or_folder.is_file():
suffix = file_or_folder.suffix[1:].lower()
if suffix not in io_by_extension:
raise ValueError(f'{suffix} is not a supported format of any IO.')
return io_by_extension[suffix]
elif file_or_folder.is_dir():
# scan files in folder to determine io type
filenames = [f for f in file_or_folder.glob('*') if f.is_file()]
# keep only relevant filenames
filenames = [f for f in filenames if f.suffix and not any([f.match(p) for p in ignore_patterns])]
# if no files are found in the folder, check subfolders
# this is necessary for nested-folder based formats like spikeglx
if not filenames:
filenames = [f for f in file_or_folder.glob('**/*') if f.is_file()]
# keep only relevant filenames
filenames = [f for f in filenames if f.suffix and not any([f.match(p) for p in ignore_patterns])]
# if only file prefix was provided, e.g /mydatafolder/session1-
# to select all files sharing the `session1-` prefix
elif file_or_folder.parent.exists():
filenames = list(file_or_folder.parent.glob(file_or_folder.name + '*'))
# if filenames empty and suffix is provided then non-existent file
# may be written in current dir. So run check for io
if len(filenames)==0 and file_or_folder.suffix:
suffix = file_or_folder.suffix[1:].lower()
if suffix not in io_by_extension:
raise ValueError(f'{suffix} is not a supported format of any IO.')
return io_by_extension[suffix]
# If non-existent file in non-existent dir is given check if this
# structure could be created with an io writing the file
elif file_or_folder.suffix:
suffix = file_or_folder.suffix[1:].lower()
if suffix not in io_by_extension:
raise ValueError(f'{suffix} is not a supported format of any IO.')
return io_by_extension[suffix]
else:
raise ValueError(f'{file_or_folder} does not contain data files of a supported format')
# find the io that fits the best with the files contained in the folder
potential_ios = []
for filename in filenames:
for suffix in filename.suffixes:
suffix = suffix[1:].lower()
if suffix in io_by_extension:
potential_ios.extend(io_by_extension[suffix])
if not potential_ios:
raise ValueError(f'Could not determine IO to load {file_or_folder}')
# return ios ordered by number of files supported
counter = Counter(potential_ios).most_common()
return [io for io, count in counter]