Python functions

The modeling project must provide a Python function for each of the following tasks:

Once you've skimmed the criteria below, you can develop your functions against the cs-kit automated testing suite.

Get Version

Returns the version of the model as a string.


def get_version():
    return "1.0.0"

Model Parameters

Accepts Meta Parameters, if they are being utilized. Returns data in the form specified in the inputs page.

import matchups


def get_inputs(meta_params_dict):
    meta_params = matchups.MetaParams()
    meta_params.adjust(meta_params_dict)
    params = matchups.MatchupsParams()
    params.set_state(use_full_data=True)
    return {
        "meta_parameters": meta_params.dump(),
        "model_parameters": params.dump()
    }

Here's what you get after filling in this function: alt text

Validate user adjustments

Accepts parsed user adjustments, separated by each major section. Returns warnings/errors (if any).

Compute Studio will provide parsed user adjustments of the form:

meta_param_dict = {
    "use_full_data": True
}

adjustment = {
  "matchup": {
    "start_date": [{ "value": "2012-08-01" }],
    "end_date": [{ "value": "2012-09-01" }],
    "pitcher": [{ "value": "Not a Real Pitcher" }]
  }
}

errors_warnings = {
    "matchup": {
        "errors": {},
        "warnings": {}
    }
}

The function should return:

Warnings/Errors:

{
    "errors_warnings": {
        "matchup": {
            "errors": {
                "pitcher": ['Pitcher "Not a Real Pitcher" not allowed']
            },
            "warnings": {}
        }
    }
}
import matchups

def validate_inputs(meta_param_dict, adjustment, errors_warnings):
    # matchups doesn't look at meta_param_dict for validating inputs.
    params = matchups.MatchupsParams()
    params.adjust(adjustment["matchup"], raise_errors=False)
    errors_warnings["matchup"]["errors"].update(params.errors)
    return {"errors_warnings": errors_warnings}

Here's what you get after filling in this function: alt text

Run simulation

Accepts Meta Parameters values and model parameters. Returns outputs as specified in the outputs page

Compute Studio submits the model's meta parameters and the parsed and formatted user adjustments:

meta_param_dict = {
    "use_full_data": True
}
adjustment = {
    "matchup": {
        "pitcher": "Max Scherzer"
    },
}
import matchups

def get_matchup(meta_param_dict, adjustment):
    result = matchups.get_matchup(meta_param_dict, adjustment)
    return result

Here's what you get after filling in this function:

alt text