Skip to content

Commit

Permalink
Cache node and edge population _properties (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaFicarelli authored Jun 12, 2023
1 parent ac75933 commit 0b59c4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 9 additions & 4 deletions bluepysnap/edges/edge_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ def __init__(self, circuit, population_name):
self._circuit = circuit
self.name = population_name

@property
@cached_property
def _properties(self):
"""Edge population properties."""
return self._circuit.to_libsonata.edge_population_properties(self.name)

@property
def _population(self):
"""Libsonata edge population.
Not cached because it would keep the hdf5 file open.
"""
return self._circuit.to_libsonata.edge_population(self.name)

@staticmethod
Expand Down Expand Up @@ -590,10 +595,10 @@ def spatial_synapse_index(self):
)
) from e

properties = self._circuit.to_libsonata.edge_population_properties(self.name)
if not properties.spatial_synapse_index_dir:
index_dir = self._properties.spatial_synapse_index_dir
if not index_dir:
raise BluepySnapError(f"It appears {self.name} does not have synapse indices")
return open_index(properties.spatial_synapse_index_dir)
return open_index(index_dir)

def __getstate__(self):
"""Make EdgePopulation pickle-able, without storing state of caches."""
Expand Down
15 changes: 9 additions & 6 deletions bluepysnap/nodes/node_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,17 @@ def _get_data(self, properties=None, node_ids=None):
result.insert(n + loc, name, values)
return result

@property
@cached_property
def _properties(self):
"""Node population properties."""
return self._circuit.to_libsonata.node_population_properties(self.name)

@property
def _population(self):
"""Libsonata node population.
Not cached because it would keep the hdf5 file open.
"""
return self._circuit.to_libsonata.node_population(self.name)

@cached_property
Expand Down Expand Up @@ -665,12 +670,10 @@ def spatial_segment_index(self):
)
) from e

properties = self._circuit.to_libsonata.node_population_properties(self.name)

if not properties.spatial_segment_index_dir:
index_dir = self._properties.spatial_segment_index_dir
if not index_dir:
raise BluepySnapError(f"It appears {self.name} does not have segment indices")

return open_index(properties.spatial_segment_index_dir)
return open_index(index_dir)

def __getstate__(self):
"""Make NodePopulation pickle-able, without storing state of caches."""
Expand Down

0 comments on commit 0b59c4d

Please sign in to comment.