def export(MCs=1, GCsPerMC=10): import os import sys import re import copy import exportHelper numGranulesTotal = MCs * GCsPerMC # Nav to neuron folder where compiled MOD files are present os.chdir("../../NEURON") from neuron import h, gui # Build the network with desired number of GCs - including spines print("Building GC network...") sys.path.append(os.getcwd()) import customsim import modeldata customsim.setup(MCs, GCsPerMC) model = modeldata.getmodel() print("GC network built.") result = exportToNML(model.granules) print("Exported the following cell files:") for file in result: print(" " + file)
def export(MCs=2): print("Exporting odor input for " + ` MCs ` + " MCs...") # Build the network import os, sys sys.path.append(os.path.abspath("../../NEURON")) os.chdir("../../NEURON") from neuron import h, gui import customsim import modeldata customsim.setup(MCs, 0) model = modeldata.getmodel() # Initialize default odor input sequence from odorstim import OdorSequence import custom_params import params odseq = OdorSequence(params.odor_sequence) odstim = odseq[0] next_time = odstim.init_ev(params.odor_sequence[0][1]) # Simulate h.run() to obtain NetCon events sectionTimes = {} loop = True while (loop): h.t = next_time event = odstim.ev(next_time) for sec in event["weights"]: secName = sec["label"] if secName not in sectionTimes: sectionTimes[secName] = {'times': [], 'weights': []} sectionTimes[secName]['times'].append(event['time']) sectionTimes[secName]['weights'].append(sec['weight']) loop = 'next_time' in event if loop: next_time = event['next_time'] import json, sys, os fileName = "odor-events.json" with open(fileName, "w") as file: json.dump(sectionTimes, file, indent=4) print("Odor stim times written to " + os.path.abspath(fileName))
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)
piece exists on this cpu. For other existence questions, use mpiece_exists, mgid2pieces, and msecden. Although granules are not currently split, we have their equivalents: gpiece_exists, ggid2pieces, and gpriden. ''' from neuron import h pc = h.ParallelContext() try: from loadbalutil import lb except ImportError: pass #dictionary for mgid, mcell (whats left of it) #model.mgid2piece from modeldata import getmodel model = getmodel() def mpiece_exists(mgid): return model.mgid2piece.has_key(mgid) def mgid2pieces(mgid): ''' return cell with existing pieces for mgid; None if does not exist.''' if mpiece_exists(mgid): return model.mgid2piece[mgid] return None def msecden(mgid, i): ''' return secondary dendrite if it exists, otherwise None.''' c = mgid2pieces(mgid)
def export(MCs = 60, GCsPerMC = 5, useOdorInput = True, odorInputMaxTime = 200, export_cells = True): import subprocess if export_cells: # Export cells first - in their own NEURON instances subprocess.Popen("python -c 'import export_mitral; export_mitral.export("+`MCs`+");'", shell=True).wait() subprocess.Popen("python -c 'import export_granule; export_granule.export(" + `MCs` + ","+`GCsPerMC`+");'", shell=True).wait() # Export the odor input if useOdorInput: subprocess.Popen("python -c 'import export_input; export_input.export(" + `MCs` + ");'", shell=True).wait() import os, neuroml, sys from math import ceil os.chdir('../../NEURON') sys.path.append(os.path.abspath(os.getcwd())) from neuron import h, gui from pyneuroml import pynml from pyneuroml.neuron import export_to_neuroml2 import customsim import modeldata networkTemplate = FileTemplate("../NeuroML2/Networks/NetworkTemplate.xml") includeTemplate = FileTemplate("../NeuroML2/Networks/IncludeTemplate.xml") glomsTemplate = FileTemplate("../NeuroML2/Networks/GlomeruliTemplate.xml") populationTemplate = FileTemplate("../NeuroML2/Networks/PopulationTemplate.xml") projectionTemplate = FileTemplate("../NeuroML2/Networks/ProjectionTemplate.xml") odorInputListTemplate = FileTemplate("../NeuroML2/Networks/OdorInputListTemplate.xml") odorSynapseTemplate = FileTemplate("../NeuroML2/Networks/OdorSynapseTemplate.xml") customsim.setup(MCs, GCsPerMC) model = modeldata.getmodel() # Exp2Syns are used for odor input if useOdorInput: section2synapse = {syn.get_segment().sec.name(): syn for syn in h.Exp2Syn} netFile = "../NeuroML2/Networks/Bulb_%iMC_%iGC%s.net.nml" % (len(model.mitral_gids), len(model.granule_gids),"_OdorIn" if useOdorInput else "") includes = "" populations = "" projections = "" gloms = "" odorInputLists = "" odorSyns = "" mcNMLs = {} gcNMLs = {} # 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]", `0`) \ .replace("[Y]", `0`) \ .replace("[Z]", `0`) # TODO: restore these when this is resolved: https://github.com/NeuroML/jNeuroML/issues/55 # .replace("[X]", `model.mitrals[mcgid].x`)\ # .replace("[Y]", `model.mitrals[mcgid].y`)\ # .replace("[Z]", `model.mitrals[mcgid].z`) # Retain mitral cell NML for later mcNML = pynml\ .read_neuroml2_file("../NeuroML2/MitralCells/Exported/Mitral_0_%i.cell.nml" % mcgid)\ .cells[0] mcNMLs.update({mcgid:mcNML}) # Each glom consists of 5 mcs -- using the highest mc id, generate the required gloms num_gloms = int(ceil((max(model.mitral_gids)+1)/5.0)) with open('realgloms.txt') as f: for g in range(num_gloms): g_x, g_y, g_z = map(float, f.readline().split(' ')) gloms += glomsTemplate.text \ .replace("[GlomID]", `g`) \ .replace("[X]", `g_x`) \ .replace("[Y]", `g_y`) \ .replace("[Z]", `g_z`) # 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)\ .cells[0] 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] # Compute the segment on the MC to which the synapse will be connected alongSegment = getFractionAlongSegment(model.mitrals[synapse.mgid].secden[synapse.isec], synapse.xm) mcSegmentIndex = alongSegment["segment_index"] alongMcSegment = alongSegment["along_segment"] # Get the NML ID of the MC segment mcSegmentID = next(seg.id for seg in mcNMLs[synapse.mgid].morphology.segments if seg.name == "Seg%i_secden_%i"%(mcSegmentIndex,synapse.isec)) # Get the NML ID of the MC spine head, which all syns are attached (at 0.5 position) gcSpineHeadSegmentID = next(seg.id for seg in gcNMLs[synapse.ggid].morphology.segments if seg.name == "head_seg") # Add Dendro-dendritic synapses # GC -> MC part projections += projectionTemplate.text\ .replace("[ProjectionID]", `sgid`+'_G2M')\ .replace("[PreCellType]", "Granule")\ .replace("[PreGID]", `synapse.ggid`)\ .replace("[PreSegment]", `gcSpineHeadSegmentID`)\ .replace("[PreAlong]", `0.5`)\ .replace("[PostCellType]", "Mitral")\ .replace("[PostGID]", `synapse.mgid`)\ .replace("[PostSegment]", `mcSegmentID`)\ .replace("[PostAlong]", `alongMcSegment`)\ .replace("[Synapse]", "FIsyn")\ # MC -> GC part projections += projectionTemplate.text\ .replace("[ProjectionID]", `sgid`+'_M2G')\ .replace("[PreCellType]", "Mitral")\ .replace("[PreGID]", `synapse.mgid`)\ .replace("[PreSegment]", `mcSegmentID`)\ .replace("[PreAlong]", `alongMcSegment`)\ .replace("[PostCellType]", "Granule")\ .replace("[PostGID]", `synapse.ggid`)\ .replace("[PostSegment]", `gcSpineHeadSegmentID`)\ .replace("[PostAlong]", "0.5")\ .replace("[Synapse]", "AmpaNmdaSynapse")\ curSyn += 1 # Add odor inputs if useOdorInput: import re, json synId = 0 with open('odor-events.json') as r: odorTimes = json.load(r) for mcid in model.mitral_gids: mc = model.mitrals[mcid] nmlmc = mcNMLs[mcid] for tuftden in mc.tuftden: # Translate NRN name into NML name tuftdenIndex = re.compile('tuftden\[(.*)\]').search(tuftden.name()).groups(1)[0] nmlTuftDenName = 'tuftden_'+tuftdenIndex nmlTuftDenSegs = [seg for seg in nmlmc.morphology.segments if seg.name.endswith(nmlTuftDenName)] alongSegment = getFractionAlongSegment(tuftden, 0.1) # Syns are placed at 0.1 nmlTuftDenSeg = nmlTuftDenSegs[alongSegment['segment_index']] nmlAlongTuftDenSeg = alongSegment['along_segment'] times = odorTimes[tuftden.name()] for i, time in enumerate(times['times']): if time > odorInputMaxTime: break # Add inputList odorInputLists += odorInputListTemplate.text\ .replace("[ID]", `synId`)\ .replace("[SynInputID]", "OdorSynTimes"+`synId`)\ .replace("[McID]", `mcid`)\ .replace("[SegmentID]", `nmlTuftDenSeg.id`)\ .replace("[FractionAlong]", `nmlAlongTuftDenSeg`) # Add synapse and timed input odorSyns += odorSynapseTemplate.text\ .replace("[ID]", `synId`)\ .replace("[Weight]", `times['weights'][i]`)\ .replace("[Time]", `time`) synId += 1 network = networkTemplate.text\ .replace("[NumGloms]", `num_gloms`) \ .replace("[GlomeruliPlaceholder]", gloms) \ .replace("[IncludesPlaceholder]", includes)\ .replace("[PopulationsPlaceholder]", populations)\ .replace("[ProjectionsPlaceholder]", projections)\ .replace("[OdorInputLists]", odorInputLists)\ .replace("[OdorInputSynapses]", odorSyns) with open(netFile, "w") as file: file.write(network) print('Net file saved to: ' + netFile)
piece exists on this cpu. For other existence questions, use mpiece_exists, mgid2pieces, and msecden. Although granules are not currently split, we have their equivalents: gpiece_exists, ggid2pieces, and gpriden. ''' from neuron import h pc = h.ParallelContext() try: from loadbalutil import lb except ImportError: pass #dictionary for mgid, mcell (whats left of it) #model.mgid2piece from modeldata import getmodel model = getmodel() def mpiece_exists(mgid): return model.mgid2piece.has_key(mgid) def mgid2pieces(mgid): ''' return cell with existing pieces for mgid; None if does not exist.''' if mpiece_exists(mgid): return model.mgid2piece[mgid] return None def msecden(mgid, i): ''' return secondary dendrite if it exists, otherwise None.''' c = mgid2pieces(mgid) if c and h.section_exists("secden", i, c): return c.secden[i]
def chk_gran_conn(): model = modeldata.getmodel() ggids = model.granule_gids mgids = model.mitral_gids mgrss = model.mgrss # for each granule, list of all mgid it connects to and vice versa g2m = {} m2g = {} for mgrs in mgrss.values(): ggid = mgrs.ggid mgid = mgrs.mgid if mgrs.gd: # granule exists on this rank if ggid not in g2m: g2m[ggid] = [] g2m[ggid].append(mgid) if mgrs.md: # mitral or mtufted exists on this rank if mgid not in m2g: m2g[mgid] = [] m2g[mgid].append(ggid) a = 'Number of granules that go to mitral and mtufted (regardless of glomerulus)' na = 0 for ms in g2m.values(): nmit = 0 nmtuft = 0 for mgid in ms: nmit += 1 if gidfunc.ismitral(mgid) else 0 nmtuft += 1 if gidfunc.ismtufted(mgid) else 0 na += 1 if nmit > 0 and nmtuft > 0 else 0 na = int(pc.allreduce(na, 1)) if pc.id() == 0: print("%d %s" % (na, a)) a = '[total,imin,imax,iavg,xmin,xmax,xavg] granule connections to mitral' b = '[total,imin,imax,iavg,xmin,xmax,xavg] granule connections to mtuft' nab = [[[], [], [], []], [[], [], [], []]] for ms in g2m.values(): # assume if first is mitral then all are mitrals otherwise mtuft dat = nab[0] if gidfunc.ismitral(ms[0]) else nab[1] # want to distinguish intra mitral, distinct mitral, and any mitral # generate map of distinct:count for that distinct instance distinct = {} for d in set(ms): distinct[d] = 0 for m in ms: distinct[m] += 1 n_any = len(ms) n_distinct_m = len(distinct) n_intra_m_min = min(distinct.values()) n_intra_m_max = max(distinct.values()) dat[0].append(n_any) dat[1].append(n_distinct_m) dat[2].append(n_intra_m_min) dat[3].append(n_intra_m_max) def nhost_len(v): return int(pc.allreduce(len(v), 1)) def nhost_sum(v): return int(pc.allreduce(sum(v), 1)) def nhost_max(v): return int(pc.allreduce(max(v), 2)) def nhost_min(v): return int(pc.allreduce(min(v), 3)) def pr0(s): if pc.id() == 0: print(s) def pr(title, dat): i = 0 pr0(title) totg = nhost_len(dat[i]) pr0("%d granules of this type" % totg) pr0("%d total connections" % nhost_sum(dat[i])) if totg: pr0("%d min ; %d max ; %g avg" % (nhost_min( dat[i]), nhost_max(dat[i]), float(nhost_sum(dat[i])) / totg)) pr(a, nab[0]) pr(b, nab[1])