def fit_data(data, order=2, Fout_high=50., fit_filename=None):

    mFout, sFout = data['Fout_mean'], data['Fout_std']
    Model = data['Model']

    neuron_params = built_up_neuron_params(Model, Model['NRN_KEY'])
    SYN_POPS = build_up_afferent_synaptic_input(Model, Model['POP_STIM'])

    RATES = {}
    for syn in SYN_POPS:
        RATES['F_' + syn['name']] = data['F_' + syn['name']]

    ### OUTPUT OF ANALYTICAL CALCULUS IN SI UNITS !! -> from here SI, be careful...
    muV, sV, gV, Tv = getting_statistical_properties(neuron_params,
                                                     SYN_POPS,
                                                     RATES,
                                                     already_SI=False)
    Proba = Proba_g_P(muV, sV, gV, 1e-3 * neuron_params['Vthre'])

    # # only strictly positive firing rates taken into account
    cond = (mFout > 0) & (mFout < Fout_high)

    Fout, muV, sV, gV, Tv, Proba = mFout[cond], muV[cond], sV[cond], gV[
        cond], Tv[cond], Proba[cond]

    # # computing effective threshold
    Vthre_eff = effective_Vthre(Fout, muV, sV, Tv)

    TERMS = get_all_normalized_terms(muV, sV, gV, Tv, Proba, order=order)

    X = np.array(TERMS).T

    reg = linear_model.LinearRegression(fit_intercept=False, normalize=False)
    reg.fit(X, Vthre_eff)  # FITTING
    COEFFS = reg.coef_

    reg = linear_model.Ridge(alpha=.9, fit_intercept=False, normalize=False)
    reg.fit(X, Vthre_eff)  # FITTING
    COEFFS = reg.coef_

    if fit_filename is not None:
        np.save(fit_filename, COEFFS)

    return COEFFS
def get_full_statistical_quantities(
    Model,
    DYN_SYSTEM={
        'RecExc': {
            'aff_pops': ['AffExc'],
            'aff_pops_input_values': [1.],
            'x0': 1.
        },
        'RecInh': {
            'aff_pops': ['AffExc'],
            'aff_pops_input_values': [1.],
            'x0': 1.
        }
    }):
    """
    we use the x0 point to calculate the satistical values, 
    so consider the replace_x0 options in find_FP or run_dyn_system
    """

    DYN_KEYS = [key for key in DYN_SYSTEM.keys()
                ]  # for a quick access to the dynamical system variables

    # initialize neuronal and synaptic params (very likely already done)
    for key in DYN_KEYS:
        DYN_SYSTEM[key]['nrn_params'] = built_up_neuron_params(Model, key)
        DYN_SYSTEM[key]['syn_input'] = build_up_afferent_synaptic_input(
            Model, DYN_KEYS + DYN_SYSTEM[key]['aff_pops'], key)

    output, RATES = {}, {}
    for key in DYN_KEYS:
        RATES['F_' + key] = DYN_SYSTEM[key][
            'x0']  # we use x0 as the activity point to compute
    for key in DYN_KEYS:
        for aff_key, x in zip(DYN_SYSTEM[key]['aff_pops'],
                              DYN_SYSTEM[key]['aff_pops_input_values']):
            RATES['F_' + aff_key] = x
        output['muV_'+key], output['sV_'+key],\
            output['gV_'+key], output['Tv_'+key],\
            output['Isyn_'+key] = getting_statistical_properties(
                                                              DYN_SYSTEM[key]['nrn_params'],
                                                              DYN_SYSTEM[key]['syn_input'],
                                                              RATES, already_SI=False, with_Isyn=True)
    return output
