From af097ee8b80524beb2b41a56501d87a7fdaa15d8 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Tue, 9 Apr 2024 14:26:31 +0100 Subject: [PATCH] wip: KC --- NeuroML2/postprocess_cells.py | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 NeuroML2/postprocess_cells.py diff --git a/NeuroML2/postprocess_cells.py b/NeuroML2/postprocess_cells.py new file mode 100644 index 0000000..b7cad54 --- /dev/null +++ b/NeuroML2/postprocess_cells.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +""" +Post process and add biophysics to cells. + +We make any updates to the morphology, and add biophysics. + +File: NeuroML2/postprocess_cells.py + +Copyright 2022 Ankur Sinha +Author: Ankur Sinha +""" + + +import random + +import neuroml +from neuroml.loaders import read_neuroml2_file +from pyneuroml.pynml import write_neuroml2_file +from neuroml.utils import component_factory + +random.seed(1412) + + +def load_and_setup_cell(cellname: str): + """Load a cell, and clean it to prepare it for further modifications. + + These operations are common for all cells. + + :param cellname: name of cell. + the file containing the cell should then be .morph.cell.nml + :returns: document with cell + :rtype: neuroml.NeuroMLDocument + + """ + celldoc = read_neuroml2_file( + f"{cellname}.morph.cell.nml" + ) # type: neuroml.NeuroMLDocument + cell = celldoc.cells[0] # type: neuroml.Cell + celldoc.networks = [] + cell.id = cellname + cell.notes = cell.notes.replace("GGN_20170309_sc_0_0", cellname) + cell.notes += ". Reference: Subhasis Ray, Zane N Aldworth, Mark A Stopfer (2020) Feedback inhibition and its control in an insect olfactory circuit eLife 9:e53281." + + [ + default_all_group, + default_soma_group, + default_dendrite_group, + default_axon_group, + ] = cell.setup_default_segment_groups( + use_convention=True, + default_groups=["all", "soma_group", "dendrite_group", "axon_group"], + ) + + # populate default groups + for sg in cell.morphology.segment_groups: + if "soma" in sg.id and sg.id != "soma_group": + default_soma_group.add(neuroml.Include(segment_groups=sg.id)) + if "axon" in sg.id and sg.id != "axon_group": + default_axon_group.add(neuroml.Include(segment_groups=sg.id)) + if "dend" in sg.id and sg.id != "dendrite_group": + default_dendrite_group.add(neuroml.Include(segment_groups=sg.id)) + + cell.optimise_segment_groups() + + return celldoc + + +def postprocess_GGM(): + """Post process GGM and add biophysics.""" + cellname = "GGM" + celldoc = load_and_setup_cell(cellname) + cell = celldoc.cells[0] # type: neuroml.Cell + + # biophysics + # all + cell.add_channel_density( + nml_cell_doc=celldoc, + cd_id="pas", + ion_channel="pas", + cond_density="0.00003 S_per_cm2", + erev="-51 mV", + group_id="all", + ion="non_specific", + ion_chan_def_file="channels/pas.channel.nml", + ) + cell.set_resistivity("0.1 kohm_cm", group_id="all") + cell.set_specific_capacitance("1 uF_per_cm2", group_id="all") + cell.set_init_memb_potential("-80mV") + + # L1 validation + # cell.validate(recursive=True) + cell.summary(morph=False, biophys=True) + # use pynml writer to also run L2 validation + write_neuroml2_file(celldoc, f"{cellname}.cell.nml") + + +def postprocess_KC(): + """Manually create KC and add biophysics.""" + celldoc = component_factory("NeuroMLDocument", id="KC_doc") # type: neuroml.NeuroMLDocument + cell = celldoc.add("Cell", id="KC", validate=False) # type: neuroml.Cell + cell.setup_nml_cell() + cell.add_segment([0, 0, 0, 20], [0, 0, 6.366, 20], seg_type="soma") + + # biophysics + # all + cell.add_channel_density( + nml_cell_doc=celldoc, + cd_id="pas", + ion_channel="pas", + cond_density=".0000975 S_per_cm2", + erev="-70 mV", + group_id="all", + ion="non_specific", + ion_chan_def_file="channels/pas.channel.nml", + ) + cell.set_resistivity("35.4 ohm_cm", group_id="all") + cell.set_specific_capacitance("1 uF_per_cm2", group_id="all") + cell.set_init_memb_potential("-80mV") + + # L1 validation + # cell.validate(recursive=True) + cell.summary(morph=False, biophys=True) + # use pynml writer to also run L2 validation + write_neuroml2_file(celldoc, "KC.cell.nml") + + +if __name__ == "__main__": + postprocess_GGM()