Beispiel #1
0
def all(substring, start=0, end=-1):
    results = []
    mt = h.MechanismType(1)
    cnt = int(mt.count())
    end = cnt if end == -1 or end > cnt else end
    for i in range(start, end):
        if mt.is_netcon_target(i) and not mt.is_artificial(i):
            mt.select(i)
            n = h.ref("")
            mt.selected(n)
            mechname = n[0]
            if substring not in mechname:
                continue
            if uses_pointer(mechname):
                print("\n%d> %s has a POINTER" % (i, mechname))
                if 'ProbAMPANMDA_EMS' not in mechname:
                    continue
            print("\n%d of %d" % (i, cnt))
            print(mechname)
            for h.secondorder in [0, 2]:
                m, result = one(mechname)
                if cnt > 30:
                    m.g = None
                if result:
                    results.append((i, result, m.g))
                del m
    h.secondorder = 0
    return results
Beispiel #2
0
def get_mech_names():
    _mech_names = []
    mt = h.MechanismType(0)
    mname = h.ref("")
    for i in range(int(mt.count())):
        mt.select(i)
        mt.selected(mname)
        _mech_names.append(mname[0])
    return _mech_names
Beispiel #3
0
def remove_mechanisms(remove_list):
    """ Removes all active mechanisms in remove_list from cell model
    """
    mt = h.MechanismType(0)
    for sec in h.allsec():
        for seg in sec:
            for mech in remove_list:
                mt.select(mech)
                mt.remove()
def _is_loaded_mechanisms():
    # copied from:
    # https://www.neuron.yale.edu/neuron/static/py_doc/modelspec/programmatic/mechtype.html
    mt = h.MechanismType(0)
    mname = h.ref('')
    mnames = list()
    for i in range(mt.count()):
        mt.select(i)
        mt.selected(mname)
        mnames.append(mname[0])
    if 'hh2' not in mnames:
        return False
    else:
        return True
Beispiel #5
0
def mechVarList ():
    msname = h.ref('')
    varList = {}
    for i, mechtype in enumerate(['mechs','pointps']):
        mt = h.MechanismType(i)  # either distributed mechs (0) or point process (1)
        varList[mechtype] = {}
        for j in xrange(int(mt.count())):
            mt.select(j)
            mt.selected(msname)
            ms = h.MechanismStandard(msname[0], 1) # PARAMETER (modifiable)
            varList[mechtype][msname[0]] = []
            propName = h.ref('')
            for var in xrange(int(ms.count())):
                k = ms.name(propName, var)
                varList[mechtype][msname[0]].append(propName[0])
    return varList
Beispiel #6
0
def is_loaded_mechanisms():
    # copied from:
    # https://www.neuron.yale.edu/neuron/static/py_doc/modelspec/programmatic/mechtype.html
    mt = h.MechanismType(0)
    mname = h.ref('')
    mnames = list()
    for i in range(mt.count()):
        mt.select(i)
        mt.selected(mname)
        mnames.append(mname[0])

    # note this check only works for the original hnn model
    # need to generalize for any model

    if 'hh2' not in mnames:
        return False
    else:
        return True
Beispiel #7
0
def ptype():
    msname = h.ref('')
    propList = {}
    for i, mechtype in enumerate(['mechs', 'pointps']):
        mt = h.MechanismType(
            i)  # either distributed mechs (0) or point process (1)
        propList[mechtype] = {}
        for j in range(int(mt.count())):
            mt.select(j)
            mt.selected(msname)
            print('\n\n', msname[0], ' mechanismtype=%d' % j)
            pname(msname[0])
            ms = h.MechanismStandard(msname[0], 1)  # PARAMETER (modifiable)
            propList[mechtype][msname[0]] = []
            propName = h.ref('')
            for prop in range(int(ms.count())):
                k = ms.name(propName, prop)
                propList[mechtype][msname[0]].append(propName[0])
    print(propList)
Beispiel #8
0
    def find_point_process(self, sec):
        """Find a point_process in a section if any.
        
        Params:
            sec - Section to search
            
        Return:
            point_process or None if not present.
        """
        mt = h.MechanismType(1)
        total_mech = int(mt.count())
        sec.push()
        pp = None
        for i in range(total_mech):
            pp_type = mt.select(i)
            pp = mt.pp_begin()
            if pp is not None:
                break

        h.pop_section()
        return pp
