-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutter_han.m
274 lines (214 loc) · 8.21 KB
/
cutter_han.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
function cutter_han(tetrode, cell_number, flag)
%CUTTER_HAN Helps with cleaning up clusters by cutting out waveforms
% Waveforms can be cut out either to 'unclustered' or they can be removed
% from the dataset as a whole.
%
% Examples:
% cutter_han(tetrode, 1); Will help with cleaning up cluster 1, waveforms
% that are cut out will be flagged as 'unclustered'.
%
% cutter_han(tetrode, 0); Will help with cleanin up all unclustered
% waveforms. The waveforms that are cut out will be flaged as noise and
% removed from the dataset.
%
% cutter_han(tetrode, 1, 'remove noise'); Will help with cleaning up
% cluster 1, waveforms taht are cut out will be flagged as noise and
% removed from the dataset.
%
% tetrode should be an object of the tetrode class.
%
% cutter_han is part of Bearphys, Bearphys is made by Han de Jong,
% Empty variables
start_point = [];
end_point = [];
% Cut to noise or cut to unclustered
cut_to_noise = false; % by default cut to unclustered
if cell_number == 0
cut_to_noise = true;
end
% Work on a third argument
if nargin==3 && strcmp(flag, 'remove noise')
cut_to_noise = true;
end
% Find the cell indexer
indexer = tetrode.cells == cell_number;
% Work on the xaxis
interval = 10^6/tetrode.raw_data.hdr.SamplingFrequency; %µs
timeline = [0:interval:1000-interval]';
% Figure out the displayfraction (to preserve system resources)
cutoff = tetrode.settings.disp_cutoff;
if sum(indexer)>cutoff
disp_fraction = cutoff/sum(indexer);
disp(['Showing ' num2str(disp_fraction*100) '% of waveforms.'])
else
disp_fraction = 1;
end
% 4 subplots for 4 electrodes
[main_figure, histogram_figure] = tetrode.show_cell(cell_number, 'fraction', disp_fraction);
close(histogram_figure);
% add callbacks to the figure
main_figure.WindowButtonDownFcn = @mouse_click;
main_figure.WindowButtonMotionFcn = @mouse_motion;
main_figure.WindowKeyPressFcn = @key_press;
% Make the main figure bigger
main_figure.Position = [300, 100, 1000, 800];
% Plot the cutter line
figure(main_figure)
hold on
cutter_line = line([0 0], [0 0], 'Visible','off','Color',[1, 0.6471, 0]);
drawing_line = false;
% Wait for the figure to be closed
% Are we done
we_are_done = false;
% Loop untill we are done
while(~we_are_done)
% If the figure is closed, end the program
if ~ishandle(main_figure)
we_are_done = true;
end
% quick pause
pause(0.01);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%% Nested Callbacks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mouse_click(scr, ev)
% Deals with mouse input
p_axis = gca;
c_point = p_axis.CurrentPoint(1,:);
if drawing_line
end_point = [c_point(1); c_point(2)];
cut_waveforms();
else
start_point = [c_point(1), c_point(2)];
drawing_line = true;
end
update_cutter_line();
end
function mouse_motion(scr, ev)
% Deals with mouse motion
p_axis = gca;
c_point = p_axis.CurrentPoint(1,:);
if drawing_line
end_point = [c_point(1); c_point(2)];
end
update_cutter_line();
end
function key_press(scr, ev)
% Deals with key presses
switch ev.Key
case 'space' % delete line (do not cut)
drawing_line = false;
start_point =[];
end_point = [];
update_cutter_line();
case 'a' % show a higher fraction of waveforms
disp_fraction = disp_fraction * 2;
if disp_fraction>100; disp_fraction = 100; end
disp(['Showing ' num2str(disp_fraction*100) '% of waveforms.'])
replace_figure();
case 'z' % show a smaller fraction of waveforms
disp_fraction = disp_fraction * 0.5;
disp(['Showing ' num2str(disp_fraction*100) '% of waveforms.'])
replace_figure();
otherwise
disp('Key not recognized.')
end
end
function update_cutter_line()
% Move the cutter line to the plot the user has last clicked on
cutter_line.Parent = gca;
% Draw the line
if drawing_line && ~isempty(end_point)
cutter_line.XData = [start_point(1), end_point(1)];
cutter_line.YData = [start_point(2), end_point(2)];
cutter_line.Visible='on';
else
cutter_line.Visible='off';
return
end
% Figure out if this is a vertical line.
if abs(start_point(1) - end_point(1))<25
cutter_line.UserData = 'vertical';
cutter_line.Color = [1, 0.6471, 0];
else
cutter_line.UserData = 'not vertical';
cutter_line.Color = [0, 0.3529, 1];
end
end
function cut_waveforms()
% Stop drawing the line
drawing_line = false;
% find out which axis we are working on
hax = gca;
electrode_nr = str2num(hax.Title.String(end));
% First find the indexer of the waveforms that should be cut
data_Y = squeeze(tetrode.data(electrode_nr,:,:));
data_X = repmat([1:size(data_Y,1)]',1, size(data_Y,2));
% For the line, we have to calculate back to X_index and raw bitvalue,
% because the data is stored in that way in the object
[~, X_index(1)] = min(abs(timeline-start_point(1)));
[~, X_index(2)] = min(abs(timeline-end_point(1)));
Y_index(1) = start_point(2)/(tetrode.settings.ADBitVolts(electrode_nr)*10^6);
Y_index(2) = end_point(2)/(tetrode.settings.ADBitVolts(electrode_nr)*10^6);
% If the line is not vertical, we'll use polyxpoly to find
% intersections. This is a bit slow.
if strcmp(cutter_line.UserData, 'not vertical')
% Use polyxpoly to find intersection
[~, ~, c] = polyxpoly(data_X, data_Y, X_index, Y_index);
% Now from the linesegment index in c to the waveform index
new_indexer = c(:,1)/(size(data_Y,1));
% Note that polyxpoly linearized the data and ALSO COUNTS THE LAST
% VALUE OF EVERY COLUMN TO THE FIRST VALUE OF THE NEXT COLUM AS A LINE
% SEGMENT. (It took me a full day to figure this out). This is why we
% divide by size(data_Y,1) and not size(data_Y,1)-1.
% If the linesegment divided by the number of datapoints per colum is a
% round number, polyxpoly cut the non-existing segment (the last
% segments of each column that supposedly connects to the next column).
new_indexer = unique(ceil(new_indexer(mod(new_indexer,1)>0)));
% Convert the indexer to a logical
logical_indexer = false(1,size(data_Y,2));
logical_indexer(new_indexer) = true;
else
% Much more easy, we just have to look at one X coordinate
X_index = round(mean(X_index));
logical_indexer = data_Y(X_index,:)>min(Y_index) & data_Y(X_index,:)<max(Y_index);
end
% Make sure we do this only on the selected cell
logical_indexer = logical_indexer & indexer;
% Remove these waveforms or label them unclustered
if cut_to_noise
tetrode.remove_noise('indexer', logical_indexer);
else
tetrode.cells(logical_indexer) = 0;
end
% Print some text
disp(['Removed ' num2str(sum(logical_indexer)) ' waveforms.']);
% Grab the indexer again
indexer = tetrode.cells == cell_number;
% Empty the cutting line for good measure
start_point =[];
end_point = [];
% And replace the figure
replace_figure();
end
function replace_figure()
% Now make a new main figure
position = main_figure.Position;
close(main_figure)
% Make a new one:
% 4 subplots for 4 electrodes
[main_figure, histogram_figure] = tetrode.show_cell(cell_number,'fraction', disp_fraction);
close(histogram_figure);
% add callbacks to the figure
main_figure.WindowButtonDownFcn = @mouse_click;
main_figure.WindowButtonMotionFcn = @mouse_motion;
main_figure.WindowKeyPressFcn = @key_press;
% Restore the original position
main_figure.Position = position;
% Plot the cutter line
figure(main_figure)
hold on
cutter_line = line([0 0], [0 0], 'Visible','off','Color',[1, 0.6471, 0]);
drawing_line = false;
end
end