Skip to content

Commit 2d6244b

Browse files
committed
add marsbar dependency
1 parent 37bf573 commit 2d6244b

File tree

430 files changed

+36494
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

430 files changed

+36494
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
function [D,Ic,changef] = add_contrasts(D, C, varargin)
2+
% method to add contrast definitions to design
3+
%
4+
% The function has many different formats
5+
%
6+
% FORMAT [D Ic changef] = add_contrasts(D, D2 [,Ic])
7+
%
8+
% where D2 is a design. If there is no third argument, all the contrasts in
9+
% D2 will be added. If third argument (Ic) is passed, specifies which
10+
% contrasts in D2 to add. If Ic passed, and empty, or contains the string
11+
% 'ui', contrasts to add are fetched using the GUI
12+
%
13+
% OR
14+
% FORMAT [D Ic changef] = add_contrasts(D, xCon)
15+
%
16+
% where xCon is a structure in SPM contrast format containing contrasts to
17+
% add. If there is no third argument, all the contrasts in xCon will be
18+
% added. If third argument (Ic) is passed, specifies which contrasts in xCon
19+
% to add. If Ic passed, and empty, or contains the string 'ui', contrasts to
20+
% add are fetched using the GUI
21+
%
22+
% OR
23+
% FORMAT [D Ic changef] = add_contrasts(D, stat_struct)
24+
% where stat_struct has fields
25+
% 'names', string, or cell array of strings
26+
% 'types', string ('T' or 'F'), or cell array
27+
% 'set_actions', string ('c', 'X0' or 'iX0') or array
28+
% (see spm_FcUtil)
29+
% (field is optional, defaults to 'c')
30+
% 'values', matrix of values
31+
%
32+
% OR
33+
% FORMAT [D Ic changef] = add_contrasts(D, names, types, values)
34+
% where names, types, values are cell arrays of values, or values
35+
% (defined as above)
36+
%
37+
% OR
38+
% FORMAT [D Ic changef] = add_contrasts(D, names, types, set_actions, values)
39+
% where names, types, set_actions, values are cell arrays of values, or
40+
% values (defined as above)
41+
%
42+
% Returns
43+
% D - possibly modified SPM design
44+
% Ic - indices of specified contrasts as stored in D
45+
% changef - 1 if D has changed during call else 0
46+
%
47+
% Contrast will not be added if it is already present, but the correct
48+
% index will be returned in Ic
49+
%
50+
% $Id$
51+
52+
if nargin < 2
53+
error('Need contrasts to add');
54+
end
55+
56+
% Get parameters from current design
57+
SPM = des_struct(D);
58+
sX = SPM.xX.xKXs;
59+
xCon = SPM.xCon;
60+
v_f = verbose(D);
61+
62+
% process inputs
63+
% The ``C`` variable will hold the xCon structure for the contrasts to add
64+
if isa(C, 'mardo') % design
65+
C = des_struct(C);
66+
end
67+
if isfield(C, 'xCon') % design structure
68+
C = C.xCon;
69+
end
70+
if isfield(C, 'STAT') % xCon structure
71+
% parse Ic input
72+
if nargin > 2 % There is Ic input
73+
Ic_in = varargin{1};
74+
if isempty(Ic_in) | strcmp(Ic_in,'ui')
75+
D2 = set_contrasts(D, C, 0);
76+
Ic_in = ui_get_contrasts(D2,'T&F',Inf,...
77+
'Select contrasts to merge','',1);
78+
end
79+
C = C(Ic_in);
80+
end
81+
else % contrast setting structure or values or cells
82+
if ~isstruct(C) % make stat_struct structure if not already passed
83+
C = sf_cell_to_conset(C, varargin{:});
84+
end
85+
% make xCon structure from stat_struct
86+
C = sf_conset_to_xcon(C, sX);
87+
end
88+
% Initial xCon before adding new contrasts
89+
xc_len = length(xCon);
90+
old_xc_len = xc_len;
91+
% C contains the contrasts to add
92+
for i=1:length(C)
93+
% Update this contrast using our own filtered design
94+
c_i = refresh_con(C(i), sX);
95+
if xc_len == 0 % the xCon to add to is empty
96+
xCon = c_i;
97+
xc_len = 1;
98+
Ic(1) = 1;
99+
continue
100+
end
101+
% Check if we have this contrast already
102+
Ic(i) = spm_FcUtil('In', c_i, sX, xCon);
103+
if Ic(i) == 0
104+
xc_len = xc_len+1;
105+
xCon(xc_len) = c_i;
106+
Ic(i) = xc_len;
107+
elseif v_f
108+
fprintf('\nContrast %s (type %s) already in xCon\n', ...
109+
c_i.name, c_i.STAT);
110+
end
111+
end
112+
changef = xc_len ~= old_xc_len;
113+
if changef
114+
SPM.xCon = xCon;
115+
D = des_struct(D, SPM);
116+
end
117+
return
118+
119+
function xcon_re_entry = refresh_con(xcon_entry, sX)
120+
% Rebuild contrast structure from our own filtered design
121+
if isempty(xcon_entry.c)
122+
error('Empty c matrix for contrast, crashing because confused')
123+
end
124+
xcon_re_entry = spm_FcUtil('Set',...
125+
xcon_entry.name,...
126+
xcon_entry.STAT,...
127+
'c',...
128+
xcon_entry.c,...
129+
sX);
130+
131+
function C = sf_cell_to_conset(names, types, varargin)
132+
% makes contrast setting structure from cell input
133+
if nargin < 3
134+
error('Need at least names, statistic types and values');
135+
end
136+
C = struct(...
137+
'names', names,...
138+
'types', types);
139+
if nargin < 4 % values call
140+
values = varargin{1};
141+
set_actions = 'c';
142+
else % contrast types, values call
143+
set_actions = deal(varargin{1});
144+
values = deal(varargin{2});
145+
end
146+
C = struct(...
147+
'names', names,...
148+
'types', types,...
149+
'set_actions', set_actions,...
150+
'values', values);
151+
return
152+
153+
function C = sf_conset_to_xcon(con_set, sX)
154+
% make xCon structure from cell or struct setting parameters
155+
if ~isfield(con_set, 'set_actions')
156+
[con_set.set_actions] = deal('c');
157+
end
158+
n_e = size(sX.X, 2);
159+
for c = 1:length(con_set)
160+
c1 = con_set(c);
161+
if size(c1.values, 1) ~= n_e & ...
162+
size(c1.values, 2) == n_e
163+
c1.values = c1.values';
164+
end
165+
C(c) = spm_FcUtil('Set',...
166+
c1.names,...
167+
c1.types,...
168+
c1.set_actions,...
169+
c1.values,...
170+
sX);
171+
end
172+
return
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Ya = adjusted_data(D, Ic)
2+
% Return adjusted data for estimated design and contrast no
3+
% FORMAT Ya = adjusted_data(D, Ic)
4+
%
5+
% D - Estimated marsbar design
6+
% Ic - Contrast number to adjust for
7+
%
8+
% Outputs
9+
% Ya - Adjusted data, N by M, where N is number of time points
10+
% and M is number of regions in estimated marsbar design
11+
%
12+
% e.g
13+
% E = estimate(D, Y);
14+
% [E Ic] = add_contrasts(E, 'task', 'T', [1 0 0]);
15+
% Ya = adjusted_data(E, Ic);
16+
%
17+
% Matthew Brett
18+
19+
if nargin < 2
20+
error('Need contrast number');
21+
end
22+
if nargin < 3
23+
r_no = 1;
24+
end
25+
Ya = [];
26+
if ~is_mars_estimated(D)
27+
error('Need a MarsBaR estimated design');
28+
end
29+
30+
SPM = des_struct(D);
31+
B = betas(D);
32+
xCon = get_contrasts(D);
33+
Ya = spm_FcUtil('Yc', xCon(Ic),SPM.xX.xKXs, B);

