Skip to content

Instantly share code, notes, and snippets.

@phargogh
Created June 3, 2020 22:47
Show Gist options
  • Save phargogh/9684e631f6a3e788988a7cb5782e62b8 to your computer and use it in GitHub Desktop.
Save phargogh/9684e631f6a3e788988a7cb5782e62b8 to your computer and use it in GitHub Desktop.
InVEST model stats per model, with aliasing and separate stats for model runs and n study areas
# Usage: python model_stats.py <path to model stats GeoJSON
import sys
import collections
from osgeo import ogr
ALIASES = {
'natcap.invest.blue_carbon.blue_carbon_preprocessor': 'CBC-preprocessor',
'natcap.invest.coastal_blue_carbon.preprocessor': 'CBC-preprocessor',
'natcap.invest.coastal_blue_carbon.coastal_blue_carbon': 'CBC',
'natcap.invest.blue_carbon.blue_carbon': 'CBC',
'natcap.invest.carbon.carbon_combined': 'natcap.invest.carbon',
'natcap.invest.routing.routedem': 'natcap.invest.routedem',
'natcap.invest.routing.delineateit': 'natcap.invest.delineateit',
'natcap.invest.sdr': 'natcap.invest.sdr.sdr',
'natcap.invest.pollination.pollination': 'natcap.invest.pollination',
'natcap.invest.coastal_vulnerability.coastal_vulnerability':
'natcap.invest.coastal_vulnerability',
'natcap.invest.habitat_risk_assessment.hra': 'natcap.invest.hra',
'natcap.invest.habitat_risk_assessment.hra_preprocessor': 'HRA-pre',
'natcap.invest.habitat_quality.habitat_quality':
'natcap.invest.habitat_quality',
'natcap.invest.recreation.recmodel_client': 'natcap.invest.recreation',
}
def doit(stats_filename):
vector = ogr.Open(stats_filename)
layer = vector.GetLayer()
total_model_runs = collections.defaultdict(int)
n_study_areas = collections.defaultdict(int)
for feature in layer:
model_name = feature.GetField('model')
try:
model_name = ALIASES[model_name]
except KeyError:
pass
n_runs = feature.GetField('n_runs')
total_model_runs[model_name] += n_runs
n_study_areas[model_name] += 1
for key, n_runs in sorted(total_model_runs.items(), key=lambda x: x[1]):
print(f"{n_runs}: {key}")
for key, n_areas in sorted(n_study_areas.items(), key=lambda x: x[1]):
print(f"{n_areas}: {key}")
if __name__ == '__main__':
doit(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment