From ab6c6ee141f88547c370794843c65bf20f65760f Mon Sep 17 00:00:00 2001 From: Matthias Wolf Date: Tue, 11 Oct 2022 08:15:41 -0400 Subject: [PATCH] Fix #226 for empty selections (#232) --- python/tests/test.py | 10 ++++++++++ src/population.hpp | 4 +++- tests/test_nodes.cpp | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/python/tests/test.py b/python/tests/test.py index 885a3d0f..7a92dc3c 100644 --- a/python/tests/test.py +++ b/python/tests/test.py @@ -163,6 +163,14 @@ def test_enumeration_names(self): ) def test_enumeration_values(self): + self.assertEqual( + self.test_obj.get_attribute( + "E-mapping-good", + Selection([]) + ).tolist(), + [] + ) + self.assertEqual( self.test_obj.get_attribute( "E-mapping-good", @@ -240,10 +248,12 @@ def test_target(self): def test_source_nodes(self): self.assertEqual(self.test_obj.source_node(1), 1) self.assertEqual(self.test_obj.source_nodes(Selection([0, 1, 2, 4])).tolist(), [1, 1, 2, 3]) + self.assertEqual(self.test_obj.source_nodes(Selection([])).tolist(), []) def test_target_nodes(self): self.assertEqual(self.test_obj.target_node(1), 2) self.assertEqual(self.test_obj.target_nodes(Selection([0, 1, 2, 4])).tolist(), [1, 2, 1, 0]) + self.assertEqual(self.test_obj.target_nodes(Selection([])).tolist(), []) def test_afferent_edges(self): self.assertEqual(self.test_obj.afferent_edges([1, 2]).ranges, [(0, 4), (5, 6)]) diff --git a/src/population.hpp b/src/population.hpp index fbed5c44..f55541e3 100644 --- a/src/population.hpp +++ b/src/population.hpp @@ -89,7 +89,9 @@ std::vector _readSelection(const HighFive::DataSet& dset, const Selection& se template ::value>::type* = nullptr> std::vector _readSelection(const HighFive::DataSet& dset, const Selection& selection) { - if (selection.ranges().size() == 1) { + if (selection.ranges().empty()) { + return {}; + } else if (selection.ranges().size() == 1) { return _readChunk(dset, selection.ranges().front()); } diff --git a/tests/test_nodes.cpp b/tests/test_nodes.cpp index 9b129eb0..62a4ca47 100644 --- a/tests/test_nodes.cpp +++ b/tests/test_nodes.cpp @@ -38,6 +38,8 @@ TEST_CASE("NodePopulation", "[base]") { REQUIRE(population.enumerationNames() == std::set{"E-mapping-good", "E-mapping-bad"}); + CHECK(population.getAttribute("attr-X", Selection({})) == + std::vector{}); CHECK(population.getAttribute("attr-X", Selection({{0, 1}, {5, 6}})) == std::vector{11.0, 16.0}); CHECK(population.getAttribute("attr-X", Selection({{0, 1}})) ==