예제 #1
0
def plot_scatter_sot(master_encoder):
    file_dict = run_main_sim_gen.all_exec(filenames_only=True)
    keys = np.vstack((file_dict.keys()))

    priv_noise_lev = np.unique(keys[:,0])
    priv_tun_rat = np.unique(keys[:,1])
    inputs = np.unique(keys[:,2])

    #Metric map:
    metric_map = {}
    metric_map['path_length'] = [(1, 0), 'cm']
    metric_map['path_error'] = [(1, 1), 'cm']
    metric_map['time2targ'] = [(0, 0), 'sec']
    metric_map['avg_speed'] = [(0, 1), 'cm/sec']

    #Colormap for input type: 
    cmap_dict = dict(all='black', shared_scaled='cornflowerblue',private_scaled='maroon')
    major_dict = {}
    dx = .1/2

    n_samples = 600
    from riglib.bmi.state_space_models import StateSpaceEndptVel2D
    ssm = StateSpaceEndptVel2D()
    A, _, W = ssm.get_ssm_matrices()
    mean = np.zeros(A.shape[0])
    mean[-1] = 1
    state_samples = np.random.multivariate_normal(mean, W, n_samples)

    units = master_encoder.get_units()
    n_units = len(units)

    dat_dict = {}
    lamb_arr = np.array([0.1, 0.25, 0.5, 1., 2.5, 5., 10., 20.])

    spike_counts = np.zeros([n_units, n_samples])
    master_encoder.call_ds_rate = 1


    master_dict = {}

    #Create figures;
    
    for pnl in priv_noise_lev:
        f, ax = plt.subplots(nrows=2, ncols=2)
        for ptr in priv_tun_rat:

            tun = 1 - .15 - float(pnl)
            p_tun = float(ptr)*tun
            s_tun = tun - p_tun
            master_encoder.wt_sources = np.array([float(pnl), p_tun, .15, s_tun])

            for k in range(n_samples):
                spike_counts[:,k] = np.array(master_encoder(state_samples[k], mode='counts')).ravel()

            zscore_X, mu = pa.zscore_spks(spike_counts.T)
            log_lik, _ = pa.find_k_FA(zscore_X, iters=5, max_k = 20, plot=False)
            num_factors = 1+(np.argmax(np.mean(log_lik, axis=0)))
            FA = skdecomp.FactorAnalysis(n_components=num_factors)

            #Samples x features:
            FA.fit(zscore_X)
            Cov_Priv = np.sum(FA.noise_variance_)
            U = np.mat(FA.components_).T
            Cov_Shar = np.trace(U*U.T)
            sot = Cov_Shar/(Cov_Shar+Cov_Priv)
            master_dict[pnl, ptr, 'sot'] = sot 
            master_dict[pnl, ptr, 'nf'] = num_factors

            print 'SOT: ', sot, pnl, ptr

            inp_cnt = -1*(dx)

            for inp in inputs:
                inp_cnt += dx
                h5 = file_dict[float(pnl), float(ptr), inp]

                try:
                    mat = sio.loadmat(h5[:-4]+'_metrics.mat')
                except:
                    mat = dict()
                    for metric in metric_map.keys(): mat[metric] = np.array([0])
                
                for metric in metric_map.keys():
                    x = mat[metric]
                    axi = ax[metric_map[metric][0][0], metric_map[metric][0][1]]
                    axi.set_title('Priv Noise: '+pnl+', Met:'+metric)

                    mn = np.mean(x)
                    master_dict[pnl, ptr, inp, metric] = mn

                    sem = np.std(x)/np.sqrt(len(x))
                    #rect = axi.errorbar(sot, mn, sem, fmt='.', markersize=15, color=cmap_dict[inp])
                    rect = axi.plot(sot, mn, '.', markersize=15, color=cmap_dict[inp])
                    
                    #axi.errorbar(np.float(ptr)+inp_cnt, mn, sem, fmt='.', markersize=15, color=inp_dict[inp])
                    axi.set_ylabel(metric_map[metric][1])
                    if metric == 'path_error':
                        axi.legend(list(inputs),fontsize=8)
                        axi.set_xlabel('Shared over Total Fraction')
                    elif metric == 'path_length':
                        axi.set_xlabel('Shared over Total Fraction')
            
        plt.tight_layout()
    return master_dict
예제 #2
0
for lamb in lamb_arr:
    dat_dict[lamb, "nf"] = []
    dat_dict[lamb, "sot"] = []
    dat_dict[lamb, "nf_orth"] = []
    for n in range(number_of_reps):
        print lamb, " rep num: ", n
        encoder.lambda_spk_update = lamb
        spike_counts = np.zeros([n_units, n_samples])
        encoder.call_ds_rate = 1
        for k in range(n_samples):
            z = np.array(encoder(state_samples[k], mode="counts"))
            print k, state_samples[k], state_samples[k].shape, z.shape, z.ravel().shape
            spike_counts[:, k] = z.ravel()

        zscore_X, mu = pa.zscore_spks(spike_counts.T)
        log_lik, ax = pa.find_k_FA(zscore_X, iters=5, max_k=15, plot=False)
        num_factors = 1 + (np.argmax(np.mean(log_lik, axis=0)))

        FA = skdecomp.FactorAnalysis(n_components=num_factors)

        # Samples x features:
        FA.fit(zscore_X)
        Cov_Priv = np.sum(FA.noise_variance_)
        U = np.mat(FA.components_).T
        Cov_Shar = np.trace(U * U.T)
        sot = Cov_Shar / (Cov_Shar + Cov_Priv)

        A = U * U.T
        u, s, v = np.linalg.svd(A)
        s_red = np.zeros_like(s)
        s_hd = np.zeros_like(s)
