-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKilosort_browser.m
385 lines (360 loc) · 14.8 KB
/
Kilosort_browser.m
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
function parent_handle = Kilosort_browser(parent_handle)
global SERVER_PATH
KSpaths=UTkilosort_paths();
h = 610; % Total window height
w = 950; % Total window width
ht=500;
pad=3;
ts=20;
do_buttons_hpos=300;
do_buttons_hpos2=550;
show_completed=1;
show_incomplete=1;
if ~exist('parent_handle', 'var')
parent_handle = figure('Menubar','none', 'Resize','off', ...
'Units','pixels', 'Position', [20 50 w h],...
'Name', 'Kilosort Jobs Browser', 'NumberTitle', 'off',...
'HandleVisibility', 'off');
end
if isempty(which('TableSorter'))
TPATH = fileparts(which('narf_set_path'));
javaaddpath([TPATH '/libs/TableSorter.jar']);
end
if isempty(which('TableColumnAdjuster'))
TPATH = fileparts(which('narf_set_path'));
javaaddpath([TPATH '/libs/TableColumnAdjuster.jar']);
end
uicontrol('Parent', parent_handle, 'Style', 'text', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Name:', ...
'BackgroundColor',[.8 .8 .8],...
'Position', [pad ht+4*ts 50 ts]);
handles.name = uicontrol('Parent', parent_handle, 'Style', 'edit', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', '', ...
'Max', 1, 'Min', 1, ...
'Position', [2*pad+50 ht+4*ts 200 ts], ...
'Callback', @reload_job_table);
handles.clear_site_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Clear all sorting from site', ...
'Max', 1, 'Min', 1, ...
'Position', [pad ht+2*ts 200 ts], ...
'Callback', @clear_site_callback);
handles.view_in_phy_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'View in Phy', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos ht+4*ts 200 ts], ...
'Callback', @view_in_phy_callback,'BUTTONDOWNFCN',@view_in_phy_callback);
handles.save_to_db_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Save to database', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos ht+3*ts 200 ts], ...
'Callback', @save_to_db_callback);
handles.save_to_temp_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Save to temp file', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos ht+2*ts 200 ts], ...
'Callback', @save_to_temp_callback);
handles.delete_job_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Delete', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos2 ht+4*ts 200 ts], ...
'Callback', @delete_job_callback);
handles.show_log_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Show Log', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos2 ht+3*ts 200 ts], ...
'Callback', @show_log_job_callback);
handles.load_job_button = uicontrol('Parent', parent_handle,...
'Style', 'pushbutton', 'Units', 'pixels',...
'HorizontalAlignment', 'left', 'String', 'Load job to workspace', ...
'Max', 1, 'Min', 1, ...
'Position', [do_buttons_hpos2 ht+2*ts 200 ts], ...
'Callback', @load_job_callback);
handles.job_table = uitable('Parent', parent_handle, ...
'Enable', 'on', 'Units', 'pixels',...
'RowName', [], ...
'ColumnWidth', {700,80,80,80}, ...
'ColumnName', {'Name', 'Sorted', 'Saved in Phy','Saved to|database'}, ...
'Position', [pad pad w-pad*2 ht]);
% Configure the job table selection to update the center and right panels
hJS = findjobj(handles.job_table);
hJT = hJS.getViewport.getView;
hJT.setNonContiguousCellSelection(false);
hJT.setColumnSelectionAllowed(false);
hJT.setRowSelectionAllowed(true);
hJT.setSelectionMode(0); % Allow only a single row to be selected at once
hJTcb = handle(hJT, 'CallbackProperties');
set(hJTcb, 'MouseReleasedCallback', {@job_table_row_selected, parent_handle});
set(hJTcb, 'KeyPressedCallback', {@job_table_row_selected, parent_handle});
jobs=[];
ji=[];
%reload_job_table
function job_table_row_selected(Hobj,ev,a)
ji = Hobj.getSelectedRows()+1;
j=load([jobs(ji).pth filesep jobs(ji).name],'results_path');
r_pth=j.results_path;
cmd=['phy_lbhb template-gui ' r_pth filesep 'params.py\n'];
fprintf(cmd)
end
function reload_job_table(Hobj,ev)
enable_buttons(0)
jobs=[];
name_filter=get(handles.name,'String');
if(show_incomplete)
pth=[KSpaths.jobs_path filesep 'in_progress'];
jobs=dir(pth);jobs(1:2)=[];
jobs(arrayfun(@(x)length(x.name)<5,jobs))=[];
jobs(arrayfun(@(x)~strcmp(x.name(end-3:end),'.mat'),jobs))=[];
if(~isempty(name_filter))
jobs(arrayfun(@(x)isempty(strfind(x.name,name_filter)),jobs))=[];
end
if(~isempty(jobs))
for i=1:length(jobs)
tmp=load([pth filesep jobs(i).name],'status','results_path');
jobs(i).sorted=logical(tmp.status);
jobs(i).saved_in_phy=exist([tmp.results_path filesep 'cluster_group.tsv'],'file')==2;
ss=strsep(strrep(jobs(i).name,'.mat',''),'_');
jobs(i).pen=ss{1};
jobs(i).rn1=ss{2};
if ~isnumeric(jobs(i).rn1)
ind=length(ss{2})-1;
while ind>0
jobs(i).rn1=str2double(ss{2}(1:ind));
if ~isnan(jobs(i).rn1)
break
end
ind=ind-1;
end
if ind==0
jobs(i).rn1=NaN;
warning(['Couldn''t figure out run number for ',jobs(i).name,'. Setting to NaN.'])
end
end
end
sf=cell(length(jobs),2);
[sf{:,1}]=deal(jobs.pen);
[sf{:,2}]=deal(jobs.rn1);
[~,si]=sortrows(sf,[1 2]);
jobs=jobs(si);
[jobs.saved_in_db]=deal(false);
[jobs.pth]=deal(pth);
else
jobs=[]
end
end
if(show_completed)
pth=[KSpaths.jobs_path filesep 'completed'];
Cjobs=dir(pth);Cjobs(1:2)=[];
Cjobs(arrayfun(@(x)length(x.name)<5,Cjobs))=[];
Cjobs(arrayfun(@(x)~strcmp(x.name(end-3:end),'.mat'),Cjobs))=[];
if(~isempty(name_filter))
Cjobs(arrayfun(@(x)isempty(strfind(x.name,name_filter)),Cjobs))=[];
end
if(~isempty(Cjobs))
for i=1:length(Cjobs)
tmp=load([pth filesep Cjobs(i).name],'status','results_path');
Cjobs(i).sorted=logical(tmp.status);
Cjobs(i).saved_in_phy=exist([tmp.results_path filesep 'cluster_group.tsv'],'file')==2;
ss=strsep(Cjobs(i).name,'_');
Cjobs(i).pen=ss{1};
Cjobs(i).rn1=ss{2};
end
sf=cell(length(Cjobs),2);
[sf{:,1}]=deal(Cjobs.pen);
[sf{:,2}]=deal(Cjobs.rn1);
[~,si]=sortrows(sf,[1 2]);
Cjobs=Cjobs(si);
[Cjobs.saved_in_db]=deal(true);
[Cjobs.pth]=deal(pth);
jobs=[jobs;Cjobs];
end
end
l = length(jobs);
c = cell(l,4);
for i = 1:l
c{i,1} = jobs(i).name(1:end-4);
c{i,2} = jobs(i).sorted;
c{i,3} = jobs(i).saved_in_phy;
c{i,4} = jobs(i).saved_in_db;
end
set(handles.job_table, 'Data', c);
a=2;
enable_buttons(1)
end
function view_in_phy_callback(Hobj,ev)
if(isempty(ji))
return
end
st=get(get(Hobj,'Parent'),'SelectionType');
enable_buttons(0);
j=load([jobs(ji).pth filesep jobs(ji).name]);
r_pth=j.results_path;
cmd=['phy_lbhb template-gui ' r_pth filesep 'params.py'];
if(strcmp(st,'alt'))
cmd=[cmd,' &'];
end
fprintf(['Loading using command: ',cmd,'\n'])
err_type=0;
try
[s,w]=unix(cmd,'-echo');
catch err
err_type=1;
end
if err_type==0 && ~strcmp(st,'alt')
try
%if phy was run blocking matlab, change permissions of new
%files phy created so other users can look at this file too.
[w2,s]=unix(['chmod -R 777 ',r_pth]); if w2, error(s), end
catch err
err_type=2;
end
end
enable_buttons(1);
if err_type==1
rethrow(err);
elseif err_type==2
warning('Could not set permissions to be open for all files in %s. This is probably because someone else owns some files here.',r_pth)
end
end
function save_to_db_callback(Hobj,ev)
%enable_buttons(0);
[s, button] = settingsdlg(...
'Description', ['Saving ',jobs(ji).name,'. Set save parameters:'],...
'title' , 'Kilosort save options',...
'WindowWidth',870,...
'ControlWidth',350,...
{'Clear sorting for this run(s) (clear celldB database and spk.mat files)'; 'purge_rawids'}, true,...
{'Don''t save these ids (don''t save these units)';'remove_Kids'},'',...
{'Only save these ids (only save these units)';'keep_Kids'},'',...
{'List of bad runs (ex ''2:4,5; 56:7,8'' to mark runs 4&5 bad for KID2, 7&8 bad for KID 56)';'bad_runs'},'');
s.load_as_temp=0;
s.append_units=1; %Always append units
s.delete_existing_spkfile = s.purge_rawids;
s.force_compute_quality_measures=0;
s.use_automerge=0;%depreciated (KS1)
s=rmfield(s,{'WindowWidth','ControlWidth'});
if(strcmp(button,'OK'))
savfiles=UTkilosort_load_completed_job([jobs(ji).pth filesep jobs(ji).name],s);
reload_job_table();
end
end
function save_to_temp_callback(Hobj,ev)
%enable_buttons(0);
[s, button] = settingsdlg(...
'Description', ['Saving ',jobs(ji).name,...
' to spk.mat file in /tmp/, not writing database. Set save parameters:'],...
'title' , 'Kilosort temporary save options',...
'WindowWidth',650,...
'ControlWidth',350,...
{'Delete existing spk.mat file(s) in /tmp/'; 'delete_existing_spkfile'}, true,...
{'Force compute quality (Computre quality metrics, by default not computed if saving to temp)'; 'force_compute_quality_measures'}, false);
s.load_as_temp=1;
s.append_units=0;%never append (append uses database)
s.purge_rawids=0;
s.remove_Kids=[];
s.keep_Kids=[];
s.use_automerge=0;%depreciated (KS1)
s=rmfield(s,{'WindowWidth','ControlWidth'});
if(strcmp(button,'OK'))
savfiles=UTkilosort_load_completed_job([jobs(ji).pth filesep jobs(ji).name],s);
reload_job_table();
end
end
function enable_buttons(en)
list={'name','view_in_phy_button','save_to_db_button',...
'save_to_temp_button','delete_job_button','show_log_button',...
'load_job_button','clear_site_button','job_table',};
for i=1:length(list)
if(en)
set(handles.(list{i}),'Enable','on')
else
set(handles.(list{i}),'Enable','off')
end
end
pause(.1)
end
function show_log_job_callback(Hobj,ev)
if(isempty(ji))
return
end
j=load([jobs(ji).pth filesep jobs(ji).name],'queueidx');
if isempty(fieldnames(j))
warning(['queueidx not found. These were only more recently ',...
'stored in the job file so they''re not available for ',...
'older files.']);
else
fn=sprintf(['%sweb',filesep,'celldb',filesep,'queue',filesep,...
'%d',filesep,'%d.out'],SERVER_PATH,floor(j.queueidx/1000)*1000,...
j.queueidx);
if exist(fn,'file')
fprintf(sprintf('-------------- Log for %s -----------------\n',...
[jobs(ji).pth filesep jobs(ji).name]))
type(fn)
fprintf('-------------- End Log -----------------\n')
else
warning(sprintf('Log file %s does not exist',fn))
end
end
end
function load_job_callback(Hobj,ev)
if(isempty(ji))
return
end
j=load([jobs(ji).pth filesep jobs(ji).name]);
assignin('base','job',j)
fprintf(sprintf('Loaded %s to ''job''\n',...
[jobs(ji).pth filesep jobs(ji).name]))
end
function delete_job_callback(Hobj,ev)
if(isempty(ji))
return
end
[s, button] = settingsdlg(...
'Description', ['Deleting ',jobs(ji).name],...
'title' , 'Delete job',...
{'Delete phy file'; 'delete_phy'},true,...
{'Delete job file'; 'delete_mat'},true);
if(strcmp(button,'OK'))
if(s.delete_phy)
tmp=load([jobs(ji).pth filesep jobs(ji).name],'results_path');
sl=strfind(tmp.results_path,filesep);
[status,m,mid]=rmdir(tmp.results_path(1:sl(end)),'s');
if(~status)
switch mid
case 'MATLAB:RMDIR:NotADirectory'
warning(m)
otherwise
error(m)
end
end
end
if(s.delete_mat)
delete([jobs(ji).pth filesep jobs(ji).name]);
end
reload_job_table();
end
end
function clear_site_callback(Hobj,ev)
if isempty(ji) || length(jobs)<ji
j=load([jobs(end).pth filesep jobs(end).name],'runs');
else
j=load([jobs(ji).pth filesep jobs(ji).name],'runs');
end
default_site = j.runs{1}(1:7);
site = inputdlg(['Warning! You''re about to delete all .spk.mat files ',...
'and clear the celldB sorting database tables for the site you enter.',...
' Which site to clear?'],'Clear All Sorting on Site',1,{default_site});
if ~isempty(site) %user didn't press cancel
if length(site{1})==7
UTclear_sorted_spikes(site{1})
else
uiwait(errordlg(['Error. Invalid Site: ' site{1}]))
end
end
end
end