-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_roi_analysis_example.m
More file actions
130 lines (104 loc) · 4.71 KB
/
run_roi_analysis_example.m
File metadata and controls
130 lines (104 loc) · 4.71 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
%% RUN_ROI_ANALYSIS_EXAMPLE — synthetic ROI analysis demo
%
% This example walks through:
% 1. Computing a PCA-based similarity metric for ROI shapes across days
% 2. Matching real and shuffled ROIs across 8 simulated days
% 3. Estimating ROI functional dimensionality (Litwin-Kumar et al., 2017)
%
% Reference (for dimensionality metric):
% Litwin-Kumar, A. et al. (2017) Optimal degrees of synaptic connectivity.
% Neuron, 93, 1153-1164.
%
% -------------------------------------------------------------------------
close all; clearvars;
rng(10,'twister');
% Add utils/ folder -------------------------------------------------------
thisFile = '~\run_roi_analysis_example.m';
projRoot = fileparts(thisFile);
addpath(fullfile(projRoot,'utils'));
%% Synthetic dataset parameters -------------------------------------------
params = struct(...
'threshold', 0.5, ... % threshold for matching
'shuffle', false, ... % Whether to shuffle ROIs
'nRois', 50, ... % number of ROIs
'fovSize', 512, ... % pixels for field of view
'traceLen', 1000, ... % time points per activity trace
'nDays', 8, ... % number of consecutive days
'rBase', [10 25], ... % min and max radius for ROIs
'centerJitter', 8, ... % position jitter
'radiusJitter', 8, ... % radius jitter
'eventProb', 0.01, ... % per-frame spike probability
'tau', 10, ... % decay constant of Ca kernel
'noiseSD', 0.025, ... % additive Gaussian noise
'baselineLevel', 0.2); % Add baseline to synthetic traces
viz = struct(...
'dayNum', 1, ...
'offsetK',1.2, ...
'colTab', lines(params.nRois));
%% Generate synthetic dataset ---------------------------------------------
sim = generate_synthetic_data(params);
disp('... synthetic data created');
%% Plot synthetic dataset -------------------------------------------------
plot_synthetic_dataset(sim,params,viz)
disp('... synthetic data plotted');
%% PCA-based ROI (dis)similarity ------------------------------------------
distance_matrix = compute_pca_similarity(sim,params);
disp('... calculated PCA-based similarity metric');
% --- plot ----------------------------------------------------------------
figure('Name','PCA similarity matrix');
imagesc(distance_matrix);
axis image tight
colorbarHandle = colorbar;
ylabel(colorbarHandle, 'Dissimilarity');
xlabel('ROI index');
ylabel('ROI index');
title(sprintf('PCA-based (dis)similarity — %d days', params.nDays));
box on
%% Match original and shuffled ROIs over all days -------------------------
results = struct('label', {'Real','Shuffled'}, ...
'shuffle', {false, true}, ...
'percent', []);
for k = 1:numel(results)
params.shuffle = results(k).shuffle;
stats = match_ROIs_across_days(sim, params);
results(k).percent = [stats.percent_matched] * 100;
end
disp('... Matched real and shuffled ROIs');
percent_matched = results(1).percent;
percent_matched_shuffled = results(2).percent;
ci95 = @(x) tinv(0.975,numel(x)-1) * std(x,0,2) / sqrt(numel(x));
mu = [mean(percent_matched), mean(percent_matched_shuffled)];
ci = [ci95(percent_matched), ci95(percent_matched_shuffled)];
% --- plot ----------------------------------------------------------------
figure('Name','Matching accuracy'); hold on
b = bar(mu,'FaceColor','flat','BarWidth',0.6);
b.CData = [viz.colTab(1,:); viz.colTab(2,:)];
errorbar(1:2, mu, ci, 'k', 'linestyle','none', 'linewidth',1.5);
set(gca,'XTick',1:2,'XTickLabel',{'Matched','Shuffled'});
ylabel('% ROIs correctly matched');
ylim([0, max(mu+ci)+5]);
title(sprintf('ROI matching (mean ±95%% CI, n = %d days)', params.nDays));
box on
text(1:2, mu+ci+2, compose('%.1f%%', mu), 'HorizontalAlignment','center');
%% Compute dimensionality of ROI functional activity ----------------------
dimAcrossDays = zeros(params.nDays,1);
for d = 1:params.nDays
dF = reshape([sim(d,:).trace], params.traceLen, []);
dimAcrossDays(d) = compute_dimensionality(dF);
end
disp('... Calculated functional dimensionality');
muDim = mean(dimAcrossDays);
ciDim = ci95(dimAcrossDays');
% --- plot ----------------------------------------------------------------
figure('Name','ROI functional dimensionality'); hold on
b = bar(muDim, 'FaceColor',viz.colTab(1,:), 'BarWidth',0.5);
errorbar(1, muDim, ciDim, 'k', 'linestyle','none', 'linewidth',1.5);
ylabel('Dimensionality');
title(sprintf('Mean ± 95%% CI over %d days', params.nDays));
ylim([0, params.nRois+5]);
yline(params.nRois,'--','Max possible (= number of ROIs per day)', ...
'LabelHorizontalAlignment','left','LabelVerticalAlignment','top');
text(1, muDim+ciDim+1.5, sprintf('%.1f', muDim), ...
'HorizontalAlignment','center');
set(gca,'XTick',[]);
box on