Skip to content

Commit

Permalink
Fix tests with libsonata 0.1.16 and warnings with pytest (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaFicarelli committed Nov 2, 2022
1 parent 02da3ed commit 1cb8c37
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 79 deletions.
5 changes: 0 additions & 5 deletions bluepysnap/edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,6 @@ def h5_filepath(self):
for edge_conf in self._circuit.config["networks"]["edges"]:
if self.name in edge_conf["populations"]:
return edge_conf["edges_file"]
for edge_conf in self._circuit.config["networks"]["edges"]:
h5_filepath = edge_conf["edges_file"]
storage = libsonata.EdgeStorage(h5_filepath)
if self.name in storage.population_names: # pylint: disable=unsupported-membership-test
return h5_filepath
raise BluepySnapError(f"h5_filepath not found for population '{self.name}'")


Expand Down
6 changes: 0 additions & 6 deletions bluepysnap/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from collections.abc import Mapping, Sequence
from copy import deepcopy

import libsonata
import numpy as np
import pandas as pd
from cached_property import cached_property
Expand Down Expand Up @@ -575,11 +574,6 @@ def h5_filepath(self):
for node_conf in self._circuit.config["networks"]["nodes"]:
if self.name in node_conf["populations"]:
return node_conf["nodes_file"]
for node_conf in self._circuit.config["networks"]["nodes"]:
h5_filepath = node_conf["nodes_file"]
storage = libsonata.NodeStorage(h5_filepath)
if self.name in storage.population_names: # pylint: disable=unsupported-membership-test
return h5_filepath
raise BluepySnapError(f"h5_filepath not found for population '{self.name}'")


Expand Down
6 changes: 6 additions & 0 deletions tests/data/circuit_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"populations": {
"default": {
"type": "biophysical"
},
"default2": {
"type": "biophysical"
}
}
}
Expand All @@ -26,6 +29,9 @@
"populations": {
"default":{
"type": "chemical"
},
"default2":{
"type": "chemical"
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from bluepysnap.edges import EdgePopulation, Edges
from bluepysnap.nodes import NodePopulation, Nodes

from utils import TEST_DATA_DIR, copy_test_data, edit_config
from utils import TEST_DATA_DIR, copy_test_data, edit_config, skip_if_libsonata_0_1_16


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"),
"populations": {"default": {"type": "biophysical"}},
"populations": {"default": {"type": "biophysical"}, "default2": {"type": "biophysical"}},
}
assert isinstance(circuit.nodes, Nodes)
assert isinstance(circuit.edges, Edges)
Expand All @@ -29,21 +29,25 @@ def test_all():
)


@skip_if_libsonata_0_1_16
def test_duplicate_node_populations():
with copy_test_data() as (_, config_path):
with edit_config(config_path) as config:
config["networks"]["nodes"].append(config["networks"]["nodes"][0])

with pytest.raises(SonataError, match="Duplicate population"):
match = "Duplicate population|Population default is declared twice"
with pytest.raises(SonataError, match=match):
test_module.Circuit(config_path)


@skip_if_libsonata_0_1_16
def test_duplicate_edge_populations():
with copy_test_data() as (_, config_path):
with edit_config(config_path) as config:
config["networks"]["edges"].append(config["networks"]["edges"][0])

with pytest.raises(SonataError, match="Duplicate population"):
match = "Duplicate population|Population default is declared twice"
with pytest.raises(SonataError, match=match):
test_module.Circuit(config_path)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_circuit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _multi_index():


class TestCircuitNodeId:
def setup(self):
def setup_method(self):
self.test_obj = test_module.CircuitNodeId("pop", 1)

def test_init(self):
Expand All @@ -32,7 +32,7 @@ def test_accessors(self):


class TestCircuitEdgeId:
def setup(self):
def setup_method(self):
self.test_obj = test_module.CircuitEdgeId("pop", 1)

def test_init(self):
Expand Down Expand Up @@ -62,7 +62,7 @@ def _circuit_ids(self, populations, ids):
index.names = ["population", self.id_name]
return index

def setup(self):
def setup_method(self):
self.test_obj_unsorted = self.ids_cls(_multi_index(), sort_index=False)
self.test_obj_sorted = self.ids_cls(_multi_index())

