Skip to content

Commit

Permalink
Cleanup 'usecases'. (#1294)
Browse files Browse the repository at this point in the history
The new structure is to have one directory per keyword. Tests can be organized in separate test files, named test_*.py (or simulate.py if that's a better name).

Numerous tests have been polished to make them easier to read, using better names and functions to extract common functionality.
  • Loading branch information
1uc committed Jun 10, 2024
1 parent d0d8b76 commit cc44ecb
Show file tree
Hide file tree
Showing 36 changed files with 341 additions and 332 deletions.
2 changes: 1 addition & 1 deletion packaging/test_wheel.bash
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_wheel () {
nmodl -o "${OUTPUT_DIR}" "${mod}" sympy --analytic
$python_exe -c "import nmodl; driver = nmodl.NmodlDriver(); driver.parse_file('${mod}')"
done
$python_exe -m pytest -vvv "${this_dir}/../test/"
$python_exe -m pytest -vvv --ignore "${this_dir}/../test/usecases" "${this_dir}/../test/"
}

echo "== Testing $python_wheel using $python_exe ($python_ver) =="
Expand Down
14 changes: 7 additions & 7 deletions test/usecases/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
set(NMODL_USECASE_DIRS
cnexp_scalar
cnexp_array
func_proc
func_proc_pnt
global_breakpoint
cnexp
function
procedure
global
hodgkin_huxley
nonspecific_current
neuron_variables
net_event
net_move
net_receive
net_send
point_process
parameter
func_in_breakpoint
suffix
table
recursion)
useion)

foreach(usecase ${NMODL_USECASE_DIRS})
add_test(NAME usecase_${usecase}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEURON {
SUFFIX leonhard
SUFFIX cnexp_array
RANGE z
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEURON {
SUFFIX leonhard
SUFFIX cnexp_scalar
}

STATE { x }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
nseg = 1

s = h.Section()
s.insert("leonhard")
s.insert("cnexp_array")
s.nseg = nseg

x_hoc = h.Vector().record(s(0.5)._ref_x_leonhard)
x_hoc = h.Vector().record(s(0.5)._ref_x_cnexp_array)
t_hoc = h.Vector().record(h._ref_t)

h.stdinit()
Expand All @@ -24,4 +24,3 @@
rel_err = np.abs(x - x_exact) / x_exact

assert np.all(rel_err < 1e-12)
print("leonhard: success")
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
nseg = 1

s = h.Section()
s.insert("leonhard")
s.insert("cnexp_scalar")
s.nseg = nseg

x_hoc = h.Vector().record(s(0.5)._ref_x_leonhard)
x_hoc = h.Vector().record(s(0.5)._ref_x_cnexp_scalar)
t_hoc = h.Vector().record(h._ref_t)

h.stdinit()
Expand All @@ -24,4 +24,3 @@
rel_err = np.abs(x - x_exact) / x_exact

assert np.all(rel_err < 1e-12)
print("leonhard: success")
1 change: 0 additions & 1 deletion test/usecases/func_in_breakpoint/simulate.py

This file was deleted.

52 changes: 0 additions & 52 deletions test/usecases/func_proc/simulate.py

This file was deleted.

20 changes: 0 additions & 20 deletions test/usecases/func_proc_pnt/func_proc_pnt.mod

This file was deleted.

27 changes: 0 additions & 27 deletions test/usecases/func_proc_pnt/simulate.py

This file was deleted.

File renamed without changes.
24 changes: 24 additions & 0 deletions test/usecases/function/functions.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
NEURON {
SUFFIX functions
RANGE x
}

ASSIGNED {
x
}

FUNCTION x_plus_a(a) {
x_plus_a = x + a
}

FUNCTION v_plus_a(a) {
v_plus_a = v + a
}

FUNCTION identity(v) {
identity = v
}

INITIAL {
x = 1.0
}
24 changes: 24 additions & 0 deletions test/usecases/function/point_functions.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
NEURON {
POINT_PROCESS point_functions
RANGE x
}

ASSIGNED {
x
}

FUNCTION x_plus_a(a) {
x_plus_a = x + a
}

FUNCTION v_plus_a(a) {
v_plus_a = v + a
}

FUNCTION identity(v) {
identity = v
}

INITIAL {
x = 1.0
}
11 changes: 11 additions & 0 deletions test/usecases/function/recursion.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NEURON {
SUFFIX recursion
}

FUNCTION fibonacci(n) {
if (n == 0 || n == 1) {
fibonacci = 1
} else {
fibonacci = fibonacci(n-1) + fibonacci(n-2)
}
}
40 changes: 40 additions & 0 deletions test/usecases/function/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from neuron import h


def check_functions(get_instance):
for x, value in zip(coords, values):
get_instance(x).x = value

for x, value in zip(coords, values):
expected = 100.0 + value
actual = get_instance(x).x_plus_a(100.0)
assert actual == expected, f"{expected} != {actual}"

x = coords[0]
v0 = -42.0
h.finitialize(v0)

# Check `f(v)`.
expected = 42.0
actual = get_instance(x).identity(expected)
assert actual == expected, f"{actual} == {expected}"

# Check `f` using `v`.
expected = -2.0
actual = get_instance(x).v_plus_a(40.0)
assert actual == expected, f"{actual} == {expected}"


nseg = 5
s = h.Section()
s.nseg = nseg

s.insert("functions")

coords = [(0.5 + k) * 1.0 / nseg for k in range(nseg)]
values = [0.1 + k for k in range(nseg)]

point_processes = {x: h.point_functions(s(x)) for x in coords}

check_functions(lambda x: s(x).functions)
check_functions(lambda x: point_processes[x])
15 changes: 15 additions & 0 deletions test/usecases/function/test_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import math

import numpy as np
from neuron import gui
from neuron import h


s = h.Section(name="soma")
s.insert("recursion")

n = 6
actual = [s(0.5).recursion.fibonacci(i) for i in range(n)]
expected = [1, 1, 2, 3, 5, 8]

assert actual == expected, f"{actual} != {expected}"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEURON {
SUFFIX leonhard
SUFFIX read_only
GLOBAL c
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
nseg = 1

s = h.Section()
s.insert("leonhard")
s.insert("read_only")
s.nseg = nseg

x_hoc = h.Vector().record(s(0.5)._ref_x_leonhard)
x_hoc = h.Vector().record(s(0.5)._ref_x_read_only)
t_hoc = h.Vector().record(h._ref_t)

h.stdinit()
Expand All @@ -24,4 +24,3 @@
abs_err = np.abs(x - x_exact)

assert np.all(abs_err < 1e-12), f"{abs_err=}"
print("leonhard: success")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NEURON {
POINT_PROCESS test_parameter
POINT_PROCESS range_parameter
RANGE x, y
}

Expand Down
24 changes: 0 additions & 24 deletions test/usecases/parameter/simulate.py

This file was deleted.

24 changes: 24 additions & 0 deletions test/usecases/parameter/test_range_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np

from neuron import h, gui
from neuron.units import ms

s = h.Section()

pp = h.range_parameter(s(0.5))

# Defaults set in the PARAMETER block:
assert pp.x == 42.0
assert pp.y == 0.0

# Assignable:
pp.x = 42.1
assert pp.x == 42.1

h.stdinit()
h.tstop = 5.0 * ms
h.run()

# Values (not) set during the INITIAL block:
assert pp.x == 42.1
assert pp.y == 43.0
3 changes: 3 additions & 0 deletions test/usecases/point_process/pp.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NEURON {
POINT_PROCESS pp
}
Loading

0 comments on commit cc44ecb

Please sign in to comment.