Exemplo n.º 1
0
def run(Vm, Rm, rates_exc, duration=300, interval=0, configFile='cv.xml'):
    current = [[0.5,1,0,0,0,0,0,0,0,0,0,1],
               [0.01,1,-300,0,0,0,0,0,0,0,0,1],
               [0.5,1,0,0,0,0,0,0,0,0,0,1],
               [0.6,1,-100,0,0,0,0,0,0,0,0,1],
               [1,1,0,0,0,0,0,0,0,0,0,1],
               [0,1,0,0,0,0,0,0,0,0,0,1]]
    preamble = np.sum(current,0)[0]
    current[-1][0] = duration-preamble
    writeStimFile('current.stim',current)
    conductance = [[preamble,1,0,0,0,0,0,0,0,0,0,1],
                   [duration-preamble,2,0,0,0,0,0,1,0,0,0,1]]
    taus = {'exc': 5, 'inh': 10}
    for rate in rates_exc:
        for V in Vm:
            ratio = lcg.computeRatesRatio(Vm=V, Rin=Rm)
            Gm_exc,Gm_inh,Gs_exc,Gs_inh = lcg.computeSynapticBackgroundCoefficients(ratio, rate, Rin=Rm)
            print('Vm = %g, rates = %g Hz (exc) %g Hz (inh).' % (V, rate, rate/ratio))
            conductance[1][2] = Gm_exc
            conductance[1][3] = Gs_exc
            conductance[1][4] = taus['exc']
            writeStimFile('gexc.stim', conductance)
            conductance[1][2] = Gm_inh
            conductance[1][3] = Gs_inh
            conductance[1][4] = taus['inh']
            writeStimFile('ginh.stim', conductance)
            sub.call(lcg.common.prog_name + ' -V 3 -c ' + configFile, shell=True)
            sub.call('sleep ' + str(interval), shell=True)
Exemplo n.º 2
0
def frequency_error(Vbal, target, Rm, R_inh, ai=0, ao=0, duration=10, interval=1, sampling_rate=20000):
    ratio = lcg.computeRatesRatio(Vm=Vbal, Rin=Rm)
    R_exc = R_inh * ratio[0]
    G0_exc,G0_inh,sigma_exc,sigma_inh = lcg.computeSynapticBackgroundCoefficients(ratio[0], R_exc, Rin=Rm)
    lcg.writeSpontaneousConfig(0, G0_exc, sigma_exc, G0_inh, sigma_inh, ai, ao, duration, sampling_rate, outfile='spontaneous.xml')
    if interval > 0:
        sub.call(['sleep', str(interval)])
    sub.call(lcg.common.prog_name + ' -c spontaneous.xml -V 4', shell=True)
    files = glob.glob('*.h5')
    files.sort()
    files = files[-1]
    entities,info = lcg.loadH5Trace(files)
    for ntt in entities:
        if ntt['name'] == 'RealNeuron':
            V = ntt['data']
            break
    if max(V) < -40:    # no spike in the presynaptic
        print('Balanced voltage: %.2f mV.' % Vbal)
        print('Spontaneous firing frequency: 0 Hz (0 spikes).')
        print('Error = %g Hz^2.' % target**2)
        return target**2
    t = np.arange(0, len(V)) * info['dt']
    spks = lcg.findSpikes(t,V,-20)
    freq = float(len(spks)) / duration
    print('Balanced voltage: %.2f mV.' % Vbal)
    print('Spontaneous firing frequency: %.3f Hz (%d spikes).' % (freq, len(spks)))
    print('Error = %g Hz^2.' % (freq-target)**2)
    return (freq - target)**2
