From 69975b99d1b2a8713ee790e701231add397f380f Mon Sep 17 00:00:00 2001 From: Werner Van Geit Date: Fri, 17 Feb 2017 14:21:17 +0100 Subject: [PATCH] More tests for deapext --- bluepyopt/tests/test_deapext/test_algorithms.py | 48 ++++++++++++++++++++++ bluepyopt/tests/test_deapext/test_optimisations.py | 17 ++++++++ 2 files changed, 65 insertions(+) create mode 100644 bluepyopt/tests/test_deapext/test_algorithms.py diff --git a/bluepyopt/tests/test_deapext/test_algorithms.py b/bluepyopt/tests/test_deapext/test_algorithms.py new file mode 100644 index 0000000..a096f42 --- /dev/null +++ b/bluepyopt/tests/test_deapext/test_algorithms.py @@ -0,0 +1,48 @@ +"""bluepyopt.optimisations tests""" + +import numpy +import deap.creator +import deap.benchmarks + +import nose.tools as nt + +import bluepyopt.deapext.algorithms + +from nose.plugins.attrib import attr + + +@attr('unit') +def test_DEAPOptimisation_constructor(): + """deapext.algorithms: Testing constructor eaAlphaMuPlusLambdaCheckpoint""" + + deap.creator.create('fit', deap.base.Fitness, weights=(-1.0,)) + deap.creator.create( + 'ind', + numpy.ndarray, + fitness=deap.creator.__dict__['fit']) + + population = [deap.creator.__dict__['ind'](x) + for x in numpy.random.uniform(0, 1, + (10, 2))] + + toolbox = deap.base.Toolbox() + toolbox.register("evaluate", deap.benchmarks.sphere) + + population, logbook, history = \ + bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint( + population=population, + toolbox=toolbox, + mu=1.0, + cxpb=1.0, + mutpb=1.0, + ngen=1, + stats=None, + halloffame=None, + cp_frequency=1, + cp_filename=None, + continue_cp=False) + + nt.assert_true(isinstance(population, list)) + nt.assert_equal(len(population), 10) + nt.assert_true(isinstance(logbook, deap.tools.support.Logbook)) + nt.assert_true(isinstance(history, deap.tools.support.History)) diff --git a/bluepyopt/tests/test_deapext/test_optimisations.py b/bluepyopt/tests/test_deapext/test_optimisations.py index 8f7de0d..af230e4 100644 --- a/bluepyopt/tests/test_deapext/test_optimisations.py +++ b/bluepyopt/tests/test_deapext/test_optimisations.py @@ -26,6 +26,23 @@ def test_DEAPOptimisation_constructor(): @attr('unit') +def test_DEAPOptimisation_run(): + "deapext.optimisation: Testing DEAPOptimisation run" + + optimisation = bluepyopt.optimisations.DEAPOptimisation( + examples.simplecell.cell_evaluator, offspring_size=1) + + pop, hof, log, hist = optimisation.run(max_ngen=1) + + ind = [0.06007731830843009, 0.06508319290092013] + nt.assert_equal(len(pop), 1) + nt.assert_almost_equal(pop[0], ind) + nt.assert_almost_equal(hof[0], ind) + nt.assert_equal(log[0]['nevals'], 1) + nt.assert_almost_equal(hist.genealogy_history[1], ind) + + +@attr('unit') def test_selectorname(): "deapext.optimisation: Testing selector_name argument"