示例#1
0
 def setUp(self):
     self.cell = Cell(name="cell")
     self.soma = self.cell.add_sec("soma", diam=10, l=10, nseg=10)
     self.dend1 = self.cell.add_sec("dend1", diam=1, l=10, nseg=10)
     self.dend2 = self.cell.add_sec("dend2", diam=1, l=10, nseg=10)
     self.cell.connect_secs(child=self.dend1, parent=self.soma, child_loc=0, parent_loc=0.1)
     self.cell.connect_secs(child=self.dend2, parent=self.soma, child_loc=0, parent_loc=0.9)
示例#2
0
 def __init__(self, name, compile_paths=model_path):
     """
     Experimental cell of Hay 2011 rewrited to Python
     :param name:
     :param compile_paths:
     """
     Cell.__init__(self, name=name, compile_paths=compile_paths)
示例#3
0
 def __init__(self, name=None, compile_paths=None):
     """
     :param name:
         Name of the cell
     :param compile_paths:
         paths to folders containing mods. Can be list or string separated by spaces.
     """
     CoreCell.__init__(self, name=name, compile_paths=compile_paths)
     Cell.__init__(self, name)
     CoreHocCell.__init__(self, name)
示例#4
0
    def setUpClass(cls):
        cls.cell = Cell(name="cell")
        cls.cell_name = "my_simple_cell"
        cls.cell = Cell(name=cls.cell_name)
        cls.soma = cls.cell.add_sec("soma", add_pas=True)

        cls.dend1 = cls.cell.add_sec("dend1", add_pas=True)
        cls.dend2 = cls.cell.add_sec("dend2", add_pas=True)
        cls.cell.connect_secs(cls.dend1, cls.soma)
        cls.cell.connect_secs(cls.dend2, cls.soma)
示例#5
0
class TestCellDistparam(unittest.TestCase):
    def setUp(self):
        self.cell = Cell(name="cell")

    def tearDown(self):
        self.cell.remove_immediate_from_neuron()

        l = len(list(h.allsec()))
        if len(list(h.allsec())) != 0:
            raise RuntimeError(
                "Not all section have been removed after teardown. "
                "Sections left: %s" % l)

    def test_uniform(self):
        num = 100
        Dist.set_seed(13)
        avgs = []
        vars = []
        ls = []
        for i in range(20):
            for i in range(num):
                s = self.cell.add_sec("dend", l=UniformDist(low=1, high=10))
                ls.append(s.hoc.L)
                s.remove_immediate_from_neuron()

            vars.append(np.var(ls))
            avgs.append(np.average(ls))

        self.assertEqual(5.3644, round(np.average(avgs), 4))
        self.assertEqual(7.0346, round(np.average(vars), 4))

    def test_normal(self):
        num = 100
        Dist.set_seed(13)
        avgs = []
        vars = []
        ls = []
        for i in range(20):
            for i in range(num):
                s = self.cell.add_sec("dend",
                                      l=NormalTruncatedDist(mean=5, std=2))
                ls.append(s.hoc.L)
                s.remove_immediate_from_neuron()

            vars.append(np.var(ls))
            avgs.append(np.average(ls))

        std = np.sqrt(np.average(vars))
        std_diff = round(abs(std - 2), 2)

        avg = np.average(avgs)
        avg_diff = round(abs(avg - 5), 2)

        self.assertLess(std_diff, 0.1)
        self.assertLess(avg_diff, 0.05)
示例#6
0
    def setUp(self):
        """
        Setup call after each test, however from the NEURON point of view - all created HOC
        objects exist, so each new call of setUp() creates a separated new cell on which tests are
        performed
        """
        morpho_path = os.path.join(path, "..", "commons/morphologies/asc/cell2.asc")
        self.cell = Cell(name="cell")
        self.cell.load_morpho(filepath=morpho_path)
        self.cell.insert("pas")
        self.cell.insert("hh")

        self.soma = self.cell.filter_secs("soma")
        self.apic1 = self.cell.filter_secs('apic[1]')
示例#7
0
 def cell_definition(self, **kwargs) -> Cell:
     cell = Cell(name="cell")
     morpho_path = os.path.join(path, "..",
                       "commons/morphologies/swc/my.swc")
     cell.load_morpho(filepath=morpho_path)
     cell.insert("pas")
     cell.insert("hh")
     return cell
示例#8
0
def get_cell_with_iclamps_as_synapses(warmup, dur, name, input_size, low=0.08, high=0.1):
    # Create cell
    cell = Cell(name=name)
    soma = cell.add_sec("soma", diam=10, l=10, nseg=5)
    dend = cell.add_sec("dend", diam=5, l=100, nseg=50)
    cell.connect_secs(child=dend, parent=soma)
    cell.insert("hh")

    # Make synapses
    iclamp_syns = []
    for i in range(input_size):
        iclamp = IClamp(segment=dend(np.random.rand()))
        syn = iclamp.stim(delay=warmup, dur=dur, amp=UniformDist(low=low, high=high))
        iclamp_syns.append(syn)

    # Make recording
    rec = Record(elements=soma(0.5), variables='v')
    cell.make_spike_detector(seg=soma(0.5))
    return cell, iclamp_syns, rec
