def __main__(): import customsim import modeldata MCs = 1 GCsPerMC = 1 networkTemplate = FileTemplate("../NeuroML2/Networks/NetworkTemplate.xml") includeTemplate = FileTemplate("../NeuroML2/Networks/IncludeTemplate.xml") populationTemplate = FileTemplate("../NeuroML2/Networks/PopulationTemplate.xml") projectionTemplate = FileTemplate("../NeuroML2/Networks/ProjectionTemplate.xml") customsim.setup(MCs, GCsPerMC) model = modeldata.getmodel() netFile = "../NeuroML2/Networks/Bulb_%iMC_%iGC.net.nml" % (len(model.mitral_gids), len(model.granule_gids)) includes = "" populations = "" projections = "" mcNMLs = {} gcNMLs = {} #import pydevd #pydevd.settrace('10.211.55.3', port=4200, stdoutToServer=True, stderrToServer=True) # Make MC includes and populations for mcgid in model.mitral_gids: includes += includeTemplate.text\ .replace("[CellType]", "Mitral")\ .replace("[GID]", `mcgid`) populations += populationTemplate.text\ .replace("[CellType]", "Mitral")\ .replace("[GID]", `mcgid`)\ .replace("[X]", `model.mitrals[mcgid].x`)\ .replace("[Y]", `model.mitrals[mcgid].y`)\ .replace("[Z]", `model.mitrals[mcgid].z`) # Retain mitral cell NML mcNML = pynml\ .read_neuroml2_file("../NeuroML2/MitralCells/Exported/Mitral_0_%i.cell.nml" % mcgid)\ .cells[0] mcNMLs.update({mcgid:mcNML}) # Make GC includes and populations import granules from neuroml.nml.nml import NeuroMLDocument for gcgid in model.granule_gids: includes += includeTemplate.text\ .replace("[CellType]", "Granule")\ .replace("[GID]", `gcgid`) populations += populationTemplate.text\ .replace("[CellType]", "Granule")\ .replace("[GID]", `gcgid`)\ .replace("[X]", `granules.gid2pos[gcgid][0]`)\ .replace("[Y]", `granules.gid2pos[gcgid][1]`)\ .replace("[Z]", `granules.gid2pos[gcgid][2]`) # Retain granule cell NML gcNML = pynml\ .read_neuroml2_file("../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml" % gcgid)\ gcNMLs.update({gcgid:gcNML}) # Add a projection for each synapse synCount = len(model.mgrss.keys()) curSyn = 0 for sgid in model.mgrss.keys(): print('Building synapse %i of %i' % (curSyn+1,synCount)) synapse = model.mgrss[sgid] nsecden = model.mitrals[synapse.mgid].secden[synapse.isec].nseg secdenIndex = min(nsecden-1, int(synapse.xm * nsecden)) postSegmentId = [seg.id\ for seg in mcNMLs[synapse.mgid].morphology.segments\ if seg.name == "Seg%i_secden_%i"%(secdenIndex,synapse.isec)\ ][0] gcNML = gcNMLs[synapse.ggid].cells[0] # Position the spine along the GC priden import exportHelper exportHelper.splitSegmentAlongFraction(gcNML,"Seg0_priden2_0","priden2_0",synapse.xg,'Seg0_neck') pynml.write_neuroml2_file(gcNMLs[synapse.ggid], "../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml" % synapse.ggid) # Add Dendro-dendritic synapses # GC -> MC part projections += projectionTemplate.text\ .replace("[ProjectionID]", `sgid`+'_G2M')\ .replace("[PreCellType]", "Granule")\ .replace("[PreGID]", `synapse.ggid`)\ .replace("[PreSegment]", `4`)\ .replace("[PreAlong]", `0.5`)\ .replace("[PostCellType]", "Mitral")\ .replace("[PostGID]", `synapse.mgid`)\ .replace("[PostSegment]", `postSegmentId`)\ .replace("[PostAlong]", "0.5")\ .replace("[Synapse]", "FIsyn")\ # MC -> GC part projections += projectionTemplate.text\ .replace("[ProjectionID]", `sgid`+'_M2G')\ .replace("[PreCellType]", "Mitral")\ .replace("[PreGID]", `synapse.mgid`)\ .replace("[PreSegment]", `postSegmentId`)\ .replace("[PreAlong]", `0.5`)\ .replace("[PostCellType]", "Granule")\ .replace("[PostGID]", `synapse.ggid`)\ .replace("[PostSegment]", `4`)\ .replace("[PostAlong]", "0.5")\ .replace("[Synapse]", "AmpaNmdaSyn")\ curSyn += 1 network = networkTemplate.text\ .replace("[IncludesPlaceholder]", includes)\ .replace("[PopulationsPlaceholder]", populations)\ .replace("[ProjectionsPlaceholder]", projections) with open(netFile, "w") as file: file.write(network) print('Net file saved to: ' + netFile)
def exportToNML(cells): nml_net_file = "../NeuroML2/GranuleCells/Exported/GCnet%iG.net.nml" % len(cells) export_to_neuroml2(None, nml_net_file, includeBiophysicalProperties=False, separateCellFiles=True) # Rename files so their cell GIDs are preserved for gcid in cells.keys(): fileId = cells[gcid]['index'] oldFile = '../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml' % fileId newFile = '../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml_TEMP' % gcid # Using TEMP files to avoid naming conflicts os.rename(oldFile, newFile) # Remove temp files after all have been renamed for gcid in cells.keys(): oldFile = '../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml_TEMP' % gcid newFile = '../NeuroML2/GranuleCells/Exported/Granule_0_%i.cell.nml' % gcid os.rename(oldFile, newFile) for gcid in cells.keys(): cell, nml_doc, nml_cell_file = readGCnml(gcid) print("Loaded GC cell %i with %i segments"%(gcid, len(cell.morphology.segments))) # Change cell ID to preserve GCID cell.id = "Granule_0_%i" % gcid # Change segment ids to start at 0 and increment exportHelper.resetRoot(cell) # Replace ModelViewParmSubset_N groups with all, axon, soma, dendrite groups buildStandardSegmentGroups(cell) # Add channel placeholders nml_doc.includes.append(neuroml.IncludeType(href="channelIncludesPLACEHOLDER")) cell.biophysical_properties = neuroml.BiophysicalProperties(id="biophysPLACEHOLDER") cell.morphology.segments.append(neuroml.Segment(id="spinePLACEHOLDER")) # Save the new NML pynml.write_neuroml2_file(nml_doc, nml_cell_file) # Replace placeholders with contents from GranuleCell...xml files replaceChannelPlaceholders(nml_cell_file) cell, nml_doc, nml_cell_file = readGCnml(gcid) # Fix the fractionAlong parent segment bug ( https://github.com/NeuroML/org.neuroml.export/issues/46 ) exportHelper.splitSegmentAlongFraction(cell, "Seg0_priden", "priden", 0.8, "Seg0_priden2_0") # Orient cell along the versor versor = granules.granule_position_orientation(gcid)[1] for seg in cell.morphology.segments: segLength = seg.length if seg.parent is not None: parentDistal = [parent for parent in cell.morphology.segments if parent.id == seg.parent.segments][0].distal seg.proximal.x = parentDistal.x seg.proximal.y = parentDistal.y seg.proximal.z = parentDistal.z seg.distal = setAlongVersor(seg.distal, versor, seg.proximal, segLength) # Make sure spine is in the all group [group for group in cell.morphology.segment_groups if group.id == 'all'][0]\ .includes\ .append(neuroml.Include(segment_groups='spine_group'))\ # and Dendrite group [group for group in cell.morphology.segment_groups if group.id == 'dendrite_group'][0]\ .includes\ .append(neuroml.Include(segment_groups='spine_group')) # Save orientation pynml.write_neuroml2_file(nml_doc, nml_cell_file) print(nml_cell_file)