Пример #1
0
def test_network_operations():
    # test NetworkOperation and network_operation
    seq = []
    def f1():
        seq.append('a')
    op1 = NetworkOperation(f1, when='start', order=1)
    @network_operation
    def f2():
        seq.append('b')
    @network_operation(when='end', order=1)
    def f3():
        seq.append('c')

    # In complex frameworks, network operations might be object methods that
    # access some common data
    class Container(object):
        def __init__(self):
            self.g1_data = 'B'
            self.g2_data = 'C'

        def g1(self):
            seq.append(self.g1_data)

        def g2(self):
            seq.append(self.g2_data)

    c = Container()
    c_op1 = NetworkOperation(c.g1)
    c_op2 = NetworkOperation(c.g2, when='end', order=1)
    net = Network(op1, f2, f3, c_op1, c_op2)
    net.run(1*ms)

    assert_equal(''.join(seq), 'bBacC'*10)
Пример #2
0
def test_incorrect_network_operations():
    # Network operations with more than one argument are not allowed
    def func(x, y):
        pass

    class Container(object):
        def func(self, x, y):
            pass

    c = Container()

    assert_raises(TypeError, lambda: NetworkOperation(func))
    assert_raises(TypeError, lambda: NetworkOperation(c.func))

    # Incorrect use of @network_operation -- it does not work on an instance
    # method
    try:

        class Container(object):
            @network_operation
            def func(self):
                pass

        raise AssertionError('expected a TypeError')
    except TypeError:
        pass  # this is what we expected
Пример #3
0
    def __init__(self, group=None):
        if group is None:
            # default to Izhikevich model neuron
            eq = '''dv/dt = (0.04*active/ms/mV + 0.04/ms/mV)*v**2+(5/ms)*v+140*mV/ms-w + I : volt (unless refractory)
                    dw/dt = (0.02/ms)*((0.2/ms)*v-w) : volt/second
                    active : 1
                    I : volt/second'''
            # create 1 neuron with 1 output
            self.group = Neuron(
                NeuronGroup(1,
                            eq,
                            threshold='v > 50*mV',
                            reset='v = -50*mV',
                            refractory=10 * ms,
                            method='rk2'))
        else:
            self.group = group

        self.state_monitor = StateMonitor(self.group, 'v',
                                          record=True)  # monitor voltages
        self.spike_monitor = SpikeMonitor(self.group)
        self.operator = NetworkOperation(self.update_active, dt=100 * ms)

        # initialise network object for neuron to run in and add elements
        self.network = Network()
        self.network.add(self.group)
        self.network.add(self.state_monitor)
        self.network.add(self.spike_monitor)
        self.network.add(self.operator)

        self.input = 0
