forked from BackyardBrains/SpikeTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphingContinuousData.py
216 lines (175 loc) · 7.27 KB
/
graphingContinuousData.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 16 10:25:37 2018
@author: benrobbins
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import spectrogram
def plotContinuousData(oSession, channel = 0, timerange = 0):
"""
Plots a time vs voltage of a session object.
Parameters
----------
oSession: session object
channel: List of channels you want to graph. Defult is all channels are graphed.
timerange: List with two values, the start time of the graph and the end
time of the graph.
"""
if not timerange:
timerange = [0,len(oSession.EEG.data[0])/ oSession.EEG.FS]
if not channel:
channel = [i for i in range(len(oSession.EEG.data))]
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
base = 0
locs = []
labels = []
temp = []
for i in range(len(oSession.EEG.data)):
temp.append(oSession.EEG.data[i][timerange[0] * oSession.EEG.FS:int(timerange[1] * oSession.EEG.FS + 1)])
oSession.EEG.data = np.array(temp, np.int32)
for i in range(len(channel)):
ax1.plot(oSession.EEG.data[channel[i]] + base)
locs.append(base)
labels.append(str(channel[i] + 1))
if channel[i] != len(oSession.EEG.data) - 1:
base += (oSession.EEG.data[channel[i]].max() + 10 - oSession.EEG.data[channel[i + 1]].min())
plt.yticks(locs, labels)
plt.ylabel('Channels')
bottomLim, topLim = ax1.get_xlim()
labels = []
locs = []
whitespace = 0 - bottomLim
for i in range(5):
labels.append((timerange[0] + (((timerange[1] - timerange[0])/4) * i)))
locs.append((bottomLim + whitespace) + ((((topLim - whitespace)- (bottomLim + whitespace))/4) * i))
plt.xticks(locs, labels)
plt.xlabel('Secounds since the start')
def plotEventTriggeredAverage(oSession, event = '', channels = 0, timerange = [-1, 3]):
"""
Plots an average time vs voltage of a session object for an event.
Parameters
----------
oSession: session object
event: string represents an event in the session object
channel: List of channels you want to graph. Defult is all channels are graphed.
timerange: List with two values, the start time of the graph and the end
time of the graph.
"""
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
base = 0
locs = []
labels = []
if not channels:
channels = [i for i in range(len(oSession.EEG.data))]
data = np.zeros((len(oSession.EEG.data), (timerange[1] - timerange[0]) * oSession.EEG.FS))
for singleEvent in eval('oSession.' + event):
for i in range(len(data)):
data[i] += oSession.EEG.data[i][(singleEvent + timerange[0]) * oSession.EEG.FS:(singleEvent + timerange[1]) * oSession.EEG.FS + 1]
data = data/len(eval('oSession.' + event))
for i in channels:
ax1.plot(data[i] + base)
locs.append(base)
labels.append(str(i + 1))
if i != len(data) - 1:
base += (data[i].max() + 10 - data[i + 1].min())
plt.yticks(locs, labels)
plt.ylabel('Channels')
bottomLim, topLim = ax1.get_xlim()
labels = []
locs = []
whitespace = 0 - bottomLim
for i in range(5):
labels.append((timerange[0] + (((timerange[1] - timerange[0])/4) * i)))
locs.append((bottomLim + whitespace) + ((((topLim - whitespace)- (bottomLim + whitespace))/4) * i))
plt.xticks(locs, labels)
plt.xlabel('Secounds (Event is at 0)')
def plotContinuousSpectrogram(oSession, channel = 0, timerange = 0):
"""
Plots a time vs frequency vs power spectogram of a session object.
Parameters
----------
oSession: session object
channel: List of channels you want to graph. Defult is all channels are graphed.
timerange: List with two values, the start time of the graph and the end
time of the graph.
"""
if not timerange:
timerange = [0,len(oSession.EEG.data[0])/ oSession.EEG.FS]
if not channel:
channel = [i for i in range(len(oSession.EEG.data))]
temp = []
for i in range(len(oSession.EEG.data)):
temp.append(oSession.EEG.data[i][timerange[0] * oSession.EEG.FS:int(timerange[1] * oSession.EEG.FS + 1)])
data = np.array(temp, np.int32)
for i in channel:
f, t, Sxx = spectrogram(data[i], fs = oSession.EEG.FS, nfft = 2500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.pcolormesh(t,f,Sxx)
plt.title('Channel ' + str(i +1))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [S]')
ax.set_ylim(0,40)
def plotEventTriggeredSpetrogram( oSession, event = '', channel = 0, timerange = [-1,3]):
"""
Plots a time vs frequency vs power spectogram of a session object.
Parameters
----------
oSession: session object
event: string represents an event in the session object
channel: List of channels you want to graph. Defult is all channels are graphed.
timerange: List with two values, the start time of the graph and the end
time of the graph.
"""
if not timerange:
timerange = [0,len(oSession.EEG.data[0])/ oSession.EEG.FS]
if not channel:
channel = [i for i in range(len(oSession.EEG.data))]
data = np.zeros((len(oSession.EEG.data), (timerange[1] - timerange[0]) * oSession.EEG.FS))
for singleEvent in eval('oSession.' + event):
for i in range(len(data)):
data[i] += oSession.EEG.data[i][(singleEvent + timerange[0]) * oSession.EEG.FS:(singleEvent + timerange[1]) * oSession.EEG.FS + 1]
data = data/len(eval('oSession.' + event))
for i in channel:
f, t, Sxx = spectrogram(data[i], fs = oSession.EEG.FS, scaling = 'spectrum', nfft = 2500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.pcolormesh(t,f,Sxx)
plt.title('Channel ' + str(i +1))
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [S]')
ax.set_ylim(0,40)
def plotContinuousSpetrum(oSession, channel = 0, timerange = 0):
"""
Plots a frequency vs power Spetrum of a session object.
Parameters
----------
oSession: session object
channel: List of channels you want to graph. Defult is all channels are graphed.
timerange: List with two values, the start time of the graph and the end
time of the graph.
"""
if not timerange:
timerange = [0,len(oSession.EEG.data[0])/ oSession.EEG.FS]
if not channel:
channel = [i for i in range(len(oSession.EEG.data))]
temp = []
for i in range(len(oSession.EEG.data)):
temp.append(oSession.EEG.data[i][timerange[0] * oSession.EEG.FS:int(timerange[1] * oSession.EEG.FS + 1)])
data = np.array(temp, np.int32)
for i in channel:
f, t, Sxx = spectrogram(data[i], fs = oSession.EEG.FS, nfft = 2500)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
newData = []
for freq in Sxx:
newData += [freq.sum()/len(freq)]
ax.bar(range(len(newData)), newData, color = 'k', width = 1)
plt.title('Channel ' + str(i +1))
plt.ylabel('Power')
plt.xlabel('Frequency[Hz]')
ax.set_xlim(0,200)