Skip to content

Commit

Permalink
Fix empty list in reports (#51)
Browse files Browse the repository at this point in the history
* Fix empty list in reports
* this is a temporary solution until
  BlueBrain/libsonata#84 is done
  • Loading branch information
tomdele committed Apr 8, 2020
1 parent 5147443 commit e1589b5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bluepysnap/frame_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from libsonata import ElementReportReader

from bluepysnap.exceptions import BluepySnapError
from bluepysnap.utils import fix_libsonata_empty_list

L = logging.getLogger(__name__)

Expand Down Expand Up @@ -211,6 +212,8 @@ def nodes(self):

def _resolve(self, group):
"""Transform a group into a node_id array."""
if group == []:
return fix_libsonata_empty_list()
return self.nodes.ids(group=group)


Expand Down
3 changes: 3 additions & 0 deletions bluepysnap/spike_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np

from bluepysnap.exceptions import BluepySnapError
from bluepysnap.utils import fix_libsonata_empty_list


def _get_reader(spike_report):
Expand Down Expand Up @@ -84,6 +85,8 @@ def nodes(self):

def _resolve_nodes(self, group):
"""Transform a node group into a node_id array."""
if group == []:
return fix_libsonata_empty_list()
return self.nodes.ids(group=group)

def get(self, group=None, t_start=None, t_stop=None):
Expand Down
8 changes: 8 additions & 0 deletions bluepysnap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def ensure_list(v):
return [v]


def fix_libsonata_empty_list():
"""Temporary solution to return empty list from libsonata report readers `.get` functions.
see: https://github.com/BlueBrain/libsonata/issues/84
"""
return np.array([-2])


def add_dynamic_prefix(properties):
"""Add the dynamic prefix to a list of properties."""
return [DYNAMICS_PREFIX + name for name in list(properties)]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_frame_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_nodes_invalid_population(self):
def test_get(self):
pdt.assert_frame_equal(self.test_obj.get(), self.df)

pdt.assert_frame_equal(self.test_obj.get([]), pd.DataFrame())

pdt.assert_frame_equal(self.test_obj.get(2), self.df.loc[:, [2]])

pdt.assert_frame_equal(self.test_obj.get([2, 0]), self.df.loc[:, [0, 2]])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_spike_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test__resolve_nodes(self):
def test_get(self):
pdt.assert_series_equal(self.test_obj.get(),
_create_series([2, 0, 1, 2, 0], [0.1, 0.2, 0.3, 0.7, 1.3]))

pdt.assert_series_equal(self.test_obj.get([]), _create_series([], []))
npt.assert_allclose(self.test_obj.get(2), np.array([0.1, 0.7]))
npt.assert_allclose(self.test_obj.get(0, t_start=1.), [1.3])
npt.assert_allclose(self.test_obj.get(0, t_stop=1.), [0.2])
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def test_ensure_list():
assert test_module.ensure_list('abc') == ['abc']


def test_fix_libsonata_empty_list():
npt.assert_array_equal(test_module.fix_libsonata_empty_list(), np.array([-2]))


def test_add_dynamic_prefix():
assert test_module.add_dynamic_prefix(["a", "b"]) == [DYNAMICS_PREFIX+"a", DYNAMICS_PREFIX+"b"]

Expand Down

0 comments on commit e1589b5

Please sign in to comment.