Пример #4
0
def test_network_operations():
    # test NetworkOperation and network_operation
    seq = []

    def f1():
        seq.append('a')

    op1 = NetworkOperation(f1, when=('start', 1))

    @network_operation
    def f2():
        seq.append('b')

    @network_operation(when=('end', 1))
    def f3():
        seq.append('c')

    run(1 * ms)
    assert_equal(''.join(seq), 'bac' * 10)
    def construct_network_operations(self, cpu4_method):
        # Set up network operations
        self.extract_heading_net_op = NetworkOperation(self.extract_heading,
                                                       dt=self.time_step * ms,
                                                       when='start',
                                                       order=0,
                                                       name='extract_heading')
        self.extract_velocity_net_op = NetworkOperation(
            self.extract_velocity,
            dt=self.time_step * ms,
            when='start',
            order=1,
            name='extract_velocity')

        if cpu4_method == 1:
            self.CPU4_accumulator_net_op = NetworkOperation(
                self.CPU4_accumulator_outbound_method1,
                dt=self.time_step * ms,
                when='start',
                order=2,
                name='CPU4_accumulator_outbound')
            self.CPU4_accumulator_inbound_net_op = NetworkOperation(
                self.CPU4_accumulator_inbound_method1,
                dt=self.time_step * ms,
                when='start',
                order=2,
                name='CPU4_accumulator_inbound')
        elif cpu4_method == 2:
            self.CPU4_accumulator_net_op = NetworkOperation(
                self.CPU4_accumulator_outbound_method2,
                dt=self.time_step * ms,
                when='start',
                order=2,
                name='CPU4_accumulator_outbound')
            self.CPU4_accumulator_inbound_net_op = NetworkOperation(
                self.CPU4_accumulator_inbound_method2,
                dt=self.time_step * ms,
                when='start',
                order=2,
                name='CPU4_accumulator_inbound')
        else:
            print('Choose cpu4_method to be 1 or 2')
            return

        self.update_inputs_net_op = NetworkOperation(self.update_inputs,
                                                     dt=self.time_step * ms,
                                                     when='start',
                                                     order=3,
                                                     name='update_inputs')

        self.set_rates_net_op = NetworkOperation(self.set_rates,
                                                 dt=self.time_step * ms,
                                                 when='start',
                                                 order=4,
                                                 name='set_rates')

        return [
            self.extract_heading_net_op, self.extract_velocity_net_op,
            self.CPU4_accumulator_net_op, self.CPU4_accumulator_inbound_net_op,
            self.update_inputs_net_op, self.set_rates_net_op
        ]
    def __init__(self,
                 neuron_eqs,
                 threshold_eqs,
                 reset_eqs,
                 sim_headings,
                 sim_velocities,
                 mem_gain_outbound,
                 decay_outbound,
                 mem_gain_inbound,
                 decay_inbound,
                 acceleration=0.3,
                 drag=0.15,
                 speed_multiplier=0,
                 rotation_factor=0.01,
                 max_velocity=12,
                 time_step=20,
                 T_outbound=1500,
                 T_inbound=1500,
                 headings_method='vonmises',
                 cpu4_method=1,
                 only_tuned_network=False,
                 follow_stone_rotation=False,
                 cx_log=None):

        ######################################
        ### PARAMETERS
        ######################################
        self.T_outbound = T_outbound
        self.T_inbound = T_inbound
        self.T = T_outbound + T_inbound

        self.time_step = time_step

        self.mem_gain_outbound = mem_gain_outbound
        self.mem_gain_inbound = mem_gain_inbound
        self.decay_outbound = decay_outbound
        self.decay_inbound = decay_inbound

        self.rotation_factor = rotation_factor
        self.max_velocity = max_velocity

        self.cpu4_method = cpu4_method

        self.acceleration = acceleration
        self.drag = drag
        self.speed_multiplier = speed_multiplier

        self.follow_stone_rotation = follow_stone_rotation
        # if self.follow_stone_rotation:
        #     self.rotation_factor = 1
        if cx_log:
            self.cx_log = cx_log
        if self.follow_stone_rotation and not cx_log:
            print('To follow rotation you need to provide a rate-based cx_log')

        self.populations_size()

        ######################################
        ### INPUTS
        ######################################
        self.headings_method = headings_method
        if self.headings_method == 'vonmises':
            headings = self.construct_headings_vonmises(
                sim_headings, sim_velocities)
        elif self.headings_method == 'cosine':
            headings = self.construct_headings_cosine(sim_headings,
                                                      sim_velocities)
        else:
            print('No headings_method selected')
            return
        flow = self.construct_flow(sim_headings, sim_velocities)
        self.inputs = [headings, flow]
        self.inputs_spike_monitors = self.create_inputs_spike_monitors()

        ######################################
        ### MEMORY
        ######################################
        memory_accumulator, spm_memory_accumulator = self.initialise_memory_accumulator(
        )
        self.inputs.append(memory_accumulator)
        self.inputs_spike_monitors.append(spm_memory_accumulator)

        ######################################
        ### NETWORK
        ######################################
        self.connectivity_matrices()

        self.neural_populations = self.construct_neural_populations(
            neuron_eqs, threshold_eqs, reset_eqs)

        self.populations_spike_monitors = self.create_populations_spike_monitors(
        )

        self.synapses = self.construct_synapses()

        ######################################
        ### NETWORK OPERATIONS
        ######################################
        self.network_operations_utilities()
        self.network_operations = self.construct_network_operations(
            self.cpu4_method)

        ######################################
        ### PLOTTING
        ######################################
        self.bee_coords = np.zeros((self.T, 2))

        self.update_bee_position_net_op = NetworkOperation(
            self.update_bee_position,
            dt=self.time_step * ms,
            when='end',
            order=4,
            name='update_bee_position')
        self.network_operations.append(self.update_bee_position_net_op)

        net = Network()
        net.add(self.inputs, self.inputs_spike_monitors,
                self.neural_populations, self.populations_spike_monitors,
                self.synapses, self.network_operations)
        self.net = net
        if only_tuned_network:
            self.only_tuned_network()
        self.net.store('initialised')