% boston_labeled_from_classer.m
% 
%   Downloads boston dataset in Classer's format, 
%   runs Classer's preprocessing scripts, 
%   and puts strip data into a form usable by DefAM_crossValidation
%
%   This first cell in this script requires that Classer be installed 
%   and that the classer executable "csd" to be on the operating system path. 
%   Since Classer runs only on Windows, so does this cell.
%
%   ***INSTEAD OF RUNNING THIS SCRIPT***
%   Consider downloading the mat file as done in DefAM_benchmarks.m
%
%   Greg Amis
%   Technology Lab
%   Dept. of Cognitive & Neural Systems
%   Boston University
%   6 April 2009
%
%   Licensing Policy
%   This implementation is made available to the general public under the terms 
%   of Copyleft, as defined by the <a href="http://www.fsf.org/">Free Software Foundation</a> 
%   (see discussion <a href="http://www.gnu.org/copyleft/copyleft.html">here</a> for example). 
%   As such, it is available free of charge, and may be copied, redistributed, or modified, 
%   as long as any resulting work is distributed under the same terms of Copyleft.


%% Download raw data, and run Classer preprocessing
RAW_DATA_URL = 'http://cns.bu.edu/techlab/classer/datasets/d_Boston.zip';
originalDir = pwd;
cd(tempdir);
try unzip(RAW_DATA_URL);
catch
    e = lasterror;
    if strfind(e.identifier,'urlwriteError')
        error(e.identifier,...
             ['Either there is a problem with your internet connection '...
              'or the boston dataset is no longer available at '...
              'the expected url, %s. Please check the tech lab website '...
              'for updates, http://cns.bu.edu/techlab'],RAW_DATA_URL);
    else
        error(e);
    end;                
end;
unzip();
cd('d_Boston');
!unWrap.bat 
    %unWrap runs classer scripts for scaling and preprocessing data
cd('PubNN');

%% Create strips for use by DefAM_crossValidation
strips = struct([]);
for i = 1:4
    s = dlmread(sprintf('Strip%d.txt',i-1),'',10,0);
    isLabeled = s(:,1)~=0;
    strips(i).labels = int8(s(isLabeled,1)'-1); 
        %No points were labeled 1 ("miss"), so shift classes down 1
    strips(i).inputs = single( s(isLabeled,5:end-1)' ); 
        %Trims leading visualization dims and trailing 0.
end;
cd(tempdir);
rmdir('d_Boston','s');
cd(originalDir);
classNames = {'beach','ocean','ice','river','road','park','residential','industrial'}';
featureNames = {...
    '4. Blue';'5. Green';'6. Red';...
    '7. NIR';'8. Mid IR 1';'9. Thermal IR 1';'10. Thermal IR 2';'11. Mid IR 2';...
    '12. Panchromatic';'13. Conditioned Blue';'14. Cond. Green';'15. Cond. Red';...
    '16. Cond. NIR';'17. Cond. Mid IR 1';'18. Cond. Thermal IR 1';...
    '19. Cond. Thermal IR 2';'20. Cond. Mid IR 2';'21. Cond. Panchromatic';...
    '22. RG Single Opponent';'23. GR Single Opponent';'24. RG Double Opponent';...
    '25. BY Single Opponent';'26. YB Single Opponent';'27. BY Double Opponent';...
    '28. BCS even combined';'29. BCS even combined 0';'30. BCS even combined 1';...
    '31. BCS even combined 2';'32. BCS odd combined';'33. BCS odd combined 0';...
    '34. BCS odd combined 1';'35. BCS odd combined 2';...
    '36. Periodic Texture Even/Odd 0';'37. Periodic Texture Even/Odd 1';...
    '38. Periodic Texture Even/Odd 2';'39. Gray Texture Even 0';...
    '40. Gray Texture Even 1';'41. Gray Texture Even 2'; };
description = ['Ground-truth labeled pixel data from the Boston testbed, ',...
    'based on d_Boston/PubNN/Strip#.txt from Classer. Unlabeled points ',...
    'are removed, and all other labels are shifted down 1, ',...
    'since no ground-truth points are mapped to class 1 ("miss"). The first ',...
    '3 features are removed, as they''re only for visualization, and the last ',...
    'feature is removed, since it is 0 for all points.'];
save('boston_strips_labeled.mat','strips','classNames','featureNames','description');
disp('Boston strips created successfully.');