예제 #1
0
    def setUpClass(cls):
        morpho_path = os.path.join(path, "..",
                                   "commons/morphologies/swc/my.swc")

        def cell_template():
            cell = Cell(name="cell")
            cell.load_morpho(filepath=morpho_path)
            cell.insert("pas")
            cell.insert("hh")
            return cell

        # Create NetStim
        cls.netstim = NetStimCell("stim1").add_netstim(start=21,
                                                       number=100,
                                                       interval=2)

        # Define connection probabilities
        Dist.set_seed(13)
        weight_dist = NormalDist(mean=0.01, std=0.024)

        # Create population 1
        cls.pop1 = Population("pop_0")
        cls.pop1.add_cells(num=30, cell_function=cell_template)

        connector = cls.pop1.connect(cell_connection_proba=0.6)
        connector.set_source(cls.netstim)
        connector.set_target([c.filter_secs("dend") for c in cls.pop1.cells])
        syn_adder = connector.add_synapse("Exp2Syn")
        syn_adder.add_netcon(weight=weight_dist)
        connector.build()

        # Create population 2
        cls.pop2 = Population("pop_1")
        cls.pop2.add_cells(num=40, cell_function=cell_template)

        connector = cls.pop2.connect(cell_connection_proba=0.8)
        connector.set_source(
            [c.filter_secs("soma")(0.5) for c in cls.pop1.cells])
        connector.set_target([c.filter_secs("dend") for c in cls.pop2.cells])
        syn_adder = connector.add_synapse("Exp2Syn")
        syn_adder.add_netcon(weight=weight_dist)
        connector.build()

        # Create population 3
        cls.pop3 = Population("pop_2")
        cls.pop3.add_cells(num=50, cell_function=cell_template)

        connector = cls.pop3.connect(cell_connection_proba=0.3)
        connector.set_source(
            [c.filter_secs("soma")(0.5) for c in cls.pop2.cells])
        connector.set_target([c.filter_secs("dend") for c in cls.pop3.cells])
        syn_adder = connector.add_synapse("Exp2Syn")
        syn_adder.add_netcon(weight=weight_dist)
        connector.build()
예제 #2
0
    def setUp(self):
        def cell():
            c = Cell(name="cell")
            c.add_sec("soma", nseg=100, l=10)
            return c

        self.pop1 = Population("pop1")
        self.pop1.add_cells(num=200, cell_function=cell)

        self.pop2 = Population("pop2")
        self.pop2.add_cells(num=100, cell_function=cell)
예제 #3
0
    def setUpClass(cls):
        def cell_name_template():
            return Cell(name="my custom name")

        def cell_noname_template():
            return Cell()

        # Create population with name for the cell template
        cls.pop1 = Population("pop_0")
        cls.pop1.add_cells(num=3, cell_function=cell_name_template)

        # Create population without name for the cell template
        cls.pop2 = Population("pop_1")
        cls.pop2.add_cells(num=3, cell_function=cell_noname_template)
예제 #4
0
def make_iclamp_cell(input_size, input_cell_num):
    def cell_ebner_ach_da():
        cell = Ebner2019AChDACell("ebner_ach_da",
                                  compile_paths="agents/commons/mods/4p_ach_da_syns "
                                                "agents/commons/mods/ebner2019")
        soma = cell.add_sec("soma", diam=20, l=20, nseg=10)
        cell.add_sec("apic", diam=2, l=50, nseg=100)
        cell.connect_secs(child="apic", parent="soma", child_loc=0, parent_loc=1)
        cell.make_default_mechanisms()
        cell.make_spike_detector(soma(0.5))
        return cell

    def cell_ebner():
        cell = Ebner2019Cell("ebner", compile_paths="agents/commons/mods/ebner2019")
        cell.add_sec("soma", diam=20, l=20, nseg=10)
        cell.add_sec("apic", diam=2, l=50, nseg=100)
        cell.connect_secs(child="apic", parent="soma", child_loc=0, parent_loc=1)
        cell.make_default_mechanisms()
        return cell

    input_syn_per_cell = int(np.ceil(input_size / input_cell_num))
    inp = Population(name="input")
    inp.add_cells(num=input_cell_num, cell_function=cell_ebner)
    con = inp.connect(rule="one", syn_num_per_cell_source=input_syn_per_cell)
    con.set_source(None)
    con.set_target(inp.cells)
    con.add_synapse("Syn4P").add_netcon(weight=UniformTruncatedDist(low=0.01, high=0.1))
    con.build()

    out = Population(name="output")
    out.add_cells(num=2, cell_function=cell_ebner_ach_da)
    con = out.connect(syn_num_per_cell_source=1, cell_connection_proba=0.1)
    con.set_source([c.filter_secs("soma")(0.5) for c in inp.cells])
    con.set_target(out.cells)
    con.add_synapse("Syn4PAChDa").add_netcon(weight=UniformTruncatedDist(low=0.01, high=0.1)) \
        .add_point_process_params(ACh_tau=50, Da_tau=50)
    con.add_synapse("SynACh").add_netcon(weight=0.3)
    con.add_synapse("SynDa").add_netcon(weight=1.0)
    con.set_synaptic_function(func=lambda syns: Ebner2019AChDACell.set_synaptic_pointers(*syns))
    con.group_synapses()
    con.build()
    reward = [s['SynACh'][0] for s in out.syns]
    punish = [s['SynDa'][0] for s in out.syns]
    return inp, out, reward, punish
