Skip to content

Commit

Permalink
Fix the sample with empty group NodePopulation.ids (#83)
Browse files Browse the repository at this point in the history
* Fix the sample raise if the group return empty list and sample is used
  • Loading branch information
tomdele committed Jul 28, 2020
1 parent b06326d commit 419a31d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bluepysnap/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ def ids(self, group=None, limit=None, sample=None):
result = utils.ensure_list(group)
self._check_ids(result)
preserve_order = isinstance(group, collections.Sequence)

if sample is not None:
result = np.random.choice(result, sample, replace=False)
if len(result) > 0:
result = np.random.choice(result, sample, replace=False)
preserve_order = False
if limit is not None:
result = result[:limit]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def test_ids(self):
npt.assert_equal(_call(group=[]), [])
npt.assert_equal(_call(limit=1), [0])
npt.assert_equal(len(_call(sample=2)), 2)
npt.assert_equal(_call(group=[], sample=2), [])
npt.assert_equal(_call(group={Cell.MTYPE: "unknown"}, sample=2), [])
npt.assert_equal(_call(0), [0])
npt.assert_equal(_call([0, 1]), [0, 1])
npt.assert_equal(_call([1, 0, 1]), [1, 0, 1]) # order and duplicates preserved
Expand Down

0 comments on commit 419a31d

Please sign in to comment.