Skip to content

Commit

Permalink
Merge pull request #2882 from nicolossus/fix_issue_2637
Browse files Browse the repository at this point in the history
Allow `NodeCollection` slicing with list of NumPy integers
  • Loading branch information
Helveg authored Aug 10, 2023
2 parents 6ee813f + 83220ea commit cd1cdcb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pynest/nest/lib/hl_api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def __getitem__(self, key):
raise IndexError("Bool index array must be the same length as NodeCollection")
np_key = numpy.array(key, dtype=bool)
# Checking that elements are not instances of bool too, because bool inherits from int
elif all(isinstance(x, int) and not isinstance(x, bool) for x in key):
elif all(isinstance(x, (int, numpy.integer)) and not isinstance(x, bool) for x in key):
np_key = numpy.array(key, dtype=numpy.uint64)
if len(numpy.unique(np_key)) != len(np_key):
raise ValueError("All node IDs in a NodeCollection have to be unique")
Expand Down
44 changes: 44 additions & 0 deletions testsuite/pytests/sli2py_regressions/test_issue_2637.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
#
# test_issue_2637.py
#
# This file is part of NEST.
#
# Copyright (C) 2004 The NEST Initiative
#
# NEST is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# NEST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

"""
Regression test for Issue #2637 (GitHub).
This set of tests ensures that a ``NodeCollection`` can be sliced with
a Python ``list`` of NumPy integers.
"""

import numpy as np
import pytest

import nest


@pytest.mark.parametrize("dtype", [int, np.int32, np.int64])
def test_nc_slice_list_of_numpy_ints(dtype):
"""
Ensure that a list of NumPy integers can slice a ``NodeCollection``.
"""

nc = nest.Create("iaf_psc_alpha", 2)
slice_arr = np.array([0, 1], dtype=dtype)
slice_lst = list(slice_arr)
nc_slice = nc[slice_lst]

0 comments on commit cd1cdcb

Please sign in to comment.