-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce NeuronModelsHelper, an analog of MorphHelper (#123)
It is used to access neuron models
- Loading branch information
1 parent
601aacc
commit fd42846
Showing
14 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright (c) 2019, EPFL/Blue Brain Project | ||
|
||
# This file is part of BlueBrain SNAP library <https://github.com/BlueBrain/snap> | ||
|
||
# This library is free software; you can redistribute it and/or modify it under | ||
# the terms of the GNU Lesser General Public License version 3.0 as published | ||
# by the Free Software Foundation. | ||
|
||
# This library is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
# details. | ||
|
||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this library; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
"""Neuron models access.""" | ||
|
||
from pathlib import Path | ||
|
||
from bluepysnap.circuit_ids import CircuitNodeId | ||
from bluepysnap.exceptions import BluepySnapError | ||
from bluepysnap.sonata_constants import Node | ||
|
||
|
||
class NeuronModelsHelper: | ||
"""Collection of neuron models related methods.""" | ||
|
||
def __init__(self, config_components, population): | ||
"""Constructor. | ||
Args: | ||
config_components (dict): 'components' part of circuit's config | ||
population (NodePopulation): NodePopulation object used to query the nodes. | ||
Returns: | ||
NeuronModelsHelper: A NeuronModelsHelper object. | ||
""" | ||
# all nodes from a population must have the same model type | ||
if population.get(0, Node.MODEL_TYPE) not in {"biophysical", "point_neuron"}: | ||
raise BluepySnapError( | ||
"Neuron models can be only in biophysical or point node population.") | ||
|
||
self._config_components = config_components | ||
self._population = population | ||
|
||
def get_filepath(self, node_id): | ||
"""Return path to model file corresponding to `node_id`. | ||
Args: | ||
node_id (int|CircuitNodeId): node id | ||
Returns: | ||
Path: path to the model file of neuron | ||
""" | ||
if not isinstance(node_id, (int, CircuitNodeId)): | ||
raise BluepySnapError("node_id must be a int or a CircuitNodeId") | ||
node = self._population.get(node_id, [Node.MODEL_TYPE, Node.MODEL_TEMPLATE]) | ||
if node[Node.MODEL_TYPE] == "biophysical": | ||
models_dir = self._config_components.get("biophysical_neuron_models_dir") | ||
if models_dir is None: | ||
raise BluepySnapError( | ||
"Missing 'biophysical_neuron_models_dir' in Sonata config") | ||
else: | ||
models_dir = self._config_components.get("point_neuron_models_dir") | ||
if models_dir is None: | ||
raise BluepySnapError("Missing 'point_neuron_models_dir' in Sonata config") | ||
|
||
template = node[Node.MODEL_TEMPLATE] | ||
assert ':' in template, "Format of 'model_template' must be <schema>:<resource>." | ||
schema, resource = template.split(':', 1) | ||
resource = Path(resource).with_suffix(f'.{schema}') | ||
if resource.is_absolute(): | ||
return resource | ||
return Path(models_dir, resource) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from pathlib import Path | ||
import h5py | ||
import bluepysnap.neuron_models as test_module | ||
from bluepysnap.circuit import Circuit, Config | ||
from bluepysnap.circuit_ids import CircuitNodeId | ||
from bluepysnap.sonata_constants import Node | ||
from bluepysnap.exceptions import BluepySnapError | ||
|
||
import pytest | ||
|
||
from utils import TEST_DATA_DIR, copy_circuit, copy_config, edit_config, create_node_population | ||
|
||
|
||
def test_invalid_model_type(): | ||
"""test that model type, other than 'biophysical' or 'point_neuron', throws an error""" | ||
with copy_circuit() as (circuit_copy_path, config_copy_path): | ||
config = Config(config_copy_path).resolve() | ||
nodes_file = config["networks"]["nodes"][0]["nodes_file"] | ||
with h5py.File(nodes_file, 'r+') as h5f: | ||
grp_name = 'nodes/default/0/model_type' | ||
data = h5f[grp_name][:] | ||
del h5f[grp_name] | ||
h5f.create_dataset(grp_name, | ||
data=["virtual"] * data.shape[0], | ||
dtype=h5py.string_dtype()) | ||
nodes = create_node_population(nodes_file, "default") | ||
with pytest.raises(BluepySnapError) as e: | ||
test_module.NeuronModelsHelper({}, nodes) | ||
assert 'biophysical or point node population' in e.value.args[0] | ||
|
||
|
||
def test_get_invalid_node_id(): | ||
nodes = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'), "default") | ||
config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve() | ||
test_obj = test_module.NeuronModelsHelper(config['components'], nodes) | ||
|
||
with pytest.raises(BluepySnapError) as e: | ||
test_obj.get_filepath('1') | ||
assert "node_id must be a int or a CircuitNodeId" in e.value.args[0] | ||
|
||
|
||
def test_get_filepath_biophysical(): | ||
nodes = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'), "default") | ||
config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve() | ||
test_obj = test_module.NeuronModelsHelper(config['components'], nodes) | ||
|
||
node_id = 0 | ||
assert nodes.get(node_id, properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A" | ||
actual = test_obj.get_filepath(node_id) | ||
expected = Path(config['components']['biophysical_neuron_models_dir'], 'small_bio-A.hoc') | ||
assert actual == expected | ||
|
||
node_id = CircuitNodeId('default', 0) | ||
assert nodes.get(node_id, properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A" | ||
actual = test_obj.get_filepath(node_id) | ||
assert actual == expected | ||
|
||
node_id = CircuitNodeId('default', 2) | ||
assert nodes.get(node_id, properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-C" | ||
actual = test_obj.get_filepath(node_id) | ||
expected = Path(config['components']['biophysical_neuron_models_dir'], 'small_bio-C.hoc') | ||
assert actual == expected | ||
|
||
|
||
def test_no_biophysical_dir(): | ||
nodes = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'), "default") | ||
config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve() | ||
del config['components']['biophysical_neuron_models_dir'] | ||
test_obj = test_module.NeuronModelsHelper(config['components'], nodes) | ||
|
||
with pytest.raises(BluepySnapError) as e: | ||
test_obj.get_filepath(0) | ||
assert "Missing 'biophysical_neuron_models_dir'" in e.value.args[0] | ||
|
||
|
||
def test_get_filepath_point(): | ||
with copy_config() as config_copy_path: | ||
with edit_config(config_copy_path) as config: | ||
config['components']['point_neuron_models_dir'] = "$COMPONENT_DIR/point_neuron_models" | ||
|
||
circuit = Circuit(config_copy_path) | ||
nodes = create_node_population(str(TEST_DATA_DIR / 'nodes_points.h5'), "default", circuit) | ||
test_obj = test_module.NeuronModelsHelper(circuit.config['components'], nodes) | ||
|
||
node_id = 0 | ||
assert nodes.get(node_id, properties=Node.MODEL_TEMPLATE) == "nml:empty_bio" | ||
actual = test_obj.get_filepath(node_id) | ||
expected = Path(circuit.config['components']['point_neuron_models_dir'], 'empty_bio.nml') | ||
assert actual == expected | ||
|
||
node_id = 1 | ||
assert nodes.get(node_id, properties=Node.MODEL_TEMPLATE) == "nml:/abs/path/empty_bio" | ||
actual = test_obj.get_filepath(node_id) | ||
expected = Path('/abs/path/empty_bio.nml') | ||
assert actual == expected | ||
|
||
|
||
def test_no_point_dir(): | ||
nodes = create_node_population(str(TEST_DATA_DIR / 'nodes_points.h5'), "default") | ||
config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve() | ||
test_obj = test_module.NeuronModelsHelper(config['components'], nodes) | ||
|
||
with pytest.raises(BluepySnapError) as e: | ||
test_obj.get_filepath(0) | ||
assert "Missing 'point_neuron_models_dir'" in e.value.args[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters