Exemplo n.º 1
0
def run_Ipulse_protocol(Model,
                        clamps=[{
                            'value': 0.,
                            'duration': 1000.
                        }],
                        with_plot=False,
                        ge=None):

    t, neuron, SEGMENTS = initialize_sim(Model)

    # recording and running
    M = ntwk.StateMonitor(neuron, ('v', 'I'), record=[0])

    t = 0
    for clamp in clamps:

        neuron.I[0] = clamp['value'] * ntwk.pA
        ntwk.run(clamp['duration'] * ntwk.ms)
        t += clamp['duration']

    output = {
        't': np.array(M.t / ntwk.ms),
        'Ic': np.array(np.array(M.I / ntwk.pA)[0, :]),
        'Vm_soma': np.array(M.v / ntwk.mV)[0, :]
    }

    return output
def adaptive_run(neuron, dt, tstop,
                 check_every=5, # ms
                 VCLIP=[-80, 0]):

    Vm, i, last_t = [], 0, 0
    while last_t<tstop:
        ntwk.run(0.2*ntwk.ms)
        neuron.v = np.clip(neuron.v/ntwk.mV, -80, 0)*ntwk.mV
        print(np.max(neuron.v/ntwk.mV), np.min(neuron.v/ntwk.mV))
        last_t = M.t[-1]/ntwk.ms
        print(M.t[-1]/ntwk.ms)
    pass
Exemplo n.º 3
0
def run_single_sim(Model, args):
    
    Model['VC-cmd'] = args.Vcmd
    Model['alphaZn'] = args.alphaZn

    Tstim = args.n_pulses/args.freq_pulses*1e3
    stim_events = args.Tdiscard0+np.arange(args.n_pulses)*1e3/args.freq_pulses

    Model['tstop'] = args.Tdiscard0+Tstim+args.Tend
    Model['dt'] = args.dt
    
    # initialize voltage-clamp sim
    t, neuron, SEGMENTS = initialize_sim(Model, method='voltage-clamp')
    
    np.random.seed(args.seed)
    
    synapses_loc = LOCs[args.syn_location]+np.arange(args.Nsyn)
    
    spike_IDs, spike_times = np.empty(0, dtype=int), np.empty(0, dtype=float)
    for ie, te in enumerate(stim_events):
        spike_times = np.concatenate([spike_times,
                                      te*np.ones(args.Nsyn)])
        spike_IDs = np.concatenate([spike_IDs, np.arange(args.Nsyn)])

    Estim, ES = ntwk.process_and_connect_event_stimulation(neuron,
                                                           spike_IDs, spike_times,
                                                           synapses_loc,
                                                           EXC_SYNAPSES_EQUATIONS.format(**Model),
                                                           ON_EXC_EVENT.format(**Model))

    # recording and running
    M = ntwk.StateMonitor(neuron, ('v'), record=[0, synapses_loc[0]])
    S = ntwk.StateMonitor(ES, ('X', 'gAMPA', 'gRiseNMDA', 'gDecayNMDA', 'bZn'), record=[0])

    # # Run simulation
    ntwk.run((args.Tdiscard0+Tstim+args.Tend)*ntwk.ms)

    output = {'t':np.array(M.t/ntwk.ms), 'Vcmd':args.Vcmd}
    output['synapses_loc'] = synapses_loc
    output['Vm_soma'] = np.array(M.v/ntwk.mV)[0,:]
    output['gAMPA_syn'] = np.array(S.gAMPA/ntwk.nS)[0,:]
    output['X_syn'] = np.array(S.X)[0,:]
    bZn, gRise, gDecay = np.array(S.bZn)[0,:], np.array(S.gRiseNMDA)[0,:], np.array(S.gDecayNMDA)[0,:]
    output['Vm_syn'] = np.array(M.v/ntwk.mV)[1,:]
    output['bZn_syn'] = bZn
    output['gNMDA_syn'] = Model['qNMDA']*Model['nNMDA']*(gDecay-gRise)/(1+0.3*np.exp(-output['Vm_syn']/Model['V0NMDA']))*(1.-Model['alphaZn']*bZn)
    output['Ic'] = (output['Vm_soma']-Model['VC-cmd'])*Model['VC-gclamp'] # nA

    output['args'] = dict(vars(args))
    
    return output
