|
| 1 | +import sys |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +try: |
| 5 | + from neuroml import Morphology, Segment, Point3DWithDiam as P |
| 6 | + have_neuroml = True |
| 7 | +except ImportError: |
| 8 | + have_neuroml = False |
| 9 | + |
| 10 | +from pyNN.utility import init_logging |
| 11 | +from pyNN.morphology import NeuroMLMorphology |
| 12 | +from pyNN.parameters import IonicSpecies |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from .fixtures import run_with_simulators |
| 17 | + |
| 18 | + |
| 19 | +@run_with_simulators("arbor", "neuron") |
| 20 | +def test_scenario5(sim): |
| 21 | + """ |
| 22 | + Array of multi-compartment neurons, each injected with a different current. |
| 23 | + """ |
| 24 | + if not have_neuroml: |
| 25 | + pytest.skip("libNeuroML not available") |
| 26 | + |
| 27 | + init_logging(logfile=None, debug=True) |
| 28 | + |
| 29 | + sim.setup(timestep=0.01) |
| 30 | + |
| 31 | + soma = Segment(proximal=P(x=18.8, y=0, z=0, diameter=18.8), |
| 32 | + distal=P(x=0, y=0, z=0, diameter=18.8), |
| 33 | + name="soma", id=0) |
| 34 | + dend = Segment(proximal=P(x=0, y=0, z=0, diameter=2), |
| 35 | + distal=P(x=-500, y=0, z=0, diameter=2), |
| 36 | + name="dendrite", |
| 37 | + parent=soma, id=1) |
| 38 | + |
| 39 | + cell_class = sim.MultiCompartmentNeuron |
| 40 | + cell_class.label = "ExampleMultiCompartmentNeuron" |
| 41 | + cell_class.ion_channels = {'pas': sim.PassiveLeak, 'na': sim.NaChannel, 'kdr': sim.KdrChannel} |
| 42 | + |
| 43 | + cell_type = cell_class( |
| 44 | + morphology=NeuroMLMorphology(Morphology(segments=(soma, dend))), |
| 45 | + cm=1.0, # mF / cm**2 |
| 46 | + Ra=500.0, # ohm.cm |
| 47 | + ionic_species={ |
| 48 | + "na": IonicSpecies("na", reversal_potential=50.0), |
| 49 | + "k": IonicSpecies("k", reversal_potential=-77.0) |
| 50 | + }, |
| 51 | + pas={"conductance_density": sim.morphology.uniform('all', 0.0003), |
| 52 | + "e_rev":-54.3}, |
| 53 | + na={"conductance_density": sim.morphology.uniform('soma', 0.120)}, |
| 54 | + kdr={"conductance_density": sim.morphology.uniform('soma', 0.036)} |
| 55 | + ) |
| 56 | + |
| 57 | + neurons = sim.Population(5, cell_type, initial_values={'v': -60.0}) |
| 58 | + |
| 59 | + I = (0.04, 0.11, 0.13, 0.15, 0.18) |
| 60 | + currents = [sim.DCSource(start=50, stop=150, amplitude=amp) |
| 61 | + for amp in I] |
| 62 | + for j, (neuron, current) in enumerate(zip(neurons, currents)): |
| 63 | + if j % 2 == 0: # these should |
| 64 | + neuron.inject(current, location="soma") # be entirely |
| 65 | + else: # equivalent |
| 66 | + current.inject_into([neuron], location="soma") |
| 67 | + |
| 68 | + neurons.record('spikes') |
| 69 | + |
| 70 | + sim.run(200.0) |
| 71 | + |
| 72 | + spiketrains = neurons.get_data().segments[0].spiketrains |
| 73 | + assert len(spiketrains) == 5 |
| 74 | + assert len(spiketrains[0]) == 0 # first cell does not fire |
| 75 | + # expected values taken from the average of simulations with NEURON and Arbor |
| 76 | + expected_spike_times = [ |
| 77 | + np.array([]), |
| 78 | + np.array([52.41]), |
| 79 | + np.array([52.15, 68.45, 84.73, 101.02, 117.31, 133.61, 149.9]), |
| 80 | + np.array([51.96, 67.14, 82.13, 97.11, 112.08, 127.06, 142.04]), |
| 81 | + np.array([51.75, 65.86, 79.7, 93.51, 107.33, 121.14, 134.96, 148.77]) |
| 82 | + ] |
| 83 | + spike_times = [np.array(st) for st in spiketrains[1:]] |
| 84 | + max_error = 0 |
| 85 | + for a, b in zip(spike_times, expected_spike_times[1:]): |
| 86 | + if a.size == b.size: |
| 87 | + max_error += abs((a - b) / b).max() |
| 88 | + else: |
| 89 | + max_error += 1 |
| 90 | + print("max error =", max_error) |
| 91 | + assert max_error < 0.005, max_error |
| 92 | + sim.end() |
| 93 | + if "pytest" not in sys.modules: |
| 94 | + return a, b, spike_times |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + from pyNN.utility import get_simulator |
| 99 | + sim, args = get_simulator() |
| 100 | + test_scenario5(sim) |
0 commit comments