/** 
* @file testImageNetBinary.cpp
* @brief perform ImageNet tests with binary tasks for OCC
* @author Alexander Lütz
* @date 23-05-2012 (dd-mm-yyyy)

*/
#include <iostream>

#include "core/basics/Config.h"

#ifdef NICE_USELIB_MATIO

#include "vislearning/cbaselib/ClassificationResults.h"
#include "vislearning/baselib/ProgressBar.h"

#include "core/matlabAccess/MatFileIO.h"
#include "vislearning/matlabAccessHighLevel/ImageNetData.h"

#include "vislearning/classifier/kernelclassifier/KCGPOneClass.h"
#include "vislearning/classifier/kernelclassifier/KCGPApproxOneClass.h"

#include "vislearning/math/kernels/KernelData.h"
#include "vislearning/math/kernels/Kernel.h"
#include "vislearning/math/kernels/KernelRBF.h"
#include "vislearning/math/kernels/KernelExp.h"

// #include "fast-hik/tools.h"


using namespace std;
using namespace NICE;
using namespace OBJREC;


/** 
    test the basic functionality of fast-hik hyperparameter optimization 
*/
int main (int argc, char **argv)
{   
  std::set_terminate(__gnu_cxx::__verbose_terminate_handler);

  Config conf ( argc, argv );
  string resultsfile = conf.gS("main", "results", "results.txt" );
  int positiveClass = conf.gI("main", "positive_class");

  std::cerr << "Positive class is " << positiveClass << std::endl;
  
  sparse_t data;
  NICE::Vector y;
  
  std::cerr << "Reading ImageNet data ..." << std::endl;
  bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  string imageNetPath;
  if (imageNetLocal)
    imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  else
    imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";

  ImageNetData imageNet ( imageNetPath + "demo/" );

//   imageNet.getBatchData ( data, y, "train", "training" );
  LabeledSetVector train;
  imageNet.loadDataAsLabeledSetVector( train );
  
  //set up the kernel function
  double rbf_sigma = conf.gD("main", "rbf_sigma", -2.0 );
  KernelRBF kernelFunction ( rbf_sigma, 0.0 );
    //KernelExp kernelFunction ( rbf_sigma, 0.0, 0.0 );

  //set up our OC-classifier
  string classifierName = conf.gS("main", "classifier", "KCGPApproxOneClass");
  
  KernelClassifier *classifier;
  if(strcmp("KCGPApproxOneClass",classifierName.c_str())==0)
  {
    classifier = new KCGPApproxOneClass ( &conf, &kernelFunction );
  }
  else if (strcmp("KCGPOneClass",classifierName.c_str())==0) {
    classifier = new KCGPOneClass ( &conf, &kernelFunction );
  }
  else{ //default
    classifier = new KCGPApproxOneClass ( &conf, &kernelFunction );
  }
  //and perform the training
  classifier->teach( train );    

//   uint n = y.size();
//   
//   set<int> positives;
//   set<int> negatives;
// 
//   map< int, set<int> > mysets;
//   for ( uint i = 0 ; i < n; i++ )
//     mysets[ y[i] ].insert ( i );
// 
//   if ( mysets[ positiveClass ].size() == 0 ) 
//     fthrow(Exception, "Class " << positiveClass << " is not available.");
// 
//   // add our positive examples
//   for ( set<int>::const_iterator i = mysets[positiveClass].begin(); i != mysets[positiveClass].end(); i++ )
//     positives.insert ( *i );
// 
//   int Nneg = conf.gI("main", "nneg", 1 );
//   for ( map<int, set<int> >::const_iterator k = mysets.begin(); k != mysets.end(); k++ )
//   {
//     int classno = k->first;
//     if ( classno == positiveClass )
//       continue;
//     const set<int> & s = k->second;
//     uint ind = 0;
//     for ( set<int>::const_iterator i = s.begin(); (i != s.end() && ind < Nneg); i++,ind++  )
//       negatives.insert ( *i );
//   }
//   std::cerr << "Number of positive examples: " << positives.size() << std::endl;
//   std::cerr << "Number of negative examples: " << negatives.size() << std::endl;

  // ------------------------------ TESTING ------------------------------
 
  std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  imageNet.preloadData ( "val", "testing" );
  imageNet.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
 
  ClassificationResults results;
  std::cerr << "Classification step ... with " << imageNet.getNumPreloadedExamples() << " examples" << std::endl;
  ProgressBar pb;
  for ( uint i = 0 ; i < (uint)imageNet.getNumPreloadedExamples(); i++ )
  {
    pb.update ( imageNet.getNumPreloadedExamples() );

    const SparseVector & svec = imageNet.getPreloadedExample ( i );
    NICE::Vector vec;
    svec.convertToVectorT( vec );

    // classification step
    ClassificationResult r = classifier->classify ( vec );
    
    // set ground truth label
    r.classno_groundtruth = (((int)imageNet.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
    results.push_back ( r );
  }

  std::cerr << "Writing results to " << resultsfile << std::endl;
  results.writeWEKA ( resultsfile, 0 );
  double perfvalue = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );

  std::cerr << "Performance: " << perfvalue << std::endl;
  
  //don't waste memory
  delete classifier;
  
  return 0;
}
#else
int main (int argc, char **argv)
{
  std::cerr << "MatIO library is missing in your system - this program will have no effect. " << std::endl;  
}

#endif