Skip to content
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

Move gates of undetermined types to standard ones where possible #206

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions neuroml/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __nml2_doc(cls, file_name: str) -> neuroml.NeuroMLDocument:
except Exception as e:
raise Exception("Not a valid NeuroML 2 doc (%s): %s" % (file_name, e), e)

utils.move_undetermined_gates_to_typed(nml2_doc)
return nml2_doc


Expand Down
95 changes: 94 additions & 1 deletion neuroml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@
import networkx

import neuroml.nml.nml as schema
from neuroml import BiophysicalProperties, Morphology, NeuroMLDocument
from neuroml import (
BiophysicalProperties,
GateFractional,
GateHHInstantaneous,
GateHHRates,
GateHHRatesInf,
GateHHRatesTau,
GateHHRatesTauInf,
GateHHTauInf,
GateHHUndetermined,
GateKS,
Morphology,
NeuroMLDocument,
)

from . import loaders

Expand Down Expand Up @@ -472,6 +485,86 @@ def fix_external_morphs_biophys_in_cell(
return newdoc


def create_new_typed_gate(
gate: GateHHUndetermined,
) -> Optional[
Union[
GateHHRates,
GateHHRatesTau,
GateHHRatesInf,
GateHHRatesTauInf,
GateHHTauInf,
GateHHInstantaneous,
GateFractional,
GateKS,
]
]:
"""Convert an undetermined gate to a "determined" gate

:param gate: gate object of GateHHUndetermined type
:type gate: GateHHUndetermined
:returns: new gate object, or None if the gate is not of a standard type
"""
gates_name_map = {
"gateHHrates": "GateHHRates",
"gateHHratesTau": "GateHHRatesTau",
"gateHHratesInf": "GateHHRatesInf",
"gateHHratesTauInf": "GateHHRatesTauInf",
"gateHHtauInf": "GateHHTauInf",
"gateHHInstantaneous": "GateHHInstantaneous",
"gateFractional": "GateFractional",
"gateKS": "GateKS",
}
gate_type = getattr(gate, "type", None)
if gate_type:
try:
gate_class_type = gates_name_map[gate_type]
# if it isn't in our dict, it's non standard
except KeyError:
return None

new_gate = component_factory(component_type=gate_class_type, validate=False)
print(gate.__dict__)
new_gate.__dict__.update(gate.__dict__)
return new_gate

return None


def move_undetermined_gates_to_typed(nml2_doc: NeuroMLDocument):
"""Replace gates of GateHHUndetermined type with their standard
counterparts where possible.

Note that this modifies the passed NeuroMLDocument object in-place.

If `nml2_doc` is not a NeuroMLDocument, this function does nothing and
simply returns None.

:param nml2_doc: NeuroMLDocument object
:type nml2_doc: NeuroMLDocument
:returns: None

"""
if not isinstance(nml2_doc, NeuroMLDocument):
return None

all_channels = (
list(nml2_doc.ion_channel_hhs.__iter__())
+ list(nml2_doc.ion_channel.__iter__())
+ list(nml2_doc.ion_channel_v_shifts.__iter__())
)
for achannel in all_channels:
determined_gates = []
undetermined_gates = getattr(achannel, "gates", [])
for gate in undetermined_gates:
new_typed_gate = create_new_typed_gate(gate)
if new_typed_gate:
achannel.add(new_typed_gate)
determined_gates.append(gate)
for d_gate in determined_gates:
undetermined_gates.remove(d_gate)


def main():
if len(sys.argv) != 2:
print("Please specify the name of the NeuroML2 file...")
Expand Down
Loading