Beispiel #9
0
def mechVarList():
    """
    Function for/to <short description of `netpyne.conversion.neuronPyHoc.mechVarList`>


    """

    msname = h.ref('')
    varList = {}
    for i, mechtype in enumerate(['mechs', 'pointps']):
        mt = h.MechanismType(
            i)  # either distributed mechs (0) or point process (1)
        varList[mechtype] = {}
        for j in range(int(mt.count())):
            mt.select(j)
            mt.selected(msname)
            ms = h.MechanismStandard(msname[0], 1)  # PARAMETER (modifiable)
            varList[mechtype][msname[0]] = []
            propName = h.ref('')
            for var in range(int(ms.count())):
                k = ms.name(propName, var)
                varList[mechtype][msname[0]].append(propName[0])
    return varList
Beispiel #10
0
def get_root(sec):
    return h.SectionRef(sec=sec).root().sec

root_sections = []
for sec in h.allsec():
    if not h.SectionRef(sec).has_parent():
        root_sections.append(sec)

# get all mech names
mech_names = []
h("""
objref mt
json_var = 0
strdef mname
""")
h.mt = h.MechanismType(0)
for i in xrange(int(h.mt.count())):
    h.mt.select(i)
    h('mt.selected(mname)')
    mech_names.append(h.mname)

# get all point process names
pointprocess_names = []
h.mt = h.MechanismType(1)
for i in xrange(int(h.mt.count())):
    h.mt.select(i)
    h('mt.selected(mname)')
    pointprocess_names.append(h.mname)

def pt_from_seg(seg):
    sec = seg.sec
Beispiel #11
0
from neuron import h
for j in range(2):
    mt = h.MechanismType(j)
    name = h.ref("")
    for i in range(mt.count()):
        mt.select(i)
        mt.selected(name)
        a = mt.code().split(sep='\n')
        print(("%s : %s" % (name[0], a[0])))
Beispiel #12
0
def psection(sec):
    from neuron import h, _has_rxd
    try:
        if _has_rxd['crxd']:
            from neuron import crxd as rxd
            from neuron.crxd import region, species
            from neuron.crxd import rxd as rxd_module

        else:
            from neuron import rxd
            from neuron.rxd import region, species
            from neuron.rxd import rxd as rxd_module
        have_rxd = True
    except:
        have_rxd = False

    results = {}

    mname = h.ref('')

    # point processes
    pps = {}
    mt = h.MechanismType(1)
    for i in range(int(mt.count())):
        mt.select(i)
        mypps = set()
        pp = mt.pp_begin(sec=sec)
        while pp is not None:
            mypps.add(pp)
            pp = mt.pp_next()
        if mypps:
            mt.selected(mname)
            pps[mname[0]] = mypps
    results['point_processes'] = pps

    center_seg_dir = dir(sec(0.5))

    mechs_present = []

    # membrane mechanisms
    mt = h.MechanismType(0)

    for i in range(int(mt.count())):
        mt.select(i)
        mt.selected(mname)
        name = mname[0]
        if name in center_seg_dir:
            mechs_present.append(name)

    results['density_mechs'] = {}
    results['ions'] = {}

    for mech in mechs_present:
        my_results = {}
        ms = h.MechanismStandard(mech, 0)
        for j in range(int(ms.count())):
            n = int(ms.name(mname, j))
            name = mname[0]
            pvals = []
            # TODO: technically this is assuming everything that ends with _ion
            #       is an ion. Check this.
            if mech.endswith('_ion'):
                pvals = [getattr(seg, name) for seg in sec]
            else:
                mechname = name  #+ '_' + mech
                for seg in sec:
                    if n > 1:
                        pvals.append(
                            [getattr(seg, mechname)[i] for i in range(n)])
                    else:
                        pvals.append(getattr(seg, mechname))
            my_results[name[:-(len(mech) +
                               1)] if name.endswith('_' +
                                                    mech) else name] = pvals
        # TODO: should be a better way of testing if an ion
        if mech.endswith('_ion'):
            results['ions'][mech[:-4]] = my_results
        else:
            results['density_mechs'][mech] = my_results

    morphology = {
        'L':
        sec.L,
        'diam': [seg.diam for seg in sec],
        'pts3d': [(sec.x3d(i), sec.y3d(i), sec.z3d(i), sec.diam3d(i))
                  for i in range(sec.n3d())],
        'parent':
        sec.parentseg(),
        'trueparent':
        sec.trueparentseg()
    }

    results['morphology'] = morphology
    results['nseg'] = sec.nseg
    results['Ra'] = sec.Ra
    results['cm'] = [seg.cm for seg in sec]

    if have_rxd:
        regions = {
            r()
            for r in region._all_regions if r() is not None and sec in r().secs
        }
        results['regions'] = regions

        my_species = []
        for sp in species._all_species:
            sp = sp()
            if sp is not None:
                sp_regions = sp._regions
                if not hasattr(sp_regions, '__len__'):
                    sp_regions = [sp_regions]
                if any(r in sp_regions for r in regions):
                    my_species.append(sp)
        results['species'] = set(my_species)
        results['name'] = sec.hname()
        results['hoc_internal_name'] = sec.hoc_internal_name()
        results['cell'] = sec.cell()

        #all_active_reactions = [r() for r in rxd_module._all_reactions if r() is not None]

    return results
