5 #include "getopts/getopts.h"
17 int main(
int argc,
char **argv ) {
23 opts.addOption(
"v",
"version" ,
"Display the version info" ,
false );
24 opts.addOption(
"r",
"xml-req" ,
"(option 1) Model evaluation request file in XML" ,
true );
25 opts.addOption(
"n",
"num-points" ,
"(option 2) Number of points to be generated" ,
true );
26 opts.addOption(
"l",
"label" ,
"(option 2) Label for the points" ,
true );
27 opts.addOption(
"q",
"seq-start" ,
"(option 2) Sequence start for points id" ,
true );
28 opts.addOption(
"m",
"mask" ,
"(option 2) Mask file" ,
true );
29 opts.addOption(
"p",
"proportion" ,
"(option 2) Proportion of absence points (decimals)" ,
true );
30 opts.addOption(
"o",
"model" ,
"(option 2) File with serialized model" ,
true );
31 opts.addOption(
"t",
"threshold" ,
"(option 2) Model threshold (default 0.5)" ,
true );
32 opts.addOption(
"" ,
"geo-unique" ,
"(option 2) Avoid repeating same coordinates" ,
false );
33 opts.addOption(
"" ,
"env-unique" ,
"(option 2) Avoid repeating same environment condition",
false );
34 opts.addOption(
"s",
"result" ,
"File to store result" ,
true );
35 opts.addOption(
"" ,
"log-level" ,
"Set the log level (debug, warn, info, error)" ,
true );
36 opts.addOption(
"" ,
"log-file" ,
"Log file" ,
true );
37 opts.addOption(
"" ,
"prog-file" ,
"File to store progress" ,
true );
38 opts.addOption(
"c",
"config-file",
"Configuration file for openModeller" ,
true );
40 std::string log_level(
"info");
41 std::string request_file;
42 std::string num_points_string;
44 std::string label(
"label");
45 std::string sequence_start_string;
46 int sequence_start = 1;
47 std::string mask_file;
48 std::string proportion_string;
49 double proportion = 1.0;
50 int num_absences_to_be_generated;
51 std::string model_file;
52 std::string threshold_string;
53 double threshold = 0.5;
54 bool geo_unique =
false;
55 bool env_unique =
false;
56 std::string result_file;
58 std::string progress_file;
59 std::string config_file;
61 if ( ! opts.parse( argc, argv ) ) {
63 opts.showHelp( argv[0] );
72 while ( ( option = opts.cycle() ) >= 0 ) {
77 printf(
"om_pseudo %s\n", om.
getVersion().c_str() );
78 printf(
"This is free software; see the source for copying conditions. There is NO\n");
79 printf(
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
83 request_file = opts.getArgs( option );
86 num_points_string = opts.getArgs( option );
89 label = opts.getArgs( option );
92 sequence_start_string = opts.getArgs( option );
95 mask_file = opts.getArgs( option );
98 proportion_string = opts.getArgs( option );
101 model_file = opts.getArgs( option );
104 threshold_string = opts.getArgs( option );
113 result_file = opts.getArgs( option );
116 log_level = opts.getArgs( option );
119 log_file = opts.getArgs( option );
122 progress_file = opts.getArgs( option );
125 config_file = opts.getArgs( option );
136 if ( ! log_file.empty() ) {
147 if ( ! config_file.empty() ) {
154 if ( request_file.empty() ) {
156 if ( num_points_string.empty() ) {
158 printf(
"Please specify the number of points to be generated\n");
162 num_points = atoi( num_points_string.c_str() );
164 if ( num_points <= 0 ) {
166 printf(
"Please specify a valid (> 0) number of points to be generated\n");
170 if ( mask_file.empty() ) {
172 printf(
"Please specify a mask file\n");
176 if ( ! sequence_start_string.empty() ) {
178 sequence_start = atoi( sequence_start_string.c_str() );
181 if ( proportion_string.empty() ) {
183 proportion_string =
"1";
186 proportion = atof( proportion_string.c_str() );
188 if ( proportion > 1.0 ) {
192 else if ( proportion < 0.0 ) {
197 if ( ! threshold_string.empty() ) {
199 threshold = atof( threshold_string.c_str() );
202 if ( threshold <= 0.0 ) {
204 printf(
"Model threshold must be greater than zero\n");
208 if ( threshold >= 1.0 ) {
210 printf(
"Model threshold must be smaller than one\n");
216 if ( ! num_points_string.empty() ) {
218 Log::instance()->
warn(
"num-points parameter will be ignored (using XML request instead)\n" );
220 if ( label.compare(
"label") != 0 ) {
222 Log::instance()->
warn(
"label parameter will be ignored (using XML request instead)\n" );
224 if ( ! sequence_start_string.empty() ) {
226 Log::instance()->
warn(
"seq-start parameter will be ignored (using XML request instead)\n" );
228 if ( ! mask_file.empty() ) {
230 Log::instance()->
warn(
"mask parameter will be ignored (using XML request instead)\n" );
232 if ( ! proportion_string.empty() ) {
234 Log::instance()->
warn(
"proportion parameter will be ignored (using XML request instead)\n" );
236 if ( ! model_file.empty() ) {
238 Log::instance()->
warn(
"model parameter will be ignored (using XML request instead)\n" );
240 if ( ! threshold_string.empty() ) {
242 Log::instance()->
warn(
"threshold parameter will be ignored (using XML request instead)\n" );
246 Log::instance()->
warn(
"geo-unique parameter will be ignored (using XML request instead)\n" );
250 Log::instance()->
warn(
"env-unique parameter will be ignored (using XML request instead)\n" );
257 if ( ! progress_file.empty() ) {
277 if ( request_file.empty() ) {
279 if ( model_file.empty() ) {
281 std::vector<std::string> categorical_layers, continuous_layers;
283 continuous_layers.push_back( mask_file );
292 if ( ! progress_file.empty() ) {
316 model = alg->getModel();
323 Log::instance()->
error(
"Could not find sampler data in the specified model file. Aborting.\n");
326 if ( ! progress_file.empty() ) {
337 env->changeMask( mask_file );
347 alg->setSampler( samp );
350 model->setNormalization( env );
364 if ( ! progress_file.empty() ) {
375 num_points = options_config->getAttributeAsInt(
"NumPoints", 0 );
377 if ( num_points <= 0 ) {
379 Log::instance()->
error(
"Please specify a valid (> 0) number of points to be generated. Aborting.\n");
382 if ( ! progress_file.empty() ) {
393 label = options_config->getAttribute(
"Label" );
403 proportion = options_config->getAttributeAsDouble(
"ProportionOfAbsences", 1.0 );
405 if ( proportion > 1.0 ) {
409 else if ( proportion < 0.0 ) {
425 ConstConfigurationPtr occ_filter_config = options_config->getSubsection(
"OccurrencesFilter",
false );
427 if ( occ_filter_config ) {
433 samp->spatiallyUnique();
436 ConstConfigurationPtr eu_config = occ_filter_config->getSubsection(
"EnvironmentallyUnique",
false );
440 samp->environmentallyUnique();
450 if ( ! progress_file.empty() ) {
459 num_absences_to_be_generated = (int)(num_points * proportion);
465 if ( num_absences_to_be_generated < num_points ) {
467 new_presences = samp->getPseudoPresences( (num_points-num_absences_to_be_generated), model, threshold, geo_unique, env_unique, sequence_start );
468 new_presences->setLabel( label );
473 if ( num_absences_to_be_generated > 0 ) {
475 new_absences = samp->getPseudoAbsences( num_absences_to_be_generated, model, threshold, geo_unique, env_unique, sequence_start+num_points-num_absences_to_be_generated );
476 new_absences->setLabel( label );
479 new_samp =
createSampler( samp->getEnvironment(), new_presences, new_absences );
483 if ( request_file.empty() ) {
486 std::streambuf * buf;
489 if ( ! result_file.empty() ) {
491 of.open( result_file.c_str() );
496 buf = std::cout.rdbuf();
499 std::ostream out(buf);
501 out <<
"#id\t" <<
"label\t" <<
"long\t" <<
"lat\t" <<
"abundance" << endl << flush;
508 while ( it != end ) {
511 out << sequence_start + i <<
"\t" << label.c_str() <<
"\t" << std::setprecision(9) << (*it)->x() <<
"\t" << (*it)->y() <<
"\t1" << endl << flush;
516 it = new_absences->begin();
517 end = new_absences->end();
520 while ( it != end ) {
523 out << sequence_start + i <<
"\t" << label.c_str() <<
"\t" << std::setprecision(9) << (*it)->x() <<
"\t" << (*it)->y() <<
"\t0" << endl << flush;
532 std::ostringstream output;
537 if ( ! result_file.empty() ) {
539 ofstream file( result_file.c_str() );
540 file << output.str();
546 std::cout << output.str().c_str() << endl << flush;
551 if ( ! progress_file.empty() ) {
557 catch ( runtime_error e ) {
560 if ( ! progress_file.empty() ) {
566 printf(
"om_pseudo: %s\n", e.what() );
static void loadConfig(const std::string configFile)
std::vector< OccurrencePtr >::iterator iterator
void warn(const char *format,...)
'Warn' level.
static ConfigurationPtr readXml(char const *filename)
static AlgorithmPtr newAlgorithm(std::string const id)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Log::Level getLogLevel(std::string level)
int main(int argc, char **argv)
void setLevel(Level level)
EnvironmentPtr createEnvironment(const std::vector< std::string > &categs, const std::vector< std::string > &maps, const std::string &mask_file)
SamplerPtr createSampler(const EnvironmentPtr &env, const OccurrencesPtr &presence, const OccurrencesPtr &absence)
void error(const char *format,...)
'Error' level.
void setupExternalResources()
void set(Level level, std::string fileName, char const *pref="")
static void writeXml(const ConstConfigurationPtr &config, char const *fileaname)
static int searchDefaultDirs()
void progressFileCallback(float progress, void *progressData)