Expand Down
23 changes: 22 additions & 1 deletion tests/test_circuit_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def test_missing_data_config_no_population_for_edge(to_remove):
assert errors == {
Error(Error.FATAL, 'No node population for "/edges/default/source_node_id"'),
Error(Error.FATAL, 'No node population for "/edges/default/target_node_id"'),
Error(Error.FATAL, 'No node population for "/edges/default2/source_node_id"'),
Error(Error.FATAL, 'No node population for "/edges/default2/target_node_id"'),
}


Expand Down Expand Up @@ -290,6 +292,16 @@ def test_no_bio_component_dirs():
"must to be defined for 'biophysical' population 'default'"
),
),
Error(
Error.FATAL, "'biophysical_neuron_models_dir' not defined for population 'default2'"
),
Error(
Error.FATAL,
(
"at least one of 'morphologies_dir' or 'alternate_morphologies' "
"must to be defined for 'biophysical' population 'default2'"
),
),
}


Expand Down Expand Up @@ -483,6 +495,14 @@ def test_no_edge_all_node_ids():
Error.FATAL,
"/edges/default/target_node_id does not have node ids in its node population",
),
Error(
Error.FATAL,
"/edges/default2/source_node_id does not have node ids in its node population",
),
Error(
Error.FATAL,
"/edges/default2/target_node_id does not have node ids in its node population",
),
}


Expand Down Expand Up @@ -530,5 +550,6 @@ def test_no_duplicate_population_names():
config["networks"]["nodes"].append(config["networks"]["nodes"][0])
errors = validate(str(config_copy_path))
assert errors == {
Error(Error.FATAL, 'Already have population "default" in config for type "nodes"')
Error(Error.FATAL, 'Already have population "default" in config for type "nodes"'),
Error(Error.FATAL, 'Already have population "default2" in config for type "nodes"'),
}
20 changes: 3 additions & 17 deletions tests/test_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_estimate_range_size_4():


class TestEdges:
def setup(self):
def setup_method(self):
circuit = Circuit(str(TEST_DATA_DIR / "circuit_config.json"))
self.test_obj = test_module.Edges(circuit)

Expand Down Expand Up @@ -809,7 +809,7 @@ def get_edge_population(circuit_path, pop_name):
circuit = Circuit(circuit_path)
return test_module.EdgePopulation(circuit, pop_name)

def setup(self):
def setup_method(self):
self.test_obj = TestEdgePopulation.get_edge_population(
str(TEST_DATA_DIR / "circuit_config.json"), "default"
)
Expand Down Expand Up @@ -1365,7 +1365,7 @@ def test_iter_connection_unique(self):
{
"edge_types_file": None,
"edges_file": str(Path(config_dir) / "edges_complete_graph.h5"),
"populations": {},
"populations": {"default": {"type": "chemical"}},
}
]

Expand Down Expand Up @@ -1395,20 +1395,6 @@ def test_iter_connection_unique(self):
def test_h5_filepath_from_config(self):
assert self.test_obj.h5_filepath == str(TEST_DATA_DIR / "edges.h5")

def test_h5_filepath_from_libsonata(self):
with copy_test_data() as (config_dir, config_path):
edge_path = str(Path(config_dir) / "edges.h5")
with edit_config(config_path) as config:
config["networks"]["edges"] = [
{
"edge_types_file": None,
"edges_file": edge_path,
"populations": {"fake": {}},
}
]
test_obj = test_module.Edges(Circuit(config_path))
assert test_obj["default"].h5_filepath == edge_path

def test_no_h5_filepath(self):
with pytest.raises(BluepySnapError, match="h5_filepath not found for population"):
self.test_obj.name = "fake"
Expand Down
12 changes: 6 additions & 6 deletions tests/test_frame_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class TestFrameReport:
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.FrameReport(self.simulation, "soma_report")
self.test_obj_info = test_module.FrameReport(self.simulation, "section_report")
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_iter(self):


class TestCompartmentReport:
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.CompartmentReport(self.simulation, "section_report")

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_filter(self):


class TestSomaReport:
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.SomaReport(self.simulation, "soma_report")

Expand Down Expand Up @@ -184,7 +184,7 @@ def test_filter(self):


class TestPopulationFrameReport:
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.FrameReport(self.simulation, "section_report")["default"]

Expand All @@ -197,7 +197,7 @@ def test__resolve(self):


class TestPopulationCompartmentReport:
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.CompartmentReport(self.simulation, "section_report")["default"]
timestamps = np.linspace(0, 0.9, 10)
Expand Down Expand Up @@ -292,7 +292,7 @@ def test_node_ids(self):


