def algorithm_serialization(args, options): nat_env_file = args[0] nat_env_name = args[1] alg_id = args[2] occ_file = args[3] spp_name = args[4] if not omtest.checkArguments(options, "algorithm", alg_id): return ('skip', None) coordSys = "GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\", 0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]" (nat_env_list, nat_env_mask) = omtest.get_env_list(nat_env_file, nat_env_name, "../data") (presences, absences) = omtest.get_occurrences(occ_file, spp_name, coordSys, 0, 1, 2, None) alg_params = omtest.get_alg_default_params(alg_id) filename_prefix = "ser_%s_%s" % (alg_id, spp_name.replace(" ", "_")) bmp_filename = filename_prefix + "_projection.bmp" xml_filename = filename_prefix + ".xml" expected_filename = filename_prefix + "_expected.bmp" mod = om.OpenModeller() mod.setOccurrences(presences, absences) mod.setEnvironment([], nat_env_list, nat_env_mask) mod.setAlgorithm(alg_id, alg_params) mod.run() cfg = mod.getModelConfiguration() om.Configuration.writeXml(cfg, xml_filename) # write native projection (to be used as expected map) mf = om.MapFormat() mf.setFormat(om.MapFormat.GreyBMP) mod.projectNativeRange(expected_filename, mf) # now load it all from xml and try to project again mod2 = om.OpenModeller() cfg2 = om.Configuration.readXml(xml_filename) mod2.setModelConfiguration(cfg2) mf2 = om.MapFormat() mf2.setFormat(om.MapFormat.GreyBMP) mod2.projectNativeRange(bmp_filename, mf2) result = omtest.get_map_results_obj(mod, None, presences, absences, "algs/" + bmp_filename, "algs/" + expected_filename, "algs/" + xml_filename) return ('success', result)
def check_algorithm(args, options): alg_id = args if not omtest.checkArguments(options, "algorithm", alg_id): return ('skip', None) mod = om.OpenModeller() try: algMetadata = mod.algorithmMetadata(alg_id) return ('success', None) except: return ('fail', None)
def algorithm_absences(args, options): nat_env_file = args[0] nat_env_name = args[1] alg_id = args[2] uses_absences = args[3] occ_file = args[4] spp_name = args[5] # disregar whether the alg uses absences or not # test whether alg runs if absence points are present #if not uses_absences: # return ('skip', None) if not omtest.checkArguments(options, "algorithm", alg_id): return ('skip', None) coordSys = "GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\", 0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]" (nat_env_list, nat_env_mask) = omtest.get_env_list(nat_env_file, nat_env_name, "../data") (presences, absences) = omtest.get_occurrences(occ_file, spp_name, coordSys, 0, 1, 2, 3) alg_params = omtest.get_alg_default_params(alg_id) filename_prefix = "abs_%s_%s" % (alg_id, spp_name) bmp_filename = filename_prefix + ".bmp" xml_filename = filename_prefix + ".xml" expected_filename = filename_prefix + "_expected.bmp" mod = om.OpenModeller() mod.setOccurrences(presences, absences) mod.setEnvironment([], nat_env_list, nat_env_mask) mod.setAlgorithm(alg_id, alg_params) mod.run() cfg = mod.getModelConfiguration() om.Configuration.writeXml(cfg, xml_filename) mf = om.MapFormat() mf.setFormat(om.MapFormat.GreyBMP) mod.projectNativeRange(bmp_filename, mf) result = omtest.get_map_results_obj(mod, None, presences, absences, "algs/" + bmp_filename, "algs/" + expected_filename, "algs/" + xml_filename) return ('success', result)
def matrix_input(args, options): nat_env_file = args[0] nat_env_name = args[1] alg_id = args[2] occ_file = args[3] spp_name = args[4] if not omtest.checkArguments(options, "algorithm", alg_id): return ('skip', None) coordSys = "GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\", 0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]" (nat_env_list, nat_env_mask) = omtest.get_env_list(nat_env_file, nat_env_name, "../data") (presences, absences) = omtest.get_occurrences(occ_file, spp_name, coordSys, 0, 1, 2, 3) # sample environment at presence and points natEnv = om.makeEnvironment([], nat_env_list, nat_env_mask) presences.setEnvironment(natEnv) absences.setEnvironment(natEnv) alg_params = omtest.get_alg_default_params(alg_id) filename_prefix = "matrix_input_%s_%s" % (alg_id, spp_name.replace( " ", "_")) bmp_filename = filename_prefix + ".bmp" xml_filename = filename_prefix + ".xml" expected_filename = filename_prefix + "_expected.bmp" mod = om.OpenModeller() mod.setOccurrences(presences, absences) mod.setAlgorithm(alg_id, alg_params) mod.run() cfg = mod.getModelConfiguration() om.Configuration.writeXml(cfg, xml_filename) mf = om.MapFormat() mf.setFormat(om.MapFormat.GreyBMP) mod.createMap(natEnv, bmp_filename, mf) result = omtest.get_map_results_obj(mod, natEnv, presences, absences, "algs/" + bmp_filename, "algs/" + expected_filename, "algs/" + xml_filename) return ('success', result)
def get_alg_default_params(alg_id): mod = om.OpenModeller() #algParams = mod.algorithmMetadata(alg_id).getParameterList(); algParamList = [] algList = mod.availableAlgorithms() for i in range(0, len(algList)): if algList[i].id == alg_id: algParams = algList[i].getParameterList() for param in algParams: algParamList.append([param.id, param.typical]) return algParamList return None
import om # You'll need this instance for sure... mod = om.OpenModeller() # Some basic information print "\nWelcome to the openModeller Python Binding test\n" print "openModeller version", mod.getVersion() # Loop over algorithms to collect their names algList = mod.availableAlgorithms() strAlgList = "" for i in range(0, len(algList)): alg = algList[i] if i > 0: strAlgList += ", " strAlgList += alg.name print "\n", mod.numAvailableAlgorithms(), "algorithm(s) available: ", strAlgList # Display all metadata for a particular algorithm algId = 'GARP' print "\nMetadata of a particular algorithm:\n" alg = mod.algorithmMetadata(algId) acceptsCategoricalMaps = "no"
def summarize(): global success_counter, failure_counter, blow_counter, skip_counter global cur_name global report_entry_set, map_stats_entry_set print print 'Test Script: %s' % cur_name print 'Succeeded: %d' % success_counter print 'Failed: %d (%d blew exceptions)' \ % (failure_counter+blow_counter, blow_counter) print 'Skipped: %d' % skip_counter print # find whether we are in test root dir or one level up if os.path.exists(os.path.join("..", "SummaryReport.tmpl")): os.chdir("..") elif not os.path.exists("SummaryReport.tmpl"): raise "Could not find HTML template files." # Compile or load already precompiled template. template = TemplateManager().prepare("SummaryReport.tmpl") tproc = TemplateProcessor() # Set the title. mod = om.OpenModeller() tproc.set("om_version", mod.getVersion()) tproc.set("om_num_algs", mod.numAvailableAlgorithms()) # htmltmpl requires var name starting with uppercase (!?!) Entries = report_entry_set tproc.set("Entries", Entries) # get list of algorithms Algorithms = [] algList = mod.availableAlgorithms() for i in range(0, len(algList)): alg = {} alg["class"] = "a" alg["id"] = algList[i].id alg["name"] = algList[i].name Algorithms.append(alg) tproc.set("Algorithms", Algorithms) # write the processed template to disk summary_file = file("index.html", "w") summary_file.write(tproc.process(template)) summary_file.close() # Compile or load already precompiled template. template = TemplateManager().prepare("MapReport.tmpl") tproc = TemplateProcessor() # htmltmpl requires var name starting with uppercase (!?!) Mapstats = map_stats_entry_set tproc.set("Mapstats", Mapstats) # write the processed template to disk map_file = file("MapReport.html", "w") map_file.write(tproc.process(template)) map_file.close()