File: test_ecs_example.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 (258 lines) | stat: -rw-r--r-- 7,012 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from math import pi

import pytest

from testutils import compare_data, tol


@pytest.fixture
def ecs_example(neuron_instance):
    """A model where something is created in one cell and diffuses to another.

    This model makes use of parameters, multicompartment reactions, an NMODL
    and the extracellular space. A substance is created in an organelle in cell1
    and leaks into the cytosol. It then enters the ECS using an NMODL mechanism,
    where it diffuses to cell 2 and enters via the same NMODL mechanism.
    """

    h, rxd, data, save_path = neuron_instance

    def make_model(alpha, lambd):
        # create cell1 where `x` will be created and leak out
        cell1 = h.Section(name="cell1")
        cell1.pt3dclear()
        cell1.pt3dadd(-2, 0, 0, 1)
        cell1.pt3dadd(-1, 0, 0, 1)
        cell1.nseg = 11
        cell1.insert("pump")

        # create cell2 where `x` will be pumped in and accumulate
        cell2 = h.Section(name="cell2")
        cell2.pt3dclear()
        cell2.pt3dadd(1, 0, 0, 1)
        cell2.pt3dadd(2, 0, 0, 1)
        cell2.nseg = 11
        cell2.insert("pump")

        # Where?
        # the intracellular spaces
        cyt = rxd.Region(
            h.allsec(),
            name="cyt",
            nrn_region="i",
            geometry=rxd.FractionalVolume(0.9, surface_fraction=1.0),
        )

        org = rxd.Region(h.allsec(), name="org", geometry=rxd.FractionalVolume(0.1))

        cyt_org_membrane = rxd.Region(
            h.allsec(),
            name="mem",
            geometry=rxd.ScalableBorder(pi / 2.0, on_cell_surface=False),
        )

        # the extracellular space
        ecs = rxd.Extracellular(
            -55, -55, -55, 55, 55, 55, dx=33, volume_fraction=alpha, tortuosity=lambd
        )

        # Who?
        x = rxd.Species(
            [cyt, org, cyt_org_membrane, ecs], name="x", d=1.0, charge=1, initial=0
        )
        Xcyt = x[cyt]
        Xorg = x[org]

        # What? - produce X in cell 1
        # parameter to limit production to cell 1
        cell1_param = rxd.Parameter(
            org, initial=lambda node: 1.0 if node.segment.sec == cell1 else 0
        )

        # production with a rate following Michaels Menton kinetics
        createX = rxd.Rate(Xorg, cell1_param[org] * 1.0 / (10.0 + Xorg))

        # leak between organelles and cytosol
        cyt_org_leak = rxd.MultiCompartmentReaction(
            Xcyt, Xorg, 1e4, 1e4, membrane=cyt_org_membrane
        )
        model = (
            cell1,
            cell2,
            cyt,
            org,
            cyt_org_membrane,
            ecs,
            x,
            Xcyt,
            Xorg,
            createX,
            cell1_param,
            createX,
            cyt_org_leak,
        )
        return model

    yield (neuron_instance, make_model)


def test_ecs_example(ecs_example):
    """Test ecs_example with fixed step methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(0.2, 1.6)
    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_cvode(ecs_example):
    """Test ecs_example with variable step methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(0.2, 1.6)
    h.CVode().active(True)
    h.CVode().atol(1e-5)

    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_alpha(ecs_example):
    """Test ecs_example with fixed step and inhomogeneous volume fraction methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 0.2, 1.6)
    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_cvode_alpha(ecs_example):
    """Test ecs_example with variable step and inhomogeneous volume fraction
    methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 0.2, 1.6)
    h.CVode().active(True)
    h.CVode().atol(1e-5)

    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_tort(ecs_example):
    """Test ecs_example with fixed step and inhomogeneous tortuosity methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 0.2, 1.6)
    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_cvode_tort(ecs_example):
    """Test ecs_example with variable step and inhomogeneous tortuosity methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 0.2, 1.6)
    h.CVode().active(True)
    h.CVode().atol(1e-5)

    h.finitialize(-65)
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_nodelists(ecs_example):
    """Test accessing species nodes with both Node1D and NodeExtracellular"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(0.2, 1.6)
    ecs, x = model[5], model[6]
    # test accessing NodeExtracellular from species with both 1D and ECS
    assert len(x.nodes(ecs)) == 64
    assert all([nd.region == ecs for nd in x.nodes(ecs)])
    # test accessing specific node by location
    nd = x[ecs].nodes((-38, 27, 27))[0]
    assert (nd.x3d, nd.y3d, nd.z3d) == (-38.5, 27.5, 27.5)


def test_ecs_example_dynamic_tort(ecs_example):
    """Test ecs_example with dynamic tortuosity"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 1.0, 1)
    h.finitialize(-65)
    (
        cell1,
        cell2,
        cyt,
        org,
        cyt_org_membrane,
        ecs,
        x,
        Xcyt,
        Xorg,
        createX,
        cell1_param,
        createX,
        cyt_org_leak,
    ) = model
    perm = rxd.Species(ecs, name="perm", initial=1.0 / 1.6**2)
    ecs.permeability = perm
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol


def test_ecs_example_dynamic_alpha(ecs_example):
    """Test ecs_example with fixed step and inhomogeneous tortuosity methods"""

    (h, rxd, data, save_path), make_model = ecs_example
    model = make_model(lambda x, y, z: 1.0, 1.6)
    h.finitialize(-65)
    (
        cell1,
        cell2,
        cyt,
        org,
        cyt_org_membrane,
        ecs,
        x,
        Xcyt,
        Xorg,
        createX,
        cell1_param,
        createX,
        cyt_org_leak,
    ) = model
    alpha = rxd.Species(ecs, name="alpha", initial=0.2)
    ecs.alpha = alpha
    h.continuerun(1000)

    if not save_path:
        max_err = compare_data(data)
        assert max_err < tol