nest-simulator icon indicating copy to clipboard operation
nest-simulator copied to clipboard

Fix for GetLocalNodeCollection introduced regression

Open heplesser opened this issue 1 month ago • 2 comments

A bugfix for GetLocalNodeCollection() in #3632 resulted in the following regression.

In NEST 3.9, the following code works correctly:

In [10]: n = nest.Create("parrot_neuron", positions=nest.spatial.free(pos=[(i/10, 0) for i in range(4)]))
In [11]: n.spatial['positions']
Out[11]: ((0.0, 0.0), (0.1, 0.0), (0.2, 0.0), (0.3, 0.0))
In [12]: s = nest.GetLocalNodeCollection(n)
In [13]: s.spatial['positions']
Out[13]: ((0.0, 0.0), (0.1, 0.0), (0.2, 0.0), (0.3, 0.0))

Indeed, interacting directly with SLI one can see that the underlying Take functions selects the correct positions:

In [14]: nest.ll_api.sps(n)
In [15]: nest.ll_api.sr("[2 4 2] Take")
In [16]: t = nest.ll_api.spp()
In [17]: t
Out[17]: 
NodeCollection(metadata=spatial,
               model=parrot_neuron, size=2, first=2, last=4, step=2)
In [18]: t.spatial['positions']
Out[18]: ((0.1, 0.0), (0.3, 0.0))

In NEST master after the merge of #3632, the spatial information is lost through slicing:

In [2]: n = nest.Create("parrot_neuron", positions=nest.spatial.free(pos=[(i/10, 0) for i in range(4)]))
In [3]: n.spatial['positions']
Out[3]: ((0.0, 0.0), (0.1, 0.0), (0.2, 0.0), (0.3, 0.0))
In [4]: s = nest.GetLocalNodeCollection(n)
In [5]: s.spatial['positions']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 s.spatial['positions']

TypeError: 'NoneType' object is not subscriptable

heplesser avatar Dec 09 '25 07:12 heplesser

The cause of the problem appears to be that the SLI Take function preserves spatial metadata, while the new and correct solution implemented in #3632 uses boolean indexing here

https://github.com/nest/nest-simulator/blob/cd8c487ee959eb0d87ca73d9996b914d8795727e/pynest/nest/lib/hl_api_nodes.py#L215

which in the end just creates a new node collection from a list of node ids:

https://github.com/nest/nest-simulator/blob/cd8c487ee959eb0d87ca73d9996b914d8795727e/nestkernel/nest.cpp#L415

Here, we need to find a way to transfer the correct spatial metadata as well and to ensure with tests that this also works well when node collections contain devices (which is where the old solution was incorrect and what was fixed by #3632).

heplesser avatar Dec 09 '25 10:12 heplesser

@heplesser the general case is how would you retrieve the spatial information from a sliced node collection and how would we handle the case of mixing two different node collections with different spatial metadata.

med-ayssar avatar Dec 10 '25 10:12 med-ayssar