openModeller  Version 1.5.0
envelope_score.cpp
Go to the documentation of this file.
1 
31 #include "envelope_score.hh"
32 
34 
36 
37 #include <string.h>
38 #include <stdio.h>
39 #include <math.h>
40 
41 /****************************************************************/
42 /********************** Algorithm's Metadata ********************/
43 
44 #define NUM_PARAM 0
45 
46 
47 /*************************************/
48 /*** Algorithm parameters metadata ***/
49 
50 //static AlgParamMetadata parameters[NUM_PARAM] = {};
51 
52 
53 /************************************/
54 /*** Algorithm's general metadata ***/
55 
57 
58  "ENVSCORE", // Id.
59  "Envelope Score", // Name.
60  "0.1", // Version.
61 
62  // Overview
63  "Uses the minimum and maximum observed value for each\
64  environmental variable to define bioclimatic envelopes.\
65  Probability of occurrence for a point is determined by\
66  the number of environmental variables within which the\
67  environmental values at the point fall into the min/max\
68  criteria.",
69  // Description.
70  "Implements a lax Bioclimatic Envelope Algorithm. For\
71  each given environmental variable the algorithm finds\
72  the minimum and maximum at all occurrence sites. During\
73  model projection, the probability of occurrences is\
74  determined as:\n\n\
75  p = layers within min-max threshold / number of layers\n\n\
76  Thus choosing a threshold of p=1 gives you the same model\
77  output as the original Bioclim model taking both the\
78  Suitable & Marginal classes as predicted presences.\
79  The Envelope Score algorithm is equivalent to the\
80  inclusive 'OR' implementation of Bioclim described\
81  in Piņeiro et al (2007).",
82 
83  "Nix, H. A.", // Author
84 
85  // Bibliography.
86  "Nix, H.A. (1986) A biogeographic analysis of Australian elapid\
87  snakes. In: Atlas of Elapid Snakes of Australia. (Ed.) R. Longmore,\
88  pp. 4-15. Australian Flora and Fauna Series Number 7. Australian\
89  Government Publishing Service: Canberra.\n\n\
90  Piņeiro, R., Aguilar, J. F., Munt, D. D. & Feliner, G. N. (2007)\
91  Ecology matters: Atlantic-Mediterranean disjunction in the sand-dune\
92  shrub Armeria pungens (Plumbaginaceae).\
93  Molecular Ecology. 16, 2155-2171.",
94 
95  "Tim Sutton and Chris Yesson", // Code author.
96  "tim [at] linfiniti.com", // Code author's contact.
97 
98  0, // Does not accept categorical data.
99  0, // Does not need (pseudo)absence points.
100 
101  NUM_PARAM // Algorithm's parameters.
102 
103 };
104 
105 
106 
107 /****************************************************************/
108 /****************** Algorithm's factory function ****************/
109 
110 OM_ALG_DLL_EXPORT
113 {
114  return new EnvelopeScore();
115 }
116 
117 OM_ALG_DLL_EXPORT
118 AlgMetadata const *
120 {
121  return &metadata;
122 }
123 
124 
125 /****************************************************************/
126 /**************************** EnvelopeScore ***************************/
127 
128 /*******************/
129 /*** constructor ***/
130 
132  AlgorithmImpl( &metadata ),
133  _done( false ),
134  _minimum(),
135  _maximum()
136 { }
137 
138 
139 /******************/
140 /*** destructor ***/
141 
143 {
144 }
145 
146 
147 /******************/
148 /*** initialize ***/
149 int
151 {
152 
153  // Number of independent variables.
154  int dim = _samp->numIndependent();
155  Log::instance()->info( "Reading %d-dimensional occurrence points.\n", dim );
156 
157  // Check the number of sampled points.
158  int npnt = _samp->numPresence();
159  if ( npnt < 1 ) {
160  Log::instance()->error( "EnvelopeScore needs at least 1 point inside the mask!\n" );
161  return 0;
162  }
163 
164  Log::instance()->info( "Using %d points to find the bioclimatic envelope.\n", npnt );
165 
166  computeStats( _samp->getPresences() );
167 
168  _done = true;
169 
170  return 1;
171 }
172 
173 
174 /***************/
175 /*** iterate ***/
176 int
178 {
179  return 1;
180 }
181 
182 
183 /************/
184 /*** done ***/
185 int
187 {
188  // This is not an iterative algorithm.
189  return _done;
190 }
191 
192 
193 /*****************/
194 /*** get Value ***/
195 Scalar
197 {
198 
199  unsigned int myMatchCount=0;
200  unsigned int myLayerCount = x.size();
201 
202  for( unsigned int i=0; i<x.size(); i++) {
203 
204  if ( (x[i] >= _minimum[i]) && (x[i] <= _maximum[i]) ) {
205  ++myMatchCount;
206  }
207  }
208  if (myMatchCount==0) {
209  return 0;
210  }
211  Scalar myProbability = myMatchCount / static_cast<double>(myLayerCount) ;
212 
213  return myProbability;
214 }
215 
216 
217 /***********************/
218 /*** get Convergence ***/
219 int
221 {
222  *val = 1.0;
223  return 1;
224 }
225 
226 
227 /*******************/
228 /*** get Minimum ***/
229 void
231 {
232 
233  // Compute minimum and maximum.
234  {
235  OccurrencesImpl::const_iterator oc = occs->begin();
236  OccurrencesImpl::const_iterator end = occs->end();
237 
238  // Initialize _minimum and _maximum to the values of the first point
239  // and increment to get it out of the loop.
240  Sample const & sample = (*oc)->environment();
241  _minimum = sample;
242  _maximum = sample;
243 
244  ++oc;
245 
246  // For each Occurrence, update the statistics for _minimum and
247  // _maximum.
248  while ( oc != end ) {
249 
250  Sample const& sample = (*oc)->environment();
251 
252  _minimum &= sample;
253  _maximum |= sample;
254 
255  ++oc;
256  }
257 
258  }
259 
260 }
261 
262 /****************************************************************/
263 /****************** configuration *******************************/
264 void
266 {
267  if ( !_done )
268  return;
269 
270  ConfigurationPtr model_config( new ConfigurationImpl("EnvelopeScore") );
271  config->addSubsection( model_config );
272 
273  model_config->addNameValue( "Minimum", _minimum );
274  model_config->addNameValue( "Maximum", _maximum );
275 
276 }
277 
278 void
280 {
281  ConstConfigurationPtr model_config = config->getSubsection("EnvelopeScore");
282 
283  if (!model_config)
284  return;
285 
286  _done = true;
287 
288  _minimum = model_config->getAttributeAsSample( "Minimum" );
289  _maximum = model_config->getAttributeAsSample( "Maximum" );
290 
291  return;
292 }
293 
294 /********************/
295 /*** log Envelope ***/
296 void
298 {
299  Log::instance()->info( "Envelope with %d dimensions (variables).\n\n", _minimum.size() );
300 
301  for ( unsigned int i = 0; i < _minimum.size(); i++ )
302  {
303  Log::instance()->info( "Variable %02d:", i );
304  Log::instance()->info( " Minimum : %f\n", _minimum[i] );
305  Log::instance()->info( " Maximum : %f\n", _maximum[i] );
306  Log::instance()->info( "\n" );
307  }
308 }
309 
int done() const
double Scalar
Type of map values.
Definition: om_defs.hh:39
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
virtual void _setConfiguration(const ConstConfigurationPtr &)
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
Scalar getValue(const Sample &x) const
Sample _maximum
Mininum value for each variable.
#define NUM_PARAM
OM_ALG_DLL_EXPORT AlgorithmImpl * algorithmFactory()
virtual void _getConfiguration(ConfigurationPtr &) const
void computeStats(const OccurrencesPtr &)
std::size_t size() const
Definition: Sample.hh:70
SamplerPtr _samp
Definition: Algorithm.hh:245
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
int getConvergence(Scalar *const val) const
OM_ALG_DLL_EXPORT AlgMetadata const * algorithmMetadata()
static AlgMetadata metadata
Sample _minimum
is true if the algorithm is finished.
Definition: Sample.hh:25