function [xe xs xh y] = prepareData(forWhat)
%
% function [xe xs xh y] = prepareData(forWhat)
%
% prepareData basically takes all the data that we collect and returns 
% three things: xe, xs, and y. When we collect more data, this is the 
% function we add the data to.
%
% forWhat = (transductive) 'SVM' (Support Vector Machine) or 'LR' (Logistic
%           Regression) or 'NB' (Naive Bayes)
% 
% "xe" is the N x 4 matrix containing the (dist, freq, orientation, dEab) 
% value for every point classified.
% for example, if there are two points, and only their dEab value is different,
% xe = [1 1 1 0.5; 1 1 1 30]
% 
% "xs" is the N x 4 matrix containing the 
% (dist, freq, orientation, spatial dEab) value for every point classified
% 
% "y" contains the column vector that labels each row of "xe" or "xs" as 
% "colored" (value of 1) or "not colored" (value of 0)
%
% See also LOGISTICREGRESSION, CLASIFYLR
%

%% Check string
if (~ischar(forWhat)), error('Preparing data ''forWhat'' must be a string: ''SVM'' or ''LR'''); end
if (strcmp(upper(forWhat), 'SVM') == 0 & strcmp(upper(forWhat), 'LR') == 0 & strcmp(upper(forWhat),'NB') == 0)
    error('You must specifying for what you are preparing the data: ''SVM'', ''LR'' (logistic regression), or ''NB'' (naive Bayes).');
end

%% Load Data
SandyData;
ErinData;
OtherData_c;
OtherData_d;
OtherData_e;
load errors_final.mat

%% Format Properly
num_dist = length(dist_cm); % in cm
num_freq = length(freq); % in CPD
num_angle = length(ang); % in degrees

%% Prepare data for carious classifiers
% x stores the "feature" vector, which contains the dimensions of distance,
% frequency, orientation, and error. xs is the same vector, but using the
% sCIELAB error. A constant "1" is also in the data to deal with "constant"
% terms later (just ignore it for now).
xe=[];
xs=[];
xh=[];

% y stores the "truth" vector. A 1 corresponds to the element that is
% "colored" and a 0 corresponds to the element that is not "colored."
y=[];
info = ['a' 'b' 'c' 'd' 'e'];

for I = 1:num_dist,
    eval(['cur_dat_' num2str(I) ' = 0;']);
    for L = 1:length(info),
        eval(['cur_dat_' num2str(I) '= cur_dat_' num2str(I) '+' info(L) num2str(I) ';']);
    end
end

for I = 1:num_dist,
    eval(['cur_dat = cur_dat_' num2str(I) ';']);
    for J = 1:num_freq,
        for K = 1:num_angle,
            xe=[xe; dist_cm(I) freq(J) ang(K) error_CIE(K,J)];
            xh=[xh; dist_cm(I) freq(J) ang(K) error_hasler(K,J)];
            eval(['xs=[xs; dist_cm(I) freq(J) ang(K) error_sCIE_' num2str(I) '(K,J)];']);
            y=[y; cur_dat(K,J)];
        end
    end
end

%% Write out data in the "appropriate" format
if (strcmp(upper(forWhat), 'LR') == 1)
    % For logistic regression, we want binary data (1's and 0's). The 0's
    % represent non-colored points and the 1's represent colored points. We
    % determine whether a data point is colored or not by the certainty of
    % the behavioral data. If 4 out of 6 people thought a harmonic was
    % colored after processing, it is labeled as 1. Similarly, if 4 out of
    % 6 people thought a harmonic was not colored after processing, it is
    % labeled as 0. Contested harmonics are discarded from the data.
    index = [find(y==0); find(y==1); find(y==4); find(y==5);];%; find(y==3)];
    xe = xe(index,:);
    xs = xs(index,:);
    xh = xh(index,:);
    y = y(index);
    y(find(y==0)) = 0;
    y(find(y==1)) = 0;
    y(find(y==4)) = 1;
    y(find(y==5)) = 1;
elseif (strcmp(upper(forWhat), 'NB') == 1)
    % To prepare for Naive Bayes classification, we want to create three
    % "bins" for the data--the "colored," "not colored," and "unknown"
    % bins. We use the same labeling system as before, except label the
    % "colored" as 2 and the "unknowns" as 1. The Naive Bayes algorithm
    % then assigns a data point to the "bin" with the highest a posteriori
    % probability.
    y(find(y==0)) = 0;
    y(find(y==1)) = 0;
    y(find(y==2)) = 1;
    y(find(y==3)) = 1;
    y(find(y==4)) = 2;
    y(find(y==5)) = 2;    
elseif (strcmp(upper(forWhat), 'SVM') == 1)
    % To prepare data for the (transductive) SVM algorithm, "colored"
    % points are labeled as 1, "not colored" points are labeled as -1, and
    % "unknowns" are labeled as 0. The SVM then finds a linear separator
    % for the data while "classifying" the unknowns appropriately.
    y(find(y==0)) = -1;
    y(find(y==1)) = -1;
    y(find(y==2)) = 0;
    y(find(y==3)) = 0;
    y(find(y==4)) = 1;
    y(find(y==5)) = 1;
end
