Skip to content

Commit

Permalink
Replace setup() with setup_method() in func tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wvangeit committed Mar 22, 2024
1 parent ae129e8 commit 1a78f86
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 89 deletions.
132 changes: 73 additions & 59 deletions bluepyopt/tests/test_l5pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import json
import os
import sys

from contextlib import contextmanager

import bluepyopt
from bluepyopt import ephys

if sys.version_info[0] < 3:
from StringIO import StringIO
else:
Expand All @@ -13,45 +16,43 @@

import pytest

L5PC_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'../../examples/l5pc'))
L5PC_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../examples/l5pc")
)
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

sys.path.insert(0, L5PC_PATH)

import bluepyopt
from bluepyopt import ephys

neuron_sim = ephys.simulators.NrnSimulator()

neuron_sim.neuron.h.nrn_load_dll(
os.path.join(
L5PC_PATH,
'x86_64/.libs/libnrnmech.so'))
os.path.join(L5PC_PATH, "x86_64/.libs/libnrnmech.so")
)


# Parameters in release circuit model
release_parameters = {
'gNaTs2_tbar_NaTs2_t.apical': 0.026145,
'gSKv3_1bar_SKv3_1.apical': 0.004226,
'gImbar_Im.apical': 0.000143,
'gNaTa_tbar_NaTa_t.axonal': 3.137968,
'gK_Tstbar_K_Tst.axonal': 0.089259,
'gamma_CaDynamics_E2.axonal': 0.002910,
'gNap_Et2bar_Nap_Et2.axonal': 0.006827,
'gSK_E2bar_SK_E2.axonal': 0.007104,
'gCa_HVAbar_Ca_HVA.axonal': 0.000990,
'gK_Pstbar_K_Pst.axonal': 0.973538,
'gSKv3_1bar_SKv3_1.axonal': 1.021945,
'decay_CaDynamics_E2.axonal': 287.198731,
'gCa_LVAstbar_Ca_LVAst.axonal': 0.008752,
'gamma_CaDynamics_E2.somatic': 0.000609,
'gSKv3_1bar_SKv3_1.somatic': 0.303472,
'gSK_E2bar_SK_E2.somatic': 0.008407,
'gCa_HVAbar_Ca_HVA.somatic': 0.000994,
'gNaTs2_tbar_NaTs2_t.somatic': 0.983955,
'decay_CaDynamics_E2.somatic': 210.485284,
'gCa_LVAstbar_Ca_LVAst.somatic': 0.000333
"gNaTs2_tbar_NaTs2_t.apical": 0.026145,
"gSKv3_1bar_SKv3_1.apical": 0.004226,
"gImbar_Im.apical": 0.000143,
"gNaTa_tbar_NaTa_t.axonal": 3.137968,
"gK_Tstbar_K_Tst.axonal": 0.089259,
"gamma_CaDynamics_E2.axonal": 0.002910,
"gNap_Et2bar_Nap_Et2.axonal": 0.006827,
"gSK_E2bar_SK_E2.axonal": 0.007104,
"gCa_HVAbar_Ca_HVA.axonal": 0.000990,
"gK_Pstbar_K_Pst.axonal": 0.973538,
"gSKv3_1bar_SKv3_1.axonal": 1.021945,
"decay_CaDynamics_E2.axonal": 287.198731,
"gCa_LVAstbar_Ca_LVAst.axonal": 0.008752,
"gamma_CaDynamics_E2.somatic": 0.000609,
"gSKv3_1bar_SKv3_1.somatic": 0.303472,
"gSK_E2bar_SK_E2.somatic": 0.008407,
"gCa_HVAbar_Ca_HVA.somatic": 0.000994,
"gNaTs2_tbar_NaTs2_t.somatic": 0.983955,
"decay_CaDynamics_E2.somatic": 210.485284,
"gCa_LVAstbar_Ca_LVAst.somatic": 0.000333,
}


Expand All @@ -65,8 +66,8 @@ def load_from_json(filename):
def dump_to_json(content, filename):
"""Dump structure to json"""

with open(filename, 'w') as json_file:
return json.dump(content, json_file, indent=4, separators=(',', ': '))
with open(filename, "w") as json_file:
return json.dump(content, json_file, indent=4, separators=(",", ": "))


def test_import():
Expand All @@ -78,60 +79,62 @@ def test_import():


