openModeller
Version 1.4.0
|
00001 /* ************************************** 00002 * GARP Modeling Package 00003 * 00004 * ************************************** 00005 * 00006 * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA. 00007 * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd. 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the license that is distributed with the software. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * license.txt file included with this software for more details. 00016 */ 00017 00018 // EnvCellSet.cpp: implementation of the EnvCellSet class. 00019 00020 #include "EnvCell.h" 00021 #include "EnvCellSet.h" 00022 #include "Utilities.h" 00023 00024 // ============================================================================ 00025 EnvCellSet::EnvCellSet() 00026 { 00027 intCount = 0; 00028 intSize = 0; 00029 objCells = NULL; 00030 00031 histogram = NULL; 00032 00033 resampled = false; 00034 } 00035 00036 // ============================================================================ 00037 EnvCellSet::EnvCellSet(int size) 00038 { 00039 intCount = 0; 00040 intSize = 0; 00041 objCells = NULL; 00042 00043 histogram = NULL; 00044 00045 resampled = false; 00046 00047 initialize(size); 00048 } 00049 00050 // ============================================================================ 00051 EnvCellSet::~EnvCellSet() 00052 { 00053 if (histogram) 00054 delete histogram; 00055 00056 if (objCells != NULL) 00057 { 00058 if (!resampled) 00059 { 00060 // not resampled: 00061 // it is not a copy of another CellSet 00062 // can delete all the EnvCells 00063 for (int i = 0; i < intCount; i++) 00064 { 00065 // EnvCellSet takes care of EnvCell property alocation and dealocation 00066 // possible bug!!! 00067 if (objCells[i]) 00068 { 00069 delete[] objCells[i]->values; 00070 delete objCells[i]; 00071 } 00072 } 00073 } 00074 00075 delete[] objCells; 00076 } 00077 } 00078 00079 // ============================================================================ 00080 void EnvCellSet::resampleInPlace() 00081 { 00082 int p, q; 00083 EnvCell * oAuxCell; 00084 00085 // shuffle pointers to cells 00086 for (int i = 0; i < intSize; i++) 00087 { 00088 // pick two random numbers 00089 p = GarpUtil::randint(0, intSize - 1); 00090 q = GarpUtil::randint(0, intSize - 1); 00091 00092 // change places between cells <p> and <q> 00093 oAuxCell = objCells[p]; 00094 objCells[p] = objCells[q]; 00095 objCells[q] = oAuxCell; 00096 } 00097 } 00098 00099 // ============================================================================ 00100 EnvCellSet * EnvCellSet::resample() 00101 { 00102 // create an EnvCellSet to hold shuffled pointers to the original set 00103 // it is a resampled set so the cells won't be dealocate upon destruction 00104 // of the resampled object 00105 EnvCellSet * objResampled = new EnvCellSet(intSize); 00106 objResampled->resampled = true; 00107 00108 // point to random data 00109 for (int i = 0; i < intSize; i++) 00110 objResampled->add(objCells[GarpUtil::randint(0, intSize - 1)]); 00111 00112 return objResampled; 00113 } 00114 00115 // ============================================================================ 00116 void EnvCellSet::initialize(int size) 00117 { 00118 intCount = 0; 00119 intSize = size; 00120 objCells = new EnvCell*[intSize]; 00121 } 00122 00123 // ============================================================================ 00124 int EnvCellSet::genes() 00125 { 00126 if (intCount) 00127 return objCells[0]->size(); 00128 else 00129 return 0; 00130 } 00131 00132 // ============================================================================ 00133 int EnvCellSet::size() 00134 { 00135 return intSize; 00136 } 00137 00138 // ============================================================================ 00139 int EnvCellSet::count() 00140 { 00141 return intCount; 00142 } 00143 00144 // ============================================================================ 00145 EnvCell * EnvCellSet::get(int index) 00146 { 00147 if (index < intCount) 00148 return objCells[index]; 00149 else 00150 throw GarpException(1, "EnvCellSet::get(): index out of bounds"); 00151 } 00152 00153 // ============================================================================ 00154 void EnvCellSet::add(EnvCell * cell) 00155 { 00156 if (intCount < intSize) 00157 objCells[intCount++] = cell; 00158 else 00159 throw GarpException(1, "EnvCellSet::add(): cell set already full"); 00160 } 00161 00162 // ========================================================================== 00163 BioclimHistogram * EnvCellSet::getBioclimHistogram() 00164 { return histogram; } 00165 00166 // ========================================================================== 00167 void EnvCellSet::createBioclimHistogram() 00168 { 00169 //EnvCell * cell; 00170 int i, j, nc, ng; 00171 BYTE pred, val; 00172 00173 if (!histogram) 00174 histogram = new BioclimHistogram; 00175 00176 histogram->reset(); 00177 00178 // number of genes 00179 ng = genes(); 00180 00181 // number of cells 00182 nc = intCount; 00183 00184 // calculate occurrence of value for each gene 00185 for (i = 0; i < ng; i++) 00186 { 00187 // take into account the occurrence of each value for each cell in train set 00188 for (j = 0; j < nc; j++) 00189 { 00190 // cell = objCells[j]; 00191 00192 // get presence or absence 00193 pred = objCells[j]->values[0]; 00194 val = objCells[j]->values[i]; 00195 histogram->matrix[pred][i][val]++; 00196 } 00197 } 00198 } 00199 00200 // ============================================================================ 00201 // ============================================================================ 00202 // ============================================================================