function U02_test_isomap_lle(method, n_rule)
%U02_TEST_ISOMAP_LLE          Test for Isomap and LLE.
%
%synopsis
%  U02_test_isomap_lle(method, n_rule)
%
%arguments
%  method: Function which should be tested. Has to be 'isomap' or 'lle'.
%  n_rule: Rule to build the graph, has to be 'knn' or 'eps-ball'.
%
%usage
%  U02_test_isomap_lle('isomap', 'knn')
%  U02_test_isomap_lle('isomap', 'eps-ball')
%  U02_test_isomap_lle('lle', 'knn')
%  U02_test_isomap_lle('lle', 'eps-ball')
%
%author
%  buenau@cs.tu-berlin.de, saputra@cs.tu-berlin.de

if strcmp(method, 'isomap') == 0 & strcmp(method, 'lle') == 0
    fprintf('Unknown method: %s, has to be ''isomap'' or ''lle''\n', method);
    return;
end

if strcmp(n_rule, 'knn') == 0 & strcmp(n_rule, 'eps-ball') == 0
    fprintf('Unknown n_rule: %s, has to be ''knn'' or ''eps-ball''\n', method);
    return;
end

n = 500;

% Generate data with structure in the first two dimensions 
% + eig noise dimensions (relatively small variance).
Xt = 10*rand(2,n);
X = [ Xt; 0.5*randn(8,n) ];

% Rotate data randomly.
X = randrot(10)*X;

if strcmp(n_rule, 'knn')
    param = 30;
else
    param = 4;
end

% check which method should be used (isomap or lle)
if strcmp(method, 'isomap')
    Xp = Isomap(X, 2, n_rule, param);
else
    Xp = LLE(X, 2, n_rule, param);
end

% Plot true 2D manifold embedded in ten dimensions.
clf;
subplot(1,3,1);
scatter(Xt(1,:), Xt(2,:));
title('True 2D manifold');
axis image;
xlabel('x_1');
ylabel('x_2');
set(gca, 'XLim', [-1 11]);
set(gca, 'YLim', [-1 11]);
set(gca, 'XTick', [], 'YTick', []);

% Plot 2D embedding found by isomap/lle with colors according to the 
% first manifold coordinate.
subplot(1,3,2);
scatter(Xp(1,:), Xp(2,:), [], Xt(1,:));
title('Embedding colored according to x_1');
axis image;
set(gca, 'XTick', [], 'YTick', []);

% Plot 2D embedding found by isomap/lle with colors according to the 
% second manifold coordinate.
subplot(1,3,3);
scatter(Xp(1,:), Xp(2,:), [], Xt(2,:));
title('Embedding colored according to x_2');
axis image;
set(gca, 'XTick', [], 'YTick', []);

function [ R, M ] = randrot(d)
%RANDROT        Generate random orthogonal matrix. 
%
%usage
%  [R,M] = randrot(d)
%
%author
%  buenau@cs.tu-berlin.de

M = 100*(rand(d,d)-0.5);
M = 0.5*(M-M');
R = expm(M);