示例#9
0
    def test_removal_cell_before_synapse_remove(self):
        """
        Removing whole cell after none of its content's reference is stored out of the cell - makes
        all its content and synapses removal.
        """
        cell2 = Cell(name="cell2")
        dend3 = cell2.add_sec("dend3", diam=10, l=10, nseg=10)
        cell2.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))
        dend3 = None

        cell2.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())
示例#10
0
    def test_distance(self):
        path = os.path.dirname(os.path.abspath(__file__))
        filepath = os.path.join(path, "..",
                                "commons/morphologies/asc/cell1.asc")
        cell = Cell("cell")
        cell.load_morpho(filepath=filepath)
        soma = cell.filter_secs("soma")

        # Filter sections by distance to the soma (return only those distance > 1000 um)
        far_secs = cell.filter_secs(
            obj_filter=lambda s: h.distance(soma.hoc(0.5), s.hoc(0.5)) > 1000)
        self.assertEqual(len(far_secs), 32)
示例#11
0
    def test_remove_new_cell_with_no_ref(self):
        """
        deletion of cell, when all its sections have no reference outside - deletes all its
        sections as well
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        soma = None
        dend1 = None
        dend2 = None

        self.assertEqual(6, _get_secs())
        cell.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())
示例#12
0
    def test_remove_new_cell_with_remove_immediate(self):
        """
        remove immediate removes each section immediate only if there is no reference to the HOC
        object of this section outside the Sec object.
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        self.assertEqual(6, _get_secs())

        soma.remove_immediate_from_neuron()
        dend1.remove_immediate_from_neuron()
        dend2.remove_immediate_from_neuron()

        self.assertEqual(3, _get_secs())
        cell.remove_immediate_from_neuron()