class TestL5PCModel(object):

"""Test L5PC model"""

def setup(self):
def setup_method(self):
"""Set up class"""
sys.path.insert(0, L5PC_PATH)

import l5pc_model # NOQA

self.l5pc_cell = l5pc_model.create()
assert isinstance(self.l5pc_cell,
bluepyopt.ephys.models.CellModel)
assert isinstance(self.l5pc_cell, bluepyopt.ephys.models.CellModel)
self.nrn = ephys.simulators.NrnSimulator()

def test_instantiate(self):
"""L5PC: test instantiation of l5pc cell model"""
self.l5pc_cell.freeze(release_parameters)
self.l5pc_cell.instantiate(sim=self.nrn)

def teardown(self):
def teardown_method(self):
"""Teardown"""
self.l5pc_cell.destroy(sim=self.nrn)


class TestL5PCEvaluator(object):

"""Test L5PC evaluator"""

def setup(self):
def setup_method(self):
"""Set up class"""

import l5pc_evaluator # NOQA

self.l5pc_evaluator = l5pc_evaluator.create()

assert isinstance(self.l5pc_evaluator,
bluepyopt.ephys.evaluators.CellEvaluator)
assert isinstance(
self.l5pc_evaluator, bluepyopt.ephys.evaluators.CellEvaluator
)

@pytest.mark.slow
def test_eval(self):
"""L5PC: test evaluation of l5pc evaluator"""
result = self.l5pc_evaluator.evaluate_with_dicts(
param_dict=release_parameters)
param_dict=release_parameters
)

expected_results = load_from_json(
os.path.join(SCRIPT_DIR, 'expected_results.json'))
os.path.join(SCRIPT_DIR, "expected_results.json")
)

# Use two lines below to update expected result
# expected_results['TestL5PCEvaluator.test_eval'] = result
# dump_to_json(expected_results, 'expected_results.json')

assert set(result.keys(
)) == set(expected_results['TestL5PCEvaluator.test_eval'].keys())
assert set(result.keys()) == set(
expected_results["TestL5PCEvaluator.test_eval"].keys()
)

def teardown(self):
def teardown_method(self):
"""Teardown"""
pass

Expand All @@ -152,7 +155,8 @@ def stdout_redirector(stream):
def test_exec():
"""L5PC Notebook: test execution"""
import numpy
numpy.seterr(all='raise')

numpy.seterr(all="raise")
old_cwd = os.getcwd()
output = StringIO()
try:
Expand All @@ -162,14 +166,14 @@ def test_exec():
# Probably because multiprocessing doesn't work correctly during
# import
if sys.version_info[0] < 3:
execfile('L5PC.py') # NOQA
execfile("L5PC.py") # NOQA
else:
with open('L5PC.py') as l5pc_file:
exec(compile(l5pc_file.read(), 'L5PC.py', 'exec')) # NOQA
with open("L5PC.py") as l5pc_file:
exec(compile(l5pc_file.read(), "L5PC.py", "exec")) # NOQA
stdout = output.getvalue()
# first and last values of optimal individual
assert '0.001017834439738432' in stdout
assert '202.18814057682334' in stdout
assert "0.001017834439738432" in stdout
assert "202.18814057682334" in stdout
assert "'gamma_CaDynamics_E2.somatic': 0.03229357096515606" in stdout
finally:
os.chdir(old_cwd)
Expand All @@ -180,7 +184,8 @@ def test_exec():
def test_l5pc_validate_neuron_arbor():
"""L5PC Neuron/Arbor validation Notebook: test execution"""
import numpy
numpy.seterr(all='raise')

