app-permutation-test.C
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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){
00083
00084 std::vector<float> model = data[0].getNormSal();
00085
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
00093
00094 for (uint ii = 0; ii < RANDITER; ii++){
00095 LINFO("Iteration::%d",ii);
00096
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)
00102 {
00103 std::random_shuffle(rshuffle.begin(),rshuffle.end());
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 }
00119
00120
00121
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
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
00152
00153 (*itsOutFile) << outval << " ";
00154
00155 }
00156 }
00157
00158
00159 else{
00160
00161 std::vector<float> model = data[0].getNormSal();
00162
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
00169
00170 for (uint ii = 0; ii < RANDITER; ii++){
00171 LINFO("Iteration::%d",ii);
00172
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 {
00180 std::random_shuffle(rshuffle.begin(),rshuffle.end());
00181 ldata = halfdata;
00182 }
00183
00184
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
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 }
00216
00217
00218
00219
00220
00221