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 | class epochTable():
"""Load epoch/stimulation from abf file using 'sweepEpochs'.
Values in epoch table are per sweep!
sweepNumber index type startPoint stopPoint startSec stopSec durSec level pulseWidth pulsePeriod digitalState
0 13 0 Step 0 25 0.0000 0.0025 0.0025 -0.1 0 0 0
1 13 1 Step 25 275 0.0025 0.0275 0.0250 -0.1 0 0 0
2 13 2 Step 275 1275 0.0275 0.1275 0.1000 0.7 0 0 0
3 13 3 Step 1275 1525 0.1275 0.1525 0.0250 -0.1 0 0 0
4 13 4 Step 1525 1600 0.1525 0.1600 0.0075 -0.1 0 0 0
"""
def __init__(self, abf: pyabf.ABF = None):
self._epochList = []
if abf is not None:
self._builFromAbf(abf)
def __str__(self):
return self.getEpochList(asDataFrame=True).to_string()
def addEpoch(self, sweepNumber, startSec, stopSec, dataPointsPerMs : int, level, type : str = 'Step'):
newEpochIdx = self.numEpochs()
p1 = (startSec * 1000) * dataPointsPerMs
p1 = int(p1)
p2 = (stopSec * 1000) * dataPointsPerMs
p2 = int(p2)
epochDict = {
"sweepNumber": sweepNumber,
"index": newEpochIdx,
"type": type,
"startPoint": p1, # point the epoch starts
"stopPoint": p2, # point the epoch ends
"startSec": startSec,
"stopSec": stopSec,
"durSec": stopSec - startSec,
"level": level,
# "pulseWidth": pulseWidth,
# "pulsePeriod": pulsePeriod,
# "digitalState": digitalState, # list of 0/1 for 8x digital states
}
self._epochList.append(epochDict)
def getSweepEpoch(self, sweep):
return self._epochList[sweep]
def _builFromAbf(self, abf: pyabf.ABF):
# dataPointsPerMs = abf.dataPointsPerMs
"""To convert point to seconds"""
try:
_tmp = abf.sweepEpochs.p1s
except AttributeError as e:
logger.error(e)
return
# abf.sweepEpoch is
# pyabf.waveform.EpochSweepWaveform
# logger.info(f' abf.sweepEpochs is {type(abf.sweepEpochs)}')
# print(abf.sweepEpochs)
# print(pyabf.waveform.epochTable())
# sweepEpochs is type "pyabf.waveform.EpochSweepWaveform"
for epochIdx, p1 in enumerate(abf.sweepEpochs.p1s):
p2 = abf.sweepEpochs.p2s[epochIdx] # stop point of each pulse
epochLevel = abf.sweepEpochs.levels[epochIdx]
epochType = abf.sweepEpochs.types[epochIdx]
pulseWidth = abf.sweepEpochs.pulseWidths[epochIdx]
pulsePeriod = abf.sweepEpochs.pulsePeriods[epochIdx]
digitalState = abf.sweepEpochs.digitalStates[epochIdx] # 20240117 owl-analysis
##print(f"epoch index {epochIdx}: at point {p1} there is a {epochType} to level {epochLevel}")
try:
p1_sec = p1 / abf.dataPointsPerMs / 1000
p2_sec = p2 / abf.dataPointsPerMs / 1000
except (ZeroDivisionError) as e:
logger.error(f'ZeroDivisionError: {e}')
p1_sec = float('nan')
p2_sec = float('nan')
epochDict = {
"sweepNumber": abf.sweepNumber,
"index": epochIdx,
"type": epochType,
"startPoint": p1, # point the epoch starts
"stopPoint": p2, # point the epoch ends
"startSec": p1_sec,
"stopSec": p2_sec,
"durSec": p2_sec - p1_sec,
"level": epochLevel,
"pulseWidth": pulseWidth,
"pulsePeriod": pulsePeriod,
"digitalState": digitalState, # list of 0/1 for 8x digital states
}
self._epochList.append(epochDict)
def getEpochList(self, asDataFrame: bool = False):
if asDataFrame:
return pd.DataFrame(self._epochList)
else:
return self._epochList
def findEpoch(self, pnt: int) -> Optional[int]:
"""Return epoch index for a point in recording.
Stop points are always the same as next epoch start point.
Be sure to use '<' like pnt<stopPnt to get epoch index correct.
If not found, return None
Parameters
----------
pnt : int
Point index into recording (within a sweep)
"""
for epochIdx, epoch in enumerate(self._epochList):
startPoint = epoch["startPoint"]
stopPoint = epoch["stopPoint"]
if pnt >= startPoint and pnt < stopPoint:
return epochIdx
#
# return None
def getLevel(self, epoch):
"""Given an epoch number return the 'level'"""
return self._epochList[epoch]["level"]
def getStartSec(self, epoch):
"""Given an epoch number return the 'startSec'"""
return self._epochList[epoch]["startSec"]
def getStartSecs(self):
"""Return all epoch start times."""
startSecs = [epoch["startSec"] for epoch in self._epochList]
return startSecs
def getEpochLines(self, yMin=0, yMax=1):
x = [float("nan")] * (self.numEpochs() * 3)
y = [float("nan")] * (self.numEpochs() * 3)
for epoch in range(self.numEpochs()):
idx = epoch * 3
x[idx] = self._epochList[epoch]["startSec"]
x[idx + 1] = self._epochList[epoch]["startSec"]
x[idx + 2] = float("nan")
y[idx] = yMin
y[idx + 1] = yMax
y[idx + 2] = float("nan")
return x, y
def numEpochs(self):
# print('qqq:', self._epochList)
return len(self._epochList)
|