示例#1
0
def coreneuron_available():
    if "NRN_ENABLE_CORENEURON=ON" not in h.nrnversion(6):
        # Not ideal. Maybe someday it will be default ON and then
        # will not appear in h.nrnversion(6)
        return False
    # But can it be loaded?
    cvode = h.CVode()
    cvode.cache_efficient(1)
    pc = h.ParallelContext()
    h.finitialize()
    result = 0
    import sys
    from io import StringIO

    original_stderr = sys.stderr
    sys.stderr = StringIO()
    try:
        pc.nrncore_run("--tstop 1 --verbose 0")
        result = 1
    except Exception as e:
        pass
    sys.stderr = original_stderr
    cvode.cache_efficient(0)
    return result
示例#2
0
def configure_hoc_env(env, bcast_template=False):
    """
    :param env: :class:'Env'
    """
    h.load_file("stdrun.hoc")
    h.load_file("loadbal.hoc")
    for template_dir in env.template_paths:
        path = "%s/rn.hoc" % template_dir
        if os.path.exists(path):
            h.load_file(path)
    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)
    h('objref pc, nc, nil')
    h('strdef dataset_path')
    if hasattr(env, 'dataset_path'):
        h.dataset_path = env.dataset_path if env.dataset_path is not None else ""
    if env.use_coreneuron:
        from neuron import coreneuron
        coreneuron.enable = True
        coreneuron.verbose = 1 if env.verbose else 0
    h.pc = h.ParallelContext()
    h.pc.gid_clear()
    env.pc = h.pc
    h.dt = env.dt
    h.tstop = env.tstop
    env.t_vec = h.Vector()  # Spike time of all cells on this host
    env.id_vec = h.Vector()  # Ids of spike times on this host
    env.t_rec = h.Vector()  # Timestamps of intracellular traces on this host
    if 'celsius' in env.globals:
        h.celsius = env.globals['celsius']
    ## more accurate integration of synaptic discontinuities
    if hasattr(h, 'nrn_netrec_state_adjust'):
        h.nrn_netrec_state_adjust = 1
    ## sparse parallel transfer
    if hasattr(h, 'nrn_sparse_partrans'):
        h.nrn_sparse_partrans = 1
示例#3
0
def test():
    from L5_pyramidal import L5Pyr
    cell = L5Pyr()

    h.load_file("stdgui.hoc")
    h.cvode_active(1)

    ns = h.NetStim()
    ns.number = 10
    ns.start = 100
    ns.interval = 50.0

    nc = h.NetCon(ns, cell.apicaltuft_ampa)
    nc.weight[0] = 0.001

    h.tstop = 2000.0

    elec = LFPElectrode([0, 100.0, 100.0], pc=h.ParallelContext())
    elec.setup()
    elec.LFPinit()
    h.run()
    elec.lfp_final()
    ion()
    plot(elec.lfp_t, elec.lfp_v)
def main():
    """ This program launches a ForSimMuscleSpindles simulation with a predefined NeuralNetwork structure and
	senory-eencoding spatiotemporal EES profiles with amplitude and frequency given by the user as argument.

	This program can be executed both with and without MPI. In case MPI is used the cells
	of the NeuralNetwork are shared between the different hosts in order to speed up the
	simulation.


	Examples of how to run this script from the terminal:
	mpiexec -np 4 python scripts/runForSimMuscleSpindlesStimModulation.py 60 280 human sensory frwSimHuman.txt testHumanSpatiotemporalEES

	"""

    parser = argparse.ArgumentParser(
        description="launch a ForSimMuscleSpindles simulation")
    parser.add_argument("eesFrequency",
                        help="ees frequency",
                        type=float,
                        choices=[gt.Range(0, 1000)])
    parser.add_argument("eesAmplitude",
                        help="ees amplitude (0-600] or %%Ia_II_Mn")
    parser.add_argument("species",
                        help="simulated species",
                        choices=["rat", "human"])
    parser.add_argument("modulation",
                        help="type of stimulation modulation",
                        choices=["sensory"])
    parser.add_argument("inputFile", help="neural network structure file")
    parser.add_argument("name", help="name to add at the output files")
    parser.add_argument("--simTime",
                        help="simulation time",
                        type=int,
                        default=-1)
    parser.add_argument("--noPlot", help="no plots flag", action="store_true")
    parser.add_argument("--burstingEes",
                        help="flag to use burst stimulation",
                        action="store_true")
    parser.add_argument("--nPulsesPerBurst",
                        help="number of pulses per burst",
                        type=int,
                        default=5)
    parser.add_argument("--burstsFrequency",
                        help="stimulation frequency within bursts",
                        type=float,
                        default=600,
                        choices=[gt.Range(0, 1000)])
    parser.add_argument(
        "--seed",
        help=
        "positive seed used to initialize random number generators (default = time.time())",
        type=int,
        choices=[gt.Range(0, 999999)])
    args = parser.parse_args()

    if args.seed is not None: sh.save_seed(args.seed)
    else: sh.save_seed(int(time.time()))

    # Import simulation specific modules
    from simulations import ForSimMuscleSpindles
    from NeuralNetwork import NeuralNetwork
    from EES import EES
    from BurstingEES import BurstingEES

    # Initialze variables...
    if args.eesAmplitude[0] == "%":
        eesAmplitude = [float(x) for x in args.eesAmplitude[1:].split("_")]
    else:
        eesAmplitude = float(args.eesAmplitude)
    name = "_" + args.species + "_" + args.name
    if args.species == "rat":
        raise (Exception(
            "Spatiotemporal modulation is not implemented for the rat model"))
    elif args.species == "human":
        muscles = hp.get_muscles_dict()
        if args.modulation == "sensory":
            fileStimMod = "../inputFiles/spatiotemporalSensoryProfile.p"
        if 'fileStimMod' in locals():
            with open(fileStimMod, 'r') as pickle_file:
                eesModulation = pickle.load(pickle_file)

    pc = h.ParallelContext()
    nn = NeuralNetwork(pc, args.inputFile)
    if not args.burstingEes:
        ees = EES(pc,
                  nn,
                  eesAmplitude,
                  args.eesFrequency,
                  pulsesNumber=100000,
                  species=args.species)
    else:
        ees = BurstingEES(pc,
                          nn,
                          eesAmplitude,
                          args.eesFrequency,
                          args.burstsFrequency,
                          args.nPulsesPerBurst,
                          species=args.species)
    ees.get_amplitude(True)
    afferentsInput = ldt.load_afferent_input(args.species, muscles)

    simulation = ForSimMuscleSpindles(pc, nn, afferentsInput, ees,
                                      eesModulation, args.simTime)

    # Run simulation, plot results and save them
    simulation.run()
    if not args.noPlot:
        simulation.plot(muscles["flex"], muscles["ext"], name, False)
    comm.Barrier()
    simulation.save_results(muscles["flex"], muscles["ext"], name)
示例#5
0
import pickle
from dipolefn import Dipole
from conf import readconf
from L5_pyramidal import L5Pyr
from L2_pyramidal import L2Pyr
from L2_basket import L2Basket
from L5_basket import L5Basket
from lfp import LFPElectrode
from morphology import shapeplot, getshapecoords

dconf = readconf()

# data directory - ./data
dproj = dconf['datdir']  # fio.return_data_dir(dconf['datdir'])
debug = dconf['debug']
pc = h.ParallelContext()
pcID = int(pc.id())
f_psim = ''
ntrial = 1
testLFP = dconf['testlfp']
testlaminarLFP = dconf['testlaminarlfp']
lelec = []  # list of LFP electrodes

# reads the specified param file
foundprm = False
for i in range(len(sys.argv)):
    if sys.argv[i].endswith('.param'):
        f_psim = sys.argv[i]
        foundprm = True
        if pcID == 0 and debug: print('using ', f_psim, ' param file.')
    elif sys.argv[i] == 'ntrial' and i + 1 < len(sys.argv):