예제 #5
0
    def setUpClass(cls):
        morpho_path = os.path.join(path, "..",
                                   "commons/morphologies/swc/my.swc")

        def cell_template():
            cell = Cell(name="cell")
            cell.load_morpho(filepath=morpho_path)
            cell.insert("pas")
            cell.insert("hh")
            return cell

        # Define connection probabilities
        Dist.set_seed(13)
        weight_dist1 = NormalTruncatedDist(mean=0.01, std=0.05)
        weight_dist2 = NormalTruncatedDist(mean=0.1, std=0.5)

        # Create population 1
        cls.pop1 = Population("pop_0")
        cls.pop1.add_cells(num=50, cell_function=cell_template)

        # Create population 2
        cls.pop2 = Population("pop_1")
        cls.pop2.add_cells(num=50, cell_function=cell_template)

        connector = cls.pop2.connect(cell_connection_proba=0.5)
        connector.set_source(
            [c.filter_secs("soma")(0.5) for c in cls.pop1.cells])
        connector.set_target([c.filter_secs("dend") for c in cls.pop2.cells])
        connector.add_synapse("ExpSyn").add_netcon(weight=weight_dist1)
        connector.add_synapse("Exp2Syn").add_netcon(weight=weight_dist2)
        connector.group_synapses(name="group1")
        connector.build()

        connector = cls.pop2.connect(cell_connection_proba=0.5)
        connector.set_source(
            [c.filter_secs("soma")(0.5) for c in cls.pop1.cells])
        connector.set_target([c.filter_secs("dend") for c in cls.pop2.cells])
        connector.add_synapse("ExpSyn").add_netcon(weight=1.5)
        connector.add_synapse("Exp2Syn").add_netcon(weight=10.5)
        connector.build()
예제 #6
0
        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

    # Create NetStim
    netstim = NetStimCell("stim").add_netstim(start=21, number=100, interval=2)

    # Define connection probabilities
    Dist.set_seed(13)
    connection_proba = NormalConnectionProba(mean=0.8, std=0.1, threshold=0.6)
    weight_dist = NormalTruncatedDist(mean=0.1, std=0.2)

    # Create population 1
    pop1 = Population("pop_1")
    pop1.add_cells(num=4, cell_function=cell_function)

    connector = pop1.connect(cell_connection_proba=connection_proba)
    connector.set_source(netstim)
    connector.set_target([d(0.5) for c in pop1.cells for d in c.filter_secs("dend")])
    syn_adder = connector.add_synapse("Exp2Syn")
    syn_adder.add_netcon(weight=weight_dist)

    connector.build()
    pop1.record()

    # Create population 2
    pop2 = Population("pop_2")
    pop2.add_cells(num=4, cell_function=cell_function)
