-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_on_substack.m
401 lines (357 loc) · 19.7 KB
/
debug_on_substack.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
render_parameters_file_path = fullfile(rendered_folder_path, 'calculated_parameters.jl') ;
render_parameters = read_renderer_calculated_parameters_file(render_parameters_file_path) ;
max_zoom_level = render_parameters.level_step_count ;
chunk_shape_ijk = render_parameters.leaf_shape ; % xyz order, same at all zoom levels, just the chunk count changes
spacing_at_max_zoom_xyz = render_parameters.spacing ;
origin_at_max_zoom_xyz = render_parameters.origin ;
spacing_at_zoom_level_0_xyz = 2^max_zoom_level * spacing_at_max_zoom_xyz ;
spacing_at_zoom_level_xyz = spacing_at_zoom_level_0_xyz ./ (2^zoom_level) ;
heckbert_origin_xyz = origin_at_max_zoom_xyz - spacing_at_max_zoom_xyz/2 ; % this origin does not change with the zoom level
origin_at_zoom_level_xyz = heckbert_origin_xyz + spacing_at_zoom_level_xyz/2 ;
stack_shape_ijk = chunk_shape_ijk * 2^zoom_level ; % the shape of the full-brain stack, at zoom level zoom_level
stack_shape_xyz = stack_shape_ijk .* spacing_at_zoom_level_xyz % the shape of the full-brain stack in um, does not change with zoom level
substack_desired_half_shape_xyz = abs(roi_corner_xyz - roi_center_xyz) ;
substack_desired_half_shape_xyz(3) = mean(substack_desired_half_shape_xyz(1:2)) ; % want similar extent in z
substack_desired_shape_xyz = 2 * substack_desired_half_shape_xyz
substack_desired_origin_xyz = roi_center_xyz - substack_desired_half_shape_xyz
substack_shape_ijk = round(substack_desired_shape_xyz ./ spacing_at_zoom_level_xyz) ;
substack_origin_ijk1 = round((substack_desired_origin_xyz - origin_at_zoom_level_xyz) ./ spacing_at_zoom_level_xyz) + 1 ;
% origin of the substack within the full stack at this zoom
substack_shape_xyz = spacing_at_zoom_level_xyz .* substack_shape_ijk
substack_origin_xyz = origin_at_zoom_level_xyz + spacing_at_zoom_level_xyz .* (substack_origin_ijk1-1)
pad_depth_ijk = ceil(pad_depth_in_um ./ spacing_at_zoom_level_xyz) ; % 3 x 1
padded_substack_origin_ijk1 = substack_origin_ijk1 - pad_depth_ijk
padded_substack_shape_ijk = substack_shape_ijk + 2*pad_depth_ijk
padded_substack_shape_xyz = spacing_at_zoom_level_xyz .* padded_substack_shape_ijk
padded_substack_origin_xyz = origin_at_zoom_level_xyz + spacing_at_zoom_level_xyz .* (padded_substack_origin_ijk1-1)
% coord of voxel center of lowest-indices voxel
padded_substack_far_corner_xyz = padded_substack_origin_xyz + (padded_substack_shape_ijk-1) .* spacing_at_zoom_level_xyz ;
% coord of voxel center of highest-indices voxel
%%
% Load the ground-truth (these are all soma locations)
xyz_from_raw_target_index = load_soma_targets() ;
% Filter out the ones outside the substack
bounding_box_lower_corner_xyz = substack_origin_xyz - spacing_at_zoom_level_xyz/2 ;
bounding_box_upper_corner_xyz = bounding_box_lower_corner_xyz + substack_shape_xyz ;
is_within_bounding_box = ...
all(bounding_box_lower_corner_xyz <= xyz_from_raw_target_index & xyz_from_raw_target_index < bounding_box_upper_corner_xyz, 2) ;
xyz_from_target_index = xyz_from_raw_target_index(is_within_bounding_box, :) ;
target_count = size(xyz_from_target_index, 1)
padded_substack_yxz = ...
get_mouselight_rendered_substack(rendered_folder_path, gfp_channel_index, padded_substack_origin_ijk1, padded_substack_shape_ijk, zoom_level) ;
padded_background_substack_yxz = ...
get_mouselight_rendered_substack(rendered_folder_path, background_channel_index, padded_substack_origin_ijk1, padded_substack_shape_ijk, zoom_level) ;
substack_mip = max(padded_substack_yxz,[], 3) ;
parameters = struct('intensity_threshold', {intensity_threshold}, ...
'minimum_volume', {minimum_volume}, ...
'maximum_volume', {maximum_volume}, ...
'maximum_sqrt_condition_number', maximum_sqrt_condition_number) ;
feature_struct_from_candidate_index = ...
find_candidate_somata_in_uint16_stack(...
padded_substack_yxz, ...
padded_background_substack_yxz, ...
padded_substack_origin_xyz, ...
origin_at_zoom_level_xyz, ...
spacing_at_zoom_level_xyz, ...
substack_origin_xyz, ...
substack_shape_xyz, ...
parameters) ;
% these are in the coordinate system of the full stack
candidate_count = length(feature_struct_from_candidate_index) ;
xyz_from_candidate_index = reshape([feature_struct_from_candidate_index.centroidoid_xyz], [3 candidate_count])' ;
voxel_count_from_candidate_index = [feature_struct_from_candidate_index.voxel_count]' ;
sqrt_condition_number_from_candidate_index = [feature_struct_from_candidate_index.sqrt_condition_number]' ;
max_intensity_from_candidate_index = [feature_struct_from_candidate_index.max_intensity]' ;
max_background_intensity_from_candidate_index = [feature_struct_from_candidate_index.max_background_intensity]' ;
% Report the perf of the guesses
print_performace_statistics_and_plot('candidates', ...
xyz_from_target_index, ...
feature_struct_from_candidate_index, ...
heckbert_origin_xyz, ...
stack_shape_xyz, ...
spacing_at_zoom_level_xyz, ...
substack_mip, ...
padded_substack_origin_xyz(1:2), ...
spacing_at_zoom_level_xyz(1:2)) ;
% % %
% % % Old way of figuring out which targets match which candidates
% % %
% %
% % % Calculate distance between each target and each guess
% % target_candidate_distance_matrix = zeros(target_count, candidate_count) ;
% % for i = 1 : target_count ,
% % target_i_xyz = xyz_from_target_index(i,:) ;
% % for j = 1 : candidate_count ,
% % candidate_j_xyz = xyz_from_candidate_index(j,:) ;
% % distance = sqrt(sum((candidate_j_xyz - target_i_xyz).^2)) ;
% % target_candidate_distance_matrix(i,j) = distance ;
% % end
% % end
% %
% % is_match_distance_threshold = 20 ;
% % [is_target_candidate_match, target_candidate_match_distance] = match_targets_and_guesses(target_candidate_distance_matrix, is_match_distance_threshold) ;
% %
% % is_there_a_matched_candidate_from_target_index = any(is_target_candidate_match, 2) ;
% % is_there_a_matched_target_from_candidate_index = any(is_target_candidate_match, 1) ;
% % [distance_to_matched_candidate_from_target_index, matching_candidate_index_from_target_index] = min(target_candidate_match_distance, [], 2) ;
% % [distance_to_matched_target_from_candidate_index, matching_target_index_from_candidate_index] = min(target_candidate_match_distance, [], 1) ;
%
% %
% % New way of figuring out which targets match which candidates
% %
%
% % Figure out what targets are in what candidates
% matching_candidate_index_from_target_index = ...
% match_targets_and_components(xyz_from_target_index, feature_struct_from_candidate_index, origin_at_zoom_level_xyz, spacing_at_zoom_level_xyz) ;
% matching_target_index_from_candidate_index = invert_partial_map_array(matching_candidate_index_from_target_index, candidate_count) ;
% is_there_a_matched_candidate_from_target_index = isfinite(matching_candidate_index_from_target_index) ;
% is_there_a_matched_target_from_candidate_index = isfinite(matching_target_index_from_candidate_index) ;
%
% %
% % End of new way
% %
%
% % Compute hit counts, precision, recall for the candidates
% target_count
% candidate_count
% candidate_hit_count = sum(is_there_a_matched_candidate_from_target_index)
% assert( sum(is_there_a_matched_target_from_candidate_index) == candidate_hit_count ) ;
% candidate_miss_count = sum(~is_there_a_matched_candidate_from_target_index)
% candidate_chase_count = sum(~is_there_a_matched_target_from_candidate_index)
%
% candidate_precision = candidate_hit_count / candidate_count
% candidate_recall = candidate_hit_count / target_count
%
%
% % Plot the MIP image
% f = figure('color', 'w', 'name', 'targets-and-candidates') ;
% a = axes(f, 'YDir', 'reverse') ;
% image(a, 'CData', substack_mip, ...
% 'XData', [padded_substack_origin_xyz(1) padded_substack_far_corner_xyz(1)], ...
% 'YData', [padded_substack_origin_xyz(2) padded_substack_far_corner_xyz(2)], ...
% 'CDataMapping', 'scaled') ;
% colormap(gray(256)) ;
% axis image
% xlabel('x (um)') ;
% ylabel('y (um)') ;
%
% % plot each target, and each candidate, and draw a line between matches
% hold on ;
% for target_index = 1 : target_count ,
% target_xyz = xyz_from_target_index(target_index,:) ;
% marker_color = fif(is_there_a_matched_candidate_from_target_index(target_index), [0 0.5 1], [1 0 0]) ;
% plot(target_xyz(1), target_xyz(2), 'Marker', '+', 'Color', marker_color) ;
% %text(target_xyz(1)+5, target_xyz(2)+5, sprintf('t%d', target_index), 'Color', 0.5*[1 1 1]) ;
% end
% % for candidate_index = 1 : candidate_count ,
% % candidate_xyz = xyz_from_candidate_index(candidate_index,:) ;
% % marker_color = fif(is_there_a_matched_target_from_candidate_index(candidate_index), [0 0.5 1], [1 0 0]) ;
% % plot(candidate_xyz(1), candidate_xyz(2), 'Marker', 'o', 'MarkerSize', 6, 'Color', marker_color) ;
% % %text(candidate_xyz(1)-5, candidate_xyz(2)-5, sprintf('g%d', candidate_index), 'Color', 0.5*[1 1 1]) ;
% % end
% % for target_index = 1 : target_count ,
% % target_xyz = xyz_from_target_index(target_index,:) ;
% % if is_there_a_matched_candidate_from_target_index(target_index) ,
% % candidate_index = matching_candidate_index_from_target_index(target_index) ;
% % candidate_xyz = xyz_from_candidate_index(candidate_index,:) ;
% % plot([target_xyz(1) candidate_xyz(1)], [target_xyz(2) candidate_xyz(2)], 'Color', [0 0.5 1]) ;
% % end
% % end
% hold off ;
volume_per_voxel = prod(spacing_at_zoom_level_xyz) ;
minimum_volume_in_voxels = minimum_volume / volume_per_voxel
maximum_volume_in_voxels = maximum_volume / volume_per_voxel
is_guess_from_candidate_index = ...
(minimum_volume_in_voxels < voxel_count_from_candidate_index) & ...
(voxel_count_from_candidate_index < maximum_volume_in_voxels) & ...
sqrt_condition_number_from_candidate_index < maximum_sqrt_condition_number & ...
max_intensity_from_candidate_index > max_background_intensity_from_candidate_index ;
feature_struct_from_guess_index = feature_struct_from_candidate_index(is_guess_from_candidate_index) ;
guess_count = length(feature_struct_from_guess_index) ;
% f = figure('color', 'w') ;
% a = axes(f, 'YDir', 'reverse') ;
% image(a, 'CData', substack_mip, ...
% 'XData', [padded_substack_origin_xyz(1) padded_substack_far_corner_xyz(1)], ...
% 'YData', [padded_substack_origin_xyz(2) padded_substack_far_corner_xyz(2)], ...
% 'CDataMapping', 'scaled') ;
% colormap(gray(256)) ;
% axis image
%
% hold on ;
% candidate_count = size(candidate_centroid_xyz_from_label,1) ;
% for i = 1 : candidate_count ,
% centroid_xyz = candidate_centroid_xyz_from_label(i,:) ;
% plot(centroid_xyz(1), centroid_xyz(2), '+', 'Color', [0 0.5 1]) ;
% end
% hold off ;
%
% hold on ;
% somata_count = size(putative_somata_xyzs,1) ;
% for i = 1 : somata_count ,
% soma_xyz = putative_somata_xyzs(i,:) ;
% plot(soma_xyz(1), soma_xyz(2), 'r+') ;
% end
% hold off ;
% miss_1_xy = [74096.2049 18331.3971] % location of a candidate centroid that is *not* classified as a soma, but should be
% distance_to_miss_1_xy = sqrt(sum((candidate_centroid_xyz_from_label(:,1:2) - miss_1_xy).^2,2)) ;
% [distance_from_miss_1_to_nearest_candidate_in_xy, label_of_miss_1] = min(distance_to_miss_1_xy)
%
% centroid_xyz_of_miss_1 = candidate_centroid_xyz_from_label(label_of_miss_1, :)
% voxel_count_of_miss_1 = voxel_count_from_label(label_of_miss_1)
% sqrt_condition_number_of_miss_1 = sqrt_condition_number_from_label(label_of_miss_1)
% is_putative_soma_for_miss_1 = is_putative_soma_from_label(label_of_miss_1)
% max_intesity_for_miss_1 = max_intensity_from_label(label_of_miss_1)
% % sqrt condition number is 2.6, voxel_count is 556, which just clears the
% % current minimum (515)
% Report the perf of the guesses
print_performace_statistics_and_plot('guesses', ...
xyz_from_target_index, ...
feature_struct_from_guess_index, ...
heckbert_origin_xyz, ...
stack_shape_xyz, ...
spacing_at_zoom_level_xyz, ...
substack_mip, ...
padded_substack_origin_xyz(1:2), ...
spacing_at_zoom_level_xyz(1:2) ) ;
% % Figure out what targets are in what guesses
% matching_guess_index_from_target_index = ...
% match_targets_and_components(xyz_from_target_index, feature_struct_from_guess_index, origin_at_zoom_level_xyz, spacing_at_zoom_level_xyz) ;
% matching_target_index_from_guess_index = invert_partial_map_array(matching_guess_index_from_target_index, guess_count) ;
% is_there_a_matched_guess_from_target_index = isfinite(matching_guess_index_from_target_index) ;
% is_there_a_matched_target_from_guess_index = isfinite(matching_target_index_from_guess_index) ;
%
%
% is_hit_from_target_index = is_there_a_matched_guess_from_target_index ;
% is_hit_from_guess_index = is_there_a_matched_target_from_guess_index ;
% is_miss_from_target_index = ~is_there_a_matched_guess_from_target_index ;
% is_chase_from_guess_index = ~is_there_a_matched_target_from_guess_index ;
%
% target_count
% guess_count
% guess_hit_count = sum(is_hit_from_target_index)
% assert( sum(is_hit_from_guess_index) == guess_hit_count ) ;
% guess_miss_count = sum(is_miss_from_target_index)
% guess_chase_count = sum(is_chase_from_guess_index)
%
% guess_precision = guess_hit_count / guess_count
% guess_recall = guess_hit_count / target_count
%
%
% % Plot the MIP image
% f = figure('color', 'w', 'name', 'targets-and-guesses') ;
% a = axes(f, 'YDir', 'reverse') ;
% image(a, 'CData', substack_mip, ...
% 'XData', [padded_substack_origin_xyz(1) padded_substack_far_corner_xyz(1)], ...
% 'YData', [padded_substack_origin_xyz(2) padded_substack_far_corner_xyz(2)], ...
% 'CDataMapping', 'scaled') ;
% colormap(gray(256)) ;
% axis image
% xlabel('x (um)') ;
% ylabel('y (um)') ;
%
% % plot each target, and each guess, and draw a line between matches
% hold on ;
% for target_index = 1 : target_count ,
% target_xyz = xyz_from_target_index(target_index,:) ;
% marker_color = fif(is_there_a_matched_guess_from_target_index(target_index), [0 0.5 1], [1 0 0]) ;
% plot(target_xyz(1), target_xyz(2), 'Marker', '+', 'Color', marker_color) ;
% %text(target_xyz(1)+5, target_xyz(2)+5, sprintf('t%d', target_index), 'Color', 0.5*[1 1 1]) ;
% end
% for guess_index = 1 : guess_count ,
% guess_xyz = feature_struct_from_guess_index(guess_index).centroidoid_xyz ;
% marker_color = fif(is_there_a_matched_target_from_guess_index(guess_index), [0 0.5 1], [1 0 0]) ;
% plot(guess_xyz(1), guess_xyz(2), 'Marker', 'o', 'MarkerSize', 6, 'Color', marker_color) ;
% %text(guess_xyz(1)-5, guess_xyz(2)-5, sprintf('g%d', guess_index), 'Color', 0.5*[1 1 1]) ;
% end
% for target_index = 1 : target_count ,
% target_xyz = xyz_from_target_index(target_index,:) ;
% if is_there_a_matched_guess_from_target_index(target_index) ,
% guess_index = matching_guess_index_from_target_index(target_index) ;
% guess_xyz = feature_struct_from_guess_index(guess_index).centroidoid_xyz ;
% plot([target_xyz(1) guess_xyz(1)], [target_xyz(2) guess_xyz(2)], 'Color', [0 0.5 1]) ;
% end
% end
% hold off ;
% % Considering just the cadidates, plot them in feature space, and
% % characterize each as hit/miss/chase/ball. This makes sense b/c we cast a wide net: every target is
% % also a candidate.
%
% is_positive_from_candidate_index = is_there_a_matched_target_from_candidate_index ;
% is_negative_from_candidate_index = ~is_there_a_matched_target_from_candidate_index ;
% positive_count_within_candidates = sum(is_positive_from_candidate_index)
% negative_count_within_candidates = sum(is_negative_from_candidate_index)
%
% is_test_positive_from_candidate_index = is_guess_from_candidate_index ;
% is_test_negative_from_candidate_index = ~is_guess_from_candidate_index ;
% test_positive_count_within_candidates = sum(is_test_positive_from_candidate_index)
% test_negative_count_within_candidates = sum(is_test_negative_from_candidate_index)
%
% is_hit_from_candidate_index = is_test_positive_from_candidate_index & is_positive_from_candidate_index ;
% is_miss_from_candidate_index = is_test_negative_from_candidate_index & is_positive_from_candidate_index ;
% is_chase_from_candidate_index = is_test_positive_from_candidate_index & is_negative_from_candidate_index ;
% is_ball_from_candidate_index = is_test_negative_from_candidate_index & is_negative_from_candidate_index ;
%
% hit_count_within_candidates = sum(is_hit_from_candidate_index)
% miss_count_within_candidates = sum(is_miss_from_candidate_index)
% chase_count_within_candidates = sum(is_chase_from_candidate_index)
% ball_count_within_candidates = sum(is_ball_from_candidate_index)
%
% precision_within_candidates = hit_count_within_candidates / test_positive_count_within_candidates
% recall_within_candidates = hit_count_within_candidates / positive_count_within_candidates
%
% hit_rate_within_candidates = hit_count_within_candidates / positive_count_within_candidates
% ball_rate_within_candidates = ball_count_within_candidates / negative_count_within_candidates
%
%
%
% % Plot the targets in the (very wimpy) feature space
% f = figure('Color', 'w', 'Name', 'features') ;
% a = axes(f) ;
% h_hit = plot3(max_intensity_from_candidate_index(is_hit_from_candidate_index), ...
% voxel_count_from_candidate_index(is_hit_from_candidate_index), ...
% sqrt_condition_number_from_candidate_index(is_hit_from_candidate_index), ...
% 'Marker', '.', 'Color', [0 0.3 1], 'LineStyle', 'none') ;
% hold on ;
% h_miss = plot3(max_intensity_from_candidate_index(is_miss_from_candidate_index), ...
% voxel_count_from_candidate_index(is_miss_from_candidate_index), ...
% sqrt_condition_number_from_candidate_index(is_miss_from_candidate_index), ...
% 'Marker', 'd', 'Color', [1 0 0], 'LineStyle', 'none') ;
% h_chase = plot3(max_intensity_from_candidate_index(is_chase_from_candidate_index), ...
% voxel_count_from_candidate_index(is_chase_from_candidate_index), ...
% sqrt_condition_number_from_candidate_index(is_chase_from_candidate_index), ...
% 'Marker', 'd', 'Color', [138 43 226]/255, 'LineStyle', 'none') ;
% h_ball = plot3(max_intensity_from_candidate_index(is_ball_from_candidate_index), ...
% voxel_count_from_candidate_index(is_ball_from_candidate_index), ...
% sqrt_condition_number_from_candidate_index(is_ball_from_candidate_index), ...
% 'Marker', '.', 'Color', [0 0.8 0], 'LineStyle', 'none') ;
% hold off ;
% xlabel('Max GFP signal (counts)') ;
% ylabel('Voxel count') ;
% zlabel('SD ratio') ;
% %xlim([0 2^16]) ;
% %ylim([0 2^16]) ;
% handles = zeros(1,0) ;
% legend_labels = cell(1,0) ;
% if ~isempty(h_hit) ,
% handles(1, end+1) = h_hit ;
% legend_labels{1, end+1} = 'hit' ;
% end
% if ~isempty(h_miss) ,
% handles(1, end+1) = h_miss ;
% legend_labels{1, end+1} = 'miss' ;
% end
% if ~isempty(h_chase) ,
% handles(1, end+1) = h_chase ;
% legend_labels{1, end+1} = 'chase' ;
% end
% if ~isempty(h_ball) ,
% handles(1, end+1) = h_ball ;
% legend_labels{1, end+1} = 'ball' ;
% end
%
% legend(handles, legend_labels, 'Location', 'northwest') ;
% axis vis3d
% grid on
% camproj perspective
%
%