Integrating external models

CHAP can run external models in two ways:

  • By specifying a path to a local code base

  • or by specifying a github URL to a git repo. The url needs to start with https://github.com/

In either case, the directory or repo should be a valid MLproject directory, with an MLproject file. Se the specification in the MLflow documentation for details. In addition, we require the following:

  • An entry point in the MLproject file called train with parameters train_data and model

  • An entry point in the MLproject file called predict with parameters historic_data, future_data, model and out_file

These should contain commands that can be run to train a model and predict the future using that model. The model parameter should be used to save a model in the train step that can be read and used in the predict step. CHAP will provide all the data (the other parameters) when running a model.

Here is an example of a valid directory with an MLproject file.

The MLproject file can specify a docker image or Python virtual environment that will be used when running the commands.

Running an external model on the command line

External models can be run on the command line using the chap evaluate command. See chap evaluate –help for details.

This example runs an auto ewars R model on public ISMIP data for Brazil using a public docker image with the R inla package. After running, a report file report.pdf should be made.

chap evaluate --model-name https://github.com/sandvelab/chap_auto_ewars --dataset-name ISIMIP_dengue_harmonized --dataset-country brazil

Running an external model in Python

CHAP contains an API for loading models through Python. The following shows an example of loading and evaluating three different models by specifying paths/github urls, and evaluating those models:

from climate_health.assessment.prediction_evaluator import evaluate_model
from climate_health.external.external_model import get_model_from_directory_or_github_url
from climate_health.external.r_models import models_path
from climate_health.file_io.example_data_set import datasets

model_names= {'deepar': models_path/ 'deepar',
              'naive_model': models_path/ 'naive_python_model_with_mlproject_file',
              'ewars': 'https://github.com/sandvelab/chap_auto_ewars'}

dataset = datasets['ISIMIP_dengue_harmonized'].load()
dataset = dataset['brazil']

all_results = {}
for name, model_name in model_names.items():
    model = get_model_from_directory_or_github_url(model_name)
    results = evaluate_model(model, dataset, prediction_length=6, n_test_sets=12, report_filename=f'{name}_report.pdf')
    all_results[name] = results

print(all_results)