Skip to content

[WIP] utility function list params in ComponentType #439

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

Open
wants to merge 3 commits into
base: development
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
26 changes: 26 additions & 0 deletions pyneuroml/utils/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,29 @@ def cell_info(cell: Cell) -> str:
)

return info


def list_params(nml2_doc: NeuroMLDocument) -> str:
"""List all parameters in ComponentType definitions.

:param nml2_doc: NeuroMLDocument object
:type nml2_doc: NeuroMLDocument
:returns: Formatted string listing parameters
"""
info = f"Parameters in NeuroML 2 document: {nml2_doc.id}\n"

for component in nml2_doc.ComponentType:
info += f"\nComponentType: {component.name}\n"

members = inspect.getmembers(component)
params = [m[1] for m in members if m[0] == "parameters"]

if params:
for param in params[0]:
param_name = param.name if hasattr(param, "name") else "UnnamedParam"
param_value = param.value if hasattr(param, "value") else "NoValue"
info += f" - {param_name}: {param_value}\n"
else:
info += " No parameters found.\n"

return info
48 changes: 48 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import math
import os
import pathlib as pl
from unittest.mock import MagicMock

import neuroml

Expand All @@ -22,6 +23,7 @@
translate_cell_to_coords,
)
from pyneuroml.utils.components import add_new_component
from pyneuroml.utils.info import list_params

from .. import BaseTestCase

Expand Down Expand Up @@ -293,3 +295,49 @@ def test_adding_new_components(self):
self.assertIsNotNone(new_comp)
self.assertIsFile(new_comp_file)
os.unlink(new_comp_file)

def setUp(self):
"""Set up a mock NeuroMLDocument with dummy ComponentType instances."""
self.nml_doc = neuroml.NeuroMLDocument(id="test_doc")

# Mock ComponentType objects with 'name' and 'parameters' attributes
self.mock_component_1 = MagicMock()
self.mock_component_1.name = "ComponentA"
self.mock_component_1.parameters = [
MagicMock(name="param1", value="10"),
MagicMock(name="param2", value="20"),
]

self.mock_component_2 = MagicMock()
self.mock_component_2.name = "ComponentB"
self.mock_component_2.parameters = [
MagicMock(name="paramX", value="100"),
]

# Component with no parameters
self.mock_component_3 = MagicMock()
self.mock_component_3.name = "ComponentC"
self.mock_component_3.parameters = []

# Add mocked components to NeuroMLDocument
self.nml_doc.ComponentType = [
self.mock_component_1,
self.mock_component_2,
self.mock_component_3,
]

def test_list_params_output(self):
"""Test if list_params correctly extracts and formats parameters."""
expected_output = (
"Parameters in NeuroML 2 document: test_doc\n"
"\nComponentType: ComponentA\n"
" - param1: 10\n"
" - param2: 20\n"
"\nComponentType: ComponentB\n"
" - paramX: 100\n"
"\nComponentType: ComponentC\n"
" No parameters found.\n"
)

output = list_params(self.nml_doc)
self.assertEqual(output.strip(), expected_output.strip())
Loading