예제 #7
0
class TestPopulationalDistparam(unittest.TestCase):
    def setUp(self):
        def cell():
            c = Cell(name="cell")
            c.add_sec("soma", nseg=100, l=10)
            return c

        self.pop1 = Population("pop1")
        self.pop1.add_cells(num=200, cell_function=cell)

        self.pop2 = Population("pop2")
        self.pop2.add_cells(num=100, cell_function=cell)

    def tearDown(self):
        self.pop1.remove_immediate_from_neuron()
        self.pop2.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_con_proba1(self):
        error = True
        try:
            NormalConnectionProba(threshold=1, mean=10, std=2)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba2(self):
        error = True
        try:
            NormalConnectionProba(threshold=0.5, mean=0.5, std=10)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba3(self):
        error = True
        try:
            NormalConnectionProba(threshold=10, mean=0.5, std=0.5)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba4(self):
        error = True
        try:
            NormalConnectionProba(threshold=-1, mean=0.5, std=0.5)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba5(self):
        error = True
        try:
            NormalConnectionProba(threshold=0.5, mean=-2, std=0.5)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba6(self):
        error = True
        try:
            NormalConnectionProba(threshold=0.5, mean=0.5, std=-2)
            error = False
        except ValueError:
            self.assertTrue(error)

    def test_con_proba7(self):
        error = True
        try:
            NormalConnectionProba(threshold=0.5, mean=0.5, std=0.5)
            error = False
        except ValueError:
            self.assertFalse(error)

    def test_con_proba8(self):
        error = True
        try:
            NormalConnectionProba(threshold=0, mean=0, std=0)
            error = False
        except ValueError:
            self.assertFalse(error)

    def test_normal_proba_connection_between_cells(self):
        """
        Compare NormalConnectionProba (which is truncated normal dist) with a regular
        truncated normal dist of the same size as the maximal all-to-all connections.

        The test will pass if difference between potential conn number is less than 0.95
        """
        std = 0.2
        mean = 0.1
        threshold = 0.4  # if Truncated Normal Dist pass this threshold - creates connection

        Dist.set_seed(13)
        cell_conn_proba = NormalConnectionProba(threshold=threshold,
                                                mean=mean,
                                                std=std)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=cell_conn_proba,
                                 seg_dist="uniform")

        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        # create Truncated Normal comparative distribution of the same size as maximal
        # all-to-all connections
        norm = np.abs(np.random.normal(loc=mean, scale=std, size=200 * 100))
        non_zero_norm = np.count_nonzero(norm[norm > threshold])

        non_zero_cell = [len(c.syns) for c in self.pop2.cells]
        non_zero_cell_sum = sum(non_zero_cell)

        # difference of connection number is less then 0.95
        if non_zero_cell_sum > non_zero_norm:
            diff = non_zero_norm / non_zero_cell_sum
        else:
            diff = non_zero_cell_sum / non_zero_norm
        self.assertGreater(diff, 0.95)

    def test_uniform_proba_connection_between_cells(self):
        """
        Compare UniformConnectionProba with a regular uniform dist of the same size as
        the maximal all-to-all connections.

        The test will pass if difference between potential conn number is less than 0.95
        """
        threshold = 0.9  # if Uniform dist pass this threshold - creates connection

        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=threshold,
                                 seg_dist="uniform")

        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        norm = np.abs(np.random.uniform(size=200 * 100))
        non_zero_norm = np.count_nonzero(norm[norm > threshold])

        non_zero_cell = [len(c.syns) for c in self.pop2.cells]
        non_zero_cell_sum = sum(non_zero_cell)

        # difference of connection number is less then 0.95
        if non_zero_cell_sum > non_zero_norm:
            diff = non_zero_norm / non_zero_cell_sum
        else:
            diff = non_zero_cell_sum / non_zero_norm
        self.assertGreater(diff, 0.95)

    def test_full_all_to_all_connections_between_cells(self):
        """
        Expected that each cell from pop1 will be connected to each cell in the pop2
        """
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1.0,
                                 seg_dist="uniform")
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        non_zero_cell = sum([len(c.syns) for c in self.pop2.cells])
        self.assertEqual(20000, non_zero_cell)

    def test_uniform_seg_dist(self):
        """
        Expected that seg.x distribution will be uniform,
        mean will be ~0.5 and distribution will be normal between 0-1
        """
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1.0,
                                 seg_dist="uniform")
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        xs = [s.parent.x for c in self.pop2.cells for s in c.syns]
        self.assertEqual(0.50, round(np.average(xs), 2))

    def test_normal_seg_dist(self):
        """
        Expected that seg.x distribution will be truncated normal,
        mean will be ~0.7 and std ~0.2
        """
        Dist.set_seed(13)
        seg_dist = NormalTruncatedSegDist(mean=0.7, std=0.2)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1.0,
                                 seg_dist=seg_dist)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        xs = [s.parent.x for c in self.pop2.cells for s in c.syns]
        avg = np.average(xs)
        std = np.std(xs)

        self.assertEqual(0.2, np.round(std, 1))
        self.assertEqual(0.7, np.round(avg, 1))
