5 #include "getopts/getopts.h"
21 int main(
int argc,
char **argv ) {
27 opts.addOption(
"v",
"version" ,
"Display version info" ,
false );
28 opts.addOption(
"r",
"xml-req" ,
"(option 1) Model evaluation request file in XML",
true );
29 opts.addOption(
"o",
"model" ,
"(option 2) Serialized model file" ,
true );
30 opts.addOption(
"p",
"points" ,
"(option 2) TAB-delimited file with points" ,
true );
31 opts.addOption(
"s",
"result" ,
"File to store evaluation result in XML" ,
true );
32 opts.addOption(
"" ,
"log-level" ,
"Set the log level (debug, warn, info, error)" ,
true );
33 opts.addOption(
"" ,
"log-file" ,
"Log file" ,
true );
34 opts.addOption(
"" ,
"prog-file" ,
"File to store job progress" ,
true );
35 opts.addOption(
"c",
"config-file" ,
"Configuration file for openModeller" ,
true );
37 std::string log_level(
"info");
38 std::string request_file;
39 std::string model_file;
40 std::string points_file;
41 std::string result_file;
43 std::string progress_file;
44 std::string config_file;
46 if ( ! opts.parse( argc, argv ) ) {
48 opts.showHelp( argv[0] );
56 while ( ( option = opts.cycle() ) >= 0 ) {
61 printf(
"om_evaluate %s\n", om.
getVersion().c_str() );
62 printf(
"This is free software; see the source for copying conditions. There is NO\n");
63 printf(
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
67 request_file = opts.getArgs( option );
70 model_file = opts.getArgs( option );
73 points_file = opts.getArgs( option );
76 result_file = opts.getArgs( option );
79 log_level = opts.getArgs( option );
82 log_file = opts.getArgs( option );
85 progress_file = opts.getArgs( option );
88 config_file = opts.getArgs( option );
98 if ( ! progress_file.empty() ) {
114 if ( ! log_file.empty() ) {
125 if ( ! config_file.empty() ) {
132 if ( request_file.empty() ) {
134 if ( model_file.empty() || points_file.empty() ) {
136 printf(
"Please specify either a test request file in XML or a serialized model and a TAB-delimited file with the points to be tested\n");
139 if ( ! progress_file.empty() ) {
150 if ( ! model_file.empty() ) {
152 Log::instance()->
warn(
"Model file parameter will be ignored (using XML request instead)\n" );
154 if ( ! points_file.empty() ) {
156 Log::instance()->
warn(
"Points file parameter will be ignored (using XML request instead)\n");
176 if ( ! request_file.empty() ) {
192 env->setConfiguration( env_config );
198 presences->setConfiguration( presences_config );
204 absences->setConfiguration( absences_config );
215 Log::instance()->
debug(
"Loading training sampler to get layers, label and spatial reference\n" );
220 env = training_sampler->getEnvironment();
223 OccurrencesPtr training_presences = training_sampler->getPresences();
225 std::string label( training_presences->label() );
226 std::string spatial_ref( training_presences->coordSystem() );
228 Log::instance()->
debug(
"Loading points %s %s\n", label.c_str(), spatial_ref.c_str() );
233 absences = oc_reader->
getAbsences( label.c_str() );
238 if ( ! alg->done() ) {
240 Log::instance()->
error(
"No model could be found as part of the specified algorithm. Aborting.\n");
243 if ( ! progress_file.empty() ) {
252 if ( alg->needNormalization() ) {
254 env->normalize( alg->getNormalizer() );
259 int num_presences = 0;
260 int num_absences = 0;
264 num_presences = presences->numOccurrences();
269 num_absences = absences->numOccurrences();
272 Log::instance()->
debug(
"Loaded %d presences and %d absences\n", num_presences, num_absences );
277 Scalar * vs =
new (nothrow)
Scalar[num_presences + num_absences];
283 if ( ! progress_file.empty() ) {
295 cnt =
get_values(alg, env, presences, vs, cnt);
299 cnt =
get_values(alg, env, absences, vs, cnt);
304 output->addNameValue(
"V", vs, num_presences + num_absences, precision );
306 std::ostringstream evaluation_output;
313 if ( ! result_file.empty() ) {
315 ofstream file( result_file.c_str() );
316 file << evaluation_output.str();
322 std::cout << evaluation_output.str().c_str() << endl << flush;
326 if ( ! progress_file.empty() ) {
334 catch ( runtime_error e ) {
337 if ( ! progress_file.empty() ) {
343 printf(
"om_evaluate aborted: %s\n", e.what() );
356 if ( occs->numOccurrences() > 0 ) {
362 while ( oc != end ) {
366 if ( (*oc)->hasEnvironment() ) {
368 if ( alg->needNormalization() ) {
370 (*oc)->normalize( alg->getNormalizer(), env->numCategoricalLayers() );
373 sample = (*oc)->environment();
377 sample = env->get( (*oc)->x(), (*oc)->y() );
380 if ( sample.
size() > 0 ) {
383 val = alg->getValue( sample );
static void loadConfig(const std::string configFile)
void warn(const char *format,...)
'Warn' level.
static ConfigurationPtr readXml(char const *filename)
static AlgorithmPtr newAlgorithm(std::string const id)
double Scalar
Type of map values.
static Log * instance()
Returns the instance pointer, creating the object on the first call.
static OccurrencesFactory & instance()
int get_values(AlgorithmPtr alg, EnvironmentPtr env, OccurrencesPtr occs, Scalar *values, int cnt)
Log::Level getLogLevel(std::string level)
virtual OccurrencesPtr getAbsences(const char *groupId)
int main(int argc, char **argv)
Main code.
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()
virtual OccurrencesPtr getPresences(const char *groupId)
void set(Level level, std::string fileName, char const *pref="")
static void writeXml(const ConstConfigurationPtr &config, char const *fileaname)
static int searchDefaultDirs()
std::vector< OccurrencePtr >::const_iterator const_iterator
void debug(const char *format,...)
'Debug' level.
void progressFileCallback(float progress, void *progressData)
OccurrencesReader * create(const char *source, const char *coordSystem)