numpy.seterr(all="raise")
old_cwd = os.getcwd()
output = StringIO()
try:
Expand All @@ -190,19 +195,28 @@ def test_l5pc_validate_neuron_arbor():
# Probably because multiprocessing doesn't work correctly during
# import
if sys.version_info[0] < 3:
execfile('l5pc_validate_neuron_arbor_somatic.py') # NOQA
execfile("l5pc_validate_neuron_arbor_somatic.py") # NOQA
else:
with open('l5pc_validate_neuron_arbor_somatic.py') \
as l5pc_file:
with open(
"l5pc_validate_neuron_arbor_somatic.py"
) as l5pc_file:
l5pc_globals = {}
exec(compile(l5pc_file.read(),
'l5pc_validate_neuron_arbor_somatic.py',
'exec'), l5pc_globals, l5pc_globals) # NOQA
exec(
compile(
l5pc_file.read(),
"l5pc_validate_neuron_arbor_somatic.py",
"exec",
),
l5pc_globals,
l5pc_globals,
) # NOQA
stdout = output.getvalue()
# mean relative L1-deviation between Arbor and Neuron below tolerance
assert 'Default dt ({:,.3g}): test_l5pc OK!'.format(0.025) + \
' The mean relative Arbor-Neuron L1-deviation and error' \
assert (
"Default dt ({:,.3g}): test_l5pc OK!".format(0.025)
+ " The mean relative Arbor-Neuron L1-deviation and error"
in stdout
)
finally:
os.chdir(old_cwd)
output.close()
12 changes: 5 additions & 7 deletions bluepyopt/tests/test_lfpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import os
import sys

import pytest

L5PC_LFPY_PATH = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../../examples/l5pc_lfpy'
)
os.path.join(os.path.dirname(__file__), "../../examples/l5pc_lfpy")
)
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

Expand All @@ -30,10 +28,10 @@ def test_lfpy_evaluator():

responses = evaluator.run_protocols(
protocols=evaluator.fitness_protocols.values(),
param_values=release_params
param_values=release_params,
)
values = evaluator.fitness_calculator.calculate_values(responses)

assert len(values) == 21
assert abs(values['Step1.soma.AP_height'] - 27.85963902931001) < 1e-5
assert len(responses['Step1.MEA.v']["voltage"]) == 40
assert abs(values["Step1.soma.AP_height"] - 27.85963902931001) < 1e-5
assert len(responses["Step1.MEA.v"]["voltage"]) == 40
41 changes: 18 additions & 23 deletions bluepyopt/tests/test_simplecell.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
"""Simple cell example test class"""

import sys
import os
import sys

SIMPLECELL_PATH = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'../../examples/simplecell'))
SIMPLECELL_PATH = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../examples/simplecell")
)

# sys.path.insert(0, SIMPLECELL_PATH)


class TestSimpleCellClass(object):

"""Simple cell example test class for NEURON"""

def setup(self):
def setup_method(self):
"""Setup"""
self.old_cwd = os.getcwd()
self.old_stdout = sys.stdout

os.chdir(SIMPLECELL_PATH)
sys.stdout = open(os.devnull, 'w')
sys.stdout = open(os.devnull, "w")

@staticmethod
def test_exec():
def test_exec(self):
"""Simplecell NEURON: test execution"""
# When using import instead of execfile this doesn't work
# Probably because multiprocessing doesn't work correctly during
# import
if sys.version_info[0] < 3:
execfile('simplecell.py') # NOQA
execfile("simplecell.py") # NOQA
else:
with open('simplecell.py') as sc_file:
exec(compile(sc_file.read(), 'simplecell.py', 'exec')) # NOQA
with open("simplecell.py") as sc_file:
exec(compile(sc_file.read(), "simplecell.py", "exec")) # NOQA

def teardown(self):
def teardown_method(self):
"""Tear down"""

sys.stdout = self.old_stdout
os.chdir(self.old_cwd)


class TestSimpleCellArborClass(object):

"""Simple cell example test class for Arbor"""

def setup(self):
def setup_method(self):
"""Setup"""
self.old_cwd = os.getcwd()
self.old_stdout = sys.stdout

os.chdir(SIMPLECELL_PATH)
sys.stdout = open(os.devnull, 'w')
sys.stdout = open(os.devnull, "w")

@staticmethod
def test_exec():
def test_exec(self):
"""Simplecell Arbor: test execution"""
# When using import instead of execfile this doesn't work
# Probably because multiprocessing doesn't work correctly during
# import
if sys.version_info[0] < 3:
execfile('simplecell_arbor.py') # NOQA
execfile("simplecell_arbor.py") # NOQA
else:
with open('simplecell_arbor.py') as sc_file:
exec(compile(sc_file.read(),
'simplecell_arbor.py', 'exec')) # NOQA
with open("simplecell_arbor.py") as sc_file:
exec(compile(sc_file.read(), "simplecell_arbor.py", "exec")) # NOQA

def teardown(self):
def teardown_method(self):
"""Tear down"""

sys.stdout = self.old_stdout
Expand Down

0 comments on commit 1a78f86

Please sign in to comment.