-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxInterval.py
More file actions
97 lines (91 loc) · 3.24 KB
/
maxInterval.py
File metadata and controls
97 lines (91 loc) · 3.24 KB
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
import numpy as np
def maxInterval(spiketrain, max_begin_ISI=0.17, max_end_ISI=0.3, min_IBI=0.2, min_burst_duration=0.01,
min_spikes_in_burst=3):
allBurstData = {}
'''
Phase 1 - Burst Detection
Here a burst is defined as starting when two consecutive spikes have an
ISI less than max_begin_ISI apart. The end of the burst is given when two
spikes have an ISI greater than max_end_ISI.
Find ISIs closer than max_begin_ISI and end with max_end_ISI.
The last spike of the previous burst will be used to calculate the IBI.
For the first burst, there is no previous IBI.
'''
inBurst = False
burstNum = 0
currentBurst = []
for n in range(1, len(spiketrain)):
ISI = spiketrain[n] - spiketrain[n - 1]
if inBurst:
if ISI > max_end_ISI: # end the burst
currentBurst = np.append(currentBurst, spiketrain[n - 1])
allBurstData[burstNum] = currentBurst
currentBurst = []
burstNum += 1
inBurst = False
elif (ISI < max_end_ISI) & (n == len(spiketrain) - 1):
currentBurst = np.append(currentBurst, spiketrain[n])
allBurstData[burstNum] = currentBurst
burstNum += 1
else:
currentBurst = np.append(currentBurst, spiketrain[n - 1])
else:
if ISI < max_begin_ISI:
currentBurst = np.append(currentBurst, spiketrain[n - 1])
inBurst = True
# Calculate IBIs
IBI = []
for b in range(1, burstNum):
prevBurstEnd = allBurstData[b - 1][-1]
currBurstBeg = allBurstData[b][0]
IBI = np.append(IBI, (currBurstBeg - prevBurstEnd))
'''
Phase 2 - Merging of Bursts
Here we see if any pair of bursts have an IBI less than min_IBI; if so,
we then merge the bursts. We specifically need to check when say three
bursts are merged into one.
'''
tmp = allBurstData.copy()
allBurstData = {}
burstNum = 0
b = 1
while b < len(tmp):
prevBurst = tmp[b - 1]
currBurst = tmp[b]
if IBI[b - 1] <= min_IBI:
prevBurst = np.append(prevBurst, currBurst)
b += 1
allBurstData[burstNum] = prevBurst
b+=1
burstNum += 1
if burstNum >= 2:
allBurstData[burstNum] = currBurst
'''
Phase 3 - Quality Control
Remove small bursts less than min_bursts_duration or having too few
spikes less than min_spikes_in_bursts. In this phase we have the
possibility of deleting all spikes.
'''
tooShort = 0
tmp = allBurstData
allBurstData = {}
burstNum = 0
if len(tmp) > 1:
for b in range(len(tmp)):
currBurst = tmp[b]
if len(currBurst) <= min_spikes_in_burst:
tooShort +=1
elif currBurst[-1] - currBurst[0] <= min_burst_duration:
tooShort += 1
else:
allBurstData[burstNum] = currBurst
burstNum += 1
'''
plt.figure()
plt.eventplot(spiketrain)
for b in allBurstData:
burst = allBurstData[b]
plt.axvline(burst[0], color='green')
plt.axvline(burst[-1], color='red')
'''
return allBurstData, tooShort