From 5d30066e979848ec8314d162e6836ef85718a0b9 Mon Sep 17 00:00:00 2001 From: Werner Van Geit Date: Tue, 21 Feb 2017 11:45:17 +0100 Subject: [PATCH] Bring fast test coverage to 92% --- bluepyopt/ephys/__init__.py | 1 + bluepyopt/ephys/evaluators.py | 11 ++-- bluepyopt/ephys/models.py | 15 +++-- bluepyopt/ephys/objectivescalculators.py | 3 +- bluepyopt/ephys/stimuli.py | 3 +- bluepyopt/tests/test_deapext/test_algorithms.py | 7 ++- bluepyopt/tests/test_deapext/test_selIBEA.py | 40 +++++++++++-- bluepyopt/tests/test_ephys/test_create_hoc.py | 30 +++++++--- bluepyopt/tests/test_ephys/test_evaluators.py | 5 ++ bluepyopt/tests/test_ephys/test_init.py | 10 ++++ bluepyopt/tests/test_ephys/test_mechanisms.py | 15 +++++ bluepyopt/tests/test_ephys/test_models.py | 6 ++ bluepyopt/tests/test_ephys/test_objectives.py | 6 ++ bluepyopt/tests/test_ephys/test_recordings.py | 77 +++++++++++++++++++++++++ bluepyopt/tests/test_ephys/test_stimuli.py | 8 +++ 15 files changed, 209 insertions(+), 28 deletions(-) create mode 100644 bluepyopt/tests/test_ephys/test_recordings.py diff --git a/bluepyopt/ephys/__init__.py b/bluepyopt/ephys/__init__.py index c834860..b4c9a1b 100644 --- a/bluepyopt/ephys/__init__.py +++ b/bluepyopt/ephys/__init__.py @@ -21,6 +21,7 @@ # pylint: disable=W0511 +from . import base # NOQA from . import simulators # NOQA from . import models # NOQA from . import evaluators # NOQA diff --git a/bluepyopt/ephys/evaluators.py b/bluepyopt/ephys/evaluators.py index dd38aeb..9a63ac0 100644 --- a/bluepyopt/ephys/evaluators.py +++ b/bluepyopt/ephys/evaluators.py @@ -198,13 +198,16 @@ def __str__(self): content = 'cell evaluator:\n' content += ' cell model:\n' - content += ' %s\n' % str(self.cell_model) + if self.cell_model is not None: + content += ' %s\n' % str(self.cell_model) content += ' fitness protocols:\n' - for fitness_protocol in self.fitness_protocols.values(): - content += ' %s\n' % str(fitness_protocol) + if self.fitness_protocols is not None: + for fitness_protocol in self.fitness_protocols.values(): + content += ' %s\n' % str(fitness_protocol) content += ' fitness calculator:\n' - content += ' %s\n' % str(self.fitness_calculator) + if self.fitness_calculator is not None: + content += ' %s\n' % str(self.fitness_calculator) return content diff --git a/bluepyopt/ephys/models.py b/bluepyopt/ephys/models.py index 90c8c9f..f31069f 100644 --- a/bluepyopt/ephys/models.py +++ b/bluepyopt/ephys/models.py @@ -295,14 +295,19 @@ def __str__(self): content = '%s:\n' % self.name content += ' morphology:\n' - content += ' %s\n' % str(self.morphology) + + if self.morphology is not None: + content += ' %s\n' % str(self.morphology) content += ' mechanisms:\n' - for mechanism in self.mechanisms: - content += ' %s\n' % mechanism + if self.mechanisms is not None: + for mechanism in self.mechanisms: + content += ' %s\n' % mechanism + content += ' params:\n' - for param in self.params.values(): - content += ' %s\n' % param + if self.params is not None: + for param in self.params.values(): + content += ' %s\n' % param return content diff --git a/bluepyopt/ephys/objectivescalculators.py b/bluepyopt/ephys/objectivescalculators.py index 9437df6..1f32f2a 100644 --- a/bluepyopt/ephys/objectivescalculators.py +++ b/bluepyopt/ephys/objectivescalculators.py @@ -43,4 +43,5 @@ def calculate_scores(self, responses): def __str__(self): return 'objectives:\n %s' % '\n '.join( - [str(obj) for obj in self.objectives]) + [str(obj) for obj in self.objectives]) \ + if self.objectives is not None else 'objectives:\n' diff --git a/bluepyopt/ephys/stimuli.py b/bluepyopt/ephys/stimuli.py index 2929d56..369ec04 100644 --- a/bluepyopt/ephys/stimuli.py +++ b/bluepyopt/ephys/stimuli.py @@ -152,7 +152,8 @@ def __str__(self): return "Netstim at %s" % ','.join( location - for location in self.locations) + for location in self.locations) \ + if self.locations is not None else "Netstim" # TODO Add 'current' to the name diff --git a/bluepyopt/tests/test_deapext/test_algorithms.py b/bluepyopt/tests/test_deapext/test_algorithms.py index a096f42..0305d3d 100644 --- a/bluepyopt/tests/test_deapext/test_algorithms.py +++ b/bluepyopt/tests/test_deapext/test_algorithms.py @@ -27,6 +27,9 @@ def test_DEAPOptimisation_constructor(): 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) population, logbook, history = \ bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint( @@ -35,7 +38,7 @@ def test_DEAPOptimisation_constructor(): mu=1.0, cxpb=1.0, mutpb=1.0, - ngen=1, + ngen=2, stats=None, halloffame=None, cp_frequency=1, @@ -43,6 +46,6 @@ def test_DEAPOptimisation_constructor(): continue_cp=False) nt.assert_true(isinstance(population, list)) - nt.assert_equal(len(population), 10) + nt.assert_equal(len(population), 20) 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_selIBEA.py b/bluepyopt/tests/test_deapext/test_selIBEA.py index abd42b9..7054c18 100644 --- a/bluepyopt/tests/test_deapext/test_selIBEA.py +++ b/bluepyopt/tests/test_deapext/test_selIBEA.py @@ -1,8 +1,11 @@ """selIBEA tests""" -import numpy as np -from nose.tools import ok_, eq_ +import nose.tools as nt +import deap +import numpy + +import bluepyopt.deapext from bluepyopt.deapext.tools.selIBEA \ import (_calc_fitness_components, _mating_selection,) from deapext_test_utils import make_mock_population @@ -18,7 +21,7 @@ def test_calc_fitness_components(): components = _calc_fitness_components(population, kappa=KAPPA) - expected = np.array( + expected = numpy.array( [ [1.00000000e+00, 4.30002298e-05, 4.26748513e-09, 2.06115362e-09, 9.71587289e-03], @@ -32,7 +35,7 @@ def test_calc_fitness_components(): 1.00000000e+00] ]) - ok_(np.allclose(expected, components)) + nt.assert_true(numpy.allclose(expected, components)) @attr('unit') @@ -42,6 +45,31 @@ def test_mating_selection(): PARENT_COUNT = 10 population = make_mock_population() parents = _mating_selection(population, PARENT_COUNT, 5) - eq_(len(parents), PARENT_COUNT) + nt.assert_equal(len(parents), PARENT_COUNT) expected = [1, 1, 1, 1, 1, 0, 1, 0, 0, 0] - eq_(expected, [ind.ibea_fitness for ind in parents]) + nt.assert_equal(expected, [ind.ibea_fitness for ind in parents]) + + +@attr('unit') +def test_selibea_init(): + """deapext.selIBEA: test selIBEA init""" + + deap.creator.create('fit', deap.base.Fitness, weights=(-1.0,)) + deap.creator.create( + 'ind', + numpy.ndarray, + fitness=deap.creator.__dict__['fit']) + + numpy.random.seed(1) + + population = [deap.creator.__dict__['ind'](x) + for x in numpy.random.uniform(0, 1, + (10, 2))] + + for ind in population: + ind.fitness.values = (numpy.random.uniform(0, 1), ) + + mu = 5 + parents = bluepyopt.deapext.tools.selIBEA(population, mu) + + nt.assert_equal(len(parents), mu) diff --git a/bluepyopt/tests/test_ephys/test_create_hoc.py b/bluepyopt/tests/test_ephys/test_create_hoc.py index 58c04af..b4e83f2 100644 --- a/bluepyopt/tests/test_ephys/test_create_hoc.py +++ b/bluepyopt/tests/test_ephys/test_create_hoc.py @@ -1,34 +1,46 @@ +"""Tests for create_hoc.py""" + +# pylint: disable=W0212 + import utils from bluepyopt.ephys import create_hoc import nose.tools as nt +from nose.plugins.attrib import attr +@attr('unit') def test__generate_channels_by_location(): + """ephys.create_hoc: Test _generate_channels_by_location""" mech = utils.make_mech() channels = create_hoc._generate_channels_by_location([mech, ]) - nt.eq_(len(channels['apical']), 1) - nt.eq_(len(channels['basal']), 1) + nt.assert_equal(len(channels['apical']), 1) + nt.assert_equal(len(channels['basal']), 1) - nt.eq_(channels['apical'], ['Ih']) - nt.eq_(channels['basal'], ['Ih']) + nt.assert_equal(channels['apical'], ['Ih']) + nt.assert_equal(channels['basal'], ['Ih']) +@attr('unit') def test__generate_parameters(): + """ephys.create_hoc: Test _generate_parameters""" parameters = utils.make_parameters() global_params, section_params, range_params = \ create_hoc._generate_parameters(parameters) - nt.eq_(global_params, {'NrnGlobalParameter': 65}) - nt.eq_(len(section_params[1]), 2) - nt.eq_(len(section_params[4]), 2) - nt.eq_(section_params[4][0], 'somatic') - nt.eq_(len(section_params[4][1]), 2) + nt.assert_equal(global_params, {'NrnGlobalParameter': 65}) + nt.assert_equal(len(section_params[1]), 2) + nt.assert_equal(len(section_params[4]), 2) + nt.assert_equal(section_params[4][0], 'somatic') + nt.assert_equal(len(section_params[4][1]), 2) + nt.assert_equal(range_params, []) +@attr('unit') def test_create_hoc(): + """ephys.create_hoc: Test create_hoc""" mech = utils.make_mech() parameters = utils.make_parameters() diff --git a/bluepyopt/tests/test_ephys/test_evaluators.py b/bluepyopt/tests/test_ephys/test_evaluators.py index 544e16b..a3f6440 100644 --- a/bluepyopt/tests/test_ephys/test_evaluators.py +++ b/bluepyopt/tests/test_ephys/test_evaluators.py @@ -33,6 +33,11 @@ def test_CellEvaluator_init(): sim=sim) nt.assert_true(isinstance(evaluator, ephys.evaluators.CellEvaluator)) + nt.assert_equal( + str(evaluator), + 'cell evaluator:\n cell model:\n test_model:\n morphology:\n ' + 'mechanisms:\n params:\n\n fitness protocols:\n ' + 'fitness calculator:\n objectives:\n\n') @attr('unit') diff --git a/bluepyopt/tests/test_ephys/test_init.py b/bluepyopt/tests/test_ephys/test_init.py index 6679564..abd9d6d 100644 --- a/bluepyopt/tests/test_ephys/test_init.py +++ b/bluepyopt/tests/test_ephys/test_init.py @@ -21,6 +21,7 @@ # pylint:disable=W0612 +import nose.tools as nt from nose.plugins.attrib import attr @@ -28,3 +29,12 @@ def test_import(): """ephys: test importing bluepyopt.ephys""" import bluepyopt.ephys # NOQA + + +@attr('unit') +def test_ephys_base(): + """ephys: test ephys base class""" + import bluepyopt.ephys as ephys + base = ephys.base.BaseEPhys(name='test', comment='comm') + + nt.assert_equal(str(base), 'BaseEPhys: test (comm)') diff --git a/bluepyopt/tests/test_ephys/test_mechanisms.py b/bluepyopt/tests/test_ephys/test_mechanisms.py index 4ea81b7..e66a563 100644 --- a/bluepyopt/tests/test_ephys/test_mechanisms.py +++ b/bluepyopt/tests/test_ephys/test_mechanisms.py @@ -37,6 +37,8 @@ def test_nrnmod_instantiate(): suffix='pas', locations=[simplecell.somatic_loc]) + nt.assert_equal(str(test_mech), "test.pas: pas at ['somatic']") + simple_cell.instantiate(sim=sim) test_mech.instantiate(sim=sim, icell=simple_cell.icell) @@ -44,6 +46,19 @@ def test_nrnmod_instantiate(): simple_cell.destroy(sim=sim) + nt.assert_raises(TypeError, ephys.mechanisms.NrnMODMechanism, + 'test.pas', + suffix='pas', + prefix='pas', + locations=[simplecell.somatic_loc]) + + test_mech = ephys.mechanisms.NrnMODMechanism( + 'test.pas', + prefix='pas', + locations=[simplecell.somatic_loc]) + + nt.assert_equal(test_mech.suffix, 'pas') + @attr('unit') def test_pprocess_instantiate(): diff --git a/bluepyopt/tests/test_ephys/test_models.py b/bluepyopt/tests/test_ephys/test_models.py index 8a22d23..0e75984 100644 --- a/bluepyopt/tests/test_ephys/test_models.py +++ b/bluepyopt/tests/test_ephys/test_models.py @@ -53,6 +53,12 @@ def test_model(): def test_cellmodel(): """ephys.models: Test CellModel class""" model = ephys.models.CellModel('test_model', morph=test_morph, mechs=[]) + + nt.assert_equal( + str(model), + 'test_model:\n morphology:\n %s\n mechanisms:\n params:\n' % + simple_morphology_path) + model.instantiate(sim=sim) model.destroy(sim=sim) nt.assert_true(isinstance(model, ephys.models.CellModel)) diff --git a/bluepyopt/tests/test_ephys/test_objectives.py b/bluepyopt/tests/test_ephys/test_objectives.py index f723004..7b23670 100644 --- a/bluepyopt/tests/test_ephys/test_objectives.py +++ b/bluepyopt/tests/test_ephys/test_objectives.py @@ -68,6 +68,7 @@ def test_SingletonObjective(): nt.assert_equal(s_obj.name, 'singleton') nt.assert_equal(s_obj.features, [efeature]) + nt.assert_equal(str(s_obj), '( %s )' % str(efeature)) response = ephys.responses.TimeVoltageResponse('mock_response') testdata_dir = os.path.join( @@ -172,3 +173,8 @@ def test_WeightedSumObjective(): nt.assert_almost_equal( w_obj.calculate_score(responses), abs(efeature_value - mean) * weight) + + nt.assert_raises(Exception, ephys.objectives.WeightedSumObjective, + 'weighted', + features=[efeature], + weights=[1, 2]) diff --git a/bluepyopt/tests/test_ephys/test_recordings.py b/bluepyopt/tests/test_ephys/test_recordings.py new file mode 100644 index 0000000..1d61890 --- /dev/null +++ b/bluepyopt/tests/test_ephys/test_recordings.py @@ -0,0 +1,77 @@ +"""bluepyopt.ephys.simulators tests""" + +""" +Copyright (c) 2016, EPFL/Blue Brain Project + + This file is part of BluePyOpt + + This library is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License version 3.0 as published + by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" + +# pylint:disable=W0612 + + +import nose.tools as nt +from nose.plugins.attrib import attr + +import bluepyopt.ephys as ephys + + +@attr('unit') +def test_comprecording_init(): + """ephys.recordings: Test CompRecording init""" + + recording = ephys.recordings.CompRecording() + + nt.assert_true(isinstance(recording, ephys.recordings.CompRecording)) + + nt.assert_equal(recording.response, None) + + ''' + 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', + 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=0.0, + step_duration=50, + total_duration=50, + location=soma_loc) + + protocol = ephys.protocols.SweepProtocol( + name='prot', + stimuli=[stim], + recordings=[rec_soma]) + + nt.assert_true(isinstance(protocol, ephys.protocols.SweepProtocol)) + nt.assert_equal(protocol.total_duration, 50) + nt.assert_equal( + protocol.subprotocols(), {'prot': protocol}) + + nt.assert_true('somatic[0](0.5)' in str(protocol)) + + protocol.destroy(sim=nrn_sim) + dummy_cell.destroy(sim=nrn_sim) + ''' diff --git a/bluepyopt/tests/test_ephys/test_stimuli.py b/bluepyopt/tests/test_ephys/test_stimuli.py index 60705bc..e719cf5 100644 --- a/bluepyopt/tests/test_ephys/test_stimuli.py +++ b/bluepyopt/tests/test_ephys/test_stimuli.py @@ -47,6 +47,8 @@ def test_NrnNetStimStimulus_init(): stim = ephys.stimuli.NrnNetStimStimulus(total_duration=100) nt.assert_is_instance(stim, ephys.stimuli.NrnNetStimStimulus) + nt.assert_equal(str(stim), 'Netstim') + @attr('unit') def test_NrnNetStimStimulus_instantiate(): @@ -158,6 +160,12 @@ def test_NrnRampPulse_instantiate(): ramp_duration=ramp_duration, total_duration=total_duration, location=soma_loc) + + nt.assert_equal( + str(stim), + 'Ramp pulse amp_start 0.100000 amp_end 1.000000 ' + 'delay 20.000000 duration 20.000000 totdur 50.000000' + ' at somatic[0](0.5)') stim.instantiate(sim=nrn_sim, icell=icell) recording.instantiate(sim=nrn_sim, icell=icell)