<commit_before>from hyperopt import fmin
import os
import inspect
import re
import sys
sys.path.append(".")


def minimize(model, data, algo, max_evals, trials):
    """Minimize a keras model for given data and implicit hyperparameters.

    Parameters
    ----------
    model: A function defining a keras model with hyperas templates, which returns a
        valid hyperopt results dictionary, e.g.
        return {'loss': -acc, 'status': STATUS_OK}
    data: A parameterless function that defines and return all data needed in the above
        model definition.
    algo: A hyperopt algorithm, like tpe.suggest or rand.suggest
    max_evals: Maximum number of optimization runs
    trials: A hyperopt trials object, used to store intermediate results for all
        optimization runs

    Returns
    -------
    A pair consisting of the results dictionary of the best run and the corresponing
    keras model.
    """
    model_string = inspect.getsource(model)
    lines = model_string.split("\n")
    raw_imports = [line.strip() + "\n" for line in lines if "import" in line]
    imports = ''.join(raw_imports)

    model_string = [line + "\n" for line in lines if "import" not in line]
    model_string = ''.join(model_string)

    parts = hyperparameter_names(model_string)
    aug_parts = augmented_names(parts)

    hyperopt_params = get_hyperparameters(model_string)
    space = get_hyperopt_space(parts, hyperopt_params)

    data_string = retrieve_data_string(data)
    model = hyperopt_keras_model(model_string, parts, aug_parts)

    write_temp_files(imports, model, data_string, space)

    try:
        from temp_model import keras_fmin_fnct, get_space
    except:
        print("Unexpected error: {}".format(sys.exc_info()[0]))
        raise
    try:
        os.remove('./temp_model.py')
        os.remove('./temp_model.pyc')
    except OSError:
        pass

    best_run = fmin(keras_fmin_fnct,
                    space=get_space(),
                    algo=algo,
                    max_evals=max_evals,
                    trials=trials)

    best_model = None
    for trial in trials:
        vals = trial.get('misc').get('vals')
        for key in vals.keys():
            vals[key] = vals[key][0]
        if trial.get('misc').get('vals') == best_run and 'model' in trial.get('result').keys():
            best_model = trial.get('result').get('model')

    return best_run, best_model

<commit_msg>Connect optim to ensembles<commit_after>from hyperopt import fmin
from .ensemble import VotingModel
import os
import inspect
import re
import sys
sys.path.append(".")


def minimize(model, data, algo, max_evals, trials):
    """Minimize a keras model for given data and implicit hyperparameters.

    Parameters
    ----------
    model: A function defining a keras model with hyperas templates, which returns a
        valid hyperopt results dictionary, e.g.
        return {'loss': -acc, 'status': STATUS_OK}
    data: A parameterless function that defines and return all data needed in the above
        model definition.
    algo: A hyperopt algorithm, like tpe.suggest or rand.suggest
    max_evals: Maximum number of optimization runs
    trials: A hyperopt trials object, used to store intermediate results for all
        optimization runs

    Returns
    -------
    A pair consisting of the results dictionary of the best run and the corresponing
    keras model.
    """
    best_run = base_minimizer(model, data, algo, max_evals, trials)

    best_model = None
    for trial in trials:
        vals = trial.get('misc').get('vals')
        for key in vals.keys():
            vals[key] = vals[key][0]
        if trial.get('misc').get('vals') == best_run and 'model' in trial.get('result').keys():
            best_model = trial.get('result').get('model')

    return best_run, best_model


def best_ensemble(nb_ensemble_models, model, data, algo, max_evals, trials, voting='hard', weights=None):
    model_list = best_models(nb_models=nb_ensemble_models, model=model,
                             data=data, algo=algo, max_evals=max_evals, trials=trials)
    return VotingModel(model_list, voting, weights)


def best_models(nb_models, model, data, algo, max_evals, trials):
    base_minimizer(model, data, algo, max_evals, trials)
    if len(trials) < nb_models:
        nb_models = len(trials)
    scores = [trial.get('misc').get('vals') for trial in trials]
    cut_off = sorted(scores, reverse=True)[nb_models-1]
    model_list = [trial.get('result').get('model') for trial in trials if trial.get('misc').get('vals') >= cut_off]
    return model_list


def base_minimizer(model, data, algo, max_evals, trials):
    model_string = inspect.getsource(model)
    lines = model_string.split("\n")
    raw_imports = [line.strip() + "\n" for line in lines if "import" in line]
    imports = ''.join(raw_imports)

    model_string = [line + "\n" for line in lines if "import" not in line]
    model_string = ''.join(model_string)

    parts = hyperparameter_names(model_string)
    aug_parts = augmented_names(parts)

    hyperopt_params = get_hyperparameters(model_string)
    space = get_hyperopt_space(parts, hyperopt_params)

    data_string = retrieve_data_string(data)
    model = hyperopt_keras_model(model_string, parts, aug_parts)

    write_temp_files(imports, model, data_string, space)

    try:
        from temp_model import keras_fmin_fnct, get_space
    except:
        print("Unexpected error: {}".format(sys.exc_info()[0]))
        raise
    try:
        os.remove('./temp_model.py')
        os.remove('./temp_model.pyc')
    except OSError:
        pass

    best_run = fmin(keras_fmin_fnct,
                    space=get_space(),
                    algo=algo,
                    max_evals=max_evals,
                    trials=trials)

    return best_run


{'fraction_non_alphanumeric': 0.10359662708655998, 'fraction_numerical': 0.0008604371020478403, 'mean_word_length': 2.935003385240352, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2307625', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1730, 'n_words': 504}