def simulate_network(params_msn_d1,
                     params_msn_d2,
                     params_stn,
                     synapse_models,
                     sim_time,
                     seed,
                     I_e_add,
                     threads=1,
                     start_rec=0,
                     model_params={}):
    '''
        params_msn_d1 - dictionary with timing and burst freq setup for msn
                     {'base_rates':[0.1, 0.1, ..., 0.1], #Size number of actions 
                      'mod_rates': [[20,0,...,0],
                                    [0,20,...,0],...[0,0,...,20]] #size number of actions times number of events   
                      'mod_times':[[500,1000],[1500,2000],[9500,10000]] # size number of events 
                      'n_neurons':500}
                      
        params_msn_d2 - dictionary with timing and burst freq setup for gpe
        params_stn    - dictionary {'rate':50}
                     same as params_msn
        neuron_model - string, the neuron model to use 
        synapse_models - dict, {'MSN':'...', 'GPE':,'...', 'STN':'...'}
        sim_time - simulation time
        seed - seed for random generator
        I_e_add - diabled
        start_rec - start recording from
        model_params - general model paramters
    '''

    I_e_add = {'SNR': 300, 'STN': 0, 'GPE': 30}
    f = 0.01  #0.01#0.5

    I_e_variation = {'GPE': 25 * f, 'SNR': 100 * f, 'STN': 10 * f}

    my_nest.ResetKernel(threads=8)
    numpy.random.seed(seed)

    params = {
        'conns': {
            'MSN_D1_SNR': {
                'syn': synapse_models[0]
            },
            'GPE_SNR': {
                'syn': synapse_models[1]
            }
        }
    }

    params = misc.dict_merge(model_params, params)

    model_list, model_dict = models()
    group_list, group_dict, connect_list, connect_params = network(
        model_dict, params)
    print connect_params

    groups = {}
    for name, model, setup in group_list:

        # Update input current
        my_nest.MyLoadModels(model_dict, [model])
        if name in I_e_add.keys():
            I_e = my_nest.GetDefaults(model)['I_e'] + I_e_add[name]
            my_nest.SetDefaults(model, {'I_e': I_e})

        groups[name] = []
        for action in range(connect_params['misc']['n_actions']):
            if model in ['MSN_D1_spk_gen', 'MSN_D2_spk_gen']:
                group = MyPoissonInput(params=setup,
                                       sd=True,
                                       sd_params={
                                           'start': start_rec,
                                           'stop': sim_time
                                       })
            else:
                group = MyGroup(params=setup,
                                sd=True,
                                mm=False,
                                mm_dt=0.1,
                                sd_params={
                                    'start': start_rec,
                                    'stop': sim_time
                                })

            groups[name].append(group)

    for action in range(connect_params['misc']['n_actions']):
        groups['MSN_D1'][action].set_spike_times(
            list(params_msn_d1['mod_rates'][action]),
            list(params_msn_d1['mod_times']),
            sim_time,
            ids=groups['MSN_D1'][action].ids)
        groups['MSN_D2'][action].set_spike_times(
            params_msn_d2['mod_rates'][action],
            params_msn_d2['mod_times'],
            sim_time,
            ids=groups['MSN_D2'][action].ids)

    # Create neurons and synapses
    for source, target, props in connect_list:
        my_nest.MyLoadModels(model_dict, [props['model']])

        for action in range(connect_params['misc']['n_actions']):

            pre = list(groups[source][action].ids)
            post = list(groups[target][action].ids)
            my_nest.MyRandomConvergentConnect(pre, post, params=props)

    STN_CTX_input_base = my_nest.Create('poisson_generator',
                                        params={
                                            'rate': params_stn['rate'],
                                            'start': 0.,
                                            'stop': sim_time
                                        })
    my_nest.MyLoadModels(model_dict, ['CTX_STN_ampa_s'])

    for action in range(connect_params['misc']['n_actions']):
        my_nest.DivergentConnect(STN_CTX_input_base,
                                 groups['STN'][action].ids,
                                 model='CTX_STN_ampa_s')

    my_nest.MySimulate(sim_time)

    for action in range(connect_params['misc']['n_actions']):
        groups['MSN_D1'][action].get_signal('s',
                                            start=start_rec,
                                            stop=sim_time)
        groups['MSN_D2'][action].get_signal('s',
                                            start=start_rec,
                                            stop=sim_time)
        groups['GPE'][action].get_signal('s', start=start_rec, stop=sim_time)
        groups['SNR'][action].get_signal('s', start=start_rec, stop=sim_time)
        groups['STN'][action].get_signal('s', start=start_rec, stop=sim_time)

    return groups