示例#6
0
def gap_junction_test (tree, v_init):
    
    h('objref gjlist, cells, Vlog1, Vlog2')

    h.pc = h.ParallelContext()
    h.cells  = h.List()
    h.gjlist = h.List()
    
    cell1 = cells.make_neurotree_cell ("MOPPCell", neurotree_dict=tree)
    cell2 = cells.make_neurotree_cell ("MOPPCell", neurotree_dict=tree)

    h.cells.append(cell1)
    h.cells.append(cell2)

    ggid        = 20000000
    source      = 10422930
    destination = 10422670
    srcbranch   = 1
    dstbranch   = 2
    weight      = 5.4e-4

    stimdur     = 500
    tstop       = 2000
    
    h.pc.set_gid2node(source, int(h.pc.id()))
    nc = cell1.connect2target(h.nil)
    h.pc.cell(source, nc, 1)

    h.pc.set_gid2node(destination, int(h.pc.id()))
    nc = cell2.connect2target(h.nil)
    h.pc.cell(destination, nc, 1)

    stim1 = h.IClamp(cell1.sections[0](0.5))
    stim1.delay = 250
    stim1.dur = stimdur
    stim1.amp = -0.1

    stim2 = h.IClamp(cell2.sections[0](0.5))
    stim2.delay = 500+stimdur
    stim2.dur = stimdur
    stim2.amp = -0.1

    log_size = old_div(tstop,h.dt) + 1
    
    h.tlog = h.Vector(log_size,0)
    h.tlog.record (h._ref_t)

    h.Vlog1 = h.Vector(log_size)
    h.Vlog1.record (cell1.sections[0](0.5)._ref_v)

    h.Vlog2 = h.Vector(log_size)
    h.Vlog2.record (cell2.sections[0](0.5)._ref_v)
    
    h.mkgap(h.pc, h.gjlist, source, srcbranch, ggid, ggid+1, weight)
    h.mkgap(h.pc, h.gjlist, destination, dstbranch, ggid+1, ggid, weight)

    h.pc.setup_transfer()
    h.pc.set_maxstep(10.0)

    h.stdinit()
    h.finitialize(v_init)
    h.pc.barrier()

    h.tstop = tstop
    h.pc.psolve(h.tstop)

    f=open("MOPPCellGJ.dat",'w')
    for (t,v1,v2) in zip(h.tlog,h.Vlog1,h.Vlog2):
        f.write("%f %f %f\n" % (t,v1,v2))
    f.close()
示例#7
0
文件: evol.py 项目: Helveg/netpyne
import datetime
import os
import glob
from copy import copy
from random import Random
from time import sleep, time
from itertools import product
from subprocess import Popen, PIPE
import importlib, types

from neuron import h
from netpyne import specs
from .utils import createFolder
from .utils import bashTemplate

pc = h.ParallelContext()  # use bulletin board master/slave

# -------------------------------------------------------------------------------
# Evolutionary optimization
# -------------------------------------------------------------------------------


