app-permutation-test.C

00001 /*!@file AppMedia/app-permutation-test.C Create small images from originals */
00002 
00003 //////////////////////////////////////////////////////////////////////////
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 //////////////////////////////////////////////////////////////////////////
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 //////////////////////////////////////////////////////////////////////////
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 //////////////////////////////////////////////////////////////////////////
00032 //
00033 // Primary maintainer for this file: David Berg <dberg@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppEye/app-permutation-test.C $
00035 
00036 #include "Util/MathFunctions.H"
00037 #include "Psycho/EyesalData.H"
00038 #include "Image/Image.H"
00039 #include <vector>
00040 #include <fstream>
00041 #include <algorithm>
00042 #include <numeric>
00043 
00044 #include <ctime>
00045 #include <cstdlib>
00046 
00047 #include "Util/StringConversions.H"
00048 #include "Util/StringUtil.H"
00049 
00050 #define RANDITER 10000
00051 
00052 class iotaGen
00053 {
00054     public:
00055     iotaGen (int iv) : current(iv) {}
00056     int operator ()() {return current++;}
00057     private:
00058     int current;
00059 };
00060 
00061 
00062 //input should be three arguments to eyesal text files  and an output file
00063 int main(int argc, char** argv)
00064 {
00065 
00066     if (argc < 3)
00067         LFATAL("At least an eyesal file and an output file are needed.");
00068     else if (argc > 4)
00069         LFATAL("Right now, I only handle two groups and an output file");
00070     uint dc = argc-2;
00071     EyesalData data[dc];
00072     for (size_t ii = 0;ii < dc;++ii)
00073         data[ii].setFile(argv[ii+1]);
00074 
00075     srand(time(0));
00076 
00077     //open an output file
00078     std::ofstream *itsOutFile = new std::ofstream(argv[argc-1]);
00079     if (itsOutFile->is_open() == false)
00080         LFATAL("Cannot open '%s' for reading",argv[argc-1]);
00081 
00082     if (dc > 1){//we want to compare two files
00083             //lets create some giant data vectors
00084         std::vector<float> model = data[0].getNormSal();
00085         //position of our original data;
00086         uint splitpos = model.size();
00087         std::vector<float> temp = data[1].getNormSal();
00088         model.insert(model.end(),temp.begin(),temp.end());
00089         std::vector< std::vector<float> > rand = data[0].getAllNormRandT();
00090         std::vector< std::vector<float> > tempr =  data[1].getAllNormRandT();
00091         rand.insert(rand.end(),tempr.begin(),tempr.end());
00092             //OK now, randomly sample from the data and loop our
00093             //computations RANDITER times
00094         for (uint ii = 0; ii < RANDITER; ii++){
00095             LINFO("Iteration::%d",ii);
00096                 //suffle the data
00097             std::vector<float> modelr(model.size());
00098             std::vector< std::vector<float> > randr(rand[0].size());
00099             std::vector<uint> rshuffle(model.size());
00100             std::generate(rshuffle.begin(),rshuffle.end(),iotaGen(0));
00101             if (ii < RANDITER-1) //calculate original groups last
00102             {
00103                 std::random_shuffle(rshuffle.begin(),rshuffle.end());
00104 
00105                 //for testing purposes
00106                 /*std::ifstream *itsinFile = new std::ifstream("order.test");
00107                 if (itsinFile->is_open() == false)
00108                     PLFATAL("Cannot open '%s'", "adadf");
00109                 std::string line; int linenum = -1;
00110                 while (getline(*itsinFile, line))
00111                 {
00112                         // one more line that we have read:
00113                     ++linenum;
00114                     rshuffle[linenum] = fromStr<int>(line)-1;
00115                 }
00116                 */
00117                 //end test
00118             }
00119 
00120 
00121                 //loop through the data and rearange
00122             for (size_t kk = 0; kk < rand[0].size();++kk)
00123             {
00124                 randr[kk].resize(model.size());
00125                 for (size_t jj = 0; jj < model.size();++jj)
00126                 {
00127                     if (kk == 0)
00128                         modelr[jj] = model[rshuffle[jj]];
00129                     randr[kk][jj] = rand[rshuffle[jj]][kk];
00130                 }
00131             }
00132 
00133             //loop through the random sampels
00134             double meanauc1 = 0;
00135             double meanauc2 = 0;
00136             for (size_t jj = 0; jj < randr.size();++jj){
00137                 const float *tm1 = &modelr[0];
00138                 const float *tr1 = &randr[jj][0];
00139                 float auc1 = AUC(tm1,tr1,splitpos,splitpos);
00140 
00141                 const float *tm2 = &modelr[splitpos];
00142                 const float *tr2 = &randr[jj][splitpos];
00143                 float auc2 = AUC(tm2,tr2,modelr.size()-splitpos,
00144                                  modelr.size()-splitpos);
00145                 meanauc1 += auc1;
00146                 meanauc2 += auc2;
00147             }
00148             meanauc1 = meanauc1/randr.size();
00149             meanauc2 = meanauc2/randr.size();
00150             double outval = (meanauc1-meanauc2);
00151             //if (outval < 0)
00152               //outval *= -1;
00153             (*itsOutFile) << outval << " ";
00154 
00155         }//end RITER
00156     }//end if dc > 1
00157 
00158 //FOR ONLY ONE FILE, TO GET AN ESTIMATE OF CONFIDENCE
00159     else{
00160    //lets create some giant data vectors
00161         std::vector<float> model = data[0].getNormSal();
00162         //position of our original data;
00163         uint splitpos = model.size();
00164         uint halfdata = (uint)floor(splitpos/2);
00165 
00166         std::vector< std::vector<float> > rand = data[0].getAllNormRandT();
00167 
00168             //OK now, randomly sample from the data and loop our
00169             //computations RANDITER times
00170         for (uint ii = 0; ii < RANDITER; ii++){
00171             LINFO("Iteration::%d",ii);
00172                 //suffle the data
00173             std::vector<float> modelr(model.size());
00174             std::vector< std::vector<float> > randr(rand[0].size());
00175             std::vector<uint> rshuffle(model.size());
00176             std::generate(rshuffle.begin(),rshuffle.end(),iotaGen(0));
00177             uint ldata = model.size();
00178             if (ii < RANDITER-1)
00179             {//calculate original groups last
00180                 std::random_shuffle(rshuffle.begin(),rshuffle.end());
00181                 ldata = halfdata;
00182             }
00183 
00184                 //loop through the data and rearange
00185             for (size_t kk = 0; kk < rand[0].size();++kk)
00186             {
00187                 randr[kk].resize(ldata);
00188                 for (size_t jj = 0; jj < ldata;++jj)
00189                 {
00190                     if (kk == 0)
00191                         modelr[jj] = model[rshuffle[jj]];
00192                     randr[kk][jj] = rand[rshuffle[jj]][kk];
00193                 }
00194             }
00195 
00196             //loop through the random sampels
00197             double meanauc = 0;
00198 
00199 
00200             for (size_t jj = 0; jj < randr.size();++jj){
00201                 const float *tm1 = &modelr[0];
00202                 const float *tr1 = &randr[jj][0];
00203                 float auc = AUC(tm1,tr1,ldata,ldata);
00204                 meanauc += auc;
00205             }
00206             meanauc = meanauc/randr.size();
00207             (*itsOutFile) << meanauc << " ";
00208 
00209         }
00210     }
00211 
00212 
00213 
00214 itsOutFile->close();
00215 }//END MAIN
00216 
00217 // ######################################################################
00218 /* So things look consistent in everyone's emacs... */
00219 /* Local Variables: */
00220 /* indent-tabs-mode: nil */
00221 /* End: */
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3