Esempio n. 3
0
def get_full_statistical_quantities(Model,
                                    DYN_SYSTEM,
                                    RATES,
                                    CURRENT_INPUTS={}):
    """
    DYN_SYSTEM = {'RecExc': {'aff_pops':['AffExc'], 'x0':1.},
                  'RecInh': {'aff_pops':['AffExc'], 'x0':1.}}    
    RATES = {'F_AffExc':1.,
             'F_RecExc':1.,
             'F_RecInh':1.}
    """

    DYN_KEYS = [key for key in DYN_SYSTEM.keys()
                ]  # for a quick access to the dynamical system variables

    # initialize neuronal and synaptic params (very likely already done)
    for key in DYN_KEYS:
        DYN_SYSTEM[key]['nrn_params'] = built_up_neuron_params(Model, key)
        DYN_SYSTEM[key]['syn_input'] = build_up_afferent_synaptic_input(
            Model, DYN_KEYS + DYN_SYSTEM[key]['aff_pops'], key)

    # output, RATES = {}, {}
    # for key in DYN_KEYS:
    #     RATES['F_'+key] = DYN_SYSTEM[key]['x0'] # we use x0 as the activity point to compute
    output = {}
    for key in DYN_KEYS:
        current_input = 0.
        if key in CURRENT_INPUTS:
            current_input = CURRENT_INPUTS[key]

        output['muV_'+key], output['sV_'+key],\
            output['gV_'+key], output['Tv_'+key],\
            output['Isyn_'+key] = getting_statistical_properties(
                DYN_SYSTEM[key]['nrn_params'],
                DYN_SYSTEM[key]['syn_input'],
                RATES,
                current_input=current_input,
                already_SI=False, with_Isyn=True)
    return output
Esempio n. 4
0
def solve_mean_field_first_order(
        Model,
        DYN_SYSTEM={
            'RecExc': {
                'aff_pops': ['AffExc'],
                'x0': 1.
            },
            'RecInh': {
                'aff_pops': ['AffExc'],
                'x0': 1.
            }
        },
        INPUTS={
            'AffExc_RecExc': np.ones(1000),
            'AffExc_RecInh': np.ones(1000)
        },
        CURRENT_INPUTS={},
        dt=1e-4,
        tstop=.1,
        T=5e-3,
        replace_x0=False,
        verbose=True):
    """
    

    if replace_x0 -> then you can directly use get_stat_props
    """
    DYN_KEYS = [key for key in DYN_SYSTEM.keys()
                ]  # for a quick access to the dynamical system variables

    # initialize neuronal and synaptic params
    for key in DYN_KEYS:
        DYN_SYSTEM[key]['nrn_params'] = built_up_neuron_params(Model, key)
        DYN_SYSTEM[key]['syn_input'] = build_up_afferent_synaptic_input(Model,\
                                                        DYN_KEYS+DYN_SYSTEM[key]['aff_pops'], key,
                                                        verbose=verbose)

    # --- CONSTRUCT THE DIFFERENTIAL OPERATOR --- #
    def dX_dt(X, t, dt, DYN_KEYS, DYN_SYSTEM, INPUTS, CURRENT_INPUTS):
        dX_dt, RATES = [], {}
        # we fill the X-defined recurent act:
        for x, key in zip(X, DYN_KEYS):
            RATES['F_' + key] = x
        # then we compute it, key by key
        for i, key in enumerate(DYN_KEYS):
            for aff_key in DYN_SYSTEM[key]['aff_pops']:
                RATES['F_'+aff_key] = INPUTS[aff_key+'_'+key][\
                                        min([int(t/dt), len(INPUTS[aff_key+'_'+key])-1]) ]
            if key in CURRENT_INPUTS:
                current_input = CURRENT_INPUTS[key][min(
                    [int(t / dt), len(CURRENT_INPUTS[key]) - 1])]
            else:
                current_input = 0
            Fout = input_output(DYN_SYSTEM[key]['nrn_params'],
                                DYN_SYSTEM[key]['syn_input'],
                                RATES,
                                Model['COEFFS_' + key],
                                current_input=current_input)
            dX_dt.append((Fout - X[i]) / T)  # Simple one-dimensional framework
        return dX_dt

    # ------------------------------------------- #

    # starting point
    X0 = []
    for key in DYN_KEYS:
        X0.append(DYN_SYSTEM[key]['x0'])

    if verbose:
        print('running ODE integration [...]')
        start_time = time.time()
    X = odeint(dX_dt,
               X0,
               np.arange(int(tstop / dt)) * dt,
               args=(dt, DYN_KEYS, DYN_SYSTEM, INPUTS, CURRENT_INPUTS))
    if verbose:
        print("--- ODE integration took %.1f seconds ---" %
              (time.time() - start_time))

    output = {}
    for key, x in zip(DYN_KEYS, X.T):
        output[key] = x
        if replace_x0:
            DYN_SYSTEM[key]['x0'] = x[-1]
    return output
