function [CM, recfiles, storeprob, classindex] = test_ljmNL3 (trainclasses, testclassdata, testclassfile_metadata, ris, scale, Thres)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% [CM,recfiles,storprob] = test_ljmNL3 (cl, indx, ris, scale)
% Date 29 Dec. 2003
% Description: test using averaged local jet features with 3rd non
%   linearity. The classification is done taking the class with maximum
%   likelihood (hypothesis of equally probable classes)
% Use:  trainclasses list of training class names {'PBS' 'OTHER}
%       testclassdata invariant data for test class
%       testclassfile_metadata file name and resolution data to test
%       ris models of training
%       scale nr. of scale in wich invariants are computed
%       CM confusion matrix
%       recfiles junk files
%       storeprob maximum probablity
%       classindex index into training class per what classifier output
%
% Author: Marc'Aurelio Ranzato  
%
% modified doliver@mbari.org - Dec 10, 2004
% modified dcline@mbari.org - July 21, 2005. Added testclassdata and
% testclassfile_metadata - removed indx array - now only tests one data set
% per training class rather than multiple testing and training sets - was
% confusing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% use this function if you have trained your data and now you want to test
% a class (ie a test class) against your training sample
DEBUG = 1;

% add classes from a saved variable removed pat line resolution statistics
resolution = ris.resolution;
NDS = 10;            % nr. of standard deviations 
% that is, don't do the test on resolution

%determine length of testing set ; assume only one test
ncltr = length(trainclasses);

%init variables
CM = zeros(ncltr+1);
mog = ris.mog;
mediatot = ris.mediatot;
FLD = ris.FLD;

% create array of class names, including the Unknown category
recfiles(1,:) = [trainclasses {'UNK'}];
junkclassindex = length(recfiles);

fprintf(1,'TESTING...\n')     

%assign the data from training
for ii=1:size(testclassfile_metadata,1)

    % extract val (invariant data), risol (image resolution), and
    % currentfile name
    val = testclassdata(ii,:);
    risol = testclassfile_metadata{ii,2};
    currentfile = {testclassfile_metadata{ii,1}};

    val = val - mediatot;
    val = (FLD' * val')';

    for jjj= 1:ncltr
        dprobab(jjj) = gmmprob( mog(jjj).mix, val );
    end

    if sum(dprobab) == 0
        Mp = 0;
        estimate = 0;
        storeprob(ii) = Mp;
    else
        prob = dprobab/sum(dprobab);
        Mp = max(prob);

        %modified store the maximum probability for a file
        storeprob(ii) = Mp;
        estimate = find(prob == Mp);
        if(size(estimate,2) > 0)
            estimate = estimate(1);
        else
            estimate = 0;
        end
    end

    maxresol = resolution(estimate,1) + NDS*resolution(estimate,2);
    minresol = risol <= resolution(estimate,1)-NDS*resolution(estimate,2);
    
    %if the risol is out of bounds with estimated class resolution and
    %is less than the minimum probability threshold, then store this in
    %the unknown class
    if  ( estimate == 0 | risol >= maxresol | risol <= minresol  | Mp <= Thres )
        % *modified - update the unknown class if the threshold not met
        % modified - store the file name in unknown column
        CM(junkclassindex) = CM(junkclassindex) + 1;
        recfiles(end+1,junkclassindex) = currentfile;
        classindex(ii) = junkclassindex;
        if(risol >= maxresol )
            fprintf(1, 'image too big %d maxsize %d\n', risol, maxresol);
        elseif (risol <= minresol)
            fprintf(1, 'image too big %d minsize %d\n', risol, minresol);
        elseif (Mp <= Thres)
            fprintf(1, 'image less than min probab %f min %f\n', Mp, Thres);
        else
            fprintf(1, 'image binned into junk\n');
        end
    else
        %modified update the sum of files found in claas and save name of file
        CM(estimate) = CM(estimate) + 1;   % update CM
        recfiles(end+1,estimate) = currentfile;
        classindex(ii) = estimate;
    end
end

end