Exemplo n.º 3
0
def main():
    mode = None

    if len(sys.argv) < 2 or sys.argv[1] in ('-h','--help','help'):
        usage()
        sys.exit(0)

    mode = sys.argv[1]
    if mode != 'current' and mode != 'conductance':
        print('Unknown working mode: [%s].' % sys.argv[1])
        sys.exit(1)

    opts = parseGlobalArgs()

    if mode == 'current':
        opts = dict(parseCurrentModeArgs(), **opts)
        tot = len(opts['correlation_coefficients']) * opts['reps']
        stim = [[opts['before'], 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [opts['duration'], -2, opts['current_mean'], 0, opts['current_tau'], 0, 0, 1, 0, 2, 0, 1], # indipendent part
                [0, -2, 0, 0, opts['current_tau'], 0, 0, 1, 0, 2, 1, 1], # common part
                [opts['after'], 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
        I = {'m': 0, 's': 0, 'mc': 0, 'sc': 0}
        np.random.seed(5061983)
        I_seeds = map(int, np.random.uniform(low=0, high=10000, size=opts['reps']))
    else:
        opts = dict(parseConductanceModeArgs(), **opts)
        writeConfigFile(opts)
        tot = len(opts['balanced_voltages']) * len(opts['correlation_coefficients']) * opts['reps']
        stim = [[opts['before'], 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [opts['duration'], 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
                [opts['after'], 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
        gampa = {'m': 0, 's': 0, 'mc': 0, 'sc': 0}
        ggaba = {'m': 0, 's': 0, 'mc': 0, 'sc': 0}
        np.random.seed(5061983)
        ampa_seeds = map(int, np.random.uniform(low=0, high=10000, size=opts['reps']))
        np.random.seed(7051983)
        gaba_seeds = map(int, np.random.uniform(low=0, high=10000, size=opts['reps']))
        if opts['with_nmda']:
            np.random.seed(723587)
            nmda_seeds = map(int, np.random.uniform(low=0, high=10000, size=opts['reps']))

    np.random.seed(int(time.time()))
    np.random.shuffle(opts['correlation_coefficients'])

    cnt = 0
    for c in opts['correlation_coefficients']:
        if mode == 'current':
            stim[1][3] = np.sqrt(1-c)*opts['current_std']
            stim[2][3] = np.sqrt(c)*opts['current_std']
            for k in range(opts['reps']):
                if cnt%opts['kernel_frequency'] == 0:
                    sub.call('lcg kernel -I ' + str(opts['ai']) + ' -O ' + str(opts['ao']), shell=True)
                stim[1][8] = int(np.random.uniform(low=0, high=100*opts['reps']))
                stim[2][8] = I_seeds[k]
                lcg.writeStimFile(stim_files['current'], stim, False)
                sub.call('lcg vcclamp -V 4 -f ' + stim_files['current'], shell=True)
                sub.call(['sleep', str(opts['interval'])])
                cnt = cnt+1
                if cnt%10 == 0:
                    print('[%02d/%02d]' % (cnt,tot))
        else:
            np.random.shuffle(opts['balanced_voltages'])
            for v in opts['balanced_voltages']:
                ratio = lcg.computeRatesRatio(Vm=v, Rin=opts['input_resistance'])
                gampa['m'],ggaba['m'],gampa['s'],ggaba['s'] = lcg.computeSynapticBackgroundCoefficients(
                    ratio,R_exc=ratio*(1-c)*opts['R_inh'],Rin=opts['input_resistance'])
                gampa['mc'],ggaba['mc'],gampa['sc'],ggaba['sc'] = lcg.computeSynapticBackgroundCoefficients(
                    ratio,R_exc=ratio*c*opts['R_inh'],Rin=opts['input_resistance'])
                for k in range(opts['reps']):
                    current_ampa_seed = int(np.random.uniform(low=0, high=100*opts['reps']))
                    stim[1][2] = gampa['m']
                    stim[1][3] = gampa['s']
                    stim[1][4] = 5
                    stim[1][8] = current_ampa_seed
                    lcg.writeStimFile(stim_files['gampa'], stim, False)
                    stim[1][2] = ggaba['m']
                    stim[1][3] = ggaba['s']
                    stim[1][4] = 10
                    stim[1][8] = int(np.random.uniform(low=0, high=100*opts['reps']))
                    lcg.writeStimFile(stim_files['ggaba'], stim, False)
                    stim[1][2] = gampa['mc']
                    stim[1][3] = gampa['sc']
                    stim[1][4] = 5
                    stim[1][8] = ampa_seeds[k]
                    lcg.writeStimFile(stim_files['gampa_common'], stim, False)
                    stim[1][2] = ggaba['mc']
                    stim[1][3] = ggaba['sc']
                    stim[1][4] = 10
                    stim[1][8] = gaba_seeds[k]
                    lcg.writeStimFile(stim_files['ggaba_common'], stim, False)
                    if opts['with_nmda']:
                        stim[1][2] = gampa['m']
                        stim[1][3] = gampa['s']
                        stim[1][4] = opts['nmda_tau']
                        stim[1][8] = current_ampa_seed
                        lcg.writeStimFile(stim_files['gnmda'], stim, False)
                        stim[1][2] = gampa['m']
                        stim[1][3] = gampa['s']
                        stim[1][4] = opts['nmda_tau']
                        stim[1][8] = ampa_seeds[k]
                        lcg.writeStimFile(stim_files['gnmda_common'], stim, False)
                    if cnt%opts['kernel_frequency'] == 0:
                        sub.call('lcg kernel -I ' + str(opts['ai']) + ' -O ' + str(opts['ao']), shell=True)
                    cnt = cnt+1
                    sub.call(lcg.common.prog_name + ' -V 4 -c ' + config_file, shell=True)
                    sub.call(['sleep', str(opts['interval'])])
                    if cnt%10 == 0:
                        print('[%02d/%02d]' % (cnt,tot))
Exemplo n.º 4
0
            options['gp'] = float(a)
        elif o == '-i':
            options['gi'] = float(a)
        elif o == '-d':
            options['gd'] = float(a)
            
    if options['target'] is None:
        print('You must specify specify the target frequency (-f switch).')
        sys.exit(1)
        
    if options['input_resistance'] is not None or options['R_exc'] is not None or options['balanced_voltage'] is not None:
        if options['input_resistance'] is None or options['R_exc'] is None or options['balanced_voltage'] is None:
            print('To inject a noisy background input, you must specify:')
            print('  1. The input resistance of the cell (-R switch).')
            print('  2. The firing frequency of the background excitatory population (-F switch).')
            print('  3. The balanced voltage (-v switch).')
            sys.exit(1)
        ratio = lcg.computeRatesRatio(options['balanced_voltage'], options['input_resistance'])
        options['Gm_exc'],options['Gm_inh'],options['Gs_exc'],options['Gs_inh'] = \
                       lcg.computeSynapticBackgroundCoefficients(ratio, options['R_exc'], options['input_resistance'])

    writeFiles(options)

    sub.call('lcg kernel -I ' + str(options['ai']) + ' -O ' + str(options['ao']), shell=True)
    sub.call(lcg.common.prog_name + ' -c ' + config_file + ' -n ' + str(nreps) + ' -i ' + str(interval), shell=True)

if __name__ == '__main__':
    main()


Exemplo n.º 5
0
            with_preamble = True
        elif o == '--no-shuffle':
            shuffle = False
        elif o == '--no-kernel':
            kernel = False

    if input_resistance is None:
        print('You must specify the input resistance of the cell (-R switch).')
        sys.exit(1)

    if R_exc is None and R_inh is None:
        print('You must specify at least one between excitatory or inhibitory presynaptic rate (--R-exc or --R-inh switches).')
        sys.exit(1)
        
    if R_inh is None and not V_bal is None:
        ratio = lcg.computeRatesRatio(Vm=V_bal, Rin=input_resistance)
        R_inh = R_exc / ratio
    elif R_exc is None and not V_bal is None:
        ratio = lcg.computeRatesRatio(Vm=V_bal, Rin=input_resistance)
        R_exc = R_inh * ratio
    elif R_exc is None or R_inh is None:
        print('You must specify the balanced voltage (--V-bal switch).')
        sys.exit(1)

    if duration is None:
        print('You must specify the duration of the stimulation (-d switch).')
        sys.exit(1)

    if len(stim_ampl) == 1:
        stim_ampl.append(stim_ampl[0])
        stim_ampl.append(1)
Exemplo n.º 6
0
                'AO_PRE': str(ao[0]),
                'AO_POST': str(ao[1]),
                'KERNEL_PRE':
                'kernel-' + str(ai[0]) + '-' + str(ao[0]) + '.dat',
                'KERNEL_POST':
                'kernel-' + str(ai[1]) + '-' + str(ao[1]) + '.dat',
                'TEND': str(stim_dur)
            })
    else:
        os.remove('pulses.stim')
        print('Default configuration file [%s] missing.' % template_file)
        usage()
        sys.exit(1)

    if with_bg:
        ratio = lcg.computeRatesRatio(Vm=balanced_voltage,
                                      Rin=input_resistance)
        Gm_exc, Gm_inh, Gs_exc, Gs_inh = lcg.computeSynapticBackgroundCoefficients(
            ratio, R_exc=bg_freq, Rin=input_resistance)
        lcg.writeGStimFiles(
            {
                'm': Gm_exc,
                's': Gs_exc,
                'tau': 5,
                'seed': 5061983
            }, {
                'm': Gm_inh,
                's': Gs_inh,
                'tau': 10,
                'seed': 7051983
            }, stim_dur, 0, 0)
Exemplo n.º 7
0
def main():
    mode = None

    if len(sys.argv) < 2 or sys.argv[1] in ('-h','--help','help'):
        usage()
        sys.exit(0)

    mode = sys.argv[1]
    if mode != 'current' and mode != 'conductance':
        print('Unknown working mode: [%s].' % sys.argv[1])
        sys.exit(1)

    opts = parseGlobalArgs()

    if mode == 'current':
        opts = dict(parseCurrentModeArgs(), **opts)
        if opts['separate'] and (type(opts['ao']) == int or type(opts['ai']) == int or \
                                     len(opts['ao']) == 1 or len(opts['ai']) == 1):
            print('When using the --separate options, two channels must be specified (use the -I and -O options).')
            sys.exit(1)
    else:
        opts = dict(parseConductanceModeArgs(), **opts)

    if (mode == 'current' and opts['with_bg']) or mode == 'conductance':
        ratio = lcg.computeRatesRatio(Vm=opts['balanced_voltage'], Rin=opts['input_resistance'])
        Gm_exc,Gm_inh,Gs_exc,Gs_inh = lcg.computeSynapticBackgroundCoefficients(ratio, R_exc=opts['R_exc'], Rin=opts['input_resistance'])
        opts['Gm_exc'] = Gm_exc
        opts['Gm_inh'] = Gm_inh
    else:
        # no background conductances, it will be a current-clamp experiment
        gexc = None
        ginh = None

    if mode == 'current':
        if opts['with_bg']:
            # background conductances only
            gexc = [[2.61,1,0,0,0,0,0,0,0,0,0,1],
                    [opts['duration'],2,Gm_exc,Gs_exc,5,0,0,1,random_seed,0,0,1],
                    [1,1,0,0,0,0,0,0,0,0,0,1]]
            ginh = [[2.61,1,0,0,0,0,0,0,0,0,0,1],
                    [opts['duration'],2,Gm_inh,Gs_inh,10,0,0,1,random_seed,0,0,1],
                    [1,1,0,0,0,0,0,0,0,0,0,1]]
            
        if opts['mean'] == 0 and opts['std'] == 0 and opts['tau'] == 0:
            # sinusoidal modulating current only
            if opts['separate']:
                current = [[opts['duration'],1,0,0,0,0,0,1,random_seed,0,0,1], # OU current
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
                modulation = [[opts['duration'],3,opts['I_modul'],frequency_value,0,0,0,0,0,0,0,1],
                              [1,1,0,0,0,0,0,0,0,0,0,1]]
            else:
                current = [[opts['duration'],3,opts['I_modul'],frequency_value,0,0,0,0,0,0,0,1],
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
        elif opts['tau'] == 0:
            # sinusoid on top of a DC current
            if opts['separate']:
                current = [[opts['duration'],1,opts['mean'],0,0,0,0,0,random_seed,0,0,1], # OU current
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
                modulation = [[opts['duration'],3,opts['I_modul'],frequency_value,0,0,0,0,0,0,0,1],
                              [1,1,0,0,0,0,0,0,0,0,0,1]]
            else:
                current = [[opts['duration'],-2,opts['mean'],0,0,0,0,0,0,1,0,1],
                           [0,-2,opts['I_modul'],frequency_value,0,0,0,0,0,3,1,1],
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
        else:
            # sinusoid on top of a noisy (OU) current
            if opts['separate']:
                current = [[opts['duration'],2,opts['mean'],opts['std'],opts['tau'],0,0,1,random_seed,0,0,1], # OU current
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
                modulation = [[opts['duration'],3,opts['I_modul'],frequency_value,0,0,0,0,0,0,0,1],
                              [1,1,0,0,0,0,0,0,0,0,0,1]]
            else:
                current = [[opts['duration'],-2,opts['mean'],opts['std'],opts['tau'],0,0,1,random_seed,2,0,1], # OU current
                           [0,-2,opts['I_modul'],frequency_value,0,0,0,0,0,3,1,1],
                           [1,1,0,0,0,0,0,0,0,0,0,1]]
    else:
        # current just for the preamble
        current = [[opts['duration']+1,1,0,0,0,0,0,0,0,0,0,1]]
        # conductances
        opts['R_inh'] = opts['R_exc']/ratio
        if opts['exc']:
            gexc = createSinusoidallyModOU(frequency_value, 'exc', random_seed, opts)
        else:
            gexc = [{'matrix': [[2.61,1,0,0,0,0,0,0,0,0,0,1],
                                [opts['duration'],2,Gm_exc,Gs_exc,5,0,0,1,random_seed,0,0,1],
                                [1,1,0,0,0,0,0,0,0,0,0,1]], 'filename': 'gexc.stim'}]
        if opts['inh']:
            ginh = createSinusoidallyModOU(frequency_value, 'inh', random_seed, opts)
        else:
            ginh = [{'matrix': [[2.61,1,0,0,0,0,0,0,0,0,0,1],
                                [opts['duration'],2,Gm_inh,Gs_inh,10,0,0,1,random_seed,0,0,1],
                                [1,1,0,0,0,0,0,0,0,0,0,1]], 'filename': 'ginh.stim'}]

    if gexc and ginh:
        writeConfigurationFile(opts)
        
    cnt = 0
    tot = opts['reps']*len(opts['frequencies'])
    for i in range(opts['reps']):
        np.random.shuffle(opts['frequencies'])
        for f in opts['frequencies']:
            if opts['compute_kernel'] and cnt%opts['kernel_frequency'] == 0:
                if 'separate' in opts and  opts['separate']:
                    # compute a kernel for each channel
                    sub.call('lcg kernel -I ' + str(opts['ai'][0]) + ' -O ' + str(opts['ao'][0]) +
                             ' -F '+ str(opts['sampling_rate']) + ' --non-rt --append', shell=True)
                    sub.call('lcg kernel -I ' + str(opts['ai'][1]) + ' -O ' + str(opts['ao'][1]) +
                             ' -F '+ str(opts['sampling_rate']) + ' --non-rt --append', shell=True)
                    pass
                else:
                    sub.call('lcg kernel -I ' + str(opts['ai']) + ' -O ' + str(opts['ao']) +
                             ' -F '+ str(opts['sampling_rate']), shell=True)
            cnt = cnt+1
            print('[%02d/%02d] F = %g Hz.' % (cnt,tot,f))
            
            I = copy.deepcopy(current)
            replaceValue(I, random_seed)
            replaceValue(I, frequency_value, f)
            lcg.writeStimFile(current_file, I, preamble=True)
            if 'separate' in opts and opts['separate']:
                I = copy.deepcopy(modulation)
                replaceValue(I, frequency_value, f)
                lcg.writeStimFile(modulation_file, I, preamble=[0,0])
            if gexc and ginh:
                for stimulus in gexc:
                    G = copy.deepcopy(stimulus['matrix'])
                    replaceValue(G, random_seed)
                    replaceValue(G, frequency_value, f)
                    lcg.writeStimFile(stimulus['filename'], G)
                for stimulus in ginh:
                    G = copy.deepcopy(stimulus['matrix'])
                    replaceValue(G, random_seed)
                    replaceValue(G, frequency_value, f)
                    lcg.writeStimFile(stimulus['filename'], G)
                sub.call(lcg.common.prog_name + ' -V 3 -c ' + config_file, shell=True)
            else:
                if 'separate' in opts and opts['separate']:
                    channels = [{'type':'input', 'channel':opts['ai'][0]},{'type':'input', 'channel':opts['ai'][1]},
                                {'type':'output', 'channel':opts['ao'][0], 'stimfile':current_file},
                                {'type':'output', 'channel':opts['ao'][1], 'stimfile':modulation_file}]
                    lcg.writeIOConfigurationFile(config_file,opts['sampling_rate'],opts['duration']+3.61,channels)
                    sub.call(lcg.common.prog_name + ' -c ' + config_file, shell=True)
                else:
                    sub.call('lcg stimulus -s ' + current_file + ' -F '+ str(opts['sampling_rate']), shell=True)

            if cnt != tot:
                sub.call(['sleep', str(opts['interval'])])
    stim_dur = lcg.writePulsesStimFile(f=stim_freq, dur=1, amp=stim_amp, N=15, delay=1, withRecovery=False)
    
    if os.path.isfile(template_file):
        lcg.substituteStrings(template_file, config_file,
                             {'WGT': str(weight),
                              'AI_PRE': str(ai[0]), 'AI_POST': str(ai[1]),
                              'AO_PRE': str(ao[0]), 'AO_POST': str(ao[1]),
                              'KERNEL_PRE': 'kernel-'+str(ai[0])+'-'+str(ao[0])+'.dat',
                              'KERNEL_POST': 'kernel-'+str(ai[1])+'-'+str(ao[1])+'.dat',
                              'TEND': str(stim_dur)})
    else:
        os.remove('pulses.stim')
        print('Default configuration file [%s] missing.' % template_file)
        usage()
        sys.exit(1)

    if with_bg:
        ratio = lcg.computeRatesRatio(Vm=balanced_voltage, Rin=input_resistance)
        Gm_exc,Gm_inh,Gs_exc,Gs_inh = lcg.computeSynapticBackgroundCoefficients(ratio, R_exc=bg_freq, Rin=input_resistance)
        lcg.writeGStimFiles({'m': Gm_exc, 's': Gs_exc, 'tau': 5, 'seed': 5061983},
                           {'m': Gm_inh, 's': Gs_inh, 'tau': 10, 'seed': 7051983},
                           stim_dur, 0, 0)

    sub.call('lcg kernel -a -F 15000 -I ' + str(ai[0]) + ' -O ' + str(ao[0]), shell=True)
    sub.call('lcg kernel -a -F 15000 -I ' + str(ai[1]) + ' -O ' + str(ao[1]), shell=True)
    sub.call(lcg.common.prog_name + ' -c ' + config_file + ' -n ' + str(trials) + ' -i ' + str(interval), shell=True)

if __name__ == '__main__':
    main()