Repository navigation
Expand file tree
/
Copy pathtutorial_end_to_end.m
More file actions
342 lines (288 loc) · 13.3 KB
/
Copy pathtutorial_end_to_end.m
File metadata and controls
342 lines (288 loc) · 13.3 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
%% tutorial_end_to_end.m - Full pipeline: Import → Process → Blocks → Experiment
%
% This tutorial walks through the complete processFNIRS2 workflow:
%
% 1. Import raw fNIRS data
% 2. Set processing options and convert to hemoglobin
% 3. Define task blocks from event markers
% 4. Extract block segments (time-locked epochs)
% 5. Create an Experiment for group analysis
% 6. Three output paths:
% a) Scripted stats & plots (no GUI)
% b) Export to CSV or MATLAB table
% c) Open the exploreFNIRS GUI with settings pre-loaded
%
% The sample data has no markers, so we inject synthetic ones to
% demonstrate the block workflow. With real data you'd skip that step.
%
% Requirements:
% - processFNIRS2 on the MATLAB path
% - Sample data: pf2.import.sampleData.fNIR2000()
cd(fileparts(mfilename('fullpath')));
cd('../..'); % project root
% Uncomment to save figures and exports to disk:
% outDir = fullfile(tempdir, 'pf2_tutorial');
% if ~exist(outDir, 'dir'), mkdir(outDir); end
%% ========================================================================
% PART 1: IMPORT
% ========================================================================
%
% processFNIRS2 supports several device formats:
%
% data = pf2.import.importNIR(filepath); % fNIR Devices / Biopac (.nir)
% data = pf2.import.importSNIRF(filepath); % SNIRF format (.snirf)
% data = pf2.import.importHitachiMES(filepath); % Hitachi ETG-4000 (.mes)
% data = pf2.import.importNIRX(filepath); % NIRx (.hdr/.wl1/.wl2)
%
% For this tutorial we use built-in sample data:
fprintf('=== Part 1: Import ===\n');
raw = pf2.import.sampleData.fNIR2000();
fprintf(' Channels: %d\n', size(raw.raw, 2));
fprintf(' Samples: %d\n', size(raw.raw, 1));
fprintf(' Rate: %.1f Hz\n', raw.fs);
fprintf(' Duration: %.1f seconds\n', max(raw.time) - min(raw.time));
%% ========================================================================
% PART 2: PROCESS
% ========================================================================
%
% processFNIRS2 converts raw light intensity to hemoglobin concentrations
% in three stages:
%
% Stage 1: Raw intensity → Optical density (log transform)
% Stage 2: OD → Hemoglobin (Modified Beer-Lambert Law)
% Stage 3: Hemoglobin → Filtered hemoglobin
%
% Key parameters:
% ShowGUI - false for headless / true for interactive
% DPFmode - 'Calc' (age-based), 'Fixed', or 'None'
% defaultSubjectAge - age in years (for DPF calculation)
% blLength - baseline length in seconds
% blStartTime - baseline start (seconds from recording start)
% Raw_Method - name of raw-stage processing method (e.g. 'OD_TDDR')
% Oxy_Method - name of oxy-stage processing method (e.g. 'lpf_car')
fprintf('\n=== Part 2: Process ===\n');
processed = processFNIRS2(raw, ...
'DPFmode', 'Calc', ...
'defaultSubjectAge', 30, ...
'blLength', 10, ...
'blStartTime', 0);
fprintf(' Output fields: HbO, HbR, HbTotal, HbDiff, CBSI\n');
fprintf(' HbO size: %d timepoints x %d channels\n', size(processed.HbO));
fprintf(' Units: %s\n', processed.units);
%% ========================================================================
% PART 3: DEFINE BLOCKS
% ========================================================================
%
% Task blocks are epochs of interest within a continuous recording.
% They're defined by event markers embedded in the data.
%
% Markers format: [time_sec, code, duration, amplitude]
%
% The sample data has no markers, so we create a synthetic experiment:
% - 6 blocks alternating between "Task A" (code 10) and "Task B" (code 20)
% - Each block is 30 seconds long
% - Blocks start every 60 seconds beginning at t=60
fprintf('\n=== Part 3: Define Blocks ===\n');
% Inject synthetic markers (skip this with real data that has markers)
processed.markers = pf2_base.normalizeMarkers([
60, 10, 0, 1; % Task A at 60s
120, 20, 0, 1; % Task B at 120s
180, 10, 0, 1; % Task A at 180s
240, 20, 0, 1; % Task B at 240s
300, 10, 0, 1; % Task A at 300s
360, 20, 0, 1; % Task B at 360s
]);
% Define blocks: marker codes [10, 20], 30 seconds each
% ConditionMap labels each code with a human-readable name
blocks = pf2.data.defineBlocks(processed, ...
'MarkerCode', [10, 20], ...
'Duration', 30, ...
'ConditionMap', {10, 'TaskA'; 20, 'TaskB'}, ...
'Embed', false);
fprintf(' Found %d blocks:\n', length(blocks));
for i = 1:length(blocks)
fprintf(' Block %d: code=%d (%s), %.0f-%.0fs\n', ...
i, blocks(i).markerCode, blocks(i).info.Condition, ...
blocks(i).startTime, blocks(i).endTime);
end
%% ========================================================================
% PART 4: EXTRACT SEGMENTS
% ========================================================================
%
% extractBlocks cuts the continuous recording into time-locked segments.
%
% Key options:
% PreTime - seconds before block onset to include (for baseline)
% PostTime - seconds after block end to include
% BaselineWindow - [start, end] relative to onset for baseline correction
% SetT0 - shift time so block onset = 0
% CopyInfo - merge parent .info fields into each segment
fprintf('\n=== Part 4: Extract Segments ===\n');
segments = pf2.data.extractBlocks(processed, blocks, ...
'PreTime', 5, ... % 5s before onset (baseline)
'PostTime', 15, ... % 15s after block end (HRF tail)
'BaselineWindow', [-5, 0], ... % baseline correction window
'SetT0', true, ... % onset = t0
'CopyInfo', true);
fprintf(' Extracted %d segments\n', length(segments));
seg1 = segments{1};
fprintf(' Segment 1: %.1f to %.1f s, %d channels\n', ...
min(seg1.time), max(seg1.time), size(seg1.HbO, 2));
fprintf(' Info fields: %s\n', strjoin(fieldnames(seg1.info), ', '));
%% ========================================================================
% PART 5: BUILD MULTI-SUBJECT DATASET
% ========================================================================
%
% In a real experiment you'd process each subject's file separately and
% collect all segments. Here we simulate 3 subjects by duplicating and
% labeling the segments.
fprintf('\n=== Part 5: Build Multi-Subject Dataset ===\n');
rng(42);
allSegments = {};
subjectIDs = {'Sub01', 'Sub02', 'Sub03'};
groups = {'Young', 'Young', 'Older'};
for s = 1:length(subjectIDs)
for i = 1:length(segments)
seg = segments{i};
% Label this segment
seg.info.SubjectID = subjectIDs{s};
seg.info.Group = groups{s};
seg.info.Session = 'S1';
seg.info.Trial = ceil(i / 2); % blocks pair into trials
% Add some subject-level variation (synthetic)
noise = 0.05 * randn(size(seg.HbO));
seg.HbO = seg.HbO + noise * (s * 0.3);
seg.HbR = seg.HbR - noise * (s * 0.2);
allSegments{end+1} = seg; %#ok<SAGROW>
end
end
fprintf(' Total segments: %d (%d subjects x %d blocks)\n', ...
length(allSegments), length(subjectIDs), length(segments));
%% ========================================================================
% PART 6: CREATE AN EXPERIMENT
% ========================================================================
%
% The Experiment class is the bridge between single-subject processing
% and group analysis. It organizes segments, handles filtering/grouping,
% and manages hierarchical within-subject averaging.
fprintf('\n=== Part 6: Create Experiment ===\n');
ex = exploreFNIRS.core.Experiment(allSegments, ...
'Hierarchy', {'SubjectID', 'Session', 'Condition', 'Trial'});
% Configure analysis settings
ex.settings.baseline = [-5, 0]; % baseline window (seconds)
ex.settings.taskStart = 0; % task onset
ex.settings.resampleRate = 1; % resample to 1 Hz for temporal
ex.settings.barBinSize = 10; % 10-second bins for bar charts
ex.settings.useBaseline = true; % apply baseline correction
ex.settings.avgMode = 'hierarchy'; % hierarchical averaging
ex.summary();
%% ========================================================================
% PATH A: SCRIPTED ANALYSIS (no GUI)
% ========================================================================
fprintf('\n=== Path A: Scripted Analysis ===\n');
% --- A1: Select and group ---
ex.select('Condition', {'TaskA', 'TaskB'}); % keep both conditions
ex.groupby({'Condition'});
ex.aggregate();
% --- A2: Temporal plot ---
fig = ex.plotTemporal('Biomarkers', {'HbO', 'HbR'}, 'Channels', 1:5, ...
'Title', 'TaskA vs TaskB: HbO & HbR');
% fig = ex.plotTemporal('Biomarkers', {'HbO', 'HbR'}, 'Channels', 1:5, ...
% 'Title', 'TaskA vs TaskB: HbO & HbR', ...
% 'Visible', 'off', 'SavePath', fullfile(outDir, 'temporal.png'));
% close(fig);
% --- A3: Bar chart ---
fig = ex.plotBar('Biomarker', 'HbO', 'Channels', 1:5, ...
'TimeWindow', [5, 25], 'ShowIndividual', true, ...
'Title', 'Mean HbO (5-25s)');
% fig = ex.plotBar('Biomarker', 'HbO', 'Channels', 1:5, ...
% 'TimeWindow', [5, 25], 'ShowIndividual', true, ...
% 'Title', 'Mean HbO (5-25s)', ...
% 'Visible', 'off', 'SavePath', fullfile(outDir, 'bar.png'));
% close(fig);
% --- A4: LME statistics ---
results = ex.statsFitLME('Biomarkers', {'HbO'}, 'Channels', 1:5);
fprintf(' LME formula: %s\n', results.formula);
fprintf(' ANOVA p-values (Condition effect, channels 1-5):\n');
disp(results.anova_pval);
% --- A5: Summarize for publication ---
T_anova = ex.statsSummarize(results, 'Type', 'anova');
fprintf(' ANOVA summary table (%d rows):\n', height(T_anova));
disp(T_anova);
% --- A6: Group x Condition (reset and regroup) ---
ex.reset();
ex.select('Condition', {'TaskA', 'TaskB'});
ex.groupby({'Group', 'Condition'});
ex.aggregate();
fig = ex.plotBar('Biomarker', 'HbO', 'Channels', 1:5, ...
'TimeWindow', [5, 25], 'ShowIndividual', true, ...
'Title', 'Group x Condition: HbO');
% fig = ex.plotBar('Biomarker', 'HbO', 'Channels', 1:5, ...
% 'TimeWindow', [5, 25], 'ShowIndividual', true, ...
% 'Title', 'Group x Condition: HbO', ...
% 'Visible', 'off', 'SavePath', fullfile(outDir, 'bar_group_x_cond.png'));
% close(fig);
%% ========================================================================
% PATH B: EXPORT DATA
% ========================================================================
fprintf('\n=== Path B: Export Data ===\n');
% Make sure we have aggregated data
ex.reset();
ex.select('Condition', {'TaskA', 'TaskB'});
ex.groupby({'Group', 'Condition'});
ex.aggregate();
% --- B1: Export to CSV (long format, for R / Python / SPSS) ---
% ex.writeCSV(fullfile(outDir, 'export_long.csv'), ...
% 'Format', 'long', ...
% 'Biomarkers', {'HbO', 'HbR'}, ...
% 'Channels', 1:10);
% --- B2: Export to CSV (wide format) ---
% ex.writeCSV(fullfile(outDir, 'export_wide.csv'), ...
% 'Format', 'wide', ...
% 'Biomarkers', {'HbO'}, ...
% 'Channels', 1:10);
% --- B3: Get as MATLAB table (for further scripting) ---
T = ex.toLongTable({'HbO', 'HbR'}, 1:5);
fprintf(' MATLAB table: %d rows x %d columns\n', height(T), width(T));
fprintf(' Columns: %s\n', strjoin(T.Properties.VariableNames, ', '));
% --- B4: Save MATLAB table to .mat ---
% save(fullfile(outDir, 'results_table.mat'), 'T');
% fprintf(' Saved MATLAB table to results_table.mat\n');
% --- B5: Batch export fNIRS structs to SNIRF files ---
% Use asSNIRF or asNIR with a cell array and a directory path.
% Dir1-Dir4 map .info field values to subdirectories (inverse of importDirectory).
% Prefix builds filenames from .info field values.
% snirfOutDir = fullfile(outDir, 'snirf_export');
% pf2.export.asSNIRF(allSegments, snirfOutDir, ...
% 'Dir1', 'Group', 'Prefix', {'SubjectID', 'Condition'});
% fprintf(' Batch exported %d segments to %s\n', length(allSegments), snirfOutDir);
%% ========================================================================
% PATH C: OPEN THE GUI
% ========================================================================
%
% Pass the Experiment directly into the exploreFNIRS GUI.
% All settings (baseline, resample rate, hierarchy, avg mode) are
% pre-populated. You can then interactively adjust, re-group, plot,
% and export from the GUI.
%
% Uncomment the line below to open:
fprintf('\n=== Path C: GUI ===\n');
fprintf(' To open the GUI with this Experiment:\n');
fprintf(' exploreFNIRS(ex)\n');
fprintf(' Settings will be pre-loaded from the Experiment object.\n');
% exploreFNIRS(ex); % <-- uncomment to open
%% ========================================================================
% SUMMARY
% ========================================================================
fprintf('\n=== Tutorial complete ===\n');
fprintf('\nPipeline recap:\n');
fprintf(' 1. pf2.import.*() → raw fNIRS struct\n');
fprintf(' 2. processFNIRS2() → hemoglobin concentrations\n');
fprintf(' 3. pf2.data.defineBlocks() → block definitions\n');
fprintf(' 4. pf2.data.extractBlocks() → time-locked segments\n');
fprintf(' 5. exploreFNIRS.core.Experiment() → group analysis container\n');
fprintf(' 6. ex.groupby() → ex.aggregate() → averaged data\n');
fprintf(' 7a. ex.plot.* / ex.statsFitLME() → scripted analysis\n');
fprintf(' 7b. ex.writeCSV() / ex.toLongTable() → tabular export\n');
fprintf(' 7c. pf2.export.asSNIRF(cells, dir) → batch file export\n');
fprintf(' 7d. exploreFNIRS(ex) → GUI exploration\n');