openModeller  Version 1.5.0
csm.cpp
Go to the documentation of this file.
1 //
2 // Csm
3 //
4 // Description:
5 //
6 //
7 // Author: CRIA <t.sutton@reading.ac.uk>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
13 
14 #include <string.h>
15 #include <cassert>
16 #include "csm.hh"
17 
18 #include <gsl/gsl_statistics_double.h>
19 #include <gsl/gsl_multifit_nlin.h> //remove this when we have proper covar matrix
20 #include <gsl/gsl_math.h>
21 #include <gsl/gsl_eigen.h>
22 #include <gsl/gsl_sf_gamma.h>
23 //for chi function gsl_ran_chisq_pdf
24 #include <gsl/gsl_randist.h>
25 //for gsl_cdf_chisq_Q
26 #include <gsl/gsl_cdf.h>
27 
28 #include <math.h>
29 
30 #ifdef MSVC
31 #include <float.h>
32 #define isnan _isnan
33 #endif
34 
35 #if defined(__APPLE__) && (__APPLE_CC__ < 4000)
36 extern "C"
37 {
38  int isnan(double);
39 }
40 #endif
41 
42 /****************************************************************/
43 /********************** Algorithm's Metadata ********************/
44 
45 #define NUM_PARAM 0
47 
48 /****************************************************************/
49 /****************************** Csm *****************************/
50 
51 
52 
58  AlgorithmImpl( metadata )
59 {
60  _initialized = 0;
62  if (parameters != 0)
63  {
64  std::cout << "Error __LINE__, __FILE__ : this is an abstract class it should have no params" << std::endl;
65  }
66 }
67 
68 
71 {
72  if ( _initialized )
73  {
74  gsl_matrix_free (_gsl_environment_matrix);
75  gsl_matrix_free (_gsl_covariance_matrix);
76  gsl_vector_free (_gsl_avg_vector);
77  gsl_vector_free (_gsl_stddev_vector);
78  gsl_vector_free (_gsl_eigenvalue_vector);
79  gsl_matrix_free (_gsl_eigenvector_matrix);
80  }
81 }
82 
83 
84 //
85 // Methods used to build the model
86 //
87 
88 
89 
90 
99 {
100  _initialized = 1;
101 
102  Log::instance()->debug( "Base CSM class initializing\n" );
103 
104  //set the class member that holds the number of environmental variables
105  //we subtract 1 because the first column contains the specimen count
106  _layer_count = _samp->numIndependent();
107  //set the class member that holds the number of occurences
108  _localityCount = _samp->numPresence();
109  Log::instance()->debug( "Checking more than 1 samples exist\n" );
110  if (_localityCount<2)
111  {
112  Log::instance()->warn( "CSM needs at least two occurrence points..aborting...\n" );
113  _initialized = 0;
114  return 0;
115  }
116 
117  //convert the sampler to a matrix and store in the local gsl_matrix
118  Log::instance()->debug( "Converting samples to GSL_Matrix\n" );
119  if (!SamplerToMatrix())
120  {
121  Log::instance()->warn( "All occurences are outside the masked area!\n" );
122  _initialized = 0;
123  return 0;
124  }
125  //show what we have calculated so far....
126  //displayMatrix(_gsl_environment_matrix,"Environemntal Layer Samples");
127  bool myFlag = csm1();
128  if (myFlag)
129  {
130  Log::instance()->debug( "Model initialization completed ok!\n" );
131  }
132  else
133  {
134  Log::instance()->warn( "Model initialization failed!\n" );
135  }
136  return myFlag;
137 }
143 {
144  if (_localityCount < 1)
145  {
146  return 0;
147  }
148 
149  OccurrencesImpl::const_iterator pit = _samp->getPresences()->begin();
150  OccurrencesImpl::const_iterator fin = _samp->getPresences()->end();
151 
152  // Allocate the gsl matrix to store environment data at each locality
154  // now populate the gsl matrix from the sample data
155  for (int i=0; pit != fin; ++pit, ++i)
156  {
157  for (int j=0;j<_layer_count;j++)
158  {
159  //we add one to j in order to omit the specimen count column
160  float myCellValue = (float)(*pit)->environment()[j];
161  gsl_matrix_set (_gsl_environment_matrix,i,j,myCellValue);
162  }
163  }
164  Log::instance()->debug( "Csm::SampleToMatrix: x: %i y: %i\n",_layer_count,_localityCount );
165  //for debugging - write matrix to file
166  //FILE * myFile = fopen("/tmp/csm_debug_sample_matrix.dat","w");
167  //gsl_matrix_fwrite(myFile, _gsl_environment_matrix);
168  //fclose(myFile);
169  return 1;
170 }
171 
172 
173 
175 int Csm::calculateMeanAndSd(gsl_matrix * theMatrix,
176  gsl_vector * theMeanVector,
177  gsl_vector * theStdDevVector)
178 {
179 #ifndef WIN32
180  assert (theMatrix != 0);
181  assert (theMeanVector !=0);
182  assert (theStdDevVector !=0);
183 #endif
184  //Initialise the vector to hold the mean of each column
185  gsl_vector_set_zero(theMeanVector);
186 
187  //Initialise the vector to hold the stddev of each column
188  gsl_vector_set_zero(theStdDevVector);
189 
190  //Log::instance()->debug( "Memory location of theMeanVector is %X\n", &theMeanVector );
191  //Log::instance()->debug( "Memory location of gsl_vector_set_zero is %X\n", &gsl_vector_set_zero );
192  //Log::instance()->debug( "Memory location of _gsl_avg_vector is %X\n", &_gsl_avg_vector );
193  //Log::instance()->debug( "Memory location of _gsl_stddev_vector is %X\n", &_gsl_stddev_vector );
194 
195  //calculate the mean and stddev of each column
196  for (int j = 0; j < _layer_count; j++)
197  {
198  //get the current column from the array as a vector
199  gsl_vector_view myColumn = gsl_matrix_column (theMatrix, j);
200  //calculate the average for the column ...
201  double myAverage = gsl_stats_mean (myColumn.vector.data, myColumn.vector.stride, myColumn.vector.size);
202  // ...and assign it to the jth element in the column means vector
203  gsl_vector_set (theMeanVector,j,myAverage);
204  //calculate the stddev for the column and ...
205  double myStdDev = gsl_stats_sd (myColumn.vector.data, myColumn.vector.stride, myColumn.vector.size);
206  // ...and assign it to the jth element in the column stddev vector
207  gsl_vector_set (theStdDevVector,j,myStdDev);
208  }
209  //displayVector(theMeanVector,"Average vector - theMeanVector");
210  //displayVector(theStdDevVector,"Standard Deviation vector - theStdDevVector");
211  //displayVector(_gsl_avg_vector,"Average vector - _gsl_avg_vector");
212  //displayVector(_gsl_stddev_vector,"Standard Deviation vector - _gsl_stddev_vector");
213 
214  return 0;
215 }
216 
218 {
219  //
220  //Subtract the column mean from every value in each column
221  //Divide each resultant column value by the stddev for that column
222  //Note that we are walking the matrix column wise
223  //
224  Log::instance()->debug( "Centering data\n" );
225  for (int j=0;j<_layer_count;j++)
226  {
227  //get the stddev and mean for this column
228  double myAverage = gsl_vector_get (_gsl_avg_vector,j);
229  double myStdDev = gsl_vector_get (_gsl_stddev_vector,j);
230 
231  for (int i=0;i<_localityCount;++i)
232  {
233  double myDouble = gsl_matrix_get (_gsl_environment_matrix,i,j);
234  if (myStdDev > 0)
235  {
236  myDouble = (myDouble-myAverage)/myStdDev;
237  }
238  else
239  {
240  myDouble=0;
241  }
242  //update the gsl_matrix with the new value
243  gsl_matrix_set(_gsl_environment_matrix,i,j,myDouble);
244  }
245  }
246 
247  return 0;
248 }
249 
250 
256 {
257  _done=1;
258  return 1;
259 }
260 
261 
268 int Csm::done() const
269 {
270  return _done;
271 }
272 
273 
274 //
275 // Methods used to project the model
276 //
277 
278 
283 Scalar Csm::getValue( const Sample& x ) const
284 {
285 
286  float myFloat;
287  //bool myAllAreZeroFlag=true;
288  //first thing we do is convert the oM primitive env value array to a gsl matrix
289  //with only one row so we can do matrix multplication with it
290  gsl_matrix * tmp_gsl_matrix = gsl_matrix_alloc (1,_layer_count);
291  gsl_matrix * tmp_raw_gsl_matrix = gsl_matrix_alloc (1,_layer_count);
292  for (signed int i = 0;i<_layer_count;++i)
293  {
294  myFloat = static_cast<float>(x[i]);
295  gsl_matrix_set (tmp_raw_gsl_matrix,0,i,myFloat);
296 
297  //if (myFloat!=0)
298  //{
299  // myAllAreZeroFlag=false;
300  //}
301  //Log::instance()->debug( "myFloat = %f\n", myFloat );
302  //get the stddev and mean for this column
303  float myAverage = (float)gsl_vector_get (_gsl_avg_vector,i);
304  float myStdDev = (float)gsl_vector_get (_gsl_stddev_vector,i);
305  //subtract the mean from the value then divide by the standard deviation
306  if (myStdDev > 0)
307  {
308  myFloat = (myFloat-myAverage)/myStdDev;
309  }
310  else
311  {
312  myFloat=myFloat-myAverage;
313  }
314  //assign the result to our vector
315  gsl_matrix_set (tmp_gsl_matrix,0,i,myFloat);
316  //Log::instance()->debug( "myFloat = %f\n", myFloat );
317  }
318  //Log::instance()->debug( " ---- end of scalar \n");
319  displayMatrix(tmp_raw_gsl_matrix,"Voxel passed to getValue",false);
320  displayMatrix(tmp_gsl_matrix,"Voxel passed to getValue -Mean / stddev");
321  displayVector(_gsl_avg_vector,"Averages");
323  //if (myAllAreZeroFlag) {return 0;}
324 
325 
326  gsl_matrix * z = product(tmp_gsl_matrix, _gsl_eigenvector_matrix);
327  displayMatrix(z,"z - Product of voxel passed to getValue -Mean / stddev");
328 
329  // z should match the dimensions of tmp_gsl_matrix so do some error checking
330  if (z->size1 != tmp_gsl_matrix->size1)
331  {
332  Log::instance()->warn( "Error during creation of product Z in CSM getValue - number of rows don't match\n" );
333  exit(0);
334  }
335 
336  // number of cols in z should == number of components
337  if (z->size2 != tmp_gsl_matrix->size1)
338  {
339  //Log::instance()->warn( "Error during creation of product Z in CSM getValue - number of cols don't match number of components\n" );
340  //exit(0);
341  }
342 
343  //displayMatrix(z,"z ");
344  // now we standardise the values in z
345  // we do this by dividing each element in z by the square root of its associated element in
346  // the eigenvalues vector
347 
348  for (unsigned int i=0;i<z->size2;i++)
349  {
350  gsl_matrix_set(z,0,i,gsl_matrix_get (z,0,i)/sqrt(gsl_vector_get(_gsl_eigenvalue_vector,i)));
351  }
352  displayMatrix(z,"Standardised : Each value in z / sqrt of associated element in the eigenvalues vector");
353  // now we square each element and sum them
354  double mySumOfSquares=0;
355  for (unsigned int i=0;i<z->size2;i++)
356  {
357  double myValue=gsl_matrix_get (z,0,i);
358  if (!isnan(myValue))
359  {
360  mySumOfSquares+= pow(gsl_matrix_get (z,0,i), 2);
361  //Warning uncommenting the next line will spew a lot of stuff to stderr!!!!
362  //Log::instance()->debug( "myValue : %f Cumulative : %f\n", myValue , myFloat );
363  }
364  }
365 
366  //now work out the probability of myFloat between 0 and 1
367  double myHalfComponentCountDouble=(z->size2)/2;
368  double myHalfSumOfSquaresDouble=mySumOfSquares/2;
369  //Log::instance()->debug( "Component count %f , Half sum of squares %f\n", myHalfComponentCountDouble, myHalfSumOfSquaresDouble );
370  //
371  //This way id deprecated in favour of the chi square test
372  //
373  //float myProbability=1-gsl_sf_gamma_inc_Q (myHalfSumOfSquaresDouble,myHalfComponentCountDouble);
374  //float myProbability=1-gsl_ran_chisq_pdf(mySumOfSquares,z->size2);
375  double myProbability=gsl_cdf_chisq_Q(mySumOfSquares,z->size2);
377  {
378  printf("\n-------------------------------\n");
379  printf("Component count : %u\n",static_cast<unsigned int>(z->size2));
380  printf("Component count / 2: %f\n",myHalfComponentCountDouble);
381  printf("Sum of squares : %f\n",mySumOfSquares);
382  printf("Sum of squares / 2: %f\n",myHalfSumOfSquaresDouble);
383  printf("Probability: %f\n\n", myProbability);
384  printf("-------------------------------\n");
385 
386  }
387 
388  //Log::instance()->debug( "Prob: %f \r", myProbability);
389  //now clear away the temporary vars
390  gsl_matrix_free (z);
391  //gsl_vector_free (component1_gsl_vector);
392  gsl_matrix_free (tmp_gsl_matrix);
393  gsl_matrix_free (tmp_raw_gsl_matrix);
394  return myProbability;
395 }
396 
403 int Csm::getConvergence( Scalar * const val ) const
404 {
405  return 0;
406 }
407 
408 //
409 // General Helper Methods
410 //
411 
412 
413 void Csm::displayVector(const gsl_vector * v, const char * name, const bool roundFlag) const
414 {
416  {
417  if (roundFlag)
418  {
419 
420  fprintf( stderr, "\nDisplaying Vector rounded to 4 decimal places '%s' (%u): \n----------------------------------------------\n[ ", name, static_cast<unsigned int>(v->size) );
421  }
422  else
423  {
424  fprintf( stderr, "\nDisplaying Vector '%s' (%u): \n----------------------------------------------\n[ ", name, static_cast<unsigned int>(v->size) );
425  }
426 
427  char sep1[] = ", ";
428 
429  for (unsigned int i=0;i<v->size;++i)
430  {
431  if (i == v->size -1)
432  strcpy(sep1, " ]");
433 
434  double myDouble = gsl_vector_get (v,i);
435  if (roundFlag)
436  {
437  fprintf( stderr, "%.4g %s", myDouble, sep1 );
438  }
439  else
440  {
441  fprintf( stderr, "%g %s", myDouble, sep1 );
442  }
443  }
444  fprintf( stderr, "\n----------------------------------------------\n" );
445  }
446 }
447 
448 
449 /**********************/
450 /**** displayMatrix ***/
451 void Csm::displayMatrix(const gsl_matrix * m, const char * name, const bool roundFlag) const
452 {
454  {
455  if (!roundFlag)
456  {
457  fprintf( stderr, "\nDisplaying Matrix '%s' (%u / %u): \n----------------------------------------------\n[\n", name, static_cast<unsigned int>(m->size1), static_cast<unsigned int>(m->size2) );
458  }
459  else
460  {
461  fprintf( stderr, "\nDisplaying Matrix rounded to 4 decimal places '%s' (%u / %u): \n----------------------------------------------\n[\n", name, static_cast<unsigned int>(m->size1), static_cast<unsigned int>(m->size2) );
462  }
463  for (unsigned int i=0;i<m->size1;++i)
464  {
465  char sep1[] = ",";
466  char sep2[] = ";";
467 
468  for (unsigned int j=0;j<m->size2;j++)
469  {
470  double myDouble = gsl_matrix_get (m,i,j);
471 
472  if (j == m->size2 -1)
473  strcpy(sep1, "");
474  if (!roundFlag)
475  {
476  fprintf( stderr, "%g %s ", myDouble, sep1 );
477  }
478  else
479  {
480  fprintf( stderr, "%.4g %s ", myDouble, sep1 );
481  }
482  }
483 
484  fprintf( stderr, "%s\n", sep2 );
485  }
486  fprintf( stderr, "]\n----------------------------------------------\n" );
487  }
488 }
489 
490 
491 /******************/
492 /**** transpose ***/
493 gsl_matrix * Csm::transpose (gsl_matrix * m)
494 {
495  gsl_matrix * t = gsl_matrix_alloc (m->size2, m->size1);
496 
497  for (unsigned int i = 0; i < m->size1; i++)
498  {
499  gsl_vector * v = gsl_vector_alloc(m->size2);
500  gsl_matrix_get_row (v, m, i);
501  gsl_matrix_set_col (t, i, v);
502  gsl_vector_free(v);
503  }
504 
505  return t;
506 }
507 
508 /************************/
509 /**** Vectors product ***/
510 double Csm::product (gsl_vector * va, gsl_vector * vb) const
511 {
512  // fix me: need to check if vectors are of the same size !!!
513 
514  double res = 0.0;
515 
516  for (unsigned int i = 0; i < va->size; i++)
517  {
518  res += gsl_vector_get(va, i)*gsl_vector_get(vb, i);
519  }
520 
521  return res;
522 }
523 
524 /*************************/
525 /**** Matrices product ***/
526 gsl_matrix * Csm::product (gsl_matrix * a, gsl_matrix * b) const
527 {
528  // fix me: need to check if a->size2 is equal to b->size1 !!!
529 
530  gsl_matrix * p = gsl_matrix_alloc (a->size1, b->size2);
531 
532  for (unsigned int i = 0; i < a->size1; i++)
533  {
534  gsl_vector * va = gsl_vector_alloc(a->size2);
535 
536  gsl_matrix_get_row (va, a, i);
537 
538  for (unsigned int j = 0; j < b->size2; j++)
539  {
540  gsl_vector * vb = gsl_vector_alloc(a->size2);
541 
542  gsl_matrix_get_col (vb, b, j);
543 
544  double vp = product(va, vb);
545 
546  gsl_matrix_set (p, i, j, vp);
547 
548  gsl_vector_free (vb);
549  }
550 
551  gsl_vector_free (va);
552  }
553 
554  return p;
555 }
556 
557 /***********************/
558 /**** autoCovariance ***/
578 gsl_matrix * Csm::autoCovariance(gsl_matrix * original_matrix)
579 {
580  int j;
581 
582  // Build a copy of the input matrix to work with
583  gsl_matrix * m = gsl_matrix_alloc (original_matrix->size1, original_matrix->size2);
584  gsl_matrix_memcpy (m, original_matrix);
585 
586  int numrows = m->size1;
587  int numcols = m->size2;
588 
589  if (numrows == 1)
590  {
591  m = transpose(m);
592  }
593 
594  // compute: ones (n, 1) * sum (x)
595  gsl_matrix * s = gsl_matrix_alloc (numrows, numcols);
596 
597  if (numrows == 1)
598  {
599  gsl_matrix_memcpy (m, s);
600  }
601  else
602  {
603  gsl_vector * v = gsl_vector_alloc(numcols);
604 
605  for (int i = 0; i < numcols; i++)
606  {
607  double val = 0.0;
608 
609  for (j = 0; j < numrows; j++)
610  {
611  val += gsl_matrix_get (m, j, i);
612  }
613 
614  gsl_vector_set (v, i, val);
615 
616  for (j = 0; j < numrows; j++)
617  {
618  gsl_matrix_set_row (s, j, v);
619  }
620  }
621  gsl_vector_free(v);
622  }
623 
624  // divide by "n"
625  gsl_matrix_scale (s, (double)1/numrows);
626 
627  // subtract the result from x
628  gsl_matrix_sub (m, s);
629 
630  // get x'
631  gsl_matrix * mt = transpose(m);
632 
633  // multiply by x'
634  gsl_matrix * p = product(mt, m);
635 
636  // Note: scaling should happen after calculating the product with x-transpose
637  // x / (n - 1)
638  gsl_matrix_scale (p, (double)1/(numrows-1));
639 
640  gsl_matrix_free (mt);
641  gsl_matrix_free (m);
642  gsl_matrix_free (s);
643 
644  return p;
645 }
646 
648 bool Csm::csm1()
649 {
650  //calculate the mean and std deviation
651  Log::instance()->debug( "Calculating mean and stddev\n" );
652  _gsl_avg_vector = gsl_vector_alloc (_gsl_environment_matrix->size2);
653  _gsl_stddev_vector = gsl_vector_alloc (_gsl_environment_matrix->size2) ;
655  //displayVector(_gsl_avg_vector,"Average vector");
656  //displayVector(_gsl_stddev_vector,"Standard Deviation vector");
657  //center and standardise the data
658  Log::instance()->debug( "Centering and standardising\n" );
659  center();
660  //show what we have calculated so far....
661  // displayMatrix(_gsl_environment_matrix,"Environemntal Layer Samples (after centering)");
662 
663  //Now calculate the covariance matrix:
664  Log::instance()->debug( "Calculating covariance matrix\n" );
666  //the rows and columns in the cavariance matrix should be equal, otherwise abort
667  if (_gsl_covariance_matrix->size1 != _gsl_covariance_matrix->size2)
668  {
669  Log::instance()->warn( "CSM critical error - covariance matrix is not square!\n" );
670  return false;
671  }
672  //and display the result...
673  //displayMatrix(_gsl_covariance_matrix,"Covariance Matrix");
674 
675  //now compute the eigen value and vector
676  Log::instance()->debug( "Calculating eigenvalue and eigenvector\n" );
677  _gsl_eigenvalue_vector = gsl_vector_alloc (_layer_count);
678  _gsl_eigenvector_matrix = gsl_matrix_alloc (_layer_count, _layer_count);
679  //create a temporary workspace
680  gsl_eigen_symmv_workspace * myWorkpace = gsl_eigen_symmv_alloc (_layer_count);
681  gsl_eigen_symmv (_gsl_covariance_matrix,
684  myWorkpace);
685  //free the temporary workspace again
686  gsl_eigen_symmv_free (myWorkpace);
687  //Initialise the retained components count (to be used further down and in displayEigen())
689  //show the eigen before sorting
690  //Log::instance()->debug( "\nBefore sorting : \n" );
691  //displayVector(_gsl_eigenvalue_vector,"Unsorted Eigen Values");
692  //displayMatrix(_gsl_eigenvector_matrix,"Unsorted Eigen Vector");
693  //sort the eigen vector by the eigen values (in descending order)
694  gsl_eigen_symmv_sort (_gsl_eigenvalue_vector, _gsl_eigenvector_matrix,
695  GSL_EIGEN_SORT_VAL_DESC);
696  //print out the result
697  Log::instance()->debug( "Eigenvector sorted\n" );
698  //displayVector(_gsl_eigenvalue_vector,"Sorted Eigen Values");
699  //displayMatrix(_gsl_eigenvector_matrix,"Sorted Eigen Vector");
700 
701  Log::instance()->debug( "CSM Model Generation Completed\n" );
702 
703  //After the model is generated, we can discard unwanted components!
704  if (discardComponents())
705  {
706  Log::instance()->debug( "Unwanted components discarded\n" );
707  return true;
708  }
709  else
710  {
711  Log::instance()->debug( "Discard components retained too few components - model can not be generated!\n" );
712  Log::instance()->warn( "Could not generate a model with sufficient components!\n" );
713  }
714  return false;
715  //print out the result
716 }
717 
718 
719 
720 /****************************************************************/
721 /****************** configuration *******************************/
722 void
724 {
725  if (!_done )
726  return;
727 
728  ConfigurationPtr model_config( new ConfigurationImpl("Csm") );
729  config->addSubsection( model_config );
730 
731  // _gsl_avg_vector
732  double *values = new double[_layer_count];
733 
734  for (int i=0; i < _layer_count; ++i)
735  values[i] = gsl_vector_get(_gsl_avg_vector, i);
736 
737  model_config->addNameValue( "AvgVector", values, _layer_count );
738 
739  // _gsl_stddev_vector
740  for (int i=0; i < _layer_count; ++i)
741  values[i] = gsl_vector_get(_gsl_stddev_vector, i);
742 
743  model_config->addNameValue( "StddevVector", values, _layer_count );
744 
745  // _gsl_eigenvalue_vector
746  for (int i=0; i < _retained_components_count; ++i)
747  values[i] = gsl_vector_get(_gsl_eigenvalue_vector, i);
748 
749  model_config->addNameValue( "EigenvalueVector", values, _retained_components_count );
750 
751  delete[] values;
752 
753  // _gsl_eigenvector_matrix
754  int num_cells = _layer_count * _retained_components_count;
755 
756  double *flat_eigenvector_matrix = new double[num_cells];
757 
758  int cnt = 0;
759 
760  for (int i=0; i < _layer_count; ++i)
761  for (int j=0; j < _retained_components_count; ++j, ++cnt)
762  flat_eigenvector_matrix[cnt] = gsl_matrix_get( _gsl_eigenvector_matrix, i, j );
763 
764  model_config->addNameValue( "EigenvectorMatrix", flat_eigenvector_matrix, num_cells );
765 
766  delete[] flat_eigenvector_matrix;
767 }
768 
769 void
771 {
772  ConstConfigurationPtr model_config = config->getSubsection( "Csm",false );
773 
774  if (!model_config)
775  return;
776 
777  // _gsl_avg_vector
778  std::vector<double> stl_vector = model_config->getAttributeAsVecDouble( "AvgVector" );
779 
780  _layer_count = stl_vector.size();
781 
782  _gsl_avg_vector = gsl_vector_alloc( _layer_count );
783 
784  for (int i=0; i < _layer_count; ++i)
785  gsl_vector_set( _gsl_avg_vector, i, stl_vector[i] );
786 
787  // _gsl_stddev_vector
788  stl_vector = model_config->getAttributeAsVecDouble( "StddevVector" );
789 
790  _gsl_stddev_vector = gsl_vector_alloc( _layer_count );
791 
792  for (int i=0; i < _layer_count; ++i)
793  gsl_vector_set( _gsl_stddev_vector, i, stl_vector[i] );
794 
795  // _gsl_eigenvalue_vector
796  stl_vector = model_config->getAttributeAsVecDouble( "EigenvalueVector" );
797 
798  _retained_components_count = stl_vector.size();
799 
801 
802  for (int i=0; i < _retained_components_count; ++i)
803  gsl_vector_set( _gsl_eigenvalue_vector, i, stl_vector[i] );
804 
805  // _gsl_eigenvector_matrix
806  stl_vector = model_config->getAttributeAsVecDouble( "EigenvectorMatrix" );
807 
808  _gsl_eigenvector_matrix = gsl_matrix_alloc( _layer_count, _retained_components_count );
809 
810  int cnt = 0;
811 
812  for (int i=0; i < _layer_count; ++i)
813  for (int j=0; j < _retained_components_count; ++j, ++cnt)
814  gsl_matrix_set( _gsl_eigenvector_matrix, i, j, stl_vector[cnt] );
815 
816  _done = true;
817 }
Scalar getValue(const Sample &x) const
Definition: csm.cpp:283
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
gsl_matrix * _gsl_eigenvector_matrix
Definition: csm.hh:442
bool csm1()
Definition: csm.cpp:648
double Scalar
Type of map values.
Definition: om_defs.hh:39
int done() const
Definition: csm.cpp:268
virtual int initialize()
Definition: csm.cpp:98
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
int center()
Definition: csm.cpp:217
int getConvergence(Scalar *const val) const
Definition: csm.cpp:403
virtual void _getConfiguration(ConfigurationPtr &config) const
Definition: csm.cpp:723
int calculateMeanAndSd(gsl_matrix *theMatrix, gsl_vector *theMeanVector, gsl_vector *theStdDevVector)
Definition: csm.cpp:175
int _localityCount
Definition: csm.hh:448
virtual void _setConfiguration(const ConstConfigurationPtr &config)
Definition: csm.cpp:770
bool verboseDebuggingBool
Definition: csm.hh:453
int _retained_components_count
Definition: csm.hh:446
virtual int discardComponents()=0
Csm(AlgMetadata const *metadata)
Definition: csm.cpp:57
gsl_vector * _gsl_eigenvalue_vector
Definition: csm.hh:440
~Csm()
Definition: csm.cpp:70
gsl_vector * _gsl_avg_vector
Definition: csm.hh:436
gsl_vector * _gsl_stddev_vector
Definition: csm.hh:438
void displayMatrix(const gsl_matrix *m, const char *name, const bool roundFlag=true) const
Definition: csm.cpp:451
int _layer_count
Definition: csm.hh:444
gsl_matrix * autoCovariance(gsl_matrix *m)
Definition: csm.cpp:578
void displayVector(const gsl_vector *v, const char *name, const bool roundFlag=true) const
Definition: csm.cpp:413
int SamplerToMatrix()
Definition: csm.cpp:142
int _initialized
Definition: csm.hh:423
gsl_matrix * _gsl_covariance_matrix
Definition: csm.hh:433
gsl_matrix * transpose(gsl_matrix *m)
Definition: csm.cpp:493
double product(gsl_vector *va, gsl_vector *vb) const
Definition: csm.cpp:510
SamplerPtr _samp
Definition: Algorithm.hh:245
gsl_matrix * _gsl_environment_matrix
Definition: csm.hh:430
int _done
Definition: csm.hh:426
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
int iterate()
Definition: csm.cpp:255
static AlgParamMetadata * parameters
Definition: csm.cpp:46
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
AlgMetadata metadata
Definition: garp.cpp:134
Definition: Sample.hh:25