|
| 1 | +function cpp_bids(varargin) |
| 2 | + % |
| 3 | + % General intro function for CPP BIDS |
| 4 | + % |
| 5 | + % USAGE:: |
| 6 | + % |
| 7 | + % cpp_bids |
| 8 | + % cpp_bids('init') |
| 9 | + % cpp_bids('uninit') |
| 10 | + % cpp_bids('dev') |
| 11 | + % |
| 12 | + % :param action: |
| 13 | + % :type action: string |
| 14 | + % |
| 15 | + % :returns: - :action: (type) (dimension) |
| 16 | + % |
| 17 | + % Example:: |
| 18 | + % |
| 19 | + % (C) Copyright 2022 CPP_BIDS developers |
| 20 | + |
| 21 | + p = inputParser; |
| 22 | + |
| 23 | + defaultAction = 'init'; |
| 24 | + |
| 25 | + addOptional(p, 'action', defaultAction, @ischar); |
| 26 | + addParameter(p, 'verbose', true); |
| 27 | + |
| 28 | + parse(p, varargin{:}); |
| 29 | + |
| 30 | + action = p.Results.action; |
| 31 | + verbose = p.Results.verbose; |
| 32 | + |
| 33 | + switch lower(action) |
| 34 | + |
| 35 | + case 'init' |
| 36 | + |
| 37 | + initCppBids(verbose); |
| 38 | + |
| 39 | + case 'uninit' |
| 40 | + |
| 41 | + uninitCppBids(); |
| 42 | + |
| 43 | + case 'run_tests' |
| 44 | + |
| 45 | + runTests(); |
| 46 | + |
| 47 | + end |
| 48 | + |
| 49 | +end |
| 50 | + |
| 51 | +function initCppBids(verbose) |
| 52 | + % |
| 53 | + % Adds the relevant folders to the path for a given session. |
| 54 | + % Has to be run to be able to use CPP_BIDS. |
| 55 | + % |
| 56 | + % USAGE:: |
| 57 | + % |
| 58 | + % initCppPtb() |
| 59 | + % |
| 60 | + % (C) Copyright 2022 CPP_BIDS developers |
| 61 | + |
| 62 | + thisDirectory = fileparts(mfilename('fullpath')); |
| 63 | + |
| 64 | + global CPP_BIDS_INITIALIZED |
| 65 | + global CPP_BIDS_PATHS |
| 66 | + |
| 67 | + if isempty(CPP_BIDS_INITIALIZED) |
| 68 | + |
| 69 | + pathSep = ':'; |
| 70 | + if ~isunix |
| 71 | + pathSep = ';'; |
| 72 | + end |
| 73 | + |
| 74 | + CPP_BIDS_PATHS = fullfile(thisDirectory); |
| 75 | + CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, ... |
| 76 | + pathSep, ... |
| 77 | + genpath(fullfile(thisDirectory, 'src'))); |
| 78 | + assert(isdir(fullfile(thisDirectory, 'lib', 'bids-matlab', '+bids'))); |
| 79 | + CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, pathSep, ... |
| 80 | + fullfile(thisDirectory, 'lib', 'bids-matlab')); |
| 81 | + assert(isdir(fullfile(thisDirectory, 'lib', 'JSONio'))); |
| 82 | + CPP_BIDS_PATHS = cat(2, CPP_BIDS_PATHS, pathSep, ... |
| 83 | + fullfile(thisDirectory, 'lib', 'JSONio')); |
| 84 | + |
| 85 | + addpath(CPP_BIDS_PATHS, '-begin'); |
| 86 | + |
| 87 | + CPP_BIDS_INITIALIZED = true(); |
| 88 | + |
| 89 | + detectCppBids(); |
| 90 | + |
| 91 | + if verbose |
| 92 | + printCreditsCppBids(); |
| 93 | + end |
| 94 | + |
| 95 | + else |
| 96 | + if verbose |
| 97 | + fprintf('\n\nCPP_BIDS already initialized\n\n'); |
| 98 | + end |
| 99 | + |
| 100 | + end |
| 101 | + |
| 102 | +end |
| 103 | + |
| 104 | +function detectCppBids() |
| 105 | + |
| 106 | + workflowsDir = cellstr(which('saveEventsFile.m', '-ALL')); |
| 107 | + |
| 108 | + if isempty(workflowsDir) |
| 109 | + error('CPP_BIDS is not in your MATLAB / Octave path.\n'); |
| 110 | + |
| 111 | + elseif numel(workflowsDir) > 1 |
| 112 | + fprintf('CPP_BIDS seems to appear in several different folders:\n'); |
| 113 | + for i = 1:numel(workflowsDir) |
| 114 | + fprintf(' * %s\n', fullfile(workflowsDir{i}, '..', '..')); |
| 115 | + end |
| 116 | + error('Remove all but one with ''pathtool'' .\n'); % or ''spm_rmpath |
| 117 | + |
| 118 | + end |
| 119 | +end |
| 120 | + |
| 121 | +function uninitCppBids() |
| 122 | + % |
| 123 | + % Removes the added folders from the path for a given session. |
| 124 | + % |
| 125 | + % USAGE:: |
| 126 | + % |
| 127 | + % uninitCppPtb() |
| 128 | + % |
| 129 | + % (C) Copyright 2021 CPP_BIDS developers |
| 130 | + |
| 131 | + global CPP_BIDS_INITIALIZED |
| 132 | + global CPP_BIDS_PATHS |
| 133 | + |
| 134 | + if isempty(CPP_BIDS_INITIALIZED) || ~CPP_BIDS_INITIALIZED |
| 135 | + fprintf('\n\nCPP_BIDS not initialized\n\n'); |
| 136 | + return |
| 137 | + |
| 138 | + else |
| 139 | + rmpath(CPP_BIDS_PATHS); |
| 140 | + |
| 141 | + if isOctave |
| 142 | + clear -g; |
| 143 | + else |
| 144 | + clearvars -GLOBAL; |
| 145 | + end |
| 146 | + |
| 147 | + end |
| 148 | + |
| 149 | +end |
| 150 | + |
| 151 | +function retval = isOctave() |
| 152 | + % |
| 153 | + % Returns true if the environment is Octave. |
| 154 | + % |
| 155 | + % USAGE:: |
| 156 | + % |
| 157 | + % retval = isOctave() |
| 158 | + % |
| 159 | + % :returns: :retval: (boolean) |
| 160 | + % |
| 161 | + % (C) Copyright 2020 Agah Karakuzu |
| 162 | + % (C) Copyright 2022 CPP_BIDS developers |
| 163 | + |
| 164 | + persistent cacheval % speeds up repeated calls |
| 165 | + |
| 166 | + if isempty (cacheval) |
| 167 | + cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0); |
| 168 | + end |
| 169 | + |
| 170 | + retval = cacheval; |
| 171 | +end |
0 commit comments