-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca_builder.m
More file actions
2301 lines (1998 loc) · 94.8 KB
/
Copy pathpca_builder.m
File metadata and controls
2301 lines (1998 loc) · 94.8 KB
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 pca_builder()
%sets up everything
close all; clc;
% colors for teh whole app
colors = struct();
colors.var1 = [0.95, 0.7, 0.75]; %pink
colors.var2 = [0.7, 0.85, 0.95];%blue
colors.var3 = [0.85, 0.95, 0.75];%green
colors.mean = [1, 0.6, 0.4];%redishs
colors.pc1 = [1, 0.5, 0.5];%pink
colors.pc2 = [0.6, 0.7, 1];%red
colors.pc3 = [0.5, 0.9, 0.7];%blue
colors.ellipse = [1, 0.85, 0.4];%green
% main figure
fig = figure('Position', [50, 50, 1400, 850], 'Color', [0.08, 0.08, 0.08], 'NumberTitle', 'off', 'Name', 'PCA Builder');
% state holds everyting
% all teh app state in one struct for easy passing around
state = struct();
state.n = 50; % number of point that can be modified
state.stage = 1; % 1=3x1D, 2=2D, 3=3D, 4=PCA
state.pca_step = 0; % wich pca step to show
%diff combination of mean and sd needs diff combaintion of varibales
state.mean1 = 0; state.sd1 = 1;%by defualt tsrandradzed data
state.mean2 = 0; state.sd2 = 1;%by defualt tsrandradzed data
state.mean3 = 0; state.sd3 = 1;%by defualt tsrandradzed data
state.corr12 = 0.7; % default correlations
state.corr13 = 0.6;% default correlations
state.corr23 = 0.5;% default correlations
% display toggles/clikciable
state.show_mean = true;
state.show_sd = true;
state.show_deviations = false;%set to false cause vsiually overloading
state.show_component_deviations = false;
state.show_ellipsoid = true;
state.show_reference_cube = true;
state.show_grid = true;
state.show_pc_projections = false;
state.color_by_pc1 = false;
% appearance settings
state.dot_size = 80;% will need to make modifiable later
state.dot_color = [0.95, 0.7, 0.75];% will need to make modifiable later
state.deviation_color = [0.6, 0.6, 0.6];% will need to make modifiable later
state.color_axes_by_var = false;% setting htis speifically for 3D where it can be hard to see which axis is reposnsible for what
% interaction state
state.selected_point_idx = -1; % -1 means no selection
state.var_pair_2d = [1, 2]; % wich pair to show in 2D mode
state.interaction_mode = 'rotate'; % or 'select'
state.animating = false; % true during animations
state.colors = colors;
state = generate_data(state);% from user seletcion of all of the above
%3x1D axes stacked vertically for stage 1
state.ax1 = axes('Position', [0.05, 0.72, 0.50, 0.18], 'Color', [0.05, 0.05, 0.05]);%pink
state.ax2 = axes('Position', [0.05, 0.42, 0.50, 0.18], 'Color', [0.05, 0.05, 0.05]);%blue
state.ax3 = axes('Position', [0.05, 0.12, 0.50, 0.18], 'Color', [0.05, 0.05, 0.05]);%green
% main axis used for stages 2-4
state.ax_main = axes('Position', [0.05, 0.12, 0.50, 0.78], 'Color', [0.05, 0.05, 0.05]);
%hide main axis,, we start in 3x1D mode
set(state.ax_main, 'Visible', 'off');
% buildteh UI and start renderig
create_ui(fig, state);
setappdata(fig, 'state', state); % store state in figure!!!!!!
update_controls_visibility(fig);
update_display(fig);
fprintf('PCA Builder Ready!\n');
end
%% data generation step
function state = generate_data(state)
% generates random data based on current params
% different logic for each stage
n = state.n;
% correlation values form sliders
r12 = state.corr12;
r13 = state.corr13;
r23 = state.corr23;
if state.stage == 1
% stage 1 => independent vars, no correlation
% jsut generate 3 separate normal distributions
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
state.I3 = state.mean3 + state.sd3 * randn(n, 1);
elseif state.stage == 2
% stage 2 +> check if corr matrix is valid first
% chol fails if matrix isnt positive definite
R = [1, r12, r13; r12, 1, r23; r13, r23, 1];
[~, p] = chol(R);
if p > 0
% then would not be valid SO we need to fix r23
% clamp r23 to valid range based on r12 nad r13
min_r23 = r12*r13 - sqrt(max(0, (1-r12^2)*(1-r13^2)));
max_r23 = r12*r13 + sqrt(max(0, (1-r12^2)*(1-r13^2)));
r23 = max(min_r23, min(max_r23, r23));
R = [1, r12, r13; r12, 1, r23; r13, r23, 1];
[~, p] = chol(R);
end
% which var pair?
pair = state.var_pair_2d;
% handle perfect correlation cases separately (caused issue before-)
% becasue mvnrnd cant handle r=1 or r=-1
if isequal(pair, [1,2]) && abs(r12) >= 0.999 && state.sd1 > 0 && state.sd2 > 0
% then pair 1-2 with perfect corr
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
% var2 is jsut a linear transform of var1 sp
state.I2 = state.mean2 + sign(r12) * state.sd2 * (state.I1 - state.mean1) / state.sd1;
state.I3 = state.mean3 + state.sd3 * randn(n, 1);
elseif isequal(pair, [1,3]) && abs(r13) >= 0.999 && state.sd1 > 0 && state.sd3 > 0
% pair1-3 with perfect corr
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
state.I3 = state.mean3 + sign(r13) * state.sd3 * (state.I1 - state.mean1) / state.sd1;
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
elseif isequal(pair, [2,3]) && abs(r23) >= 0.999 && state.sd2 > 0 && state.sd3 > 0
% pair 2-3 with perfect corr
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
state.I3 = state.mean3 + sign(r23) * state.sd3 * (state.I2 - state.mean2) / state.sd2;
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
elseif p == 0 && state.sd1 > 0 && state.sd2 > 0 && state.sd3 > 0
% matrix valid, so use mvnrnd
% and convert correlation matrix to covariance matrix
Sigma = diag([state.sd1, state.sd2, state.sd3]) * R * diag([state.sd1, state.sd2, state.sd3]);
mu = [state.mean1, state.mean2, state.mean3];
raw = mvnrnd(mu, Sigma, n);
state.I1 = raw(:, 1);
state.I2 = raw(:, 2);
state.I3 = raw(:, 3);
else
% fallback if nothing else work
% manually induces correlation using linear combo
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
z = randn(n, 1);
% r12 part is correlated, and sqrt part is independent noise
state.I2 = state.mean2 + state.sd2 * (r12 * (state.I1 - state.mean1)/state.sd1 + sqrt(max(0, 1-r12^2)) * z);
state.I3 = state.mean3 + state.sd3 * randn(n, 1);
end
else
%stage 3+ full 3D
% same logic as stage 2 but fo all three vars
R = [1, r12, r13; r12, 1, r23; r13, r23, 1];
[~, p] = chol(R);
%check if perfect correlations first
if abs(r12) >= 0.999 && abs(r13) >= 0.999 && abs(r23) >= 0.999
%if all correlations perfect so points lie on a line
t = randn(n, 1);
state.I1 = state.mean1 + state.sd1 * t;
state.I2 = state.mean2 + sign(r12) * state.sd2 * t;
state.I3 = state.mean3 + sign(r13) * state.sd3 * t;
elseif abs(r12) >= 0.999 && state.sd1 > 0 && state.sd2 > 0
% perfect r12 case
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
state.I2 = state.mean2 + sign(r12) * state.sd2 * (state.I1 - state.mean1) / state.sd1;
state.I3 = state.mean3 + state.sd3 * randn(n, 1);
elseif abs(r13) >= 0.999 && state.sd1 > 0 && state.sd3 > 0
% perfect r13 case
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
state.I3 = state.mean3 + sign(r13) * state.sd3 * (state.I1 - state.mean1) / state.sd1;
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
elseif abs(r23) >= 0.999 && state.sd2 > 0 && state.sd3 > 0
% perfect r23 case
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
state.I3 = state.mean3 + sign(r23) * state.sd3 * (state.I2 - state.mean2) / state.sd2;
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
elseif p > 0
%matrix invalid so adjust r23 to fix it
% but r23 must be in a certian range given r12 and r13
min_r23 = r12*r13 - sqrt(max(0, (1-r12^2)*(1-r13^2)));
max_r23 = r12*r13 + sqrt(max(0, (1-r12^2)*(1-r13^2)));
% then add small buffer to stay away form boundary
r23_adjusted = max(min_r23 + 0.001, min(max_r23 - 0.001, r23));
R = [1, r12, r13; r12, 1, r23_adjusted; r13, r23, 1];
[~, p2] = chol(R);
if p2 == 0 && state.sd1 > 0 && state.sd2 > 0 && state.sd3 > 0
Sigma = diag([state.sd1, state.sd2, state.sd3]) * R * diag([state.sd1, state.sd2, state.sd3]);
mu = [state.mean1, state.mean2, state.mean3];
raw = mvnrnd(mu, Sigma, n);
state.I1 = raw(:, 1);
state.I2 = raw(:, 2);
state.I3 = raw(:, 3);
else
% if doesn't work still, then last resort just ignroe correlations
state.I1 = state.mean1 + state.sd1 * randn(n, 1);
state.I2 = state.mean2 + state.sd2 * randn(n, 1);
state.I3 = state.mean3 + state.sd3 * randn(n, 1);
end
else
% else if matrix valid so we can use mvnrnd directly
if state.sd1 > 0 && state.sd2 > 0 && state.sd3 > 0
Sigma = diag([state.sd1, state.sd2, state.sd3]) * R * diag([state.sd1, state.sd2, state.sd3]);
mu = [state.mean1, state.mean2, state.mean3];
raw = mvnrnd(mu, Sigma, n);
state.I1 = raw(:, 1);
state.I2 = raw(:, 2);
state.I3 = raw(:, 3);
else
state.I1 = state.mean1 * ones(n, 1);
state.I2 = state.mean2 * ones(n, 1);
state.I3 = state.mean3 * ones(n, 1);
end
end
% !!!!!!!pca stats computation!!!!!!!
%start by compute eigenvectors adn eigenvalues from covariance
state.mean_vec = [mean(state.I1), mean(state.I2), mean(state.I3)];
if std(state.I1) > 0.001 && std(state.I2) > 0.001 && std(state.I3) > 0.001
state.cov = cov([state.I1, state.I2, state.I3]);
[V, D] = eig(state.cov);
%then sort descending so PC1 has largest eigenvalue
[eigenvalues, idx] = sort(diag(D), 'descend');
state.eigenvalues = eigenvalues;
state.eigenvectors = V(:, idx);
state.var_exp = eigenvalues / max(sum(eigenvalues), 0.001) * 100;
else
state.cov = eye(3) * 0.001;
state.eigenvalues = [0.001; 0.001; 0.001];
state.eigenvectors = eye(3);
state.var_exp = [33.3; 33.3; 33.3];
end
end
% IMPORTANT from prior issues i had : handle SD=0 cases or things break
% if sd is zero all points collapse to teh mean
if state.sd1 == 0, state.I1 = state.mean1 * ones(n, 1); end
if state.sd2 == 0, state.I2 = state.mean2 * ones(n, 1); end
if state.sd3 == 0, state.I3 = state.mean3 * ones(n, 1); end
end
%% UI
function create_ui(fig, state)
%build all controls on the right side panel
x = 0.60; w = 0.38; y = 0.96;
bg = [0.08, 0.08, 0.08];
sep_color = [0.3, 0.3, 0.3];
% the stage buttons at the top
uicontrol('Style', 'text', 'String', 'STAGE:', 'Units', 'normalized', ...
'Position', [x, y, 0.08, 0.025], 'BackgroundColor', bg, ...
'ForegroundColor', 'w', 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
stages = {'3x1D', '2D', '3D', 'PCA'};
% create 4 stage buttons in a row
for i = 1:4
uicontrol('Style', 'pushbutton', 'String', stages{i}, ...
'Units', 'normalized', 'Position', [x + 0.08 + (i-1)*0.07, y-0.005, 0.065, 0.035], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'ForegroundColor', 'w', 'FontSize', 10, ...
'Tag', sprintf('stage_%d', i), ...
'Callback', @(~,~) change_stage(fig, i));
end
% then 2D pair selection buttons
% only visible in stage 2 to pick wich vars to plot
y = y - 0.04;
uicontrol('Style', 'text', 'String', '2D Pair:', 'Units', 'normalized', ...
'Position', [x, y, 0.08, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.7, 0.7, 0.7], 'FontSize', 9, 'HorizontalAlignment', 'left', ...
'Tag', 'pair_label', 'Visible', 'off');
pairs = {'1-2', '1-3', '2-3'};
pair_vals = {[1,2], [1,3], [2,3]};
for i = 1:3
uicontrol('Style', 'pushbutton', 'String', pairs{i}, ...
'Units', 'normalized', 'Position', [x + 0.08 + (i-1)*0.055, y, 0.05, 0.025], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'ForegroundColor', 'w', 'FontSize', 10, ...
'Tag', sprintf('pair_%d', i), 'Visible', 'off', ...
'Callback', @(~,~) change_pair(fig, pair_vals{i}));
end
% had some issue so mnaully moved a bit downa ndup some setcions
y = y - 0.012;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% variables section panle
y = y - 0.025;
uicontrol('Style', 'text', 'String', '📊 VARIABLES', 'Units', 'normalized', ...
'Position', [x, y, w, 0.022], 'BackgroundColor', bg, ...
'ForegroundColor', [0.6, 0.85, 1], 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
y = y - 0.045;
y = create_var_controls(fig, x, y, w, 1, state);
y = y - 0.008;
y = create_var_controls(fig, x, y, w, 2, state);
y = y - 0.008;
y = create_var_controls(fig, x, y, w, 3, state);
% same as above issue iwth positioning
y = y - 0.027;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% correlations section panel
y = y - 0.025;
uicontrol('Style', 'text', 'String', '🔗 CORRELATIONS', 'Units', 'normalized', ...
'Position', [x, y, w, 0.022], 'BackgroundColor', bg, ...
'ForegroundColor', [0.6, 0.85, 1], 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
y = y - 0.025;% same as above issue iwth positioning
y = create_corr_control(fig, x, y, w, '1-2', state.corr12);
y = create_corr_control(fig, x, y, w, '1-3', state.corr13);
y = create_corr_control(fig, x, y, w, '2-3', state.corr23);
% n slider
y = y - 0.005;
uicontrol('Style', 'text', 'String', 'N:', 'Units', 'normalized', ...
'Position', [x, y, 0.04, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.8, 0.8, 0.8], 'HorizontalAlignment', 'left');
uicontrol('Style', 'slider', 'Min', 3, 'Max', 200, 'Value', state.n, ...
'Units', 'normalized', 'Position', [x+0.04, y, 0.15, 0.02], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'Tag', 'n_slider', ...
'Callback', @(src,~) update_n(fig, round(src.Value)));
uicontrol('Style', 'text', 'String', sprintf('%d', state.n), ...
'Units', 'normalized', 'Position', [x+0.20, y, 0.04, 0.02], ...
'BackgroundColor', bg, 'ForegroundColor', [1, 1, 1], ...
'FontWeight', 'bold', 'Tag', 'n_val');
% % same as above issue iwth positioning
y = y - 0.015;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% action buttons rows
y = y - 0.06;
uicontrol('Style', 'pushbutton', 'String', 'New Sample', ...
'Units', 'normalized', 'Position', [x, y, 0.12, 0.035], ...
'BackgroundColor', [0.3, 0.15, 0.15], 'ForegroundColor', 'w', 'FontSize', 10, ...
'Callback', @(~,~) new_sample(fig));
uicontrol('Style', 'pushbutton', 'String', 'CLEAR SELECTION', ...
'Units', 'normalized', 'Position', [x+0.13, y, 0.14, 0.035], ...
'BackgroundColor', [0.15, 0.15, 0.4], 'ForegroundColor', 'y', 'FontSize', 9, 'FontWeight', 'bold', ...
'Callback', @(~,~) clear_selection(fig));
uicontrol('Style', 'pushbutton', 'String', 'MODE: ROTATE', ...
'Units', 'normalized', 'Position', [x+0.28, y, 0.10, 0.035], ...
'BackgroundColor', [0.15, 0.3, 0.15], 'ForegroundColor', 'w', 'FontSize', 9, 'FontWeight', 'bold', ...
'Tag', 'mode_toggle', ...
'Callback', @(~,~) toggle_mode(fig));
% same as above issue iwth positioning
y = y - 0.015;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% appearance section panel
y = y - 0.045;
uicontrol('Style', 'text', 'String', '🎨 APPEARANCE', 'Units', 'normalized', ...
'Position', [x, y, w, 0.022], 'BackgroundColor', bg, ...
'ForegroundColor', [0.6, 0.85, 1], 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
% dot size slider
y = y - 0.028;
uicontrol('Style', 'text', 'String', 'Size:', 'Units', 'normalized', ...
'Position', [x, y, 0.05, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.8, 0.8, 0.8], 'HorizontalAlignment', 'left');
uicontrol('Style', 'slider', 'Min', 20, 'Max', 200, 'Value', state.dot_size, ...
'Units', 'normalized', 'Position', [x+0.05, y, 0.12, 0.02], ...
'BackgroundColor', [0.15, 0.15, 0.15], ...
'Callback', @(src,~) update_dot_size(fig, round(src.Value)));
% dot color presets
y = y - 0.028;
uicontrol('Style', 'text', 'String', 'Dots:', 'Units', 'normalized', ...
'Position', [x, y, 0.05, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.8, 0.8, 0.8], 'HorizontalAlignment', 'left');
color_presets = {[0.95, 0.7, 0.75]; [0.7, 0.85, 0.95]; [0.85, 0.95, 0.75]; [1, 0.8, 0.4]; [0.9, 0.5, 0.5]; [0.7, 0.5, 0.9]; [1, 1, 1]; [0.3, 0.8, 0.8]};
for i = 1:8
col = color_presets{i};
uicontrol('Style', 'pushbutton', 'String', '', ...
'Units', 'normalized', 'Position', [x+0.05+(i-1)*0.032, y, 0.028, 0.022], ...
'BackgroundColor', col, 'Callback', @(~,~) update_dot_color(fig, col));
end
% deviation line colors choice
y = y - 0.028;
uicontrol('Style', 'text', 'String', 'Devs:', 'Units', 'normalized', ...
'Position', [x, y, 0.05, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.8, 0.8, 0.8], 'HorizontalAlignment', 'left');
for i = 1:8
col = color_presets{i};
uicontrol('Style', 'pushbutton', 'String', '', ...
'Units', 'normalized', 'Position', [x+0.05+(i-1)*0.032, y, 0.028, 0.022], ...
'BackgroundColor', col, 'Callback', @(~,~) update_deviation_color(fig, col));
end
% color axes checkbox
y = y - 0.028;
uicontrol('Style', 'checkbox', 'String', 'Color Axes by Variable', 'Value', state.color_axes_by_var, ...
'Units', 'normalized', 'Position', [x, y, 0.20, 0.022], ...
'BackgroundColor', bg, 'ForegroundColor', 'w', 'FontSize', 9, ...
'Callback', @(src,~) toggle_option(fig, 'color_axes_by_var', src.Value));
% ---
y = y - 0.012;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% display toggles section panel
y = y - 0.055;
uicontrol('Style', 'text', 'String', '👁️ DISPLAY', 'Units', 'normalized', ...
'Position', [x, y, w, 0.022], 'BackgroundColor', bg, ...
'ForegroundColor', [0.6, 0.85, 1], 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
% checkboxes in 3 columns
y = y - 0.02;
toggles = {'show_mean', 'Mean'; 'show_sd', 'SD'; 'show_deviations', 'Devs'; ...
'show_component_deviations', 'Comps'; 'show_ellipsoid', 'Ellipse'; 'show_reference_cube', 'Cube'; ...
'show_grid', 'Grid'; 'show_pc_projections', 'Proj'; 'color_by_pc1', 'PC1 Col'};
col_w = w / 3;
for i = 1:size(toggles, 1)
row = floor((i-1) / 3);
col = mod(i-1, 3);
uicontrol('Style', 'checkbox', 'String', toggles{i,2}, 'Value', state.(toggles{i,1}), ...
'Units', 'normalized', 'Position', [x + col*col_w, y - row*0.026, col_w-0.01, 0.024], ...
'BackgroundColor', bg, 'ForegroundColor', 'w', 'FontSize', 9, ...
'Tag', toggles{i,1}, 'Callback', @(src,~) toggle_option(fig, toggles{i,1}, src.Value));
end
% same as above issue iwth positioning
y = y - 0.07;
uipanel('Parent', fig, 'Units', 'normalized', 'Position', [x, y, w, 0.002], ...
'BackgroundColor', sep_color, 'BorderType', 'none');
% %pca steps nit only only visible in stage 4
y = y - 0.012;
h1 = uicontrol('Style', 'text', 'String', '🎯 PCA STEPS', 'Units', 'normalized', ...
'Position', [x, y, w, 0.022], 'BackgroundColor', bg, ...
'ForegroundColor', [0.6, 0.85, 1], 'FontSize', 11, 'FontWeight', 'bold', 'HorizontalAlignment', 'left', ...
'Tag', 'pca_label', 'Visible', 'off');
y = y - 0.032;
steps = {'None', 'Ellip', '+PC1', '+PC2', '+PC3', 'All'};
pca_handles = h1;
for i = 1:6
h = uicontrol('Style', 'pushbutton', 'String', steps{i}, 'Units', 'normalized', ...
'Position', [x + (i-1)*0.062, y, 0.058, 0.028], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'ForegroundColor', 'w', 'FontSize', 10, ...
'Tag', sprintf('pca_%d', i-1), 'Visible', 'off', ...
'Callback', @(~,~) set_pca(fig, i-1));
pca_handles = [pca_handles, h];
end
setappdata(fig, 'pca_handles', pca_handles);
% help button
y = y - 0.038;
uicontrol('Style', 'pushbutton', 'String', '📚 HELP', ...
'Units', 'normalized', 'Position', [x, y, 0.18, 0.032], ...
'BackgroundColor', [0.2, 0.35, 0.5], 'ForegroundColor', 'w', ...
'FontSize', 11, 'FontWeight', 'bold', ...
'Callback', @(~,~) open_help_window(fig));
end
function y = create_var_controls(fig, x, y, w, var_num, state)
colors = {[0.95, 0.7, 0.75], [0.7, 0.85, 0.95], [0.85, 0.95, 0.75]};
col = colors{var_num};
bg = [0.08, 0.08, 0.08];
mean_val = state.(sprintf('mean%d', var_num));
sd_val = state.(sprintf('sd%d', var_num));
uicontrol('Style', 'text', 'String', sprintf('Var %d:', var_num), 'Units', 'normalized', ...
'Position', [x, y, 0.06, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', col, 'FontSize', 10, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
uicontrol('Style', 'text', 'String', 'μ', 'Units', 'normalized', ...
'Position', [x+0.06, y, 0.02, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.7, 0.7, 0.7], 'HorizontalAlignment', 'center');
uicontrol('Style', 'slider', 'Min', -10, 'Max', 10, 'Value', mean_val, ...
'Units', 'normalized', 'Position', [x+0.08, y, 0.10, 0.02], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'Tag', sprintf('mean%d_slider', var_num), ...
'Callback', @(src,~) update_param(fig, sprintf('mean%d', var_num), src.Value));
uicontrol('Style', 'text', 'String', sprintf('%.1f', mean_val), ...
'Units', 'normalized', 'Position', [x+0.19, y, 0.04, 0.02], ...
'BackgroundColor', bg, 'ForegroundColor', col, 'FontWeight', 'bold', ...
'Tag', sprintf('mean%d_val', var_num));
uicontrol('Style', 'text', 'String', 'σ', 'Units', 'normalized', ...
'Position', [x+0.24, y, 0.02, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.7, 0.7, 0.7], 'HorizontalAlignment', 'center');
uicontrol('Style', 'slider', 'Min', 0, 'Max', 5, 'Value', sd_val, ...
'Units', 'normalized', 'Position', [x+0.26, y, 0.08, 0.02], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'Tag', sprintf('sd%d_slider', var_num), ...
'Callback', @(src,~) update_param(fig, sprintf('sd%d', var_num), src.Value));
uicontrol('Style', 'text', 'String', sprintf('%.1f', sd_val), ...
'Units', 'normalized', 'Position', [x+0.35, y, 0.03, 0.02], ...
'BackgroundColor', bg, 'ForegroundColor', col, 'FontWeight', 'bold', ...
'Tag', sprintf('sd%d_val', var_num));
y = y - 0.035;
end
function y = create_corr_control(fig, x, y, w, label, val)
tag = ['corr', strrep(label, '-', '')];
bg = [0.08, 0.08, 0.08];
uicontrol('Style', 'text', 'String', ['r' label ':'], 'Units', 'normalized', ...
'Position', [x, y, 0.06, 0.02], 'BackgroundColor', bg, ...
'ForegroundColor', [0.8, 0.8, 0.8], 'FontSize', 10, 'HorizontalAlignment', 'left', ...
'Tag', [tag '_label'], 'Visible', 'off');
uicontrol('Style', 'slider', 'Min', -1, 'Max', 1, 'Value', val, ...
'Units', 'normalized', 'Position', [x+0.06, y, 0.15, 0.02], ...
'BackgroundColor', [0.15, 0.15, 0.15], 'Tag', [tag '_slider'], 'Visible', 'off', ...
'Callback', @(src,~) update_corr(fig, tag, src.Value));
uicontrol('Style', 'text', 'String', sprintf('%.2f', val), ...
'Units', 'normalized', 'Position', [x+0.22, y, 0.05, 0.02], ...
'BackgroundColor', bg, 'ForegroundColor', [1, 1, 1], 'FontWeight', 'bold', ...
'Tag', [tag '_val'], 'Visible', 'off');
y = y - 0.032;
end
%% callbacks
% all the UI callback functions go here
function change_stage(fig, new_stage)
% called when users click a stage buttons
state = getappdata(fig, 'state');
state.stage = new_stage;
state.selected_point_idx = -1; % clear any selection
state.animating = false;
if new_stage == 4, state.pca_step = 0; end
state = generate_data(state);
setappdata(fig, 'state', state);
update_controls_visibility(fig);
update_display(fig);
end
% called when users want to change a
% diffent selecion of pairwise vsiuals variable
function change_pair(fig, pair)
state = getappdata(fig, 'state');
state.var_pair_2d = pair;
state.selected_point_idx = -1;
state.animating = false;
state = generate_data(state);
setappdata(fig, 'state', state);
update_pair_buttons(fig, pair);
update_display(fig);
end
function update_pair_buttons(fig, pair)
pairs = {[1,2], [1,3], [2,3]};
for i = 1:3
btn = findobj(fig, 'Tag', sprintf('pair_%d', i));
if ~isempty(btn)
if isequal(pairs{i}, pair)
set(btn, 'BackgroundColor', [0.4, 0.25, 0.25]);
else
set(btn, 'BackgroundColor', [0.15, 0.15, 0.15]);
end
end
end
end
%called when mean or sd slider changes
function update_param(fig, name, val)
state = getappdata(fig, 'state');
state.(name) = val;
state.animating = false;
% update teh display label next to slider
label = findobj(fig, 'Tag', [name '_val']);
if ~isempty(label), set(label, 'String', sprintf('%.1f', val)); end
state = generate_data(state);
setappdata(fig, 'state', state);
update_display(fig);
end
% called when correlation slider changes
function update_corr(fig, tag, val)
state = getappdata(fig, 'state');
state.(tag) = val;
state.animating = false;
label = findobj(fig, 'Tag', [tag '_val']);
if ~isempty(label), set(label, 'String', sprintf('%.2f', val)); end
state = generate_data(state);
setappdata(fig, 'state', state);
update_display(fig);
end
% sam ebut for n values of data deisre dby usrs
function update_n(fig, val)
state = getappdata(fig, 'state');
state.n = val;
state.animating = false;
label = findobj(fig, 'Tag', 'n_val');
if ~isempty(label), set(label, 'String', sprintf('%d', val)); end
state = generate_data(state);
setappdata(fig, 'state', state);
update_display(fig);
end
function update_dot_size(fig, val)
state = getappdata(fig, 'state');
state.dot_size = val;
setappdata(fig, 'state', state);
update_display(fig);
end
function update_dot_color(fig, col)
state = getappdata(fig, 'state');
state.dot_color = col;
setappdata(fig, 'state', state);
update_display(fig);
end
function update_deviation_color(fig, col)
state = getappdata(fig, 'state');
state.deviation_color = col;
setappdata(fig, 'state', state);
update_display(fig);
end
function toggle_option(fig, name, val)
state = getappdata(fig, 'state');
state.(name) = logical(val);
setappdata(fig, 'state', state);
update_display(fig);
end
% regenerate random data with same params (same as baove if user change)
function new_sample(fig)
state = getappdata(fig, 'state');
state.selected_point_idx = -1;
state.animating = false;
state = generate_data(state);
setappdata(fig, 'state', state);
update_display(fig);
end
% deselect current point if any
%% WILL NEED CHANGE DOESN4T WORK VERY WELL
function clear_selection(fig)
state = getappdata(fig, 'state');
state.selected_point_idx = -1;
state.animating = false;
setappdata(fig, 'state', state);
update_display(fig);
fprintf('Selection cleared!\n');
end
% switch between rotate and select modes
function toggle_mode(fig)
state = getappdata(fig, 'state');
if strcmp(state.interaction_mode, 'rotate')
state.interaction_mode = 'select';
else
state.interaction_mode = 'rotate';
end
setappdata(fig, 'state', state);
update_mode_button(fig);
update_display(fig);
end
function update_mode_button(fig)
state = getappdata(fig, 'state');
btn = findobj(fig, 'Tag', 'mode_toggle');
if ~isempty(btn)
if strcmp(state.interaction_mode, 'select')
set(btn, 'String', 'MODE: SELECT', 'BackgroundColor', [0.3, 0.15, 0.3]);
else
set(btn, 'String', 'MODE: ROTATE', 'BackgroundColor', [0.15, 0.3, 0.15]);
end
end
end
function set_pca(fig, step)
% called when user clicks pca step button
state = getappdata(fig, 'state');
old_step = state.pca_step;
state.pca_step = step;
setappdata(fig, 'state', state);
update_pca_buttons(fig, step);
%animate when stepping through pca
%only animate if going forward nad step>=2
if step > old_step && step >= 2
animate_pca_variance(fig, old_step, step);
else
update_display(fig);
end
end
function update_pca_buttons(fig, step)
for i = 0:5
btn = findobj(fig, 'Tag', sprintf('pca_%d', i));
if ~isempty(btn)
if i == step
set(btn, 'BackgroundColor', [0.4, 0.25, 0.25]);
else
set(btn, 'BackgroundColor', [0.15, 0.15, 0.15]);
end
end
end
end
function update_controls_visibility(fig)
% show/hide controls based on current stage
% eg correlation sliders only visible in stage 2+
state = getappdata(fig, 'state');
% stage buttons - highlight current one
for i = 1:4
btn = findobj(fig, 'Tag', sprintf('stage_%d', i));
if ~isempty(btn)
if i == state.stage
set(btn, 'BackgroundColor', [0.5, 0.3, 0.3]);
else
set(btn, 'BackgroundColor', [0.15, 0.15, 0.15]);
end
end
end
% 2D pair buttons - only show in stage 2
pair_tags = {'pair_label', 'pair_1', 'pair_2', 'pair_3'};
vis = 'off'; if state.stage == 2, vis = 'on'; end
for i = 1:length(pair_tags)
obj = findobj(fig, 'Tag', pair_tags{i});
if ~isempty(obj), set(obj, 'Visible', vis); end
end
if state.stage == 2
update_pair_buttons(fig, state.var_pair_2d);
end
% corr sliders show for stages 2+
corr_tags = {'corr12_label', 'corr12_slider', 'corr12_val', ...
'corr13_label', 'corr13_slider', 'corr13_val', ...
'corr23_label', 'corr23_slider', 'corr23_val'};
vis = 'off'; if state.stage >= 2, vis = 'on'; end
for i = 1:length(corr_tags)
obj = findobj(fig, 'Tag', corr_tags{i});
if ~isempty(obj), set(obj, 'Visible', vis); end
end
% pca step buttons !!only stage 4
handles = getappdata(fig, 'pca_handles');
vis = 'off'; if state.stage == 4, vis = 'on'; end
if ~isempty(handles)
set(handles, 'Visible', vis);
end
end
%% display funciton
function update_display(fig)
% main render function, called after any state change
state = getappdata(fig, 'state');
if state.stage == 1
% stage 1: three 1D plots stacked, hide main axis
cla(state.ax_main, 'reset');
set(state.ax_main, 'Visible', 'off');
% show the three 1D axes
set(state.ax1, 'Visible', 'on');
set(state.ax2, 'Visible', 'on');
set(state.ax3, 'Visible', 'on');
render_1d(state.ax1, state.I1, 'Variable 1', state.colors.var1, state, state.mean1, state.sd1);
render_1d(state.ax2, state.I2, 'Variable 2', state.colors.var2, state, state.mean2, state.sd2);
render_1d(state.ax3, state.I3, 'Variable 3', state.colors.var3, state, state.mean3, state.sd3);
else
%stages 2-4:show main axis, hide teh 1D ones
cla(state.ax1, 'reset'); set(state.ax1, 'Visible', 'off');
cla(state.ax2, 'reset'); set(state.ax2, 'Visible', 'off');
cla(state.ax3, 'reset'); set(state.ax3, 'Visible', 'off');
% resetmain axis for clean render
cla(state.ax_main, 'reset');
set(state.ax_main, 'Visible', 'on');
set(state.ax_main, 'Color', [0.05, 0.05, 0.05]);
switch state.stage
case 2, render_2d(state.ax_main, state);
case 3, render_3d(state.ax_main, state);
case 4, render_pca(state.ax_main, state);
end
end
drawnow;
end
%% render 1D
function render_1d(ax, data, label, color, state, param_mean, param_sd)
% draws a single 1D distribution on a number line
cla(ax);
hold(ax, 'on');
% axis line throuhg center
plot(ax, [-10, 10], [0, 0], 'Color', [0.5, 0.5, 0.5], 'LineWidth', 2);
% tick mark every 2 units
for tick = -10:2:10
plot(ax, [tick, tick], [-0.1, 0.1], 'Color', [0.5, 0.5, 0.5], 'LineWidth', 1);
if tick == 0
text(ax, tick, -0.28, '0', 'Color', [1, 1, 1], 'FontSize', 9, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
else
text(ax, tick, -0.28, sprintf('%d', tick), 'Color', [0.6, 0.6, 0.6], 'FontSize', 8, 'HorizontalAlignment', 'center');
end
end
% deviation lines => draw before points so points are on top
%each line goes form point to mean
if state.show_deviations && param_sd > 0
for i = 1:length(data)
plot(ax, [data(i), param_mean], [0, 0], 'Color', [state.deviation_color, 0.6], 'LineWidth', 2);
end
end
% points on top of everything else
scatter(ax, data, zeros(size(data)), state.dot_size, state.dot_color, 'filled', 'MarkerEdgeColor', 'w', 'LineWidth', 1);
%mean line is vertical bar at mean position
if state.show_mean
plot(ax, [param_mean, param_mean], [-0.4, 0.4], 'Color', state.colors.mean, 'LineWidth', 4);
text(ax, param_mean, 0.55, sprintf('%.1f', param_mean), 'Color', state.colors.mean, 'FontSize', 9, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end
%sd lines is dashed lines at mean more or minus 1 sd
if state.show_sd && param_sd > 0
plot(ax, [param_mean-param_sd, param_mean-param_sd], [-0.3, 0.3], 'Color', state.colors.mean, 'LineWidth', 2, 'LineStyle', '--');
plot(ax, [param_mean+param_sd, param_mean+param_sd], [-0.3, 0.3], 'Color', state.colors.mean, 'LineWidth', 2, 'LineStyle', '--');
end
title(ax, sprintf('%s (μ=%.1f, σ=%.1f)', label, param_mean, param_sd), 'Color', color, 'FontSize', 11, 'FontWeight', 'bold');
xlim(ax, [-10, 10]); ylim(ax, [-0.7, 0.7]);
set(ax, 'YTick', [], 'XTick', [], 'Color', [0.05, 0.05, 0.05], 'XColor', [0.5, 0.5, 0.5], 'YColor', [0.5, 0.5, 0.5]);
hold(ax, 'off');
end
%% render 2D
function render_2d(ax, state)
% draws a 2D scatter plot for selected var pair
hold(ax, 'on');
view(ax, 2);
% figure out which var pair we're showing
% dx dy =>data, cx cy =>colors, lx ly => labels !!!!!!!
pair = state.var_pair_2d;
if isequal(pair, [1,2])
dx = state.I1; dy = state.I2; cx = state.colors.var1; cy = state.colors.var2; lx = 'Var 1'; ly = 'Var 2';
elseif isequal(pair, [1,3])
dx = state.I1; dy = state.I3; cx = state.colors.var1; cy = state.colors.var3; lx = 'Var 1'; ly = 'Var 3';
else
dx = state.I2; dy = state.I3; cx = state.colors.var2; cy = state.colors.var3; lx = 'Var 2'; ly = 'Var 3';
end
% ellipse shoudl be at bottom layer showing data spread but not prohibting
% using slect a point to see its eculdian distance! FIXED
if state.show_ellipsoid
plot_ellipse(ax, [mean(dx), mean(dy)], cov([dx, dy]), state.colors.ellipse);
end
% deviation lines: clickable, drawn !!!before!!! points
% TEHN clicking a line triggers the decomposition animations
if state.show_deviations
mx = mean(dx); my = mean(dy);
for i = 1:length(dx)
h_dev = plot(ax, [dx(i), mx], [dy(i), my], 'Color', [state.deviation_color, 0.6], 'LineWidth', 1.5);
% make clickable for animation
set(h_dev, 'ButtonDownFcn', @(~,~) click_deviation_2d(ancestor(ax,'figure'), i, dx, dy, cx, cy));
end
end
% component deviations where x and y parts drawn separately
% shows how total deviation breaks into x adn y components
if state.show_component_deviations
mx = mean(dx); my = mean(dy);
for i = 1:length(dx)
plot(ax, [mx, dx(i)], [my, my], 'Color', [cx, 0.5], 'LineWidth', 1.5);
plot(ax, [dx(i), dx(i)], [my, dy(i)], 'Color', [cy, 0.5], 'LineWidth', 1.5);
end
end
% points on top => make them clickable too
h = scatter(ax, dx, dy, state.dot_size, state.dot_color, 'filled', 'MarkerEdgeColor', 'w', 'LineWidth', 1);
set(h, 'ButtonDownFcn', @(src,~) click_2d(ax, dx, dy));
% mean point visual
if state.show_mean
scatter(ax, mean(dx), mean(dy), 200, state.colors.mean, 'filled', 'MarkerEdgeColor', 'w', 'LineWidth', 2);
end
% selected point highlight on top of everything
if state.selected_point_idx > 0 && state.selected_point_idx <= length(dx)
draw_2d_highlight(ax, dx, dy, state.selected_point_idx);
end
title(ax, sprintf('%s vs %s', lx, ly), 'Color', 'w', 'FontSize', 13);
xlabel(ax, lx, 'Color', cx); ylabel(ax, ly, 'Color', cy);
set(ax, 'XLim', [-12, 12], 'YLim', [-12, 12]);
set(ax, 'Color', [0.05, 0.05, 0.05], 'XColor', [0.6, 0.6, 0.6], 'YColor', [0.6, 0.6, 0.6]);
grid(ax, 'on'); set(ax, 'GridColor', [0.3, 0.3, 0.3]);
axis(ax, 'equal');
hold(ax, 'off');
end
function click_2d(ax, dx, dy)
%handles click on 2D scatter plot to select a point
fig = ancestor(ax, 'figure');
state = getappdata(fig, 'state');
%dont allow clicks during animation or it breaks everyting
if state.animating
return;
end
%get click location in data coords
pt = get(ax, 'CurrentPoint');
x_click = pt(1,1); y_click = pt(1,2);
%find closest point to click using euclidean dist
distances = sqrt((dx - x_click).^2 + (dy - y_click).^2);
[~, idx] = min(distances);
current_state = getappdata(fig, 'state');
%toggle => click same point again to clear seleciton
if idx == current_state.selected_point_idx
current_state.selected_point_idx = -1;
else
current_state.selected_point_idx = idx;
end
setappdata(fig, 'state', current_state);
update_display(fig);
end
function click_deviation_2d(fig, idx, dx, dy, cx, cy)
%clicked a deviation line so do the decomposition animation
%this is the cool part where euclidean breaks into x adn y
state = getappdata(fig, 'state');
if state.animating
return;
end
state.animating = true;
setappdata(fig, 'state', state);
animate_deviation_decomposition_2d(state.ax_main, dx, dy, idx, cx, cy, state);
%done animating so reset flag
state = getappdata(fig, 'state');
state.animating = false;
setappdata(fig, 'state', state);
end
function animate_deviation_decomposition_2d(ax, dx, dy, idx, cx, cy, state)
%animate breaking down 2D deviation into x and y components
%3 phases: show euclidean then crosshairs then componenets
mx = mean(dx);
my = mean(dy);
x = dx(idx);
y = dy(idx);
cla(ax, 'reset');
hold(ax, 'on');
view(ax, 2);
%animation params (can tweak these for speed)
n_frames = 50;
pause_time = 0.03;
%phase 1 => show original 2D deviation
%draw the euclidean line form point to mean
for frame = 1:15
cla(ax);
hold(ax, 'on');
% faded background
scatter(ax, dx, dy, state.dot_size*0.5, [0.3, 0.3, 0.3], 'filled', 'MarkerEdgeAlpha', 0.3);
% mean
scatter(ax, mx, my, 150, state.colors.mean, 'filled', 'MarkerEdgeColor', 'w', 'LineWidth', 2);
% selected point
scatter(ax, x, y, state.dot_size*1.5, 'y', 'filled', 'MarkerEdgeColor', 'w', 'LineWidth', 2);
% 2D deviation line
plot(ax, [x, mx], [y, my], 'Color', [1, 1, 0], 'LineWidth', 3);
text(ax, mx + (x-mx)*0.5, my + (y-my)*0.5 + 1, 'Euclidean Deviation', ...
'Color', 'y', 'FontSize', 12, 'FontWeight', 'bold', 'HorizontalAlignment', 'center');
set(ax, 'XLim', [-12, 12], 'YLim', [-12, 12]);
set(ax, 'Color', [0.05, 0.05, 0.05]);
axis(ax, 'equal');
grid(ax, 'on');
drawnow;
pause(pause_time);
end
%phase 2 => transform mean point into crosshairs
%fade out mean dot adn grow the axis lines
for frame = 1:15
cla(ax);
hold(ax, 'on');
alpha = frame / 15;
% faded background
scatter(ax, dx, dy, state.dot_size*0.3, [0.2, 0.2, 0.2], 'filled', 'MarkerEdgeAlpha', 0.2);
% mean transforms into cross
point_size = 150 * (1 - alpha);