Example #1
0
    def test_get_set(self):
        n_neurons = 4
        label = "pop_1"
        sim.setup(timestep=1.0)
        pop_1 = sim.Population(n_neurons, sim.IF_curr_exp(), label=label)
        view = PopulationView(pop_1, [1, 3], label="Odds")

        pop_1.set(tau_m=2)
        self.assertEqual([2, 2, 2, 2], pop_1.get("tau_m"))
        self.assertEqual([2, 2], view.get("tau_m", simplify=False))
        view.set(tau_m=3)
        self.assertEqual([2, 3, 2, 3], pop_1.get("tau_m"))
        sim.end()
Example #2
0
    def create_population(self,
                          sim,
                          n_neurons,
                          label,
                          spike_rate=None,
                          spike_rec_indexes=None,
                          v_rate=None,
                          v_rec_indexes=None):

        v_start = self.V_PATTERN * int(math.ceil(n_neurons / self.V_COUNT))
        v_start = v_start[:n_neurons]
        pop = sim.Population(n_neurons,
                             sim.IF_curr_exp(i_offset=1, tau_refrac=0),
                             label=label)
        pop.initialize(v=v_start)
        if spike_rec_indexes is None:
            pop.record(['spikes'], sampling_interval=spike_rate)
        else:
            view = PopulationView(pop, spike_rec_indexes)
            view.record(['spikes'], sampling_interval=spike_rate)
        if v_rec_indexes is None:
            pop.record(['v'], sampling_interval=v_rate)
        else:
            view = PopulationView(pop, v_rec_indexes)
            view.record(['v'], sampling_interval=v_rate)
        return pop
Example #3
0
 def __getitem__(self, index_or_slice):
     if isinstance(index_or_slice, int):
         return IDMixin(self, index_or_slice)
     else:
         return PopulationView(self,
                               index_or_slice,
                               label="view over {}".format(self.label))
Example #4
0
 def test_projection(self):
     sim.setup(timestep=1.0)
     pop = sim.Population(5, sim.IF_curr_exp(), label="pop_1")
     view = PopulationView(pop, [1, 3], label="Odds")
     try:
         sim.Projection(pop, view, sim.OneToOneConnector())
     except NotImplementedError:
         pass  # Exceptable but better if it worked
     with pytest.raises(ConfigurationException):
         sim.Projection(pop, "SOMETHING WIERD", sim.OneToOneConnector())
Example #5
0
    def test_simple(self):
        n_neurons = 5
        label = "pop_1"
        sim.setup(timestep=1.0)
        pop_1 = sim.Population(n_neurons, sim.IF_curr_exp(), label=label)
        mask = [1, 3]
        view = PopulationView(pop_1, mask, label=label)
        self.assertEqual(2, view.size)
        self.assertEqual(2, view.local_size)

        self.assertEqual(label, view.label)
        self.assertEqual(pop_1.celltype, view.celltype)

        view_initial_values = view.initial_values
        pop_initial_values = pop_1.initial_values
        self.assertEqual(len(view_initial_values), len(pop_initial_values))
        for key in pop_initial_values:
            self.assertEqual(pop_initial_values[key][3],
                             view_initial_values[key][1])

        self.assertEqual(pop_1, view.parent)
        self.assertEqual(mask, view.mask)

        cells = view.all_cells
        self.assertEqual(2, len(cells))
        self.assertEqual(1, cells[0].id)
        self.assertEqual(3, cells[1].id)

        self.assertEqual(cells, view.local_cells)
        self.assertEqual(cells[0], view[1])

        iterator = iter(view)
        self.assertEqual(1, next(iterator).id)
        self.assertEqual(3, next(iterator).id)
        with pytest.raises(StopIteration):
            next(iterator)

        self.assertEqual(2, len(view))

        iterator = view.all()
        self.assertEqual(1, next(iterator).id)
        self.assertEqual(3, next(iterator).id)
        with pytest.raises(StopIteration):
            next(iterator)

        self.assertEqual(view.can_record("v"), pop_1.can_record("v"))
        self.assertEqual(view.conductance_based, pop_1.conductance_based)

        describe = view.describe()
        self.assertIn('PopulationView "pop_1"', describe)
        self.assertIn('parent : "pop_1"', describe)
        self.assertIn('size   : 2', describe)
        self.assertIn('mask   : [1, 3]', describe)

        self.assertEqual(pop_1.find_units("v"), view.find_units("v"))

        sim.end()
Example #6
0
 def test_initial_value(self):
     sim.setup(timestep=1.0)
     pop = sim.Population(5, sim.IF_curr_exp(), label="pop_1")
     self.assertEqual([-65, -65, -65, -65, -65], pop.get_initial_value("v"))
     view = PopulationView(pop, [1, 3], label="Odds")
     view2 = PopulationView(pop, [1, 2], label="OneTwo")
     view_iv = view.initial_values
     self.assertEqual(1, len(view_iv))
     self.assertEqual([-65, -65], view_iv["v"])
     view.initialize(v=-60)
     self.assertEqual([-65, -60, -65, -60, -65], pop.get_initial_value("v"))
     self.assertEqual([-60, -60], view.initial_values["v"])
     self.assertEqual([-60, -65], view2.initial_values["v"])
     rand_distr = RandomDistribution("uniform",
                                     parameters_pos=[-65.0, -55.0],
                                     rng=NumpyRNG(seed=85524))
     view.initialize(v=rand_distr)
     self.assertEqual([-64.43349869042906, -63.663421790102184],
                      view.initial_values["v"])
     view.initialize(v=lambda i: -65 + i / 10.0)
     self.assertEqual([-64.9, -64.7], view.initial_values["v"])
     sim.end()
