-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_bads_reports.asv
303 lines (268 loc) · 11.7 KB
/
create_bads_reports.asv
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
function create_bads_reports(base_folder_path, subs, params)
% Import necessary packages
import mlreportgen.report.*
import mlreportgen.dom.*
% Loop over each subject in the list
for subNumber = subs
% Convert subNumber to a two-digit string
subStr = sprintf('%02d', subNumber);
% Create the report name
reportName = ['sub_', subStr, ' bads'];
% Define the subject folder path
subjectFolderPath = fullfile(base_folder_path, ['sub_', subStr]);
% Create a new report
rpt = Report(fullfile(subjectFolderPath, reportName), 'pdf');
% Add a title page
titlepg = TitlePage;
titlepg.Title = ['Subject ', subStr, ' Preprocessing'];
add(rpt, titlepg);
% Add the table of contents
toc = TableOfContents();
add(rpt, toc);
% Define sections
sections = {'opm', 'meg', 'opmeeg', 'megeeg'};
%%
chapter = Chapter('Parame');
processStruct(myStruct, 'myStruct', chapter);
add(rpt, chapter);
%% Bad Channels chapter
chapter = Chapter('Bad Channels');
chapter.Numbered = false; % Remove chapter numbering
for i_section = 1:length(sections)
filePath = fullfile(subjectFolderPath, ['sub_', subStr, '_' sections{i_section} '_badchs.mat']);
if isfile(filePath)
data = load(filePath);
section = Section(sections(i_section));
section.Numbered = false; % Remove section numbering
fields = fieldnames(data);
totalLength = 0;
for j = 1:length(fields)
varData = data.(fields{j});
if isempty(varData)
varLength = 0;
varList = '';
elseif iscell(varData)
varLength = length(varData);
varList = strjoin(varData, ', ');
else
varLength = max(size(varData));
varList = num2str(varData(:)');
end
totalLength = totalLength + varLength;
para = Paragraph([fields{j},' (n=', num2str(varLength), '): ', varList]);
add(section, para);
end
para = Paragraph(['n_{total}: ', num2str(totalLength)]);
add(section, para);
add(chapter, section);
end
end
add(rpt, chapter);
%% Bad Trials chapter
chapter = Chapter('Bad Trials');
chapter.Numbered = false; % Remove chapter numbering
for i_section = 1:length(sections)
filePath = fullfile(subjectFolderPath, ['sub_', subStr, '_' sections{i_section} '_badtrls.mat']);
if isfile(filePath)
data = load(filePath);
section = Section(sections(i_section));
section.Numbered = false; % Remove section numbering
fields = fieldnames(data);
totalLength = 0;
for j = 1:length(fields)
varData = data.(fields{j});
if isempty(varData)
varLength = 0;
varList = '';
elseif iscell(varData)
varLength = length(varData);
varList = strjoin(varData, ', ');
else
varLength = max(size(varData));
varList = strjoin(arrayfun(@(x) sprintf('%d %d', varData(x, 1), varData(x, 2)), 1:size(varData, 1), 'UniformOutput', false), '; ');
end
totalLength = totalLength + varLength;
para = Paragraph([fields{j},' (n=', num2str(varLength), '): ', varList]);
add(section, para);
end
para = Paragraph(['n_{total}: ', num2str(totalLength)]);
add(section, para);
add(chapter, section);
end
end
add(rpt, chapter);
%% ICA chapter
chapter = Chapter('ICA');
chapter.Numbered = false; % Remove chapter numbering
for i_section = 1:length(sections)
filePath = fullfile(subjectFolderPath,['sub_' subStr '_' sections{i_section} '_ica_comp.mat']);
if isfile(filePath)
data = load(filePath);
section = Section(sections(i_section));
section.Numbered = false; % Remove section numbering
fields = {'ecg_comp_idx', 'eog1_comp_idx', 'eog2_comp_idx'};
totalLength = 0;
for j = 1:length(fields)
varData = data.(fields{j});
if isempty(varData)
varLength = 0;
varList = '';
elseif iscell(varData)
varLength = length(varData);
varList = strjoin(varData, ', ');
else
varLength = max(size(varData));
varList = num2str(varData(:)');
varList = strjoin(arrayfun(@num2str, varData(:)', 'UniformOutput', false), ', ');
end
totalLength = totalLength + varLength;
para = Paragraph([fields{j}, ': ', varList]);
add(section, para);
end
% Define the folder and the pattern
pattern = ['sub_' subStr '_' sections{i_section} '_ica_rejected_comps*.jpg']; % Replace 'your_string' with the starting string
files = dir(fullfile(subjectFolderPath, 'figs', pattern));
if ~isempty(files)
for i_file = 1:length(files)
img = Image(fullfile(subjectFolderPath,'figs',files(i_file).name));
img.Style = {ScaleToFit};
add(section, img);
end
end
add(chapter, section);
end
end
add(rpt, chapter);
%% Butterfly plots chapter
chapter = Chapter('Timelocked');
chapter.Numbered = false; % Remove chapter numbering
for i_phalange = 1:length(params.phalange_labels)
% Add rows and cells to the table and insert the images
section = Section(['Phalange: ' params.phalange_labels(i_phalange)]);
section.Numbered = false; % Remove section numbering
tbl = Table();
tbl.Style = {Border('solid'), Width('100%'), RowSep('solid'), ColSep('solid')};
for i = 1:2
row = TableRow();
for j = 1:2
imgIndex = (j-1)*2 + i;
img = Image(fullfile(subjectFolderPath,'figs',['sub_' subStr '_' sections{imgIndex} '_butterfly_ph-' params.phalange_labels{i_phalange} '.jpg']));
img.Style = {Width('8cm'), ScaleToFit};
entry = TableEntry();
append(entry, img);
append(row, entry);
end
append(tbl, row);
end
add(section, tbl);
add(chapter, section);
add(chapter, PageBreak());
end
add(rpt, chapter);
%% M100 chapter
chapter = Chapter('M100');
chapter.Numbered = false; % Remove chapter numbering
for i_section = 1:length(sections)
section = Section(sections(i_section));
section.Numbered = false; % Remove section numbering
if contains(sections(i_section),'eeg')
fieldMultiplier = 1e9;
fieldUnit = '[nV]';
else
fieldMultiplier = 1e15;
fieldUnit = '[fT]';
end
% Load the .mat file
data = load(fullfile(subjectFolderPath,['sub_' subStr '_' sections{i_section} '_M100.mat']));
M100 = data.M100;
% Create the table with the required data
num_phalanges = length(params.phalange_labels);
T = table('Size', [6, num_phalanges], 'VariableTypes', repmat({'double'}, 1, num_phalanges), 'VariableNames', params.phalange_labels);
T.Properties.RowNames = {['peak_amplitude ' fieldUnit], 'peak_latency [ms]', 'SNR_prestim', 'SNR_stderr', ['std_prestim ' fieldUnit], ['stderr ' fieldUnit]};
for i_phalange = 1:num_phalanges
phalange_data = M100{i_phalange};
T{['peak_amplitude ' fieldUnit], params.phalange_labels{i_phalange}} = fieldMultiplier*phalange_data.max_amplitude;
T{'peak_latency [ms]', params.phalange_labels{i_phalange}} = 1e3*phalange_data.peak_latency;
T{'SNR_prestim', params.phalange_labels{i_phalange}} = phalange_data.max_amplitude / phalange_data.prestim_std;
T{'SNR_stderr', params.phalange_labels{i_phalange}} = phalange_data.max_amplitude / phalange_data.std_error;
T{['std_prestim ' fieldUnit], params.phalange_labels{i_phalange}} = fieldMultiplier*phalange_data.prestim_std;
T{['stderr ' fieldUnit], params.phalange_labels{i_phalange}} = fieldMultiplier*phalange_data.std_error;
end
% Convert the MATLAB table to a DOM table
domTable = Table();
domTable.Style = {Border('solid'), Width('100%'), RowSep('solid'), ColSep('solid')};
% Add header row
headerRow = TableRow();
append(headerRow, TableEntry(' '));
for i_phalange = 1:num_phalanges
headerEntry = TableEntry(params.phalange_labels{i_phalange});
headerEntry.Style = {HAlign('center'), Bold()};
append(headerRow, headerEntry);
end
append(domTable, headerRow);
formatString = {'%.1f','%.f','%.1f','%.2f','%.1f','%.1f'};
% Add data rows
for i_row = 1:height(T)
row = TableRow();
rowNameEntry = TableEntry(T.Properties.RowNames{i_row});
rowNameEntry.Style = {Bold()};
append(row, rowNameEntry);
for j = 1:num_phalanges
entry = TableEntry(num2str(T{i_row, j},formatString{i_row}));
entry.Style = {HAlign('right')};
append(row, entry);
end
append(domTable, row);
end
add(section,domTable);
add(chapter,section)
end
% Max channel plots
for i_phalange = 1:length(params.phalange_labels)
% Add rows and cells to the table and insert the images
section = Section(['Phalange: ' params.phalange_labels(i_phalange)]);
section.Numbered = false; % Remove section numbering
tbl = Table();
tbl.Style = {Border('solid'), Width('100%'), RowSep('solid'), ColSep('solid')};
for i = 1:2
row = TableRow();
for j = 1:2
imgIndex = (j-1)*2 + i;
img = Image(fullfile(subjectFolderPath,'figs',['sub_' subStr '_' sections{imgIndex} '_evoked_maxchannel_ph-' num2str(i_phalange) '.jpg']));
img.Style = {Width('8cm'), ScaleToFit};
entry = TableEntry();
append(entry, img);
append(row, entry);
end
append(tbl, row);
end
add(section, tbl);
add(chapter, section);
add(chapter, PageBreak());
end
add(rpt, chapter);
%% Close the report
close(rpt);
end
function processStruct(s, parentName, chapter)
import mlreportgen.dom.*;
fields = fieldnames(s);
for i = 1:numel(fields)
fieldName = fields{i};
fieldValue = s.(fieldName);
fullName = strcat(parentName, '.', fieldName);
if isstruct(fieldValue)
% If the field is a struct, process it recursively
processStruct(fieldValue, fullName, chapter);
elseif isvector(fieldValue) && isnumeric(fieldValue)
% If the field is a 1-D array, convert it to a comma-separated string
valueStr = strjoin(arrayfun(@num2str, fieldValue, 'UniformOutput', false), ', ');
append(chapter, Paragraph([fullName, ': ', valueStr]));
else
% Otherwise, convert the value to a string and append it
valueStr = num2str(fieldValue);
append(chapter, Paragraph([fullName, ': ', valueStr]));
end
end
end
end