0001 function [phiwD, connos, changef] = write_contrasts(phiwD, connos, flags)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 def_flags = struct(...
0031 'no_new', 0,...
0032 't_only', 0,...
0033 'f_only', 0,...
0034 'single', 0,...
0035 'con_only', 0);
0036
0037 if ~is_phiw_estimated(phiwD), error('Need phiwave estimated design'); end
0038 changef = 0;
0039 if nargin < 2
0040 connos = [];
0041 end
0042 if nargin < 3
0043 fnames = '';
0044 end
0045 if nargin < 4
0046 flags = [];
0047 end
0048 rmsi = [];
0049
0050
0051 flags = mars_struct('ffillsplit', def_flags, flags);
0052 wOK = flags.no_new;
0053 if flags.t_only
0054 statstr = 'T';
0055 elseif flags.f_only
0056 statstr = 'F';
0057 else
0058 statstr = 'T|F';
0059 end
0060 if flags.single
0061 ncons = 1;
0062 else
0063 ncons = Inf;
0064 end
0065
0066
0067 bs30 = repmat(sprintf('\b'),1,30);
0068
0069
0070 Vbeta = get_vol_field(phiwD, 'Vbeta');
0071 VResMS = get_vol_field(phiwD, 'VResMS');
0072 VY = get_images(phiwD);
0073 xM = masking_struct(phiwD);
0074 erdf = error_df(phiwD);
0075 M = VY(1).mat;
0076 DIM = VY(1).dim(1:3);
0077 xX = design_structure(phiwD);
0078 Swd = swd(phiwD);
0079
0080
0081
0082 wOK = swd_writable(phiwD);
0083 if ~wOK
0084 str = {'Can''t write to the results directory:',...
0085 [' ',Swd],...
0086 ' ','-> results restricted to contrasts already computed'};
0087 spm('alert!',str,mfilename,1);
0088 end
0089
0090
0091
0092 nVar = size(VY,2);
0093 if nVar > 1
0094 connos == 1;
0095 elseif isempty(connos)
0096 [connos, phiwD, changef] = ui_get_contrasts(phiwD, statstr, ncons,...
0097 ' Select contrasts...','to compute', wOK);
0098 end
0099
0100 if isempty(connos)
0101 return
0102 end
0103 xCon = get_contrasts(phiwD);
0104
0105
0106 wave = get_wave(phiwD);
0107 if isempty(wave)
0108 error(['No wave info for first image - unlikely to be wavelet' ...
0109 ' analysis'])
0110 end
0111 wvp = getfield(wtinfo(wave), 'wtprefix');
0112
0113
0114
0115 spm('Pointer','Watch')
0116 spm_progress_bar('Init',100,'computing...')
0117 nPar = size(xX.X,2);
0118 I = unique(connos);
0119 for ii = 1:length(I)
0120
0121 i = I(ii);
0122
0123
0124 if i > length(xCon)
0125 warning(['Contrast ' num2str(i) ' does not exist']);
0126 break;
0127 end
0128
0129
0130
0131 eidf = mars_struct('getifthere', xCon(i), 'eidf');
0132 if isempty(eidf)
0133 X1o = spm_FcUtil('X1o',xCon(i),xX.xKXs);
0134 [trMV,trMVMV] = spm_SpUtil('trMV',X1o,xX.V);
0135 xCon(i).eidf = trMV^2/trMVMV;
0136 else
0137 trMV = []; trMVMV = [];
0138 end
0139
0140
0141 fsuff = mars_utils('str2fname', xCon(i).name);
0142
0143
0144
0145 Vcon = full_vol(phiwD, ...
0146 mars_struct('getifthere', xCon(i), 'Vcon'));
0147 if isempty(Vcon.dim)
0148
0149
0150 changef = 1;
0151
0152
0153
0154 if ~wOK, spm('alert*',{ 'Can''t write to the results directory:',...
0155 [' ',Swd],' ','=> can''t compute new contrasts'},...
0156 mfilename,sqrt(-1));
0157 spm('Pointer','Arrow')
0158 error('can''t write contrast image')
0159 end
0160
0161 switch(xCon(i).STAT)
0162
0163 case 'T'
0164
0165 fprintf('\t%-32s: %-10s%20s',sprintf('contrast image %2d',i),...
0166 '(spm_add)','...initialising')
0167
0168 Q = find(abs(xCon(i).c) > 0);
0169 V = Vbeta(Q);
0170 for j = 1:length(Q)
0171 V(j).pinfo(1,:) = V(j).pinfo(1,:)*xCon(i).c(Q(j));
0172 end
0173
0174
0175
0176 Vcon = struct(...
0177 'fname', fullfile(Swd,sprintf('%scon_%04d_%s.img',wvp,i,fsuff)),...
0178 'dim', [DIM,16],...
0179 'mat', M,...
0180 'pinfo', [1,0,0]',...
0181 'descrip',sprintf('Phiwave contrast - %d: %s',i,xCon(i).name));
0182
0183
0184
0185 fprintf('%s%30s',bs30,'...computing')
0186 Vcon = spm_create_image(Vcon);
0187 Vcon.pinfo(1,1) = spm_add(V, Vcon);
0188 Vcon = spm_close_vol(Vcon);
0189
0190 fprintf('%s%30s\n',bs30,sprintf(...
0191 '...written %s',spm_str_manip(Vcon.fname,'t')))
0192 case 'F'
0193 fprintf('\t%-32s: %30s',sprintf('ESS image %2d',i),...
0194 '...computing')
0195
0196
0197
0198 h = spm_FcUtil('Hsqr',xCon(i),xX.xKXs);
0199
0200
0201
0202 Vcon = struct(...
0203 'fname', fullfile(Swd,sprintf('%sess_%04d_%s.img',wvp,i,fsuff)),...
0204 'dim', [DIM 16],...
0205 'mat', M,...
0206 'pinfo', [1,0,0]',...
0207 'descrip',sprintf('Phiwave ESS - contrast %d: %s',i,xCon(i).name));
0208
0209
0210
0211 fprintf('%s',bs30)
0212 Vcon = spm_create_image(Vcon);
0213 Vcon = spm_resss(Vbeta, Vcon, h);
0214 Vcon = spm_close_vol(Vcon);
0215
0216 otherwise
0217
0218 error(['unknown STAT "',xCon(i).STAT,'"'])
0219
0220 end
0221
0222
0223 xCon(i).Vcon = design_vol(phiwD, Vcon);
0224
0225
0226 putwave(Vcon.fname,wave);
0227
0228 end
0229
0230 spm_progress_bar('Set',100*(2*ii-1)/(2*length(I)+2))
0231
0232
0233
0234 Vspm = full_vol(phiwD, ...
0235 mars_struct('getifthere', xCon(i), 'Vspm'));
0236 if ~flags.con_only & isempty(Vspm.dim)
0237
0238
0239 changef = 1;
0240
0241
0242 if isempty(rmsi)
0243 fprintf('\t%-32s: %30s','ResMS file...','...done');
0244 rmsi = spm_read_vols(VResMS);
0245 fprintf('%s%30s\n',bs30,'...done')
0246 rmsi(abs(rmsi)<eps) = NaN;
0247 end
0248
0249
0250
0251 if ~wOK, spm('alert*',{ 'Can''t write to the results directory:',...
0252 [' ',Swd],' ','=> can''t compute new contrasts'},...
0253 mfilename,sqrt(-1));
0254 spm('Pointer','Arrow')
0255 error('can''t write Phiwave image')
0256 end
0257
0258 fprintf('\t%-32s: %30s',sprintf('phiw{%c} image %2d',xCon(i).STAT,i),...
0259 '...computing')
0260
0261 switch(xCon(i).STAT)
0262 case 'T'
0263
0264 Z = spm_read_vols(Vcon)./...
0265 (sqrt(rmsi * (xCon(i).c'*xX.Bcov*xCon(i).c) ));
0266
0267 str = sprintf('[%.2g]',erdf);
0268
0269 case 'F'
0270
0271 if isempty(trMV)
0272 trMV = spm_SpUtil('trMV',spm_FcUtil('X1o',xCon(i),xX.xKXs),xX.V);
0273 end
0274 Z =(spm_read_vols(Vcon)/trMV)./rmsi;
0275
0276 str = sprintf('[%.2g,%.2g]',xCon(i).eidf,erdf);
0277
0278 otherwise
0279
0280 error(['unknown STAT "',xCon(i).STAT,'"'])
0281 end
0282
0283
0284
0285 fprintf('%s%30s',bs30,'...writing')
0286 Vspm = struct(...
0287 'fname', fullfile(Swd,sprintf('%sphiw%c_%04d_%s.img',wvp,xCon(i).STAT,i,fsuff)),...
0288 'dim', [DIM 16],...
0289 'mat', M,...
0290 'pinfo', [1,0,0]',...
0291 'descrip',sprintf('Phiwave{%c_%s} - contrast %d: %s',...
0292 xCon(i).STAT,str,i,xCon(i).name));
0293
0294 Vspm = spm_write_vol(Vspm,Z);
0295
0296
0297 putwave(Vspm.fname,wave);
0298
0299 fprintf('%s%30s\n',bs30,sprintf(...
0300 '...written %s',spm_str_manip(Vspm.fname,'t')))
0301
0302 end
0303
0304
0305 xCon(i).Vspm = design_vol(phiwD, Vspm);
0306
0307
0308 putwave(Vspm.fname,wave);
0309
0310 spm_progress_bar('Set',100*(2*ii-0)/(2*length(I)+2))
0311
0312 end
0313
0314 spm_progress_bar('Set',100)
0315
0316
0317
0318 phiwD = set_contrasts(phiwD, xCon);
0319
0320 spm_progress_bar('Clear')
0321
0322 return
0323
0324