Example #7
0
 def test_view_of_view(self):
     n_neurons = 10
     sim.setup(timestep=1.0)
     pop_1 = sim.Population(n_neurons, sim.IF_curr_exp(), label="pop_1")
     view1 = PopulationView(pop_1, [1, 3, 5, 7, 9], label="Odds")
     view2 = PopulationView(view1, [1, 3], label="AlternativeOdds")
     # Not a normal way to access but good to test
     self.assertEqual([3, 7], view2._indexes)
     self.assertEqual(view2.parent, view1)
     self.assertEqual(view1.grandparent, pop_1)
     self.assertEqual(view2.grandparent, pop_1)
     cells = view2.all_cells
     self.assertEqual(3, cells[0].id)
     self.assertEqual(7, cells[1].id)
     self.assertEqual(3, view1.id_to_index(7))
     self.assertEqual([3, 0], view1.id_to_index([7, 1]))
     self.assertEqual(1, view2.id_to_index(7))
     view3 = view1[1:3]
     self.assertEqual([3, 5], view3._indexes)
     view4 = view1.sample(2)
     self.assertEqual(2, len(view4._indexes))
     sim.end()
Example #8
0
def run_script(simtime,
               n_neurons,
               run_split=1,
               record_spikes=False,
               spike_rate=None,
               spike_rec_indexes=None,
               spike_get_indexes=None,
               record_v=False,
               v_rate=None,
               v_rec_indexes=None,
               v_get_indexes=None,
               record_exc=False,
               exc_rate=None,
               exc_rec_indexes=None,
               exc_get_indexes=None,
               record_inh=False,
               inh_rate=None,
               inh_rec_indexes=None,
               inh_get_indexes=None,
               file_prefix=""):

    sim.setup(timestep=1)

    pop_1 = sim.Population(n_neurons, sim.IF_curr_exp(), label="pop_1")
    input1 = sim.Population(1,
                            sim.SpikeSourceArray(spike_times=[0]),
                            label="input")
    sim.Projection(input1,
                   pop_1,
                   sim.AllToAllConnector(),
                   synapse_type=sim.StaticSynapse(weight=5, delay=1))
    input2 = sim.Population(n_neurons,
                            sim.SpikeSourcePoisson(rate=100.0),
                            label="Stim_Exc",
                            additional_parameters={"seed": 1})
    sim.Projection(input2,
                   pop_1,
                   sim.OneToOneConnector(),
                   synapse_type=sim.StaticSynapse(weight=5, delay=1))
    if record_spikes:
        if spike_rec_indexes is None:
            pop_1.record(['spikes'], sampling_interval=spike_rate)
        else:
            view = PopulationView(pop_1, spike_rec_indexes)
            view.record(['spikes'], sampling_interval=spike_rate)
    if record_v:
        if v_rec_indexes is None:
            pop_1.record(['v'], sampling_interval=v_rate)
        else:
            view = PopulationView(pop_1, v_rec_indexes)
            view.record(['v'], sampling_interval=v_rate)
    if record_exc:
        if exc_rec_indexes is None:
            pop_1.record(['gsyn_exc'], sampling_interval=exc_rate)
        else:
            view = PopulationView(pop_1, exc_rec_indexes)
            view.record(['gsyn_exc'], sampling_interval=exc_rate)
    if record_inh:
        if inh_rec_indexes is None:
            pop_1.record(['gsyn_inh'], sampling_interval=inh_rate)
        else:
            view = PopulationView(pop_1, inh_rec_indexes)
            view.record(['gsyn_inh'], sampling_interval=inh_rate)
    for i in range(run_split):
        sim.run(simtime / run_split)

    if record_spikes:
        if spike_get_indexes is None:
            neo = pop_1.get_data("spikes")
        else:
            view = PopulationView(pop_1, spike_get_indexes)
            neo = view.get_data("spikes")
        spikes = neo.segments[0].spiketrains
        spike_file = os.path.join(current_file_path,
                                  file_prefix + "spikes.csv")
        write_spikes(spikes, spike_file)
    else:
        spikes = None

    if record_v:
        if v_get_indexes is None:
            neo = pop_1.get_data("v")
        else:
            view = PopulationView(pop_1, v_get_indexes)
            neo = view.get_data("v")
        v = neo.segments[0].filter(name='v')[0]
        v_file = os.path.join(current_file_path, file_prefix + "v.csv")
        numpy.savetxt(v_file, v, delimiter=',')
    else:
        v = None

    if record_exc:
        if exc_get_indexes is None:
            neo = pop_1.get_data('gsyn_exc')
        else:
            view = PopulationView(pop_1, exc_get_indexes)
            neo = view.get_data('gsyn_exc')
        exc = neo.segments[0].filter(name='gsyn_exc')[0]
        exc_file = os.path.join(current_file_path, file_prefix + "exc.csv")
        numpy.savetxt(exc_file, exc, delimiter=',')
    else:
        exc = None
    if record_inh:
        if inh_get_indexes is None:
            neo = pop_1.get_data('gsyn_inh')
        else:
            view = PopulationView(pop_1, inh_get_indexes)
            neo = view.get_data('gsyn_inh')
        inh = neo.segments[0].filter(name='gsyn_inh')[0]
        inh_file = os.path.join(current_file_path, file_prefix + "inh.csv")
        numpy.savetxt(inh_file, inh, delimiter=',')
    else:
        inh = None

    sim.end()

    return spikes, v, exc, inh