0001 function [phiwD] = vox2wt_ana(phiwD, params)
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 defparams = struct('wavelet', phiw_lemarie(2), ...
0029 'scales', 4, ...
0030 'wtprefix', 'wv_', ...
0031 'maskthresh', 0.05);
0032
0033 if nargin < 2
0034 params = [];
0035 end
0036 params = mars_struct('ffillsplit', defparams, params);
0037
0038
0039 if ~has_images(phiwD), error('Need images in design'); end
0040 VY = get_images(phiwD);
0041
0042
0043 g_wt_opts = struct('reproc', 1, ...
0044 'verbose', verbose(phiwD), ...
0045 'find_similar', 1);
0046 for i = 1:prod(size(VY))
0047 [v1 v2] = sf_get_wted(VY(i), params, g_wt_opts);
0048 [VY_wt(i) VY_o(i)] = deal(v1, v2);
0049 end
0050 VY_wt = reshape(VY_wt, size(VY));
0051 VY_o = reshape(VY_o, size(VY));
0052
0053
0054 xM = masking_struct(phiwD);
0055 if isempty(xM), error('Need masking structure'), end
0056
0057
0058
0059 if isfield(xM, 'wave')
0060 redo_mask = ~same_wtinfo(xM.wave, params)
0061 else
0062 redo_mask = 1;
0063 end
0064 if redo_mask
0065 xM = sf_make_mask(xM, VY_o, params);
0066 end
0067
0068
0069 phiwD = set_images(phiwD, VY_wt);
0070 phiwD = masking_struct(phiwD, xM);
0071
0072 return
0073
0074
0075
0076
0077 function [Vw, Vo] = sf_get_wted(V, params, options)
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 f_s = mars_struct('getifthere', options, 'find_similar');
0098 wv_opts = struct('noproc', 1, 'wtprefix', params.wtprefix, ...
0099 'find_similar', f_s);
0100
0101
0102 reproc_f = mars_struct('isthere', options, 'reproc');
0103
0104 if phiw_wvimg('is_wted', V)
0105
0106
0107 Vo = phiw_wvimg('orig_vol', V);
0108 wvobj = phiw_wvimg(V, wv_opts);
0109 if same_wtinfo(wvobj, params)
0110 Vw = V;
0111 return
0112 end
0113
0114
0115 if ~reproc_f
0116 error(sprintf('%s is already WTed with different parameters', ...
0117 V.fname))
0118 end
0119 options.find_similar = 0;
0120 [Vw, Vo] = sf_get_wted(Vo, params, options);
0121 return
0122
0123 end
0124
0125
0126
0127 wvobj = phiw_wvimg(V, wv_opts, params.wavelet, params.scales);
0128 if ~is_wted(wvobj)
0129 wvobj = write_wtimg(wvobj);
0130 end
0131 Vw = wv_vol(wvobj);
0132 Vo = V;
0133
0134 return
0135
0136 function xM = sf_make_mask(xM, VY, params)
0137
0138
0139
0140
0141
0142
0143 if ~isstruct(xM)
0144 xM = struct( 'T', [],...
0145 'TH', xM,...
0146 'I', 0,...
0147 'VM', {[]},...
0148 'xs', struct('Masking','analysis threshold'));
0149 end
0150
0151
0152 mask = zeros(VY(1).dim(1:3));
0153
0154
0155 if isempty(xM.TH)
0156 xM.TH = zeros(size(VY))-Inf;
0157 thmf = 0;
0158 else
0159 thmf = any(xM.TH(:) ~= -Inf);
0160 end
0161 [vol X Y Z] = deal(VY(1),1,2,3);
0162
0163 plsz = prod(vol.dim(X:Y));
0164 xords = [1:vol.dim(X)]'*ones(1,vol.dim(Y));
0165 yords = ones(vol.dim(X),1)*[1:vol.dim(Y)];
0166 tPl = [xords(:) yords(:)]';
0167
0168 YNaNrep = spm_type(VY(1,1).dim(4),'nanrep');
0169 IM = xM.I & ~YNaNrep & xM.TH(:,1)<0;
0170
0171 fprintf(' Calculating masking image...');
0172
0173 for z = 1:vol.dim(3)
0174 pPl = tPl;
0175 pPl(Z,:) = ones(1, plsz)*z;
0176
0177 if thmf
0178 for i = 1:prod(size(VY))
0179 if isempty(pPl), break,end
0180 vals = spm_sample_vol(VY(i),pPl(X,:),pPl(Y,:),pPl(Z,:),0);
0181 msk = vals>xM.TH(i);
0182 if IM(i), msk = msk & abs(vals)>eps; end
0183 pPl = pPl(:, msk);
0184 end
0185 end
0186
0187 for i = 1:prod(size(xM.VM))
0188 if isempty(pPl), break,end
0189 tM = inv(xM.VM(i).mat)*vol.mat;
0190 tmp = tM * [pPl;ones(1,size(pPl,2))];
0191
0192
0193
0194 vals = spm_sample_vol(xM.VM(i),tmp(X,:),tmp(Y,:),tmp(Z,:),0);
0195 pPl = pPl(:, vals>0);
0196 end
0197 if ~isempty(pPl)
0198 inds = pPl(X,:)+(pPl(Y,:)-1)*vol.dim(X)+(z-1)*plsz;
0199 mask(inds) = 1;
0200 end
0201 end
0202 fprintf('...done\n');
0203
0204
0205 VM = struct('fname', 'voxmask.img',...
0206 'dim', [vol.dim(1:3),spm_type('uint8')],...
0207 'mat', vol.mat,...
0208 'pinfo', [1 0 0]',...
0209 'descrip', 'phiw:resultant analysis mask');
0210 fprintf(' Saving the intial analysis mask as %s ...', VM.fname);
0211 spm_write_vol(VM, mask);
0212 fprintf('...done\n');
0213
0214
0215 wvVM = pr_wvmask(VM, params);
0216
0217
0218 fprintf(' Saving the wavelet transformed mask as %s...', wvVM.wvol.fname);
0219 wvm = write_wtimg(wvVM);
0220 fprintf('...done\n');
0221
0222
0223 xM = struct(...
0224 'TH',ones(size(VY))*-Inf,...
0225 'T', -Inf,...
0226 'I', 0,...
0227 'VM',wvm.wvol, ...
0228 'xs', struct(...
0229 'Analysis_threshold','None (-Inf)',...
0230 'Explicit_masking', 'Yes'),...
0231 'wave', thin(wvm));
0232
0233 return