Skip to content

Commit

Permalink
add endpoint for taxon geomodel bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Sep 29, 2023
1 parent 71a8bc9 commit 57724bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
24 changes: 21 additions & 3 deletions lib/inat_inferrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def __init__(self, config):

def setup_taxonomy(self, config):
self.taxonomy = ModelTaxonomyDataframe(
config["taxonomy_path"], config["tf_elev_thresholds"]
config["taxonomy_path"],
config["tf_elev_thresholds"] if "tf_elev_thresholds" in config else None
)

def setup_vision_model(self, config):
self.vision_inferrer = VisionInferrer(config["vision_model_path"], self.taxonomy)

def setup_elevation_dataframe(self, config):
self.geo_elevation_cells = None
# load elevation data stored at H3 resolution 4
if "elevation_h3_r4" in config:
self.geo_elevation_cells = pd.read_csv(config["elevation_h3_r4"]). \
Expand All @@ -60,6 +62,8 @@ def setup_elevation_dataframe_from_worldclim(self, config, resolution):
elev_dfh3 = elev_dfh3.drop(columns=["lng", "lat"]).groupby(f'h3_0{resolution}').mean()

def setup_geo_model(self, config):
self.geo_elevation_model = None
self.geo_model_features = None
if "tf_geo_elevation_model_path" in config and self.geo_elevation_cells is not None:
self.geo_elevation_model = TFGeoPriorModelElev(config["tf_geo_elevation_model_path"])
self.geo_model_features = self.geo_elevation_model.features_for_one_class_elevation(
Expand Down Expand Up @@ -280,7 +284,7 @@ def aggregate_results(self, leaf_scores, filter_taxon, score_without_geo=False,
print("")
return all_node_scores

def h3_04_geo_results_for_taxon(self, taxon_id, bounds=[], thresholded=False):
def h3_04_geo_results_for_taxon(self, taxon_id, bounds=[], thresholded=False, raw_results=False):
if (self.geo_elevation_cells is None) or (self.geo_elevation_model is None):
return
try:
Expand Down Expand Up @@ -313,6 +317,8 @@ def h3_04_geo_results_for_taxon(self, taxon_id, bounds=[], thresholded=False):
(np.log10(geo_score_cells["geo_score"]) - math.log10(min)) / \
(math.log10(max) - math.log10(min))

if raw_results:
return geo_score_cells
return dict(zip(geo_score_cells.index.astype(str), geo_score_cells["geo_score"]))

def h3_04_taxon_range(self, taxon_id, bounds=[]):
Expand All @@ -328,7 +334,7 @@ def h3_04_taxon_range(self, taxon_id, bounds=[]):
return dict(zip(taxon_range_df.index.astype(str), taxon_range_df["value"]))

def h3_04_taxon_range_comparison(self, taxon_id, bounds=[]):
geomodel_results = self.h3_04_geo_results_for_taxon(taxon_id, bounds, True) or {}
geomodel_results = self.h3_04_geo_results_for_taxon(taxon_id, bounds, thresholded=True) or {}
taxon_range_results = self.h3_04_taxon_range(taxon_id, bounds) or {}
combined_results = {}
for cell_key in geomodel_results:
Expand All @@ -341,6 +347,18 @@ def h3_04_taxon_range_comparison(self, taxon_id, bounds=[]):
combined_results[cell_key] = 1
return combined_results

def h3_04_bounds(self, taxon_id):
geomodel_results = self.h3_04_geo_results_for_taxon(
taxon_id, bounds=None, thresholded=True, raw_results=True)
if geomodel_results is None:
return
return {
"swlat": geomodel_results["lat"].min(),
"swlng": geomodel_results["lng"].min(),
"nelat": geomodel_results["lat"].max(),
"nelng": geomodel_results["lng"].max()
}

@staticmethod
def add_lat_lng_to_h3_geo_dataframe(geo_df):
geo_df = geo_df.h3.h3_to_geo()
Expand Down
12 changes: 12 additions & 0 deletions lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def __init__(self, config):
self.h3_04_taxon_range_route, methods=["GET"])
self.app.add_url_rule("/h3_04_taxon_range_comparison", "h3_04_taxon_range_comparison",
self.h3_04_taxon_range_comparison_route, methods=["GET"])
self.app.add_url_rule("/h3_04_bounds", "h3_04_bounds",
self.h3_04_bounds_route, methods=["GET"])

def setup_inferrer(self, config):
self.inferrer = InatInferrer(config)
Expand Down Expand Up @@ -58,6 +60,16 @@ def h3_04_default_route(self, h3_04_method):
return f'Unknown taxon_id {taxon_id}', 422
return InatVisionAPI.round_floats(results_dict, 8)

def h3_04_bounds_route(self):
taxon_id, error_message, error_code = self.valid_leaf_taxon_id_for_request(request)
if error_message:
return error_message, error_code

results_dict = self.inferrer.h3_04_bounds(taxon_id)
if results_dict is None:
return f'Unknown taxon_id {taxon_id}', 422
return results_dict

def index_route(self):
form = ImageForm()
if "observation_id" in request.args:
Expand Down
3 changes: 2 additions & 1 deletion lib/model_test_data_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def process_api_response(self, api_parameters, used_observations): # noqa: C901

# must have a taxon and observed_on_details
if not row["taxon"] or "observed_on_details" not in row \
or not row["observed_on_details"] or not row["iconic_taxon_id"]:
or not row["observed_on_details"] or "taxon" not in row \
or "iconic_taxon_id" not in row["taxon"] or not row["taxon"]["iconic_taxon_id"]:
used_observations[row["uuid"]] = True
continue

Expand Down

0 comments on commit 57724bc

Please sign in to comment.