examples/example_24_large_scale_brian2_simulation/clusternet.py
def _build_connections(self, traj, brian_list, network_dict):
        """Connects neuron groups `neurons_i` and `neurons_e`.

        Adds all connections to `brian_list` and adds a list of connections
        with the key 'connections' to the `network_dict`.

        """

        connections = traj.connections

        neurons_i = network_dict['neurons_i']
        neurons_e = network_dict['neurons_e']

        print('Connecting ii')
        self.conn_ii = Synapses(neurons_i,neurons_i, on_pre='y_i += %f' % connections.J_ii)
        self.conn_ii.connect('i != j', p=connections.p_ii)

        print('Connecting ei')
        self.conn_ei = Synapses(neurons_i,neurons_e, on_pre='y_i += %f' % connections.J_ei)
        self.conn_ei.connect('i != j', p=connections.p_ei)

        print('Connecting ie')
        self.conn_ie = Synapses(neurons_e,neurons_i, on_pre='y_e += %f' % connections.J_ie)
        self.conn_ie.connect('i != j', p=connections.p_ie)

        conns_list = [self.conn_ii, self.conn_ei, self.conn_ie]


        if connections.R_ee > 1.0:
            # If we come here we want to create clusters

            cluster_list=[]
            cluster_conns_list=[]
            model=traj.model

            # Compute the number of clusters
            clusters = int(model.N_e/connections.clustersize_e)
            traj.f_add_derived_parameter('connections.clusters', clusters, comment='Number of clusters')

            # Compute outgoing connection probability
            p_out = (connections.p_ee*model.N_e) / \
                    (connections.R_ee*connections.clustersize_e+model.N_e- connections.clustersize_e)

            # Compute within cluster connection probability
            p_in = p_out * connections.R_ee

            # We keep these derived parameters
            traj.f_add_derived_parameter('connections.p_ee_in', p_in ,
                                         comment='Connection prob within cluster')
            traj.f_add_derived_parameter('connections.p_ee_out', p_out ,
                                         comment='Connection prob to outside of cluster')


            low_index = 0
            high_index = connections.clustersize_e
            # Iterate through cluster and connect within clusters and to the rest of the neurons
            for irun in range(clusters):

                cluster = neurons_e[low_index:high_index]

                # Connections within cluster
                print('Connecting ee cluster #%d of %d' % (irun, clusters))
                conn = Synapses(cluster,cluster,
                                on_pre='y_e += %f' % (connections.J_ee*connections.strength_factor))
                conn.connect('i != j', p=p_in)
                cluster_conns_list.append(conn)

                # Connections reaching out from cluster
                # A cluster consists of `clustersize_e` neurons with consecutive indices.
                # So usually the outside world consists of two groups, neurons with lower
                # indices than the cluster indices, and neurons with higher indices.
                # Only the clusters at the index boundaries project to neurons with only either
                # lower or higher indices
                if low_index > 0:
                    rest_low = neurons_e[0:low_index]
                    print('Connecting cluster with other neurons of lower index')
                    low_conn = Synapses(cluster,rest_low,
                                on_pre='y_e += %f' % connections.J_ee)
                    low_conn.connect('i != j', p=p_out)

                    cluster_conns_list.append(low_conn)

                if high_index < model.N_e:
                    rest_high = neurons_e[high_index:model.N_e]
                    print('Connecting cluster with other neurons of higher index')

                    high_conn = Synapses(cluster,rest_high,
                                on_pre='y_e += %f' % connections.J_ee)
                    high_conn.connect('i != j', p=p_out)

                    cluster_conns_list.append(high_conn)

                low_index=high_index
                high_index+=connections.clustersize_e

            self.cluster_conns=cluster_conns_list
            conns_list+=cluster_conns_list
        else:
            # Here we don't cluster and connection probabilities are homogeneous
            print('Connectiong ee')

            self.conn_ee = Synapses(neurons_e,neurons_e,
                                on_pre='y_e += %f' % connections.J_ee)
            self.conn_ee.connect('i != j', p=connections.p_ee)

            conns_list.append(self.conn_ee)


        # Add the connections to the `brian_list` and the network dict
        brian_list.extend(conns_list)
        network_dict['connections'] = conns_list