Esempio n. 5
0
    def build_TF_func(self, Ngrid=20,
                      coeffs_location='data/COEFFS_pyrExc.npy',
                      with_Vm_functions=False,
                      pop=None,
                      Exc_lim=[0.01,1000], Inh_lim=[0.01, 1000], sampling='log',
                      EXC_VALUE_THRESHOLD=10.):
        """
        """

        print('Initializing simulation [...]')
        if pop is None:
            pop = self.REC_POPS[0]
            
        # taking just one Exc and One Inh pop for the scan !!
        Exc_pop = [rec for rec in self.REC_POPS if len(rec.split('Exc'))>1][0]
        Inh_pop = [rec for rec in self.REC_POPS if len(rec.split('Inh'))>1][0]
        
        # building artificial simulation situation (with just one exc and one inh)
        AFF_POPS = [Exc_pop, Inh_pop]
        Model2 = self.Model.copy()
        Model2['N_%s'%Exc_pop], Model2['N_%s'%Inh_pop] = 10, 10
        Model2['p_%s_%s'%(Exc_pop, pop)], Model2['p_%s_%s'%(Inh_pop, pop)] = 0.1, 0.1
        
        nrn_params = built_up_neuron_params(Model2, pop)
        syn_input = build_up_afferent_synaptic_input(Model2,
                                                          AFF_POPS, pop)
        Model2['COEFFS'] = np.load(coeffs_location)
        if sampling=='log':
            Freq_Exc = np.logspace(*np.log10(Exc_lim), Ngrid+1)
            Freq_Inh = np.logspace(*np.log10(Inh_lim), Ngrid)
        else:
            Freq_Exc = np.linspace(*Exc_lim, Ngrid+1)
            Freq_Inh = np.linspace(*Inh_lim, Ngrid)

        Ioscill = np.linspace(0, 20*10, int(Ngrid/2))

        output_freq = np.zeros((len(Freq_Exc), len(Freq_Inh), len(Ioscill)))
        
        if with_Vm_functions:
            mean_Vm = np.zeros((len(Freq_Exc), len(Freq_Inh), len(Ioscill)))
            std_Vm = np.zeros((len(Freq_Exc), len(Freq_Inh), len(Ioscill)))
            gamma_Vm = np.zeros((len(Freq_Exc), len(Freq_Inh), len(Ioscill)))

        print('Performing grid simulation [...]')
        for i, j, k in itertools.product(range(len(Freq_Exc)), range(len(Freq_Inh)),
                                         range(len(Ioscill))):
            if Freq_Exc[i]<EXC_VALUE_THRESHOLD:
                output_freq[i,j,k] = 0
            else:
                output_freq[i,j,k] = input_output(nrn_params, syn_input,
                                                  {'F_%s'%Exc_pop:Freq_Exc[i], 'F_%s'%Inh_pop:Freq_Inh[j]},
                                                  Model2['COEFFS'],
                                                  current_input=Ioscill[k])
            if with_Vm_functions:
                mean_Vm[i,j,k], std_Vm[i,j,k], _, _ =  getting_statistical_properties(nrn_params, syn_input,
                                                                                      {'F_%s'%Exc_pop:Freq_Exc[i], 'F_%s'%Inh_pop:Freq_Inh[j]},
                                                                                      current_input=Ioscill[k])
                
        print('Building interpolation [...]')
        self.TF_func = RegularGridInterpolator([Freq_Exc*\
                                                Model2['p_%s_%s'%(Exc_pop, pop)]*Model2['N_%s'%Exc_pop],
                                                Freq_Inh*\
                                                Model2['p_%s_%s'%(Inh_pop, pop)]*Model2['N_%s'%Inh_pop],
                                                Ioscill],
                                               output_freq,
                                               method='linear',
                                               fill_value=None, bounds_error=False)
        if with_Vm_functions:
            self.mean_Vm_func = RegularGridInterpolator([Freq_Exc*\
                                                         Model2['p_%s_%s'%(Exc_pop,pop)]*Model2['N_%s'%Exc_pop],
                                                         Freq_Inh*\
                                                         Model2['p_%s_%s'%(Inh_pop,pop)]*Model2['N_%s'%Inh_pop],
                                                         Ioscill],
                                                         mean_Vm,
                                                         method='linear',
                                                         fill_value=None, bounds_error=False)
            # self.std_Vm_func = RegularGridInterpolator([Freq_Exc*\
            #                                             Model2['p_%s_%s'%(Exc_pop, pop)]*Model2['N_%s'%Exc_pop],
            #                                             Freq_Inh*\
            #                                             Model2['p_%s_%s'%(Inh_pop, pop)]*Model2['N_%s'%Inh_pop],
            #                                             Ioscill],
            #                                             std_Vm,
            #                                             method='linear',
            #                                             fill_value=None, bounds_error=False)
        print('--> Done !')
