openModeller  Version 1.5.0
bioclim_histogram.cpp
Go to the documentation of this file.
1 
36 #include "bioclim_histogram.hh"
37 
38 #include <openmodeller/Random.hh>
39 #include <string.h>
40 
41 /****************************************************************/
42 /************************* Bioclim Histogram ********************/
44  _upperLevels( NULL ),
45  _lowerLevels( NULL )
46 {
47  reset();
48 }
49 
51  {
52  memset(_matrix, 0, 2 * MAX_ENV_LAYERS * 256 * sizeof(int));
53  memset(_depend, 0, 2 * sizeof(int));
54  }
55 
57 {
58  if (_upperLevels)
59  delete _upperLevels;
60  if (_lowerLevels)
61  delete _lowerLevels;
62 }
63 
65 {
66  reset();
67  _resamples = occs->numOccurrences();
68 
69  // number of layers/genes/independent variables
70 
71  int numLayers = (*occs)[0]->environment().size();
72 
73  // iterate through the samples
74  OccurrencesImpl::const_iterator it = occs->begin();
75  OccurrencesImpl::const_iterator end = occs->end();
76 
77  int sampleIndex = 0;
78  while (it != end)
79  {
80  Scalar pointValue = ( (*it)->abundance() > 0.0 ) ? 1.0 : 0.0;
81  Sample sample = (*it)->environment();
82 
83  int predictionIndex = static_cast<int>(pointValue);
84 
85  //printf("%d %f=%d (%f,%f) \n", sampleIndex, pointValue, predictionIndex, (*it)->x(), (*it)->y() );
86 
87  _depend[predictionIndex]++;
88 
89  // iterate through the layers
90  //printf("layers: %d %d \n", numLayers, sample.size() );
91  for ( int layerIndex = 0; layerIndex < numLayers; layerIndex++)
92  {
93  int normValue = (int) ((sample[layerIndex] + 1.0) / 2.0 * 253.0) + 1;
94  if (normValue < 0) {
95  normValue = 0;
96  }
97  else if (normValue > 255 ) {
98  normValue = 255;
99  }
100  //printf("Value[%d]: %f (%d)\n", layerIndex, sample[layerIndex], normValue);
101  _matrix[predictionIndex][layerIndex][normValue]++;
102  }
103 
104  ++it;
105  sampleIndex++;
106  }
107 
108 // printf("Dependent counts: %d %d\n", _depend[0], _depend[1]);
109 
110 // _depend[0] = _depend[0] % 256;
111 // _depend[1] = _depend[1] % 256;
112 
113 // printf("Dependent counts: %d %d\n", _depend[0], _depend[1]);
114 // for (int i = 0; i < 2; i++)
115 // {
116 // for (int k = 0; k < 256; k++)
117 // {
118 // printf("Pred=%1d Value=%3d: ", i, k);
119 // for (int j = 0; j < numLayers; j++)
120 // { printf("%5d ", _matrix[i][j][k]); }
121 // printf("\n");
122 // }
123 // }
124 }
125 
127 {
128  return _resamples;
129 }
130 
131 // ============
132 void BioclimHistogram::getBioclimRange(Scalar prediction, int layerIndex,
133  Scalar& minCutLevel, Scalar& maxCutLevel) const
134 {
135  Random rnd;
136  int sum, n, UL, LL;
137 
138  int predIndex = (prediction == 1.0);
139  double level = rnd.get(0.1);
140  int totalExcluded = (int) (_depend[predIndex] * level);
141 
142  LL = 0;
143  UL = 255;
144 
145  sum = 0;
146  for (n = 0; n <= 255; n++)
147  {
148  sum += _matrix[predIndex][layerIndex][n];
149  if (sum > totalExcluded)
150  {
151  LL = n;
152  break;
153  }
154  }
155 
156  sum = 0;
157  for (n = 255; n >= 0; n--)
158  {
159  sum += _matrix[predIndex][layerIndex][n];
160  if (sum > totalExcluded)
161  {
162  UL = n;
163  break;
164  }
165  }
166 
167  minCutLevel = ( LL / 255.0 * 2 ) - 1.0;
168  maxCutLevel = ( UL / 255.0 * 2 ) - 1.0;
169 
170 
171  //printf("Layer:%3d Excl: %5d Level:%7.4f Cut=(%+7.4f, %+7.4f)\n",
172  // layerIndex, totalExcluded, level, *minCutLevel, *maxCutLevel);
173 }
double get(double min, double max)
Definition: Random.cpp:54
double Scalar
Type of map values.
Definition: om_defs.hh:39
void getBioclimRange(Scalar prediction, int layerIndex, Scalar &minCutLevel, Scalar &maxCutLevel) const
Definition: Random.hh:44
const int MAX_ENV_LAYERS
Definition: Utilities.h:53
int _matrix[2][MAX_ENV_LAYERS][256]
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
void initialize(const OccurrencesPtr &occs)
Definition: Sample.hh:25