def simulate_network_test(params_msn_d1,
                          params_msn_d2,
                          params_stn,
                          synapse_models,
                          sim_time,
                          seed,
                          I_e_add,
                          threads=1,
                          start_rec=0,
                          model_params={},
                          params_in={},
                          dis_conn_GPE_STN=False):
    '''
        params_msn_d1 - dictionary with timing and burst freq setup for msn
                     {'base_rates':0.1, 
                      'base_times':[1], 
                      'mod_rates': 20,
                      'mod_times':[1,200], 
                      'mod_units':list()
                      'n_tot':500, 
                       n_mod=20}
        params_msn_d2 - dictionary with timing and burst freq setup for gpe
        params_stn    - dictionary {'rate':50}
                     same as params_msn
        neuron_model - string, the neuron model to use 
        synapse_models - dict, {'MSN':'...', 'GPE':,'...', 'STN':'...'}
        sim_time - simulation time
        seed - seed for random generator
        I_e_add - diabled
        start_rec - start recording from
        model_params - general model paramters
    '''

    my_nest.ResetKernel(threads=8)
    numpy.random.seed(seed)

    params = {
        'conns': {
            'MSN_D1_SNR': {
                'syn': synapse_models[0]
            },
            'GPE_SNR': {
                'syn': synapse_models[1]
            }
        }
    }

    params = misc.dict_merge(model_params, params)
    params = misc.dict_merge({'neurons': {'GPE': {'paused': 0}}}, params)

    model_list, model_dict = models(params_in)
    layer_list, connect_list = network(model_dict, params)

    # Create neurons and synapses
    layer_dic = {}
    for name, model, props in layer_list:

        # Update input current
        my_nest.MyLoadModels(model_dict, [model[1]])
        if name in I_IN_VIVO.keys():
            I_e = my_nest.GetDefaults(model[1])['I_e'] + I_IN_VIVO[name]
            my_nest.SetDefaults(model[1], {'I_e': I_e})

        #! Create layer, retrieve neurons ids per elements and p
        if model[0] == 'spike_generator':
            layer = MyLayerPoissonInput(layer_props=props,
                                        sd=True,
                                        sd_params={
                                            'start': start_rec,
                                            'stop': sim_time
                                        })

        else:
            layer = MyLayerGroup(layer_props=props,
                                 sd=True,
                                 mm=False,
                                 mm_dt=0.1,
                                 sd_params={
                                     'start': start_rec,
                                     'stop': sim_time
                                 })

            for iter, id in enumerate(layer[:]):

                if name == 'GPE' and params_msn_d2[
                        'n_mod'] and iter < params['neurons']['GPE']['paused']:
                    scg = my_nest.Create('step_current_generator', n=1)
                    rec = my_nest.GetStatus([id])[0]['receptor_types']
                    my_nest.SetStatus(
                        scg, {
                            'amplitude_times': params_msn_d2['mod_times'],
                            'amplitude_values': [0., -300., 0.]
                        })
                    my_nest.Connect(scg, [id],
                                    params={'receptor_type': rec['CURR']})

                I_e = my_nest.GetDefaults(model[1])['I_e']
                if I_E_VARIATION[name]:
                    I = numpy.random.normal(I_e, I_E_VARIATION[name])
                else:
                    I = I_e
                #I=I_e
                my_nest.SetStatus([id], {'I_e': I})
        layer_dic[name] = layer

    mm = nest.Create('multimeter', 1)
    recodables = ['V_m', 'I', 'g_AMPA', 'g_NMDA', 'g_GABAA_1', 'g_GABAA_2']

    my_nest.SetStatus(mm, {'interval': 0.1, 'record_from': recodables})
    my_nest.Connect(mm, [layer_dic['STN'].ids[0]])

    # Connect populations
    for conn in connect_list:
        name = conn[0] + '_' + conn[1]
        my_nest.MyLoadModels(model_dict, [conn[2]['synapse_model']])

        if dis_conn_GPE_STN == 'GPE' and (name in ['GPE_SNR']):
            r, syn = 32 * 30.0, 'GPE_SNR_gaba_s_ref'
            if not syn in my_nest.Models():
                my_nest.MyLoadModels(model_dict, [syn])
            pg = my_nest.Create('poisson_generator', 1, {
                'rate': r,
                'start': 1.
            })
            my_nest.DivergentConnect(pg, layer_dic[conn[1]].ids, model=syn)
        elif dis_conn_GPE_STN == 'STN' and (name in ['STN_SNR']):
            r, syn = 30 * 10.0, 'STN_SNR_ampa_s'
            if not syn in my_nest.Models():
                my_nest.MyLoadModels(model_dict, [syn])
            pg = my_nest.Create('poisson_generator', 1, {
                'rate': r,
                'start': 1.
            })
            my_nest.DivergentConnect(pg, layer_dic[conn[1]].ids, model=syn)

        else:
            name = name + '_' + conn[3]
            tp.ConnectLayers(layer_dic[conn[0]].layer_id,
                             layer_dic[conn[1]].layer_id, conn[2])
            layer_dic[conn[1]].add_connection(source=layer_dic[conn[0]],
                                              type=conn[3],
                                              props=conn[2])

    # Sort MSN D2 such that the closest to center is first in ids list.
    # Do this to we can get focused inhibition in GPe
    if params_msn_d2['focus']:
        MSN_D2_idx = layer_dic['MSN_D2'].sort_ids()
    else:
        MSN_D2_idx = range(len(numpy.array(layer_dic['MSN_D2'].ids)))

    n_mod_msn_d1 = params_msn_d1['n_mod']
    n_mod_msn_d2 = params_msn_d2['n_mod']

    MSN_D1_ids = layer_dic['MSN_D1'].ids
    MSN_D2_ids = layer_dic['MSN_D2'].ids

    MSN_D1_mod, MSN_D2_mod = [], []
    if params_msn_d1['n_mod']: MSN_D1_mod = MSN_D1_ids[0:n_mod_msn_d1]
    if params_msn_d2['n_mod']:
        MSN_D2_mod = MSN_D2_ids[0:n_mod_msn_d2 *
                                params_msn_d2['skip']:params_msn_d2['skip']]

    MSN_D1_base = list(set(MSN_D1_ids).difference(MSN_D1_mod))
    MSN_D2_base = list(set(MSN_D2_ids).difference(MSN_D2_mod))

    #layer_dic['MSN_D1'].ids[0:n_base_msn_d1]

    #MSN_D2_ids=numpy.array(layer_dic['MSN_D2'].ids)
    #MSN_D2_base=MSN_D2_ids#[MSN_D2_idx[0:n_base_msn_d1]]

    #set().difference(t)

    layer_dic['MSN_D1'].set_spike_times(params_msn_d1['base_rates'],
                                        params_msn_d1['base_times'],
                                        sim_time,
                                        ids=MSN_D1_base)
    layer_dic['MSN_D2'].set_spike_times(params_msn_d2['base_rates'],
                                        params_msn_d2['base_times'],
                                        sim_time,
                                        ids=MSN_D2_base)

    if params_msn_d1['n_mod']:
        layer_dic['MSN_D1'].set_spike_times(params_msn_d1['mod_rates'],
                                            params_msn_d1['mod_times'],
                                            sim_time,
                                            ids=MSN_D1_mod)
    if params_msn_d2['n_mod']:
        layer_dic['MSN_D2'].set_spike_times(params_msn_d2['mod_rates'],
                                            params_msn_d2['mod_times'],
                                            sim_time,
                                            ids=MSN_D2_mod)

    STN_CTX_input_base = my_nest.Create('poisson_generator',
                                        params={
                                            'rate': BASE_RATE_CTX_STN,
                                            'start': 0.,
                                            'stop': sim_time
                                        })
    my_nest.MyLoadModels(model_dict, ['CTX_STN_ampa_s'])
    my_nest.DivergentConnect(STN_CTX_input_base,
                             layer_dic['STN'].ids,
                             model='CTX_STN_ampa_s')

    if params_stn['mod']:
        STN_CTX_input_mod = my_nest.Create('poisson_generator',
                                           params={
                                               'rate': params_stn['mod_rate'],
                                               'start':
                                               params_stn['mod_times'][0],
                                               'stop':
                                               params_stn['mod_times'][1]
                                           })
        my_nest.DivergentConnect(STN_CTX_input_mod,
                                 layer_dic['STN'].ids,
                                 model='CTX_STN_ampa_s')

    #tar=[]
    #for id in layer_dic['MSN_D1'].ids:
    #    tar.extend(sorted(nest.GetStatus(my_nest.FindConnections([id]),'target'))[:-1])

    #pylab.subplot(211).hist(tar, 1500)