# func needs to be outside of class
def runEvolJob(nrnCommand, script, cfgSavePath, netParamsSavePath,
               simDataPath):
    """
    Function for/to <short description of `netpyne.batch.evol.runEvolJob`>

    Parameters
    ----------
    script : <type>
示例#8
0
def test_direct_memory_transfer():
    #h('''create soma''')
    cell = h.rinzelnrn()
    #h.psection()
    #dir(h)
    #h.soma.L=5.6419
    #h.soma.diam=5.6419
    #h.soma.insert("rinzelnrn")
    gc = 2.1
    pp = 0.5

    cell.soma.Ra = 1
    cell.dend.Ra = 1
    global_Ra = (1e-6 / (gc / pp *
                         (h.area(0.5) * 1e-8) * 1e-3)) / (2 * h.ri(0.5))
    cell.soma.Ra = global_Ra
    cell.soma.cm = 3
    cell.dend.Ra = global_Ra
    cell.dend.cm = 3

    # soma
    cell.soma.insert("pas")
    cell.soma.insert("kdr")
    cell.soma.insert("nafPR")

    cell.soma.gmax_nafPR = 30e-3
    cell.soma.gmax_kdr = 15e-3
    cell.soma.g_pas = 0.1e-3
    cell.soma.e_pas = -60

    # dend
    cell.dend.insert("pas")
    cell.dend.insert("rcadecay")
    cell.dend.insert("cal")
    cell.dend.insert("kcRT03")
    cell.dend.insert("rkq")

    cell.dend.g_pas = 0.1e-3
    cell.dend.e_pas = -60
    cell.dend.phi_rcadecay = 130
    cell.dend.gmax_cal = 00010e-3
    cell.dend.erev_cal = 80
    cell.dend.gmax_kcRT03 = 00015e-3
    cell.dend.gmax_rkq = 0000.8e-3
    cell.dend.ek = -75

    ic = h.IClamp(cell.soma(.5))
    ic.delay = .5
    ic.dur = 0.1
    ic.amp = 0.3

    #for testing external mod file
    #h.soma.insert("hh")

    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)

    h.tstop = 50
    h.celsius = 37.0
    h.v_init = -60
    v = h.Vector()
    v.record(cell.soma(.5)._ref_v, sec=cell.soma)
    i_mem = h.Vector()
    i_mem.record(cell.soma(.5)._ref_i_membrane_, sec=cell.soma)
    tv = h.Vector()
    tv.record(h._ref_t, sec=cell.soma)
    h.run()
    vstd = v.cl()
    tvstd = tv.cl()
    i_memstd = i_mem.cl()
    # Save current (after run) value to compare with transfer back from coreneuron
    #tran_std = [h.t, cell.soma(.5).v, cell.soma(.5).hh.m]

    from neuron import coreneuron
    coreneuron.enable = True

    pc = h.ParallelContext()
    pc.set_maxstep(10)

    h.stdinit()
    pc.psolve(h.tstop)
    #tran = [h.t, cell.soma(.5).v, cell.soma(.5).hh.m]

    assert (tv.eq(tvstd))
    #assert(v.cl().sub(vstd).abs().max() < 1e-10)
    assert (i_mem.cl().sub(i_memstd).abs().max() < 1e-10)
    #assert(h.Vector(tran_std).sub(h.Vector(tran)).abs().max() < 1e-10)

    f = open('v.dat', 'w')
    for i in range(tv.size()):
        print('{} {}'.format(tv[i], v[i]), file=open("v.dat", "a"))

    h.quit()
示例#9
0
def test_spikes(use_mpi4py=False):
    if use_mpi4py:
        from mpi4py import MPI
    h('''create soma''')
    h.soma.L = 5.6419
    h.soma.diam = 5.6419
    h.soma.insert("hh")
    h.soma.nseg = 3
    ic = h.IClamp(h.soma(.25))
    ic.delay = .1
    ic.dur = 0.1
    ic.amp = 0.3

    ic2 = h.IClamp(h.soma(.75))
    ic2.delay = 5.5
    ic2.dur = 1
    ic2.amp = 0.3

    h.tstop = 10
    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)

    pc = h.ParallelContext()

    pc.set_gid2node(pc.id() + 1, pc.id())
    myobj = h.NetCon(h.soma(0.5)._ref_v, None, sec=h.soma)
    pc.cell(pc.id() + 1, myobj)

    # NEURON spikes run
    nrn_spike_t = h.Vector()
    nrn_spike_gids = h.Vector()
    pc.spike_record(-1, nrn_spike_t, nrn_spike_gids)

    h.run()

    nrn_spike_t = nrn_spike_t.to_python()
    nrn_spike_gids = nrn_spike_gids.to_python()

    # CORENEURON spike_record(-1) / spike_record(gidlist):
    from neuron import coreneuron
    coreneuron.enable = True
    coreneuron.verbose = 0
    h.stdinit()
    corenrn_all_spike_t = h.Vector()
    corenrn_all_spike_gids = h.Vector()

    pc.spike_record(-1 if pc.id() == 0 else (pc.id()), corenrn_all_spike_t,
                    corenrn_all_spike_gids)
    pc.psolve(h.tstop)

    corenrn_all_spike_t = corenrn_all_spike_t.to_python()
    corenrn_all_spike_gids = corenrn_all_spike_gids.to_python()

    # check spikes match
    assert (len(nrn_spike_t))  # check we've actually got spikes
    assert (len(nrn_spike_t) == len(nrn_spike_gids))
    # matching no. of gids
    assert (nrn_spike_t == corenrn_all_spike_t)
    assert (nrn_spike_gids == corenrn_all_spike_gids)

    h.quit()
示例#10
0
def test_fastimem_corenrn():

    if not coreneuron_available():
        return

    print("test_fastimem_corenrn")
    pc = h.ParallelContext()
    ncell = 5
    cvode = h.CVode()
    cvode.cache_efficient(0)
    cells = [Cell(id, 10) for id in range(ncell)]
    cvode.use_fast_imem(1)
    imem = [h.Vector().record(cell.secs[3](0.5)._ref_i_membrane_) for cell in cells]
    tstop = 1.0

    def init_v():
        # to get nontrivial initialized i_membrane_, initialize to random voltage.
        r = h.Random()
        r.Random123(0, 1, 0)
        for sec in h.allsec():
            for seg in sec.allseg():
                # don't care if some segments counted twice
                seg.v = -65.0 + r.uniform(0, 5)
        h.finitialize()

    def run(tstop):
        pc.set_maxstep(10)
        init_v()
        pc.psolve(tstop)

    # standard
    run(tstop)
    imem_std = [vec.c() for vec in imem]

    def compare():
        for i in range(ncell):
            if not imem_std[i].eq(imem[i]):
                print("imem for cell ", i)
                for j, x in enumerate(imem_std[i]):
                    print(j, x, imem[i][j], x - imem[i][j])
            assert imem_std[i].eq(imem[i])
            imem[i].resize(0)

    compare()  # just starting iwth imem cleared

    print("cache efficient NEURON")
    cvode.cache_efficient(1)
    run(tstop)
    compare()

    print("direct mode (online) coreneuron")
    from neuron import coreneuron

    coreneuron.enable = True
    coreneuron.verbose = 0
    coreneuron.cell_permute = 0
    run(tstop)
    compare()
    coreneuron.enable = False

    print("Are the i_membrane_ trajectories correct when ...")

    tvec = h.Vector().record(h._ref_t)
    init_v()
    while h.t < tstop - h.dt / 2:
        dt_above = 1.1*h.dt # comfortably above dt to avoid 0 step advance
        coreneuron.enable = True
        told = h.t
        pc.psolve(h.t + dt_above)
        assert h.t > told
        coreneuron.enable = False
        pc.psolve(h.t + dt_above)
    compare()

    print("For file mode (offline) coreneuron comparison of i_membrane_ initialization")

    init_v()
    print_fast_imem()

    # The cells must have gids.
    for i, cell in enumerate(cells):
        pc.set_gid2node(i, pc.id())
        sec = cell.secs[0]
        pc.cell(i, h.NetCon(sec(0.5)._ref_v, None, sec=sec))

    # Write the data files
    init_v()
    pc.nrncore_write("./corenrn_data")

    # args needed for offline run of coreneuron
    coreneuron.enable = True
    coreneuron.file_mode = True
    arg = coreneuron.nrncore_arg(tstop)
    coreneuron.enable = False
    pc.gid_clear()
    print(arg)

    cvode.use_fast_imem(0)
示例#11
0
    def simulate(self, args):

        start = timeit.default_timer()

        from .simulate import SnuddaSimulate

        if (args.networkFile):
            networkFile = args.networkFile
        else:
            networkFile = self.networkPath \
              + "/network-pruned-synapses.hdf5"

        if (args.inputFile):
            inputFile = args.inputFile
        else:
            inputFile = self.networkPath + "/input-spikes.hdf5"

        self.makeDirIfNeeded(self.networkPath + "/simulation")

        print("Using input file " + inputFile)

        #nWorkers = args.ncores
        #print("Using " + str(nWorkers) + " workers for neuron")

        # Problems with nested symbolic links when the second one is a relative
        # path going beyond the original base path
        if (args.mechDir is None):
            mechDir = os.path.dirname(networkFile) + "/mechanisms"

            # !!! problem with paths, testing to create mechanism dir in current dir
            mechDir = "mechanisms"

            if (not os.path.exists(mechDir)):
                mDir = os.path.dirname(__file__) + "/data/cellspecs/mechanisms"
                os.symlink(mDir, mechDir)
        else:
            mechDir = args.mechDir

        # !!! These are saved in current directory x86_64
        # --- problem since nrnivmodl seems to want a relative path...

        makeModsStr = "nrnivmodl " + mechDir
        if (not os.path.exists('x86_64')):
            print("Please first run: " + makeModsStr)
            exit(-1)
            # I was having problems when running nrnivmodl in the script, but
            # running it manually in bash works... WHY?!!

        # os.system(makeModsStr)

        saveDir = os.path.dirname(networkFile) + "/simulation/"

        if (not os.path.exists(saveDir)):
            print("Creating directory " + saveDir)
            os.makedirs(saveDir, exist_ok=True)

        # Get the SlurmID, used in default file names
        SlurmID = os.getenv('SLURM_JOBID')

        if (SlurmID is None):
            SlurmID = str(666)

        print("args: " + str(args))

        if (args.voltOut is not None):
            # Save neuron voltage
            if (args.voltOut == "default"):
                voltFile = saveDir + 'network-voltage-' + SlurmID + '.csv'
            else:
                voltFile = args.voltOut
        else:
            voltFile = None

        if (args.spikesOut is None or args.spikesOut == "default"):
            spikesFile = saveDir + 'network-output-spikes-' + SlurmID + '.txt'
        else:
            spikesFile = args.spikesOut

        disableGJ = args.disableGJ
        if (disableGJ):
            print("!!! WE HAVE DISABLED GAP JUNCTIONS !!!")

        logFile = os.path.dirname(networkFile) \
          + "/log/network-simulation-log.txt"

        logDir = os.path.dirname(networkFile) + "/log"
        if (not os.path.exists(logDir)):
            print("Creating directory " + logDir)
            os.makedirs(logDir, exist_ok=True)

        from mpi4py import MPI  # This must be imported before neuron, to run parallel
        from neuron import h  #, gui

        pc = h.ParallelContext()

        sim = SnuddaSimulate(networkFile=networkFile,
                             inputFile=inputFile,
                             disableGapJunctions=disableGJ,
                             logFile=logFile,
                             verbose=args.verbose)

        sim.addExternalInput()
        sim.checkMemoryStatus()

        if (voltFile is not None):
            sim.addRecording(
                sideLen=None)  # Side len let you record from a subset
            #sim.addRecordingOfType("dSPN",5) # Side len let you record from a subset

        tSim = args.time * 1000  # Convert from s to ms for Neuron simulator

        sim.checkMemoryStatus()
        print("Running simulation for " + str(tSim) + " ms.")
        sim.run(tSim)  # In milliseconds

        print("Simulation done, saving output")
        if (spikesFile is not None):
            sim.writeSpikes(spikesFile)

        if (voltFile is not None):
            sim.writeVoltage(voltFile)

        stop = timeit.default_timer()
        if (sim.pc.id() == 0):
            print("Program run time: " + str(stop - start))

        # sim.plot()
        exit(0)
def init(env):
    h.load_file("nrngui.hoc")
    h.load_file("loadbal.hoc")
    h('objref fi_status, fi_checksimtime, pc, nclist, nc, nil')
    h('strdef datasetPath')
    h('numCells = 0')
    h('totalNumCells = 0')
    h('max_walltime_hrs = 0')
    h('mkcellstime = 0')
    h('mkstimtime = 0')
    h('connectcellstime = 0')
    h('connectgjstime = 0')
    h('results_write_time = 0')
    h.nclist = h.List()
    datasetPath = os.path.join(env.datasetPrefix, env.datasetName)
    h.datasetPath = datasetPath
    ##  new ParallelContext object
    h.pc = h.ParallelContext()
    env.pc = h.pc
    rank = int(env.pc.id())
    nhosts = int(env.pc.nhost())
    ## polymorphic value template
    h.load_file("./templates/Value.hoc")
    ## randomstream template
    h.load_file("./templates/ranstream.hoc")
    ## stimulus cell template
    h.load_file("./templates/StimCell.hoc")
    h.xopen("./lib.hoc")
    h.dt = env.dt
    h.tstop = env.tstop
    if env.optldbal or env.optlptbal:
        lb = h.LoadBalance()
        if not os.path.isfile("mcomplex.dat"):
            lb.ExperimentalMechComplex()

    if (env.pc.id() == 0):
        mkspikeout(env, env.spikeoutPath)
    env.pc.barrier()
    h.startsw()
    mkcells(env)
    env.mkcellstime = h.stopsw()
    env.pc.barrier()
    if (env.pc.id() == 0):
        print "*** Cells created in %g seconds" % env.mkcellstime
    print "*** Rank %i created %i cells" % (env.pc.id(), len(env.cells))
    h.startsw()
    mkstim(env)
    env.mkstimtime = h.stopsw()
    if (env.pc.id() == 0):
        print "*** Stimuli created in %g seconds" % env.mkstimtime
    env.pc.barrier()
    h.startsw()
    connectcells(env)
    env.connectcellstime = h.stopsw()
    env.pc.barrier()
    if (env.pc.id() == 0):
        print "*** Connections created in %g seconds" % env.connectcellstime
    print "*** Rank %i created %i connections" % (env.pc.id(), int(h.nclist.count()))
    h.startsw()
    # connectgjs(env)
    env.connectgjstime = h.stopsw()
    if (env.pc.id() == 0):
        print "*** Gap junctions created in %g seconds" % env.connectgjstime
    env.pc.setup_transfer()
    env.pc.set_maxstep(10.0)
    h.max_walltime_hrs = env.max_walltime_hrs
    h.mkcellstime = env.mkcellstime
    h.mkstimtime = env.mkstimtime
    h.connectcellstime = env.connectcellstime
    h.connectgjstime = env.connectgjstime
    h.results_write_time = env.results_write_time
    h.fi_checksimtime = h.FInitializeHandler("checksimtime(pc)")
    if (env.pc.id() == 0):
        print "dt = %g" % h.dt
        print "tstop = %g" % h.tstop
        h.fi_status = h.FInitializeHandler("simstatus()")
    h.v_init = env.v_init
    h.stdinit()
    h.finitialize(env.v_init)
    env.pc.barrier()
    if env.optldbal or env.optlptbal:
        cx(env)
        ld_bal(env)
        if env.optlptbal:
            lpt_bal(env)
示例#13
0
 def __init__(self, ho):
     #h.mulfit_after_quad_pycallback = self.after_quad
     pc = h.ParallelContext()
     nhost = int(pc.nhost_bbs())
     if nhost > 1:
         fitglobals.verbose = 0
     self.xlvec = h.Vector()
     self.ylvec = h.Vector()
     self.g = None
     self.rf = ho
     ol = []
     vl = self.rf.yvarlist
     fl = self.rf.fitnesslist
     tlast = 0
     self.n_chdat = 0
     for i in range(len(vl)):
         self.n_chdat += fl.o(i).n_chdat
         tl = list(fl.o(i).xdat_)
         o = obs.NeuronObservable(vl.o(i), tl)
         o.sigma = 0.01
         ol.append(o)
         if (tlast < tl[-1]):
             tlast = tl[-1]
     s = h.Vector()
     cvodewrap.active(1)
     cvodewrap.states(s)
     assert (len(s) > 0)
     assert (len(vl) > 0)
     self.covGrowthTime = 100
     self.varTerm = 1
     self.processNoise = []
     self.Sdiag = []
     Sto = sto.StochasticModel(len(s), tlast)
     for i in range(len(s)):
         self.Sdiag.append(WrappedVal(Sto.InitialCovSqrt[i, i]))
     for i in range(len(s)):
         self.processNoise.append(WrappedVal(Sto.B[i, i]))
     Obs = obs.ObservationModel(ol)
     self.Eve = eve.EventTable(Sto, Obs)
     self.hhB = self.Eve.Sto.hhB
     self.nNa = self.Eve.Sto.nNa
     self.nK = self.Eve.Sto.nK
     self.Sys = detsys.NeuronModel()
     self.inj_invl = 1.0
     self.Eve.newInjectionInterval(self.inj_invl)
     # self.inj_invl_changed(Sys, P.tstop)
     # self.M = models.Model(Sys, Obs, P)
     self.Data = self.__data(fl, self.Eve)
     self.pf = self.getParmFitness()
     self.pf.verbose = fitglobals.verbose
     self.dlikedt = h.Vector()
     self.likefailed = False
     #CONSTRAINTS GUI INIT
     s = h.Vector()
     cvodewrap.states(s)
     nstates = len(s)
     self.geq0 = []
     self.leq1 = []
     self.sumto1 = []
     self.cvxopt_sel = True
     self.custom_sel = True
     self.nsums = 2
     for j in range(self.nsums):
         self.sumto1.append([])
     for i in range(nstates):
         self.geq0.append(xstruct())
         self.leq1.append(xstruct())
         for j in range(self.nsums):
             self.sumto1[j].append(xstruct())
     EKF.constraintsOn(self.geq0, self.leq1, self.sumto1)
示例#14
0
"""Illustrates the asynchrony of multiple processors"""
from neuron import h
print('all')
pc = h.ParallelContext()  # here's the splitting
print(pc.id(), ' of ', pc.nhost())
if pc.id() == 0: print('Message from rank0 (node0)')
h.quit()
示例#15
0
def main():

    parser = argparse.ArgumentParser(description="Parallel transfer test.")
    parser.add_argument(
        "--sparse-partrans",
        dest="sparse_partrans",
        default=False,
        action="store_true",
        help="use sparse parallel transfer",
    )
    parser.add_argument(
        "--result-prefix",
        default=".",
        help="place output files in given directory (must exist before launch)",
    )
    parser.add_argument("--ngids",
                        default=2,
                        type=int,
                        help="number of gids to create (must be even)")

    args, unknown = parser.parse_known_args()

    pc = h.ParallelContext()
    myrank = int(pc.id())

    mkcells(pc, args.ngids)
    mkgjs(pc, args.ngids)

    pc.setup_transfer()

    if args.sparse_partrans:
        if hasattr(h, "nrn_sparse_partrans"):
            h.nrn_sparse_partrans = 1

    rec_t = h.Vector()
    rec_t.record(h._ref_t)

    wt = time.time()

    h.dt = 0.25
    pc.set_maxstep(10)
    h.finitialize(-65)
    pc.psolve(500)

    total_wt = time.time() - wt

    gjtime = pc.vtransfer_time()

    print("rank %d: parallel transfer time: %.02f" % (myrank, gjtime))
    print("rank %d: total compute time: %.02f" % (myrank, total_wt))

    output = itertools.chain(
        [np.asarray(rec_t.to_python())],
        [np.asarray(vrec.to_python()) for vrec in vrecs],
    )
    np.savetxt(
        "%s/ParGJ_%04i.dat" % (args.result_prefix, myrank),
        np.column_stack(tuple(output)),
    )

    pc.runworker()
    pc.done()

    h.quit()
示例#16
0
def main():
	""" This program launches a ForwardSimulation simulation with a predefined NeuralNetwork structure,
	different stimulation amplitudes are tested to evealuate the muslce recruitment curve.
	The plots resulting from this simulation are saved in the results folder.

	This program can be executed both with and without MPI. In case MPI is used the cells
	of the NeuralNetwork are shared between the different hosts in order to speed up the
	simulation.

	This script is run by the runBatchOfRecCurve.py script. 
	"""

	parser = argparse.ArgumentParser(description="Estimate the reflex responses induced by a range of stimulation amplitudes")
	parser.add_argument("inputFile", help="neural network structure file")
	parser.add_argument("--name", help="name to add at the output files", type=str, default="")
	parser.add_argument("--noPlot", help=" no plot flag", action="store_true")
	parser.add_argument("--mnReal", help=" real mn flag", action="store_true")
	parser.add_argument("--burstingEes", help="flag to use burst stimulation", action="store_true")
	parser.add_argument("--nPulsesPerBurst", help="number of pulses per burst", type=int, default=5)
	parser.add_argument("--burstsFrequency", help="stimulation frequency within bursts",type=float, default=600, choices=[gt.Range(0,1000)])
	parser.add_argument("--seed", help="positive seed used to initialize random number generators (default = time.time())", type=int, choices=[gt.Range(0,999999)])
	parser.add_argument("--membranePotential", help="flag to compute the membrane potential", action="store_true")
	parser.add_argument("--muscleName", help="flag to compute the membrane potential", type=str, default="GM")
	args = parser.parse_args()

	if args.seed is not None: sh.save_seed(args.seed)
	else: sh.save_seed(int(time.time()))

	# Import simulation specific modules
	from simulations import ForwardSimulation
	from simulations import ForSimSpinalModulation
	from NeuralNetwork import NeuralNetwork
	from EES import EES
	from BurstingEES import BurstingEES

	# Initialize parameters
	eesAmplitudes = [[x,0,0] for x in np.arange(0.05,1.05,0.05)]
	eesFrequency = 10
	simTime = 150
	plotResults = True


	# Create a Neuron ParallelContext object to support parallel simulations
	pc = h.ParallelContext()
	nn=NeuralNetwork(pc,args.inputFile)

	if not args.burstingEes: ees = EES(pc,nn,eesAmplitudes[0],eesFrequency)
	else: ees = BurstingEES(pc,nn,eesAmplitudes[0],eesFrequency,args.burstsFrequency,args.nPulsesPerBurst)
	afferentsInput = None
	eesModulation = None

	if args.membranePotential:
		if args.mnReal:
			cellsToRecord = {"MnReal":[mn.soma for mn in nn.cells[args.muscleName]['MnReal']]}
			modelTypes = {"MnReal":"real"}
		else:
			cellsToRecord = {"Mn":nn.cells[args.muscleName]['Mn']}
			modelTypes = {"Mn":"artificial"}
		simulation = ForSimSpinalModulation(pc,nn,cellsToRecord,modelTypes, afferentsInput, ees, eesModulation, simTime)
		membranePotentials = []
	else: simulation = ForwardSimulation(pc,nn, afferentsInput, ees, eesModulation, simTime)

	mEmg = []
	mSpikes = []
	mStatTemp = []
	nSamplesToAnalyse = -100 # last 100 samples

	if not args.noPlot:
		fig, ax = plt.subplots(3,figsize=(16,9),sharex='col',sharey='col')
		fig2, ax2 = plt.subplots(1,figsize=(16,3))

	for eesAmplitude in eesAmplitudes:
		ees.set_amplitude(eesAmplitude)
		percFiberActEes = ees.get_amplitude(True)
		simulation.run()

		# Extract emg responses
		try: mEmg.append(simulation.get_estimated_emg(args.muscleName)[nSamplesToAnalyse:])
		except (ValueError, TypeError) as error: mEmg.append(np.zeros(abs(nSamplesToAnalyse)))

		# Extract mn spikes
		try: mSpikes.append(simulation.get_mn_spikes_profile(args.muscleName)[nSamplesToAnalyse:])
		except (ValueError, TypeError) as error: mSpikes.append(np.zeros(abs(nSamplesToAnalyse)))

		# plot mn membrane potentials
		if args.membranePotential:
			title = "%s_amp_%d_Ia_%f_II_%f_Mn_%f"%(args.name,percFiberActEes[0],percFiberActEes[1],percFiberActEes[2],percFiberActEes[3])
			try: fileName = "%s_amp_%d"%(args.name,eesAmplitude)
			except: fileName = "%s_amp_%.2f"%(args.name,eesAmplitude[0])
			simulation.plot_membrane_potatial(fileName,title)

		# Compute statistics
		mStatTemp.append(np.abs(mEmg[-1]).sum())

		if rank==0 and not args.noPlot:
			ax[1].plot(mEmg[-1])
			ax[0].plot(mSpikes[-1])
		comm.Barrier()

	if rank==0:
		resultsFolder = "../../results/"
		generalFileName = time.strftime("%Y_%m_%d_recCurve"+args.name)
		mStat = np.array(mStatTemp).sum()
		if not args.noPlot:
			ax[2].bar(1, mStat, 0.2)
			ax[0].set_title('Mn action potentials')
			ax[1].set_title('EMG response')
			ax[2].set_title('Statistic')

			ax2.plot([np.sum(x) for x in mSpikes])

			fileName = generalFileName+".pdf"
			pp = PdfPages(resultsFolder+fileName)
			pp.savefig(fig)
			pp.close()
			plt.show()

		fileName = generalFileName+".p"
		with open(resultsFolder+fileName, 'w') as pickle_file:
			pickle.dump(mSpikes, pickle_file)
			pickle.dump(mEmg, pickle_file)
			pickle.dump(mStat, pickle_file)

	comm.Barrier()
示例#17
0
 def __init__(self, p):
     # set the params internally for this net
     # better than passing it around like ...
     self.p = p
     # Number of time points
     # Originally used to create the empty vec for synaptic currents,
     # ensuring that they exist on this node irrespective of whether
     # or not cells of relevant type actually do
     self.N_t = np.arange(0., h.tstop, self.p['dt']).size + 1
     # Create a h.Vector() with size 1xself.N_t, zero'd
     self.current = {
         'L5Pyr_soma': h.Vector(self.N_t, 0),
         'L2Pyr_soma': h.Vector(self.N_t, 0),
     }
     # int variables for grid of pyramidal cells (for now in both L2 and L5)
     self.gridpyr = {
         'x': self.p['N_pyr_x'],
         'y': self.p['N_pyr_y'],
     }
     # Parallel stuff
     self.pc = h.ParallelContext()
     self.n_hosts = int(self.pc.nhost())
     self.rank = int(self.pc.id())
     self.N_src = 0
     # seed debugging
     # for key, val in self.p.items():
     #     if key.startswith('prng_seedcore_'):
     #         print("in net: %i, %s, %i" % (self.rank, key, val))
     self.N = {}  # numbers of sources
     self.N_cells = 0  # init self.N_cells
     # zdiff is expressed as a positive DEPTH of L5 relative to L2
     # this is a deviation from the original, where L5 was defined at 0
     # this should not change interlaminar weight/delay calculations
     self.zdiff = 1307.4
     # params of external inputs in p_ext
     # Global number of external inputs ... automatic counting makes more sense
     # p_unique represent ext inputs that are going to go to each cell
     self.p_ext, self.p_unique = paramrw.create_pext(self.p, h.tstop)
     self.N_extinput = len(self.p_ext)
     # Source list of names
     # in particular order (cells, extinput, alpha names of unique inputs)
     self.src_list_new = self.__create_src_list()
     # cell position lists, also will give counts: must be known by ALL nodes
     # extinput positions are all located at origin.
     # sort of a hack bc of redundancy
     self.pos_dict = dict.fromkeys(self.src_list_new)
     # create coords in pos_dict for all cells first
     self.__create_coords_pyr()
     self.__create_coords_basket()
     self.__count_cells()
     # create coords for all other sources
     self.__create_coords_extinput()
     # count external sources
     self.__count_extsrcs()
     # create dictionary of GIDs according to cell type
     # global dictionary of gid and cell type
     self.gid_dict = {}
     self.__create_gid_dict()
     # assign gid to hosts, creates list of gids for this node in __gid_list
     # __gid_list length is number of cells assigned to this id()
     self.__gid_list = []
     self.__gid_assign()
     # create cells (and create self.origin in create_cells_pyr())
     self.cells = []
     self.extinput_list = []
     # external unique input list dictionary
     self.ext_list = dict.fromkeys(self.p_unique)
     # initialize the lists in the dict
     for key in self.ext_list.keys():
         self.ext_list[key] = []
     # create sources and init
     self.__create_all_src()
     self.state_init()
     # parallel network connector
     self.__parnet_connect()
     # set to record spikes
     self.spiketimes = h.Vector()
     self.spikegids = h.Vector()
     self.__record_spikes()
def main(import_mpi4py, run_nrnmpi_init, procs_per_worker, sleep):
    """

    :param import_mpi4py: int
    :param run_nrnmpi_init: bool
    :param procs_per_worker: int
    :param sleep: float
    """
    time.sleep(sleep)
    if import_mpi4py == 1:
        order = 'before'
        from mpi4py import MPI
        time.sleep(1.)
        print('test_mpiguard: getting past import mpi4py')
        sys.stdout.flush()
        time.sleep(1.)

    from neuron import h
    time.sleep(1.)
    print('test_mpiguard: getting past from neuron import h')
    sys.stdout.flush()
    time.sleep(1.)

    if run_nrnmpi_init:
        try:
            h.nrnmpi_init()
            time.sleep(1.)
            print('test_mpiguard: getting past h.nrnmpi_init()')
            sys.stdout.flush()
            time.sleep(1.)
        except:
            print(
                'test_mpiguard: problem calling h.nrnmpi_init(); may not be defined in this version of NEURON'
            )
            time.sleep(1.)
            sys.stdout.flush()
            time.sleep(1.)
    else:
        print('test_mpiguard: h.nrnmpi_init() not executed')
        time.sleep(1.)
        sys.stdout.flush()
        time.sleep(1.)
    if import_mpi4py == 2:
        order = 'after'
        from mpi4py import MPI
        print('test_mpiguard: getting past import mpi4py')
        time.sleep(1.)
        sys.stdout.flush()
        time.sleep(1.)

    if import_mpi4py > 0:
        comm = MPI.COMM_WORLD

    pc = h.ParallelContext()
    pc.subworlds(procs_per_worker)
    global_rank = int(pc.id_world())
    global_size = int(pc.nhost_world())
    rank = int(pc.id())
    size = int(pc.nhost())

    if import_mpi4py > 0:
        print(
            'test_mpiguard: mpi4py imported %s neuron: process id: %i; global rank: %i / %i; local rank: %i / %i; '
            'comm.rank: %i; comm.size: %i' %
            (order, os.getpid(), global_rank, global_size, rank, size,
             comm.rank, comm.size))
    else:
        print(
            'test_mpiguard: mpi4py not imported: process id: %i; global rank: %i / %i; local rank: %i / %i'
            % (os.getpid(), global_rank, global_size, rank, size))
    time.sleep(1.)
    sys.stdout.flush()
    time.sleep(1.)

    pc.runworker()
    time.sleep(1.)
    print('test_mpiguard: got past pc.runworker()')
    time.sleep(1.)
    sys.stdout.flush()
    time.sleep(1.)

    # catch workers escaping from runworker loop
    if global_rank != 0:
        print(
            'test_mpiguard: global_rank: %i escaped from the pc.runworker loop'
        )
        sys.stdout.flush()
        time.sleep(1.)
        os._exit(1)

    pc.done()
    time.sleep(1.)
    print('test_mpiguard: got past pc.done()')
    time.sleep(1.)
    sys.stdout.flush()
    time.sleep(1.)

    print('calling h_quit')
    sys.stdout.flush()
    time.sleep(1.)
    h.quit()
    time.sleep(1.)
    sys.stdout.flush()
    time.sleep(1.)
示例#19
0
def test_spikes(use_mpi4py=False, use_nrnmpi_init=False, file_mode=False):
    # mpi4py needs tp be imported before importing h
    if use_mpi4py:
        from mpi4py import MPI
        from neuron import h, gui
    # without mpi4py we need to call nrnmpi_init explicitly
    elif use_nrnmpi_init:
        from neuron import h, gui
        h.nrnmpi_init()
    # otherwise serial execution
    else:
        from neuron import h, gui

    h('''create soma''')
    h.soma.L=5.6419
    h.soma.diam=5.6419
    h.soma.insert("hh")
    h.soma.nseg = 3
    ic = h.IClamp(h.soma(.25))
    ic.delay = .1
    ic.dur = 0.1
    ic.amp = 0.3

    ic2 = h.IClamp(h.soma(.75))
    ic2.delay = 5.5
    ic2.dur = 1
    ic2.amp = 0.3

    h.tstop = 10
    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)

    pc = h.ParallelContext()

    pc.set_gid2node(pc.id()+1, pc.id())
    myobj = h.NetCon(h.soma(0.5)._ref_v, None, sec=h.soma)
    pc.cell(pc.id()+1, myobj)

    # NEURON run
    nrn_spike_t = h.Vector()
    nrn_spike_gids = h.Vector()

    # rank 0 record spikes for all gid while others
    # for specific gid. this is for better test coverage.
    pc.spike_record(-1 if pc.id() == 0 else (pc.id()+1), nrn_spike_t, nrn_spike_gids)

    h.run()

    nrn_spike_t = nrn_spike_t.to_python()
    nrn_spike_gids = nrn_spike_gids.to_python()

    # CORENEURON run
    from neuron import coreneuron
    coreneuron.enable = True
    coreneuron.file_mode = file_mode
    coreneuron.verbose = 0
    h.stdinit()
    corenrn_all_spike_t = h.Vector()
    corenrn_all_spike_gids = h.Vector()

    pc.spike_record(-1, corenrn_all_spike_t, corenrn_all_spike_gids )
    pc.psolve(h.tstop)

    corenrn_all_spike_t = corenrn_all_spike_t.to_python()
    corenrn_all_spike_gids = corenrn_all_spike_gids.to_python()

    # check spikes match
    assert(len(nrn_spike_t)) # check we've actually got spikes
    assert(len(nrn_spike_t) == len(nrn_spike_gids)); # matching no. of gids
    assert(nrn_spike_t == corenrn_all_spike_t)
    assert(nrn_spike_gids == corenrn_all_spike_gids)

    h.quit()
示例#20
0
Functions to import/export to SONATA format
"""

