Skip to content

Commit

Permalink
Add test for code export
Browse files Browse the repository at this point in the history
  • Loading branch information
PGijsbers committed Aug 25, 2022
1 parent 9c53cc2 commit db74a53
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/source/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Maintenance:
- doc build and deploy
- pre-commit check
- check for changelog
- Small changes to avoid FutureWarnings and/or DeprecationWarnings.

Bugfixes:
- #137: raise an output if ``output_directory`` is non-empty.
Expand Down
8 changes: 2 additions & 6 deletions gama/postprocessing/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(
self,
metric,
y: pd.DataFrame,
evaluation_library: EvaluationLibrary = None,
evaluation_library: EvaluationLibrary,
shrink_on_pickle=True,
downsample_to: Optional[int] = 10_000,
use_top_n_only: Optional[int] = 200,
Expand Down Expand Up @@ -166,11 +166,7 @@ def __init__(
"metric must be specified as string or `gama.ea.metrics.Metric`."
)

if evaluation_library is None:
raise ValueError(
"`evaluation_library` is None but must be EvaluationLibrary."
)
elif not isinstance(evaluation_library, EvaluationLibrary):
if not isinstance(evaluation_library, EvaluationLibrary):
raise TypeError(
"`evaluation_library` must be of type "
"gama.utilities.evaluation_library.EvaluationLibrary."
Expand Down
42 changes: 41 additions & 1 deletion tests/unit/test_auto_ensemble.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from sklearn.svm import LinearSVC
from sklearn.datasets import load_iris
from gama.postprocessing.ensemble import fit_and_weight
from sklearn.pipeline import Pipeline
from sklearn.ensemble import VotingClassifier

from gama.genetic_programming.compilers.scikitlearn import compile_individual
from gama.genetic_programming.components.individual import Individual
from gama.postprocessing.ensemble import (
EnsemblePostProcessing,
fit_and_weight,
EnsembleClassifier,
)
from gama.utilities.evaluation_library import Evaluation, EvaluationLibrary
from gama.utilities.metrics import Metric


def test_fit_and_weight():
Expand All @@ -14,3 +25,32 @@ def test_fit_and_weight():
assert 1 == w
_, w = fit_and_weight((bad_estimator, x, y, 1))
assert 0 == w


def test_code_export_produces_working_code(GNB, ForestPipeline):
x, y = load_iris(return_X_y=True, as_frame=True)

ensemble = EnsemblePostProcessing()

ensemble._ensemble = EnsembleClassifier(
Metric("neg_log_loss"),
y,
evaluation_library=EvaluationLibrary(n=None),
)
gnb = GNB
gnb._to_pipeline = compile_individual
fp = ForestPipeline
fp._to_pipeline = compile_individual
ensemble._ensemble._models = {
"a": (Evaluation(gnb), 1),
"b": (Evaluation(fp), 2),
}
ensemble._ensemble._metric = Metric("neg_log_loss")

code = ensemble.to_code()
local = {}
exec(code, {}, local)
exported_ensemble = local["ensemble"] # should be defined in exported code
assert isinstance(exported_ensemble, VotingClassifier)
exported_ensemble.fit(x, y)
assert 0.9 < exported_ensemble.score(x, y)

0 comments on commit db74a53

Please sign in to comment.