Skip to content

Commit

Permalink
Add helper method fix_external_morphs_biophys_in_cell; use by default…
Browse files Browse the repository at this point in the history
… on XMLParser
  • Loading branch information
pgleeson committed May 31, 2024
1 parent 31041b3 commit b6f606e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions neuroml/hdf5/NeuroMLXMLParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions neuroml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import networkx

import neuroml.nml.nml as schema
from neuroml import NeuroMLDocument

from . import loaders

Expand Down Expand Up @@ -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...")
Expand Down

0 comments on commit b6f606e

Please sign in to comment.