import os
import sys

try:
    import tables  # requires installing hdf5 via brew and tables via pip!
    from neuroml.hdf5.NeuroMLXMLParser import NeuroMLXMLParser
    from neuroml.loaders import read_neuroml2_file
    from pyneuroml import pynml
    from . import neuromlFormat  # import NetPyNEBuilder
    from  netpyne.support.nml_reader  import NMLTree
except ImportError:
    from neuron import h
    pc = h.ParallelContext() # MPI: Initialize the ParallelContext class
    if int(pc.id()) == 0:  # only print for master node
        print('Note: SONATA import failed; import/export functions for SONATA will not be available. \n  To use this feature please install "HDF5" and the "tables" Python package.')

from . import neuronPyHoc
from .. import sim, specs
import neuron
from neuron import h
h.load_file('stdgui.hoc')
h.load_file('import3d.hoc')

import pprint
pp = pprint.PrettyPrinter(depth=6)

# ------------------------------------------------------------------------------------------------------------
# Helper functions (some adapted from https://github.com/NeuroML/NeuroMLlite/)
示例#21
0
    def simulate(self, args):

        start = timeit.default_timer()

        from snudda.simulate.simulate import SnuddaSimulate

        if args.network_file:
            network_file = args.network_file
        else:
            network_file = os.path.join(self.network_path,
                                        "network-synapses.hdf5")

        if args.input_file:
            input_file = args.input_file
        else:
            input_file = os.path.join(self.network_path, "input-spikes.hdf5")

        self.make_dir_if_needed(os.path.join(self.network_path, "simulation"))

        print(f"Using input file {input_file}")

        # nWorkers = args.ncores
        # print("Using " + str(nWorkers) + " workers for neuron")

        # Problems with nested symbolic links when the second one is a relative
        # path going beyond the original base path
        if args.mech_dir is None:
            # mech_dir = os.path.join(os.path.dirname(network_file), "mechanisms")

            # TODO!!! problem with paths, testing to create mechanism dir in current dir
            mech_dir = "mechanisms"

            if not os.path.exists(mech_dir):
                try:
                    m_dir = os.path.realpath(
                        os.path.join(os.path.dirname(__file__), "data",
                                     "neurons", "mechanisms"))
                    os.symlink(m_dir, mech_dir)
                except:
                    print(f"Failed to create symlink {mech_dir} -> {m_dir}")
        else:
            mech_dir = args.mech_dir

        # !!! These are saved in current directory x86_64
        # --- problem since nrnivmodl seems to want a relative path...

        make_mods_str = f"nrnivmodl {mech_dir}"

        # x86_64 on linux, nrnmech.dll on windows...
        if not os.path.exists("x86_64") and not os.path.exists("nrnmech.dll"):
            print(f"Please first run: {make_mods_str}")
            os.sys.exit(-1)
            # I was having problems when running nrnivmodl in the script, but
            # running it manually in bash works... WHY?!!

        # os.system(makeModsStr)

        save_dir = os.path.join(os.path.dirname(network_file), "simulation")

        if not os.path.exists(save_dir):
            print(f"Creating directory {save_dir}")
            os.makedirs(save_dir, exist_ok=True)

        # Get the SlurmID, used in default file names
        slurm_id = os.getenv('SLURM_JOBID')

        if slurm_id is None:
            slurm_id = str(666)

        print(f"args: {args}")

        if args.volt_out is not None:
            # Save neuron voltage
            if args.volt_out == "default":
                volt_file = os.path.join(save_dir,
                                         f"network-voltage-{slurm_id}.csv")
            else:
                volt_file = args.volt_out
        else:
            volt_file = None

        if args.spikes_out is None or args.spikes_out == "default":
            spikes_file = os.path.join(
                save_dir, f"network-output-spikes-{slurm_id}.txt")
        else:
            spikes_file = args.spikes_out

        disable_gj = args.disable_gj
        if disable_gj:
            print("!!! WE HAVE DISABLED GAP JUNCTIONS !!!")

        log_file = os.path.join(os.path.dirname(network_file), "log",
                                "network-simulation-log.txt")

        log_dir = os.path.join(os.path.dirname(network_file), "log")
        if not os.path.exists(log_dir):
            print(f"Creating directory {log_dir}")
            os.makedirs(log_dir, exist_ok=True)

        from mpi4py import MPI  # This must be imported before neuron, to run parallel
        from neuron import h  # , gui

        pc = h.ParallelContext()

        # Simulate is deterministic, no random seed.
        sim = SnuddaSimulate(network_file=network_file,
                             input_file=input_file,
                             disable_gap_junctions=disable_gj,
                             log_file=log_file,
                             verbose=args.verbose)

        sim.add_external_input()

        sim.check_memory_status()

        if volt_file is not None:
            sim.add_recording(
                side_len=None)  # Side len let you record from a subset
            # sim.addRecordingOfType("dSPN",5) # Side len let you record from a subset

        t_sim = args.time * 1000  # Convert from s to ms for Neuron simulator

        if args.exportCoreNeuron:
            sim.export_to_core_neuron()
            return  # We do not run simulation when exporting to core neuron

        sim.check_memory_status()
        print("Running simulation for " + str(t_sim) + " ms.")
        sim.run(t_sim)  # In milliseconds

        print("Simulation done, saving output")
        if spikes_file is not None:
            sim.write_spikes(spikes_file)

        if volt_file is not None:
            sim.write_voltage(volt_file)

        stop = timeit.default_timer()
        if sim.pc.id() == 0:
            print("Program run time: " + str(stop - start))
