0001 function output = prt_machine(d,m)
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 SANITYCHECK = true;
0044
0045
0046
0047 if SANITYCHECK==true
0048
0049 if ~isempty(m)
0050 if isstruct(m)
0051 if isfield(m,'function')
0052
0053 if ~exist(m.function,'file')
0054 error('prt_machine:machineFunctionFileNotFound',...
0055 ['Error: %s function could not be found!'],...
0056 m.function);
0057 end
0058 else
0059 error('prt_machine:machineFunctionFieldNotFound',...
0060 ['Error: machine structure should contain'...
0061 ' ''.function'' field!']);
0062 end
0063 if ~isfield(m,'args')
0064 error('prt_machine:argsFieldNotFound',...
0065 ['Error: machine structure should contain' ...
0066 ' ''.args'' field!']);
0067 end
0068 else
0069 error('prt_machine:machineNotStruct',...
0070 'Error: machine should be a structure!');
0071 end
0072 else
0073 error('prt_machine:machineStructEmpty',...
0074 'Error: ''machine'' struct cannot be empty!');
0075 end
0076
0077
0078
0079 if ~isempty(d)
0080
0081 if ~isfield(d,'train')
0082 error('prt_machine:missingField_train',...
0083 ['Error: ''data'' struct must contain a ''train'' '...
0084 ' field!']);
0085 end
0086 if ~isfield(d,'test')
0087 error('prt_machine:missingField_test',...
0088 ['Error: ''data'' struct must contain a ''test'' '...
0089 ' field!']);
0090 end
0091 if ~isfield(d,'tr_targets')
0092 error('prt_machine:missingField_tr_targets',...
0093 ['Error: ''data'' struct must contain a ''tr_targets'' '...
0094 ' field!']);
0095 end
0096 if ~isfield(d,'use_kernel')
0097 error('prt_machine:missingField_use_kernel',...
0098 ['Error: ''data'' struct must contain a ''use_kernel'' '...
0099 ' field!']);
0100 end
0101 if ~isfield(d,'pred_type')
0102 error('prt_machine:missingField_pred_type',...
0103 ['Error: ''data'' struct must contain a ''pred_type'' '...
0104 ' field!']);
0105 end
0106
0107
0108
0109 if isempty(d.train) || isempty(d.test),
0110 error('prt_machine:TrAndTeEmpty',...
0111 'Error: training and testing data cannot be empty!');
0112 else
0113 if ~iscell(d.train) || ~iscell(d.test),
0114 error('prt_machine:TrAndTeEmpty',...
0115 'Error: training and testing data should be cell arrays!');
0116 end
0117 end
0118
0119
0120 if ~isempty(d.tr_targets)
0121 if isvector(d.tr_targets)
0122
0123 d.tr_targets = d.tr_targets(:);
0124 Ntrain_lbs = length(d.tr_targets);
0125 else
0126 error('prt_machine:trainingLabelsNotVector',...
0127 'Error: training labels should be a vector!');
0128 end
0129 else
0130 error('prt_machine:trainingLabelsEmpty',...
0131 'Error: training labels cannot be empty!');
0132 end
0133
0134
0135 Nk_train = length(d.train);
0136
0137
0138 if Nk_train > 1
0139 error('prt_machine:MKLnotSupported',...
0140 'Error: Multi-kernel learning not supported yet!');
0141 end
0142
0143
0144
0145 if strcmp(d.pred_type,'regression')
0146 if ~any(strcmp(m.function,{'prt_machine_krr','prt_machine_rvr',...
0147 'prt_machine_gpml','prt_machine_gpr'}))
0148 error('prt_machine:RgressionMachineSupport',...
0149 'Error: Regresion can only chose use KRR or RVR machines');
0150 end
0151 end
0152
0153
0154 for k = 1:Nk_train,
0155 if ~isempty(d.train{k}) && ~isempty(d.test{k})
0156 if (~prt_ismatrix(d.train{k}) && ~isvector(d.train{k}) ) || ...
0157 (~prt_ismatrix(d.test{k}) && ~isvector(d.test{k}) )
0158 error('prt_machine:TrAndTeNotMatrices',...
0159 ['Error: training and testing datasets should ' ...
0160 ' be either matrices or vectors!']);
0161 end
0162 else
0163 error('prt_machine:TrAndTeEmpty',...
0164 'Error: training and testing datasest cannot be empty!');
0165 end
0166
0167 [Ntrain Dtrain] = size(d.train{k});
0168 [Ntest, Dtest] = size(d.test{k});
0169
0170 if ~(Dtrain==Dtest)
0171 error('prt_machine:DtrNotEqDte',['Error: Training and testing '...
0172 'dimensions should match, but Dtrain=%d and Dtest=%d for '...
0173 'dataset %d!'],Dtrain,Dtest,k);
0174 end
0175
0176 if ~(Ntrain_lbs==Ntrain)
0177 error('prt_machine:NtrlbsNotEqNtr',['Error: Number of training '...
0178 'examples and training labels should match, but Ntrain_lbs=%d '...
0179 'and Ntrain=%d for dataset %d!'],Ntrain_lbs,Ntrain,k);
0180 end
0181
0182 if d.use_kernel
0183 if ~(Ntrain==Dtrain)
0184 error('prt_machine:NtrainNotEqDtrain',['Error: Training '...
0185 'dimensions should match, but Ntr=%d and Dtr=%d for '...
0186 'dataset %d!'],Ntrain,Dtrain,k);
0187 end
0188 if ~(Dtest==Ntrain)
0189 error('prt_machine:DtestNotEqNtrain',['Error: Testing '...
0190 'dimensions should match, but Dte=%d and Ntr=%d for '...
0191 'dataset %d!'],Dtest,Ntrain,k);
0192 end
0193 end
0194 end
0195 else
0196 error('prt_machine:dataStructEmpty',...
0197 'Error: data struct cannot be empty!');
0198 end
0199 end
0200
0201
0202
0203 fnch = str2func(m.function);
0204
0205 try
0206 output = fnch(d,m.args);
0207 catch
0208 err = lasterror;
0209 err_ID=lower(err.identifier);
0210 err_libProblem = strfind(err_ID,'libnotfound');
0211 err_argsProblem = strfind(err_ID,'argsproblem');
0212 disp('prt_machine: machine did not run sucessfully.');
0213 if ~isempty(err_libProblem)
0214 error('prt_machine:libNotFound',['Error: the library for '...
0215 'machine %s could not be found on your path. '],m.function);
0216 elseif ~isempty(err_argsProblem)
0217 disp(['Error: the arguments supplied '...
0218 ' are invalid. ' ...
0219 'SOLUTION: Please follow the advice given by the machine.']);
0220 error('prt_machine:argsProblem',...
0221 'Error running machine %s: %s %s', ...
0222 m.function,err.identifier,err.message);
0223 else
0224
0225 disp(['SOLUTION: Please read the message below and attempt to' ...
0226 ' correct the problem, or ask the developpers for ' ...
0227 'assistance by copy-pasting all messages and explaining the'...
0228 ' exact steps that led to the problem.']);
0229 disp(['These kinds of issues are typically caused by Matlab '...
0230 'path problems.']);
0231 for en=numel(err.stack):-1:1
0232 e=err.stack(en);
0233 fprintf('%d : function [%s] in file [%s] at line [%d]\n',...
0234 en,e.name,e.file,e.line);
0235 end
0236 error('prt_machine:otherProblem',...
0237 'Error running machine %s: %s %s', ...
0238 m.function,err.identifier,err.message);
0239 end
0240 end
0241
0242
0243
0244 if SANITYCHECK==true
0245
0246
0247 if ~isfield(output,'predictions');
0248 error('prt_machine:outputNoPredictions',['Output of machine should '...
0249 'contain the field ''.predictions''.']);
0250 else
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260 end
0261
0262 end
0263
0264 end
0265
0266
0267 function out = prt_ismatrix(A)
0268
0269
0270
0271
0272 out=(ndims(A)==2) && (min(size(A)) ~= 1);
0273 end