% create a MIL dataset from the original WAV-files, segmentation of the
% spectrograms, and computation of features on the segmented regions
%
% This is the new 19 species dataset from the MLSP competition

% some settings:
windowlen = 512;
fmax = 256;
intens_thr = 0.8;   % remove 80% of the signal??
f_min = 2000;       % frequency threshold (everything below is removed)
% blurring of the spectrogram:
G = fspecial('gaussian',[5 5],2);

% load the 'meta' data like labels and filenames
%dpath = '../birds_mlsp2013/mlsp_contest_dataset/essential_data/';
dpath = '/data/birds_mlsp2013/mlsp_contest_dataset2/essential_data';
% load the filenames
fid = fopen(fullfile(dpath,'rec_id2filename.txt'));
data = textscan(fid,'%n%s','headerlines',1);
fclose(fid);
bagid = data{1};
names = data{2};
% next load the labels:
fid = fopen(fullfile(dpath,'rec_labels_test_hidden.txt'));
data = textscan(fid,'%n%s','headerlines',1);
bagid2 = data{1};
labstr = data{2};
% load the indices for the training and test objects:
N = length(bagid2);
CVfile = fopen(fullfile(dpath,'CVfolds_2.txt'));
   CVdata = textscan(CVfile, '%f,%f', N, 'headerlines',1);
fclose(CVfile);
bagid3 = CVdata{1};
Itst = CVdata{2};

% some checking 
if any(bagid~=bagid2)
	error('Bagid''s do not match.');
end

% run over the files, and get the features:
B = size(bagid,1);
x = cell(B,1);
baglab = zeros(B,13);
instlab = '';
bagid = [];
for i=1:B
    i
   
	%load the signal;
	[signal,fs] = wavread(fullfile(dpath,'src_wavs',names{i}(2:end)));
	[S,f,t] = spectrogram(signal,windowlen,windowlen/2,fmax,fs);
	% smooth and threshold the spectrogram:
	I = imfilter(abs(S),G,'same');
	mask = (I>dd_threshold(I(:),intens_thr));
	mask(f<f_min) = 0;
	% find interesting regions:
	props = regionprops(bwlabel(mask),abs(S));
	bloblab = bwlabel(mask);
	Nseg = max(unique(bloblab));

	% run over blobs:
	absim = abs(S);
	realim = real(S);
    imagim = imag(S);
    
    maskfeats = nan(Nseg,3);
    absfeats = nan(Nseg, 7);
    realfeats = nan(Nseg,7);
    imagfeats = nan(Nseg,7);
        
	for j=1:Nseg
   
        
		ix = (bloblab==j);
        
      % compute/add some blob-properties:
	  %	thisx(j,:) = [props(j).Area, props(j).Centroid, props(j).BoundingBox];
      
        pixtotal = sum(sum(ix));
        pixheight = max(sum(ix,1));
        pixwidth = max(sum(ix,2));

        maskfeats(j,:) = [pixtotal pixheight pixwidth];


        seg = absim(ix);
        absfeats(j,1) = mean(seg); 
        absfeats(j,2) = std(seg); 

        absfeats(j,3) = quantile(seg(:),0);
        absfeats(j,4) = quantile(seg(:),0.25);
        absfeats(j,5) = quantile(seg(:),0.5);
        absfeats(j,6) = quantile(seg(:),0.75);
        absfeats(j,7) = quantile(seg(:),1);
        
           seg = realim(ix);
        realfeats(j,1) = mean(seg); 
        realfeats(j,2) = std(seg); 

        realfeats(j,3) = quantile(seg(:),0);
        realfeats(j,4) = quantile(seg(:),0.25);
        realfeats(j,5) = quantile(seg(:),0.5);
        realfeats(j,6) = quantile(seg(:),0.75);
        realfeats(j,7) = quantile(seg(:),1);
        
           seg = imagim(ix);
        imagfeats(j,1) = mean(seg); 
        imagfeats(j,2) = std(seg); 

        imagfeats(j,3) = quantile(seg(:),0);
        imagfeats(j,4) = quantile(seg(:),0.25);
        imagfeats(j,5) = quantile(seg(:),0.5);
        imagfeats(j,6) = quantile(seg(:),0.75);
        imagfeats(j,7) = quantile(seg(:),1);
          
           seg = absim.*ix;
        momentfeat = [moments(seg,[1;0],[0;1],1,0) ...
           moments(seg,[2;1;0],[0;1;2],1,0) ...
           moments(seg,[2,1,0],[0,1,2],1,1) ...
           hu_moments(seg) zernike_moments(seg)];
        
		% don't forget:
		bagid(end+1) = i;

        
	end
	x{i} = [maskfeats absfeats realfeats imagfeats momentfeat];

	%Get the labels right for the training bags:
    
   if ~Itst(i)
      eval(['baglab(i,[',labstr{i}(2:end),']+1)=1;']);
   end
end

% create a dataset
a = genmil(x);
% add the labels one by one:
ll = [...
'BRCR-Brown Creeper            ';
'PAWR-Pacific Wren             ';
'PSFL-Pacific-slope Flycatcher ';
'RBNU-Red-breasted Nuthatch    ';
'DEJU-Dark-eyed Junco          ';
'OSFL-Olive-sided Flycatcher   ';
'HETH-Hermit Thrush            ';
'CBCH-Chestnut-backed Chickadee';
'VATH-Varied Thrush            ';
'HEWA-Hermit Warbler           ';
'SWTH-Swainsons Thrush         ';
'HAFL-Hammonds Flycatcher      ';
'WETA-Western Tanager          ';
'BHGB-Black-headed Grosbeak    ';
'GCKI-Golden Crowned Kinglet   ';
'WAVI-Warbling Vireo           ';
'MGWA-MacGillivrays Warbler    ';
'STJA-Stellars Jay             ';
'CONI-Common Nighthawk         '];


for i=1:size(baglab,2)
   I = ismember(bagid,find(baglab(:,i)));
   a = addlabels(a,genmillabels(I',1),ll(i,:));
end
% set it to the first bird:
a = changelablist(a,2);
thisll = getlablistnames(a);
a = setname(a,strtrim(thisll(curlablist(a),:)));

J = Itst(bagid);
x = a(~J,:);
z = a(logical(J),:);
save('birds20130709.mat', 'a', 'x', 'z', 'Itst', 'J');