示例#22
0
def setup_nrnbbcore_register_mapping(rings):

    #for recording
    recordlist = []

    #vector for soma sections and segment
    somasec = h.Vector()
    somaseg = h.Vector()

    #vector for dendrite sections and segment
    densec = h.Vector()
    denseg = h.Vector()

    pc = h.ParallelContext()

    #all rings in the simulation
    for ring in rings:

        #every gid in the ring
        for gid in ring.gids:

            #clear previous vector if any
            somasec.size(0)
            somaseg.size(0)
            densec.size(0)
            denseg.size(0)

            #if gid exist on rank
            if (pc.gid_exists(gid)):

                #get cell instance
                cell = pc.gid2cell(gid)
                isec = 0

                #soma section, only pne
                for sec in [cell.soma]:
                    for seg in sec:
                        #get section and segment index
                        somasec.append(isec)
                        somaseg.append(seg.node_index())

                        #vector for recording
                        v = h.Vector()
                        v.record(seg._ref_v)
                        v.label("soma %d %d" % (isec, seg.node_index()))
                        recordlist.append(v)
                isec += 1

                #for sections in dendrite
                for sec in cell.den:
                    for seg in sec:
                        densec.append(isec)
                        denseg.append(seg.node_index())

                        #for recordings
                        v = h.Vector()
                        v.record(seg._ref_v)
                        v.label("dend %d %d" % (isec, seg.node_index()))
                        recordlist.append(v)
                    isec += 1

        #register soma section list
        pc.nrnbbcore_register_mapping(gid, "soma", somasec, somaseg)

        #register dend section list
        pc.nrnbbcore_register_mapping(gid, "dend", densec, denseg)

    return recordlist