#
#    tar=[]
#    for id in layer_dic['MSN_D2'].ids:
#        tar.extend(sorted(nest.GetStatus(my_nest.FindConnections([id]),'target'))[1:])
#
#    pylab.subplot(212).hist(tar, 1500)
#pylab.show()
#
#
    my_nest.MySimulate(sim_time)

    if params_msn_d1['n_mod']: layer_dic['MSN_D1'].id_mod = MSN_D1_mod
    if params_msn_d2['n_mod']: layer_dic['MSN_D2'].id_mod = MSN_D2_mod

    #layer_dic['MSN_D1'].get_signal( 's', start=start_rec, stop=sim_time )
    #layer_dic['MSN_D2'].get_signal( 's', start=start_rec, stop=sim_time )
    #layer_dic['GPE'].get_signal( 's', start=start_rec, stop=sim_time )
    #layer_dic['SNR'].get_signal( 's', start=start_rec, stop=sim_time )
    #layer_dic['STN'].get_signal( 's', start=start_rec, stop=sim_time )

    st_mm = my_nest.GetStatus(mm)[0]
    pylab.plot(st_mm['events']['g_AMPA'])
    pylab.plot(st_mm['events']['g_GABAA_1'])
    pylab.plot(st_mm['events']['g_NMDA'])
    pylab.plot(st_mm['events']['g_GABAA_2'])
    m_ampa = numpy.mean(st_mm['events']['g_AMPA'])
    m_gaba = numpy.mean(st_mm['events']['g_GABAA_1'])
    pylab.title("{0} m_ampa:{1:2.1f} m_gaba:{2:2.1f}".format(
        my_nest.version(), m_ampa, m_gaba))
    pylab.show()
    return layer_dic