예제 #8
0
class TestPopulationalSynNumPerCellSource(unittest.TestCase):
    def setUp(self):
        def cell():
            c = Cell(name="cell")
            c.add_sec("soma", nseg=10, l=10)
            return c

        self.pop1 = Population("pop1")
        self.pop1.add_cells(num=50, cell_function=cell)

        self.pop2 = Population("pop2")
        self.pop2.add_cells(num=50, cell_function=cell)

    def tearDown(self):
        self.pop1.remove_immediate_from_neuron()
        self.pop2.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_syn_per_cell_source1(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=1)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg = np.average(lens)
        self.assertEqual(50, avg)

    def test_syn_per_cell_source2(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=3)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg = np.average(lens)
        self.assertEqual(150, avg)

    def test_syn_per_cell_source3(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=9)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg = np.average(lens)
        self.assertEqual(450, avg)

    def test_syn_per_cell_source4(self):
        Dist.set_seed(15)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=0.5,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=1)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg = np.average(lens)
        self.assertEqual(25, round(avg))

    def test_syn_per_cell_source5(self):
        Dist.set_seed(15)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=0.5,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=3)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg = np.average(lens)
        self.assertEqual(75, round(avg))

    def test_syn_per_cell_source_pop_syns_num(self):
        Dist.set_seed(15)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=UniformTruncatedDist(
                                     low=0, high=5))
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        defaultdict(int)
        counts = Counter([s.sources[0].parent.cell for s in self.pop2.syns])
        avg_source = np.average(list(counts.values()))
        avg_lens = np.average(lens)
        self.assertEqual(avg_source, avg_lens)

    def test_syn_per_cell_source_uniform(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=UniformTruncatedDist(
                                     low=0, high=5))
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg_lens = np.average(lens)

        Dist.set_seed(13)
        rand_avg = np.average(np.random.uniform(0, 5, size=50) * 50)

        if avg_lens > rand_avg:
            diff = rand_avg / avg_lens
        else:
            diff = avg_lens / rand_avg
        self.assertGreater(diff, 0.9)

    def test_syn_per_cell_source_normal(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="all",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=NormalTruncatedDist(
                                     mean=2, std=0.5))
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg_lens = np.average(lens)

        Dist.set_seed(13)
        rand_avg = np.average(np.random.normal(loc=2, scale=0.5, size=50) * 50)

        if avg_lens > rand_avg:
            diff = rand_avg / avg_lens
        else:
            diff = avg_lens / rand_avg
        self.assertGreater(diff, 0.9)

    def test_syn_per_cell_source_lognormal(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(
            rule="all",
            cell_connection_proba=1,
            seg_dist="uniform",
            syn_num_per_cell_source=LogNormalTruncatedDist(mean=2, std=0.5))
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg_lens = np.average(lens)

        Dist.set_seed(13)
        rand_avg = np.average(
            np.random.lognormal(mean=2, sigma=0.5, size=50) * 50)

        if avg_lens > rand_avg:
            diff = rand_avg / avg_lens
        else:
            diff = avg_lens / rand_avg
        self.assertGreater(diff, 0.9)

    def test_syn_per_cell_rule_one_normal_truncated(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="one",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=NormalTruncatedDist(
                                     mean=2, std=0.5))
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        avg_lens = np.average(lens)

        Dist.set_seed(13)
        rand_avg = np.average(np.random.normal(loc=2, scale=0.5, size=50))

        if avg_lens > rand_avg:
            diff = rand_avg / avg_lens
        else:
            diff = avg_lens / rand_avg
        self.assertGreater(diff, 0.9)

    def test_syn_per_cell_rule_one_syn_per_source_1(self):
        Dist.set_seed(13)
        conn = self.pop2.connect(rule="one",
                                 cell_connection_proba=1,
                                 seg_dist="uniform",
                                 syn_num_per_cell_source=1)
        conn.set_source([c.filter_secs("soma")(0.5) for c in self.pop1.cells])
        conn.set_target(self.pop2.cells)
        conn.add_synapse("Exp2Syn").add_netcon()
        conn.build()

        lens = [len(c.syns) for c in self.pop2.cells]
        self.assertEqual(50, sum(lens))