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 all 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
44 changes: 36 additions & 8 deletions bluepysnap/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, config):
Circuit: A Circuit object.
"""
self._config = Config(config).resolve()
self.is_open = True

@property
def config(self):
Expand All @@ -58,15 +59,42 @@ def config(self):
@cached_property
def nodes(self):
"""Access to node population(s). See :py:class:`~bluepysnap.nodes.NodePopulation`."""
return _collect_populations(
self._config['networks']['nodes'],
lambda cfg: NodeStorage(cfg, self)
)
if self.is_open:
return _collect_populations(
self._config['networks']['nodes'],
lambda cfg: NodeStorage(cfg, self)
)
raise BluepySnapError("I/O error. Cannot access the h5 files with closed context.")

@cached_property
def edges(self):
"""Access to edge population(s). See :py:class:`~bluepysnap.edges.EdgePopulation`."""
return _collect_populations(
self._config['networks']['edges'],
lambda cfg: EdgeStorage(cfg, self)
)
if self.is_open:
return _collect_populations(
self._config['networks']['edges'],
lambda cfg: EdgeStorage(cfg, self)
)
raise BluepySnapError("I/O error. Cannot access the h5 files with closed context.")

def __exit__(self, exc_type, exc_val, exc_tb):
"""Close the context for all populations."""
tomdele marked this conversation as resolved.
Show resolved Hide resolved

def _close_context(pop):
"""Close the h5 context for population."""
if "_population" in pop.__dict__:
del pop.__dict__["_population"]

if self.nodes:
for population in self.nodes.values():
_close_context(population)
if self.edges:
for population in self.edges.values():
_close_context(population)

del self.__dict__["nodes"]
del self.__dict__["edges"]
self.is_open = False

def __enter__(self):
"""Enter the context manager for circuit."""
return self
9 changes: 5 additions & 4 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 @@ -111,9 +111,11 @@ def __init__(self, edge_storage, population_name):

@cached_property
def _population(self):
return self._edge_storage.storage.open_population(self.name)
if self._edge_storage.circuit.is_open:
return self._edge_storage.storage.open_population(self.name)
raise BluepySnapError("I/O error. Cannot access the h5 files with closed context.")

@property
@cached_property
def size(self):
"""Population size."""
return self._population.size
Expand Down Expand Up @@ -396,7 +398,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
6 changes: 4 additions & 2 deletions 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 @@ -174,7 +174,9 @@ def _data(self):

@cached_property
def _population(self):
return self._node_storage.storage.open_population(self.name)
if self._node_storage.circuit.is_open:
return self._node_storage.storage.open_population(self.name)
raise BluepySnapError("I/O error. Cannot access the h5 files with closed context.")

@cached_property
def size(self):
Expand Down
47 changes: 41 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,37 @@ def test_duplicate_population():
with pytest.raises(BluepySnapError):
circuit.nodes


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

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

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

def test_close_contexts_errors_extracted_obj():
with test_module.Circuit(str(TEST_DATA_DIR / 'circuit_config.json')) as circuit:
edges = circuit.edges['default']
edges.size
nodes = circuit.nodes['default']
nodes.size

with pytest.raises(BluepySnapError):
circuit.nodes

with pytest.raises(BluepySnapError):
circuit.edges

with pytest.raises(BluepySnapError):
nodes.property_names

with pytest.raises(BluepySnapError):
edges.property_names
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