From b6f606ee268d371548b1e53d2d9ce3fceb0c185f Mon Sep 17 00:00:00 2001 From: pgleeson Date: Fri, 31 May 2024 10:44:21 +0100 Subject: [PATCH] Add helper method fix_external_morphs_biophys_in_cell; use by default on XMLParser --- neuroml/hdf5/NeuroMLXMLParser.py | 17 +++++++++++++++-- neuroml/utils.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/neuroml/hdf5/NeuroMLXMLParser.py b/neuroml/hdf5/NeuroMLXMLParser.py index fe1682bc..020a8991 100644 --- a/neuroml/hdf5/NeuroMLXMLParser.py +++ b/neuroml/hdf5/NeuroMLXMLParser.py @@ -27,8 +27,9 @@ class NeuroMLXMLParser: currentProjectionPostPop = "" currentSynapse = "" - def __init__(self, netHandler): + def __init__(self, netHandler, fix_external_morphs_biophys=True): self.netHandler = netHandler + self.fix_external_morphs_biophys = fix_external_morphs_biophys # For continued use with old API if not hasattr(self.netHandler, "handle_network") or hasattr( @@ -91,6 +92,10 @@ def parse(self, filename): self.nml_doc = loaders.read_neuroml2_file( filename, include_includes=True, already_included=[] ) + if self.fix_external_morphs_biophys: + from neuroml.utils import fix_external_morphs_biophys_in_cell + fix_external_morphs_biophys_in_cell(self.nml_doc) + print("Loaded: %s as NeuroMLDocument" % filename) @@ -474,7 +479,11 @@ def parse(self, filename): if __name__ == "__main__": - file_name = "../examples/tmp/testh5.nml" + file_name = "../examples/tmp/testnet.nml" + + import sys + if len(sys.argv)==2: + file_name = sys.argv[1] logging.basicConfig( level=logging.INFO, format="%(name)-19s %(levelname)-5s - %(message)s" @@ -503,6 +512,10 @@ def parse(self, filename): nml_doc = nmlHandler.get_nml_doc() print(nml_doc.summary()) + print(nml_doc.cells) + + for cell in nml_doc.cells: + print('--- Cell: %s'%cell) nml_file = "../examples/tmp/testh5_2_.nml" import neuroml.writers as writers diff --git a/neuroml/utils.py b/neuroml/utils.py index b5998f43..526a7636 100644 --- a/neuroml/utils.py +++ b/neuroml/utils.py @@ -12,6 +12,7 @@ import networkx import neuroml.nml.nml as schema +from neuroml import NeuroMLDocument from . import loaders @@ -305,6 +306,21 @@ def get_relative_component_path(src: str, dest: str, root: Type = return (path, graph) + +def fix_external_morphs_biophys_in_cell(nml2_doc: NeuroMLDocument) -> None: + """ + Only used in the case where a cell element has a morphology (or biophysicalProperties) attribute, as opposed to a + subelement morphology/biophysicalProperties. This will substitute the external element into the cell element for ease of access + """ + for cell in nml2_doc.cells: + if cell.morphology_attr != None: + ext_morph = nml2_doc.get_by_id(cell.morphology_attr) + cell.morphology = ext_morph + if cell.biophysical_properties_attr != None: + ext_bp = nml2_doc.get_by_id(cell.biophysical_properties_attr) + cell.biophysical_properties = ext_bp + + def main(): if len(sys.argv) != 2: print("Please specify the name of the NeuroML2 file...")