def create_experiment(
        name,
        metamodel_id,
        optimization_targets,
        environment_id,
        experiment_type,
        population_size,
        budget,
        metamodel_version,
        precursor,
        algorithm_version,
        estimated_cr,
        long_tail_correction,
        sample_rate,
        target_confidence,
        audience_query_file,
        config):
    """ Create an Experiment """
    _confirm_account()

    evolv_client = EvolvClient(EVOLV_CONFIG)
    if not metamodel_version:
        # if no Metamodel version use latest
        metamodel = evolv_client.get_metamodel(metamodel_id, account_id=EVOLV_ACCOUNT_ID)
        if not metamodel:
            raise Exception("There was no Metamodel with the id {}".format(metamodel_id))
        metamodel_version = metamodel['content_version']

    audience_query = None
    content = None
    content_type = None

    if audience_query_file:
        with open(audience_query_file, 'r') as json_data:
            audience_query = json_data.read()

    if config is not None:
        assert config.endswith('.yml') or config.endswith('.json')

        content = Path(config).read_text()
        content_type = APPLICATION_YAML if config.endswith('.yml') else APPLICATION_JSON

    response = evolv_client.create_experiment(name=name, metamodel_id=metamodel_id, metamodel_version=metamodel_version,
                                              account_id=EVOLV_ACCOUNT_ID,
                                              optimization_targets=optimization_targets.split(','),
                                              environment_id=environment_id, experiment_type=experiment_type,
                                              population_size=population_size,
                                              budget=budget, precursor=precursor, algorithm_version=algorithm_version,
                                              estimated_cr=estimated_cr, long_tail_correction=long_tail_correction,
                                              sample_rate=sample_rate, target_confidence=target_confidence,
                                              audience_query=audience_query, content=content, content_type=content_type)
    _print_dict(response)
def update_metamodel(metamodel_id, file):
    """ Update the content of an existing Metamodel """
    _confirm_account()

    evolv_client = EvolvClient(EVOLV_CONFIG)
    metamodel = evolv_client.get_metamodel(metamodel_id, account_id=EVOLV_ACCOUNT_ID)
    if not metamodel:
        raise Exception("Failed to retrieve the previous metamodel.")

    response = evolv_client.update_metamodel(metamodel_id=metamodel_id, name=metamodel['name'],
                                             content=file.read().decode('utf-8'),
                                             content_type=APPLICATION_YAML,
                                             account_id=EVOLV_ACCOUNT_ID)
    _print_dict(response)