From 45a6ba50844b9e9dc0acaf91de4c19e3e1f3717a Mon Sep 17 00:00:00 2001 From: Werner Van Geit Date: Tue, 21 Feb 2017 13:51:08 +0100 Subject: [PATCH 1/2] Improve test coverage --- Makefile | 1 + bluepyopt/ephys/mechanisms.py | 4 +- bluepyopt/ephys/parameters.py | 7 -- bluepyopt/tests/test_deapext/test_algorithms.py | 69 +++++++++++++++- bluepyopt/tests/test_deapext/test_optimisations.py | 24 +++++- bluepyopt/tests/test_ephys/test_mechanisms.py | 94 +++++++++++++++++++++- bluepyopt/tests/test_ephys/test_protocols.py | 59 ++++++++++++-- 7 files changed, 236 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index c775bbd..1a07014 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,7 @@ clean: rm -rf docs/build rm -rf bluepyopt/tests/.coverage rm -rf bluepyopt/tests/coverage.xml + rm -rf bluepyopt/tests/coverage_html rm -rf examples/l5pc/x86_64 rm -rf examples/stochkv/x86_64 find . -name "*.pyc" -exec rm -rf {} \; diff --git a/bluepyopt/ephys/mechanisms.py b/bluepyopt/ephys/mechanisms.py index 391f1e5..b5ec5b9 100644 --- a/bluepyopt/ephys/mechanisms.py +++ b/bluepyopt/ephys/mechanisms.py @@ -295,8 +295,8 @@ def instantiate(self, sim=None, icell=None): try: iclass = getattr(sim.neuron.h, self.suffix) self.pprocesses.append(iclass(icomp.x, sec=icomp.sec)) - except ValueError as e: - raise ValueError(str(e) + ': ' + self.suffix) + except AttributeError as e: + raise AttributeError(str(e) + ': ' + self.suffix) logger.debug( 'Inserted %s at %s ', self.suffix, [ diff --git a/bluepyopt/ephys/parameters.py b/bluepyopt/ephys/parameters.py index 11bd3f8..6cc6dad 100644 --- a/bluepyopt/ephys/parameters.py +++ b/bluepyopt/ephys/parameters.py @@ -31,13 +31,6 @@ # TODO location and stimulus parameters should also be optimisable -FLOAT_FORMAT = '%.17g' - - -def format_float(value): - """Format float string""" - return FLOAT_FORMAT % value - class NrnParameter(bluepyopt.parameters.Parameter): diff --git a/bluepyopt/tests/test_deapext/test_algorithms.py b/bluepyopt/tests/test_deapext/test_algorithms.py index 0305d3d..adc5173 100644 --- a/bluepyopt/tests/test_deapext/test_algorithms.py +++ b/bluepyopt/tests/test_deapext/test_algorithms.py @@ -1,6 +1,8 @@ """bluepyopt.optimisations tests""" import numpy +import mock + import deap.creator import deap.benchmarks @@ -12,8 +14,8 @@ @attr('unit') -def test_DEAPOptimisation_constructor(): - """deapext.algorithms: Testing constructor eaAlphaMuPlusLambdaCheckpoint""" +def test_eaAlphaMuPlusLambdaCheckpoint(): + """deapext.algorithms: Testing eaAlphaMuPlusLambdaCheckpoint""" deap.creator.create('fit', deap.base.Fitness, weights=(-1.0,)) deap.creator.create( @@ -30,6 +32,7 @@ def test_DEAPOptimisation_constructor(): toolbox.register("mate", lambda x, y: (x, y)) toolbox.register("mutate", lambda x: (x,)) toolbox.register("select", lambda pop, mu: pop) + toolbox.register("variate", lambda par, toolb, cxpb, mutpb: par) population, logbook, history = \ bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint( @@ -49,3 +52,65 @@ def test_DEAPOptimisation_constructor(): nt.assert_equal(len(population), 20) nt.assert_true(isinstance(logbook, deap.tools.support.Logbook)) nt.assert_true(isinstance(history, deap.tools.support.History)) + + +@attr('unit') +def test_eaAlphaMuPlusLambdaCheckpoint_with_checkpoint(): + """deapext.algorithms: Testing 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) + toolbox.register("mate", lambda x, y: (x, y)) + toolbox.register("mutate", lambda x: (x,)) + toolbox.register("select", lambda pop, mu: pop) + + with mock.patch('pickle.dump'): + with mock.patch('bluepyopt.deapext.algorithms.open', return_value=None): + population, logbook, history = \ + bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint( + population=population, + toolbox=toolbox, + mu=1.0, + cxpb=1.0, + mutpb=1.0, + ngen=2, + stats=None, + halloffame=None, + cp_frequency=1, + cp_filename='cp_test', + continue_cp=False) + + import random + with mock.patch('pickle.load', return_value={'population': population, + 'logbook': logbook, + 'history': history, + 'parents': None, + 'halloffame': None, + 'rndstate': random.getstate(), + 'generation': 1}): + with mock.patch('bluepyopt.deapext.algorithms.open', return_value=None): + new_population, logbook, history = \ + bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint( + population=population, + toolbox=toolbox, + mu=1.0, + cxpb=1.0, + mutpb=1.0, + ngen=0, + stats=None, + halloffame=None, + cp_frequency=1, + cp_filename='cp_test', + continue_cp=True) + + nt.assert_equal(new_population, population) diff --git a/bluepyopt/tests/test_deapext/test_optimisations.py b/bluepyopt/tests/test_deapext/test_optimisations.py index af230e4..55a7c25 100644 --- a/bluepyopt/tests/test_deapext/test_optimisations.py +++ b/bluepyopt/tests/test_deapext/test_optimisations.py @@ -14,16 +14,34 @@ def test_DEAPOptimisation_constructor(): "deapext.optimisation: Testing constructor DEAPOptimisation" - optimisation = bluepyopt.optimisations.DEAPOptimisation( - examples.simplecell.cell_evaluator) + optimisation = bluepyopt.deapext.optimisations.DEAPOptimisation( + examples.simplecell.cell_evaluator, map_function=map) nt.assert_is_instance( optimisation, - bluepyopt.optimisations.DEAPOptimisation) + bluepyopt.deapext.optimisations.DEAPOptimisation) nt.assert_is_instance( optimisation.evaluator, bluepyopt.evaluators.Evaluator) + nt.assert_raises( + ValueError, + bluepyopt.deapext.optimisations.DEAPOptimisation, + examples.simplecell.cell_evaluator, + selector_name='wrong') + + +@attr('unit') +def test_IBEADEAPOptimisation_constructor(): + "deapext.optimisation: Testing constructor IBEADEAPOptimisation" + + optimisation = bluepyopt.deapext.optimisations.IBEADEAPOptimisation( + examples.simplecell.cell_evaluator, map_function=map) + + nt.assert_is_instance( + optimisation, + bluepyopt.deapext.optimisations.IBEADEAPOptimisation) + @attr('unit') def test_DEAPOptimisation_run(): diff --git a/bluepyopt/tests/test_ephys/test_mechanisms.py b/bluepyopt/tests/test_ephys/test_mechanisms.py index e66a563..eed5767 100644 --- a/bluepyopt/tests/test_ephys/test_mechanisms.py +++ b/bluepyopt/tests/test_ephys/test_mechanisms.py @@ -23,9 +23,9 @@ def test_mechanism_serialize(): """ephys.mechanisms: Testing serialize""" mech = utils.make_mech() serialized = mech.to_dict() - nt.ok_(isinstance(json.dumps(serialized), str)) + nt.assert_true(isinstance(json.dumps(serialized), str)) deserialized = instantiator(serialized) - nt.ok_(isinstance(deserialized, ephys.mechanisms.NrnMODMechanism)) + nt.assert_true(isinstance(deserialized, ephys.mechanisms.NrnMODMechanism)) @attr('unit') @@ -59,6 +59,76 @@ def test_nrnmod_instantiate(): nt.assert_equal(test_mech.suffix, 'pas') + test_mech.prefix = 'pas2' + nt.assert_equal(test_mech.suffix, 'pas2') + + test_mech = ephys.mechanisms.NrnMODMechanism( + 'unknown', + suffix='unknown', + locations=[simplecell.somatic_loc]) + + simple_cell.instantiate(sim=sim) + + nt.assert_raises( + ValueError, + test_mech.instantiate, + sim=sim, + icell=simple_cell.icell) + + test_mech.destroy(sim=sim) + simple_cell.destroy(sim=sim) + + +@attr('unit') +def test_nrnmod_reinitrng_block(): + """ephys.mechanisms: Testing reinitrng_block""" + + test_mech = ephys.mechanisms.NrnMODMechanism( + 'stoch', + suffix='Stoch', + locations=[simplecell.somatic_loc]) + + block = test_mech.generate_reinitrng_hoc_block() + + nt.assert_equal(block, 'forsec somatic { deterministic_Stoch = 1 }\n') + + test_mech = ephys.mechanisms.NrnMODMechanism( + 'stoch', + suffix='Stoch', + deterministic=False, + locations=[simplecell.somatic_loc]) + + block = test_mech.generate_reinitrng_hoc_block() + + nt.assert_equal(block, 'forsec somatic { \n for (x, 0) {\n' + ' setdata_Stoch(x)\n' + ' sf.tail(secname(), "\\\\.", name)\n' + ' sprint(full_str, "%s.%.19g", name, x)\n' + ' setRNG_Stoch(0, hash_str(full_str))\n' + ' }\n }\n') + + +@attr('unit') +def test_nrnmod_determinism(): + """ephys.mechanisms: Testing determinism""" + + test_mech = ephys.mechanisms.NrnMODMechanism( + 'pas', + suffix='pas', + deterministic=False, + locations=[simplecell.somatic_loc]) + + simple_cell.instantiate(sim=sim) + + nt.assert_raises( + TypeError, + test_mech.instantiate, + sim=sim, + icell=simple_cell.icell) + test_mech.destroy(sim=sim) + + simple_cell.destroy(sim=sim) + @attr('unit') def test_pprocess_instantiate(): @@ -69,6 +139,8 @@ def test_pprocess_instantiate(): suffix='ExpSyn', locations=[simplecell.somacenter_loc]) + nt.assert_equal(str(test_pprocess), "expsyn: ExpSyn at ['somatic[0](0.5)']") + simple_cell.instantiate(sim=sim) nt.assert_equal(test_pprocess.pprocesses, None) @@ -78,12 +150,28 @@ def test_pprocess_instantiate(): pprocess = test_pprocess.pprocesses[0] nt.assert_true(hasattr(pprocess, 'tau')) - test_pprocess.destroy(sim=sim) + nt.assert_equal(test_pprocess.pprocesses, None) simple_cell.destroy(sim=sim) + test_pprocess = ephys.mechanisms.NrnMODPointProcessMechanism( + name='expsyn', + suffix='Exp', + locations=[simplecell.somacenter_loc]) + + simple_cell.instantiate(sim=sim) + + nt.assert_raises( + AttributeError, + test_pprocess.instantiate, + sim=sim, + icell=simple_cell.icell) + + test_pprocess.destroy(sim=sim) + simple_cell.destroy(sim=sim) + @attr('unit') def test_string_hash_functions(): diff --git a/bluepyopt/tests/test_ephys/test_protocols.py b/bluepyopt/tests/test_ephys/test_protocols.py index 6fe5557..f0c92f1 100644 --- a/bluepyopt/tests/test_ephys/test_protocols.py +++ b/bluepyopt/tests/test_ephys/test_protocols.py @@ -231,6 +231,44 @@ def test_sequenceprotocol_run(): @attr('unit') +def test_stepprotocol_init(): + """ephys.protocols: Test StepProtocol init""" + + soma_loc = ephys.locations.NrnSeclistCompLocation( + name='soma_loc', + seclist_name='somatic', + sec_index=0, + comp_x=.5) + + rec_soma = ephys.recordings.CompRecording( + name='soma.v', + location=soma_loc, + variable='v') + + stim = ephys.stimuli.NrnSquarePulse( + step_amplitude=0.0, + step_delay=5.0, + step_duration=50, + total_duration=50, + location=soma_loc) + hold_stim = ephys.stimuli.NrnSquarePulse( + step_amplitude=0.0, + step_delay=0.0, + step_duration=50, + total_duration=50, + location=soma_loc) + + step_protocol = ephys.protocols.StepProtocol( + name='step_prot', + step_stimulus=stim, + holding_stimulus=hold_stim, + recordings=[rec_soma]) + + nt.assert_equal(step_protocol.step_delay, 5.0) + nt.assert_equal(step_protocol.step_duration, 50) + + +@attr('unit') def test_sweepprotocol_run_unisolated(): """ephys.protocols: Test SweepProtocol unisolated run""" @@ -242,11 +280,19 @@ def test_sweepprotocol_run_unisolated(): seclist_name='somatic', sec_index=0, comp_x=.5) + unknown_loc = ephys.locations.NrnSomaDistanceCompLocation( + name='unknown_loc', + seclist_name='somatic', + soma_distance=100) rec_soma = ephys.recordings.CompRecording( name='soma.v', location=soma_loc, variable='v') + rec_unknown = ephys.recordings.CompRecording( + name='unknown.v', + location=unknown_loc, + variable='v') stim = ephys.stimuli.NrnSquarePulse( step_amplitude=0.0, @@ -258,7 +304,7 @@ def test_sweepprotocol_run_unisolated(): protocol = ephys.protocols.SweepProtocol( name='prot', stimuli=[stim], - recordings=[rec_soma]) + recordings=[rec_soma, rec_unknown]) responses = protocol.run( cell_model=dummy_cell, @@ -266,7 +312,9 @@ def test_sweepprotocol_run_unisolated(): sim=nrn_sim, isolate=False) - nt.assert_true(responses is not None) + nt.assert_true('soma.v' in responses) + nt.assert_true('unknown.v' in responses) + nt.assert_equal(responses['unknown.v'], None) protocol.destroy(sim=nrn_sim) dummy_cell.destroy(sim=nrn_sim) @@ -278,7 +326,6 @@ def test_nrnsimulator_exception(): nrn_sim = ephys.simulators.NrnSimulator() dummy_cell = testmodels.dummycells.DummyCellModel1() - # icell = dummy_cell.instantiate(sim=nrn_sim) soma_loc = ephys.locations.NrnSeclistCompLocation( name='soma_loc', seclist_name='somatic', @@ -307,7 +354,8 @@ def test_nrnsimulator_exception(): responses = protocol.run( cell_model=dummy_cell, param_values={}, - sim=nrn_sim) + sim=nrn_sim, + isolate=False) nt.assert_equal(responses['soma.v'], None) @@ -316,7 +364,8 @@ def test_nrnsimulator_exception(): responses = protocol.run( cell_model=dummy_cell, param_values={}, - sim=nrn_sim) + sim=nrn_sim, + isolate=False) nt.assert_equal(responses['soma.v'], None) From 403c98bf5c54f0e6c944b95ffcd17c470dad47f2 Mon Sep 17 00:00:00 2001 From: Werner Van Geit Date: Tue, 21 Feb 2017 13:52:35 +0100 Subject: [PATCH 2/2] Increase threshold for code coverage --- codecov.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codecov.yml b/codecov.yml index 2d91b4f..e18db62 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,9 +1,9 @@ coverage: - range: "70...100" + range: "90...100" status: project: default: - target: "70%" + target: "90%" threshold: "5%" patch: false