def test_pure_diffusion_3d(ics_pure_diffusion): """Test ics_pure_diffusion with fixed step methods""" neuron_instance, model = ics_pure_diffusion h, rxd, data = neuron_instance dend, r, ca = model h.dt *= 50 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_hybrid_cvode(ics_diffusion_hybrid): """Test ics_diffusion_hybrid with variable step methods""" neuron_instance, model = ics_diffusion_hybrid h, rxd, data = neuron_instance dend, r, ca = model h.CVode().active(True) h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_soma_outlines(cell_model): """Import a Neurolucida model and use the soma outline for voxelization.""" h, rxd, data, save_path, Cell = cell_model c = Cell() rxd.set_solve_type(c.all, dimension=3) cyt = rxd.Region(c.all, name="cyt", nrn_region="i", dx=1.0) ca = rxd.Species( cyt, initial=lambda node: 1 if node in c.soma[0] else 0, d=0.5, charge=2 ) h.finitialize(-65) h.continuerun(10) if not save_path: max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_anisotropic_z(ics_pure_diffusion_anisotropic): """Test anisotropic without diffusion in the z direction with fixed step methods""" neuron_instance, make_test = ics_pure_diffusion_anisotropic h, rxd, data = neuron_instance dend, r, ca = make_test([0.1, 0.1, 0]) h.dt *= 50 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_anisotropic_z_inhom(ics_pure_diffusion_anisotropic): """Test inhomogeneous anisotropic diffusion without diffusion in the z direction with fixed step methods""" neuron_instance, make_test = ics_pure_diffusion_anisotropic h, rxd, data, save_path = neuron_instance dend, r, ca = make_test(lambda nd, dr: 0 if dr == 2 else 0.1) h.dt *= 50 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() if not save_path: assert loss < tol max_err = compare_data(data) if not save_path: assert max_err < tol
def test_empty_region_multicompartment_reaction(empty_regions_model): """Test a reaction that should change the ECS with an empty region""" neuron_instance, model = empty_regions_model h, rxd, data = neuron_instance sec, cyt, mem, ecs, k = model kcyt, kecs = k[cyt], k[ecs] react = rxd.MultiCompartmentReaction(kcyt, kecs, 1e5, 0, membrane=mem, membrane_flux=False) h.finitialize(-70) h.continuerun(10) max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_anisotropic_x_cvode(ics_pure_diffusion_anisotropic): """Test anisotropic without diffusion in the x direction with variable step methods""" neuron_instance, make_test = ics_pure_diffusion_anisotropic h, rxd, data, save_path = neuron_instance dend, r, ca = make_test([0, 0.1, 0.1]) h.CVode().active(True) h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() if not save_path: assert loss < tol max_err = compare_data(data) if not save_path: assert max_err < tol
def test_soma_move(cell_model): """Import a Neurolucida model and move it.""" h, rxd, data, save_path, Cell = cell_model c = Cell(shift=(20, 10, 20)) rxd.set_solve_type(c.all, dimension=3) cyt = rxd.Region(c.all, name="cyt", nrn_region="i", dx=1.0) ca = rxd.Species( cyt, initial=lambda node: 1 if node in c.soma[0] else 0, d=0.5, charge=2 ) h.finitialize(-65) h.continuerun(10) assert min([nd.x3d for nd in ca.nodes(c.soma[0])]) > 29 if not save_path: max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_cvode(ics_pure_diffusion): """Test ics_pure_diffusion with variable step methods""" neuron_instance, model = ics_pure_diffusion h, rxd, data, save_path = neuron_instance dend, r, ca = model h.CVode().active(True) vec = h.Vector() h.CVode().states(vec) h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() if not save_path: assert loss < tol max_err = compare_data(data) if not save_path: assert max_err < tol
def test_pure_diffusion_3d_anisotropic_y_inhom_cvode( ics_pure_diffusion_anisotropic): """Test inhomogeneous anisotropic diffusion without diffusion in the y direction with variable step methods""" neuron_instance, make_test = ics_pure_diffusion_anisotropic h, rxd, data = neuron_instance dend, r, ca = make_test(lambda nd, dr: 0 if dr == 1 else 0.1) h.CVode().active(True) h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_inhom_cvode(ics_pure_diffusion): """Test ics_pure_diffusion with variable step methods and inhomogeneous diffusion coefficients. """ neuron_instance, model = ics_pure_diffusion h, rxd, data = neuron_instance dend, r, ca = model h.CVode().active(True) for nd in ca.nodes: if nd.x >= 0.5: nd.d = 0 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_cvode(ics_pure_diffusion): """Test ics_pure_diffusion with variable step methods""" neuron_instance, model = ics_pure_diffusion h, rxd, data = neuron_instance dend, r, ca = model h.CVode().active(True) vec = h.Vector() h.CVode().states(vec) print(vec.size()) h.finitialize(-65) print(vec.size(), vec.min(), vec.max(), vec.mean()) print(list(ca._intracellular_instances.values())[0]._grid_id) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_hybrid_small_grid(ics_diffusion_hybrid): """Test ics_diffusion_hybrid with fixed step methods where 1D sections are outside the 3D grid """ neuron_instance, model = ics_diffusion_hybrid h, rxd, data, save_path = neuron_instance dend, r, ca = model dend[1].diam = 0.75 h.dt *= 50 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() if not save_path: assert loss < tol max_err = compare_data(data) assert max_err < tol
def test_ecs_diffusion_3d_tort_state(ecs_diffusion): neuron_instance, make_model = ecs_diffusion ecs, k = make_model(11, 11, 11, 0.2, None, perm={ "name": "permeability", "initial": 0 }) h, rxd, data, save_path = neuron_instance for nd in ecs.permeability.nodes: nd.value = 100 / (10 + (nd.x3d + nd.y3d + nd.z3d)**2) h.finitialize(1000) h.continuerun(10) if not save_path: max_err = compare_data(data) assert max_err < tol
def test_multiple_soma(cell_model): """Create two Neurolucida models.""" h, rxd, data, save_path, Cell = cell_model c = Cell() c2 = Cell(shift=(20, 10, 20)) rxd.set_solve_type(c.all, dimension=3) cyt = rxd.Region(c.all, name="cyt", nrn_region="i", dx=1.0) ca = rxd.Species( cyt, initial=lambda node: 1 if node in c.soma[0] or node in c2.soma[0] else 0, d=0.5, charge=2, ) h.finitialize(-65) h.continuerun(10) if not save_path: max_err = compare_data(data) assert max_err < tol
def test_pure_diffusion_3d_inhom(ics_pure_diffusion): """Test ics_pure_diffusion with fixed step methods and inhomogeneous diffusion coefficients. """ neuron_instance, model = ics_pure_diffusion h, rxd, data, save_path = neuron_instance dend, r, ca = model h.dt *= 50 for nd in ca.nodes: if nd.x >= 0.5: nd.d = 0 h.finitialize(-65) loss = -(numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() h.continuerun(125) loss += (numpy.array(ca.nodes.concentration) * numpy.array(ca.nodes.volume)).sum() if not save_path: assert loss < tol max_err = compare_data(data) if not save_path: assert max_err < tol
def test_multicompartment_reactions(neuron_instance): """A tests of mulicompartment reactions using an intracellular Ca model. Test based on example copied from the RxD tutorial; http://www.neuron.yale.edu/neuron/static/docs/rxd/index.html Where 1D intracellular space is divided into cytsol and endoplasmic reticulum. Calcium is transported between the regions by multicompartment reactions for a leak, SERCA pump and IP3 receptor. """ h, rxd, data = neuron_instance sec = h.Section() sec.L = 100 sec.diam = 1 sec.nseg = 100 h.CVode().active(1) h.CVode().atol(1e-4) caDiff = 0.016 ip3Diff = 0.283 cac_init = 1.0e-4 ip3_init = 0.1 gip3r = 12040 gserca = 0.3913 gleak = 6.020 kserca = 0.1 kip3 = 0.15 kact = 0.4 ip3rtau = 2000.0 # These parameters where missing in the tutorial so arbitrary values were chosen # any resemblance to experimental values is purely coincidental. fc = 0.7 fe = 0.3 caCYT_init = 0.1 cyt = rxd.Region( h.allsec(), name='cyt', nrn_region='i', geometry=rxd.FractionalVolume(fc, surface_fraction=1), ) er = rxd.Region(h.allsec(), name='er', geometry=rxd.FractionalVolume(fe / 2.0)) cyt_er_membrane = rxd.Region(h.allsec(), name='mem', geometry=rxd.ScalableBorder( 1, on_cell_surface=False)) ca = rxd.Species([cyt, er], d=caDiff, name="ca", charge=2, initial=caCYT_init) ip3 = rxd.Species(cyt, d=ip3Diff, name="ip3", initial=ip3_init) ip3r_gate_state = rxd.Species(cyt_er_membrane, name="gate", initial=0.8) h_gate = ip3r_gate_state[cyt_er_membrane] minf = ip3[cyt] * 1000.0 * ca[cyt] / (ip3[cyt] + kip3) / (1000.0 * ca[cyt] + kact) k = gip3r * (minf * h_gate)**3 ip3r = rxd.MultiCompartmentReaction(ca[er], ca[cyt], k, k, membrane=cyt_er_membrane) serca = rxd.MultiCompartmentReaction( ca[cyt], ca[er], gserca / ((kserca / (1000.0 * ca[cyt]))**2 + 1), membrane=cyt_er_membrane, custom_dynamics=True, ) leak = rxd.MultiCompartmentReaction(ca[er], ca[cyt], gleak, gleak, membrane=cyt_er_membrane) ip3rg = rxd.Rate(h_gate, (1.0 / (1 + 1000.0 * ca[cyt] / (0.3)) - h_gate) / ip3rtau) h.finitialize(-65) cae_init = (0.0017 - cac_init * fc) / fe ca[er].concentration = cae_init for node in ip3.nodes: if node.x < 0.2: node.concentration = 2 h.CVode().re_init() h.continuerun(1000) max_err = compare_data(data) assert max_err < tol
def test_multicompartment_reactions_aligment(neuron_instance): """ A test for multicompartment reactions where one regions has more sections than the other. """ h, rxd, data, save_path = neuron_instance #Dendrite and spine dend = h.Section(name='dend') dend.nseg = 101 dend.pt3dclear() dend.pt3dadd(-10, 0, 0, 1) dend.pt3dadd(10, 0, 0, 1) spine = h.Section(name='spine') spine.nseg = 11 spine.pt3dclear() spine.pt3dadd(0, 0, 0, 0.2) spine.pt3dadd(0, 1, 0, 0.2) spine.connect(dend(0.5)) # Where -- define the shells and borders N = 5 # number of shells -- must be >= 2 # Where -- shells and border between them shells = [] border = [] for i in range(N - 1): shells.append( rxd.Region([dend], name='shell%i' % i, geometry=rxd.Shell(float(i) / N, (1.0 + i) / N))) border.append( rxd.Region([dend], name='border%i' % i, geometry=rxd.FixedPerimeter(2.0 * h.PI * (1.0 + i)))) shells.append( rxd.Region([dend, spine], nrn_region='i', name='shell%i' % (N - 1), geometry=rxd.MultipleGeometry( secs=[dend, spine], geos=[rxd.Shell((N - 1.0) / N, 1), rxd.inside]))) # Who -- calcium with an inhomogeneous initial condition Dca = 0.6 #um^2/ms # scale factor so the flux (Dca/dr)*Ca has units molecules/um^2/ms (where dr is the thickness of the shell) mM_to_mol_per_um = 6.0221409e+23 * 1e-18 ca = rxd.Species(shells, d=Dca, name='ca', charge=2, initial=lambda nd: 1000e-6 if nd.segment in spine else 100e-6) # What -- use reactions to setup diffusion between the shells cas = [] # calcium on the shells for reg in shells: cas.append(ca[reg]) # create the multi-compartment reactions between the pairs of shells diffusions = [] for i in range(N - 1): diffusions.append( rxd.MultiCompartmentReaction(cas[i], cas[i + 1], mM_to_mol_per_um * Dca, mM_to_mol_per_um * Dca, border=border[i])) h.finitialize(-65) h.continuerun(2.5) if not save_path: max_err = compare_data(data) assert max_err < tol