def simulate_network_poisson(params_msn_d1,
                             params_msn_d2,
                             params_stn,
                             synapse_models,
                             sim_time,
                             seed,
                             I_e_add,
                             threads=1,
                             start_rec=0,
                             model_params={},
                             params_in={},
                             p_weights=False,
                             p_conn=False,
                             p_I_e=False):
    '''
    
    Assume that the background MSN are static weak, then can use poisson process 
    for them,
        params_msn_d1 - dictionary with timing and burst freq setup for msn
                     {'base_rates':0.1, 
                      'base_times':[1], 
                      'mod_rates': 20,
                      'mod_times':[1,200], 
                      'mod_units':list()
                      'n_tot':500, 
                       n_mod=20}
        params_msn_d2 - dictionary with timing and burst freq setup for gpe
        params_stn    - dictionary {'rate':50}
                     same as params_msn
        neuron_model - string, the neuron model to use 
        synapse_models - dict, {'MSN':'...', 'GPE':,'...', 'STN':'...'}
        sim_time - simulation time
        seed - seed for random generator
        I_e_add - diabled
        start_rec - start recording from
        model_params - general model paramters
    '''

    params = {
        'conns': {
            'MSN_D1_SNR': {
                'syn': synapse_models[0]
            },
            'GPE_SNR': {
                'syn': synapse_models[1]
            }
        }
    }

    my_nest.ResetKernel(threads=8)
    numpy.random.seed(seed)

    params = misc.dict_merge(model_params, params)
    params = misc.dict_merge({'neurons': {'GPE': {'paused': 0}}}, params)

    model_list, model_dict = models({}, p_weights)
    layer_list, connect_list = network(model_dict, params, p_conn)

    dic_p_I_e = {'SNR': 1., 'GPE': 1., 'STN': 1.}
    if p_I_e is not False:
        dic_p_I_e['SNR'] *= p_I_e[0]
        dic_p_I_e['GPE'] *= p_I_e[1]
        dic_p_I_e['STN'] *= p_I_e[2]

    # Create neurons and synapses
    layer_dic = {}
    for name, model, props in layer_list:

        # Update input current
        my_nest.MyLoadModels(model_dict, [model[1]])
        if name in I_IN_VIVO.keys():
            I_in_vitro = my_nest.GetDefaults(model[1])['I_e']
            I_e = I_in_vitro + I_IN_VIVO[name]
            my_nest.SetDefaults(model[1], {'I_e': I_e * dic_p_I_e[name]})

        #! Create layer, retrieve neurons ids per elements and p
        if model[0] == 'spike_generator':
            layer = MyLayerPoissonInput(layer_props=props,
                                        sd=True,
                                        sd_params={
                                            'start': start_rec,
                                            'stop': sim_time
                                        })
        elif model[0] == 'poisson_generator':
            layer = MyPoissonInput(model[0],
                                   props['columns'],
                                   sd=True,
                                   sd_params={
                                       'start': start_rec,
                                       'stop': sim_time
                                   })

        else:
            layer = MyLayerGroup(layer_props=props,
                                 sd=True,
                                 mm=False,
                                 mm_dt=0.1,
                                 sd_params={
                                     'start': start_rec,
                                     'stop': sim_time
                                 })

            for iter, id in enumerate(layer[:]):

                if name == 'GPE' and params_msn_d2[
                        'n_mod'] and iter < params['neurons']['GPE']['paused']:
                    scg = my_nest.Create('step_current_generator', n=1)
                    rec = my_nest.GetStatus([id])[0]['receptor_types']
                    my_nest.SetStatus(
                        scg, {
                            'amplitude_times': params_msn_d2['mod_times'],
                            'amplitude_values': [0., -300., 0.]
                        })
                    my_nest.Connect(scg, [id],
                                    params={'receptor_type': rec['CURR']})

                I_e = my_nest.GetDefaults(model[1])['I_e']
                if I_E_VARIATION[name]:
                    I = numpy.random.normal(
                        I_e, I_E_VARIATION[name])  #I_E_VARIATION[name])
                else:
                    I = I_e
                my_nest.SetStatus([id], {'I_e': I})

        layer_dic[name] = layer

    # Connect populations
    for conn in connect_list:
        print[conn[2]['synapse_model']]
        if not conn[2]['synapse_model'] in nest.Models():
            my_nest.MyLoadModels(model_dict, [conn[2]['synapse_model']])

        if layer_dic[conn[0]].model == 'poisson_generator':
            my_nest.Connect(layer_dic[conn[0]].ids,
                            layer_dic[conn[1]].ids,
                            model=conn[2]['synapse_model'])
        else:

            name = conn[0] + '_' + conn[1] + '_' + conn[3]
            tp.ConnectLayers(layer_dic[conn[0]].layer_id,
                             layer_dic[conn[1]].layer_id, conn[2])
            layer_dic[conn[1]].add_connection(source=layer_dic[conn[0]],
                                              type=conn[3],
                                              props=conn[2])

    # Sort MSN D2 such that the closest to center is first in ids list.
    # Do this to we can get focused inhibition in GPe

    if params_msn_d2['focus']:
        MSN_D2_idx = layer_dic['MSN_D2'].sort_ids()
    else:
        MSN_D2_idx = range(len(numpy.array(layer_dic['MSN_D2'].ids)))

    n_mod_msn_d1 = params_msn_d1['n_mod']
    n_mod_msn_d2 = params_msn_d2['n_mod']

    MSN_D1_ids = layer_dic['MSN_D1'].ids
    MSN_D2_ids = layer_dic['MSN_D2'].ids

    MSN_D1_mod, MSN_D2_mod = [], []
    if params_msn_d1['n_mod']: MSN_D1_mod = MSN_D1_ids[0:n_mod_msn_d1]
    if params_msn_d2['n_mod']:
        MSN_D2_mod = MSN_D2_ids[0:n_mod_msn_d2 *
                                params_msn_d2['skip']:params_msn_d2['skip']]

    MSN_D1_base = list(set(MSN_D1_ids).difference(MSN_D1_mod))
    MSN_D2_base = list(set(MSN_D2_ids).difference(MSN_D2_mod))

    layer_dic['MSN_D1'].set_spike_times(params_msn_d1['base_rates'],
                                        params_msn_d1['base_times'],
                                        sim_time,
                                        ids=MSN_D1_base)
    layer_dic['MSN_D2'].set_spike_times(params_msn_d2['base_rates'],
                                        params_msn_d2['base_times'],
                                        sim_time,
                                        ids=MSN_D2_base)

    if params_msn_d1['n_mod']:
        layer_dic['MSN_D1'].set_spike_times(params_msn_d1['mod_rates'],
                                            params_msn_d1['mod_times'],
                                            sim_time)
    if params_msn_d2['n_mod']:
        layer_dic['MSN_D2'].set_spike_times(params_msn_d2['mod_rates'],
                                            params_msn_d2['mod_times'],
                                            sim_time,
                                            ids=MSN_D2_mod)

    # If background poisson are use
    if params_msn_d1['bg_rate']:
        layer_dic['MSN_D1_bg'].set_spike_times(params_msn_d1['bg_rate'], [1.],
                                               sim_time)
    if params_msn_d2['bg_rate']:
        layer_dic['MSN_D2_bg'].set_spike_times(params_msn_d2['bg_rate'], [1.],
                                               sim_time)

    STN_CTX_input_base = my_nest.Create('poisson_generator',
                                        params={
                                            'rate': BASE_RATE_CTX_STN,
                                            'start': 0.,
                                            'stop': sim_time
                                        })
    my_nest.MyLoadModels(model_dict, ['CTX_STN_ampa_s'])

    if 'STN' in layer_dic.keys():
        my_nest.DivergentConnect(STN_CTX_input_base,
                                 layer_dic['STN'].ids,
                                 model='CTX_STN_ampa_s')

    if params_stn['mod'] and 'STN' in layer_dic.keys():
        STN_CTX_input_mod = my_nest.Create('poisson_generator',
                                           params={
                                               'rate': params_stn['mod_rate'],
                                               'start':
                                               params_stn['mod_times'][0],
                                               'stop':
                                               params_stn['mod_times'][1]
                                           })
        my_nest.DivergentConnect(STN_CTX_input_mod,
                                 layer_dic['STN'].ids,
                                 model='CTX_STN_ampa_s')

    my_nest.MySimulate(sim_time)

    if params_msn_d1['n_mod']: layer_dic['MSN_D1'].id_mod = MSN_D1_mod
    if params_msn_d2['n_mod']: layer_dic['MSN_D2'].id_mod = MSN_D2_mod

    if 'MSN_D1' in layer_dic.keys():
        layer_dic['MSN_D1'].get_signal('s', start=start_rec, stop=sim_time)
    if 'MSN_D2' in layer_dic.keys():
        layer_dic['MSN_D2'].get_signal('s', start=start_rec, stop=sim_time)
    if 'GPE' in layer_dic.keys():
        layer_dic['GPE'].get_signal('s', start=start_rec, stop=sim_time)
    if 'SNR' in layer_dic.keys():
        layer_dic['SNR'].get_signal('s', start=start_rec, stop=sim_time)
    if 'STN' in layer_dic.keys():
        layer_dic['STN'].get_signal('s', start=start_rec, stop=sim_time)

    return layer_dic