Beispiel #13
0
def run_simulation(params):
    """
    Function get parameters and run the model
    """
    pc = h.ParallelContext()

    pc.timeout(3600)
    h.load_file("stdgui.hoc")
    h.load_file("stdrun.hoc")
    h.load_file("import3d.hoc")

    RNG = np.random.default_rng()

    load_mechanisms("./mods/")
    # h.dt = 0.1 * ms
    h.cvode.use_fast_imem(1)
    # h.CVode().fixed_step(1)
    # h.cvode.use_local_dt(1)

    sys.path.append("../LFPsimpy/")  # path to LFPsimpy
    from LFPsimpy import LfpElectrode

    # load all cells
    cell_path = "./cells/"
    for file in os.listdir(cell_path):
        if file.find(".hoc") != -1:
            h.load_file(cell_path + file)

    gid_vect = np.asarray(
        [neuron_param["gid"] for neuron_param in params["neurons"]])

    all_cells = h.List()
    hh_cells = h.List()

    pyramidal_sec_list = h.SectionList()
    is_pyrs_thread = False
    radius_for_pyramids = params["common_params"]["radius4piramids"]

    spike_count_obj = []
    spike_times_vecs = []

    soma_v_vecs = []

    # create objects for neurons simulation
    for neuron_param in params["neurons"]:

        celltypename = neuron_param["celltype"]
        cellclass_name = neuron_param["cellclass"]
        gid = neuron_param["gid"]

        cellclass = getattr(h, cellclass_name)

        cell = cellclass(gid, 0)

        pc.set_gid2node(gid, pc.id())

        if celltypename == "pyr":
            # x and y position of pyramidal cells

            angle = 2 * np.pi * (RNG.random() - 0.5)
            radius = radius_for_pyramids * 2 * (RNG.random() - 0.5)
            pyr_coord_in_layer_x = radius * np.cos(angle)
            pyr_coord_in_layer_y = radius * np.sin(angle)

            cell.position(pyr_coord_in_layer_x, 0, pyr_coord_in_layer_y)

            for sec in cell.all:
                pyramidal_sec_list.append(sec)
            is_pyrs_thread = True

        # set counters for spike generation
        if cell.is_art() == 0:
            for sec in cell.all:
                sec.insert("IextNoise")
                sec.myseed_IextNoise = RNG.integers(0, 1000000000000000, 1)
                sec.sigma_IextNoise = 0.005
                sec.mean_IextNoise = neuron_param["cellparams"]["iext"]

            if hasattr(cell, "axon_list"):
                mt = h.MechanismType(0)
                mt.select('IextNoise')
                for sec in cell.axon_list:
                    mt.remove(sec=sec)

            firing = h.NetCon(cell.soma[0](0.5)._ref_v, None, sec=cell.soma[0])
            firing.threshold = -30 * mV

        else:
            cell.celltype = celltypename

            for p_name, p_val in neuron_param["cellparams"].items():
                if hasattr(cell.acell, p_name):
                    setattr(cell.acell, p_name, p_val)

            setattr(cell.acell, "delta_t", h.dt)

            setattr(cell.acell, "myseed", RNG.integers(0, 1000000000000000, 1))

            firing = h.NetCon(cell.acell, None)

        pc.cell(gid, firing)
        fring_vector = h.Vector()
        firing.record(fring_vector)
        spike_count_obj.append(firing)
        spike_times_vecs.append(fring_vector)

        # check is need to save Vm of soma
        if np.sum(params["save_soma_v"] == gid) == 1:
            soma_v = h.Vector()
            soma_v.record(cell.soma[0](0.5)._ref_v)
            soma_v_vecs.append(soma_v)
        else:
            soma_v_vecs.append(np.empty(shape=0))

        if cell.is_art() == 0:
            hh_cells.append(cell)

        all_cells.append(cell)

    pc.barrier()
    if pc.id() == 0:
        print("End of neurons settings")

    # set connection
    connections = h.List()
    synapses = h.List()

    for syn_params in params["synapses_params"]:
        post_idx = np.argwhere(gid_vect == syn_params["post_gid"]).ravel()
        post_idx = post_idx[0]

        postsynaptic_cell = all_cells[post_idx]

        post_list = getattr(postsynaptic_cell,
                            syn_params["target_compartment"])
        len_postlist = sum([1 for _ in post_list])

        if len_postlist == 1:
            post_idx = 0
        else:
            post_idx = RNG.integers(0, len_postlist - 1)

        for idx_tmp, post_comp_tmp in enumerate(post_list):
            if idx_tmp == post_idx: post_comp = post_comp_tmp

        syn = h.Exp2Syn(post_comp(0.5))
        syn.e = syn_params["Erev"]
        syn.tau1 = syn_params["tau_rise"]
        syn.tau2 = syn_params["tau_decay"]

        conn = pc.gid_connect(syn_params["pre_gid"], syn)
        conn.delay = syn_params["delay"]
        conn.weight[0] = syn_params["gmax"]
        conn.threshold = -30 * mV

        connections.append(conn)
        synapses.append(syn)

        try:
            # check NMDA in synapse
            gmax_nmda = syn_params["NMDA"]["gNMDAmax"]
            syn_nmda = h.NMDA(post_comp(0.5), sec=post_comp)
            syn_nmda.tcon = syn_params["NMDA"]["tcon"]
            syn_nmda.tcoff = syn_params["NMDA"]["tcoff"]
            syn_nmda.gNMDAmax = gmax_nmda

            conn2 = pc.gid_connect(syn_params["pre_gid"], syn_nmda)
            conn2.delay = syn_params["delay"]
            conn2.weight[0] = syn_params["NMDA"]["gNMDAmax"]
            conn2.threshold = -30 * mV

            connections.append(conn2)
            synapses.append(syn_nmda)

        except KeyError:
            pass

    pc.barrier()

    # create gap junction objects
    gap_junctions = h.List()

    for gap_params in params["gap_junctions"]:

        idx1 = np.argwhere(gid_vect == gap_params["gid1"]).ravel()
        idx2 = np.argwhere(gid_vect == gap_params["gid2"]).ravel()

        if idx1.size != 0 and idx2.size != 0:

            idx1 = idx1[0]
            idx2 = idx2[0]

            cell1 = all_cells[idx1]
            cell2 = all_cells[idx2]

            comp1_list = getattr(cell1, gap_params["compartment1"])
            len_list = sum([1 for _ in comp1_list])

            if len_list == 1:
                idx1 = 0
            else:
                idx1 = RNG.integers(0, len_list - 1)

            for idx_tmp, comp_tmp in enumerate(comp1_list):
                if idx_tmp == idx1: comp1 = comp_tmp

            comp2_list = getattr(cell2, gap_params["compartment2"])
            len_list = sum([1 for _ in comp2_list])

            if len_list == 1:
                idx2 = 0
            else:
                idx2 = RNG.integers(0, len_list - 1)

            for idx_tmp, comp_tmp in enumerate(comp2_list):
                if idx_tmp == idx2: comp2 = comp_tmp

            pc.source_var(comp1(0.5)._ref_v, gap_params["sgid_gap"],
                          sec=comp1)  # gap_params["gid1"]

            gap = h.GAP(comp1(0.5), sec=comp1)
            gap.r = gap_params["r"]
            pc.target_var(gap._ref_vgap,
                          gap_params["sgid_gap"] + 1)  # gap_params["gid2"]

            gap_junctions.append(gap)

            pc.source_var(comp2(0.5)._ref_v,
                          gap_params["sgid_gap"] + 1,
                          sec=comp2)  # gap_params["gid2"]
            gap = h.GAP(comp2(0.5), sec=comp2)
            gap.r = gap_params["r"]
            pc.target_var(gap._ref_vgap,
                          gap_params["sgid_gap"])  # gap_params["gid1"]
            gap_junctions.append(gap)

        elif idx1.size != 0 or idx2.size != 0:

            if idx1.size != 0 and idx2.size != 0: continue

            if idx1.size != 0:
                this_idx = idx1[0]
                this_gid = gap_params["gid1"]
                out_gid = gap_params["gid2"]
                comp_name = gap_params["compartment1"]

                sgid_gap_src = gap_params["sgid_gap"]
                sgid_gap_trg = gap_params["sgid_gap"] + 1
            else:
                this_idx = idx2[0]
                this_gid = gap_params["gid2"]
                out_gid = gap_params["gid1"]
                comp_name = gap_params["compartment2"]

                sgid_gap_src = gap_params["sgid_gap"] + 1
                sgid_gap_trg = gap_params["sgid_gap"]

            cell = all_cells[this_idx]
            comp_list = getattr(cell, comp_name)

            for idx_tmp, comp_tmp in enumerate(comp_list):
                if idx_tmp == 0: comp = comp_tmp

            pc.source_var(comp(0.5)._ref_v, sgid_gap_src, sec=comp)

            gap = h.GAP(0.5, sec=comp)
            gap.r = gap_params["r"]

            pc.target_var(gap._ref_vgap, sgid_gap_trg)

    pc.setup_transfer()
    pc.barrier()
    if pc.id() == 0:
        print("End of connection settings")

    # create electrodes objects for LFP simulation
    el_x = params["elecs"]["el_x"]
    el_y = params["elecs"]["el_y"]
    el_z = params["elecs"]["el_z"]

    electrodes = []

    for idx_el in range(el_x.size):
        if is_pyrs_thread:
            le = LfpElectrode(x=el_x[idx_el], y=el_y[idx_el], z=el_z[idx_el], sampling_period=h.dt, \
                              method='Line', sec_list=pyramidal_sec_list)
            electrodes.append(le)
        else:
            electrodes.append(None)

    if pc.id() == 0:
        t_sim = h.Vector()
        t_sim.record(h._ref_t)
    else:
        t_sim = None

    h.tstop = params["duration"] * ms

    pc.set_maxstep(5 * ms)

    h.finitialize()
    pc.barrier()
    if pc.id() == 0:
        print("Start simulation")

    timer = time()

    pc.psolve(params["duration"] * ms)
    pc.barrier()
    if pc.id() == 0:
        print("End of the simulation!")
        print("Time of simulation in sec ", time() - timer)

    # unite data from all threads to 0 thread
    comm = MPI.COMM_WORLD
    lfp_data = join_lfp(comm, electrodes)
    spike_trains = join_vect_lists(comm, spike_times_vecs, gid_vect)
    soma_v_list = join_vect_lists(comm, soma_v_vecs, gid_vect)

    if (pc.id() == 0) and (params["file_results"] != None):

        t_sim = np.asarray(t_sim)
        rem_time = params["del_start_time"]
        if rem_time > t_sim[-1]:
            rem_time = 0
        rem_idx = int(rem_time / h.dt)

        # save results to file
        with h5py.File(params["file_results"], 'w') as h5file:
            celltypes = params["cell_types_in_model"]
            t_sim = t_sim[rem_idx:] - rem_time

            h5file.create_dataset("time", data=t_sim)

            # save LFP data
            extracellular_group = h5file.create_group("extracellular")
            ele_group = extracellular_group.create_group('electrode_1')
            lfp_group = ele_group.create_group('lfp')

            lfp_group_origin = lfp_group.create_group('origin_data')
            lfp_group_origin.attrs['SamplingRate'] = 1000 / h.dt  # dt in ms

            for idx_el, lfp in enumerate(lfp_data):
                lfp_group_origin.create_dataset("channel_" + str(idx_el + 1),
                                                data=lfp[rem_idx:])

            # save firing data
            firing_group = h5file.create_group(
                "extracellular/electrode_1/firing/origin_data")

            for celltype in set(celltypes):
                cell_friring_group = firing_group.create_group(celltype)

                for cell_idx, sp_times in enumerate(spike_trains):
                    if celltype != celltypes[cell_idx]:
                        continue
                    sp_times = sp_times[sp_times >= rem_time] - rem_time
                    cell_friring_group.create_dataset(
                        "neuron_" + str(cell_idx + 1),
                        data=sp_times)  # cell_spikes_dataset

            # save intracellular membrane potential
            intracellular_group = h5file.create_group("intracellular")
            intracellular_group_origin = intracellular_group.create_group(
                "origin_data")

            for soma_v_idx in params["save_soma_v"]:
                soma_v = soma_v_list[soma_v_idx]
                if soma_v.size == 0: continue
                soma_v_dataset = intracellular_group_origin.create_dataset(
                    "neuron_" + str(soma_v_idx + 1), data=soma_v[rem_idx:])
                cell_type = celltypes[soma_v_idx]
                soma_v_dataset.attrs["celltype"] = cell_type

    pc.done()
    h.quit()

    return