예제 #3
0
def targ_vs_all_subspace_align(te_list, file_name=None, cycle_FAs=None, epoch_size=50,
    compare_to_orig_te = False):
    '''
    Summary: function that takes a task entry list, parses each task entry into epochs, and computes
        a factor analysis model for each epoch using (1:cycle_FAs = number of factors), and then either
        saves or returns the output and a modified task list

    Input param: te_list: list of task entries (not tiered)
    Input param: file_name: name to save overlap data to afterwards, if None, then returns args
    Input param: cycle_FAs: number of factors to cycle through (1:cycle_FAs) -- uses optimal number if None
    Input param: epoch_size: size of epochs to parse task entries into (uses ONLY reward trials)
    Input param: compare_to_orig_te: if True, then also compares all of the compute FAs to the original 
        FA model in the hdf file of the task entry listed in 'compare to original te'

    Output param: 
    '''

    fa_dict = {}
    te_list = np.array(te_list)
    te_mod_list = []

    #assume no more than 10 FA epochs per task entry
    mod_increment = 0.01
    increment_offs = 0.1
    #For each TE get the right FA model: 
    for te in te_list:

        t = dbfn.TaskEntry(te)
        hdf = t.hdf

        #Now get the right FA model: 
        drives_neurons_ix0 = 3
        rew_ix_total = pa.get_trials_per_min(hdf)

        #Epoch rew_ix into epochs of 'epoch_size' 
        internal_state = hdf.root.task[:]['internal_decoder_state']
        update_bmi_ix = np.nonzero(np.diff(np.squeeze(internal_state[:, drives_neurons_ix0, 0])))[0]+1
        more_epochs = 1
        cnt = 0

        while more_epochs:

            #If not enough trials, still use TE, or if not epoch size specified
            if (epoch_size is None) or (len(rew_ix_total) < epoch_size):
                bin_spk, targ_pos, targ_ix, z, zz = pa.extract_trials_all(hdf, rew_ix_total, update_bmi_ix=update_bmi_ix)
                more_epochs = 0
                te_mod = float(te)

            else:
                if (cnt+1)*epoch_size <= len(rew_ix_total):
                    rew_ix = rew_ix_total[cnt*epoch_size:(cnt+1)*epoch_size]
                    if len(rew_ix) != epoch_size:
                        print moose
                    #Only use rew indices for that epoch:
                    bin_spk, targ_pos, targ_ix, z, zz = pa.extract_trials_all(hdf, rew_ix, time_cutoff=1000, update_bmi_ix=update_bmi_ix)
                    te_mod = float(te) + (mod_increment*cnt) + increment_offs
                    cnt += 1

                    #Be done if next epoch won't have enought trials
                    if (cnt+1)*epoch_size > len(rew_ix_total):
                        print (cnt+1)*epoch_size, len(rew_ix_total)
                        more_epochs = 0
    
            #Add modified te to dictionary
            te_mod_list.append(te_mod)

            #Use the bin_spk from above
            zscore_X, mu = pa.zscore_spks(bin_spk)
            n_neurons = zscore_X.shape[1]
            #If no assigned number of factors to cycle through, find optimal
            if cycle_FAs is None:
                log_lik, ax = pa.find_k_FA(zscore_X, iters=10, max_k = n_neurons, plot=False)

                mn_log_like = np.mean(log_lik, axis=0)
                ix = np.argmax(mn_log_like)
                num_factors = ix + 1

                FA_full = skdecomp.FactorAnalysis(n_components = num_factors)
                FA_full.fit(zscore_X)
                fa_dict[te, 0] = FA_full
                fa_dict['units', te] = t.decoder.units

            else:
                for i in np.arange(1, cycle_FAs+1):
                    FA = skdecomp.FactorAnalysis(n_components = i)
                    FA.fit(zscore_X)
                    fa_dict[te_mod, i] = FA
    
    #Now FA dict is completed:
    print 'now computing overlaps', te_mod_list
    
    if file_name is None:
        d, te_mod = ss_overlap(cycle_FAs, te_mod_list, fa_dict, file_name=file_name, 
            compare_to_orig_te=compare_to_orig_te)
        return d, te_mod

    else:
        ss_overlap(cycle_FAs, te_mod_list, fa_dict, file_name=file_name, 
            compare_to_orig_te=compare_to_orig_te)