def simulate_network_direct_indirect_onoff_vs_rate(m_d1,
                                                   m_d2,
                                                   params_msn_d1,
                                                   params_msn_d2,
                                                   params_stn,
                                                   synapse_models,
                                                   I_e_add,
                                                   sim_time=2000.,
                                                   seed=1,
                                                   threads=4,
                                                   start_rec=0,
                                                   model_params={},
                                                   flag_bg=False):

    if flag_bg:
        N_MSN = model_params['neurons']['MSN_D1']['n']

        params_msn_d1.update(
            {'bg_rate': [0.1 * (500 - 500 * m_d1 / float(N_MSN))]})
        params_msn_d2.update(
            {'bg_rate': [0.1 * (500 - 500 * m_d2 / float(N_MSN))]})

        # Change paramters to use poisson background
        params = {
            'conns': {
                'MSN_D1_SNR': {
                    'p': 500. / float(N_MSN)
                },
                'MSN_D2_GPE': {
                    'p': 500. / float(N_MSN),
                    'lines': False
                }
            },
            'neurons': {
                'MSN_D1': {
                    'n': m_d1
                },
                'MSN_D2': {
                    'n': m_d2
                },
                'MSN_D1_bg': {
                    'n': 300,
                    'lesion': False
                },
                'MSN_D2_bg': {
                    'n': 300,
                    'lesion': False
                },
                'GPE': {
                    'paused': False
                }
            }
        }
        model_params = misc.dict_merge(model_params, params)

    params_msn_d1.update({'n_mod': int(m_d1)})
    params_msn_d2.update({'n_mod': int(m_d2)})

    layer_dic = simulate_network_poisson(params_msn_d1,
                                         params_msn_d2,
                                         params_stn,
                                         synapse_models,
                                         sim_time=sim_time,
                                         seed=seed,
                                         I_e_add=I_e_add,
                                         threads=4,
                                         start_rec=start_rec,
                                         model_params=model_params)

    layer_dic['SNR'].get_signal('s', start=start_rec, stop=sim_time)
    signal = layer_dic['SNR'].signals['spikes']
    spk_mean = numpy.mean(signal.spike_histogram(time_bin=1, normalized=True),
                          axis=0)
    spk = spk_mean

    layer_dic['GPE'].get_signal('s', start=start_rec, stop=sim_time)
    signal = layer_dic['GPE'].signals['spikes']
    spk_mean = numpy.mean(signal.spike_histogram(time_bin=1, normalized=True),
                          axis=0)
    spk2 = spk_mean

    return spk, spk2