示例#23
0
文件: Simulation.py 项目: NBELab/RSME
    def __init__(self,
                 simulation_parameters,
                 probes="v",
                 synapse_dictionary_file=None,
                 print_mode=False):

        start = time.time()

        # Intiates a logger
        self.logger = simulation_parameters.logger
        self.results_dir = simulation_parameters.results_dir
        self.print_mode = simulation_parameters.print_mode

        # Intiates the main simulation dictionary using the parsed simulation parameters
        self.simulation = {
            'print_mode': simulation_parameters.print_mode,
            'XML_description': simulation_parameters.simulation_parameters_xml,
            'simdur': simulation_parameters.simdur,
            'description': simulation_parameters.description,
            'stimuli_params': simulation_parameters.stimuli_params,
            'stimuli': simulation_parameters.stimuli,
            'synapses': simulation_parameters.biophysics['synapses'],
            'projections': simulation_parameters.network['projections'],
            'cells': {},
            'pc': NEURON.ParallelContext(),
            'sim_dt': simulation_parameters.sim_dt,
            'light_dt': simulation_parameters.light_dt,
            # For GA optimization
            'scale_factor': simulation_parameters.scale_factor,
            'offset': simulation_parameters.offset,
            'transition_placement': simulation_parameters.transition_placement,
            'refiling_rate': simulation_parameters.refiling_rate,
            'release_probability': simulation_parameters.release_probability,
            'offset_dyn': simulation_parameters.offset_dyn,
            'syn_weight': simulation_parameters.syn_weight,
            'kinetic_change_end': simulation_parameters.kinetic_change_end
        }

        for cell_type in simulation_parameters.network['populations']:
            self.simulation['cells'][cell_type] = {
                'type':
                simulation_parameters.network['populations'][cell_type]['type']
            }

            # Handling grid arrangement layer
            if simulation_parameters.network['populations'][cell_type][
                    'type'] == 'grid_arrangement':
                self.simulation['cells'][cell_type]['architecture'] = (
                    simulation_parameters.network['populations'][cell_type]
                    ['architecture'].copy())

            self.simulation['cells'][cell_type]['instances'] = (
                simulation_parameters.network['populations'][cell_type]
                ['cells'].copy())
            self.simulation['cells'][cell_type]['morphology'] = (
                simulation_parameters.morphologies[cell_type].copy())
            self.simulation['cells'][cell_type]['biophysics'] = (
                simulation_parameters.biophysics['cells'][cell_type].copy())

        n_cells = 0
        for cell_type in self.simulation['cells']:
            n_cells += len(self.simulation['cells'][cell_type]['instances'])
        self.simulation['n'] = n_cells

        self.logger.info('Simulation was succefully parametrized')

        if self.print_mode:
            print(
                'Simulation was succefully parametrized with {} cells'.format(
                    n_cells))

        # Loads .hoc morphology and segmenting the results using the precompiled 'fixnseg.hoc' file
        load_morphology(NEURON, self.simulation, simulation_parameters.logger)

        # NEURON.topology()  # Verify morphology with an hirerchical section tree
        # NEURON.PlotShape() # Verify morphology with Neuron's plot

        # initialize the morphology sections_dict
        # section_dics holds the list of sctions, IDs mapping and branching distances
        make_section_map(NEURON, self.simulation, simulation_parameters.logger)

        # Distributes channels , as well as defines sections' properties (Ra, diameter)
        define_sections(self.simulation, simulation_parameters.logger)

        self.synapse_dict = {
            'projections':
            define_projection_synapses(self.simulation,
                                       simulation_parameters.logger),
            'light':
            define_light_synpases(self.simulation,
                                  simulation_parameters.logger),
            'intersynapses':
            define_inner_synapses(self.simulation,
                                  simulation_parameters.logger)
        }

        self.plotting_dict = initiate_plotting(self.simulation)

        assign_probes(self.simulation, probes, simulation_parameters.logger)

        end = time.time()
        elapsed = end - start

        self.logger.info(
            'Simulation was sucessfully intialized. Init time: {} sec'.format(
                elapsed))
