openModeller  Version 1.5.0
EnvCellSet.cpp
Go to the documentation of this file.
1 /* **************************************
2  * GARP Modeling Package
3  *
4  * **************************************
5  *
6  * Copyright (c), The Center for Research, University of Kansas, 2385 Irving Hill Road, Lawrence, KS 66044-4755, USA.
7  * Copyright (C), David R.B. Stockwell of Symbiotik Pty. Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the license that is distributed with the software.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * license.txt file included with this software for more details.
16  */
17 
18 // EnvCellSet.cpp: implementation of the EnvCellSet class.
19 
20 #include "EnvCell.h"
21 #include "EnvCellSet.h"
22 #include "Utilities.h"
23 
24 // ============================================================================
26 {
27  intCount = 0;
28  intSize = 0;
29  objCells = NULL;
30 
31  histogram = NULL;
32 
33  resampled = false;
34 }
35 
36 // ============================================================================
38 {
39  intCount = 0;
40  intSize = 0;
41  objCells = NULL;
42 
43  histogram = NULL;
44 
45  resampled = false;
46 
47  initialize(size);
48 }
49 
50 // ============================================================================
52 {
53  if (histogram)
54  delete histogram;
55 
56  if (objCells != NULL)
57  {
58  if (!resampled)
59  {
60  // not resampled:
61  // it is not a copy of another CellSet
62  // can delete all the EnvCells
63  for (int i = 0; i < intCount; i++)
64  {
65  // EnvCellSet takes care of EnvCell property alocation and dealocation
66  // possible bug!!!
67  if (objCells[i])
68  {
69  delete[] objCells[i]->values;
70  delete objCells[i];
71  }
72  }
73  }
74 
75  delete[] objCells;
76  }
77 }
78 
79 // ============================================================================
81 {
82  int p, q;
83  EnvCell * oAuxCell;
84 
85  // shuffle pointers to cells
86  for (int i = 0; i < intSize; i++)
87  {
88  // pick two random numbers
89  p = GarpUtil::randint(0, intSize - 1);
90  q = GarpUtil::randint(0, intSize - 1);
91 
92  // change places between cells <p> and <q>
93  oAuxCell = objCells[p];
94  objCells[p] = objCells[q];
95  objCells[q] = oAuxCell;
96  }
97 }
98 
99 // ============================================================================
101 {
102  // create an EnvCellSet to hold shuffled pointers to the original set
103  // it is a resampled set so the cells won't be dealocate upon destruction
104  // of the resampled object
105  EnvCellSet * objResampled = new EnvCellSet(intSize);
106  objResampled->resampled = true;
107 
108  // point to random data
109  for (int i = 0; i < intSize; i++)
110  objResampled->add(objCells[GarpUtil::randint(0, intSize - 1)]);
111 
112  return objResampled;
113 }
114 
115 // ============================================================================
117 {
118  intCount = 0;
119  intSize = size;
120  objCells = new EnvCell*[intSize];
121 }
122 
123 // ============================================================================
125 {
126  if (intCount)
127  return objCells[0]->size();
128  else
129  return 0;
130 }
131 
132 // ============================================================================
134 {
135  return intSize;
136 }
137 
138 // ============================================================================
140 {
141  return intCount;
142 }
143 
144 // ============================================================================
146 {
147  if (index < intCount)
148  return objCells[index];
149  else
150  throw GarpException(1, "EnvCellSet::get(): index out of bounds");
151 }
152 
153 // ============================================================================
155 {
156  if (intCount < intSize)
157  objCells[intCount++] = cell;
158  else
159  throw GarpException(1, "EnvCellSet::add(): cell set already full");
160 }
161 
162 // ==========================================================================
164 { return histogram; }
165 
166 // ==========================================================================
168 {
169  //EnvCell * cell;
170  int i, j, nc, ng;
171  BYTE pred, val;
172 
173  if (!histogram)
175 
176  histogram->reset();
177 
178  // number of genes
179  ng = genes();
180 
181  // number of cells
182  nc = intCount;
183 
184  // calculate occurrence of value for each gene
185  for (i = 0; i < ng; i++)
186  {
187  // take into account the occurrence of each value for each cell in train set
188  for (j = 0; j < nc; j++)
189  {
190  // cell = objCells[j];
191 
192  // get presence or absence
193  pred = objCells[j]->values[0];
194  val = objCells[j]->values[i];
195  histogram->matrix[pred][i][val]++;
196  }
197  }
198 }
199 
200 // ============================================================================
201 // ============================================================================
202 // ============================================================================
void initialize(int size)
Definition: EnvCellSet.cpp:116
BYTE matrix[2][MAX_ENV_LAYERS][256]
Definition: Utilities.h:264
static int randint(int low, int high)
Definition: Utilities.h:216
BYTE * values
Definition: EnvCell.h:32
BioclimHistogram * getBioclimHistogram()
Definition: EnvCellSet.cpp:163
unsigned char BYTE
Definition: Utilities.h:36
EnvCell ** objCells
Definition: EnvCellSet.h:35
virtual ~EnvCellSet()
Definition: EnvCellSet.cpp:51
void add(EnvCell *cell)
Definition: EnvCellSet.cpp:154
void resampleInPlace()
Definition: EnvCellSet.cpp:80
int intCount
Definition: EnvCellSet.h:33
BioclimHistogram * histogram
Definition: EnvCellSet.h:40
EnvCellSet * resample()
Definition: EnvCellSet.cpp:100
bool resampled
Definition: EnvCellSet.h:37
int size()
Definition: EnvCell.cpp:70
EnvCell * get(int index)
Definition: EnvCellSet.cpp:145
int intSize
Definition: EnvCellSet.h:34
void createBioclimHistogram()
Definition: EnvCellSet.cpp:167