def simulate_poisson_15000(params_msn_d1, params_msn_d2, params_stn,
                           synapse_models, sim_time, seed, I_e_add, threads,
                           start_rec, model_params):
    N_MSN = 15000

    params = {
        'misc': {
            'N_MSN': N_MSN
        },
        'conns': {
            'MSN_D1_SNR': {
                'p': 500. / float(N_MSN)
            },
            'MSN_D2_GPE': {
                'p': 500. / float(N_MSN),
                'lines': False
            }
        },
        'neurons': {
            'MSN_D1': {
                'n': 0
            },
            'MSN_D2': {
                'n': 0
            },
            'MSN_D1_bg': {
                'n': 300,
                'lesion': False
            },
            'MSN_D2_bg': {
                'n': 300,
                'lesion': False
            },
            'GPE': {
                'paused': False
            }
        }
    }

    model_params = misc.dict_merge(params, model_params)

    layer_dic = simulate_network_poisson(params_msn_d1, params_msn_d2,
                                         params_stn, synapse_models, sim_time,
                                         seed, I_e_add, threads, start_rec,
                                         model_params)

    mr = []
    std = []
    if 'SNR' in layer_dic.keys():
        signal = layer_dic['SNR'].signals['spikes']
        m_hist = numpy.mean(signal.spike_histogram(time_bin=1,
                                                   normalized=True),
                            axis=1)
        mr.append(numpy.mean(m_hist, axis=0))
        std.append(numpy.std(m_hist, axis=0))
    else:
        mr.append(0)
        std.append(0)

    if 'GPE' in layer_dic.keys():
        signal = layer_dic['GPE'].signals['spikes']
        m_hist = numpy.mean(signal.spike_histogram(time_bin=1,
                                                   normalized=True),
                            axis=1)
        mr.append(numpy.mean(m_hist, axis=0))
        std.append(numpy.std(m_hist, axis=0))
    else:
        mr.append(0)
        std.append(0)

    if 'STN' in layer_dic.keys():
        signal = layer_dic['STN'].signals['spikes']
        m_hist = numpy.mean(signal.spike_histogram(time_bin=1,
                                                   normalized=True),
                            axis=1)
        mr.append(numpy.mean(m_hist, axis=0))
        std.append(numpy.std(m_hist, axis=0))
    else:
        mr.append(0)
        std.append(0)
    return layer_dic, mr, std