class TestPopulationSomaReport(TestPopulationCompartmentReport):
def setup(self):
def setup_method(self):
self.simulation = Simulation(str(TEST_DATA_DIR / "simulation_config.json"))
self.test_obj = test_module.SomaReport(self.simulation, "soma_report")["default"]
timestamps = np.linspace(0, 0.9, 10)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class TestMorphHelper:
def setup(self):
def setup_method(self):
self.nodes = create_node_population(str(TEST_DATA_DIR / "nodes_quaternions.h5"), "default")
self.morph_path = TEST_DATA_DIR / "morphologies"
self.test_obj = test_module.MorphHelper(str(self.morph_path), self.nodes)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_node_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class TestNodeSets:
def setup(self):
def setup_method(self):
self.test_obj = test_module.NodeSets(str(TEST_DATA_DIR / "node_sets_file.json"))

def test_init(self):
Expand Down
18 changes: 2 additions & 16 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class TestNodes:
def setup(self):
def setup_method(self):
circuit = Circuit(str(TEST_DATA_DIR / "circuit_config.json"))
self.test_obj = test_module.Nodes(circuit)

Expand Down Expand Up @@ -420,7 +420,7 @@ def test_get(self):


class TestNodePopulation:
def setup(self):
def setup_method(self):
self.test_obj = Circuit(str(TEST_DATA_DIR / "circuit_config.json")).nodes["default"]

def test_basic(self):
Expand Down Expand Up @@ -997,20 +997,6 @@ def test_models(self):
def test_h5_filepath_from_config(self):
assert self.test_obj.h5_filepath == str(TEST_DATA_DIR / "nodes.h5")

def test_h5_filepath_from_libsonata(self):
with copy_test_data() as (config_dir, config_path):
node_path = str(Path(config_dir) / "nodes.h5")
with edit_config(config_path) as config:
config["networks"]["nodes"] = [
{
"node_types_file": None,
"nodes_file": node_path,
"populations": {"fake": {}},
}
]
test_obj = test_module.Nodes(Circuit(config_path))
assert test_obj["default"].h5_filepath == node_path

def test_no_h5_filepath(self):
with pytest.raises(BluepySnapError, match="h5_filepath not found for population"):
self.test_obj.name = "fake"
Expand Down
37 changes: 22 additions & 15 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,45 @@ def test_validate_config_ok_missing_optional_fields(to_remove):


@pytest.mark.parametrize(
("to_remove", "expected"),
("to_remove_list", "expected"),
(
(
["networks", "nodes", 0, "populations", "default"],
[
["networks", "nodes", 0, "populations", "default"],
["networks", "nodes", 0, "populations", "default2"],
],
"networks.nodes[0].populations: too few properties",
),
(
["networks", "edges", 0, "populations", "default"],
[
["networks", "edges", 0, "populations", "default"],
["networks", "edges", 0, "populations", "default2"],
],
"networks.edges[0].populations: too few properties",
),
(
["networks", "nodes", 0, "populations"],
[["networks", "nodes", 0, "populations"]],
"networks.nodes[0]: 'populations' is a required property",
),
(
["networks", "edges", 0, "populations"],
[["networks", "edges", 0, "populations"]],
"networks.edges[0]: 'populations' is a required property",
),
(["networks", "nodes", 0], "networks.nodes: [] is too short"),
(["networks", "edges", 0], "networks.edges: [] is too short"),
(["networks", "nodes"], "networks: 'nodes' is a required property"),
(["networks", "edges"], "networks: 'edges' is a required property"),
(["networks"], "'networks' is a required property"),
([["networks", "nodes", 0]], "networks.nodes: [] is too short"),
([["networks", "edges", 0]], "networks.edges: [] is too short"),
([["networks", "nodes"]], "networks: 'nodes' is a required property"),
([["networks", "edges"]], "networks: 'edges' is a required property"),
([["networks"]], "'networks' is a required property"),
),
)
def test_validate_config_error(to_remove, expected):
def test_validate_config_error(to_remove_list, expected):
with copy_test_data() as (_, config_copy_path):
with edit_config(config_copy_path) as config:
c = config
for key in to_remove[:-1]:
c = c[key]
del c[to_remove[-1]]
for to_remove in to_remove_list:
c = config
for key in to_remove[:-1]:
c = c[key]
del c[to_remove[-1]]
errors = test_module.validate_circuit_schema(str(config_copy_path), config)

assert len(errors) == 1
Expand Down

0 comments on commit 1cb8c37

Please sign in to comment.