lib/marsbar-0.44/@mardo/betas.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function r = betas(o)
2+
% method to get estimated betas
3+
%
4+
% $Id$
5+
6+
if ~is_mars_estimated(o)
7+
error('No betas, model not estimated');
8+
end
9+
SPM = des_struct(o);
10+
r = SPM.betas;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function bmrs = block_mean_rows(D)
2+
% method returns rows for means for blocks in design
3+
%
4+
% $Id$
5+
6+
bmrs = [];
7+
SPM = des_struct(D);
8+
bmrs = SPM.xX.iB;
9+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function bms = block_means(D)
2+
% method returns means for blocks in design
3+
%
4+
% Inputs
5+
% D - design object
6+
%
7+
% Outputs
8+
% bms - means over block. Returns B x N matrix
9+
% where B is number of blocks, and N is number
10+
% of ROIs
11+
%
12+
% $Id$
13+
14+
Y = data(D);
15+
if isempty(Y)
16+
error('Design does not yet have data');
17+
end
18+
bms = summary_block_means(data(D));
19+
20+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function D = cd_images(D, newpath, byteswap)
2+
% method for changing path to image files in design
3+
% FORMAT D = cd_images(D, newpath [, byteswap])
4+
%
5+
% Synopsis
6+
% D = cd_images(D); % get new path from GUI
7+
% D = cd_images(D, '/new/root/path');
8+
% D = cd_images(D, '/new/root/path', 1); % force byteswap
9+
% D = cd_images(D, '/new/root/path', 1); % prevent byteswap
10+
%
11+
% D - mardo design
12+
% newpath - path to replace common path of files in analysis [GUI]
13+
% byteswap - whether to indicate byte swapping in vol structs
14+
% [determined from images by default]
15+
%
16+
% $Id$
17+
18+
if nargin < 2
19+
newpath = spm_get(-1, '', 'New directory root for files');
20+
end
21+
if nargin < 3
22+
byteswap=[];
23+
end
24+
25+
% get images
26+
if ~has_images(D)
27+
warning('Design does not contain images');
28+
return
29+
end
30+
VY = get_images(D);
31+
32+
% now change directory
33+
newpath = spm_get('cpath', newpath);
34+
if filesep == '\', other_filesep='/';else other_filesep='\';end
35+
n = length(VY);
36+
strout = strvcat(VY(:).fname);
37+
msk = diff(strout+0)~=0; % common path
38+
d1 = min(find(sum(msk,1)));
39+
d1 = max([find(strout(1,1:d1) == other_filesep | strout(1,1:d1) == filesep) 0]);
40+
ffnames = strout(:,d1+1:end); % common path removed
41+
tmp = ffnames == other_filesep; % filesep exchanged for this platform
42+
ffnames(tmp) = filesep;
43+
nfnames = cellstr(...
44+
strcat(repmat(newpath,n,1),filesep,ffnames));
45+
[VY(:).fname] = deal(nfnames{:});
46+
47+
% do the files exist here then?
48+
if ~exist(nfnames{1}, 'file')
49+
error(['Cannot find first file here: ' nfnames{1}]);
50+
end
51+
if isempty(byteswap)
52+
byteswap = mars_vol_utils('is_swapped_wrong', VY(1));
53+
end
54+
55+
% do byteswap as necessary
56+
if byteswap
57+
VY = mars_vol_utils('byte_swap', VY);
58+
if verbose(D)
59+
disp('Images vols byteswapped');
60+
end
61+
end
62+
63+
D = set_images(D, VY);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function r = contrasts(o, xcon)
2+
% method to get or set contrasts
3+
%
4+
% $Id$
5+
6+
if nargin < 2
7+
r = get_contrasts(o);
8+
else
9+
% Always refreshes the contrasts for safety
10+
r = set_contrasts(o, xcon);
11+
end

lib/marsbar-0.44/@mardo/data.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function r = data(o, Y)
2+
% method to get or set data object
3+
%
4+
% $Id$
5+
6+
if nargin < 2
7+
r = get_data(o);
8+
else
9+
r = set_data(o, Y);
10+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function res = des_struct(obj, Struct)
2+
% get/set method for des_struct field
3+
%
4+
% $Id$
5+
6+
if nargin > 1
7+
obj.des_struct = Struct;
8+
res = obj;
9+
else
10+
res = obj.des_struct;
11+
end

lib/marsbar-0.44/@mardo/descrip.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function strs = descrip(D)
2+
% method gets cell string description of design
3+
%
4+
% $Id$
5+
6+
SPM = des_struct(D);
7+
strs = {'Not specified'};
8+
if ~isfield(SPM, 'xsDes');
9+
return
10+
end
11+
strs = mars_struct('celldisp', SPM.xsDes);

0 commit comments

Comments
 (0)