-
Notifications
You must be signed in to change notification settings - Fork 14
/
bidspm.m
474 lines (355 loc) · 10.5 KB
/
bidspm.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
function returnCode = bidspm(varargin)
%
% Type bidspm('action', 'help') for more information.
%
% (C) Copyright 2022 bidspm developers
args = defaultInputParser();
try
parse(args, varargin{:});
catch ME
displayArguments(varargin{:});
rethrow(ME);
end
action = args.Results.action;
% to simplify the API, user can call things like ``bidspm help``
% or ``bidspm init`` but then this needs to override the action value.
bidsDir = args.Results.bids_dir;
if ismember(bidsDir, lowLevelActions())
action = bidsDir;
end
try
returnCode = executeAction(action, varargin{:});
catch ME
opt.dir.output = args.Results.output_dir;
opt.dryRun = false;
opt.verbosity = 3;
if isempty(which('bugReport'))
rethrow(ME);
else
bugReport(opt, ME);
end
rethrow(ME);
end
end
function returnCode = executeAction(varargin)
action = varargin{1};
returnCode = 0;
switch lower(action)
case 'init'
initBidspm();
case 'help'
system('bidspm --help');
help(fullfile(rootDir(), 'src', 'messages', 'bidspmHelp.m'));
case 'version'
versionBidspm();
case 'dev'
initBidspm(true);
case 'uninit'
uninitBidspm();
case 'update'
update();
case 'run_tests'
returnCode = run_tests();
case 'copy'
cliCopy(varargin{2:end});
case 'create_roi'
cliCreateRoi(varargin{2:end});
case 'smooth'
cliSmooth(varargin{2:end});
case 'preprocess'
cliPreprocess(varargin{2:end});
case 'default_model'
cliDefaultModel(varargin{2:end});
case {'stats', 'contrasts', 'results', 'specify_only'}
cliStats(varargin{2:end});
case {'bms', 'bms-posterior', 'bms-bms'}
cliBayesModel(varargin{2:end});
case 'meaning_of_life'
fprintf('\n42\n\n');
otherwise
errorStruct.identifier = 'bidspm:unknownAction';
errorStruct.message = sprintf('action %s is not among the known actions:\n\t- %s', ...
action, ...
strjoin(allowedActions(), '\n\t- '));
error(errorStruct);
end
end
function displayArguments(varargin)
disp('arguments passed were :');
for i = 1:numel(varargin)
fprintf('- ');
disp(varargin{i});
end
fprintf(1, '\n');
end
function value = rootDir()
value = fileparts(mfilename('fullpath'));
end
%% low level actions
function versionBidspm()
try
versionNumber = getVersion();
catch
versionNumber = fileread(rootDir(), 'version.txt');
versionNumber = versionNumber(1:end - 1);
end
fprintf(1, '%s\n', versionNumber);
end
function initBidspm(dev)
%
% Adds the relevant folders to the path for a given session.
% Has to be run to be able to use bidspm.
%
% USAGE::
%
% initBidspm()
%
% (C) Copyright 2021 bidspm developers
if nargin < 1
dev = false;
end
opt.verbosity = 2;
opt.msg.color = '';
octaveVersion = '6.4.0';
matlabVersion = '8.6.0';
% octave packages
installlist = {'io', 'statistics', 'image'};
global BIDSPM_INITIALIZED
global BIDSPM_PATHS
if isempty(BIDSPM_INITIALIZED)
pathSep = ':';
if ~isunix
pathSep = ';';
end
% add bidspm source code
BIDSPM_PATHS = fullfile(rootDir());
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, ...
pathSep, ...
genpath(fullfile(rootDir(), 'src')));
if dev
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, pathSep, ...
fullfile(rootDir(), 'tests', 'utils'));
end
% Make sure MACS toolbox used by SPM is the one from bidspm
updateMacstoolbox();
% for some reasons this folder was otherwise not added to the path in Octave
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, ...
pathSep, ...
genpath(fullfile(rootDir(), 'src', 'workflows', 'stats')));
libList = {'mancoreg', ...
'bids-matlab', ...
'slice_display', ...
'panel-2.14', ...
'utils'};
for i = 1:numel(libList)
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, pathSep, ...
fullfile(rootDir(), 'lib', libList{i}));
end
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, pathSep, ...
fullfile(rootDir(), 'lib', 'brain_colours', 'code'));
BIDSPM_PATHS = cat(2, BIDSPM_PATHS, pathSep, ...
fullfile(rootDir(), 'lib', 'riksneurotools', 'GLM'));
addpath(BIDSPM_PATHS, '-begin');
silenceOctaveWarning();
% add library that have a set up script
run(fullfile(rootDir(), 'lib', 'CPP_ROI', 'initCppRoi'));
run(fullfile(rootDir(), 'lib', 'spm_2_bids', 'init_spm_2_bids'));
run(fullfile(rootDir(), 'lib', 'octache', 'setup'));
checkDependencies(opt);
printCredits(opt);
%%
if bids.internal.is_octave()
% Exit if min version is not satisfied
if ~compare_versions(OCTAVE_VERSION, octaveVersion, '>=')
error('Minimum required Octave version: %s', octaveVersion);
end
for ii = 1:length(installlist)
packageName = installlist{ii};
try
% Try loading Octave packages
printToScreen(['loading ' packageName '\n']);
pkg('load', packageName);
catch
tryInstallFromForge(packageName);
end
end
else % MATLAB ----------------------------
if verLessThan('matlab', matlabVersion)
error('Sorry, minimum required MATLAB version is R2015b. :(');
end
end
BIDSPM_INITIALIZED = true();
detectBidspm();
else
logger('INFO', 'bidspm already initialized');
end
end
function updateMacstoolbox()
SPM_DIR = spm('dir');
target_dir = fullfile(SPM_DIR, 'toolbox', 'MACS');
MACS_TOOLBOX_DIR = fullfile(rootDir(), 'lib', 'MACS');
if exist(target_dir, 'dir') == 7 && exist(fullfile(target_dir, '.git'), 'dir') == 0
rmdir(target_dir, 's');
end
if exist(target_dir, 'dir') == 7
msg = sprintf('updating MACS toolbox\n');
fprintf(1, msg);
[status, cmdout] = system(sprintf('git -C %s pull', target_dir));
if status ~= 0
fprintf(1, 'Failed to update MACS toolbox: %s\n', cmdout);
end
else
msg = sprintf('installing MACS toolbox in:\n%s.\n', target_dir);
fprintf(1, msg);
system(sprintf('git clone %s %s', ...
MACS_TOOLBOX_DIR, ...
target_dir));
end
end
function uninitBidspm()
%
% Removes the added folders from the path for a given session.
%
% USAGE::
%
% uninitBidspm()
%
% (C) Copyright 2021 bidspm developers
global BIDSPM_INITIALIZED
global BIDSPM_PATHS
if isempty(BIDSPM_INITIALIZED) || ~BIDSPM_INITIALIZED
fprintf('\n\nbidspm not initialized\n\n');
return
else
run(fullfile(rootDir(), 'lib', 'CPP_ROI', 'uninitCppRoi'));
rmpath(BIDSPM_PATHS);
spm('Clean');
spm('Quit');
if isOctave()
clear -g;
else
clearvars -GLOBAL;
end
end
end
function returnCode = run_tests()
%
% (C) Copyright 2019 bidspm developers
tic;
bidspm('action', 'dev');
% to reduce noise in the output
silenceOctaveWarning();
cd(fileparts(mfilename('fullpath')));
if bids.internal.is_github_ci()
logger('INFO', 'This is github CI');
else
logger('INFO', 'This is not github CI');
end
logger('INFO', sprintf('Home is "%s"\n', getenv('HOME')));
warning('OFF');
spm('defaults', 'fMRI');
folderToCover = fullfile(pwd, 'src');
subfolder = '';
if usingSlowTestMode()
fprintf(1, 'Running in slow tests only.\n');
subfolder = 'tests_slow';
end
testFolder = fullfile(pwd, 'tests', subfolder);
generateLayoutMat();
returnCode = moxunit_runtests(testFolder, ...
'-verbose', '-recursive', '-randomize_order', ...
'-with_coverage', ...
'-cover', folderToCover, ...
'-cover_xml_file', 'coverage.xml', ...
'-cover_html_dir', fullfile(pwd, 'coverage_html'));
toc;
end
function update()
try
initBidspm();
cwd = pwd();
cd(returnRootDir());
system('make update');
cd(cwd);
catch ME
warning('Could not run the update.');
rethrow(ME);
end
end
%% constants
function value = lowLevelActions()
value = {'init'; ...
'uninit'; ...
'dev'; ...
'version'; ...
'run_tests'; ...
'update'; ...
'help'; ...
'meaning_of_life'};
end
function value = bidsAppsActions()
value = {'copy'; ...
'create_roi'; ...
'preprocess'; ...
'smooth'; ...
'default_model'; ...
'stats'; ...
'contrasts'; ...
'results'; ...
'specify_only'; ...
'bms'; ...
'bms-posterior'; ...
'bms-bms'};
end
function value = allowedActions()
value = cat(1, bidsAppsActions(), lowLevelActions());
end
%% input parsers
function args = defaultInputParser()
args = inputParser;
args.CaseSensitive = false;
args.KeepUnmatched = true;
args.FunctionName = 'bidspm';
defaultAction = 'init';
isLowLevelActionOrDir = @(x) (ismember(x, lowLevelActions()) || isdir(x));
isChar = @(x) ischar(x);
addOptional(args, 'bids_dir', pwd, isLowLevelActionOrDir);
addOptional(args, 'output_dir', '', isChar);
addOptional(args, 'analysis_level', 'subject', @(x) ismember(x, {'subject', 'dataset'}));
addParameter(args, 'action', defaultAction, isChar);
end
%% helpers functions
function detectBidspm()
workflowsDir = cellstr(which('bidsSpatialPrepro.m', '-ALL'));
if isempty(workflowsDir)
error('bidspm is not in your MATLAB / Octave path.\n');
elseif numel(workflowsDir) > 1
printToScreen('bidspm seems to appear in several different folders:');
for i = 1:numel(workflowsDir)
fprintf(' * %s\n', fullfile(workflowsDir{i}, '..', '..'));
end
error('Remove all but one with ''pathtool''.\n'); % or ''spm_rmpath
end
end
function tryInstallFromForge(packageName)
errorcount = 1;
while errorcount % Attempt twice in case installation fails
try
pkg('install', '-forge', packageName);
pkg('load', packageName);
errorcount = 0;
catch err
errorcount = errorcount + 1;
if errorcount > 2
error(err.message);
end
end
end
end
function retval = isOctave()
persistent cacheval % speeds up repeated calls
if isempty (cacheval)
cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
end
retval = cacheval;
end