Esempio n. 1
0
def fit_FA_w_init(factors=5,ntrials=100, hdf=None):
    rew_ix = pa.get_trials_per_min(hdf)
    bin_spk, targ_pos, targ_ix, trial_ix, reach_time, hdf_ix = pa.extract_trials_all(hdf, 
        rew_ix, hdf_ix=True)

    if ntrials=='max':
        ix = np.arange(len(trial_ix))
    else:
        ix = np.nonzero(trial_ix<=ntrials)[0]
    FA = skdecomp.FactorAnalysis(n_components=factors)
    zsc, mu = pa.zscore_spks(bin_spk[ix,:])
    FA.fit(bin_spk[ix,:])
    return FA, mu, trial_ix, hdf_ix, bin_spk
Esempio n. 2
0
def fit_FA_by_targ(factors=5, ntrials='max', hdf=None):
    rew_ix = pa.get_trials_per_min(hdf)
    bin_spk, targ_pos, targ_ix, trial_ix, reach_time, hdf_ix = pa.extract_trials_all(hdf, 
        rew_ix, hdf_ix=True)

    if ntrials=='max':
        ix_cutoff = len(trial_ix)
    else:
        ix_cutoff = len(np.nonzero(trial_ix<=ntrials)[0])

    FA_dict = dict()
    mu_dict = dict()
    for tg in np.unique(targ_ix):
        tg_ix = np.nonzero(targ_ix==tg)[0]
        tg_ix = tg_ix[tg_ix<ix_cutoff]
        FA = skdecomp.FactorAnalysis(n_components=factors)
        zsc, mu = pa.zscore_spks(bin_spk[tg_ix,:])
        FA.fit(bin_spk[tg_ix,:])

        FA_dict[str(tg)] = FA
        mu_dict[str(tg)] = mu
        print 'done with target: ', str(tg)

    return FA_dict, mu_dict, trial_ix, hdf_ix, bin_spk, targ_ix
Esempio n. 3
0
suff = "_rev2_"
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)
Esempio n. 4
0
def sliding_sot(te_num, fname, epoch_time=2., epoch_step = .5, test_n_fact=6, 
    drives_neurons_ix0=3, rew_only=False):

    te = dbfn.TaskEntry(te_num)

    #Include ALL neural data 
    hdf = te.hdf
    epoch_bins = np.arange(0., len(hdf.root.task), epoch_step*60.*60.)

    #Get hdf_task_msgs
    if rew_only:
        msg_list = ['reward']

    else:
        msg_list = ['reward','timeout_penalty','hold_penalty','obstacle_penalty']

    trial_ix = np.array([i for i in hdf.root.task_msgs[:] if i['msg'] in msg_list ], 
        dtype=hdf.root.task_msgs.dtype)

    trial_ix_cnt = np.array([j for j, i in enumerate(hdf.root.task_msgs[:]) if i['msg'] in msg_list ])

    step_dict = dict(reward=3, hold_penalty=2, timeout_penalty=1, obstacle_penalty=1)

    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

    sot_ratio = {}
    fa_dict = {}
    #Get all trials activity for epoch:
    for ie, e_start in enumerate(epoch_bins[:-1]):
        e_end = e_start + (epoch_time*60*60)

        epoch_ix = np.nonzero(np.logical_and(trial_ix['time'] <= e_end, trial_ix['time'] >= e_start))[0]
         
        for e in epoch_ix:
            #Step backwards to the go cue: 
            trl_end = trial_ix[e]['time']
            start = hdf.root.task_msgs[trial_ix_cnt[e] - step_dict[trial_ix[e]['msg']]]
            if start['msg'] != 'target':
                print 'errr!'
            else:
                trl_start = start['time']
            
            spk_unbinned = hdf.root.task[trl_start:trl_end]['spike_counts']
            
            spk_binned = []
            spk = np.zeros_like(hdf.root.task[0]['spike_counts'])
            for i_t, t in enumerate(np.arange(trl_start, trl_end)):

                spk += spk_unbinned[i_t,:,:]
                if t in update_bmi_ix:
                    spk_binned.append(spk)
                    spk = np.zeros_like(hdf.root.task[0]['spike_counts'])

        # Neurons x time:
        spk_epoch = np.hstack(spk_binned)
        zscore_X, mu = pa.zscore_spks(spk_epoch.T) #Zscore in time x neurons
        for n_factors in range(test_n_fact):
            FA = skdecomp.FactorAnalysis(n_components=n_factors)
            FA.fit(zscore_X)
            fa_dict[ie, n_factors] = FA
            
            Cov_Priv = np.sum(FA.noise_variance_)
            U = np.mat(FA.components_).T
            Cov_Shar = np.trace(U*U.T)
            try:
                sot_ratio[n_factors].append(Cov_Shar/(Cov_Shar+Cov_Priv))            
            except:
                sot_ratio[n_factors] = [Cov_Shar/(Cov_Shar+Cov_Priv)]

    if hasattr(hdf.root, 'fa_params'):
        fa = FA_faux(hdf.root.fa_params)
        fa_dict[te_num] = fa
        compare_to_orig_te = te_num
    else:
        compare_to_orig_te = None

    if fname is None:
        return sot_ratio, fa_dict

    else:
        fa_dict['sot_ratio'] = sot_ratio
        subspace_overlap.ss_overlap(test_n_fact, range(len(epoch_bins[:-1])), fa_dict, 
            file_name=fname[:-4]+'_sso_.pkl', compare_to_orig_te=compare_to_orig_te)

        f = open(fname, 'wb')
        pickle.dump(fa_dict, f)
        f.close()

        print fname
        print fname[:-4]+'_sso_.pkl'
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
Esempio n. 6
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)
rew_ix = pa.get_trials_per_min(vfb.hdf)
vfb_bin_spk = sfv.get_vfb_spk(vfb, rew_ix, train_data.decoder.units)


train_param = ['vfb']+range(8,64,8)

LL = np.zeros((len(train_param), len(n_factors), n_reps))

for it, tr in enumerate(train_param):
    if tr == 'vfb':
        train_bin_spk = vfb_bin_spk.copy()
    else:
        cutoff = train_rew_ix[tr+1]/60./60. # in minutes
        train_bin_spk, targ_pos, targ_ix, z, zz = pa.extract_trials_all(train_data.hdf, train_rew_ix, time_cutoff=cutoff)

    zscore_X_train, mu = pa.zscore_spks(train_bin_spk)
    zscore_X_test = test_bin_spk - np.tile(mu[0,:,np.newaxis].T, [test_bin_spk.shape[0], 1])
    
    for n, num in enumerate(n_factors):
        for i in range(n_reps):
            FA = skdecomp.FactorAnalysis(n_components=num)
            FA.fit(zscore_X_train)
            LL[it, n, i] = FA.score(zscore_X_test)

f, ax = plt.subplots(nrows=len(n_factors))
for i, axi in enumerate(ax):
    yerr = np.std(LL[:,i,:], axis=1)
    axi.errorbar(np.arange(0,64,8), np.mean(LL[:,i,:], axis=1), yerr=yerr)
    axi.set_title('N Factors: '+str(n_factors[i]))    
axi.set_xlabel('Num trials used in BMI calib (0 = VFB)')