openModeller  Version 1.5.0
PreChiSquare.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/Sampler.hh>
29 #include <openmodeller/Log.hh>
31 
32 #include <stdio.h>
33 #include <math.h>
34 
36 {
37 }
38 
40 {
41 }
42 
43 bool
45 {
46  SamplerPtr samplerPtr;
47  if( ! parameters.retrieve( "Sampler", samplerPtr ) )
48  {
49  Log::instance()->error( "Missing parameter: Sampler. \n" );
50  return false;
51  }
52 
53  if ( ! samplerPtr->getEnvironment() )
54  {
55  std::string msg = "Sampler has no environment.\n";
56 
57  Log::instance()->error( msg.c_str() );
58 
59  throw InvalidParameterException( msg );
60  }
61  return true;
62 }
63 
64 bool
66 {
67  size_t layer1, layer2;
68 
69  init();
70 
71  for( layer1 = 0; layer1 < num_layers; layer1++ )
72  {
73  statistic1.push_back(0);
74  statistic2.push_back(0);
75  }
76 
77  for( layer1 = 0; layer1 < num_layers; layer1++ )
78  for(layer2 = layer1+1; layer2 < num_layers; layer2++)
79  {
80  setMeasured(layer1, layer2);
81  setExpected();
82  setChicell();
83  setStatistic(layer1, layer2);
84  }
85 
86  SamplerPtr samplerPtr;
87  params_.retrieve( "Sampler", samplerPtr );
88 
89  size_t aux;
90  for( unsigned int ind = 0 ; ind < num_layers ; ind++ )
91  {
92  string layer_id = samplerPtr->getEnvironment()->getLayerPath(ind);
93 
94  PreParameters result;
95 
96  aux = statistic1[ind] + statistic2[ind];
97 
98  result.store( "number of correlated layers", (int)aux );
99 
100  result_by_layer_[layer_id] = result;
101  }
102 
103  return true;
104 }
105 
106 void
108 {
109  info["Sampler"] = "samplerPtr";
110 }
111 
112 void
114 {
115 
116 }
117 
118 void
120 {
121  info["number of correlated layers"] = "int";
122 }
123 
124 bool
126 {
127  SamplerPtr samplerPtr;
128  params_.retrieve( "Sampler", samplerPtr );
129 
130  setNlayers(samplerPtr);
131 
132  my_presences = samplerPtr->getPresences();
133 
134  setNpoints();
135  setNclass();
136  setMinimum();
137  setDelta();
138 
139  if (nclass > classLimit)
140  {
141  Log::instance()->error( "ChiSquare: measured, expected, chicell: number of class > %d\n", classLimit );
142  return false;
143  }
144 
145  return true;
146 }
147 
148 size_t
150 {
151  return num_points;
152 }
153 
154 void
156 {
157  num_points = my_presences->numOccurrences();
158 }
159 
160 size_t
162 {
163  return num_layers;
164 }
165 
166 void
168 {
169  num_layers = samplerPtr->numIndependent();
170 
171  if ( num_layers < 2 )
172  {
173  std::string msg = "chisquare needs at least 2 layers.\n";
174 
175  Log::instance()->error( msg.c_str() );
176 
177  throw InvalidParameterException( msg );
178  }
179 }
180 
181 size_t
183 {
184  return nclass;
185 }
186 
187 void
189 {
190  nclass = (size_t)floor(sqrt((double)(num_points)));
191 }
192 
193 void
195 {
198 
199  Sample sample = (*it)->environment();
200  minimum = sample;
201  ++it;
202  while ( it != last )
203  {
204  Sample const& sample = (*it)->environment();
205  minimum &= sample;
206  ++it;
207  }
208 }
209 
210 Sample
212 {
213  return minimum;
214 }
215 
216 void
218 {
221 
222  Sample sample = (*it)->environment();
223  delta = sample;
224  ++it;
225  while ( it != last )
226  {
227  Sample const& sample = (*it)->environment();
228  delta |= sample;
229  ++it;
230  }
231 
232  delta -= minimum;
233  delta /= (Scalar)nclass;
234 }
235 
236 Sample
238 {
239  return delta;
240 }
241 
242 void
243 PreChiSquare::setMeasured(size_t layer1, size_t layer2)
244 {
247 
248  size_t row, col;
249 
250  for (size_t i = 0; i < classLimit; i++)
251  for (size_t j = 0; j < classLimit; j++)
252  measured[i][j] = 0.0;
253 
254  while ( it != last )
255  {
256  Sample const& sample = (*it)->environment();
257 
258  row = (size_t)floor( ( sample[layer1] - minimum[layer1] ) / delta[layer1] );
259  if (row == nclass)
260  row--;
261  col = (size_t)floor( ( sample[layer2] - minimum[layer2] ) / delta[layer2] );
262  if (col == nclass)
263  col--;
264  measured[row][col] += 1.0;
265 
266  ++it;
267  }
268 }
269 void
271 {
272  size_t i, j;
273  Scalar col_sum[classLimit], row_sum[classLimit], sum;
274 
275  for (i = 0; i < nclass; i++)
276  {
277  col_sum[i] = measured[i][0];
278  for ( j = 1; j < nclass; j++)
279  col_sum[i] += measured[i][j];
280  }
281 
282  for (j = 0; j < nclass; j++)
283  {
284  row_sum[j] = measured[0][j];
285  for ( i = 1; i < nclass; i++)
286  row_sum[j] += measured[i][j];
287  }
288 
289  sum = col_sum[0];
290  for (i = 1; i < nclass; i++)
291  sum += col_sum[i];
292 
293  for (i = 0; i < nclass; i++)
294  for (j = 0; j < nclass; j++)
295  expected[i][j] = col_sum[i] * row_sum[j] / sum;
296 }
297 void
299 {
300  size_t i, j;
301  Scalar aux;
302 
303  for (i = 0; i < nclass; i++)
304  for (j = 0; j < nclass; j++)
305  if (expected[i][j] == 0.0)
306  chicell[i][j] = 0.0;
307  else
308  {
309  aux = expected[i][j] - measured[i][j];
310  chicell[i][j] = (aux * aux) / expected[i][j];
311  }
312 }
313 
314 void
315 PreChiSquare::setStatistic(size_t layer1, size_t layer2)
316 {
317  size_t i, j;
318  Scalar chi=0.0;
319  Scalar delimita[classLimit] = {0, 3.8415, 5.9915, 7.8147, 9.4877, 11.0705, 12.5916, 14.0671, 15.5073, 16.9190, 18.3070, 19.6751, 21.0261, 22.3620, 23.6848, 24.9958 };
320 
321  for (i = 0; i < nclass; i++)
322  for (j = 0; j < nclass; j++)
323  chi += chicell[i][j];
324 
325  if (chi < delimita[nclass - 1])
326  {
327  statistic1[layer1] += 1;
328  statistic2[layer2] += 1;
329  }
330 }
void setExpected()
bool checkParameters(const PreParameters &parameters) const
std::vector< OccurrencePtr >::iterator iterator
Definition: Occurrences.hh:86
double Scalar
Type of map values.
Definition: om_defs.hh:39
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
size_t getNclass()
size_t getNpoints()
std::map< string, string > stringMap
Definition: PreAlgorithm.hh:91
std::map< string, PreParameters > result_by_layer_
Scalar chicell[classLimit][classLimit]
Definition: PreChiSquare.hh:58
void setMeasured(size_t layer1, size_t layer2)
PreParameters params_
Scalar measured[classLimit][classLimit]
Definition: PreChiSquare.hh:56
std::vector< size_t > statistic1
Definition: PreChiSquare.hh:59
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
bool retrieve(const PreMultiContainerKeyT &obj_key, ObjectT &obj_reference) const
void getLayersetResultSpec(stringMap &info)
void store(const PreMultiContainerKeyT &obj_key, const ObjectT &obj_reference)
void setStatistic(size_t layer1, size_t layer2)
size_t num_layers
Definition: PreChiSquare.hh:51
Sample getMinimum()
#define classLimit
Definition: PreChiSquare.hh:38
std::vector< size_t > statistic2
Definition: PreChiSquare.hh:60
size_t num_points
Definition: PreChiSquare.hh:50
OccurrencesPtr my_presences
Definition: PreChiSquare.hh:55
Sample getDelta()
Sample minimum
Definition: PreChiSquare.hh:53
Scalar expected[classLimit][classLimit]
Definition: PreChiSquare.hh:57
void setNlayers(SamplerPtr samplerPtr)
AlgParamMetadata parameters[NUM_PARAM]
Definition: garp.cpp:55
size_t getNlayers()
bool runImplementation()
void getLayerResultSpec(stringMap &info)
Definition: Sample.hh:25
void getAcceptedParameters(stringMap &info)