Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pyneuroml/utils/CellBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ChannelDensityNonUniformNernst,
ChannelDensityNonUniformGHK,
)
from . import component_factory
from neuroml.utils import component_factory

neuro_lex_ids = {
"axon": "GO:0030424",
Expand Down
92 changes: 0 additions & 92 deletions pyneuroml/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,105 +7,13 @@
Author: Ankur Sinha <sanjay DOT ankur AT gmail DOT com>
"""

from typing import Any, Union
import neuroml
import logging


logger = logging.getLogger(__name__)


def component_factory(component_type: Union[str, type], validate: bool = True, **kwargs: Any) -> Any:
"""Factory function to create a NeuroML Component object.

Users can provide the name of the component as a string or the class
variable, along with its named constructor arguments, and this function
will create a new object of the Component and return it.

Users can use the `add()` helper function to further modify components

This factory runs two checks while creating the component object:

- that all arguments given do belong to the ComponentType (useful for
caching typos)
- that the created component is valid NeuroML

It is therefore less error prone than creating Components directly using
the ComponentType constructors.

It may be useful to disable validation when starting a model. The `validate`
parameter can be set to False for this.

:param component_type: component type to create component from:
this can either be the name of the component as a string, e.g.
"NeuroMLDocument", or it can be the class type itself: NeuroMLDocument.
Note that when providing the class type, one will need to import it,
e.g.: `import NeuroMLDocument`, to ensure that it is defined, whereas
this will not be required when using the string.
:type component_type: str/type
:param validate: toggle validation (default: True)
:type validate: bool
:param **kwargs: named arguments to be passed to ComponentType constructor
:type **kwargs: named arguments
:returns: new Component (object) of provided ComponentType
:rtype: object
:raises ValueError: if validation/checks fail

"""
if isinstance(component_type, str):
comp_type_class = getattr(neuroml.nml.nml, component_type)
else:
comp_type_class = getattr(neuroml.nml.nml, component_type.__name__)

comp = comp_type_class(**kwargs)
check_component_type_arg_list(comp, **kwargs)
if validate:
comp.validate()
return comp


def check_component_type_arg_list(comp: Any, **kwargs: Any) -> None:
"""Check that the correct arguments have been passed for creation of a
particular Component comp.

This is required because generally, in Python, if one passes a keyword
argument that is not listed in a Class constructor, Python will error.
However, in libNeuroML/nml.py, all constructors have a last keyword
argument `**kwargs` which means extra keyword arguments that do not match
the members are silently accepted and then ignored---because they are not
used in the constructor.

This means that common mistakes like typos will not be caught by Python,
and in larger models, one will have to inspect the model in great detail to
realise that a mistake has been made while creating a component from a
NeuroML ComponentType.

This function makes this check manually.

:param comp: component to check
:type comp: Any
:param **kwargs: arg list passed for creation of component
:type **kwargs: Any
:returns: None
:rtype: None
:raises ValueError: if given argument list does not match permitted member

"""
members = comp.get_members()
member_names = []
for m in members:
member_names.append(m.get_name())

args = list(kwargs.keys())

for arg in args:
if arg not in member_names:
err = f"'{arg}' is not a permitted argument for ComponentType '{comp.__class__.__name__}'\n"
logger.error(err)
comp.info()
raise ValueError(err)


def extract_position_info(nml_model, verbose):

cell_id_vs_cell = {}
Expand Down
65 changes: 0 additions & 65 deletions tests/utils/test_utils.py

This file was deleted.