openModeller
Version 1.4.0
|
00001 00036 #include "bioclim_histogram.hh" 00037 00038 #include <openmodeller/Random.hh> 00039 #include <string.h> 00040 00041 /****************************************************************/ 00042 /************************* Bioclim Histogram ********************/ 00043 BioclimHistogram::BioclimHistogram() : 00044 _upperLevels( NULL ), 00045 _lowerLevels( NULL ) 00046 { 00047 reset(); 00048 } 00049 00050 void BioclimHistogram::reset() 00051 { 00052 memset(_matrix, 0, 2 * MAX_ENV_LAYERS * 256 * sizeof(int)); 00053 memset(_depend, 0, 2 * sizeof(int)); 00054 } 00055 00056 BioclimHistogram::~BioclimHistogram() 00057 { 00058 if (_upperLevels) 00059 delete _upperLevels; 00060 if (_lowerLevels) 00061 delete _lowerLevels; 00062 } 00063 00064 void BioclimHistogram::initialize(const OccurrencesPtr& occs) 00065 { 00066 reset(); 00067 _resamples = occs->numOccurrences(); 00068 00069 // number of layers/genes/independent variables 00070 00071 int numLayers = (*occs)[0]->environment().size(); 00072 00073 // iterate through the samples 00074 OccurrencesImpl::const_iterator it = occs->begin(); 00075 OccurrencesImpl::const_iterator end = occs->end(); 00076 00077 int sampleIndex = 0; 00078 while (it != end) 00079 { 00080 Scalar pointValue = ( (*it)->abundance() > 0.0 ) ? 1.0 : 0.0; 00081 Sample sample = (*it)->environment(); 00082 00083 int predictionIndex = static_cast<int>(pointValue); 00084 00085 //printf("%d %f=%d (%f,%f) \n", sampleIndex, pointValue, predictionIndex, (*it)->x(), (*it)->y() ); 00086 00087 _depend[predictionIndex]++; 00088 00089 // iterate through the layers 00090 //printf("layers: %d %d \n", numLayers, sample.size() ); 00091 for ( int layerIndex = 0; layerIndex < numLayers; layerIndex++) 00092 { 00093 int normValue = (int) ((sample[layerIndex] + 1.0) / 2.0 * 253.0) + 1; 00094 if (normValue < 0) { 00095 normValue = 0; 00096 } 00097 else if (normValue > 255 ) { 00098 normValue = 255; 00099 } 00100 //printf("Value[%d]: %f (%d)\n", layerIndex, sample[layerIndex], normValue); 00101 _matrix[predictionIndex][layerIndex][normValue]++; 00102 } 00103 00104 ++it; 00105 sampleIndex++; 00106 } 00107 00108 // printf("Dependent counts: %d %d\n", _depend[0], _depend[1]); 00109 00110 // _depend[0] = _depend[0] % 256; 00111 // _depend[1] = _depend[1] % 256; 00112 00113 // printf("Dependent counts: %d %d\n", _depend[0], _depend[1]); 00114 // for (int i = 0; i < 2; i++) 00115 // { 00116 // for (int k = 0; k < 256; k++) 00117 // { 00118 // printf("Pred=%1d Value=%3d: ", i, k); 00119 // for (int j = 0; j < numLayers; j++) 00120 // { printf("%5d ", _matrix[i][j][k]); } 00121 // printf("\n"); 00122 // } 00123 // } 00124 } 00125 00126 int BioclimHistogram::resamples() const 00127 { 00128 return _resamples; 00129 } 00130 00131 // ============ 00132 void BioclimHistogram::getBioclimRange(Scalar prediction, int layerIndex, 00133 Scalar& minCutLevel, Scalar& maxCutLevel) const 00134 { 00135 Random rnd; 00136 int sum, n, UL, LL; 00137 00138 int predIndex = (prediction == 1.0); 00139 double level = rnd.get(0.1); 00140 int totalExcluded = (int) (_depend[predIndex] * level); 00141 00142 LL = 0; 00143 UL = 255; 00144 00145 sum = 0; 00146 for (n = 0; n <= 255; n++) 00147 { 00148 sum += _matrix[predIndex][layerIndex][n]; 00149 if (sum > totalExcluded) 00150 { 00151 LL = n; 00152 break; 00153 } 00154 } 00155 00156 sum = 0; 00157 for (n = 255; n >= 0; n--) 00158 { 00159 sum += _matrix[predIndex][layerIndex][n]; 00160 if (sum > totalExcluded) 00161 { 00162 UL = n; 00163 break; 00164 } 00165 } 00166 00167 minCutLevel = ( LL / 255.0 * 2 ) - 1.0; 00168 maxCutLevel = ( UL / 255.0 * 2 ) - 1.0; 00169 00170 00171 //printf("Layer:%3d Excl: %5d Level:%7.4f Cut=(%+7.4f, %+7.4f)\n", 00172 // layerIndex, totalExcluded, level, *minCutLevel, *maxCutLevel); 00173 }