openModeller  Version 1.5.0
AlgorithmRun.cpp
Go to the documentation of this file.
1 
30 #include "AlgorithmRun.hh"
31 #include "threads.hh"
32 
33 /****************************************************************/
34 /************************ GARP Run Thread ***********************/
35 
36 // TODO: modify strategy to reuse threads instead of creating a
37 // new one for every garp run
38 
40 {
41  AlgorithmRun * algRun = (AlgorithmRun *) threadData;
42 
43  algRun->createModel();
44 
45  algRun->stop();
47 }
48 
49 
50 /****************************************************************/
51 /************************* GARP Run *****************************/
52 
54  _alg( algo ),
55  _id(-1),
56  _running( false ),
57  _omission( -1.0 ),
58  _commission( -1.0 ),
59  _commission_samples( 0 ),
60  _train_sampler(),
61  _test_sampler()
62 {
63 
64 }
65 
66 /****************************************************************/
68 {
69 
70 }
71 
72 /****************************************************************/
73 int AlgorithmRun::initialize(int id, int comm_samples,
74  const SamplerPtr& train_sampler,
75  const SamplerPtr& test_sampler )
76 {
77  _id = id;
78  _commission_samples = comm_samples;
79 
80  _train_sampler = train_sampler;
81  _test_sampler = test_sampler;
82 
83  return 1;
84 }
85 
86 /****************************************************************/
88 {
89  _running = true;
91 }
92 
94 {
95  _running = false;
96  THREAD_END();
97 }
98 
99 
100 /****************************************************************/
102 {
103  return _running;
104 }
105 
106 /****************************************************************/
108 {
109  _alg->createModel( _train_sampler );
110 
111 #if 1
114 #else
115 
116  ConfusionMatrix cm;
117  cm.calculate( _alg->getModel(), _train_sampler );
118 
120 
122 #endif
123 }
124 
125 /****************************************************************/
127 { return _alg->getProgress(); }
128 
129 /****************************************************************/
131 {
132  int i;
133  double sum = 0.0;
134 
135  // TODO: check how to use absences in computing commission
136 
137  // get random points from the background to estimate
138  // area predicted present
139  bool hasAbsences = (_train_sampler->numAbsence() != 0);
140  for (i = 0; i < _commission_samples; i++) {
141 
142  Scalar value;
143  if (hasAbsences) {
144 
145  ConstOccurrencePtr occ = _train_sampler->getAbsence();
146  value = _alg->getValue(occ->environment());
147  }
148  else {
149 
150  OccurrencePtr occ = _train_sampler->getPseudoAbsence();
151  value = _alg->getValue(occ->environment());
152  }
153 
154  // discard novalue (-1); zero is irrelevant to the sum
155  if (value > 0)
156  sum += value;
157  }
158 
159  _commission = sum / (double) _commission_samples;
160 
161  return 1;
162 }
163 
164 /****************************************************************/
166 {
167  // TODO: check how to use absences in computing omission
168 
169  // test which kind of test (intrinsic or extrinsic) should be performed
170  SamplerPtr sampler;
171 
172  if (!_test_sampler)
173  { sampler = _train_sampler; }
174  else
175  { sampler = _test_sampler; }
176 
177  int nomitted = 0;
178  OccurrencesPtr presences = sampler->getPresences();
179  OccurrencesImpl::const_iterator it = presences->begin();
180  OccurrencesImpl::const_iterator end = presences->end();
181 
182  while (it != end)
183  {
184  nomitted = !_alg->getValue((*it)->environment());
185  ++it;
186  }
187 
188  _omission = (double) nomitted / (double) presences->numOccurrences();
189 
190  return 1;
191 }
192 
193 /****************************************************************/
195 { return _omission; }
196 
197 /****************************************************************/
199 { return _commission; }
200 
201 /****************************************************************/
202 double AlgorithmRun::getError(int type) const
203 {
204  if (!type)
205  { return _omission; }
206  else
207  { return _commission; }
208 }
209 
210 /****************************************************************/
211 double AlgorithmRun::getValue(const Sample& x) const
212 { return _alg->getValue(x); }
213 
int _commission_samples
Commission error, approximated by area predicted present.
Definition: AlgorithmRun.hh:83
THREAD_PROC_RETURN_TYPE AlgorithmRunThreadProc(void *threadData)
SamplerPtr _train_sampler
Number of points used to calculate commission.
Definition: AlgorithmRun.hh:84
#define THREAD_PROC_RETURN_TYPE
Definition: threads.hh:56
double Scalar
Type of map values.
Definition: om_defs.hh:39
double _omission
Indicates whether the thread is running.
Definition: AlgorithmRun.hh:81
float getProgress() const
AlgorithmPtr _alg
Number of points used to calculate commission.
Definition: AlgorithmRun.hh:77
int calculateCommission()
double getCommissionError() const
bool _running
Identified for this particular garp run.
Definition: AlgorithmRun.hh:80
double getOmission() const
double getError(int type) const
void calculate(const EnvironmentPtr &env, const Model &model, const OccurrencesPtr &presences, const OccurrencesPtr &absences=OccurrencesPtr())
#define THREAD_PROC_RETURN_STATEMENT
Definition: threads.hh:57
double _commission
Omission error for this run.
Definition: AlgorithmRun.hh:82
#define THREAD_START(threadProc, threadData)
Definition: threads.hh:62
SamplerPtr _test_sampler
Definition: AlgorithmRun.hh:85
double getOmissionError() const
double getCommission() const
#define THREAD_END()
Definition: threads.hh:66
void createModel()
Scalar getValue(const Sample &x) const
int calculateOmission()
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
bool running() const
int _id
Algorithm used in this run.
Definition: AlgorithmRun.hh:79
Definition: Sample.hh:25
int initialize(int id, int comm_samples, const SamplerPtr &train_sampler, const SamplerPtr &test_sampler)