示例#24
0
def spike_record():
    global tvec, idvec
    pc = h.ParallelContext()
    tvec = h.Vector(1000000)
    idvec = h.Vector(1000000)
    pc.spike_record(-1, tvec, idvec)
示例#25
0
    def __init__(self, params):
        self.pc = h.ParallelContext()
        self.d_rank = int(self.pc.id())
        self.d_size = int(self.pc.nhost())

        self.num_cells = params.num_cells
        self.min_delay = params.min_delay
        self.cell_params = params.cell
        self.ring_size = params.ring_size
        self.synapses_per_cell = params.cell.synapses
        self.cells = []

        # distribute gid in round robin
        self.gids = range(self.d_rank, self.num_cells, self.d_size)

        # generate the cells
        for gid in self.gids:
            c = cell.branchy_cell(gid, self.cell_params)

            self.cells.append(c)

            # register this gid
            self.pc.set_gid2node(gid, self.d_rank)

            # This is the neuronic way to register a cell in a distributed
            # context. The netcon isn't meant to be used, so we hope that the
            # garbage collection does its job.
            nc = h.NetCon(c.soma(0.5)._ref_v, None, sec=c.soma)
            nc.threshold = 10
            self.pc.cell(gid, nc)  # Associate the cell with this host and gid

        total_comp = 0
        total_seg = 0
        for c in self.cells:
            total_comp += c.ncomp
            total_seg += c.nseg

        if self.d_size > 1:
            from mpi4py import MPI
            total_comp = MPI.COMM_WORLD.reduce(total_comp, op=MPI.SUM, root=0)
            total_seg = MPI.COMM_WORLD.reduce(total_seg, op=MPI.SUM, root=0)

        if self.d_rank == 0:
            print(
                'cell stats: {} cells; {} segments; {} compartments; {} comp/cell.'
                .format(self.num_cells, total_seg, total_comp,
                        total_comp / self.num_cells))

        # Generate the connections.
        # For each local gid, make an incoming connection with source at gid-1.
        self.connections = []
        num_local_cells = len(self.gids)
        self.stims = []
        self.stim_connections = []
        for i in range(num_local_cells):
            gid = self.gids[i]

            # attach ring connection to previous gid in local ring
            s = self.ring_size
            ring = int(gid / s)
            ring_start = s * ring
            ring_end = min(ring_start + s, self.num_cells)
            src = gid - 1
            if gid == ring_start:
                src = ring_end - 1

            con = self.pc.gid_connect(src, self.cells[i].synapses[0])
            con.delay = self.min_delay
            con.weight[0] = 0.01
            self.connections.append(con)

            # Attach stimulus if cell is first in sub-ring
            if gid == ring_start:
                stim = h.NetStim()
                stim.number = 1  # one spike
                stim.start = 0  # at t=0
                stim_connection = h.NetCon(stim, self.cells[i].synapses[0])
                stim_connection.delay = 1
                stim_connection.weight[0] = 0.01
                self.stims.append(stim)
                self.stim_connections.append(stim_connection)

            # TODO: for loop that makes dummy connections
            for sid in range(1, self.synapses_per_cell):
                src = random.randint(0, self.num_cells - 2)
                if src == gid:
                    src = src + 1
                delay = params.min_delay + random.uniform(
                    0, 2 * params.min_delay)
                con = self.pc.gid_connect(src, self.cells[i].synapses[sid])
                con.weight[0] = 0
                con.delay = delay
                self.connections.append(con)
示例#26
0
def test_spikes(
    use_mpi4py=mpi4py_option,
    use_nrnmpi_init=nrnmpi_init_option,
    file_mode=file_mode_option,
):
    print(
        "test_spikes(use_mpi4py={}, use_nrnmpi_init={}, file_mode={})".format(
            use_mpi4py, use_nrnmpi_init, file_mode))
    h("""create soma""")
    h.soma.L = 5.6419
    h.soma.diam = 5.6419
    h.soma.insert("hh")
    h.soma.nseg = 3
    ic = h.IClamp(h.soma(0.25))
    ic.delay = 0.1
    ic.dur = 0.1
    ic.amp = 0.3

    ic2 = h.IClamp(h.soma(0.75))
    ic2.delay = 5.5
    ic2.dur = 1
    ic2.amp = 0.3

    h.tstop = 10
    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)

    pc = h.ParallelContext()

    pc.set_gid2node(pc.id() + 1, pc.id())
    myobj = h.NetCon(h.soma(0.5)._ref_v, None, sec=h.soma)
    pc.cell(pc.id() + 1, myobj)

    # NEURON run
    nrn_spike_t = h.Vector()
    nrn_spike_gids = h.Vector()

    # rank 0 record spikes for all gid while others
    # for specific gid. this is for better test coverage.
    pc.spike_record(-1 if pc.id() == 0 else (pc.id() + 1), nrn_spike_t,
                    nrn_spike_gids)

    h.run()

    nrn_spike_t = nrn_spike_t.to_python()
    nrn_spike_gids = nrn_spike_gids.to_python()

    # CORENEURON run
    from neuron import coreneuron

    coreneuron.enable = True
    coreneuron.gpu = enable_gpu
    coreneuron.file_mode = file_mode
    coreneuron.verbose = 0
    corenrn_all_spike_t = h.Vector()
    corenrn_all_spike_gids = h.Vector()

    pc.spike_record(-1, corenrn_all_spike_t, corenrn_all_spike_gids)

    pc.set_maxstep(10)

    def run(mode):
        h.stdinit()
        if mode == 0:
            pc.psolve(h.tstop)
        elif mode == 1:
            while h.t < h.tstop:
                pc.psolve(h.t + 1.0)
        else:
            while h.t < h.tstop:
                h.continuerun(h.t + 0.5)
                pc.psolve(h.t + 0.5)

        corenrn_all_spike_t_py = corenrn_all_spike_t.to_python()
        corenrn_all_spike_gids_py = corenrn_all_spike_gids.to_python()

        # check spikes match
        assert len(nrn_spike_t)  # check we've actually got spikes
        assert len(nrn_spike_t) == len(nrn_spike_gids)  # matching no. of gids
        if nrn_spike_t != corenrn_all_spike_t_py:
            print(mode)
            print(nrn_spike_t)
            print(nrn_spike_gids)
            print(corenrn_all_spike_t_py)
            print(corenrn_all_spike_gids_py)
            print([
                corenrn_all_spike_t[i] - nrn_spike_t[i]
                for i in range(len(nrn_spike_t))
            ])
        assert nrn_spike_t == corenrn_all_spike_t_py
        assert nrn_spike_gids == corenrn_all_spike_gids_py

    if file_mode is False:
        for mode in [0, 1, 2]:
            run(mode)
    else:
        run(0)

    return h
