-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeg_nemar_plugin.m
90 lines (84 loc) · 3.06 KB
/
eeg_nemar_plugin.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
% NEMAR plugin pipeline
% Required input:
% EEG - [struct] input dataset
% Optional inputs:
% specific - [cell] plugins to specifically run
% logdir - [string] directory to log execution output
% Output:
% EEG - [struct] plotted dataset
function EEG = eeg_nemar_plugin(EEG, varargin)
opt = finputcheck(varargin, { ...
'specific' 'cell' {} {}; ... % plugins to specifically run
'modeval' 'string' {'new', 'resume', 'rerun'} 'resume'; ... % if import mode, pipeline will overwrite existing outputdir. rerun won't
'logdir' 'string' {} './eeg_nemar_logs'; ...
}, 'eeg_nemar_plugin');
if isstr(opt), error(opt); end
if ~exist(opt.logdir, 'dir')
logdirstatus = mkdir(opt.logdir);
end
try
which eeglab;
catch
warning('EEGLAB not found in path. Trying to add it from expanse...')
addpath('/expanse/projects/nemar/eeglab');
eeglab nogui;
end
[filepath, filename, ext] = fileparts(EEG.filename);
log_file = fullfile(opt.logdir, filename);
diary(log_file);
fprintf('Running NEMAR plugins on %s\n', fullfile(EEG.filepath, EEG.filename));
opt
% retrieve plugin from nemar_plugins folder
path = fileparts(which('pop_run_pipeline'));
files = dir(fullfile(path, 'nemar_plugins'));
addpath(fullfile(path, 'nemar_plugins'));
plugins = {};
for i=1:numel(files)
if startsWith(files(i).name, 'nemar_plugin') && endsWith(files(i).name, '.m')
fcn = files(i).name(1:end-2);
plugins = [plugins fcn(numel('nemar_plugin_')+1:end)];
end
end
% run plugins on EEG
splitted = split(EEG.filename(1:end-4), '_');
modality = splitted{end};
status = zeros(1,numel(plugins));
for i=1:numel(plugins)
fcn = ['nemar_plugin_' plugins{i}];
if ~isempty(opt.specific) && ~any(strcmp(opt.specific, plugins{i}))
continue
end
try
[finished, templateFields] = feval(fcn, EEG, modality);
status(i) = finished;
catch ME
fprintf('Error running plugin %s\n', plugins{i});
status(i) = 0;
ME
end
end
% log results
status_file = fullfile(opt.logdir, [EEG.filename(1:end-4) '_plugins.csv']);
if exist(status_file, 'file') && ~isempty(opt.specific)
disp('status exist')
status
status_tbl = readtable(status_file);
for p=1:numel(plugins)
plugin = plugins{p}
if any(strcmp(opt.specific, plugin))
plugin
status(p)
status_tbl.(plugin) = status(p);
end
end
else
status_tbl = array2table(zeros(1, numel(plugins)));
status_tbl.Properties.VariableNames = plugins;
for p=1:numel(plugins)
plugin = plugins{p};
status_tbl.(plugin) = status(p);
end
end
disp(status_tbl)
writetable(status_tbl, status_file);
end