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?
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: