% Script to create a SVM for each cluster after apc clustering

% Params:
% apc_st4tmeshpat = cluster membership
% apc_st4temesh.stain = staining patterns of each cluster member
% 


%src_training = apc_st4temesh.stain;
src_training = st4tmesh_svm';

clus_ctr = unique(apc_st4tmeshpat);

apc_svm = cell(length(clus_ctr), 1);

for clus_no = 1:length(clus_ctr),

    [data, class] = apc2trainingset(apc_st4tmeshpat, src_training, clus_ctr(clus_no), 4);
    data_n = libsvm_normalize(data');
    [c, g, cv] = libsvm_gridsearch(class, data_n);
    
    apc_svm{clus_no}.center = clus_ctr(clus_no);
    apc_svm{clus_no}.c = c;
    apc_svm{clus_no}.g = g;
    apc_svm{clus_no}.cv = cv;
    
    cmd = ['-c ', num2str(c), ' -g ', num2str(g), ' -b 1 -t 2'];
    apc_svm{clus_no}.model = svmtrain(class, data_n, cmd);

end

fprintf('Results:\n');
for clus_no = 1:length(clus_ctr),

    fprintf('%d: center=%d c=%g, g=%g, accuracy=%g\n', clus_no, ...
        apc_svm{clus_no}.center, apc_svm{clus_no}.c, apc_svm{clus_no}.g, apc_svm{clus_no}.cv );
    
end