I am exploring neural network simulations between these "neurons" that you see in the figure below. The difficulty I have is connecting each neuron to its nearest neighbor. I first figured that I could do it by tuple unpacking, but it has become very complicated.

def _connect_cells(self):
    for source, target in zip(self.cells, self.cells[1:] + [self.cells[0]]):
        nc = h.NetCon(source.soma(0.5)._ref_v, target.syn, sec=source.soma)
        nc.weight[0] = self._syn_w
        nc.delay = self._syn_delay
        source._ncs.append(nc)

In this example code snippet, the tuple unpacking is configured such that the i th neuron connects to i + 1 th neuron until neuron n. When it reaches neuron n, the n th neuron will connect back to the first neuron. This tuple unpacking is for a network structure resembling a ring of neurons.

However, in my case, the structure is a grid of n x n neurons. The list bellow corresponds to the neurons:

20 21 22 23 24
15 16 17 18 19
10 11 12 13 14
 5  6  7  8  9
 0  1  2  3  4

The above tuple unpacking would not work for this because neuron 4 is not supposed to connect to neuron 5. The list goes exclusively left to right due to how the neurons are created. The exact connection that I am trying to achieve is shown in the figure below. It is possible to do it manually (it would take a lot of code), but how can I approach it with a for loop in the same way as the example code?

Connection matrix

1

There are 1 best solutions below

0
On

If I understand correctly you want each neuron in a square grid connect to each neighbouring neuron, horizontally, vertically or diagonally. This will do the job:

def connect(side):
    maxcol = side-1
    maxrow = side*maxcol
    neurons = list(range(side**2))
    conns = []
    for col in range(side):
        for row in range(0, side**2, side):
            if col < maxcol: #connect left
                conns.append((neurons[row+col],neurons[row+col+1]))
                if row > 0: #connect left up
                    conns.append((neurons[row+col],neurons[row+col+1-side]))
                if row < maxrow: #connect left down
                    conns.append((neurons[row+col],neurons[row+col+1+side]))
            if row < maxrow: #connect down
                conns.append((neurons[row+col],neurons[row+col+side]))
    return conns

connect(5)
[(0, 1), (0, 6), (0, 5), (5, 6), (5, 1), (5, 11), (5, 10), (10, 11), (10, 6), (10, 16), (10, 15), (15, 16), (15, 11), (15, 21), (15, 20), (20, 21), (20, 16), (1, 2), (1, 7), (1, 6), (6, 7), (6, 2), (6, 12), (6, 11), (11, 12), (11, 7), (11, 17), (11, 16), (16, 17), (16, 12), (16, 22), (16, 21), (21, 22), (21, 17), (2, 3), (2, 8), (2, 7), (7, 8), (7, 3), (7, 13), (7, 12), (12, 13), (12, 8), (12, 18), (12, 17), (17, 18), (17, 13), (17, 23), (17, 22), (22, 23), (22, 18), (3, 4), (3, 9), (3, 8), (8, 9), (8, 4), (8, 14), (8, 13), (13, 14), (13, 9), (13, 19), (13, 18), (18, 19), (18, 14), (18, 24), (18, 23), (23, 24), (23, 19), (4, 9), (9, 14), (14, 19), (19, 24)]