def simulate_network(params_msn_d1, params_msn_d2, params_stn,
                     synapse_models, sim_time, seed, I_e_add, threads=1, 
                     start_rec=0, model_params={}):    
    '''
        params_msn_d1 - dictionary with timing and burst freq setup for msn
                     {'base_rates':[0.1, 0.1, ..., 0.1], #Size number of actions 
                      'mod_rates': [[20,0,...,0],
                                    [0,20,...,0],...[0,0,...,20]] #size number of actions times number of events   
                      'mod_times':[[500,1000],[1500,2000],[9500,10000]] # size number of events 
                      'n_neurons':500}
                      
        params_msn_d2 - dictionary with timing and burst freq setup for gpe
        params_stn    - dictionary {'rate':50}
                     same as params_msn
        neuron_model - string, the neuron model to use 
        synapse_models - dict, {'MSN':'...', 'GPE':,'...', 'STN':'...'}
        sim_time - simulation time
        seed - seed for random generator
        I_e_add - diabled
        start_rec - start recording from
        model_params - general model paramters
    '''

    
    I_e_add={'SNR':300, 'STN':0,'GPE':30}
    f=0.01#0.01#0.5
    
    I_e_variation={'GPE':25*f,'SNR':100*f,'STN':10*f}
    
    my_nest.ResetKernel(threads=8) 
    numpy.random.seed(seed)
    
    params = {'conns':{'MSN_D1_SNR':{'syn':synapse_models[0]},   
                       'GPE_SNR':{'syn':synapse_models[1]}}}   
    
    params=misc.dict_merge(model_params, params)
               
    model_list, model_dict = models()
    group_list, group_dict, connect_list, connect_params = network(model_dict, params)
    print connect_params
    

    
    
    groups={}
    for name, model, setup in group_list:
        
        # Update input current
        my_nest.MyLoadModels( model_dict, [model] )
        if name in I_e_add.keys():
            I_e=my_nest.GetDefaults(model)['I_e']+I_e_add[name]
            my_nest.SetDefaults(model,{'I_e':I_e})
            
        groups[name]=[]
        for action in range(connect_params['misc']['n_actions']):
            if model in ['MSN_D1_spk_gen','MSN_D2_spk_gen']:
                group=MyPoissonInput(params=setup, sd=True, sd_params={'start':start_rec, 'stop':sim_time})
            else:
                group=MyGroup(params=setup, sd=True, mm=False, mm_dt = 0.1,
                               sd_params={'start':start_rec, 'stop':sim_time} )
                
            groups[name].append(group)
    
    for action in range(connect_params['misc']['n_actions']):
        groups['MSN_D1'][action].set_spike_times(list(params_msn_d1['mod_rates'][action]), 
                                        list(params_msn_d1['mod_times']), sim_time, 
                                        ids=groups['MSN_D1'][action].ids)
        groups['MSN_D2'][action].set_spike_times(params_msn_d2['mod_rates'][action], 
                                        params_msn_d2['mod_times'], sim_time, 
                                        ids=groups['MSN_D2'][action].ids)
      
    # Create neurons and synapses
    for source, target, props  in connect_list:
        my_nest.MyLoadModels( model_dict, [props['model']] )
        
        
        for action in range(connect_params['misc']['n_actions']):
            
            pre=list(groups[source][action].ids)
            post=list(groups[target][action].ids)
            my_nest.MyRandomConvergentConnect(pre, post, params=props)
           
    STN_CTX_input_base=my_nest.Create('poisson_generator',params={'rate':params_stn['rate'], 'start':0., 'stop':sim_time})
    my_nest.MyLoadModels( model_dict, ['CTX_STN_ampa_s'] )
    
    for action in range(connect_params['misc']['n_actions']): 
        my_nest.DivergentConnect(STN_CTX_input_base, groups['STN'][action].ids, 
                                 model='CTX_STN_ampa_s')
    
    
    my_nest.MySimulate(sim_time)    
    
    for action in range(connect_params['misc']['n_actions']):     
        groups['MSN_D1'][action].get_signal( 's', start=start_rec, stop=sim_time )
        groups['MSN_D2'][action].get_signal( 's', start=start_rec, stop=sim_time )
        groups['GPE'][action].get_signal( 's', start=start_rec, stop=sim_time )    
        groups['SNR'][action].get_signal( 's', start=start_rec, stop=sim_time )    
        groups['STN'][action].get_signal( 's', start=start_rec, stop=sim_time )    
    
    return groups
