%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab script to do Sammon's mapping on agriculture data
%
% Sammon's mapping, experimental
% 
% requires:
% - statistics toolbox for princomp 
% - readColData script (see link below)
% - probably savefig script from matlab central
%   (mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10889)
% - a local (user's) .matlab directory 
% - Sammon's Mapping script from: 
%   http://theoval.sys.uea.ac.uk/~gcc/matlab/default.html#sammon
% - SOM toolbox from:
%   http://www.cis.hut.fi/projects/somtoolbox/
% -----
% Georg Ru{\ss}
% russ@iws.cs.uni-magdeburg.de
% 2008-12-30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% clean workspace
clear all;

%% Preparation steps, workspace

% set clock
clock_start = clock;
% seed random for reproducible results
rand('seed',12345);

sD330=som_read_data('330_2006-alle-labels-som.data');
sD131=som_read_data('131_var_all_labels_som.data');
sD131net= som_read_data('131_var_netz_som.data');

% change paths to wherever your data is located and readable to matlab
% uses script readColData from
% http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/loadingPlotData.html#colHeadings
%[label,id_column,data]=readColData('sorted_all',10,10,1);

% convert labels to strings instead of character arrays; transpose
%label = transpose(cellstr(label));


%% data-specific stuff

% none

%% sammon's mapping stuff
% compute mapping

sD330.sammon = sammon(sD330.data,2);
sD131.sammon = sammon(sD131.data,2);

%%
sD131net.sammon = sammon(sD131net.data,2);



%% movie generation
makemovie(sD330, 'F330');
makemovie(sD131, 'F131');
makemovie(sD131net, 'F131-net');

%% figure generation for sD330 data set

% set directory for graphics output
cd('/tmp')


% set directory for graphics output
cd('/tmp')

% the following three lines generate an index
% which is later used to plot the respective data points where
% an attribute is 'F', 'N' or 'S'
[x,idx] = unique(sD330.data(:,1:8), 'rows');
t = char(sD330.labels(idx,1));
n = size(x, 1);

% plot results

figure(1);
clf;
%set(axes, 'FontSize', 16);
plot(sD330.sammon(t == 'F',1), sD330.sammon(t == 'F',2), 'k.', ...
   sD330.sammon(t == 'N',1), sD330.sammon(t == 'N',2), 'k+', ...
    sD330.sammon(t == 'S',1), sD330.sammon(t == 'S',2), 'ko');

title(['Sammon Mapping of the F330 data set']);
legend('F - farm strategy', 'N - ANN strategy', 'S - sensor strategy');

savefig('sD330-all-sammon','eps');
savefig('sD330-all-sammon','png');

%% for second data set

[x,idx] = unique(sD131.data(:,1:8), 'rows');
t = char(sD131.labels(idx,1));
n = size(x, 1);

figure(2);
clf;
%set(axes, 'FontSize', 16);
plot(sD131.sammon(t == 'F',1), sD131.sammon(t == 'F',2), 'r.', ...
   sD131.sammon(t == 'N',1), sD131.sammon(t == 'N',2), 'b+');

title(['Sammon Mapping of the F131 data set']);
legend('F - farm strategy', 'N - ANN strategy');

savefig('sD131-all-sammon','eps');
savefig('sD131-all-sammon','png');

%% figure generation for third data set

% set directory for graphics output
cd('/tmp')

% the following three lines generate an index
% which is later used to plot the respective data points where
% an attribute is 'F', 'N' or 'S'
[x,idx] = unique(sD131net.data(:,1:8), 'rows');
%t = char(sD131net.labels(idx,1));
n = size(x, 1);

yield06 = sD131net.data(idx,8);
reip49 = sD131net.data(idx,7);
em38 = sD131net.data(idx,1);
n1 = sD131net.data(idx,3);

% plot results

figure(1);
clf;
%set(axes, 'FontSize', 16);
%plot(sD131net.sammon(:,1), sD131net.sammon(:,2), 'k.')
%, ...
plot(sD131net.sammon(yield06>=5,1), sD131net.sammon(yield06>=5,2), 'ko', ...
    sD131net.sammon(yield06<5,1), sD131net.sammon(yield06<5,2), 'k+');
title(['F131-net, split at YIELD06 threshold of 5.00'], ...
    'FontSize', 16);
legend('YIELD06 >= 5', 'YIELD06 < 5');
savefig('F131net-sammon-yield06-split-5','eps');

plot(sD131net.sammon(yield06>=5,1), sD131net.sammon(yield06>=5,2), 'ko');
title(['Sammon Mapping of F131-net, YIELD06 >= 5.00'], ...
    'FontSize', 16);
savefig('F131net-sammon-yield06-geq-5','eps');

plot(sD131net.sammon(yield06<5,1), sD131net.sammon(yield06<5,2), 'k+');
title(['Sammon Mapping of F131-net, YIELD06 < 5.00'], ...
    'FontSize', 16);
savefig('F131net-sammon-yield06-smaller-5','eps');

plot(sD131net.sammon(reip49<=725,1), sD131net.sammon(reip49<=725,2), 'k+');
title(['Sammon Mapping of F131-net, REIP49 <= 725'], ...
    'FontSize', 16);
savefig('F131net-sammon-reip49-leq-725','eps');

plot(sD131net.sammon(em38<=55,1), sD131net.sammon(em38<=55,2), 'k+');
title(['Sammon Mapping of F131-net, EM38 <= 55'], ...
    'FontSize', 16);
savefig('F131net-sammon-em38-leq-55','eps');

plot(sD131net.sammon(n1>=60,1), sD131net.sammon(n1>=60,2), 'k+');
title(['Sammon Mapping of F131-net, N1 >= 60'], ...
    'FontSize', 16);
savefig('F131net-sammon-n1-geq-60','eps');

%% get script stats
duration = etime(clock, clock_start)
