forked from ma3mool/TempoSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSandbox.py
More file actions
228 lines (179 loc) · 8.42 KB
/
Sandbox.py
File metadata and controls
228 lines (179 loc) · 8.42 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
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
from src.Detection import*
from src.Filtering import *
from src.Groundtruth import *
from src.ReadBinary import *
from src.Visualize import *
from src.Whitening import *
from comparisons.Filtering import*
from comparisons.ReadBinary import*
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import time
sample_data = 'data/2017_rat_hippocampus/decompressed_files'
channel_map = 'data/2017_rat_hippocampus/map_channels'
channel_locations = 'data/2017_rat_hippocampus/channel_locations'
spike_times = 'data/2017_rat_hippocampus/spike_times'
spike_clusters ='data/2017_rat_hippocampus/spike_clusters'
fs = 30000
def main(sample_data, channel_map, fs):
#pre-load important data
chan_locs = np.load(channel_locations, mmap_mode = "r")
chan_map = np.load(channel_map, mmap_mode = "r")
chunked_data = read_directly_to_chunks(sample_data)
#filtering/whitening of chunks
first_chunk = next(chunked_data) # First minute of data
filtered_chunk = highpass_every_channel(first_chunk, 300, fs) # 1000 window size
whitened_chunk = zca_whitening(filtered_chunk)
#ground-control spike times
gc_spikes = np.load(spike_times, mmap_mode = "r")
gc_spikes = gc_spikes.flatten()
#generate graph
#channel_spikes = get_channel_spikes(whitened_chunk, gc_spikes)
combined_channel_plot(whitened_chunk, chan_map, chan_locs, np.arange(384), 1800, 1900, None)
#visualize_neuropixel(chan_map, chan_locs, [1,2,3,4,5,6])
# detected_spikes = detect_spikes(filtered_chunk, 15)
# gc_spikes = np.load(spike_times, mmap_mode = "r")
# gc_spikes = gc_spikes.flatten()
# combined_channel_plot(filtered_chunk, [1,2,3,4,5,6], 0, 5000)
# gamma = capture_gamma(first_chunk, fs)
# plot_fourier(gamma, fs, 0, 1000)
#param_sweep_filtering(first_chunk, fs, spike_times)
#gamma_comparison(first_chunk, fs, spike_times, spike_clusters)
# print(np.load(spike_times, mmap_mode="r"))
# print(np.load(spike_clusters, mmap_mode="r"))
# timeslice_iterator = timeslice_generator(raw_data, 300)
# animate_timeslice(raw_data, timeslice_iterator, 1, fs)
## Visualize moving average vs. butterworth filtering for LFP
def compare_avg_butter_LFP(raw_data, fs):
mv_lfp = capture_LFP(raw_data, fs)
cmp_lfp = butter_lowpass_filter(raw_data, 300, fs)
filt_comparison_plot(raw_data, mv_lfp, cmp_lfp, 0, 0, 1000)
## Visualize moving average vs. butterworth highpass filtering
def compare_avg_butter_compliment(raw_data, fs):
mv_ap = capture_action_potential(raw_data, fs)
cmp_ap = raw_data - butter_lowpass_filter(raw_data, 300, fs)
filt_comparison_plot(raw_data, mv_ap, cmp_ap, 0, 0, 10)
## Visualize moving average vs. butterworth bandpass filtering
def compare_avg_butter_bandpass(raw_data, fs):
mv_lfp = capture_action_potential(raw_data, fs)
cmp_lfp = butter_bandpass_filter(raw_data, 300, 6000, fs)
filt_comparison_plot(raw_data, mv_lfp, cmp_lfp, 0, 0, 1000)
## Visualize approximately 20 gamma cycles
def visualize_gammawaves(raw_data,fs):
samples_per_gamma = fs//40
twenty_gamma_cycles = samples_per_gamma * 20
gamma = capture_gamma(raw_data, fs)
plot_channel_data(gamma, 0, 0, twenty_gamma_cycles)
## Compare detection plot produced by moving average vs. butterworth filter
def compare_detect_spikes(test_matrix):
filtered_matrix = butter_bandpass_filter(test_matrix, 300, 6000, fs)
mv_matrix = capture_action_potential(test_matrix, fs)
butter_spikes = detect_spikes(filtered_matrix, 20)
mv_spikes = detect_spikes(mv_matrix, 20)
plot_channel_spikes(test_matrix, butter_spikes, 1, 0, 5000)
plot_channel_spikes(test_matrix, mv_spikes, 1, 0, 5000)
## Live plotting of spiking data at channel
def animate_timeslice(raw_data, timeslice_iterator, channel, fs):
fig, ax = plt.subplots()
x_data, y_data = [], []
line, = ax.plot([], [], lw=2)
ax.set_xlim(0, len(raw_data[0]))
ax.set_ylim(-50, 50)
timing_array = []
def update(frame):
try:
timeslice = next(timeslice_iterator)
except StopIteration:
return line,
start_time = time.time()
filtered_timeslice = capture_action_potential(timeslice, fs)
spikes = detect_spikes(filtered_timeslice, 20)
end_time = time.time()
timing_array.append(end_time - start_time)
channel_spikes = spikes[channel]
x_data.extend(range(len(x_data), len(x_data) + len(timeslice[0])))
y_data.extend(channel_spikes)
#Displays the last 200ms
x_data_disp = x_data[-6000:]
y_data_disp = y_data[-6000:]
line.set_data(x_data_disp, y_data_disp)
ax.set_xlim(max(len(x_data) - 6000, 0), len(x_data))
ax.set_ylim(np.min(y_data), np.max(y_data))
return line,
ani = animation.FuncAnimation(fig, update, blit=True, cache_frame_data=False)
plt.show()
# time to process each 10ms timeslice
print(timing_array)
## Parameter sweep to identify best window-size and stride-length for moving average filter
def param_sweep_filtering(raw_data, fs, spike_times):
detection_threshold = 2 * calculate_rms(raw_data) # 2RMS threshold
spike_times = np.load(spike_times, mmap_mode = "r")
spike_times = spike_times.flatten()
spike_scores = []
for window in range(100, 401, 100):
for stride in range(1, 6, 1):
filtered_data = highpass_every_channel(raw_data, 300, fs, window_size=window, stride=stride)
spikes = detect_spikes(filtered_data, detection_threshold)
score = detection_score(spikes, spike_times)
spike_scores.append((window, stride, score))
plt.figure()
spike_scores = np.array(spike_scores)
print(spike_scores)
window_sizes = spike_scores[:, 0]
strides = spike_scores[:, 1]
scores = spike_scores[:, 2]
plt.figure(figsize=(10, 8))
scatter = plt.scatter(window_sizes, strides, c=scores, cmap='viridis')
plt.colorbar(scatter)
plt.xlabel('Window Size')
plt.ylabel('Stride')
plt.title('Accuracy of Spike Detection with Varying Parameters')
plt.show()
print(max(spike_scores, key=lambda x: x[2]))
## Plots the gamma wave and the detected spikes associated with a particular cluster
## on the same graph to help identify cyclic neural activity
def gamma_comparison(raw_data, fs, spike_times, spike_clusters, spike_cluster, start=0, end=7500):
length_of_interval = end - start
full_gamma = capture_gamma(raw_data, fs)
gamma_on_channel = full_gamma[1]
gamma = gamma_on_channel[start:end]
spike_cluster_times = format_output(spike_times, spike_clusters)
first_cluster = [time for time in spike_cluster_times[spike_cluster]]
first_cluster_on_interval = [idx for idx in first_cluster if idx < length_of_interval]
first_cluster_spike_times = np.zeros(length_of_interval)
first_cluster_spike_times[first_cluster_on_interval] = 50
plt.figure()
times = np.arange(0, length_of_interval)
plt.plot(times, gamma)
plt.plot(times, first_cluster_spike_times)
plt.legend(['Gamma', 'Spike Cluster'])
plt.xlabel('Time (ms)')
plt.ylabel('Amplitude')
plt.show()
#Function which returns a dictionary in which each entry represents the timse at which a cluster
#featured a spike
def get_spikes_by_cluster(path_to_spike_time, path_to_spike_clusters):
spike_times = np.load(path_to_spike_time, mmap_mode="r")
spike_clusters = np.load(path_to_spike_clusters, mmap_mode="r")
spike_times = spike_times.flatten()
spike_clusters = spike_clusters.flatten()
spike_dict = {}
for i in range(len(spike_times)):
if spike_clusters[i] in spike_dict:
spike_dict[spike_clusters[i]].append(spike_times[i])
else:
spike_dict[spike_clusters[i]] = [spike_times[i]]
return spike_dict
##experiments with spike_interface
# def compare_mv_bp_spikes(sample_data, channel_map, fs):
# raw_data = sp_read_binary(sample_data, fs, 384, "int16")
# mv_data = mv_trace_flat - capture_LFP(mv_trace_flat, fs)
# cmp_bp = spikeinterface_bp(cmp_data)
# cmp_trace = cmp_bp.get_traces(channel_ids=[1], start_frame=0, end_frame=1000)
# cmp_trace_flat = np.expand_dims(cmp_trace.flatten(), axis=0)
# filt_comparison_plot(raw_data, mv_trace, cmp_trace_flat, 0, 0, 1000)
## experiments trying to understand channel_locations
# print(np.load(channel_locations))
if __name__ == "__main__":
main(sample_data, channel_map, fs)