Exemple #7
0
def simulate_hyperdirect_fun(params_msn_d1,
                             params_msn_d2,
                             params_stn,
                             synapse_models,
                             I_e_add,
                             sim_time,
                             seed,
                             threads,
                             start_rec,
                             model_params,
                             flag_bg,
                             stn_syn='STN_SNR_gaba_s'):

    if flag_bg:
        N_MSN = model_params['neurons']['MSN_D1']['n']

        params_msn_d1.update({'bg_rate': [0.1 * (500)]})
        params_msn_d2.update({'bg_rate': [0.1 * (500)]})

        # Change paramters to use poisson background
        params = {
            'conns': {
                'MSN_D1_SNR': {
                    'p': 0
                },
                'MSN_D2_GPE': {
                    'p': 0,
                    'lines': False
                }
            },
            'neurons': {
                'MSN_D1': {
                    'n': 0
                },
                'MSN_D2': {
                    'n': 0
                },
                'MSN_D1_bg': {
                    'n': 300,
                    'lesion': False
                },
                'MSN_D2_bg': {
                    'n': 300,
                    'lesion': False
                },
                'GPE': {
                    'paused': False
                }
            }
        }
        model_params = misc.dict_merge(model_params, params)

    layer_dic = simulate_network_poisson(params_msn_d1,
                                         params_msn_d2,
                                         params_stn,
                                         synapse_models,
                                         sim_time=sim_time,
                                         seed=seed,
                                         I_e_add=I_e_add,
                                         threads=4,
                                         start_rec=start_rec,
                                         model_params=model_params)

    r = []
    signal = layer_dic['SNR'].signals['spikes']
    r.append(
        numpy.mean(signal.spike_histogram(time_bin=1, normalized=True),
                   axis=0))

    signal = layer_dic['GPE'].signals['spikes']
    r.append(
        numpy.mean(signal.spike_histogram(time_bin=1, normalized=True),
                   axis=0))

    signal = layer_dic['STN'].signals['spikes']
    r.append(
        numpy.mean(signal.spike_histogram(time_bin=1, normalized=True),
                   axis=0))

    numpy.array(r)
    return r
Exemple #8
0
def simulate_direct_fun(mod,
                        freq,
                        params_msn_d1,
                        params_msn_d2,
                        params_stn,
                        synapse_models,
                        I_e_add,
                        sim_time=2000.,
                        seed=1,
                        threads=4,
                        start_rec=0,
                        model_params={},
                        flag_bg=False):
    spk = []

    params_msn_d1.update({
        'mod_rates': [0.1, freq, 0.1],
        'mod_times': [1, 1000, 1000 + 500],
        'n_mod': int(mod)
    })
    if flag_bg:
        N_MSN = model_params['neurons']['MSN_D1']['n']

        params_msn_d1.update(
            {'bg_rate': [0.1 * (500 - 500 * mod / float(N_MSN))]})
        params_msn_d2.update({'bg_rate': [0.1 * (500)]})

        # Change paramters to use poisson background
        params = {
            'misc': {
                'N_MSN': N_MSN
            },
            'conns': {
                'MSN_D1_SNR': {
                    'p': 500. / float(N_MSN)
                },
                'MSN_D2_GPE': {
                    'p': 500. / float(N_MSN),
                    'lines': False
                }
            },
            'neurons': {
                'MSN_D1': {
                    'n': mod
                },
                'MSN_D2': {
                    'n': 0
                },
                'MSN_D1_bg': {
                    'n': 300,
                    'lesion': False
                },
                'MSN_D2_bg': {
                    'n': 300,
                    'lesion': False
                },
                'GPE': {
                    'paused': False
                }
            }
        }

    model_params = misc.dict_merge(model_params, params)

    layer_dic = simulate_network_poisson(params_msn_d1,
                                         params_msn_d2,
                                         params_stn,
                                         synapse_models,
                                         sim_time=sim_time,
                                         seed=seed,
                                         I_e_add=I_e_add,
                                         threads=4,
                                         start_rec=start_rec,
                                         model_params=model_params)

    layer_dic['SNR'].get_signal('s', start=start_rec, stop=sim_time)
    signal = layer_dic['SNR'].signals['spikes']
    spk = signal.spike_histogram(time_bin=1, normalized=True)

    pop_rate = numpy.mean(spk, axis=0)
    return pop_rate