def _build_connections(self, traj, brian_list, network_dict):
        """Connects neuron groups `neurons_i` and `neurons_e`.

        Adds all connections to `brian_list` and adds a list of connections
        with the key 'connections' to the `network_dict`.

        """

        connections = traj.connections

        neurons_i = network_dict['neurons_i']
        neurons_e = network_dict['neurons_e']

        print('Connecting ii')
        self.conn_ii = Synapses(neurons_i,neurons_i, on_pre='y_i += %f' % connections.J_ii)
        self.conn_ii.connect('i != j', p=connections.p_ii)

        print('Connecting ei')
        self.conn_ei = Synapses(neurons_i,neurons_e, on_pre='y_i += %f' % connections.J_ei)
        self.conn_ei.connect('i != j', p=connections.p_ei)

        print('Connecting ie')
        self.conn_ie = Synapses(neurons_e,neurons_i, on_pre='y_e += %f' % connections.J_ie)
        self.conn_ie.connect('i != j', p=connections.p_ie)

        conns_list = [self.conn_ii, self.conn_ei, self.conn_ie]


        if connections.R_ee > 1.0:
            # If we come here we want to create clusters

            cluster_list=[]
            cluster_conns_list=[]
            model=traj.model

            # Compute the number of clusters
            clusters = int(model.N_e/connections.clustersize_e)
            traj.f_add_derived_parameter('connections.clusters', clusters, comment='Number of clusters')

            # Compute outgoing connection probability
            p_out = (connections.p_ee*model.N_e) / \
                    (connections.R_ee*connections.clustersize_e+model.N_e- connections.clustersize_e)

            # Compute within cluster connection probability
            p_in = p_out * connections.R_ee

            # We keep these derived parameters
            traj.f_add_derived_parameter('connections.p_ee_in', p_in ,
                                         comment='Connection prob within cluster')
            traj.f_add_derived_parameter('connections.p_ee_out', p_out ,
                                         comment='Connection prob to outside of cluster')


            low_index = 0
            high_index = connections.clustersize_e
            # Iterate through cluster and connect within clusters and to the rest of the neurons
            for irun in range(clusters):

                cluster = neurons_e[low_index:high_index]

                # Connections within cluster
                print('Connecting ee cluster #%d of %d' % (irun, clusters))
                conn = Synapses(cluster,cluster,
                                on_pre='y_e += %f' % (connections.J_ee*connections.strength_factor))
                conn.connect('i != j', p=p_in)
                cluster_conns_list.append(conn)

                # Connections reaching out from cluster
                # A cluster consists of `clustersize_e` neurons with consecutive indices.
                # So usually the outside world consists of two groups, neurons with lower
                # indices than the cluster indices, and neurons with higher indices.
                # Only the clusters at the index boundaries project to neurons with only either
                # lower or higher indices
                if low_index > 0:
                    rest_low = neurons_e[0:low_index]
                    print('Connecting cluster with other neurons of lower index')
                    low_conn = Synapses(cluster,rest_low,
                                on_pre='y_e += %f' % connections.J_ee)
                    low_conn.connect('i != j', p=p_out)

                    cluster_conns_list.append(low_conn)

                if high_index < model.N_e:
                    rest_high = neurons_e[high_index:model.N_e]
                    print('Connecting cluster with other neurons of higher index')

                    high_conn = Synapses(cluster,rest_high,
                                on_pre='y_e += %f' % connections.J_ee)
                    high_conn.connect('i != j', p=p_out)

                    cluster_conns_list.append(high_conn)

                low_index=high_index
                high_index+=connections.clustersize_e

            self.cluster_conns=cluster_conns_list
            conns_list+=cluster_conns_list
        else:
            # Here we don't cluster and connection probabilities are homogeneous
            print('Connectiong ee')

            self.conn_ee = Synapses(neurons_e,neurons_e,
                                on_pre='y_e += %f' % connections.J_ee)
            self.conn_ee.connect('i != j', p=connections.p_ee)

            conns_list.append(self.conn_ee)


        # Add the connections to the `brian_list` and the network dict
        brian_list.extend(conns_list)
        network_dict['connections'] = conns_list
Connects neuron groups `neurons_i` and `neurons_e`.

        Adds all connections to `brian_list` and adds a list of connections
        with the key 'connections' to the `network_dict`.