File: test_pycallobject.py

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (157 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from neuron import h, gui
from neuron.expect_hocerr import expect_err
import sys

pc = h.ParallelContext()


def callback(arg):
    print("callback: arg={}".format(arg))
    if arg == 1:
        print(1 / 0)
    if arg == 2:
        print("calling sys.exit()")
        sys.exit(0)


def test_finithandler():
    soma = h.Section(name="soma")
    fih = h.FInitializeHandler(1, (callback, (0)))
    for i in range(2):
        h.finitialize()

    fih = h.FInitializeHandler(1, (callback, (1)))
    for i in range(1):
        expect_err("h.finitialize()")

    del fih  # a mysterious consequence of expect_err is that need this
    locals()  # in order for the callback to be deleted in time for next
    if __name__ == "__main__":
        print("another test")
        fih = h.FInitializeHandler(1, (callback, (2)))
        h.finitialize()
        print("this line gets printed in interactive mode")


def efun(v):
    if v.x[0] == 100.0:
        print(1 / 0)
    if v.x[0] == 200.0:
        print("calling sys.exit()")
        sys.exit(0)
    e = v.sumsq()
    return e


def test_praxis():
    vec = h.Vector([2, 3])
    h.attr_praxis(1e-5, 0.5, 0)
    print("x={} vec={}".format(efun(vec), vec.to_python()))
    x = h.fit_praxis(efun, vec)
    print("after fit_praxis x={} vec={}".format(x, vec.to_python()))

    vec.x[0] = 100.0
    expect_err("h.fit_praxis(efun, vec)")
    if __name__ == "__main__":
        vec.x[0] = 200.0
        print("another test")
        x = h.fit_praxis(efun, vec)
        print("this line gets printed in interactive mode")


def foo(i):
    i = int(i)
    if i == 2:
        1 / 0
    if i == 3:
        sys.exit(0)
    return list(range(i))


def test_py2n_component():
    h("objref po")
    h.po = foo
    h("po = po._(1)")
    assert type(h.po) == type([])
    h.po = foo
    a = h("po = po._(2)")
    assert a == 0
    if __name__ == "__main__":
        h.po = foo
        h("po = po._(3)")
        print("this test_py2n_component line gets printed in interactive mode")
    h("objref po")


def test_func_call():
    for sec in h.allsec():
        h.delete_section(sec=sec)
    soma = h.Section(name="soma")
    dend = h.Section(name="dend")
    axon = h.Section(name="axon")
    dend.connect(soma(0))
    dend.nseg = 5
    axon.connect(soma(1))
    axon.nseg = 3
    h.topology()
    invoke = False

    def f(x):
        y = x**2
        if invoke:
            if h.cas() == dend and x == 1.0:
                1 / 0
            if h.cas() == dend and x > 0.6:
                sys.exit(0)
        print("{}({})".format(h.cas(), x))
        return y

    # all points exist
    rvp = h.RangeVarPlot(f, dend(1.0), axon(1.0))
    vec = h.Vector()
    rvp.to_vector(vec)
    invoke = True

    # but no errors as those are marked non-existent
    h.RangeVarPlot(f, dend(1.0), axon(1.0))

    # now an error
    expect_err("rvp.to_vector(vec)")

    if __name__ == "__main__":
        invoke = False
        rvp = h.RangeVarPlot(f, dend(0.9), axon(1.0))
        invoke = True
        rvp.to_vector(vec)
        print("this test_func_call line gets printed in interactive mode")

    del soma, dend, axon, rvp, vec
    locals()


def f(x):
    y = x * x
    if x == 4:
        y = 1.0 / 0.0
    return x, y


def test_call_picklef():
    print("test_call_picklef")
    pc.runworker()
    for i in range(6):
        pc.submit(f, float(i))
    try:
        while pc.working():
            x, y = pc.pyret()
            print("{} = f({})".format(y, x))
    except Exception as e:
        print(e)


if __name__ == "__main__":
    test_praxis()
    test_finithandler()
    test_py2n_component()
    test_call_picklef()
    test_func_call()