Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #40 #42

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bluepysnap/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ def edges(self):
self._config['networks']['edges'],
lambda cfg: EdgeStorage(cfg, self)
)

def close_contexts(self):
"""Close the context for all populations."""
tomdele marked this conversation as resolved.
Show resolved Hide resolved
if self.nodes:
for population in self.nodes.values():
population.close_context()
if self.edges:
for population in self.edges.values():
population.close_context()
10 changes: 7 additions & 3 deletions bluepysnap/edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, config, circuit):
self._circuit = circuit
self._populations = {}

@cached_property
wizmer marked this conversation as resolved.
Show resolved Hide resolved
@property
def storage(self):
"""Access to the libsonata edge storage."""
return libsonata.EdgeStorage(self._h5_filepath)
Expand Down Expand Up @@ -113,7 +113,12 @@ def __init__(self, edge_storage, population_name):
def _population(self):
return self._edge_storage.storage.open_population(self.name)

@property
def close_context(self):
"""Close the h5 context for edge population."""
if "_population" in self.__dict__:
del self.__dict__["_population"]

@cached_property
def size(self):
"""Population size."""
return self._population.size
Expand Down Expand Up @@ -396,7 +401,6 @@ def _optimal_direction():
secondary_node_ids = np.unique(secondary_node_ids)

secondary_node_ids_used = set()

for key_node_id in primary_node_ids:
connected_node_ids = get_connected_node_ids(key_node_id, unique=False)
connected_node_ids_with_count = np.stack(
Expand Down
7 changes: 6 additions & 1 deletion bluepysnap/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, config, circuit):
self._circuit = circuit
self._populations = {}

@cached_property
@property
def storage(self):
"""Access to the libsonata node storage."""
return libsonata.NodeStorage(self._h5_filepath)
Expand Down Expand Up @@ -176,6 +176,11 @@ def _data(self):
def _population(self):
return self._node_storage.storage.open_population(self.name)

def close_context(self):
"""Close the h5 context for node population."""
if "_population" in self.__dict__:
del self.__dict__["_population"]

@cached_property
def size(self):
"""Node population size."""
Expand Down
30 changes: 24 additions & 6 deletions tests/test_circuit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import h5py

from bluepysnap.nodes import NodePopulation
from bluepysnap.edges import EdgePopulation
Expand All @@ -11,12 +12,12 @@
def test_all():
circuit = test_module.Circuit(
str(TEST_DATA_DIR / 'circuit_config.json'))
assert(
circuit.config['networks']['nodes'][0] ==
{
'nodes_file': str(TEST_DATA_DIR / 'nodes.h5'),
'node_types_file': None,
}
assert (
circuit.config['networks']['nodes'][0] ==
{
'nodes_file': str(TEST_DATA_DIR / 'nodes.h5'),
'node_types_file': None,
}
)
assert isinstance(circuit.nodes, dict)
assert isinstance(circuit.edges, dict)
Expand All @@ -34,3 +35,20 @@ def test_duplicate_population():
with pytest.raises(BluepySnapError):
circuit.nodes


def test_close_contexts():
circuit = test_module.Circuit(
str(TEST_DATA_DIR / 'circuit_config.json')
)
circuit.edges['default'].size
circuit.nodes['default'].size
node_file = circuit.config['networks']['nodes'][0]['nodes_file']
edge_file = circuit.config['networks']['edges'][0]['edges_file']

circuit.close_contexts()

with h5py.File(node_file, "r+") as h5:
list(h5)

with h5py.File(edge_file, "r+") as h5:
list(h5)
1 change: 1 addition & 0 deletions tests/test_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd
import pandas.testing as pdt
import pytest
import h5py

import libsonata
from mock import Mock
Expand Down
3 changes: 2 additions & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
import numpy.testing as npt
import pandas as pd
import pandas.util.testing as pdt
import pandas.testing as pdt
import pytest
import h5py

import libsonata
from mock import Mock
Expand Down