forked from CorentinWicht/RESTINGLAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLOG.m
292 lines (262 loc) · 11.7 KB
/
LOG.m
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
function LOG(SavePath,Date_Start,varargin)
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LOG SCRIPT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WRITE INSTRUCTIONS HERE !!
% Process Secondary Arguments
if nargin > 2
for i = 1:2:length(varargin)
Param = varargin{i};
Value = varargin{i+1};
if ~ischar(Param)
error('Flag arguments must be strings')
end
Param = lower(Param);
switch Param
case 'logmst'
LogMST = Value;
case 'logic'
LogIC = Value;
case 'errorstudy'
ErrorSTUDY = Value;
case 'errorspreproc'
ErrorArtifacts = Value;
otherwise
display (['Unknown parameter setting: ' Param])
end
end
end
%-------------------------------------------------------------------------%
% LOADING GUI MATRICES
%-------------------------------------------------------------------------%
% Retrieving the list of all matrices
ParametersPath=dir([SavePath '\Parameters\' Date_Start '\']);
ParametersPath=ParametersPath(~cell2mat({ParametersPath.isdir}));
% Loading the matrices stored with the GUI
for k=1:length(ParametersPath)
load([ParametersPath(k).folder '\' ParametersPath(k).name]);
end
% If creating new parameters and only running GroupStudy, the
% MainWorkspace.mat might be missing.
if nnz(contains({ParametersPath.name},'MainWorkspace.mat')) < 1
% Get time and date
CurrentDate = datenum(clock);
% List of possible matrices
TempList = dir([SavePath '\Parameters\' '**/*' 'MainWorkspace.mat']);
if length(TempList) > 1
for m=1:length(TempList)
% Find the file whose date is closest to today
SplitDateFolder = strsplit(TempList(m).folder,'\');
SplitDateFolder = strrep(SplitDateFolder{end},'_','-');
SplitDateFolder = strsplit(SplitDateFolder,'-');
SplitDateFolder = [str2double(SplitDateFolder{3}) str2double(SplitDateFolder{2}) ...
str2double(SplitDateFolder{1}) str2double(SplitDateFolder{4 }) ...
str2double(SplitDateFolder{5}) 00];
Datefolders(m) = datenum(SplitDateFolder);
end
% Compute distance from dates to now and take index of smallest
[~, SmallDistDateIdx] = min(CurrentDate - Datefolders);
load([TempList(SmallDistDateIdx).folder '\' TempList(SmallDistDateIdx).name]);
else
load([TempList.folder '\' TempList.name]);
end
end
% Initialize variables
time_end = datestr(now);
username=getenv('USERNAME');
% Decision if doing basic analyses
if exist('AnalysesSwitch','var')
Analysis = 1;
end
% Group analyses log data
if exist('LogMST','var')
MSTVersions = fieldnames(LogMST);
end
% Converting Date_Start to comparable format
SplitDateFolder = strrep(Date_Start,'_','-');
SplitDateFolder = strsplit(SplitDateFolder,'-');
SplitDateFolder = [str2double(SplitDateFolder{3}) str2double(SplitDateFolder{2}) ...
str2double(SplitDateFolder{1}) str2double(SplitDateFolder{4 }) ...
str2double(SplitDateFolder{5}) 00];
time_start = datestr(SplitDateFolder);
% Creating the log file
% date_name = datestr(now,'dd-mm-yy_HHMM');
if sum(~SavePath)<1
OutputPath = [SavePath '\RESTINGLablog_' Date_Start '.txt'];
fid = fopen(OutputPath,'w');
else
OutputPath = [CurrentPWD '\RESTINGLablog_' Date_Start '.txt'];
fid = fopen(OutputPath,'w');
end
% date, starting time, finished time, number of analyzed files
fprintf(fid,'%s\t%s\r\n',['Start : ',time_start],['End: ',time_end]);
fprintf(fid,'\r\n%s\r\n',['Windows username : ' username]);
% Summary of design ----------
BetweenFactors = BetweenFactors(~cellfun('isempty',BetweenFactors));
WithinFactors = WithinFactors(~cellfun('isempty',WithinFactors));
% Between subject
fprintf(fid,'\r\n\r\n%s\r\n','------ DESIGN SUMMARY ------');
if ~isempty(BetweenFactors)
BetweenLevels = BetweenLevels(~cellfun('isempty',BetweenLevels));
for k=1:length(BetweenFactors)
LevelsTemp = '';
for t=1:length(BetweenLevels(:,k))
if t~=length(BetweenLevels(:,k))
LevelsTemp = strcat(LevelsTemp, BetweenLevels{t,k}, ' Vs. ');
else
LevelsTemp = strcat(LevelsTemp, BetweenLevels{t,k});
end
end
fprintf(fid,'\r\n%s',sprintf('Between-subject factor %d : %s, with levels : %s',...
k,upper(BetweenFactors{k}),LevelsTemp));
end
end
% Within subject
if ~isempty(WithinFactors)
WithinLevels = WithinLevels(~cellfun('isempty',WithinLevels));
for k=1:length(WithinFactors)
fprintf(fid,'\r\n\r\n%s',sprintf('Within-subject factor %d : %s, with levels : %s',...
k,upper(WithinFactors{k}),sprintf('%s ',WithinLevels{:,k})));
end
end
% Summary of parameters ---------
fprintf(fid,'\r\n\r\n%s\r\n','------ PARAMETERS SUMMARY ------');
fprintf(fid,'\r\n%s',sprintf('Common files name: %s%s',FileNames, Extension));
fprintf(fid,'\r\n%s',sprintf('Sampling rate : %d Hz',SamplingRate));
fprintf(fid,'\r\n%s',sprintf('Channels list : %s',num2str(Channels)));
if ~isempty(Beginning) || ~isempty(End)
fprintf(fid,'\r\n%s',sprintf('Data length restriction : %d - %d (seconds)',Beginning, End));
else
fprintf(fid,'\r\n%s',sprintf('Data length restriction : NO'));
end
% Summary of preprocessing -------
fprintf(fid,'\r\n\r\n%s\r\n','------ PREPROCESSING SUMMARY ------');
fprintf(fid,'\r\n%s',sprintf('Filtering frequencies : %d-%d Hz',HighPass,LowPass));
fprintf(fid,'\r\n%s',sprintf('Subjects included : %s',num2str(Subjectslist)));
fprintf(fid,'\r\n%s',sprintf('1) Reject asleep epochs : %s',upper(Sleep)));
if strcmpi(Sleep,'Yes')
fprintf(fid,'\r\n%s',sprintf('Seconds/epoch : %d',SecondsEpoch));
end
fprintf(fid,'\r\n%s',sprintf('2) Make processing semi-automatic : %s',upper(Automaticity)));
fprintf(fid,'\r\n%s',sprintf('3) Compute ICA : %s',upper(ICA)));
fprintf(fid,'\r\n%s',sprintf('4) Use ASR : %s',upper(ASR)));
fprintf(fid,'\r\n%s',sprintf('4) Use BLINKER : %s',upper(BLINKER)));
if strcmpi(ICA,'Yes')
% ICA PARAMETERS
fprintf(fid,'\r\n\r\n%s','ICA parameters ----');
fprintf(fid,'\r\n%s',sprintf('- Number of iterations : %s',AnswerICA{:}));
end
if strcmpi(ASR,'Yes')
% ASR PARAMETERS
fprintf(fid,'\r\n\r\n%s','ASR parameters ----');
fprintf(fid,'\r\n%s',sprintf('- Repair bursts using ASR (std) : %s',AnswerASR{1}));
fprintf(fid,'\r\n%s',sprintf('- Remove time windows (0-1) : %s',AnswerASR{2}));
end
if strcmpi(BLINKER,'Yes')
% BLINKER PARAMETERS
BlinkerParam = {'Reject portions of data containing blinks','Interpolate portions containing blinks using ASR'};
fprintf(fid,'\r\n\r\n%s','BLINKER parameters ----');
fprintf(fid,'\r\n%s',sprintf('- BLINKER applied as following : %s',BlinkerParam{str2double(AnswerBLINKER{1})}));
end
% CLEANLINE PARAMETERS
fprintf(fid,'\r\n\r\n%s','CLEANLINE parameters ----');
fprintf(fid,'\r\n%s',sprintf('- Line noise frequencies to remove : %s',AnswerCleanLine{1}));
fprintf(fid,'\r\n%s',sprintf('- P-value for detection of significant sinusoid : %s',AnswerCleanLine{2}));
fprintf(fid,'\r\n%s',sprintf('- Bandwidth [Hz] : %s',AnswerCleanLine{3}));
fprintf(fid,'\r\n%s',sprintf('- Sliding window length (sec) : %s',AnswerCleanLine{4}));
fprintf(fid,'\r\n%s',sprintf('- Sliding window step size (sec) : %s',AnswerCleanLine{5}));
fprintf(fid,'\r\n%s',sprintf('- Window overlap smoothing factor : %s',AnswerCleanLine{6}));
fprintf(fid,'\r\n%s',sprintf('- FFT padding factor : %s',AnswerCleanLine{7}));
% Summary of analyses ----------
if Analysis
fprintf(fid,'\r\n\r\n%s\r\n','------ ANALYSIS SUMMARY ------');
fprintf(fid,'\r\n\r\n%s\r\n','1) Single-subject level ------');
fprintf(fid,'\r\n%s',sprintf('1) Compute Global Power Spectra (GPS) : %s',AnalysesSwitch{1,end}));
fprintf(fid,'\r\n%s',sprintf('2) Compute GPS on scalp areas : %s',AnalysesSwitch{2,end}));
if strcmpi(AnalysesSwitch{1,end},'Yes')
fprintf(fid,'\r\n%s',sprintf('Plotted frequencies : %sHz',num2str(FreqsToPlot)));
fprintf(fid,'\r\n\r\n%s','- Frequency Ranges -');
for k=1:size(FreqData,1)
fprintf(fid,'\r\n%s',sprintf('%d) %s : %sHz',k,FreqData{k,1},FreqData{k,2}));
end
end
if (strcmpi(Steps,'Both') || strcmpi(Steps,'Group analyses')) && ...
strcmpi(MicroStatesSwitch,'Yes')
% STUDY analyses
fprintf(fid,'\r\n\r\n%s\r\n','2) Groups/Conditions levels ------');
fprintf(fid,'\r\n%s\r\n','A) MicroStates analyses ------');
for p=1:length(MSTVersions)
CurrentV = regexp(MSTVersions{p},'\d*','Match');
CurrentStruct = LogMST.(MSTVersions{p});
InternalFields = fieldnames(CurrentStruct);
fprintf(fid,'\r\n%s',sprintf('VERSION %s ------',CurrentV{:}));
for m=1:length(InternalFields)
% Changing data to string
if ~isstring(CurrentStruct.(InternalFields{m}))
TempData = num2str(CurrentStruct.(InternalFields{m}));
else
TempData =CurrentStruct.(InternalFields{m});
end
fprintf(fid,'\r\n%s',sprintf('%d) %s : %s',...
m,InternalFields{m},TempData));
end
end
end
if strcmpi(Steps,'Both') || strcmpi(Steps,'Group analyses') && ...
strcmpi(ICclusteringSwitch,'Yes')
% WRITE ALSO FOR IC CLUSTERING !!
% LogIC
end
end
% FILES PROCESSED ----------
fprintf(fid,'\r\n\r\n%s\r\n','------ PROCESSED FILES SUMMARY ------');
GroupsTemp=fieldnames(Participant_load);
% For each BS levels
% WHAT ABOUT MULTIPLE FACTORS ????
for k=1:numel(GroupsTemp)
SubjTemp = fieldnames(Participant_load.(GroupsTemp{k}));
fprintf(fid,'\r\n\r\n%s',sprintf('- %s -',upper(GroupsTemp{k})));
% For each subjects
for l=1:length(SubjTemp)
% For each processed file
FilesTemp = '';
for t=1:length(Participant_load.(GroupsTemp{k}).(SubjTemp{l}).FileList)
if t~=length(Participant_load.(GroupsTemp{k}).(SubjTemp{l}).FileList)
FilesTemp = strcat(FilesTemp,Participant_load.(GroupsTemp{k}).(SubjTemp{l}).FileList{t},', ');
else
FilesTemp = strcat(FilesTemp,Participant_load.(GroupsTemp{k}).(SubjTemp{l}).FileList{t});
end
end
fprintf(fid,'\r\n%s',sprintf('%s) : %s',upper(SubjTemp{l}),FilesTemp));
end
end
% ERRORS ----------
% IMPLEMENT ERRORS LOG IN SCRIPT! e.g. files that cannot be processed for
% unknown reasons, maybe list potential reasons (e.g. not enough epochs,
% etc)
fprintf(fid,'\r\n\r\n%s\r\n','------ ERRORS ------');
fprintf(fid,'\r\n%s\r\n','------ 1) Preprocessing: ------');
if exist('ErrorArtifacts','var') && ~isempty(ErrorArtifacts)
for k=1:size(ErrorArtifacts,1)
fprintf(fid,'\r\n\r\n%s',sprintf(['File %s could not be processed due to massive artifacts'...
newline 'We recommend that you manually remove the time period containing the artifacts'],...
ErrorArtifacts{k}));
end
else
fprintf(fid,'\r\n No errors reported, congrats!');
end
fprintf(fid,'\r\n%s\r\n','------ 2) Group Analyses: ------');
if exist('ErrorSTUDY','var') && ~isempty(ErrorSTUDY)
fprintf(fid,'\r\n%s',sprintf(['The following files in the STUDY displayed a different' ...
'sampling rate than others, hence we downsampled all of them to %dHz:'],ErrorSTUDY{1}));
for k=2:size(ErrorSTUDY,1)
fprintf(fid,'\r\n\r\n%s',sprintf('%d) %s',k-1,ErrorSTUDY{k}));
end
else
fprintf(fid,'\r\n No errors reported, congrats!');
end
% Output message
fprintf('LOG file exported in %s\n',OutputPath)
% Closing the file
fclose(fid);
end