%  [label_pred, confidence] = netpred(data, preproc, netarch, options)

%

%    Perform preprocessing and run network over given data.

%

%    Input:   data     =  given input data

%             preproc  =  previously revealed preprocessing parameters

%             netarch  =  previously revealed network architecture

%             options  =  options for network processing. Should consist of:

%                         .preproc_options (see definition of findarch.m)

%                         .conf_options    = 'variance' OR 'bp' OR 'risk'

%

%    Output:  label_pred  =  output label produced by network

%             confidence  =  confidence level

%



function [label_pred, confidence] = netpred(data, preproc, netarch, options)



disp('netpred / Yuval Lifshitz, Liron Weissman, Gilad Arnold');



% Preprocess the data

procoptsions = options.preproc_options;

train_label = [];

[processed, preproc] = findpreproc(data, train_label, procoptsions, preproc);



% Execute the ensemble on the data

for i = 1:15

  	label_pred_vector(i) = sim(netarch(i).net, processed');

end



% Let the ensembles vote for the predicted label

if (sum(label_pred_vector) > size(label_pred_vector)/2)

   label_pred = 1;

else

   label_pred = 0;

end



% Check the confidence of the answer

if (strcmp(options.conf_options, 'variance'))

   confidence = var(label_pred_vector);

elseif (strcmp(options.conf_options, 'bp'))

   count = 0;

   for i = 1 : 15

      if (label_pred_vector(i) == label_pred)

         confidence = confidence + netarch(i).mse_test_error;

         count = count + 1;

      end

   end

   confidence = confidence / count;

else

   error('netpred: variance option not provided or invalid');

end

