-
Notifications
You must be signed in to change notification settings - Fork 0
/
runspikedetekt_lib.py
235 lines (192 loc) · 9.27 KB
/
runspikedetekt_lib.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
#A collection of functions for running SpikeDetekt
#All decorated with joblib
import hash_utils
import joblib_utils as ju
import hybridata_creation_lib as hcl
import os
#from spikedetekt2.dataio.kwik import (add_recording, create_files, open_files,
# close_files, add_event_type, add_cluster_group, get_filenames,
# add_cluster,files_exist)
from spikedetekt2 import *
# functions used are get_params, run
# classes used: Experiment
from kwiklib.dataio import (add_clustering, open_files, close_files)
#----------------------------------------------------------------------
def create_files_Experiment(filename, DIRPATH, prm, prb):
#files_exist is a function in SD2.dataio.kwik
if not files_exist(filename, dir=DIRPATH):
create_files(filename, dir=DIRPATH, prm=prm, prb=prb)
# Open the files.
files = open_files(filename, dir=DIRPATH, mode='a')
# Add data.
add_recording(files,
sample_rate=prm['sample_rate'],
nchannels=prm['nchannels'])
add_cluster_group(files, channel_group_id='0', id='0', name='Noise')
add_cluster(files, channel_group_id='0',)
# Close the files
close_files(files)
@ju.func_cache
def run_spikedetekt(hybdatadict,sdparams,prb):
'''This function will call hash_hyb_SD(sdparams,hybdatadict)
and will run SpikeDetekt on the hybrid dataset specified by
hybdatadict with the parameters sd params'''
filename = hybdatadict['hashD']+'.kwd'
DIRPATH = hybdatadict['output_path']
# Make the product hash output name
hashSDparams = hash_utils.hash_dictionary_md5(sdparams)
# chose whether to include the probe, if so uncomment the two lines below
#hashprobe = hash_utils.hash_dictionary_md5(prb)
#hashdictlist = [ hybdatadict['hashD'],hashSDparams, hashprobe]
hashdictlist = [hybdatadict['hashD'],hashSDparams]
hash_hyb_SD_prb = hash_utils.make_concatenated_filename(hashdictlist)
outputfilename = hash_hyb_SD_prb +'.kwd'
# Need to create a symlink from hashD.kwd to hash_hyb_SD_prb.kwd
datasource = os.path.join(DIRPATH, filename )
dest = os.path.join(DIRPATH, outputfilename )
if not os.path.isfile(dest):
os.symlink(datasource, dest)
else:
print 'Warning: Symbolic link ',dest ,' already exists'
#Read in the raw data
raw_data = read_raw(dest,sdparams['nchannels'])
create_files_Experiment(outputfilename, DIRPATH, sdparams, prb)
# Run SpikeDetekt2
with Experiment(hash_hyb_SD_prb, dir= DIRPATH, mode='a') as exp:
run(raw_data,experiment=exp,prm=sdparams,probe=Probe(prb))
# add_cluster(hash_hyb_SD_prb, channel_group_id='0', id='0', clustering='main',
# cluster_group='0', color=None,
# )
return hash_hyb_SD_prb
#@ju.func_cache
def run_SD_oneparamfamily(param2vary,paramrange,defaultSDparams, hybdatadict):
'''
param2vary is a particular user adjustable variable in Spikedetekt
e.g. threshold_weak_std_factor
This function will loop over the values for param2vary over paramrange
(a one parameter family) and call run_spikedetekt()
with the defaultSDparams supplemented with the required
value for param2vary'''
pass
#return (all the hashes for each member of the family)
def run_spikedetekt_debug(hybdatadict,sdparams,prb):
''' Uncached version for debugging
This function will call hash_hyb_SD(sdparams,hybdatadict)
and will run SpikeDetekt on the hybrid dataset specified by
hybdatadict with the parameters sd params'''
filename = hybdatadict['hashD']+'.kwd'
DIRPATH = hybdatadict['output_path']
# Make the product hash output name
hashSDparams = hash_utils.hash_dictionary_md5(sdparams)
# chose whether to include the probe, if so uncomment the two lines below
#hashprobe = hash_utils.hash_dictionary_md5(prb)
#hashdictlist = [ hybdatadict['hashD'],hashSDparams, hashprobe]
hashdictlist = [hybdatadict['hashD'],hashSDparams]
hash_hyb_SD_prb = hash_utils.make_concatenated_filename(hashdictlist)
outputfilename = hash_hyb_SD_prb +'.kwd'
# Need to create a symlink from hashD.kwd to hash_hyb_SD_prb.kwd
datasource = os.path.join(DIRPATH, filename )
dest = os.path.join(DIRPATH, outputfilename )
if not os.path.isfile(dest):
os.symlink(datasource, dest)
else:
print 'Warning: Symbolic link ',dest ,' already exists'
#Read in the raw data
raw_data = read_raw(dest,sdparams['nchannels'])
create_files_Experiment(outputfilename, DIRPATH, sdparams, prb)
# Run SpikeDetekt2
with Experiment(hash_hyb_SD_prb, dir= DIRPATH, mode='a') as exp:
run(raw_data,experiment=exp,prm=sdparams,probe=Probe(prb))
return hash_hyb_SD_prb
#---------------------------------------------------------------------------------------------
if __name__== "__main__":
sample_rate = 20000
duration = 1.
nchannels = 32
#chunk_size = 20000 automatically set below
nsamples = int(sample_rate*duration)
#--------------------LIST OF ALL PARAMETERS--------------------------------
# Filtering
# ---------
filter_low = 500. # Low pass frequency (Hz)
filter_high = 0.95 * .5 * sample_rate
filter_butter_order = 3 # Order of Butterworth filter.
# Chunks
# ------
chunk_size = int(1. * sample_rate) # 1 second
chunk_overlap = int(.015 * sample_rate) # 15 ms
# Spike detection
# ---------------
# Uniformly scattered chunks, for computing the threshold from the std of the
# signal across the whole recording.
nexcerpts = 50
excerpt_size = int(1. * sample_rate)
threshold_strong_std_factor = 4.5
threshold_weak_std_factor = 2.
detect_spikes = 'negative'
#precomputed_threshold = None
# Connected component
# -------------------
connected_component_join_size = int(.00005 * sample_rate)
# Spike extraction
# ----------------
extract_s_before = 16
extract_s_after = 16
waveforms_nsamples = extract_s_before + extract_s_after
# Features
# --------
nfeatures_per_channel = 3 # Number of features per channel.
pca_nwaveforms_max = 10000
#----------------------------------------------------------------------
sdparams = get_params(**{
'nchannels': nchannels,
'sample_rate': sample_rate,
'filter_low': filter_low,
'filter_high':filter_high,
'filter_butter_order':filter_butter_order,
'chunk_size': chunk_size,
'chunk_overlap':chunk_overlap ,
'nexcerpts': nexcerpts,
'excerpt_size': excerpt_size,
'threshold_strong_std_factor': threshold_strong_std_factor,
'threshold_weak_std_factor' : threshold_weak_std_factor,
'detect_spikes': detect_spikes,
'connected_component_join_size' : connected_component_join_size,
'extract_s_before' : extract_s_before,
'extract_s_after': extract_s_after,
'waveforms_nsamples': waveforms_nsamples,
'nfeatures_per_channel': nfeatures_per_channel,
'pca_nwaveforms_max': pca_nwaveforms_max
})
prb = {'channel_groups': [
{
'channels': range(nchannels),
'graph': [
[0, 1], [0, 2], [1, 2], [1, 3], [2, 3], [2, 4],
[3, 4], [3, 5], [4, 5], [4, 6], [5, 6], [5, 7],
[6, 7], [6, 8], [7, 8], [7, 9], [8, 9], [8, 10],
[9, 10], [9, 11], [10, 11], [10, 12], [11, 12], [11, 13],
[12, 13], [12, 14], [13, 14], [13, 15], [14, 15], [14, 16],
[15, 16], [15, 17], [16, 17], [16, 18], [17, 18], [17, 19],
[18, 19], [18, 20], [19, 20], [19, 21], [20, 21], [20, 22],
[21, 22], [21, 23], [22, 23], [22, 24], [23, 24], [23, 25],
[24, 25], [24, 26], [25, 26], [25, 27], [26, 27], [26, 28],
[27, 28], [27, 29], [28, 29], [28, 30], [29, 30], [29, 31],
[30, 31]
],
}
]}
hybdatadict = {'hashD': '233124281b9bb0818d256d7b2ddc07ca_6a6f653c449505ddc30478cd5670f85c_b829bc190ca1a38622e5890c2dce883d',
'donorspike_timeseries_generating_function': hcl.create_time_series_constant,
'numchannels': 32, 'donor_path': '/chandelierhome/skadir/hybrid_analysis/mariano/donors/', 'donorcluster': 41,
'donorcluid': 'MKKdistfloat', 'amplitude_generating_function': hcl.make_uniform_amplitudes,
'start_time': 10, 'acceptor_path': '/chandelierhome/skadir/hybrid_analysis/mariano/acceptors/',
'amplitude_generating_function_args': [0.5, 1.5], 'acceptor': 'n6mab041109_60sec.dat',
'output_path': '/chandelierhome/skadir/hybrid_analysis/mariano/', 'end_time': None,
'experiment_path': '/chandelierhome/skadir/hybrid_analysis/mariano/', 'sampling_rate': 20000,
'donor': 'n6mab031109', 'donorspike_timeseries_arguments': 'arg', 'donorshanknum': 1, 'firing_rate': 3}
#Run SpikeDetekt
hash_hyb_SD = run_spikedetekt(hybdatadict,sdparams,prb)