-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathya_drawProbes_bezierCurves.m
1924 lines (1498 loc) · 73.5 KB
/
ya_drawProbes_bezierCurves.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function ya_drawProbes_bezierCurves(tv, av, st, registeredImage, outputDir, screenOrientation)
% based on:
% AP_get_probe_histology(tv,av,st,slice_im_path)
%
% QQ: fine-tune button locations: they are hard-coded and only work work for 1 screen type
if nargin < 6 || isempty(screenOrientation)
screenOrientation = 'portrait';
end
%% create the GUI figure
% Initialize guidata
warning off;
gui_data = struct;
gui_data.tv = tv;
gui_data.av = av;
gui_data.st = st;
% Query number of probes from user
gui_data.n_probes = str2num(cell2mat(inputdlg('How many probes?')));
% Load in slice images
gui_data.slice_im_path = outputDir;
for curr_slice = 1:size(registeredImage, 3)
gui_data.slice_im{curr_slice} = registeredImage(:, :, curr_slice);
end
% Load corresponding CCF slices
ccf_slice_fn = [outputDir, filesep, '/manual/histology_ccf.mat'];
load(ccf_slice_fn);
gui_data.histology_ccf = histology_ccf;
% Load histology/CCF alignment
ccf_alignment_fn = [outputDir, filesep, '/manual/atlas2histology_tform.mat'];
load(ccf_alignment_fn);
gui_data.histology_ccf_alignment = atlas2histology_tform;
% Warp area labels by histology alignment
gui_data.histology_aligned_av_slices = cell(length(gui_data.slice_im), 1);
for curr_slice = 1:length(gui_data.histology_ccf)
curr_av_slice = squeeze(gui_data.histology_ccf(curr_slice).av_slices);
curr_av_slice(isnan(curr_av_slice)) = 1;
curr_slice_im = gui_data.slice_im{curr_slice};
tform = affine2d;
tform.T = gui_data.histology_ccf_alignment{curr_slice};
tform_size = imref2d([size(curr_slice_im, 1), size(curr_slice_im, 2)]);
gui_data.histology_aligned_av_slices{curr_slice} = ...
imwarp(curr_av_slice, tform, 'nearest', 'OutputView', tform_size);
end
% Create figure, set button functions
gui_fig = uifigure('KeyPressFcn', @keypress, 'Color', 'k');
% Row and col position for other GUI elements
if strcmp(screenOrientation, 'portrait')
g = uigridlayout(gui_fig, [7, 8], 'BackgroundColor', 'k'); % 7 rows, 8 columns grid layout
g.RowHeight = {'fit', 'fit', '6x', '1x', 'fit', '2x', 'fit'};
g.ColumnWidth = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
% row start, col start, col stop, row stop
mainAxisPosition = [3, 1, 8, 4];
autoButtonPosition = [2, 1, 2];
brightnessSliderPosition = [2, 3];
brightnessTextPosition = [1, 3];
contrastSliderPosition = [2, 4];
contrastTextPosition = [1, 4];
pointSliderPosition = [2, 5];
pointTextPosition = [1, 5];
saveButtonPosition = [2, 6];
loadButtonPosition = [2, 7];
plotButtonPosition = [2, 8];
delButtonPosition = [7, 1, 2];
startButtonPosition = [7, 3, 4];
stopButtonPosition = [7, 5, 6];
probePointsBoxPosition = [6, 1, 6];
probeDropdownBoxPosition = [5, 1, 6];
legendPosition = [5, 7, 8, 7];
else % landscape mode
g = uigridlayout(gui_fig, [5, 10], 'BackgroundColor', 'k'); % grid layout
g.RowHeight = {'fit', 'fit', 'fit', '2x', 'fit'};
g.ColumnWidth = {'10x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
% row start, col start, col stop, row stop
mainAxisPosition = [1, 1, 2, 5];
autoButtonPosition = [2, 3, 4];
brightnessSliderPosition = [2, 5];
brightnessTextPosition = [1, 5];
contrastSliderPosition = [2, 6];
contrastTextPosition = [1, 6];
pointSliderPosition = [2, 7];
pointTextPosition = [1, 7];
saveButtonPosition = [2, 8];
loadButtonPosition = [2, 9];
plotButtonPosition = [2, 10];
delButtonPosition = [5, 3, 4];
startButtonPosition = [5, 5, 6];
stopButtonPosition = [5, 7, 8];
probePointsBoxPosition = [4, 3, 8];
probeDropdownBoxPosition = [3, 3, 8];
legendPosition = [3, 9, 10, 5];
end
% Position and size of histology axis
gui_data.histology_ax = uiaxes(g);
gui_data.histology_ax.Layout.Row = [mainAxisPosition(1), mainAxisPosition(4)];
gui_data.histology_ax.Layout.Column = [mainAxisPosition(2), mainAxisPosition(3)];
gui_data.histology_ax.YDir = 'reverse';
% auto contrast/brightness button
gui_data.auto_contrast_btn = uibutton(g, 'Text', 'Auto brightness + contrast', 'BackgroundColor', [1, 0.08, 0.58], 'FontColor', [1, 1, 1], 'FontSize', 14);
gui_data.auto_contrast_btn.ButtonPushedFcn = @(varargin) autoContrastButtonPushed(gui_fig);
gui_data.auto_contrast_btn.Layout.Row = autoButtonPosition(1);
gui_data.auto_contrast_btn.Layout.Column = [autoButtonPosition(2), autoButtonPosition(3)];
% brightness slider
gui_data.brightness_beta = 0;
gui_data.brightness_slider = uislider(g, 'Limits', [-100, 100], 'Value', gui_data.brightness_beta, 'FontColor', 'w');
gui_data.brightness_slider.ValueChangedFcn = @(varargin) brightnessButtonPushed(gui_fig);
gui_data.brightness_slider.Layout.Row = brightnessSliderPosition(1);
gui_data.brightness_slider.Layout.Column = brightnessSliderPosition(2);
% Brightness text
gui_data.brightness_text = uilabel(g, 'Text', 'Brightness %', 'BackgroundColor', [0, 0, 0], 'FontColor', [1, 1, 1], 'FontSize', 14);
gui_data.brightness_text.Layout.Row = brightnessTextPosition(1);
gui_data.brightness_text.Layout.Column = brightnessTextPosition(2);
% Contrast slider
gui_data.contrast_alpha = 1;
gui_data.contrast_slider = uislider(g, 'Limits', [0, 1], 'Value', gui_data.contrast_alpha, 'FontColor', 'w');
gui_data.contrast_slider.ValueChangedFcn = @(varargin) contrastButtonPushed(gui_fig);
gui_data.contrast_slider.Layout.Row = contrastSliderPosition(1);
gui_data.contrast_slider.Layout.Column = contrastSliderPosition(2);
% Contrast text
gui_data.contrast_text = uilabel(g, 'Text', 'Contrast', 'BackgroundColor', [0, 0, 0], 'FontColor', [1, 1, 1], 'FontSize', 14);
gui_data.contrast_text.Layout.Row = contrastTextPosition(1);
gui_data.contrast_text.Layout.Column = contrastTextPosition(2);
% point size slider
gui_data.point_size = 20;
gui_data.point_size_slider = uislider(g, 'Limits', [1, 100], 'Value', gui_data.point_size, 'FontColor', [1, 1, 1], 'FontSize', 14);
gui_data.point_size_slider.ValueChangedFcn = @(varargin) pointSizeButtonPushed(gui_fig);
gui_data.point_size_slider.Layout.Row = pointSliderPosition(1);
gui_data.point_size_slider.Layout.Column = pointSliderPosition(2);
% Point size text
gui_data.point_size_text = uilabel(g, 'Text', 'Point size (px)', 'BackgroundColor', [0, 0, 0], 'FontColor', [1, 1, 1], 'FontSize', 14);
gui_data.point_size_text.Layout.Row = pointTextPosition(1);
gui_data.point_size_text.Layout.Column = pointTextPosition(2);
% Save current datapoints
gui_data.save_btn = uibutton(g, 'Text', 'Save', 'BackgroundColor', [1, 0.843, 0], 'FontSize', 14); % Gold color
gui_data.save_btn.ButtonPushedFcn = @(varargin) saveButtonPushed(gui_fig);
gui_data.save_btn.Layout.Row = saveButtonPosition(1);
gui_data.save_btn.Layout.Column = saveButtonPosition(2);
% Load previous datapoints
gui_data.load_btn = uibutton(g, 'Text', 'Load', 'BackgroundColor', [1, 0.27, 0], 'FontColor', [0.3, 0.3, 0.3], 'FontSize', 14); % OrangeRed color
gui_data.load_btn.ButtonPushedFcn = @(varargin) loadButtonPushed(gui_fig);
gui_data.load_btn.Layout.Row = loadButtonPosition(1);
gui_data.load_btn.Layout.Column = loadButtonPosition(2);
% Plot probes
gui_data.plot_btn = uibutton(g, 'Text', 'Plot', 'BackgroundColor', [1, 0.65, 0], 'FontColor', [0.3, 0.3, 0.3], 'FontSize', 14); % Orange color
gui_data.plot_btn.ButtonPushedFcn = @(varargin) plotProbeButtonPushed(gui_fig);
gui_data.plot_btn.Layout.Row = plotButtonPosition(1);
gui_data.plot_btn.Layout.Column = plotButtonPosition(2);
% Set up axis for histology image
gui_data.curr_slice = 150;
gui_data.visibility = 1;
gui_data.fit_visibility = 0;
% Set properties of histology_ax directly
gui_data.histology_ax.YDir = 'reverse'; % Reverse Y-Axis direction if needed
gui_data.histology_ax.Colormap = gray; % Set colormap to gray
gui_data.histology_ax.Visible = 'off'; % Turn off axis lines and labels
gui_data.histology_ax.DataAspectRatioMode = 'manual';
gui_data.histology_ax.DataAspectRatio = [1, 1, 1]; % Maintain aspect ratio
% Image processing and displaying
gui_data.slice_im{1}(gui_data.slice_im{1} > 1200) = 0;
img = imadjust(gui_data.slice_im{1}, [0.1, 0.8]);
img(img == 1) = 0;
% Display image in the specified axes
gui_data.histology_im_h = image(gui_data.histology_ax, img);
colormap(gui_data.histology_ax, gray); % Apply colormap again to ensure consistency
% Ensure 'hold on' for histology axes
hold(gui_data.histology_ax, 'on');
% Create title to write area in
gui_data.histology_ax_title = title(gui_data.histology_ax, '', 'FontSize', 14, 'Color', 'white');
% Initialize probe points
gui_data.probe_color = ya_getColors(gui_data.n_probes);
gui_data.probe_points_histology = cell(length(gui_data.slice_im), gui_data.n_probes);
for iProbe = 1:gui_data.n_probes
% Use the 'Parent' property to specify the target axes
gui_data.probe_points{iProbe} = plot(gui_data.histology_ax, NaN, NaN, 'ro'); %, 'ButtonDownFcn', @(src, event)startDragFcn(src, event, gui_fig), 'Tag', 'draggable');
gui_data.start_points_scatter{iProbe} = plot(gui_data.histology_ax, NaN, NaN, 'rx');
gui_data.stop_points_scatter{iProbe} = plot(gui_data.histology_ax, NaN, NaN, 'rx');
end
gui_data.inflection_points_scatter = gobjects(gui_data.n_probes, 1);
gui_data.probe_fit_lines = gobjects(gui_data.n_probes, 1);
gui_data.stop_points = nan(gui_data.n_probes, 3);
gui_data.start_points = nan(gui_data.n_probes, 3);
% Create a legend
gui_data.legendAxes = uiaxes(g, 'Box', 'off', 'XTick', [], 'YTick', [], 'XColor', 'none', 'YColor', 'none', 'Color', 'k');
gui_data.legendAxes.Layout.Row = [legendPosition(1), legendPosition(4)];
gui_data.legendAxes.Layout.Column = [legendPosition(2), legendPosition(3)];
hold(gui_data.legendAxes, 'on');
% Define the positions for the legend items
nProbes = size(gui_data.probe_color, 1);
% Plot each probe's color in the legend
gui_data.probeLegendText = gobjects(nProbes, 1);
for i = 1:nProbes
if strcmp(screenOrientation, 'portrait')
n_probe_per_col = 10;
horiz_spacing_factor = 0.3;
vert_spacing_factor = 0.08;
else
n_probe_per_col = 25;
horiz_spacing_factor = 0.5;
vert_spacing_factor = 0.03;
end
gui_data.probeLegendText(i) = text(gui_data.legendAxes, (floor((i - 1)/n_probe_per_col))*horiz_spacing_factor+0.3,...
0.9-vert_spacing_factor*(i - (floor((i - 1)/n_probe_per_col))*n_probe_per_col), ['Probe ', num2str(i)], 'Color', gui_data.probe_color(i, :), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'FontSize', 14);
end
hold(gui_data.legendAxes, 'off');
% Initialize plots for control points and Bezier curve
gui_data.bezier_control_points = cell(gui_data.n_probes, 1);
gui_data.bezier_curves = gobjects(gui_data.n_probes, 1);
% Points table
gui_data.points_table = uilistbox(g, 'FontColor', [1, 1, 1], 'BackgroundColor', [0.1, 0.1, 0.1]);
gui_data.points_table.ValueChangedFcn = @(src, event)selectPoint(gui_fig, src, event);
gui_data.points_table.Layout.Row = probePointsBoxPosition(1);
gui_data.points_table.Layout.Column = [probePointsBoxPosition(2), probePointsBoxPosition(3)];
% Delete Point Button
gui_data.deletePointButton = uibutton(g, 'Text', 'Delete Point', 'BackgroundColor', [0.86, 0.08, 0.24]); % Crimson color
gui_data.deletePointButton.ButtonPushedFcn = @(src, event)deleteSelectedPoint(src, event, gui_fig);
gui_data.deletePointButton.Layout.Row = delButtonPosition(1);
gui_data.deletePointButton.Layout.Column = [delButtonPosition(2), delButtonPosition(3)];
% Start and stop points
gui_data.probe_points_start = gobjects(gui_data.n_probes, 1);
gui_data.probe_points_start_position = nan(gui_data.n_probes, 3);
gui_data.probe_points_stop = gobjects(gui_data.n_probes, 1);
gui_data.probe_points_stop_position = nan(gui_data.n_probes, 3);
% Start Point Button
gui_data.startPt = uibutton(g, 'Text', 'Set starting point', 'BackgroundColor', [0.6, 0.98, 0.6]); % PaleGreen color
gui_data.startPt.ButtonPushedFcn = @(varargin) startPtButtonPushed(gui_fig);
gui_data.startPt.Layout.Row = startButtonPosition(1);
gui_data.startPt.Layout.Column = [startButtonPosition(2), startButtonPosition(3)];
% Stop Point Button
gui_data.stopPt = uibutton(g, 'Text', 'Set endpoint', 'BackgroundColor', [0.94, 0.5, 0.5]); % LightCoral color
gui_data.stopPt.ButtonPushedFcn = @(varargin) stopPtButtonPushed(gui_fig);
gui_data.stopPt.Layout.Row = stopButtonPosition(1);
gui_data.stopPt.Layout.Column = [stopButtonPosition(2), stopButtonPosition(3)];
% Bezier Parameters
gui_data.pointSelected = false;
gui_data.hHighlighted = scatter(gui_data.histology_ax, NaN, NaN, 40, 'b*', 'ButtonDownFcn', @(src, event)startDragFcn(src, event, gui_fig), 'Tag', 'draggable');
gui_data.selected_row = NaN;
% initialize probe buttons
gui_data.prev_spline_fit = repmat(0.1, gui_data.n_probes, 1);
% curr_probe
gui_data.curr_probe = 1;
% Assuming gui_data.probe_color holds RGB values for each probe's color
probes_list = arrayfun(@(x) sprintf('Probe %d', x), 1:gui_data.n_probes, 'UniformOutput', false);
% Probe dropdown menu
gui_data.probe_dropdown = uidropdown(g, 'Items', probes_list, 'FontColor', [1, 1, 1], 'BackgroundColor', [0.2, 0.2, 0.2]);
gui_data.probe_dropdown.ValueChangedFcn = @(src, event)updateProbe(gui_fig);
gui_data.probe_dropdown.Layout.Row = probeDropdownBoxPosition(1);
gui_data.probe_dropdown.Layout.Column = [probeDropdownBoxPosition(2), probeDropdownBoxPosition(3)];
% Save gui_data
guidata(gui_fig, gui_data);
% Update the slice
update_slice(gui_fig);
end
%% key press
function keypress(gui_fig, eventdata)
% Get guidata
gui_data = guidata(gui_fig);
switch eventdata.Key
% left/right: move slice
case 'leftarrow'
gui_data.curr_slice = max(gui_data.curr_slice-1, 1);
guidata(gui_fig, gui_data);
update_slice(gui_fig);
case 'rightarrow'
gui_data.curr_slice = ...
min(gui_data.curr_slice+1, length(gui_data.slice_im));
guidata(gui_fig, gui_data);
update_slice(gui_fig);
case 'add' % 'add' is the numpad + key
curr_probe = gui_data.probe_dropdown.Value;
update_curr_probe(gui_fig, curr_probe)
case '+'
curr_probe = gui_data.probe_dropdown.Value;
update_curr_probe(gui_fig, curr_probe)
case 'insert' % add a probe
addProbe(gui_fig)
case 'uparrow'
gui_data.curr_probe = min(gui_data.n_probes, gui_data.curr_probe+1);
guidata(gui_fig, gui_data);
updateProbe_slider(gui_fig, gui_data.curr_probe)
case 'downarrow'
gui_data.curr_probe = max(1, gui_data.curr_probe-1);
guidata(gui_fig, gui_data);
updateProbe_slider(gui_fig, gui_data.curr_probe)
case 'z' % zoom in
zoomInFunction(gui_fig);
case 'x' % zoom out
zoomOutFunction(gui_fig);
case 'p' % pan
panFunction(gui_fig);
case 'r' % reset
resetFunction(gui_fig);
case 'escape'
opts.Default = 'Yes';
opts.Interpreter = 'tex';
user_confirm = questdlg('\fontsize{15} Save and quit?', 'Confirm exit', opts);
if strcmp(user_confirm, 'Yes')
saveButtonPushed(gui_fig)
end
end
end
%% general functions: update probe, slice, create fit, probe buttons
function update_curr_probe(gui_fig, curr_probe)
% Get guidata
gui_data = guidata(gui_fig);
gui_data.curr_probe = ismember(gui_data.probe_dropdown.Items, curr_probe);
curr_probe = gui_data.curr_probe;
set(gui_data.histology_ax_title, 'String', ['Draw control points for probe ', num2str(curr_probe)]);
% Specify the axes for drawpoint
curr_point = drawpoint('Parent', gui_data.histology_ax);
% Storing Bezier control points instead of probe points
gui_data.bezier_control_points{curr_probe} = ...
+[gui_data.bezier_control_points{curr_probe}; curr_point.Position, gui_data.curr_slice];
[~, sortIdx] = sort(gui_data.bezier_control_points{curr_probe}(:, 3)); % sort points by slice # 1+
gui_data.bezier_control_points{curr_probe} = ...
gui_data.bezier_control_points{curr_probe}(sortIdx, :);
set(gui_data.histology_ax_title, 'String', ...
['Arrows to move, Number to draw probe [', num2str(1), ':', num2str(gui_data.n_probes), '], Esc to save/quit']);
% Plot Bezier curve if there are enough control points (at least 4 for a cubic Bezier curve)
if size(gui_data.bezier_control_points{curr_probe}, 1) >= 4
% delete any previous Bezier curves
delete(gui_data.bezier_curves(curr_probe));
% Compute the new Bezier curve in 3D
t = linspace(0, 1, 1000);
B = bezier_curve(t, gui_data.bezier_control_points{gui_data.curr_probe});
% Filter based on z-slice
z_slice_tolerance = 0.1; % define a tolerance for how close the z-value of the Bezier curve has to be to z_slice to be displayed
indices = find(abs(B(:, 3)-gui_data.curr_slice) < z_slice_tolerance); % find indices of points on the Bezier curve close to z_slice
B_slice = B(indices, :);
% Ensure plot is drawn in the correct axes
gui_data.bezier_curves(curr_probe) = plot(gui_data.histology_ax, B_slice(:, 1), B_slice(:, 2), 'Color', gui_data.probe_color(curr_probe, :), 'LineWidth', 2);
end
% delete any previous control points and replot
curr_point.delete;
thesePoints = gui_data.bezier_control_points{curr_probe}(:, 3) == gui_data.curr_slice;
% Ensure set command is applied to the correct plot object
if isgraphics(gui_data.probe_points{curr_probe})
set(gui_data.probe_points{curr_probe}, 'XData', gui_data.bezier_control_points{curr_probe}(thesePoints, 1), 'YData', gui_data.bezier_control_points{curr_probe}(thesePoints, 2), ...
'MarkerFaceColor', gui_data.probe_color(curr_probe, :), 'MarkerEdgeColor', gui_data.probe_color(curr_probe, :));
end
% store points
gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe} = ...
gui_data.bezier_control_points{gui_data.curr_probe}(gui_data.bezier_control_points{gui_data.curr_probe}(:, 3) == gui_data.curr_slice, 1:2);
% Store the gui_data again
guidata(gui_fig, gui_data);
populate_points_table(gui_fig);
updateLegend(gui_fig)
end
function update_slice(gui_fig)
% Draw histology and CCF slice
% Get guidata
gui_data = guidata(gui_fig);
% Set next histology slice
set(gui_data.histology_im_h, 'CData', (gui_data.slice_im{gui_data.curr_slice})*gui_data.contrast_alpha+gui_data.brightness_beta)
% Clear any current lines, draw probe lines
for iProbe = 1:gui_data.n_probes
gui_data.bezier_curves(iProbe).delete;
end
%gui_data.probe_fit_lines.delete;
%gui_data.inflection_points_scatter.delete;
for curr_probe = 1:size(gui_data.probe_points_histology, 2)
try
if sum(gui_data.bezier_control_points{curr_probe}(:, 3) == gui_data.curr_slice) > 0
thesePoints = gui_data.bezier_control_points{curr_probe}(:, 3) == gui_data.curr_slice;
set(gui_data.probe_points{curr_probe}, 'XData', gui_data.bezier_control_points{curr_probe}(thesePoints, 1), 'YData', gui_data.bezier_control_points{curr_probe}(thesePoints, 2), ...
'MarkerFaceColor', gui_data.probe_color(curr_probe, :), 'MarkerEdgeColor', gui_data.probe_color(curr_probe, :));
t = linspace(0, 1, 1000);
B = bezier_curve(t, gui_data.bezier_control_points{curr_probe});
% Filter based on z-slice
z_slice_tolerance = 0.1; % define a tolerance for how close the z-value of the Bezier curve has to be to z_slice to be displayed
indices = find(abs(B(:, 3)-gui_data.curr_slice) < z_slice_tolerance); % find indices of points on the Bezier curve close to z_slice
B_slice = B(indices, :);
gui_data.bezier_curves(curr_probe) = plot(B_slice(:, 1), B_slice(:, 2), 'Color', gui_data.probe_color(curr_probe, :), 'Parent', gui_data.histology_ax, 'LineWidth', 2);
else
try
set(gui_data.probe_points{curr_probe}, 'XData', NaN, 'YData', NaN);
set(gui_data.bezier_curves(curr_probe), 'XData', NaN, 'YData', NaN);
catch
end
end
catch
end
end
set(gui_data.histology_ax_title, 'String', ...
['Arrows to move, Number to draw probe [', num2str(1), ':', num2str(gui_data.n_probes), '], Esc to save/quit']);
% Upload gui data
guidata(gui_fig, gui_data);
populate_points_table(gui_fig);
updateLegend(gui_fig)
end
function plot_probe(gui_data, probe_ccf)
% Plot probe trajectories
figure('Name', 'Probe trajectories');
axes_atlas = axes;
[~, brain_outline] = plotBrainGrid([], axes_atlas, [], 1);
set(axes_atlas, 'ZDir', 'reverse');
hold(axes_atlas, 'on');
axis vis3d equal off manual
view([-30, 25]);
caxis([0, 300]);
[ap_max, dv_max, ml_max] = size(gui_data.tv);
% xlim([-10,ml_max+10])
% ylim([-10,dv_max+10])
% zlim([-10,ap_max+10])
h = rotate3d(gca);
h.Enable = 'on';
for curr_probe = 1:length(probe_ccf)
% Plot points and line of best fit
slice_points = find(~cellfun(@isempty, gui_data.probe_points_histology(:, curr_probe)'));
if ~isempty(slice_points)
linear_fit = 0; %hard coded for now
spline_fit = 0;
if linear_fit
thesePoints = probe_ccf(curr_probe).points * 2.5; % QQ 2.5 to correct for atlas 25 um where we draw probes and 10 um in plotBrainGrid
r0 = mean(thesePoints, 1);
xyz = bsxfun(@minus, thesePoints, r0);
[~, ~, V] = svd(xyz, 0);
%V= permute(V, [3, 2, 1]);
histology_probe_direction = V(:, 1);
% (make sure the direction goes down in DV - flip if it's going up)
if histology_probe_direction(2) < 0
histology_probe_direction = -histology_probe_direction;
end
line_eval = [-1000, 1000];
probe_fit_line = bsxfun(@plus, bsxfun(@times, line_eval', histology_probe_direction'), r0);
plot3(thesePoints(:, 1), ...
thesePoints(:, 2), ...
thesePoints(:, 3), ...
'.', 'color', gui_data.probe_color(curr_probe, :), 'MarkerSize', 20);
line(probe_fit_line(:, 1), probe_fit_line(:, 2), probe_fit_line(:, 3), ...
'color', gui_data.probe_color(curr_probe, :), 'linewidth', 2)
elseif spline_fit
thesePoints = probe_ccf(curr_probe).points * 2.5; % QQ 2.5 to correct for atlas 25 um where we draw probes and 10 um in plotBrainGrid
plot3(thesePoints(:, 1), ...
thesePoints(:, 2), ...
thesePoints(:, 3), ...
'.', 'color', gui_data.probe_color(curr_probe, :), 'MarkerSize', 20);
x = thesePoints(:, 1);
y = thesePoints(:, 2);
z = thesePoints(:, 3);
% (spline)
t = (1:length(x))'; % Creating a parameter t based on the number of data points
% Creating fittype objects for smooth spline with smoothing parameter
ft = fittype('smoothingspline');
% Define a smoothing parameter
smoothing_param = gui_data.spline_smoothness_factor(curr_probe); % Adjust this value between 0 and 1 to control the smoothness
% Fitting the curves with the smoothing parameter
fitresult_x = fit(t, x, ft, 'SmoothingParam', smoothing_param);
fitresult_y = fit(t, y, ft, 'SmoothingParam', smoothing_param);
fitresult_z = fit(t, z, ft, 'SmoothingParam', smoothing_param);
delta = 5;
% Step 3: Extending the Curve and Visualizing
% Extend the range of t for extrapolation
t_extended = [linspace(min(t)-delta, max(t)+delta, 1000)]'; % Adjust delta to control the extension amount
% Evaluate the extended spline at the new t values
x_new = fitresult_x(t_extended);
y_new = fitresult_y(t_extended);
z_new = fitresult_z(t_extended);
plot3(x_new, y_new, z_new, ...
'color', gui_data.probe_color(curr_probe, :), 'linewidth', 2)
else
thesePoints = probe_ccf(curr_probe).points *2.5; %.* [2.5,1,1]; % QQ 2.5 to correct for atlas 25 um where we draw probes and 10 um in plotBrainGrid
%plot3(thesePoints(:, 1), ...
% thesePoints(:, 2), ...
% thesePoints(:, 3), ...
% '.', 'color', gui_data.probe_color(curr_probe, :), 'MarkerSize', 20);
x = thesePoints(:, 1);
y = thesePoints(:, 2);
z = thesePoints(:, 3);
% (bezier)
t = linspace(0, 1, 1000); % Creating a parameter t based on the number of data points
Bezier_fit = bezier_curve(t, [x, y, z]);
plot3(Bezier_fit(:, 1), Bezier_fit(:, 2), Bezier_fit(:, 3), ...
'color', gui_data.probe_color(curr_probe, :), 'linewidth', 2)
end
end
end
% Plot probe areas
figure('Name', 'Trajectory areas', 'Color', 'k');
% (load the colormap - located in the repository, find by associated fcn)
filePath = fileparts(matlab.desktop.editor.getActiveFilename);
try
cmap_filename = [filePath, filesep, 'allen_ccf_colormap_2017.mat'];
load(cmap_filename);
catch
cmap_filename = '/home/julie/Dropbox/MATLAB/onPaths/YAHT/allen_ccf_colormap_2017.mat';
load(cmap_filename);
end
for curr_probe = 1:length(probe_ccf)
slice_points = find(~cellfun(@isempty, gui_data.probe_points_histology(:, curr_probe)'));
if ~isempty(slice_points)
curr_axes = subplot(1, gui_data.n_probes, curr_probe);
% flip probe direction if necessary - check dv (dim 2) goes down
if probe_ccf(curr_probe).trajectory_coords(1, 2) > probe_ccf(curr_probe).trajectory_coords(end, 2)
probe_ccf(curr_probe).trajectory_coords = flipud(probe_ccf(curr_probe).trajectory_coords);
probe_ccf(curr_probe).trajectory_areas = flipud(probe_ccf(curr_probe).trajectory_areas);
probe_ccf(curr_probe).points = flipud(probe_ccf(curr_probe).points);
end
trajectory_area_boundaries = ...
[1; find(diff(probe_ccf(curr_probe).trajectory_areas) ~= 0); length(probe_ccf(curr_probe).trajectory_areas)];
trajectory_area_centers = trajectory_area_boundaries(1:end-1) + diff(trajectory_area_boundaries) / 2;
for iArea = 1:size(trajectory_area_centers, 1)
trajectory_area_labels(iArea) = gui_data.st.acronym(gui_data.st.id == ...
probe_ccf(curr_probe).trajectory_areas(round(trajectory_area_centers(iArea))));
end
image(probe_ccf(curr_probe).trajectory_areas);
colormap(curr_axes, cmap);
caxis([1, size(cmap, 1)])
set(curr_axes, 'YTick', trajectory_area_centers, 'YTickLabels', trajectory_area_labels);
set(curr_axes, 'XTick', []);
if curr_probe == 1
title(['Probe ', num2str(curr_probe)], 'Color', gui_data.probe_color(curr_probe, :));
else
title([num2str(curr_probe)], 'Color', gui_data.probe_color(curr_probe, :));
end
set(gca, 'color', 'k');
set(gca, 'YColor', 'w');
set(gca, 'XColor', 'w');
set(gca, 'GridColor', 'w');
end
end
end
function updateProbe(gui_fig)
gui_data = guidata(gui_fig);
% Get the selected probe index
selected_probe_idx = ismember(gui_data.probe_dropdown.Items, get(gui_data.probe_dropdown, 'Value'));
% Set the current probe in gui_data
gui_data.curr_probe = selected_probe_idx;
% Refresh the points list
gui_data.selected_row = 1;
gui_data.points_table.Value = gui_data.points_table.Items(gui_data.selected_row);
% Save gui_data
guidata(gui_fig, gui_data);
populate_points_table(gui_fig);
updateLegend(gui_fig)
end
function updateProbe_slider(gui_fig, curr_probe)
gui_data = guidata(gui_fig);
% Get the selected probe index
set(gui_data.probe_dropdown, 'Value', curr_probe);
% Refresh the points list
gui_data.selected_row = 1;
gui_data.points_table.Value = gui_data.selected_row;
% Save gui_data
guidata(gui_fig, gui_data);
populate_points_table(gui_fig);
end
function startPtButtonPushed(gui_fig)
% Get guidata
gui_data = guidata(gui_fig);
% Set start button for current probe
curr_point = drawpoint;
gui_data.start_points(gui_data.curr_probe, :) = [curr_point.Position, gui_data.curr_slice];
%gui_data.start_points_scatter{gui_data.curr_probe}.delete;
%gui_data.start_points_scatter{gui_data.curr_probe} = plot(curr_point.Position(1), curr_point.Position(2), 'Color', gui_data.probe_color(gui_data.curr_probe, :))
set(gui_data.start_points_scatter{gui_data.curr_probe}, 'XData', curr_point.Position(1), 'YData', curr_point.Position(2), 'Color', gui_data.probe_color(gui_data.curr_probe, :), ...
'LineWidth', 2)
curr_point.delete;
% Upload gui data
guidata(gui_fig, gui_data);
end
function stopPtButtonPushed(gui_fig)
% Set start button for current probe
curr_point = drawpoint;
gui_data.stop_points(gui_data.curr_probe, :) = [curr_point.Position, gui_data.curr_slice];
%gui_data.start_points_scatter{gui_data.curr_probe}.delete;
%gui_data.start_points_scatter{gui_data.curr_probe} = plot(curr_point.Position(1), curr_point.Position(2), 'Color', gui_data.probe_color(gui_data.curr_probe, :))
set(gui_data.stop_points_scatter{gui_data.curr_probe}, 'XData', curr_point.Position(1), 'YData', curr_point.Position(2), 'Color', gui_data.probe_color(gui_data.curr_probe, :), ...
'LineWidth', 2)
curr_point.delete;
% Upload gui data
guidata(gui_fig, gui_data);
end
function updateLegend(gui_fig)
gui_data = guidata(gui_fig);
% Define the positions for the legend items
ncolProbes = size(gui_data.probe_color, 1);
if ncolProbes < gui_data.n_probes
hold(gui_data.legendAxes, 'on');
% add any missing
for i = ncolProbes + 1:nProbes
gui_data.probeLegendText{iProbe} = text(gui_data.legendAxes, (floor((i - 1)/10))*0.3, 0.9-0.08*(i - (floor((i - 1)/10))*10), ['Probe ', num2str(i)], 'Color', gui_data.probe_color(i, :), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'top', 'FontSize', 14);
end
hold(gui_data.legendAxes, 'off');
end
% update curr probe and slice probes
nonEmptyCells = ~cellfun(@isempty, gui_data.probe_points_histology(gui_data.curr_slice, :));
theseProbes = unique([nonEmptyCells, gui_data.curr_probe]);
for iProbe = 1:gui_data.n_probes
if ismember(iProbe, theseProbes)
gui_data.probeLegendText(iProbe).FontWeight = 'Bold';
gui_data.probeLegendText(iProbe).FontSize = 16;
else
try
gui_data.probeLegendText(iProbe).FontWeight = 'Normal';
gui_data.probeLegendText(iProbe).FontSize = 14;
catch
end
end
end
% gui_data.points_table.BackgroundColor = gui_data.probe_color(gui_data.curr_probe,:);
% if sum(gui_data.points_table.BackgroundColor) < 1.5
% gui_data.points_table.FontColor = [1,1,1];
% else
% gui_data.points_table.FontColor = [0,0,0];
% end
gui_data.probe_dropdown.BackgroundColor = gui_data.probe_color(gui_data.curr_probe, :);
if sum(gui_data.probe_dropdown.BackgroundColor) < 1.5
gui_data.probe_dropdown.FontColor = [1, 1, 1];
else
gui_data.probe_dropdown.FontColor = [0, 0, 0];
end
% Upload gui data
guidata(gui_fig, gui_data);
end
%% zoom/ pan function
function zoomInFunction(gui_fig)
gui_data = guidata(gui_fig);
zoom(gui_data.histology_ax, 'on');
% Upload gui data
guidata(gui_fig, gui_data);
end
function zoomOutFunction(gui_fig)
gui_data = guidata(gui_fig);
zoom(gui_data.histology_ax, 'on');
% Upload gui data
guidata(gui_fig, gui_data);
end
function panFunction(gui_fig)
gui_data = guidata(gui_fig);
zoom(gui_data.histology_ax, 'off');
pan(gui_data.histology_ax, 'on');
% Upload gui data
guidata(gui_fig, gui_data);
end
function resetFunction(gui_fig) %BUGGY
gui_data = guidata(gui_fig);
zoom(gui_data.histology_ax, 1);
% Upload gui data
guidata(gui_fig, gui_data);
end
%% bezier functions
function selectPoint(gui_fig, table, event)
gui_data = guidata(gui_fig);
removeHighlighted(gui_fig);
%set(gui_data.hHighlighted, 'PickableParts', 'visible');
gui_data.selected_row = event.Source.Value; % Assuming only one row is selectable at a time
selected_point = gui_data.bezier_control_points{gui_data.curr_probe}(ismember(gui_data.points_table.Items, gui_data.selected_row), :);
%selected_point = gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe}{1}(selected_row,:);
% Make the point draggable and set a listener to update the Bezier fit when dragged
set(gui_data.hHighlighted, 'XData', selected_point(:, 1), 'YData', selected_point(:, 2));
gui_data.pointSelected = true; % Indicate that a point is currently selected
%addlistener(selected_point, 'ROIMoved', @(src,evt) bezier_fit_probe(gui_fig));
% Optionally, change the color or appearance of the selected point
%selected_point.Color = 'g';
guidata(gui_fig, gui_data);
end
function removeHighlighted(gui_fig)
gui_data = guidata(gui_fig);
if ishandle(gui_data.hHighlighted)
set(gui_data.hHighlighted, 'XData', NaN, 'YData', NaN);
end
guidata(gui_fig, gui_data);
end
function populate_points_table(gui_fig)
gui_data = guidata(gui_fig);
%probe_points = gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe}{1};
%non_empty_slices = find(~cellfun(@isempty, gui_data.probe_points_histology(:, gui_data.curr_probe)));
pointStrings = cell(size(gui_data.bezier_control_points{gui_data.curr_probe}, 1), 1);
if ~isempty(gui_data.bezier_control_points{gui_data.curr_probe})
for i = 1:size(gui_data.bezier_control_points{gui_data.curr_probe}, 1)
prefix = '';
if gui_data.bezier_control_points{gui_data.curr_probe}(i, 3) == gui_data.curr_slice %condition to check if the point is in the current slice%
prefix = '[CURR] ';
end
pointStrings{i} = [prefix, 'Point ', num2str(i), ': (', ...
num2str(gui_data.bezier_control_points{gui_data.curr_probe}(i, 1)), ', ', num2str(gui_data.bezier_control_points{gui_data.curr_probe}(i, 2)), ')'];
end
end
set(gui_data.points_table, 'Items', pointStrings);
guidata(gui_fig, gui_data);
end
function deleteSelectedPoint(~, ~, gui_fig)
gui_data = guidata(gui_fig);
selectedIdx = ismember(gui_data.points_table.Items, gui_data.selected_row);
if ~isempty(selectedIdx) && ~isempty(gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe})
gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe}( ...
ismember(gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe}, ...
gui_data.bezier_control_points{gui_data.curr_probe}(selectedIdx, 1:2), 'rows'), :) = [];
gui_data.bezier_control_points{gui_data.curr_probe}(selectedIdx, :) = [];
end
gui_data.pointSelected = true;
guidata(gui_fig, gui_data);
removeHighlighted(gui_fig);
if selectedIdx > 1
selectedIdx = find(selectedIdx) - 1;
else
selectedIdx = find(selectedIdx) + 1;
end
gui_data.points_table.Value = gui_data.points_table.Items(selectedIdx);
gui_data.selected_row = gui_data.points_table.Items(selectedIdx);
guidata(gui_fig, gui_data);
populate_points_table(gui_fig)
update_slice(gui_fig);
end
function startDragFcn(~, ~, gui_fig)
gui_data = guidata(gui_fig);
selectedIdx = gui_data.selected_row;
if ~isempty(selectedIdx)
set(gui_fig, 'WindowButtonMotionFcn', @(src, event)draggingFcn(src, event, gui_fig));
set(gui_fig, 'WindowButtonUpFcn', @(src, event)stopDragFcn(src, event, gui_fig));
end
end
function draggingFcn(~, ~, gui_fig)
gui_data = guidata(gui_fig);
currentAxis = gui_data.histology_ax;
% Get the current point with respect to the intended axis
pt = get(currentAxis, 'CurrentPoint');
draggedPos = pt(1, 1:2);
% Get the axis limits
xLim = get(currentAxis, 'XLim');
yLim = get(currentAxis, 'YLim');
% Check and adjust the x position if it's outside the axis limits
if draggedPos(1) < xLim(1)
draggedPos(1) = xLim(1);
elseif draggedPos(1) > xLim(2)
draggedPos(1) = xLim(2);
end
% Check and adjust the y position if it's outside the axis limits
if draggedPos(2) < yLim(1)
draggedPos(2) = yLim(1);
elseif draggedPos(2) > yLim(2)
draggedPos(2) = yLim(2);
end
% Update the position of the point
selectedIdx = gui_data.selected_row;
selected_point = contains(gui_data.points_table.Items, selectedIdx(1:17));
gui_data.bezier_control_points{gui_data.curr_probe}(selected_point, 1:2) = draggedPos;
gui_data.probe_points_histology{gui_data.curr_slice, gui_data.curr_probe}(selected_point, 1:2) = draggedPos;
set(gui_data.hHighlighted, 'XData', draggedPos(1), 'YData', draggedPos(2));
gui_data.bezier_curves(gui_data.curr_probe).delete;
t = linspace(0, 1, 1000);
B = bezier_curve(t, gui_data.bezier_control_points{gui_data.curr_probe});
% Filter based on z-slice
z_slice_tolerance = 0.1; % define a tolerance for how close the z-value of the Bezier curve has to be to z_slice to be displayed
indices = find(abs(B(:, 3)-gui_data.curr_slice) < z_slice_tolerance); % find indices of points on the Bezier curve close to z_slice
B_slice = B(indices, :);
gui_data.bezier_curves(gui_data.curr_probe) = plot(B_slice(:, 1), B_slice(:, 2), 'Color', ...
gui_data.probe_color(gui_data.curr_probe, :), 'Parent', gui_data.histology_ax, 'LineWidth', 2);
guidata(gui_fig, gui_data);
populate_points_table(gui_fig);
%removeHighlighted(gui_fig);
end
function stopDragFcn(~, ~, gui_fig)
gui_data = guidata(gui_fig);
set(gui_fig, 'WindowButtonMotionFcn', '');
set(gui_fig, 'WindowButtonUpFcn', '');
gui_data.pointSelected = false; % Reset the flag since we're no longer selecting a point
guidata(gui_fig, gui_data);
update_slice(gui_fig);
end
% Bezier curve function
function B = bezier_curve(t, control_points)
n = size(control_points, 1) - 1; % degree of the polynomial
B = zeros(3, length(t)); % Change to 2 for 3D
[~, control_points_idx] = sort(control_points(:, 3));
control_points = control_points(control_points_idx, :);
for i = 0:n
B = B + nchoosek(n, i) * (1 - t).^(n - i) .* t.^i .* control_points(i+1, :)';
end
B = B';
end
%% control appearance
function autoContrastButtonPushed(gui_fig)
% Get guidata
gui_data = guidata(gui_fig);
% Set contrast and brightness
% Auto based on this function :
% g(i,j) = α * f(i,j) + β - we want to find α (contrast) and β
% (brightness)
% α = 255 / (maximum_gray - minimum_gray)
% solve β by plugging it into the formula where g(i, j)=0 and
% f(i, j)=minimum_gray
gui_data.contrast_alpha = 255 / double(max(gui_data.slice_im{gui_data.curr_slice}, [], 'all')- ...
min(gui_data.slice_im{gui_data.curr_slice}, [], 'all'));
gui_data.brightness_beta = -(gui_data.contrast_alpha * double(min(gui_data.slice_im{gui_data.curr_slice}, [], 'all')));
% update bright and contast slider values
gui_data.brightness_slider.Value = gui_data.brightness_beta;
gui_data.contrast_slider.Value = gui_data.contrast_alpha;
% Upload gui data
guidata(gui_fig, gui_data);
% Update slice
update_slice(gui_fig)
end