Esempio n. 6
0
    def simulate_TF_func(self, Ngrid=20,
                         coeffs_location='data/COEFFS_pyrExc.npy',
                         tf_sim_file='tf_sim_points.npz',
                         with_Vm_functions=False,
                         pop=None,
                         Exc_lim=[0.01,1000],
                         Inh_lim=[0.01, 1000],
                         Iinj_lim=None,
                         sampling='log',
                         EXC_VALUE_THRESHOLD=10.):
        """
        """

        print('Initializing simulation [...]')
        if pop is None:
            pop = self.REC_POPS[0]
            
        # taking just one Exc and One Inh pop for the scan !!
        Exc_pop = [rec for rec in self.REC_POPS if len(rec.split('Exc'))>1][0]
        Inh_pop = [rec for rec in self.REC_POPS if len(rec.split('Inh'))>1][0]
        
        # building artificial simulation situation (with just one exc and one inh)
        AFF_POPS = [Exc_pop, Inh_pop]
        Model2 = copy.deepcopy(self.Model)
        Model2['N_%s'%Exc_pop], Model2['N_%s'%Inh_pop] = 10, 10
        Model2['p_%s_%s'%(Exc_pop, pop)], Model2['p_%s_%s'%(Inh_pop, pop)] = 0.1, 0.1
        
        nrn_params = built_up_neuron_params(Model2, pop)
        syn_input = build_up_afferent_synaptic_input(Model2,
                                                     AFF_POPS, pop)
        Model2['COEFFS'] = np.load(coeffs_location)

        Freq_Exc, Freq_Inh, Iinj = self.build_TF_inputs(Exc_lim, Inh_lim, Iinj_lim, sampling, Ngrid)

        output_freq = np.zeros((len(Freq_Exc), len(Freq_Inh), len(Iinj)))
        
        if with_Vm_functions:
            mean_Vm, std_Vm, gamma_Vm = 0*output_freq, 0*output_freq, 0*output_freq

        print('Performing grid simulation [...]')
        for i, j, k in itertools.product(range(len(Freq_Exc)),
                                         range(len(Freq_Inh)),
                                         range(len(Iinj))):
            # if Freq_Exc[i]<EXC_VALUE_THRESHOLD:
            #     output_freq[i,j,k] = 0
            # else:
            output_freq[i,j,k] = input_output(nrn_params,
                                                  syn_input,
                                                  {'F_%s'%Exc_pop:Freq_Exc[i], 'F_%s'%Inh_pop:Freq_Inh[j]},
                                                  Model2['COEFFS'],
                                                  current_input=Iinj[k])
            if with_Vm_functions:
                mean_Vm[i,j,k], std_Vm[i,j,k], _, _ =  getting_statistical_properties(nrn_params, syn_input,
                                                                                      {'F_%s'%Exc_pop:Freq_Exc[i], 'F_%s'%Inh_pop:Freq_Inh[j]},
                                                                                      current_input=Iinj[k])

        np.savez(tf_sim_file, **{'output_freq':output_freq,
                                 'mean_Vm':mean_Vm, 'std_Vm':std_Vm,
                                 'sampling':sampling,
                                 'Ngrid':Ngrid,
                                 'Exc_lim':Exc_lim,
                                 'Inh_lim':Inh_lim,
                                 'Iinj_lim':Iinj_lim})