def run(neuron, Model, Estim, ES, Istim, IS):

    # recording and running
    M = ntwk.StateMonitor(neuron, ('v'), record=[0, # soma
                                                 Model['stim_apic_compartment_index']])
    S = ntwk.StateMonitor(ES, ('X', 'gAMPA', 'gNMDA', 'bZn'), record=[0])
    
    # # Run simulation
    ntwk.run(Model['tstop']*ntwk.ms)

    output = {'t':np.array(M.t/ntwk.ms),
              'Vm_soma':np.array(M.v/ntwk.mV)[0,:],
              'bZn_syn':np.array(S.bZn)[0,:],
              'gAMPA_syn':np.array(S.gAMPA/ntwk.nS)[0,:],
              'gNMDA_syn':np.array(S.gNMDA/ntwk.nS)[0,:],
              'X_syn':np.array(S.X)[0,:],
              'Vm_syn':np.array(M.v/ntwk.mV)[1,:],
              'Model':Model}

    output['Ic'] = (output['Vm_soma']-Model['VC-cmd'])*Model['VC-gclamp'] # nA
    return output
    # recording and running
    if active:
        M = ntwk.StateMonitor(neuron, ('v', 'InternalCalcium'), record=[0, synapses_loc[0]])
    else:
        M = ntwk.StateMonitor(neuron, ('v'), record=[0, synapses_loc[0]])
        S = ntwk.StateMonitor(ES, ('X', 'gAMPA', 'gE_post', 'bZn'), record=[0])

    # # Run simulation
    start = time.time()

    if active:
        ntwk.defaultclock.dt = 0.01*ntwk.ms
    else:
        ntwk.defaultclock.dt = 0.025*ntwk.ms
    
    ntwk.run(tstop*ntwk.ms)
    # if not active:
    #     ntwk.run(tstop*ntwk.ms)
    # else:
    #     adaptive_run(tstop, neuron, M)
        
    
    # ntwk.run(tstop*ntwk.ms)
    print('Runtime: %.2f s' % (time.time()-start))
    
    from datavyz import ges as ge
    fig, AX = ge.figure(axes=(1,2),figsize=(2,1))
    
    AX[0].plot(np.array(M.t/ntwk.ms), np.array(M.v/ntwk.mV)[0,:], label='soma')
    ge.plot(np.array(M.t/ntwk.ms), np.array(M.v/ntwk.mV)[1,:], label='dend', ax=AX[0])
    if active:
def run_single_trial(Model):
    
    np.random.seed(Model['seed']+Model['branch_index'])

    # computing Nsyn_array 
    if Model['ISIB_log_Nsyn']:
        Nsyn_array = np.logspace(np.log10(Model['ISIB_Nsyn1']), np.log10(Model['ISIB_Nsyn2']),
                                 Model['ISIB_Nsyn_N'], dtype=int)
    else:
        Nsyn_array = np.linspace(np.log10(Model['ISIB_Nsyn1']), np.log10(Model['ISIB_Nsyn2']),
                                 Model['ISIB_Nsyn_N'], dtype=int)
        

    # morphology analysis
    morpho = ntwk.Morphology.from_swc_file(Model['morpho_file'])
    SEGMENTS = ntwk.morpho_analysis.compute_segments(morpho)
    nseg = len(SEGMENTS['x'])
    segments_of_branches = compute_branches_for_stimuli(SEGMENTS)
    isegs_branch = segments_of_branches[Model['branch_index']]


    basal_cond = ntwk.morpho_analysis.find_conditions(SEGMENTS,
                                                      comp_type='dend',
                                                      min_distance_to_soma=20e-6)

    # spreading synapses for bg noise
    Nsyn_Glut, pre_to_iseg_Glut,\
        Nsyn_per_seg_Glut = ntwk.spread_synapses_on_morpho(SEGMENTS,
                                                           Model['DensityGlut_L23'],
                                                           cond=basal_cond,
                                                           density_factor=1./100./1e-12)
    

    # simulation
    Model['tstop'] = Model['ISIB_delay']+len(Nsyn_array)*Model['ISIB_window']
    
    t, neuron, SEGMENTS = initialize_sim(Model)
    
    if Model['Fexc_bg']>0:
        spike_IDs, spike_times = ntwk.spikes_from_time_varying_rate(t,
                                                                    0*t+Model['Fexc_bg'],
                                                                    N=len(isegs_branch),
                                                                    SEED=Model['seed']+1)
    else:
        spike_IDs, spike_times = np.empty(0, dtype=int), np.empty(0, dtype=float)

        
    for i, n in enumerate(Nsyn_array):

        nsyn = int(min([len(isegs_branch), n]))
        synapses_loc = np.random.choice(isegs_branch, nsyn, replace=False)
        spike_times = np.concatenate([spike_times, Model['ISIB_delay']+i*Model['ISIB_window']*np.ones(len(synapses_loc))])
        spike_IDs = np.concatenate([spike_IDs, np.arange(len(synapses_loc))])
        it = 0
        while nsyn<n:
            nsyn2 = int(min([len(isegs_branch), n-nsyn]))
            synapses_loc = np.random.choice(isegs_branch, nsyn2, replace=False)
            spike_times = np.concatenate([spike_times,
                                          Model['ISIB_delay']+i*Model['ISIB_window']*np.ones(len(synapses_loc))+(it+1)*Model['dt']])
            spike_IDs = np.concatenate([spike_IDs, np.arange(len(synapses_loc))])
            nsyn += nsyn2
            it+=1

    spike_IDs, spike_times = ntwk.deal_with_multiple_spikes_per_bin(spike_IDs, spike_times, t)
    Estim, ES = ntwk.process_and_connect_event_stimulation(neuron,
                                                           spike_IDs, spike_times,
                                                           isegs_branch,
                                                           EXC_SYNAPSES_EQUATIONS.format(**Model),
                                                           ON_EXC_EVENT.format(**Model))

    # recording and running
    M = ntwk.StateMonitor(neuron, ('v'), record=[0, synapses_loc[0]])
    
    # # Run simulation
    ntwk.run(Model['tstop']*ntwk.ms)

    return np.array(M.t/ntwk.ms), np.array(M.v/ntwk.mV)[0,:]
