Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wvangeit committed Feb 21, 2017
1 parent bfaa581 commit 45a6ba5
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 22 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -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 {} \;
Expand Down
4 changes: 2 additions & 2 deletions bluepyopt/ephys/mechanisms.py
Expand Up @@ -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, [
Expand Down
7 changes: 0 additions & 7 deletions bluepyopt/ephys/parameters.py
Expand Up @@ -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):

Expand Down
69 changes: 67 additions & 2 deletions bluepyopt/tests/test_deapext/test_algorithms.py
@@ -1,6 +1,8 @@
"""bluepyopt.optimisations tests"""

import numpy
import mock

import deap.creator
import deap.benchmarks

Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)
24 changes: 21 additions & 3 deletions bluepyopt/tests/test_deapext/test_optimisations.py
Expand Up @@ -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():
Expand Down
94 changes: 91 additions & 3 deletions bluepyopt/tests/test_ephys/test_mechanisms.py
Expand Up @@ -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')
Expand Down Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -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():
Expand Down

0 comments on commit 45a6ba5

Please sign in to comment.