forked from erhanbas/navigator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
explore_skels_and_frags_2019_04_17.m
183 lines (151 loc) · 9.58 KB
/
explore_skels_and_frags_2019_04_17.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
sample_date = '2019-04-17' ;
this_file_path = mfilename('fullpath') ;
this_folder_path = fileparts(this_file_path) ;
%output_folder_path = sprintf('/groups/mousebrainmicro/mousebrainmicro/cluster/%s/carver', sample_date) ;
output_folder_path = fullfile(this_folder_path, sample_date) ;
trees_folder_path = sprintf('/nrs/mouselight/cluster/classifierOutputs/%s/build-brain-output/full', sample_date) ; % these are .swcs for this sample
fragments_folder_path = sprintf('/nrs/mouselight/cluster/classifierOutputs/%s/build-brain-output/frags-with-at-least-5-points', sample_date) ;
consensus_swcs_folder_path = sprintf('/groups/mousebrainmicro/mousebrainmicro/shared_tracing/Finished_Neurons/%s', sample_date) ;
sample_folder_path = sprintf('/nrs/mouselight/SAMPLES/%s', sample_date) ;
skeleton_folder_path = sprintf('/nrs/mouselight/cluster/classifierOutputs/%s/skeletonization', sample_date) ;
% Get the metadata for the rendered sample
[shape_xyz, origin, spacing, jaws_origin] = load_sample_shape_origin_and_spacing(sample_folder_path);
% Load the fragments from disk
all_fragment_xyzs_in_erhan_coords = ...
compute_or_read_from_memo(output_folder_path, ...
'all_fragment_xyzs_in_erhan_coords', ...
@()(read_all_fragment_centerpoints(fragments_folder_path))) ;
% Check that the fragment nodes are on the grid as we expect
% all_fragment_ijks_unrounded = (all_fragment_xyzs_in_erhan_coords - origin) ./ spacing + 0.5 ; % these should be one-based ijks
% all_fragment_ijks = round(all_fragment_ijks_unrounded) ; % these should be one-based ijks
% fragment_offsets = all_fragment_ijks_unrounded - all_fragment_ijks ;
% assert( all( all( abs(fragment_offsets) < spacing/1024 ) ) ) ; % the offsets should just be floating-point error
fraction_of_fragment_nodes_at_voxel_centers = fraction_of_xyzs_at_voxel_centers_using_erhan_conventions(all_fragment_xyzs_in_erhan_coords, spacing, origin)
assert( fraction_of_fragment_nodes_at_voxel_centers == 1) ;
% Convert fragment coords to JaWS coords
all_fragment_ijks_unrounded = (all_fragment_xyzs_in_erhan_coords - origin) ./ spacing + 0.5 ; % these should be one-based ijks
%all_fragment_ijks_unrounded = (all_fragment_xyzs_in_erhan_coords - origin) ./ spacing + 1.5 ; % seems like maybe this is correct?
all_fragment_ijks = round(all_fragment_ijks_unrounded) ; % these should be one-based ijks
all_fragment_xyzs = jaws_origin + spacing .* (all_fragment_ijks-1) ; % um, n x 3
% Get the skeleton graph, either from .txt files or from mat
graph_file_path = fullfile(output_folder_path, 'skeleton-as-graph.mat') ;
if exist(graph_file_path, 'file') ,
load(graph_file_path, 'skeleton_graph', 'skeleton_ijks') ;
else
[skeleton_graph, skeleton_ijks] = load_skeleton_graph_from_txt_files(skeleton_folder_path, shape_xyz) ; % these ijks are also one-based
save(graph_file_path, 'skeleton_graph','skeleton_ijks', '-v7.3') ;
end
% What fraction of fragment points are also skeleton points, looking at ijk
% coords
is_fragment_a_skeleton_point_from_ijk = is_point_drawn_from_pool(all_fragment_ijks, skeleton_ijks, [1 1 1]) ;
fraction_fragment_points_that_are_skeleton_points_from_ijk = mean(is_fragment_a_skeleton_point_from_ijk)
% What if you shift the fragment points by 1 in each dim?
is_shifted_fragment_a_skeleton_point_from_ijk = is_point_drawn_from_pool(all_fragment_ijks+1, skeleton_ijks, [1 1 1]) ;
fraction_shifted_fragment_points_that_are_skel_points_from_ijk = mean(is_shifted_fragment_a_skeleton_point_from_ijk)
skeleton_xyzs = jaws_origin + spacing .* (skeleton_ijks-1) ; % um, n x 3
% skeleton_ijks are 1-based, we want to map them to voxel centers
% in JaWS coordinates
% % Write the skeleton to json file, for Will & Jan
% graph_as_json_file_path = fullfile(output_folder_path, 'skeleton-as-graph.json') ;
% if ~exist(graph_as_json_file_path, 'file') ,
% json_ready_skeleton = struct() ;
% json_ready_skeleton.ijks = skeleton_ijks-1 ; % convert to zero-based indexing
% edges_with_third_col = table2array(skeleton_graph.Edges)-1 ; % convert to zero-based indexing
% json_ready_skeleton.edges = edges_with_third_col(:,1:2) ;
% json_ready_skeleton.jaws_origin = jaws_origin ;
% json_ready_skeleton.spacing = spacing ;
% json_ready_skeleton.origin = origin ;
% json_ready_skeleton.shape_xyz = shape_xyz ;
% json_ready_skeleton.xyz_from_ijk_formula = 'xyzs = jaws_origin + spacing .* ijks' ;
% json_text = jsonencode(json_ready_skeleton) ;
% dump_string_to_file(graph_as_json_file_path, json_text) ;
% end
% What fraction of fragment points are also skeleton points
is_fragment_a_skeleton_point = is_point_drawn_from_pool(all_fragment_xyzs, skeleton_xyzs, spacing) ;
fraction_fragment_points_that_are_skeleton_points = mean(is_fragment_a_skeleton_point)
% Make fragment points, skeleton point indices
fragments_kd_tree = KDTreeSearcher(all_fragment_xyzs) ;
skeleton_kd_tree = KDTreeSearcher(skeleton_xyzs) ;
%%
% Pick a random fragment point, plot all the fragment and skeleton points
% nearby.
fragment_node_count = size(all_fragment_xyzs, 1)
fragment_node_index = randi(fragment_node_count, 1)
test_xyz = all_fragment_xyzs(fragment_node_index, :)
r = 10 ; % um
indices_of_nearby_fragment_points_as_cell = fragments_kd_tree.rangesearch(test_xyz, r) ;
indices_of_nearby_fragment_points = indices_of_nearby_fragment_points_as_cell{1} ;
xyzs_of_nearby_fragment_points = all_fragment_xyzs(indices_of_nearby_fragment_points, :) ;
indices_of_nearby_skeleton_points_as_cell = skeleton_kd_tree.rangesearch(test_xyz, r) ;
indices_of_nearby_skeleton_points = indices_of_nearby_skeleton_points_as_cell{1} ;
xyzs_of_nearby_skeleton_points = skeleton_xyzs(indices_of_nearby_skeleton_points, :) ;
f = figure('Color', 'w') ;
a = axes(f) ;
l1 = line_in_3d_bang(a, xyzs_of_nearby_fragment_points-test_xyz, 'LineStyle', 'none', 'Marker', '.', 'Color', 'b') ;
l2 = line_in_3d_bang(a, xyzs_of_nearby_skeleton_points-test_xyz, 'LineStyle', 'none', 'Marker', 'o', 'Color', 'r') ;
l0 = line_in_3d_bang(a, test_xyz-test_xyz, 'LineStyle', 'none', 'Marker', '.', 'MarkerSize', 12, 'Color', 'b') ;
a.DataAspectRatio=[1 1 1] ;
view(3) ;
axis vis3d
xlabel('x (um)') ;
ylabel('y (um)') ;
zlabel('z (um)') ;
drawnow;
xlim(1.01*r*[-1 +1])
ylim(1.01*r*[-1 +1])
zlim(1.01*r*[-1 +1])
%%
desired_sample_count = 100 ;
[sample_stacks, sample_radius] = sample_rendered_data(sample_folder_path, all_fragment_ijks, desired_sample_count) ;
mean_sample_stack = mean(sample_stacks, 4) ;
mean_sample_stack_xy_slice = mean_sample_stack(:,:,sample_radius(3)+1) ;
f = figure('Color', 'w') ;
a = axes(f, 'DataAspectRatio', [1 1 1]) ;
imagesc(a, sample_radius(1)*[-1 +1], sample_radius(1)*[-1 +1], mean_sample_stack_xy_slice) ;
axis equal
axis tight
line(a, 'XData', 0, 'YData', 0, 'Marker', '.', 'MarkerSize', 12) ;
title('Fragments') ;
%%
desired_sample_count = 200 ;
[sample_stacks, sample_radius] = sample_rendered_data(sample_folder_path, skeleton_ijks, desired_sample_count) ;
mean_sample_stack = mean(sample_stacks, 4) ;
mean_sample_stack_xy_slice = mean_sample_stack(:,:,sample_radius(3)+1) ;
f = figure('Color', 'w') ;
a = axes(f, 'DataAspectRatio', [1 1 1]) ;
imagesc(a, sample_radius(1)*[-1 +1], sample_radius(1)*[-1 +1], mean_sample_stack_xy_slice) ;
axis equal
axis tight
line(a, 'XData', 0, 'YData', 0, 'Marker', '.', 'MarkerSize', 12) ;
title('Skeleton') ;
% sample_stack_xy_slices = squeeze(sample_stacks(:,:,sample_radius(3)+1,:)) ;
% f = figure('Color', 'w') ;
% a = axes(f, 'DataAspectRatio', [1 1 1]) ;
% imagesc(a, sample_radius(1)*[-1 +1], sample_radius(1)*[-1 +1], sample_stack_xy_slices(:,:,60)) ;
% axis equal
% axis tight
% line(a, 'XData', 0, 'YData', 0, 'Marker', '.', 'MarkerSize', 12) ;
% title('Skeleton') ;
%%
% Load the tree points from disk
all_tree_xyzs_in_erhan_coords = ...
compute_or_read_from_memo(output_folder_path, ...
'all_tree_xyzs_in_erhan_coords', ...
@()(read_all_fragment_centerpoints(trees_folder_path))) ;
% Check that the tree nodes are on the grid as we expect
% all_tree_ijks_unrounded = (all_tree_xyzs_in_erhan_coords - origin) ./ spacing + 0.5 ; % these should be one-based ijks
% all_tree_ijks = round(all_tree_ijks_unrounded) ; % these should be one-based ijks
% tree_offsets = all_tree_ijks_unrounded - all_tree_ijks ;
% assert( all( all( abs(tree_offsets) < spacing/1024 ) ) ) ; % the offsets should just be floating-point error
fraction_of_tree_nodes_at_voxel_centers = fraction_of_xyzs_at_voxel_centers_using_erhan_conventions(all_tree_xyzs_in_erhan_coords, spacing, origin)
% Convert tree coords to JaWS coords
all_tree_ijks_unrounded = (all_tree_xyzs_in_erhan_coords - origin) ./ spacing + 0.5 ; % these should be one-based ijks
all_tree_ijks = round(all_tree_ijks_unrounded) ; % these should be one-based ijks
all_tree_xyzs = jaws_origin + spacing .* (all_tree_ijks-1) ; % um, n x 3
% What fraction of tree points are also skeleton points, looking at ijk
% coords
is_tree_a_skeleton_point_from_ijk = is_point_drawn_from_pool(all_tree_ijks, skeleton_ijks, [1 1 1]) ;
fraction_tree_points_that_are_skeleton_points_from_ijk = mean(is_tree_a_skeleton_point_from_ijk)
% What if you shift the tree points by 1 in each dim?
is_shifted_tree_a_skeleton_point_from_ijk = is_point_drawn_from_pool(all_tree_ijks+1, skeleton_ijks, [1 1 1]) ;
fraction_shifted_tree_points_that_are_skel_points_from_ijk = mean(is_shifted_tree_a_skeleton_point_from_ijk)