-
Notifications
You must be signed in to change notification settings - Fork 0
/
stio.py
117 lines (107 loc) · 4.51 KB
/
stio.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
from neo.io import AxonIO
import numpy as np
from .classes import AbfFile
#from .classes import MiniFile
from .classes import AnalogData
def read_neo(path):
# Read abf file with neo
reader = AxonIO(filename=path)
block = reader.read()
# Extract analog signals
if len(block[0].segments[0].analogsignals) == 1:
channel1 = np.empty(block[0].segments[0].analogsignals[0].T.shape)
channel1_units = block[0].segments[0].analogsignals[0].units
channel2 = None
channel2_units = None
t = block[0].segments[0].analogsignals[0].times.magnitude
for idx, seg in enumerate(block[0].segments):
channel1[idx] = seg.analogsignals[0].T
#
# return {'file_name': block.file_origin,
# 'recording_date': block.rec_datetime,
# 'time': t,
# 'channel1': channel1,
elif len(block[0].segments[0].analogsignals) == 2:
channel1 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[0].size))
channel1_units = block[0].segments[0].analogsignals[0].units
channel1_sampling = block[0].segments[0].analogsignals[0].sampling_rate
channel2 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[1].size))
channel2_units = block[0].segments[0].analogsignals[1].units
channel2_sampling = block[0].segments[0].analogsignals[1].sampling_rate
t = block[0].segments[0].analogsignals[0].times.magnitude
for idx, seg in enumerate(block[0].segments):
channel1[idx] = seg.analogsignals[0].T
channel2[idx] = seg.analogsignals[1].T
#channel1[idx] = seg.analogsignals[0]
#channel2[idx] = seg.analogsignals[1]
channel_1 = AnalogData(
signal=channel1,
time=t,
units=channel1_units,
sampling_rate=channel1_sampling
)
channel_2 = AnalogData(
signal=channel2,
time=t,
units=channel2_units,
sampling_rate=channel2_sampling)
else:
raise Exception('File {0} either has too many channels, or no channels.'.format(path))
return AbfFile(
[channel_1, channel_2],
file_name=block[0].file_origin,
date=block[0].rec_datetime)
# def read_mini(path):
#
# # Read abf file with neo
# reader = AxonIO(filename=path)
# block = reader.read()
#
# # Extract analog signals
# if len(block[0].segments[0].analogsignals) == 1:
# channel1 = np.empty(block[0].segments[0].analogsignals[0].T.shape)
# channel1_units = block[0].segments[0].analogsignals[0].units
# channel2 = None
# channel2_units = None
# t = block[0].segments[0].analogsignals[0].times.magnitude
# for idx, seg in enumerate(block[0].segments):
# channel1[idx] = seg.analogsignals[0].T
# #
# # return {'file_name': block.file_origin,
# # 'recording_date': block.rec_datetime,
# # 'time': t,
# # 'channel1': channel1,
#
# elif len(block[0].segments[0].analogsignals) == 2:
#
# channel1 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[0].size))
# channel1_units = block[0].segments[0].analogsignals[0].units
# channel1_sampling = block[0].segments[0].analogsignals[0].sampling_rate
# channel2 = np.empty((len(block[0].segments), block[0].segments[0].analogsignals[1].size))
# channel2_units = block[0].segments[0].analogsignals[1].units
# channel2_sampling = block[0].segments[0].analogsignals[1].sampling_rate
# t = block[0].segments[0].analogsignals[0].times.magnitude
# for idx, seg in enumerate(block[0].segments):
# channel1[idx] = seg.analogsignals[0].T
# channel2[idx] = seg.analogsignals[1].T
# #channel1[idx] = seg.analogsignals[0]
# #channel2[idx] = seg.analogsignals[1]
# channel_1 = AnalogData(
# signal=channel1,
# time=t,
# units=channel1_units,
# sampling_rate=channel1_sampling
# )
# channel_2 = AnalogData(
# signal=channel2,
# time=t,
# units=channel2_units,
# sampling_rate=channel2_sampling)
#
# else:
# raise Exception('File {0} either has too many channels, or no channels.'.format(path))
#
# return MiniFile(
# [channel_1, channel_2],
# file_name=block[0].file_origin,
# date=block[0].rec_datetime)