Skip to content

Commit 2d7feae

Browse files
committed
make initCppSpm load octave packages
1 parent e976438 commit 2d7feae

File tree

1 file changed

+117
-42
lines changed

1 file changed

+117
-42
lines changed

initCppSpm.m

Lines changed: 117 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,126 @@
11
function initCppSpm()
2-
%
3-
% Adds the relevant folders to the path for a given session.
4-
% Has to be run to be able to use CPP_SPM.
5-
%
6-
% USAGE::
7-
%
8-
% initCppSpm()
9-
%
10-
% (C) Copyright 2021 CPP_SPM developers
11-
12-
opt.verbosity = 1;
13-
14-
global CPP_SPM_INITIALIZED
15-
16-
if isempty(CPP_SPM_INITIALIZED)
17-
18-
thisDirectory = fileparts(mfilename('fullpath'));
19-
20-
addpath(genpath(fullfile(thisDirectory, 'src')));
21-
addpath(genpath(fullfile(thisDirectory, 'lib', 'spmup')));
22-
addpath(genpath(fullfile(thisDirectory, 'lib', 'spm_2_bids')));
23-
24-
libList = { ...
25-
'mancoreg', ...
26-
'NiftiTools', ...
27-
'bids-matlab', ...
28-
'slice_display', ...
29-
'panel-2.14', ...
30-
'utils'};
31-
32-
for i = 1:numel(libList)
33-
addpath(fullfile(thisDirectory, 'lib', libList{i}));
34-
end
2+
%
3+
% Adds the relevant folders to the path for a given session.
4+
% Has to be run to be able to use CPP_SPM.
5+
%
6+
% USAGE::
7+
%
8+
% initCppSpm()
9+
%
10+
% (C) Copyright 2021 CPP_SPM developers
11+
12+
opt.verbosity = 1;
13+
14+
octaveVersion = '4.0.3';
15+
matlabVersion = '8.6.0';
16+
17+
installlist = {'io', 'statistics', 'image'};
18+
19+
global CPP_SPM_INITIALIZED
20+
21+
if isempty(CPP_SPM_INITIALIZED)
22+
23+
thisDirectory = fileparts(mfilename('fullpath'));
24+
25+
addpath(genpath(fullfile(thisDirectory, 'src')));
26+
addpath(genpath(fullfile(thisDirectory, 'lib', 'spmup')));
27+
addpath(genpath(fullfile(thisDirectory, 'lib', 'spm_2_bids')));
28+
29+
libList = { ...
30+
'mancoreg', ...
31+
'NiftiTools', ...
32+
'bids-matlab', ...
33+
'slice_display', ...
34+
'panel-2.14', ...
35+
'utils'};
36+
37+
for i = 1:numel(libList)
38+
addpath(fullfile(thisDirectory, 'lib', libList{i}));
39+
end
40+
41+
addpath(fullfile(thisDirectory, 'lib', 'brain_colours', 'code'));
42+
addpath(fullfile(thisDirectory, 'lib', 'riksneurotools', 'GLM'));
43+
44+
checkDependencies(opt);
45+
printCredits(opt);
46+
47+
run(fullfile(thisDirectory, 'lib', 'CPP_ROI', 'initCppRoi'));
48+
49+
%%
50+
if isOctave
51+
52+
% Exit if min version is not satisfied
53+
if ~compare_versions(OCTAVE_VERSION, octaveVersion, '>=')
54+
error('Minimum required Octave version: %s', octaveVersion);
55+
end
3556

36-
addpath(fullfile(thisDirectory, 'lib', 'brain_colours', 'code'));
37-
addpath(fullfile(thisDirectory, 'lib', 'riksneurotools', 'GLM'));
57+
for ii = 1:length(installlist)
3858

39-
checkDependencies(opt);
40-
printCredits(opt);
59+
packageName = installlist{ii};
4160

42-
run(fullfile(thisDirectory, 'lib', 'CPP_ROI', 'initCppRoi'));
61+
try
62+
% Try loading Octave packages
63+
disp(['loading ' packageName]);
64+
pkg('load', packageName);
4365

44-
CPP_SPM_INITIALIZED = true();
66+
catch
4567

46-
else
47-
printToScreen('\n\nCPP_SPM already initialized\n\n', opt);
68+
tryInstallFromForge(packageName);
4869

49-
end
70+
end
71+
end
72+
73+
else % MATLAB ----------------------------
74+
75+
if verLessThan('matlab', matlabVersion)
76+
error('Sorry, minimum required MATLAB version is R2015b. :(');
77+
end
78+
79+
80+
end
81+
82+
CPP_SPM_INITIALIZED = true();
83+
84+
else
85+
printToScreen('\n\nCPP_SPM already initialized\n\n', opt);
86+
87+
end
88+
89+
% %-Detect SPM directory
90+
% %--------------------------------------------------------------------------
91+
% SPMdir = cellstr(which('spm.m','-ALL'));
92+
% if isempty(SPMdir)
93+
% fprintf('SPM is not in your %s path.\n',software);
94+
% return;
95+
% elseif numel(SPMdir) > 1
96+
% fprintf('SPM seems to appear in several different folders:\n');
97+
% for i=1:numel(SPMdir)
98+
% fprintf(' * %s\n',SPMdir{i});
99+
% end
100+
% fprintf('Remove all but one with ''pathtool'' or ''spm_rmpath''.\n');
101+
% return;
102+
% else
103+
% fprintf('SPM is installed in: %s\n',fileparts(SPMdir{1}));
104+
% end
105+
% SPMdir = fileparts(SPMdir{1});
106+
107+
end
108+
109+
110+
function tryInstallFromForge(packageName)
111+
112+
errorcount = 1;
113+
while errorcount % Attempt twice in case installation fails
114+
try
115+
pkg('install', '-forge', packageName);
116+
pkg('load', packageName);
117+
errorcount = 0;
118+
catch err
119+
errorcount = errorcount + 1;
120+
if errorcount > 2
121+
error(err.message);
122+
end
123+
end
124+
end
50125

51126
end

0 commit comments

Comments
 (0)