File: modifyDistanceMatrix.cc

package info (click to toggle)
gnuift 0.1.14%2Bds-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,632 kB
  • ctags: 2,973
  • sloc: cpp: 15,867; sh: 8,281; ansic: 1,812; perl: 1,007; php: 651; makefile: 483; lisp: 344
file content (190 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/** here there is something to do:
    i would like to have a small program, which takes ALL
    the scores and calculates their average as well as
    their standard deviation. 

    Using these two numbers, scores are then transformed into
    pseudo distances.
*/

#include <iterator>
#include <fstream>
#include <iostream>
#include <cmath>
#include <cassert>
#include <string>
#include <map>
#include <functional>
#include <cstdlib>

using namespace std;

int gCount=0;
double gSum=0;
double gSumOfSquares=0;

void calculateSums(float inFloat){
 gCount       ++;
 gSum         += inFloat;
 gSumOfSquares+= inFloat * inFloat;
}

typedef map<double,int> CMap;

void vectorToMap(float* inBegin,
		 float* inEnd,
		 CMap& outMap){
  int lCount=0;
  for(float* i=inBegin;
      i!=inEnd;
      i++){
    outMap.insert(make_pair(*i,lCount++));
    //    cout << "-" << flush;
  }
}

double rankifyMatrix(float* inMatrix,
		     int inSize){
  /// Take the matrix
  /// make pairs of score and ID 
  for(int lLine=0;
      lLine<inSize;
      lLine++){
    //this map is sorted by rank inascending order
    CMap lMap;
    vectorToMap(inMatrix + lLine*inSize,
		inMatrix + (1+lLine)*inSize,
		lMap);
    //cout << endl;

    int lCount=inSize;
    for(CMap::const_iterator i=lMap.begin();
	i!=lMap.end();
	i++){
      //in the matrix now are the query ranks
      float l= float(--lCount)/inSize;
      if(l>1)
	l=1;
      if(l<0)
	l=0;
      inMatrix[(lLine*inSize)+i->second]=1-l;
    }
  }
}


double rescale(float inFloat){
  double lAverage=(gSum/gCount);
  double lAverageSquare=
    gSumOfSquares/gCount;
  double lSDev=
    sqrt(lAverageSquare
	 -
	 lAverage*lAverage);
  //...so from score to pseudo distance
  double lReturnValue=0.5-((inFloat-lAverage)/lSDev/3);

  if(lReturnValue>.9999)
    lReturnValue=1;
  if(lReturnValue<0.0001)
    lReturnValue=0;

  //  cout << "[" << lReturnValue << "]" <<flush;
  assert((lReturnValue<=1) && (lReturnValue>=0));

  return 
    lReturnValue;
}

//changes sign of matrix

void changeSign(float* inMatrix,
		int inSize){
  inSize=inSize*inSize;

  for(float* p=inMatrix;p!=inMatrix+inSize;p++){
    *p = 1 - *p;
  }
}

//symmetrifies the matrix given by an array
void symmetrify(float* inMatrix,
		int inSize){
  for(int i=0;
      i<inSize;
      i++){
    for(int j=0;
	j<inSize;
	j++){
      float l=min(inMatrix[i*inSize+j],
		  inMatrix[j*inSize+i]);
      inMatrix[i*inSize+j]=l;
      inMatrix[j*inSize+i]=l;
    }

  }
};


main(int argc,char **argv){

  if(argc!=4){
    cout  << "usage: "
	  << argv[0]
	  << " <INFILE> <OUTFILE> <SIZE>"
	  << endl;
    exit(1);
  }
  
  string lInName=argv[1];
  string lOutName=argv[2];
  int lSize=atoi(argv[3]);

  float* lMatrix=new float[lSize*lSize];
  assert(lMatrix);
  {
    ifstream lIn(lInName.c_str());
    
    while(lIn){
      float lBuffer;
      lIn.read((char*)&lBuffer,sizeof(lBuffer));
      calculateSums(lBuffer);
    }
  }
  cout << "until here "
       << flush 
       << endl;
  {
    ifstream lIn(lInName.c_str());
    ofstream lOut(lOutName.c_str());
    
    assert(lOut);

    int lCount=0;
    while(lIn){
      float lBuffer;
      lIn.read((char*)&lBuffer,
	       sizeof(lBuffer));
      lMatrix[lCount++]=lBuffer;
    }

#ifdef VISTEX
     rankifyMatrix(lMatrix,
		   lSize);
#endif

     //symmetrify the matrix
     symmetrify(lMatrix,
		lSize);
     
     //get from score to distance
     changeSign(lMatrix,
		lSize);


    lOut.write((char*)lMatrix,
	       sizeof(float)
	       *(lSize*lSize));
      
  }
}