-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
363 lines (238 loc) · 8.93 KB
/
Copy pathutils.py
File metadata and controls
363 lines (238 loc) · 8.93 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import json
import numpy as np
import scipy.io
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
from sklearn.cluster import MeanShift
from feature import FeatureSelection
from metrics import Metrics
from cluster import Cluster
from spikes import SpikeTime, SpikeLabel, SpikeWave
# Number of Channels in tetrode
num_channels = 4
# Number of samples for single spike
num_samples = 64
# Sampling frequency
freq_sampling = 30000
def read_mat_file(filename):
'''
Read .mat file
Parameters
----------
filename: str
Name of the .mat file to be read
Returns
-------
mat: dict
Dictionary with keys as parameters and values
are parameter value
'''
filepath = filepath = 'F:/Neuroscience/data/' + filename + '.mat'
# Reading .mat file
mat = scipy.io.loadmat(filepath)
return mat
def read_binary_file(filename):
'''
'''
filepath = 'F:/Neuroscience/data/' + filename
file = open(filepath, 'rb')
return file
def spike_times(duration_in_hrs,
filename):
'''
Read SpikeTimes file
Parameters
----------
duration_in_hrs: float
Time duration upto which spikes need to be
considered
filename: str
Name of the file to be read
Returns
-------
time_series: numpy.array
Time Bin at which spikes occured upto time
duration_in_hrs
'''
# convert time from hrs to seconds
duration_in_sec = duration_in_hrs*3600
# Number of samples corresponding to 30 kHz frequency
num_samples = 30000*duration_in_sec
filepath = 'F:/Neuroscience/data/' + filename
# Reading binary file with data stored in uint64 dtype
spike_time_data = np.fromfile(filepath, dtype=np.uint64)
# Count number of samples 'i' upto given time duration
for i in range(len(spike_time_data)):
if spike_time_data[i] >num_samples:
break
# Returns samples upto ith bin
return spike_time_data[:i]
def spike_data_from_channels(no_of_spikes,
filename):
'''
Reads binary file for simulation spike data and
convert data N*256 matrix where each row will be
spike samples recorded in each channel
Parameters
----------
no_of_spikes: int
Number of spikes occured in given time
duration
filename: str
Name of spike data file to be read
Returns
-------
'''
no_of_samples = no_of_spikes*num_channels*num_samples
filepath = 'F:/Neuroscience/data/' + filename
data = np.fromfile(filepath, dtype=np.int16, count=no_of_samples)
channel1_data = []
channel2_data = []
channel3_data = []
channel4_data = []
spike_data_4_channel = []
# Store spike data for each channel in seperate
# list
for i in range(0,no_of_samples,4):
channel1_data.append(data[i])
channel2_data.append(data[i+1])
channel3_data.append(data[i+2])
channel4_data.append(data[i+3])
# Store spike data in N*256 dim matrix
# where each row corresponds to different
# spikes and columns corresponds to data
# recorded on 4 channels seprately by
# dividing data in the 64 equal chunks
for i in range(no_of_spikes):
shift_idx = 0
# Start and end bin index for given spikes
start_idx = int(num_samples*(i+shift_idx))
end_idx = int(num_samples*(i+1+shift_idx))
try:
#print(len(channel1_data[start_idx:(end_idx)]))
if len(channel1_data[start_idx:(end_idx)]) == 64:
spike_data_4_channel.append(channel1_data[start_idx:(end_idx)]+
channel2_data[start_idx:(end_idx)]+
channel3_data[start_idx:(end_idx)]+
channel4_data[start_idx:(end_idx)])
except:
continue
# Convert all the spike data from list to array, it will facilitate
# lots of numpy array functionalities that will be later helpful
channel1_data =np.array(channel1_data)
channel2_data =np.array(channel2_data)
channel3_data =np.array(channel3_data)
channel4_data =np.array(channel4_data)
spike_data_4_channel = np.array(spike_data_4_channel)
return spike_data_4_channel
def avg_spikes_channels(ch1, ch2, ch3, ch4):
'''
'''
return (ch1+ch2+ch3+ch4)/4
def avg_multiple_spikes(spike_data, no_of_spikes):
'''
'''
avg_spikes = np.zeros(num_samples)
for i in range(no_of_spikes):
#print(num_samples*(i+1/2), num_samples*(i+3/2))
start_idx = int(num_samples*(i))#+1/2))
end_idx = int(num_samples*(i+1))#3/2))
try:
avg_spikes = (avg_spikes+spike_data[start_idx:end_idx])
except:
continue
avg_spikes = avg_spikes/no_of_spikes
return avg_spikes
def gmm_feature(features_array,
num_peaks):
'''
'''
gmm = GaussianMixture(n_components=num_peaks,
random_state=0).fit(features_array)
return gmm
def peaks_gmm_model(features_array,
gmm_object):
'''
'''
feature_max_value = max(features_array)
feature_min_value = min(features_array)
feature_array = np.linspace(feature_min_value, feature_max_value, 100)
feature_probs = gmm_object.predict_proba(feature_array)
plt.plot(feature_probs)
plt.show()
return feature_probs
def jsonify_simulation_labels(num_spikes,
spike_time_data,
ground_truth_time,
ground_truth):
'''
'''
# this index used to reduce the search for
# simulation spike time, once the spike time
# has been searched it is not possible that
# next spike will occur at previous time bins
# because spikes will occur in increasing
# time order only.
idx_readed = 0
# This list contains final labels for spike
# i.e. labels for simulation spike will be
# either ground truth spike label if it occurs
# in certain range of original spike time
# or it will be labeled as zero i.e. noise
ground_noise_label = []
# Iterating over number of spike events in given
# duration of time
for i in range(num_spikes):
# Simulation Time of current spike
simulation_time = spike_time_data[i]
# Default label for current spike as true
# if it does not gets any other label
spike_is_noise = True
# Counting number of iterations completed
# for searching spike in ground_truth spike
# time
j = 0
# Iterating over ground truth spike times
for real_time in ground_truth_time[idx_readed:]:
# Count increases by 1 as one iteration for seaching spike
# started
j += 1
# If difference of samples between simulation and real time is
# less then 2 samples, then spikes will be considered as true
# spike
if np.abs(simulation_time-real_time) < 2:
# As simulatio spike has been detected as true spike
# it should be given label from the ground truth
ground_noise_label.append(int(ground_truth[ground_truth_time.index(real_time)]))
# Update the index upto which spike time has already been
# checked
idx_readed = ground_truth_time.index(real_time)
# As spike has been detected as true spike it will get noise label
# as false and inner for loop will break
spike_is_noise = False
break
# If real time has exceeded above simulation time
# then there is no more possibility of finding
# ground truth corresponding to simulation spike
# and the loop will break
elif real_time>simulation_time:
break
print('Number of iterations completed:', j, i)
# Give spike label zero if it is noise
if spike_is_noise:
#print('Hitted Noise')
ground_noise_label.append(0)
#print('Ground Noise Level:',ground_noise_label)
#print('DataSet Ground Labels without Noise:', ground_truth[:num_spikes])
# Store labels of the spikes in dictionary
ground_noise_label_dict = {'Ground_Noise_Label': ground_noise_label}
#print(ground_noise_label_dict)
# create json object from the spike labels
json_object = json.dumps(ground_noise_label_dict)
# Writing to sample.json
# Write json file from json object
with open("ground_noise_label.json", "w") as outfile:
outfile.write(json_object)
if __name__ == '__main__':
print('Utils python script executed')