def run_single_sim(Model, stim, Vcmd=20, Npicked=100, seed=0):

    Model['VC-cmd'] = Vcmd
    Model['dt'] = 0.01

    # initialize voltage-clamp sim
    t, neuron, SEGMENTS = initialize_sim(Model,
                                         method='voltage-clamp',
                                         tstop=stim['t'][-1])

    # find synapses
    basal_cond = ntwk.morpho_analysis.find_conditions(
        SEGMENTS, comp_type='dend', min_distance_to_soma=20e-6)
    Nsyn, pre_to_iseg,\
        Nsyn_per_seg = ntwk.spread_synapses_on_morpho(SEGMENTS,
                                                      10, # density
                                                      cond=basal_cond,
                                                      density_factor=1./100./1e-12)

    if type(Npicked) is int:
        Npicked = Npicked * np.ones(14, dtype=int)

    np.random.seed(seed)

    synapses_loc = np.random.choice(pre_to_iseg, np.max(Npicked))

    spike_IDs, spike_times = np.empty(0, dtype=int), np.empty(0, dtype=float)
    for te, n in zip(stim['events'], Npicked):
        spike_times = np.concatenate([spike_times, te * np.ones(n)])
        spike_IDs = np.concatenate([spike_IDs, np.arange(n)])

    Estim, ES = ntwk.process_and_connect_event_stimulation(
        neuron, spike_IDs, spike_times, synapses_loc,
        EXC_SYNAPSES_EQUATIONS.format(**Model), ON_EXC_EVENT.format(**Model))

    # recording and running
    M = ntwk.StateMonitor(neuron, ('v'), record=[0, synapses_loc[0]])
    S = ntwk.StateMonitor(ES, ('X', 'gAMPA', 'gRiseNMDA', 'gDecayNMDA', 'bZn'),
                          record=[0])

    # # Run simulation
    print(Model['tstop'])
    ntwk.run(Model['tstop'] * ntwk.ms)

    output = {'t': np.array(M.t / ntwk.ms), 'Vcmd': Vcmd}
    output['synapses_loc'] = synapses_loc
    output['Npicked'] = Npicked
    output['Vm_soma'] = np.array(M.v / ntwk.mV)[0, :]
    output['gAMPA_syn'] = np.array(S.gAMPA / ntwk.nS)[0, :]
    output['X_syn'] = np.array(S.X)[0, :]
    bZn, gRise, gDecay = np.array(S.bZn)[0, :], np.array(
        S.gRiseNMDA)[0, :], np.array(S.gDecayNMDA)[0, :]
    output['Vm_syn'] = np.array(M.v / ntwk.mV)[1, :]
    output['bZn_syn'] = bZn
    output['gNMDA_syn'] = Model['qNMDA'] * Model['nNMDA'] * (
        gDecay -
        gRise) / (1 + 0.3 * np.exp(-output['Vm_syn'] / Model['V0NMDA'])) * (
            1. - Model['alphaZn'] * bZn)
    output['Ic'] = (output['Vm_soma'] -
                    Model['VC-cmd']) * Model['VC-gclamp']  # nA

    for key in stim:
        if key not in output:
            output[key] = stim[key]

    return output