示例#27
0
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import numpy as np
from bmtk.simulator.bionet import utils, nrn
from bmtk.simulator.bionet.cell import Cell
import six

from neuron import h

pc = h.ParallelContext()  # object to access MPI methods


class BioCell(Cell):
    """Implemntation of a morphologically and biophysically detailed type cell.

    """
    def __init__(self, node, bionetwork):
        super(BioCell, self).__init__(node)

        # Set up netcon object that can be used to detect and communicate cell spikes.
        self.set_spike_detector(bionetwork.spike_threshold)

        self._morph = None
        self._seg_coords = {}
示例#28
0
def test_direct_memory_transfer():
    h("""create soma""")
    h.soma.L = 5.6419
    h.soma.diam = 5.6419
    h.soma.insert("hh")
    ic = h.IClamp(h.soma(0.5))
    ic.delay = 0.5
    ic.dur = 0.1
    ic.amp = 0.3

    # for testing external mod file
    h.soma.insert("Sample")

    h.cvode.use_fast_imem(1)
    h.cvode.cache_efficient(1)

    v = h.Vector()
    v.record(h.soma(0.5)._ref_v, sec=h.soma)
    i_mem = h.Vector()
    i_mem.record(h.soma(0.5)._ref_i_membrane_, sec=h.soma)
    tv = h.Vector()
    tv.record(h._ref_t, sec=h.soma)
    h.run()
    vstd = v.cl()
    tvstd = tv.cl()
    i_memstd = i_mem.cl()
    # Save current (after run) value to compare with transfer back from coreneuron
    tran_std = [h.t, h.soma(0.5).v, h.soma(0.5).hh.m]

    from neuron import coreneuron

    coreneuron.enable = True
    coreneuron.verbose = 0
    coreneuron.gpu = enable_gpu

    pc = h.ParallelContext()

    def run(mode):
        pc.set_maxstep(10)
        h.stdinit()
        if mode == 0:
            pc.psolve(h.tstop)
        elif mode == 1:
            while h.t < h.tstop:
                pc.psolve(h.t + 1.0)
        else:
            while h.t < h.tstop:
                h.continuerun(h.t + 0.5)
                pc.psolve(h.t + 0.5)
        tran = [h.t, h.soma(0.5).v, h.soma(0.5).hh.m]

        assert tv.eq(tvstd)
        assert (
            v.cl().sub(vstd).abs().max() < 1e-10
        )  # usually v == vstd, some compilers might give slightly different results
        assert i_mem.cl().sub(i_memstd).abs().max() < 1e-10
        assert h.Vector(tran_std).sub(h.Vector(tran)).abs().max() < 1e-10

    for mode in [0, 1, 2]:
        run(mode)

    cnargs = coreneuron.nrncore_arg(h.tstop)
    h.stdinit()
    assert tv.size() == 1 and tvstd.size() != 1
    pc.nrncore_run(cnargs, 1)
    assert tv.eq(tvstd)

    # print warning if HocEvent on event queue when CoreNEURON starts
    def test_hoc_event():
        print("in test_hoc_event() at t=%g" % h.t)
        if h.t < 1.001:
            h.CVode().event(h.t + 1.0, test_hoc_event)

    fi = h.FInitializeHandler(2, test_hoc_event)
    coreneuron.enable = False
    run(0)
    coreneuron.enable = True
    run(0)
示例#29
0
def main(config, template_path, output_path, forest_path, populations, io_size,
         chunk_size, value_chunk_size, cache_size, verbose):
    """

    :param config:
    :param template_path:
    :param forest_path:
    :param populations:
    :param io_size:
    :param chunk_size:
    :param value_chunk_size:
    :param cache_size:
    """

    utils.config_logging(verbose)
    logger = utils.get_script_logger(script_name)

    comm = MPI.COMM_WORLD
    rank = comm.rank

    env = Env(comm=MPI.COMM_WORLD,
              config_file=config,
              template_paths=template_path)
    h('objref nil, pc, templatePaths')
    h.load_file("nrngui.hoc")
    h.load_file("./templates/Value.hoc")
    h.xopen("./lib.hoc")
    h.pc = h.ParallelContext()

    if io_size == -1:
        io_size = comm.size
    if rank == 0:
        logger.info('%i ranks have been allocated' % comm.size)

    h.templatePaths = h.List()
    for path in env.templatePaths:
        h.templatePaths.append(h.Value(1, path))

    if output_path is None:
        output_path = forest_path

    if rank == 0:
        if not os.path.isfile(output_path):
            input_file = h5py.File(forest_path, 'r')
            output_file = h5py.File(output_path, 'w')
            input_file.copy('/H5Types', output_file)
            input_file.close()
            output_file.close()
    comm.barrier()

    (pop_ranges, _) = read_population_ranges(forest_path, comm=comm)
    start_time = time.time()
    for population in populations:
        logger.info('Rank %i population: %s' % (rank, population))
        count = 0
        (population_start, _) = pop_ranges[population]
        template_name = env.celltypes[population]['template']
        h.find_template(h.pc, h.templatePaths, template_name)
        template_class = eval('h.%s' % template_name)
        measures_dict = {}
        for gid, morph_dict in NeuroH5TreeGen(forest_path,
                                              population,
                                              io_size=io_size,
                                              comm=comm,
                                              topology=True):
            if gid is not None:
                logger.info('Rank %i gid: %i' % (rank, gid))
                cell = cells.make_neurotree_cell(template_class,
                                                 neurotree_dict=morph_dict,
                                                 gid=gid)
                secnodes_dict = morph_dict['section_topology']['nodes']

                apicalidx = set(cell.apicalidx)
                basalidx = set(cell.basalidx)

                dendrite_area_dict = {k + 1: 0.0 for k in range(0, 4)}
                dendrite_length_dict = {k + 1: 0.0 for k in range(0, 4)}
                for (i, sec) in enumerate(cell.sections):
                    if (i in apicalidx) or (i in basalidx):
                        secnodes = secnodes_dict[i]
                        prev_layer = None
                        for seg in sec.allseg():
                            L = seg.sec.L
                            nseg = seg.sec.nseg
                            seg_l = old_div(L, nseg)
                            seg_area = h.area(seg.x)
                            layer = cells.get_node_attribute(
                                'layer', morph_dict, seg.sec, secnodes, seg.x)
                            layer = layer if layer > 0 else (
                                prev_layer if prev_layer is not None else 1)
                            prev_layer = layer
                            dendrite_length_dict[layer] += seg_l
                            dendrite_area_dict[layer] += seg_area

                measures_dict[gid] = { 'dendrite_area': np.asarray([ dendrite_area_dict[k] for k in sorted(dendrite_area_dict.keys()) ], dtype=np.float32), \
                                       'dendrite_length': np.asarray([ dendrite_length_dict[k] for k in sorted(dendrite_length_dict.keys()) ], dtype=np.float32) }

                del cell
                count += 1
            else:
                logger.info('Rank %i gid is None' % rank)
        append_cell_attributes(output_path,
                               population,
                               measures_dict,
                               namespace='Tree Measurements',
                               comm=comm,
                               io_size=io_size,
                               chunk_size=chunk_size,
                               value_chunk_size=value_chunk_size,
                               cache_size=cache_size)
    MPI.Finalize()
示例#30
0
def test_py_alltoall_dict_err():
    pc = h.ParallelContext()
    src = {i: (100 + i) for i in range(2)}
    expect_hocerr(pc.py_alltoall, src, ("hocobj_call error",))