-
Notifications
You must be signed in to change notification settings - Fork 9
/
cpp_bids.m
184 lines (135 loc) · 4.03 KB
/
cpp_bids.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
function cpp_bids(varargin)
%
% General intro function for CPP BIDS
%
% USAGE::
%
% cpp_bids
% cpp_bids('init')
% cpp_bids('uninit')
% cpp_bids('dev')
%
% :param action:
% :type action: string
%
% :returns: - :action: (type) (dimension)
%
% Example::
%
% (C) Copyright 2022 CPP_BIDS developers
p = inputParser;
defaultAction = 'init';
addOptional(p, 'action', defaultAction, @ischar);
addParameter(p, 'verbose', true);
parse(p, varargin{:});
action = p.Results.action;
verbose = p.Results.verbose;
switch lower(action)
case 'init'
initCppBids(verbose);
case 'dev'
initCppBids(verbose);
thisDirectory = fileparts(mfilename('fullpath'));
testFolder = fullfile(thisDirectory, 'tests');
addpath(testFolder, '-begin');
utilFolder = fullfile(thisDirectory, 'tests', 'utils');
addpath(utilFolder, '-begin');
case 'uninit'
uninitCppBids();
case 'run_tests'
runTests();
end
end
function initCppBids(verbose)
%
% Adds the relevant folders to the path for a given session.
% Has to be run to be able to use CPP_BIDS.
%
% USAGE::
%
% initCppPtb()
%
% (C) Copyright 2022 CPP_BIDS developers
thisDirectory = fileparts(mfilename('fullpath'));
global CPP_BIDS_INITIALIZED
global CPP_BIDS_PATHS
if isempty(CPP_BIDS_INITIALIZED)
pathSep = ':';
if ~isunix
pathSep = ';';
end
CPP_BIDS_PATHS = fullfile(thisDirectory);
CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, ...
pathSep, ...
genpath(fullfile(thisDirectory, 'src')));
assert(isdir(fullfile(thisDirectory, 'lib', 'bids-matlab', '+bids')));
CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, pathSep, ...
fullfile(thisDirectory, 'lib', 'bids-matlab'));
assert(isdir(fullfile(thisDirectory, 'lib', 'JSONio')));
CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, pathSep, ...
fullfile(thisDirectory, 'lib', 'JSONio'));
addpath(CPP_BIDS_PATHS, '-begin');
CPP_BIDS_INITIALIZED = true();
detectCppBids();
if verbose
printCreditsCppBids();
end
else
if verbose
fprintf('\n\nCPP_BIDS already initialized\n\n');
end
end
end
function detectCppBids()
workflowsDir = cellstr(which('saveEventsFile.m', '-ALL'));
if isempty(workflowsDir)
error('CPP_BIDS is not in your MATLAB / Octave path.\n');
elseif numel(workflowsDir) > 1
fprintf('CPP_BIDS seems to appear in several different folders:\n');
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 uninitCppBids()
%
% Removes the added folders from the path for a given session.
%
% USAGE::
%
% uninitCppPtb()
%
% (C) Copyright 2021 CPP_BIDS developers
global CPP_BIDS_INITIALIZED
global CPP_BIDS_PATHS
if isempty(CPP_BIDS_INITIALIZED) || ~CPP_BIDS_INITIALIZED
fprintf('\n\nCPP_BIDS not initialized\n\n');
return
else
rmpath(CPP_BIDS_PATHS);
if isOctave
clear -g CPP_BIDS_INITIALIZED CPP_BIDS_PATHS;
else
clearvars -GLOBAL CPP_BIDS_INITIALIZED CPP_BIDS_PATHS;
end
end
end
function retval = isOctave()
%
% Returns true if the environment is Octave.
%
% USAGE::
%
% retval = isOctave()
%
% :returns: :retval: (boolean)
%
% (C) Copyright 2020 Agah Karakuzu
% (C) Copyright 2022 CPP_BIDS developers
persistent cacheval % speeds up repeated calls
if isempty (cacheval)
cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
end
retval = cacheval;
end