0001 function PRT = prt_model(PRT,in)
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
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 [modelid, PRT] = prt_init_model(PRT,in);
0046
0047
0048 PRT.model(modelid).input.type = in.type;
0049 if strcmp(in.type,'classification')
0050 for c = 1:length(in.class)
0051 PRT.model(modelid).input.class(c) = in.class(c);
0052 end
0053 end
0054
0055 for f = 1:length(in.fs)
0056 fid = prt_init_fs(PRT,in.fs(f));
0057
0058 if length(PRT.fs(fid).modality) > 1 && length(in.fs) > 1
0059 error('prt_model:multipleFeatureSetsAppliedAsSamplesAndAsFeatures',...
0060 ['Feature set ',in.fs(f).fs_name,' contains multiple modalities ',...
0061 'and job specifies that multiple feature sets should be ',...
0062 'supplied to the machine. This usage is not supported.']);
0063 end
0064
0065 PRT.model(modelid).input.fs(f).fs_name = in.fs(f).fs_name;
0066 end
0067
0068
0069
0070 if strcmp(in.type,'classification')
0071 [targets, samp_idx, t_allscans, samp_allscans] = compute_targets(PRT, in);
0072 else
0073 [targets, samp_idx, t_allscans] = compute_target_reg(PRT, in);
0074 end
0075
0076 if isfield(in,'include_allscans') && in.include_allscans
0077 PRT.model(modelid).input.samp_idx = samp_allscans;
0078 PRT.model(modelid).input.include_allscans = in.include_allscans;
0079 else
0080 PRT.model(modelid).input.samp_idx = samp_idx;
0081 PRT.model(modelid).input.include_allscans = false;
0082 end
0083 PRT.model(modelid).input.targets = targets;
0084 PRT.model(modelid).input.targ_allscans = t_allscans;
0085
0086
0087
0088 PRT.model(modelid).input.cv_mat = compute_cv_mat(PRT,in, modelid);
0089 PRT.model(modelid).input.operations = in.operations;
0090
0091
0092 PRT.model(modelid).input.cv_type=in.cv.type;
0093
0094
0095 disp('Updating PRT.mat.......>>')
0096 if spm_matlab_version_chk('7') >= 0
0097 save(in.fname,'-V7','PRT');
0098 else
0099 save(in.fname,'-V6','PRT');
0100 end
0101
0102 end
0103
0104
0105
0106
0107
0108 function [targets, samp_idx, t_all samp_all] = compute_targets(PRT, in)
0109
0110
0111
0112 fid = prt_init_fs(PRT, in.fs(1));
0113 ID = PRT.fs(fid).id_mat;
0114 n = size(ID,1);
0115
0116
0117 if length(in.fs) > 1
0118 for f = 1:length(in.fs)
0119 fid = prt_init_fs(PRT, in.fs(f));
0120 if size(PRT.fs(fid).id_mat,1) ~= n
0121 error('prt_model:sizeOfFeatureSetsDiffer',...
0122 ['Multiple feature sets included, but they have different ',...
0123 'numbers of samples']);
0124 end
0125 end
0126 end
0127
0128 modalities = {PRT.masks(:).mod_name};
0129 groups = {PRT.group(:).gr_name};
0130
0131 t_all = zeros(n,1);
0132 samp_all = zeros(n,1);
0133 for c = 1:length(in.class)
0134
0135
0136 for g = 1:length(in.class(c).group)
0137 gr_name = in.class(c).group(g).gr_name;
0138 if any(strcmpi(gr_name,groups))
0139 gid = find(strcmpi(gr_name,groups));
0140 else
0141 error('prt_model:groupNotFoundInPRT',...
0142 ['Group ',gr_name,' not found in PRT.mat']);
0143 end
0144
0145
0146 for s = 1:length(in.class(c).group(g).subj)
0147 sid = in.class(c).group(g).subj(s).num;
0148
0149 for m = 1:length(in.class(c).group(g).subj(s).modality)
0150 mod_name = in.class(c).group(g).subj(s).modality(m).mod_name;
0151 if any(strcmpi(mod_name,modalities))
0152 mid = find(strcmpi(mod_name,modalities));
0153 else
0154 error('prt_model:groupNotFoundInPRT',...
0155 ['Modality ',mod_name,' not found in PRT.mat']);
0156 end
0157
0158 if isfield(in.class(c).group(g).subj(s).modality(m), 'all_scans')
0159
0160
0161 if strcmpi(PRT.fs(fid).modality(m).mode,'all_cond')
0162 error('prt_model:fsIsAllCondModelisAllScans',...
0163 ['''All scans'' selected for subject ',num2str(s),...
0164 ', group ',num2str(g), ', modality ', num2str(m),...
0165 ' but the feature set was constructed using ',...
0166 '''All conditions''. This syntax is invalid. ',...
0167 'Please use ''All Conditions'' instead.']);
0168 end
0169
0170
0171
0172 idx = ID(:,1) == gid & ID(:,2) == sid & ID(:,3) == mid;
0173 t_all(idx) = c;
0174 else
0175
0176 conds = {PRT.group(gid).subject(sid).modality(mid).design.conds(:).cond_name};
0177
0178
0179 if ~isfield(PRT.group(gid).subject(sid).modality(mid).design,'conds')
0180 error('prt_model:conditionsSpecifiedButNoneInDesign',...
0181 ['Conditions selected for subject ',num2str(s),...
0182 ', class ',num2str(c),', group ',num2str(g), ...
0183 ', modality ', num2str(m),' but there are none in the design. ',...
0184 'Please use ''All Scans'' or adjust design.']);
0185 end
0186 if isfield(in.class(c).group(g).subj(s).modality(m), 'all_cond')
0187
0188 for cid = 1:length(conds)
0189 idx = ID(:,1) == gid & ID(:,2) == sid & ID(:,3) == mid & ID(:,4) == cid;
0190 t_all(idx) = c;
0191 end
0192 else
0193 for cond = 1:length(in.class(c).group(g).subj(s).modality(m).conds)
0194 cond_name = in.class(c).group(g).subj(s).modality(m).conds(cond).cond_name;
0195
0196 if any(strcmpi(cond_name,conds))
0197 cid = find(strcmpi(cond_name,conds));
0198 else
0199 error('prt_model:groupNotFoundInPRT',...
0200 ['Condition ',cond_name,' not found in PRT.mat']);
0201 end
0202
0203 idx = ID(:,1) == gid & ID(:,2) == sid & ID(:,3) == mid & ID(:,4) == cid;
0204 t_all(idx) = c;
0205 end
0206 end
0207 s_idx_mod = ID(:,1) == gid & ID(:,2) == sid & ID(:,3) == mid;
0208 samp_all(s_idx_mod) = 1;
0209 end
0210 end
0211 end
0212 end
0213 end
0214
0215 samp_idx = find(t_all);
0216 samp_all = find(samp_all);
0217 targets = t_all(samp_idx);
0218
0219 end
0220
0221 function CV = compute_cv_mat(PRT, in, modelid)
0222
0223
0224 fid = prt_init_fs(PRT, in.fs(1));
0225
0226 if isfield(in,'include_allscans') && in.include_allscans
0227
0228 ID = PRT.fs(fid).id_mat;
0229 else
0230
0231
0232
0233
0234 ID = PRT.fs(fid).id_mat(PRT.model(modelid).input.samp_idx,:);
0235 end
0236
0237 switch in.cv.type
0238 case 'loso'
0239
0240
0241 gids = unique(ID(:,1));
0242 gc = 0;
0243 for g = 1:length(gids)
0244 gidx = ID(:,1) == gids(g);
0245 ID(gidx,2) = ID(gidx,2) + gc;
0246 gc = gc + max(ID(gidx,2));
0247 end
0248
0249
0250 snums = histc(ID(:,2),unique(ID(:,2)));
0251 if length(snums) == 1
0252 error('prt_model:losoSelectedWithOneSubject',...
0253 'LOSO CV selected but only one subject is included');
0254 end
0255
0256 G = cell(length(snums),1);
0257 for s = 1:length(snums)
0258 G{s} = ones(snums(s),1);
0259 end
0260 CV = blkdiag(G{:}) + 1;
0261
0262 case 'losgo'
0263
0264 sids = unique(ID(:,2));
0265 if length(sids) == 1
0266 error('prt_model:losoSelectedWithOneSubject',...
0267 'LOSGO CV selected but only one subject is included');
0268 end
0269
0270 CV = zeros(size(ID,1),length(sids));
0271 for s = 1:length(sids)
0272 sidx = ID(:,2) == sids(s);
0273 CV(:,s) = double(sidx) + 1;
0274 end
0275
0276 case 'lobo'
0277
0278
0279
0280 snums = histc(ID(:,5),unique(ID(:,5)));
0281 G = cell(length(snums),1);
0282 for s = 1:length(snums)
0283 G{s} = ones(snums(s),1);
0284 end
0285 CV = blkdiag(G{:}) + 1;
0286
0287 case 'locbo'
0288
0289 error('leave-one-condition-per-block-out not yet implemented');
0290
0291 case 'loro'
0292
0293
0294 mids = unique(ID(:,3));
0295
0296 CV = zeros(size(ID,1),length(mids));
0297 for m = 1:length(mids)
0298 midx = ID(:,3) == mids(m);
0299 CV(:,m) = double(midx) + 1;
0300 end
0301
0302 case 'custom'
0303 error('custom CV not implemented yet');
0304
0305 otherwise
0306 error('prt_cv:unknownTypeSpecified',...
0307 ['Unknown type specified for CV structure (',in.type',')']);
0308 end
0309
0310 end
0311
0312 function [targets, samp_idx, targ_allscans]=compute_target_reg(PRT, in)
0313
0314
0315
0316 fid = prt_init_fs(PRT, in.fs(1));
0317 ID = PRT.fs(fid).id_mat;
0318 n = size(ID,1);
0319
0320 modalities = {PRT.masks(:).mod_name};
0321 groups = {PRT.group(:).gr_name};
0322
0323 targ_allscans=zeros(n,1);
0324 samp_idx=[];
0325 targ_g=[];
0326 for g = 1:length(in.group)
0327 gr_name = in.group(g).gr_name;
0328 if any(strcmpi(gr_name,groups))
0329 gid = find(strcmpi(gr_name,groups));
0330 else
0331 error('prt_model:groupNotFoundInPRT',...
0332 ['Group ',gr_name,' not found in PRT.mat']);
0333 end
0334 targets=zeros(length(in.group(g).subj),1);
0335
0336 for s = 1:length(in.group(g).subj)
0337
0338 m=1;
0339 mod_name = in.group(g).subj(s).modality(m).mod_name;
0340 if any(strcmpi(mod_name,modalities))
0341 mid = find(strcmpi(mod_name,modalities));
0342 else
0343 error('prt_model:modalityNotFoundInPRT',...
0344 ['Modality ',mod_name,' not found in PRT.mat']);
0345 end
0346 idx = in.group(g).subj(s).num;
0347 targets(s) = PRT.group(gid).subject(idx).modality(mid).rt_subj;
0348 samp_idx=[samp_idx; find(ID(:,1) == gid & ID(:,2) == idx & ID(:,3) == mid)];
0349
0350 end
0351 targ_g=[targ_g;targets];
0352 end
0353 targ_allscans(samp_idx)=targ_g;
0354 targets=targ_g;
0355 end