Repository navigation
Expand file tree
/
Copy pathexample_hyperscanning.m
More file actions
300 lines (245 loc) · 10.6 KB
/
Copy pathexample_hyperscanning.m
File metadata and controls
300 lines (245 loc) · 10.6 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
%% example_hyperscanning.m - Inter-brain synchrony analysis
%
% Demonstrates hyperscanning (inter-brain coupling) analysis for paired
% participants. Uses ROIs defined before processing so that
% processStageFilterHb auto-builds ROI signals during reprocessing.
%
% Covers:
% 1. Data preparation: assign dyad metadata, define ROIs, process
% 2. ROI-level inter-brain coupling
% 3. Permutation testing for significance
% 4. Block-wise hyperscanning (coupling per task condition)
% 5. Dyad-level visualization (heatmap + inter-brain topo)
% 6. Channel-level hyperscanning (full cross-brain matrix)
% 7. Within-subject connectivity for comparison
% 8. Multiple coupling methods
% 9. Connectogram of a condition contrast (node-colored Delta-r)
%
% Requirements:
% - processFNIRS2 on path
% - Sample data available via pf2.import.sampleData.fNIR2000()
% Uncomment to save figures instead of displaying them:
% outDir = '/tmp/hyperscanning_examples';
% if ~exist(outDir, 'dir'), mkdir(outDir); end
%% Step 0: Generate paired data, define ROIs, process
fprintf('=== Step 0: Generate paired data, define ROIs, process ===\n');
[subjects, blockDefs] = pf2.import.sampleData.experiment('blocks');
[rawMethod, oxyMethod] = pf2_base.examples.addDemoPipelines();
% Assign dyad metadata
dyadIDs = {'D1', 'D1', 'D2', 'D2'};
roles = {'Speaker', 'Listener', 'Speaker', 'Listener'};
for s = 1:length(subjects)
subjects{s}.info.DyadID = dyadIDs{s};
subjects{s}.info.Role = roles{s};
end
% ROI definitions
nCh = size(subjects{1}.HbO, 2);
roiSize = floor(nCh / 3);
roiDef = {1:roiSize; roiSize+1:2*roiSize; 2*roiSize+1:nCh};
roiNames = {'Left_PFC', 'Center_PFC', 'Right_PFC'};
subjects = pf2.probe.roi.defineROI(subjects, roiDef, roiNames);
subjects = processFNIRS2(subjects, rawMethod, oxyMethod);
fprintf(' %d subjects in %d dyads\n', length(subjects), length(unique(dyadIDs)));
fprintf(' ROI channels: %s\n', strjoin(roiNames, ', '));
%% Step 1: ROI-level inter-brain coupling
fprintf('\n=== Step 1: ROI-level inter-brain coupling ===\n');
ex = exploreFNIRS.core.Experiment(subjects);
result = ex.hyperscanning( ...
'Method', 'pearson', ...
'Biomarker', 'HbO', ...
'UseROI', true, ...
'ChannelPairing', 'same');
fprintf(' Mean inter-brain coupling per ROI:\n');
for r = 1:length(result.channels)
fprintf(' ROI %d: r = %.3f (p = %.4f)\n', ...
r, result.Mean(r), result.pvalue(r));
end
fig = exploreFNIRS.hyperscanning.plotGroup(result, ...
'ShowSignificance', true, ...
'ChannelLabels', roiNames, ...
'Title', 'Inter-Brain Coupling (ROI-level, Pearson)');
% fig = exploreFNIRS.hyperscanning.plotGroup(result, ...
% 'ShowSignificance', true, ...
% 'ChannelLabels', roiNames, ...
% 'Title', 'Inter-Brain Coupling (ROI-level, Pearson)', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step1_group_bar.png'));
% close(fig);
%% Step 2: Permutation testing
fprintf('\n=== Step 2: Permutation testing ===\n');
permResult = ex.hyperscanning( ...
'Method', 'pearson', ...
'Biomarker', 'HbO', ...
'UseROI', true, ...
'ChannelPairing', 'same', ...
'Permutations', 500, ...
'PThreshold', 0.05);
if isfield(permResult, 'permutation') && ~isempty(permResult.permutation)
perm = permResult.permutation;
fprintf(' Significant ROIs (permutation p < .05): %d / %d\n', ...
sum(perm.significant), length(perm.significant));
for r = 1:length(perm.significant)
sigStr = '';
if perm.significant(r), sigStr = '*'; end
fprintf(' ROI %d: observed=%.3f, null mean=%.3f, p=%.4f %s\n', ...
r, perm.observed(r), perm.nullMean(r), perm.pvalue(r), sigStr);
end
end
fig = exploreFNIRS.hyperscanning.plotGroup(permResult, ...
'ShowSignificance', true, ...
'ChannelLabels', roiNames, ...
'Title', 'Inter-Brain Coupling (with permutation test)');
% fig = exploreFNIRS.hyperscanning.plotGroup(permResult, ...
% 'ShowSignificance', true, ...
% 'ChannelLabels', roiNames, ...
% 'Title', 'Inter-Brain Coupling (with permutation test)', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step2_permutation.png'));
% close(fig);
%% Step 3: Block-wise hyperscanning
fprintf('\n=== Step 3: Block-wise hyperscanning ===\n');
blocks = blockDefs{1};
blockResult = ex.hyperscanning( ...
'Method', 'pearson', ...
'Biomarker', 'HbO', ...
'UseROI', true, ...
'ChannelPairing', 'same', ...
'Blocks', blocks);
for b = 1:length(blockResult)
fprintf(' Block %d (%s, %.0f-%.0fs): mean r = %.3f\n', ...
b, blockResult(b).blockInfo.Condition, ...
blockResult(b).startTime, blockResult(b).endTime, ...
mean(blockResult(b).coupling.Mean, 'omitnan'));
end
fig = exploreFNIRS.hyperscanning.plotGroup(blockResult, ...
'ChannelLabels', roiNames, ...
'Title', 'Inter-Brain Coupling per Block');
% fig = exploreFNIRS.hyperscanning.plotGroup(blockResult, ...
% 'ChannelLabels', roiNames, ...
% 'Title', 'Inter-Brain Coupling per Block', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step3_block_wise.png'));
% close(fig);
%% Step 4: Dyad-level visualization
fprintf('\n=== Step 4: Dyad-level visualization ===\n');
fig = exploreFNIRS.hyperscanning.plotDyadMatrix(result, ...
'SortDyads', 'mean', ...
'Title', 'Dyad x ROI Coupling');
% fig = exploreFNIRS.hyperscanning.plotDyadMatrix(result, ...
% 'SortDyads', 'mean', ...
% 'Title', 'Dyad x ROI Coupling', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step4_dyad_matrix.png'));
% close(fig);
fig = exploreFNIRS.hyperscanning.plotInterBrainTopo(result, ...
'BrainLabels', {'Speaker', 'Listener'}, ...
'LineThreshold', 0.1, ...
'Title', 'Inter-Brain Topology');
% fig = exploreFNIRS.hyperscanning.plotInterBrainTopo(result, ...
% 'BrainLabels', {'Speaker', 'Listener'}, ...
% 'LineThreshold', 0.1, ...
% 'Title', 'Inter-Brain Topology', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step4_inter_brain_topo.png'));
% close(fig);
%% Step 5: Channel-level hyperscanning (full cross-brain matrix)
fprintf('\n=== Step 5: Channel-level cross-brain matrix ===\n');
chResult = ex.hyperscanning( ...
'Method', 'pearson', ...
'Biomarker', 'HbO', ...
'ChannelPairing', 'all');
fprintf(' Cross-brain matrix: %d x %d\n', size(chResult.Mean));
fprintf(' Mean coupling: %.3f\n', mean(chResult.Mean(:), 'omitnan'));
fig = exploreFNIRS.connectivity.plotMatrix(chResult, ...
'Title', 'Cross-Brain Matrix (all channel pairs)');
% fig = exploreFNIRS.connectivity.plotMatrix(chResult, ...
% 'Title', 'Cross-Brain Matrix (all channel pairs)', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step5_cross_brain_matrix.png'));
% close(fig);
%% Step 6: Within-subject connectivity for comparison
fprintf('\n=== Step 6: Within-subject connectivity ===\n');
ex.groupby({'Group'});
withinResult = ex.interROI('Method', 'pearson', 'Biomarker', 'HbO');
fprintf(' Within-subject ROI connectivity vs inter-brain coupling:\n');
for g = 1:length(withinResult)
fprintf(' %s within-subject: mean r = %.3f\n', ...
withinResult(g).label, withinResult(g).globalMean);
end
fprintf(' Inter-brain (all dyads): mean r = %.3f\n', ...
mean(result.Mean, 'omitnan'));
withinT = exploreFNIRS.report.connectivitySummary(withinResult);
fprintf('\n Within-subject connectivity summary:\n');
disp(withinT);
%% Step 7: Multiple coupling methods
fprintf('\n=== Step 7: Multiple coupling methods ===\n');
methods = {'pearson', 'spearman', 'coherence'};
for m = 1:length(methods)
methodResult = ex.hyperscanning( ...
'Method', methods{m}, ...
'Biomarker', 'HbO', ...
'UseROI', true, ...
'ChannelPairing', 'same');
fprintf(' %s: mean inter-brain coupling = %.3f\n', ...
methods{m}, mean(methodResult.Mean, 'omitnan'));
end
%% Step 8: Connectogram of a condition contrast (node-colored Delta-r)
%
% Reproduce the publication-style circular connectogram: channel nodes on a
% ring, coupled pairs joined by arcs, and each NODE colored by a per-node
% contrast (here Delta-r between the two task conditions) with its own
% colorbar. Region anchors (ROI names) label the ring; edges are drawn in a
% single subtle color so the node contrast reads cleanly.
fprintf('\n=== Step 8: Connectogram (condition contrast, Delta-r) ===\n');
subj = subjects{1};
% The two task conditions are encoded by marker code. Build a within-brain
% channel connectivity matrix for each condition by averaging that
% condition's block windows, then take the per-node strength difference.
codes = unique([blocks.markerCode]);
codes = codes(1:min(2, numel(codes)));
% Per-node strength = mean off-diagonal coupling (autocorrelation excluded).
nodeStrength = @(M) (sum(M, 2, 'omitnan') - diag(M)) ./ max(size(M, 1) - 1, 1);
condMat = cell(1, numel(codes));
lastR = [];
for ci = 1:numel(codes)
bsel = blocks([blocks.markerCode] == codes(ci));
accum = []; cnt = 0;
for k = 1:numel(bsel)
w = [bsel(k).startTime, bsel(k).endTime];
if diff(w) <= 0, continue; end
r = exploreFNIRS.connectivity.computeMatrix(subj, ...
'Method', 'pearson', 'Biomarker', 'HbO', 'TimeWindow', w);
if isempty(accum), accum = zeros(size(r.matrix)); end
accum = accum + r.matrix; cnt = cnt + 1; lastR = r;
end
condMat{ci} = accum ./ max(cnt, 1);
end
% Node Delta-r contrast = node strength in condition A minus condition B.
dr = nodeStrength(condMat{1}) - nodeStrength(condMat{2});
% Edges: the condition-A matrix. Nodes grouped under their ROI region.
connResult = lastR;
connResult.matrix = condMat{1};
connResult.pmatrix = [];
grp = repmat({''}, 1, nCh);
for rr = 1:numel(roiDef)
grp(roiDef{rr}) = roiNames(rr);
end
fprintf(' Node Delta-r range: [%.3f, %.3f] across %d channels\n', ...
min(dr), max(dr), nCh);
fig = exploreFNIRS.connectivity.plotChord(connResult, ...
'NodeValues', dr, 'NodeColormap', 'rdbu', ...
'ColorbarLabel', '\Deltar (Task A - Task B)', ...
'EdgeColor', [0.55 0.75 0.88], 'ArcAlpha', 0.5, ...
'GroupLabels', grp, 'MinThreshold', 0.5, ...
'Title', 'Within-Brain Connectogram (condition contrast)');
% fig = exploreFNIRS.connectivity.plotChord(connResult, ...
% 'NodeValues', dr, 'NodeColormap', 'rdbu', ...
% 'ColorbarLabel', '\Deltar (Task A - Task B)', ...
% 'EdgeColor', [0.55 0.75 0.88], 'ArcAlpha', 0.5, ...
% 'GroupLabels', grp, 'MinThreshold', 0.5, ...
% 'Title', 'Within-Brain Connectogram (condition contrast)', ...
% 'Visible', 'off', ...
% 'SavePath', fullfile(outDir, 'step8_connectogram.png'));
% close(fig);
%% Summary
fprintf('\n=== Hyperscanning tutorial complete ===\n');