Skip to content

Commit

Permalink
fix(fix_external_morphs/biophys): add checks for missing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayankur31 committed Aug 21, 2024
1 parent 49ee2c8 commit 704483a
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions neuroml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ def get_relative_component_path(


def fix_external_morphs_biophys_in_cell(
nml2_doc: NeuroMLDocument, overwrite: bool = True
nml2_doc: NeuroMLDocument,
overwrite: bool = True,
load_morphology: bool = True,
load_biophysical_properties: bool = True,
) -> NeuroMLDocument:
"""Handle externally referenced morphologies and biophysics in cells.
Expand All @@ -338,6 +341,10 @@ def fix_external_morphs_biophys_in_cell(
:param overwrite: toggle whether the document is overwritten or a deep copy
created
:type overwrite: bool
:param load_morphology: whether morphologies should be loaded
:type load_morphology: bool
:param load_biophysical_properties: whether biophysical_properties should be loaded
:type load_biophysical_properties: bool
:returns: neuroml document
:raises KeyError: if referenced morphologies/biophysics cannot be found
"""
Expand All @@ -349,15 +356,18 @@ def fix_external_morphs_biophys_in_cell(
# get a list of morph/biophys ids being referred to by cells
referenced_ids = []
for cell in newdoc.cells:
if cell.morphology_attr is not None:
if load_morphology is True and cell.morphology_attr is not None:
if cell.morphology is None:
referenced_ids.append(cell.morphology_attr)
else:
logger.warning(
f"Cell ({cell}) already contains a Morphology, ignoring reference."
)
logger.warning("Please check/correct your cell description")
if cell.biophysical_properties_attr is not None:
if (
load_biophysical_properties is True
and cell.biophysical_properties_attr is not None
):
if cell.biophysical_properties is None:
referenced_ids.append(cell.biophysical_properties_attr)
else:
Expand All @@ -369,27 +379,39 @@ def fix_external_morphs_biophys_in_cell(
# load referenced ids from included files and store them in dicts
ext_morphs = {}
ext_biophys = {}
for inc in newdoc.includes:
incdoc = loaders.read_neuroml2_file(inc.href, verbose=False, optimized=True)
for morph in incdoc.morphology:
# includes/morphology/biophysical_properties should generally be empty
# lists and not None, but there may be cases where these have been removed
# after the document was loaded
if newdoc.includes:
for inc in newdoc.includes:
incdoc = loaders.read_neuroml2_file(inc.href, verbose=False, optimized=True)
if load_morphology is True and incdoc.morphology:
for morph in incdoc.morphology:
if morph.id in referenced_ids:
ext_morphs[morph.id] = morph
if load_biophysical_properties is True and incdoc.biophysical_properties:
for biophys in incdoc.biophysical_properties:
if biophys.id in referenced_ids:
ext_biophys[biophys.id] = biophys

if load_morphology is True and newdoc.morphology:
for morph in newdoc.morphology:
if morph.id in referenced_ids:
ext_morphs[morph.id] = morph
for biophys in incdoc.biophysical_properties:

if load_biophysical_properties is True and newdoc.biophysical_properties:
for biophys in newdoc.biophysical_properties:
if biophys.id in referenced_ids:
ext_biophys[biophys.id] = biophys

# also include morphs/biophys that are in the same document
for morph in newdoc.morphology:
if morph.id in referenced_ids:
ext_morphs[morph.id] = morph
for biophys in newdoc.biophysical_properties:
if biophys.id in referenced_ids:
ext_biophys[biophys.id] = biophys

# update cells by placing the morphology/biophys in them:
# if referenced ids are not found, throw errors
for cell in newdoc.cells:
if cell.morphology_attr is not None and cell.morphology is None:
if (
load_morphology is True
and cell.morphology_attr is not None
and cell.morphology is None
):
try:
# TODO: do we need a deepcopy here?
cell.morphology = copy.deepcopy(ext_morphs[cell.morphology_attr])
Expand All @@ -401,7 +423,8 @@ def fix_external_morphs_biophys_in_cell(
raise e

if (
cell.biophysical_properties_attr is not None
load_biophysical_properties is True
and cell.biophysical_properties_attr is not None
and cell.biophysical_properties is None
):
try:
Expand Down

0 comments on commit 704483a

Please sign in to comment.