示例#13
0
    def test_remove_new_cell_with_del_ref(self):
        """
        deletion of cell, when all its outside references are deletes - deletes all its
        sections as well. But deletion of single soma won't delete section soma since its reference
        is still in Cell
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        del soma
        del dend1
        del dend2

        self.assertEqual(6, _get_secs())
        cell.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())
示例#14
0
 def cell_noname_template():
     return Cell()
示例#15
0
 def cell_name_template():
     return Cell(name="my custom name")
示例#16
0
    def __init__(self, name=None, model_folder=model_path):
        Cell.__init__(self, name, model_folder)
        CoreHocCell.__init__(self, name)

        main_file = os.path.join(model_folder, "run_PC.hoc")
        self.load_hoc(main_file, cell_template_name="PyramidalCell")
示例#17
0
 def cell_function():
     cell = Cell(name="cell")
     morpho_path = os.path.join(path, "..", "commons/morphologies/asc/cell1.asc")
     cell.load_morpho(filepath=morpho_path)
     cell.insert("pas")
     cell.insert("hh")
     cell.make_spike_detector(seg=cell.filter_secs("soma")(0.5))
     return cell
import os
from neuronpp.cells.cell import Cell
from neuronpp.utils.synaptic_debugger import SynapticDebugger

path = os.path.dirname(os.path.abspath(__file__))

if __name__ == '__main__':
    # Prepare cell
    filepath = os.path.join(path, "..", "commons/mods/sigma3syn")
    cell = Cell("cell", compile_paths=filepath)
    soma = cell.add_sec("soma", diam=20, l=20, nseg=10)
    cell.insert('pas')
    cell.insert('hh')

    w = 0.002
    ach_w = 0.1
    da_w = 0.1

    syn = cell.add_synapse(source=None,
                           netcon_weight=w,
                           seg=soma(0.5),
                           mod_name="ExcSigma3Exp2SynAchDa",
                           tau1=1,
                           tau2=5)
    pp = syn.point_process
    ach_netcon = cell.add_netcon(source=None,
                                 point_process=pp,
                                 netcon_weight=ach_w + pp.hoc.ach_substractor,
                                 delay=1)
    da_netcon = cell.add_netcon(source=None,
                                point_process=pp,
示例#19
0
class TestSimulation(unittest.TestCase):

    def setUp(self):
        """
        Setup call after each test, however from the NEURON point of view - all created HOC
        objects exist, so each new call of setUp() creates a separated new cell on which tests are
        performed
        """
        morpho_path = os.path.join(path, "..", "commons/morphologies/asc/cell2.asc")
        self.cell = Cell(name="cell")
        self.cell.load_morpho(filepath=morpho_path)
        self.cell.insert("pas")
        self.cell.insert("hh")

        self.soma = self.cell.filter_secs("soma")
        self.apic1 = self.cell.filter_secs('apic[1]')

    def tearDown(self):
        self.soma.remove_immediate_from_neuron()
        self.apic1.remove_immediate_from_neuron()
        self.cell.remove_immediate_from_neuron()

        l = len(list(h.allsec()))
        if len(list(h.allsec())) != 0:
            raise RuntimeError("Not all section have been removed after teardown. "
                               "Sections left: %s" % l)

    def test_empty_method(self):
        sim = Simulation()
        self.assertFalse(sim.is_neuron_empty())

        self.soma.remove_immediate_from_neuron()
        self.apic1.remove_immediate_from_neuron()
        self.cell.remove_immediate_from_neuron()

        self.assertTrue(sim.is_neuron_empty())

    def test_size_method(self):
        sim = Simulation()
        self.assertEqual(198, sim.size)

        self.soma.remove_immediate_from_neuron()
        self.apic1.remove_immediate_from_neuron()
        self.cell.remove_immediate_from_neuron()

        self.assertEqual(0, sim.size)

    def test_dt(self):
        rec = Record(self.soma(0.5))

        sim = Simulation(dt=10)
        sim.run(100)
        r1 = rec.as_numpy('v')

        sim = Simulation(dt=0.01)
        sim.run(100)
        r2 = rec.as_numpy('v')

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        self.assertEqual(11, r1.size)
        self.assertEqual(10001, r2.size)

    def test_init_v(self):
        rec = Record(self.soma(0.5))

        sim = Simulation(init_v=-100)
        sim.run(100)
        r1 = rec.as_numpy('v')

        sim = Simulation(init_v=100)
        sim.run(100)
        r2 = rec.as_numpy('v')

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        self.assertEqual(-100, r1.records[0])
        self.assertEqual(100, r2.records[0])

    def test_timestep_constant(self):
        rec = Record(self.soma(0.5))

        sim = Simulation(constant_timestep=True, dt=1)
        sim.run(100)
        r1 = rec.as_numpy('v')

        sim = Simulation(constant_timestep=False, dt=1)
        sim.run(100)
        r2 = rec.as_numpy('v')

        sim = Simulation(constant_timestep=False, dt=10)
        sim.run(100)
        r3 = rec.as_numpy('v')

        sim = Simulation(constant_timestep=False, dt=0.0001)
        sim.run(100)
        r4 = rec.as_numpy('v')

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        self.assertEqual(101, r1.size)
        self.assertEqual(188, r2.size)
        self.assertEqual(180, r3.size)
        self.assertEqual(189, r4.size)

    def test_warmup(self):
        rec = Record(self.soma(0.5))

        value_error = False
        try:
            sim = Simulation(warmup=-100)
            sim.run(100)
            r1 = rec.as_numpy('v')
        except ValueError:
            value_error = True
        self.assertTrue(value_error)

        sim = Simulation(warmup=100, dt=1)
        sim.run(100)
        r2 = rec.as_numpy('v')

        sim = Simulation(warmup=100, dt=1, warmup_dt=1)
        sim.run(100)
        r3 = rec.as_numpy('v')

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        self.assertEqual(111, r2.size)
        self.assertEqual(201, r3.size)

    def test_netstim_before_sim(self):
        """
        NetStim run in a regular way before Simulation run, in this cale (netcon weight=1.0)
        we have spike of the soma.

        However NetStim start is in the absolute time.
        """
        # Netstim to synapse
        stim = NetStimCell("stim").add_netstim(start=25, number=10, interval=2)
        self.cell.add_synapse(source=stim, netcon_weight=1.0, mod_name="ExpSyn", delay=1,
                              seg=self.apic1(0.5))
        # Record
        rec = Record(self.soma(0.5))
        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)

        sim.run(100)
        r = rec.as_numpy(variable="v")

        stim.remove_immediate_from_neuron()
        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(34.5205, round(r.records.max(), 4))
        self.assertEqual(-75.3053, round(r.records.min(), 4))

        self.assertEqual(319, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(27.725, round(r.time[r.records.argmax()], 4))

    def test_netcon_event_before_sim(self):
        """
        Netcon created before sim init or start is not effective and should return error
        """
        sim = Simulation()
        sim.reinit()
        syn = self.cell.add_synapse(source=None, netcon_weight=1.0, mod_name="ExpSyn", delay=1,
                                    seg=self.apic1(0.5))
        error = False
        try:
            syn.make_event(50)
        except ConnectionRefusedError:
            error = True

        self.assertTrue(error)

    def test_netcon_event_after_sim(self):
        syn = self.cell.add_synapse(source=None, netcon_weight=1.0, mod_name="ExpSyn", delay=1,
                                    seg=self.apic1(0.5))
        # Record
        rec = Record(self.soma(0.5))
        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)

        syn.make_event(50)

        sim.run(100)
        r = rec.as_numpy(variable="v")

        syn.remove_immediate_from_neuron()
        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(34.4582, round(r.records.max(), 4))
        self.assertEqual(-75.3478, round(r.records.min(), 4))

        self.assertEqual(2159, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(73.725, round(r.time[r.records.argmax()], 4))

    def test_netstim_after_sim(self):
        """
        NetStim created after simulation run has no effect, it won't go on this simulation at all.

        However NetStim start is in the absolute time.
        """
        # Record
        rec = Record(self.soma(0.5))
        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)

        # Netstim to synapse
        stim = NetStimCell("stim").add_netstim(start=25, number=10, interval=2)
        self.cell.add_synapse(source=stim, netcon_weight=0.5, mod_name="ExpSyn", delay=1,
                              seg=self.apic1(0.5))
        sim.run(100)
        r = rec.as_numpy(variable="v")

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(-67.1917, round(r.records.max(), 4))
        self.assertEqual(-70.0, round(r.records.min(), 4))

        self.assertEqual(3, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(6, r.time[r.records.argmax()])

        stim.remove_immediate_from_neuron()
        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()

    def test_iclamp_before_sim(self):
        """
        IClamp works in a regular way before simulation run. in this case (amplitude 3 pA)
        we have spike at the soma.

        However IClamp delay is in the absolute time.
        """
        # Record
        rec = Record(self.soma(0.5))

        # IClamp to soma
        iclamp = IClamp(segment=self.soma(0.5))
        iclamp.stim(delay=25, dur=3, amp=3)

        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)

        sim.run(100)
        r = rec.as_numpy(variable="v")

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()
        iclamp.remove_immediate_from_neuron()

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(34.3815, round(r.records.max(), 4))
        self.assertEqual(-75.3247, round(r.records.min(), 4))

        self.assertEqual(330, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(28, round(r.time[r.records.argmax()], 4))

    def test_iclamp_after_sim(self):
        """
        IClamp works in a regular way after simulation run. in this case (amplitude 3 pA)
        we have spike at the soma.

        However IClamp delay is in the absolute time.
        """
        # Record
        rec = Record(self.soma(0.5))

        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)

        # IClamp to soma
        iclamp = IClamp(segment=self.soma(0.5))
        iclamp.stim(delay=25, dur=3, amp=3)

        sim.run(100)
        r = rec.as_numpy(variable="v")

        sim.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()
        iclamp.remove_immediate_from_neuron()

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(34.3815, round(r.records.max(), 4))
        self.assertEqual(-75.3247, round(r.records.min(), 4))

        self.assertEqual(330, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(28, round(r.time[r.records.argmax()], 4))

    def test_2_runs(self):
        """
        Test setup for 2 cells and their records which should be the same.

        After each creation of the Simulation object, the Record object is cleaned up
        (inside vector is empty) however on other Hoc object is removed eg. Sections.
        That means - each new sections and cell are retained in the current NEURON run.
        """

        def run_and_get_rec():
            rec = Record(self.soma(0.5))
            iclamp = IClamp(segment=self.soma(0.5))
            iclamp.stim(delay=25, dur=3, amp=3)
            sim = Simulation(init_v=-70, warmup=20)
            sim.run(100)
            return rec

        rec1 = run_and_get_rec()
        r1 = rec1.as_numpy('v').records

        self.tearDown()
        self.setUp()

        rec2 = run_and_get_rec()
        r2 = rec2.as_numpy('v').records

        sim = Simulation(init_v=-70, warmup=20)
        sim.run(100)

        # Make assertions
        self.assertEqual(4011, r1.size)
        self.assertEqual(4011, r2.size)
        self.assertTrue(np.alltrue(r1 == r2))

    def test_record_before_sim(self):
        """
        Record created before simulation run is full of data
        """
        # Record
        rec = Record(self.soma(0.5))

        # IClamp to soma
        iclamp = IClamp(segment=self.soma(0.5))
        iclamp.stim(delay=25, dur=3, amp=3)

        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)
        print(h.t)

        sim.run(100)
        r = rec.as_numpy(variable="v")

        # Make assertions
        self.assertEqual(4051, r.size)
        self.assertEqual(34.3815, round(r.records.max(), 4))
        self.assertEqual(-75.3247, round(r.records.min(), 4))

        self.assertEqual(330, r.records.argmax())
        # time in ms of max mV value
        self.assertEqual(28, round(r.time[r.records.argmax()], 4))

    def test_record_after_sim(self):
        """
        record created after simulation run is empty
        """
        # IClamp to soma
        iclamp = IClamp(segment=self.soma(0.5))
        iclamp.stim(delay=25, dur=3, amp=3)

        # Run
        sim = Simulation(init_v=-70, warmup=20)
        sim.run(1)
        print(h.t)

        # Record
        rec = Record(self.soma(0.5))

        sim.run(100)
        r = rec.as_numpy(variable="v")

        # Make assertion
        self.assertEqual(0, r.size)
示例#20
0
    def __init__(self, name=None, model_folder=f_path, spine_number=0,
                 spine_secs_names="apic",
                 spine_seed: int = None):
        """
        :param name:
            The name of the cell
        :param model_folder:
            The folder where the main folder of Combe et al. 2018 model is located
        :param spine_number:
            The number of spines added to the model with random_uniform distribution to the sections
             specified by 'spine_sec' param.
        :param spine_secs_names:
            The section or sections where to put spines. It can be:
              * a string - as a filter name, so you can set "apic" to add spies to all apical
                dendrites

              * a regex, which need to be prefixed with 'regex:' string before
                eg. 'regex:(apic)|(basal)'
              will return all sections wich have a name containing 'apic' or 'basal' string

              * a list of existing sections in the cell
        :param spine_seed:
            Seed value for the random_uniform spike distribution. Default is None
            meaning - there is no seed
        """
        Cell.__init__(self, name, model_folder)
        CoreHocCell.__init__(self, name)

        main_file = os.path.join(model_folder, "load_cell.hoc")
        self.load_hoc(main_file)

        secs = self.filter_secs(name=spine_secs_names)

        # Add spines with AMPA and NMDA synapses
        self.combe_syns = []
        if spine_number > 0:

            self.add_randuniform_spines(secs=secs, spine_number=spine_number, head_nseg=10,
                                        neck_nseg=10, seed=spine_seed)

            # Copy mechanisms from parent sec of the neck and from the nec to the head
            self.copy_mechanisms(secs_to=self.necks, sec_from='parent')
            self.copy_mechanisms(secs_to=self.heads, sec_from='parent')

            ampa_syns = []
            nmda_syns = []

            # Create AMPA synapses
            ampa_weight = 1.2 * 0.00156
            for h in self.heads:
                syn = self.add_synapse(source=None, seg=h(1.0), mod_name="Exp2Syn",
                                       netcon_weight=ampa_weight)
                syn.point_process.hoc.e = 0
                syn.point_process.hoc.tau1 = .5
                syn.point_process.hoc.tau2 = 1.0
                ampa_syns.append(syn)

            # Create NMDA synapses
            nmda_weight = 1.2 * 0.000882
            for h in self.heads:
                syn = self.add_synapse(source=None, seg=h(1.0), mod_name="nmdanet",
                                       netcon_weight=nmda_weight)
                syn.point_process.hoc.Alpha = 0.35
                syn.point_process.hoc.Beta = 0.035
                nmda_syns.append(syn)

            for syns in zip(ampa_syns, nmda_syns):
                comp_syn = self.group_synapses(synapses=syns, tag="combe_type")
                self.combe_syns.append(comp_syn)
示例#21
0
class TestSection(unittest.TestCase):
    def setUp(self):
        self.cell = Cell(name="cell")

    def tearDown(self):
        self.cell.remove_immediate_from_neuron()

        l = len(list(h.allsec()))
        if len(list(h.allsec())) != 0:
            raise RuntimeError("Not all section have been removed after teardown. "
                               "Sections left: %s" % l)

    def test_default_name(self):
        """
        If the name exists it will add a number starting from 2 to the proposed name.
        """
        pat = re.compile("[1-9]")

        for i in range(10):
            self.cell.add_sec("dend")

        for i, dend in enumerate(self.cell.filter_secs(name="dend")):
            res = pat.search(dend.name)
            if i == 0:
                self.assertIsNone(res)
            else:
                self.assertEqual(i + 1, int(dend.name.replace("dend", "")))
            dend.remove_immediate_from_neuron()

    def test_regex_search(self):
        for i in range(10):
            self.cell.add_sec("dend")

        for i, dend in enumerate(self.cell.filter_secs("regex:dend[1-9]")):
            self.assertEqual(i + 2, int(dend.name.replace("dend", "")))

        for d in self.cell.secs:
            d.remove_immediate_from_neuron()

    def test_connections(self):
        soma = self.cell.add_sec(name="soma", l=10, nseg=10)
        dend = self.cell.add_sec(name="dend", l=100, nseg=10)
        self.cell.connect_secs(child=dend, parent=soma, child_loc=0, parent_loc=0.7)

        self.assertEqual(0, dend.orientation)
        self.assertEqual(0.7, dend.parent_loc)
        self.assertIsNone(soma.parent_loc)

    def test_allseg(self):
        soma = self.cell.add_sec(name="soma", diam=1, l=10, nseg=20)
        dend = self.cell.add_sec(name="dend", diam=1, l=100, nseg=200)
        self.assertEqual(22, len(soma.segs))
        self.assertEqual(202, len(dend.segs))

    def test_area1(self):
        soma = self.cell.add_sec(name="soma", diam=1, l=10, nseg=20)
        dend = self.cell.add_sec(name="dend", diam=1, l=100, nseg=200)

        self.assertEqual(0, soma.segs[0].area)
        self.assertEqual(0, soma.segs[-1].area)
        self.assertEqual(0, dend.segs[0].area)
        self.assertEqual(0, dend.segs[-1].area)

        for s in soma.segs[1:-1]:
            self.assertEqual(1.5708, round(s.area, 4))
        for s in dend.segs[1:-1]:
            self.assertEqual(1.5708, round(s.area, 4))

    def test_area2(self):
        apic = self.cell.add_sec(name="apic", diam=10, l=10, nseg=4)
        for s in apic.segs[1:-1]:
            self.assertEqual(78.5398, round(s.area, 4))
示例#22
0
 def setUp(self):
     self.cell = Cell(name="cell")
示例#23
0
 def cell_template():
     cell = Cell(name="cell")
     cell.load_morpho(filepath=morpho_path)
     cell.insert("pas")
     cell.insert("hh")
     return cell
    def setUpClass(cls):
        morpho_path = os.path.join(path, "..",
                                   "commons/morphologies/asc/cell2.asc")

        # Create cell
        cell = Cell(name="cell")
        cell.load_morpho(filepath=morpho_path)
        cell.insert("pas")
        cell.insert("hh")

        soma = cell.filter_secs("soma")
        dend = cell.filter_secs("apic[10]")
        syn = cell.add_synapse(source=None, mod_name="ExpSyn", seg=dend(0.5))

        # Prepare EPSP and AP (IClamp) protocols
        experiment = Experiment(iti=40)
        experiment.add_epsp(num=3,
                            synapse=syn,
                            init=20,
                            interval=20,
                            weight=0.02)
        experiment.add_iclamp(num=3,
                              segment=soma(0.5),
                              init=60,
                              interval=20,
                              dur=3,
                              amp=1.6)
        experiment.build()

        # Prepare plots
        rec = Record([soma(0.5), dend(0.5)], variables='v')

        # Run
        sim = Simulation(init_v=-70,
                         warmup=20,
                         with_neuron_gui=False,
                         constant_timestep=True)
        sim.run(runtime=100)

        cls.v_soma = rec.as_numpy('v', segment_name=soma(.5).name)
        cls.v_apic = rec.as_numpy('v', segment_name=dend(.5).name)

        syn.remove_immediate_from_neuron()
        soma.remove_immediate_from_neuron()
        dend.remove_immediate_from_neuron()
        rec.remove_immediate_from_neuron()
        experiment.remove_immediate_from_neuron()
        cell.remove_immediate_from_neuron()
        sim.remove_immediate_from_neuron()

        cls.sections_left = len(list(h.allsec()))
示例#25
0
import os
import matplotlib.pyplot as plt

from neuronpp.cells.cell import Cell
from neuronpp.utils.simulation import Simulation

from neuronpp.utils.record import Record
from neuronpp.utils.iclamp import IClamp
from neuronpp.utils.utils import make_shape_plot
from neuronpp.core.cells.netstim_cell import NetStimCell

# Create cell
cell = Cell(name="cell")
path = os.path.dirname(os.path.abspath(__file__))
morpho_path = os.path.join(path, "..", "commons/morphologies/asc/cell2.asc")
cell.load_morpho(filepath=morpho_path)
cell.insert("pas")
cell.insert("hh")

# Create stim and synapses
ns_cell = NetStimCell("stim_cell")
ns = ns_cell.make_netstim(start=30, number=5, interval=10)

syns = cell.add_synapses_with_spine(source=ns,
                                    secs=cell.filter_secs("apic"),
                                    mod_name="ExpSyn",
                                    netcon_weight=0.01,
                                    delay=1,
                                    number=100)
soma = cell.filter_secs("soma")
示例#26
0
import os

from neuronpp.utils.simulation import Simulation

from neuronpp.cells.cell import Cell
from neuronpp.utils.record import Record
from neuronpp.core.cells.netstim_cell import NetStimCell

path = os.path.dirname(os.path.abspath(__file__))
model_path1 = os.path.join(path, "..", "commons/mods/ebner2019")
model_path2 = os.path.join(path, "..", "commons/morphologies/swc/my.swc")

# Prepare cell
cell = Cell(name="cell", compile_paths=model_path1)
cell.load_morpho(filepath=model_path2)
cell.add_sec("dend[1]", diam=10, l=10, nseg=10)
cell.connect_secs(source="dend[1]", target="soma")
cell.insert("pas")
cell.insert("hh")

# Two examples of synapses with NetStim:
stim_cell = NetStimCell("stim_cell")
stim = stim_cell.make_netstim(start=250, number=30, interval=1)
soma = cell.filter_secs("soma")

# 1) Hoc-style synapse
pp = cell.add_point_process(mod_name="ExpSyn", seg=soma(0.5))
cell.add_netcon(source=stim, point_process=pp, netcon_weight=0.01, delay=1)

# 2) Recommended synapse
syn1 = cell.add_synapse(source=stim,
示例#27
0
import os

from neuronpp.cells.cell import Cell
from neuronpp.utils.record import Record
from neuronpp.utils.simulation import Simulation

path = os.path.dirname(os.path.abspath(__file__))

if __name__ == '__main__':
    # Prepare cell
    filepath = os.path.join(path, "..",
                            "commons/mods/sigma3syn")
    cell = Cell("cell", compile_paths=filepath)
    soma = cell.add_sec("soma", diam=20, l=20, nseg=10)
    cell.insert('pas')
    cell.insert('hh')

    w = 0.003  # LTP
    #w = 0.0022  # LTD
    syn = cell.add_synapse(source=None, netcon_weight=w, seg=soma(0.5), mod_name="ExcSigma3Exp2Syn")

    # prepare plots and spike detector
    rec_v = Record(soma(0.5), variables="v")
    rec_w = Record(syn, variables="w")

    # run
    sim = Simulation(init_v=-68, warmup=5)
    syn.make_event(5)
    sim.run(runtime=50)

    # plot
示例#28
0
class TestSection(unittest.TestCase):
    def setUp(self):
        self.cell = Cell(name="cell")
        self.soma = self.cell.add_sec("soma", diam=10, l=10, nseg=10)
        self.dend1 = self.cell.add_sec("dend1", diam=1, l=10, nseg=10)
        self.dend2 = self.cell.add_sec("dend2", diam=1, l=10, nseg=10)
        self.cell.connect_secs(child=self.dend1, parent=self.soma, child_loc=0, parent_loc=0.1)
        self.cell.connect_secs(child=self.dend2, parent=self.soma, child_loc=0, parent_loc=0.9)

    def tearDown(self):
        self.soma.remove_immediate_from_neuron()
        self.dend1.remove_immediate_from_neuron()
        self.dend2.remove_immediate_from_neuron()
        self.cell.remove_immediate_from_neuron()

        all_sec_num = _get_secs()
        if all_sec_num != 0:
            raise RuntimeError("Not all section have been removed after teardown. "
                               "Sections left: %s" % all_sec_num)

    def test_remove_synapse(self):
        """
        Removal of the parent section doesn't remove child sections
        """
        dend3 = self.cell.add_sec("dend3", diam=1, l=10, nseg=10)
        self.cell.connect_secs(child=dend3, parent=self.soma, child_loc=0, parent_loc=0.5)

        syn = self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))

        dend3 = None
        self.assertEqual(4, _get_secs())

        syn.remove_immediate_from_neuron()
        self.assertEqual(4, _get_secs())

    def test_remove_point_process(self):
        """
        Immediate removal of Point Process in outside reference makes
        that Point Process inside synapse also delete
        """
        syn = self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=self.dend2(0.5))
        syn.point_process.remove_immediate_from_neuron()
        self.assertEqual(0, len(self.cell.syns[0].point_process.__dict__))

    def test_remove_synapse2(self):
        """
        Remove outside reference and inside (Cell's) reference deletes section from NEURON
        """
        dend3 = self.cell.add_sec("dend3", diam=1, l=10, nseg=10)
        self.cell.connect_secs(child=dend3, parent=self.soma, child_loc=0, parent_loc=0.5)

        dend3 = None
        self.assertEqual(4, _get_secs())

        self.cell.secs = self.cell.secs[:3]
        self.assertEqual(3, len(self.cell.secs))
        self.assertEqual(3, _get_secs())

    def test_remove_synapse3(self):
        """
        Remove outside reference and inside (Cell's) reference doesn't delete section if it has
        reference inside the synapse
        """
        dend3 = self.cell.add_sec("dend3", diam=1, l=10, nseg=10)
        self.cell.connect_secs(child=dend3, parent=self.soma, child_loc=0, parent_loc=0.5)

        syn = self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))

        dend3 = None
        self.assertEqual(4, _get_secs())

        self.cell.secs = self.cell.secs[:3]
        self.assertEqual(3, len(self.cell.secs))
        self.assertEqual(4, _get_secs())

        syn.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())

    def test_remove_soma(self):
        """
        Removal of the parent section doesn't remove child sections
        """
        self.soma.remove_immediate_from_neuron()
        self.assertEqual(2, _get_secs())

    def test_removal_sec_from_cell_object(self):
        """
        removal section by cell object - removes it from the cell object's self.secs list
        """
        self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        self.cell.remove_secs("dend3")
        self.assertEqual(3, _get_secs())
        self.assertEqual(3, len(self.cell.secs))

    def test_removal_sec_outside_cell_object(self):
        """
        removal section by itself - removes it from the cell object's self.secs list
        """
        dend3 = self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        dend3.remove_immediate_from_neuron()
        self.assertEqual(4, len(self.cell.secs))

    def test_removal_sec_outside_cell_object_clear_secs_manual(self):
        """
        removal section from secs clear also the object dend3
        """
        dend3 = self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        self.cell.secs.remove(dend3)
        self.assertEqual(3, len(self.cell.secs))

    def test_remove_dend1(self):
        """
        Removal of the parent section doesn't remove child sections
        """
        self.dend1.remove_immediate_from_neuron()
        self.assertEqual(2, _get_secs())

    def test_remove_dend1_soma(self):
        """
        Removal of the parent section doesn't remove child sections
        """
        self.soma.remove_immediate_from_neuron()
        self.dend1.remove_immediate_from_neuron()
        self.assertEqual(1, _get_secs())

    def test_remove_cell(self):
        """
        Removal of cell only doesn't remove any sections since their references are valid
        """
        self.cell.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())

    def test_remove_sec_with_hoc_ref(self):
        """
        Immediate removal of Section doesn't remove it from NEURON if there is outside reference to
        the HOC object        that Point Process inside synapse also delete
        """
        hoc_soma = self.soma.hoc
        self.soma.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())
        
    def test_remove_new_cell_with_no_ref(self):
        """
        deletion of cell, when all its sections have no reference outside - deletes all its
        sections as well
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        soma = None
        dend1 = None
        dend2 = None

        self.assertEqual(6, _get_secs())
        cell.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())

    def test_remove_new_cell_with_del_ref(self):
        """
        deletion of cell, when all its outside references are deletes - deletes all its
        sections as well. But deletion of single soma won't delete section soma since its reference
        is still in Cell
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        del soma
        del dend1
        del dend2

        self.assertEqual(6, _get_secs())
        cell.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())

    def test_remove_new_cell_with_remove_immediate(self):
        """
        remove immediate removes each section immediate only if there is no reference to the HOC
        object of this section outside the Sec object.
        """
        cell = Cell(name="cell")
        soma = cell.add_sec("soma")
        dend1 = cell.add_sec("dend1")
        dend2 = cell.add_sec("dend2")
        cell.connect_secs(child=dend1, parent=soma, child_loc=0, parent_loc=0.1)
        cell.connect_secs(child=dend2, parent=soma, child_loc=0, parent_loc=0.9)

        self.assertEqual(6, _get_secs())

        soma.remove_immediate_from_neuron()
        dend1.remove_immediate_from_neuron()
        dend2.remove_immediate_from_neuron()

        self.assertEqual(3, _get_secs())
        cell.remove_immediate_from_neuron()

    def test_removal_sec_after_synapse_remove(self):
        """
        Removing synapse allows to remove section as well
        """
        dend3 = self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        syn = self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))
        syn.remove_immediate_from_neuron()
        self.soma.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())

    def test_removal_sec_as_part_of_synapse(self):
        """
        Removal of Sec which is a part of a Synapse won't delete the Section in NEURON as long as
        Synapse exists.
        """
        dend3 = self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        syn = self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))
        dend3.remove_immediate_from_neuron()
        self.assertEqual(4, _get_secs())

    def test_removal_sec_before_synapse_remove(self):
        """
        Removing section before removing synapse - makes that section is not removed. It is because
        we remove dend3 section, but self.cell still holds the reference to the synapse.
        """
        dend3 = self.cell.add_sec("dend3", diam=10, l=10, nseg=10)
        self.cell.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))
        dend3.remove_immediate_from_neuron()
        self.assertEqual(4, _get_secs())

    def test_removal_cell_before_synapse_remove(self):
        """
        Removing whole cell after none of its content's reference is stored out of the cell - makes
        all its content and synapses removal.
        """
        cell2 = Cell(name="cell2")
        dend3 = cell2.add_sec("dend3", diam=10, l=10, nseg=10)
        cell2.add_synapse(source=None, mod_name="Exp2Syn", seg=dend3(0.5))
        dend3 = None

        cell2.remove_immediate_from_neuron()
        self.assertEqual(3, _get_secs())
示例#29
0
    def setUpClass(cls):
        cell1 = Cell(name="cell1")
        cls.soma1 = cell1.add_sec("soma", add_pas=True)
        cell2 = Cell(name="cell2")
        cls.soma2 = cell2.add_sec("soma", g_pas=0.002)
        cell3 = Cell(name="cell3")
        cls.soma3 = cell3.add_sec("soma", rm=1 / 0.003)

        cell4 = Cell(name="cell4")

        cls.soma4 = cell4.add_sec("soma", E_rest=-80)
        # no pas but add pas
        cls.cell5 = Cell(name="cell5")
        cls.soma5 = cls.cell5.add_sec("soma", add_pas=True)
        cls.dend = h.Section("dend", "cell5")
        cls.dend.insert("pas")
        cell6 = Cell(name="cell6")
        cls.soma6 = cell6.add_sec("soma", add_pas=True)
        cls.soma6.hoc.insert("pas")
        cell6.set_pas("soma", Rm=1000, E_rest=-77, g_pas=0.002)
示例#30
0
 def hh_cell(name):
     cell = Cell(name=f"cell_{name}")
     soma = cell.add_sec("soma", diam=20, l=20, nseg=10)
     cell.add_sec("apic", diam=2, l=50, nseg=100)
     cell.insert("hh")
     cell.connect_secs(child="apic", parent="soma", child_loc=0, parent_loc=1)
     cell.make_spike_detector(soma(0.5))
     return cell