Ejemplo n.º 1
0
def cp_als_test():
    # Create synthetic dataset.
    I, J, K, R = 25, 25, 25, 3  # dimensions and rank parameters
    # Create a random tensor consisting of a low-rank component and noise.
    X = tensortools.randn_ktensor((I, J, K), rank=R).full()
    X += np.random.randn(I, J, K)  # add some random noise

    # Perform CP tensor decomposition.
    U = tensortools.cp_als(X, rank=R, verbose=True)
    V = tensortools.cp_als(X, rank=R, verbose=True)

    # Compare the low-dimensional factors from the two fits.
    fig, _, _ = tensortools.plot_factors(U.factors)
    tensortools.plot_factors(V.factors, fig=fig)

    # Align the two fits and print a similarity score.
    similarity_score = tensortools.kruskal_align(U.factors,
                                                 V.factors,
                                                 permute_U=True,
                                                 permute_V=True)
    print(similarity_score)

    # Plot the results to see alignment.
    fig, ax, po = tensortools.plot_factors(U.factors)
    tensortools.plot_factors(V.factors, fig=fig)

    plt.show()
Ejemplo n.º 2
0
def tca(X,
        non_neg=True,
        R=10,
        prefix="",
        max_iter=500,
        epoc="all",
        effect="all"):

    # Fit CP tensor decomposition (two times).
    U = None
    V = None
    opti_str = 'ncp_bcd_'
    if non_neg:
        U = tt.ncp_bcd(X, rank=R, verbose=True, max_iter=max_iter, tol=1e-6)
        V = tt.ncp_bcd(X, rank=R, verbose=True, max_iter=max_iter, tol=1e-6)
    else:
        U = tt.cp_als(X, rank=R, verbose=True, max_iter=max_iter, tol=1e-6)
        V = tt.cp_als(X, rank=R, verbose=True, max_iter=max_iter, tol=1e-6)
        opti_str = 'cp_als_'
    # Compare the low-dimensional factors from the two fits.
    # fig, ax, po = tt.plot_factors(U.factors)
    # tt.plot_factors(V.factors, fig=fig)
    # fig.suptitle("raw models")
    # fig.tight_layout()

    # Align the two fits and print a similarity score.
    sim = tt.kruskal_align(U.factors,
                           V.factors,
                           permute_U=True,
                           permute_V=True)
    print(sim)

    # Plot the results again to see alignment.
    fig, ax, po = tt.plot_factors(U.factors,
                                  plots=["scatter", "scatter", "line"])
    tt.plot_factors(V.factors, plots=["scatter", "scatter", "line"], fig=fig)
    [x.set_xticks([11.5, 15.5, 27.5, 31.5]) for x in ax[:, 2]]

    ax[-1, 0].set_xlabel("SU #")
    ax[-1, 1].set_xlabel("Trial #")
    ax[-1, 2].set_xlabel("Time (s)")
    ax[-1, 2].set_xticklabels(["S", "+1", "T", "+1"])

    fig.suptitle("aligned models")
    fig.tight_layout()

    # Show plots.
    plt.show()
    fig.set_size_inches(40, 40)
    fig.set_dpi(300)
    fig.savefig(
        opti_str + prefix + "tca_trial_" + epoc + "_" + effect + "_" +
        str(X.shape[1]) + "_R" + str(R) + ".png",
        dpi=300,
        bbox_inches="tight",
    )
    return (U, V, sim)
Ejemplo n.º 3
0
def plotTCs(activity_tensor, targets, R, title=''):
    U = tt.cp_als(activity_tensor, rank=R, verbose=False)
    tt.plot_factors(U.factors, targets, plots=['scatter', 'line', 'bar'])
    plt.title(title)
    #plt.show()
    # plt.savefig('tensorFactors.pdf')
    #return neuron factors
    return U.factors[2][:,0]
Ejemplo n.º 4
0
def run_tensor(f_behavior,
               f_ephys,
               smooth_method='both',
               smooth_width=[80, 40],
               pad=[400, 400],
               z_score=True,
               trial_duration=None,
               max_duration=5000,
               min_rate=0.1,
               n_components=12,
               epoch=None):
    ##get the data
    X, trial_data = ptr.get_trial_spikes(f_behavior,
                                         f_ephys,
                                         smooth_method=smooth_method,
                                         smooth_width=smooth_width,
                                         pad=pad,
                                         z_score=z_score,
                                         trial_duration=trial_duration,
                                         max_duration=max_duration,
                                         min_rate=min_rate)
    ##reshape X to be neurons x timepoints x trials
    X = X.transpose(1, 2, 0)
    ##take a specific window, if requested
    if epoch == 'action':
        if smooth_method == 'bins':
            X = X[:, :int(pad[0] / smooth_width), :]
        elif smooth_method == 'both':
            X = X[:, :int(pad[0] / smooth_width[1]), :]
        else:
            X = X[:, :int(pad[0]), :]
    if epoch == 'outcome':
        if smooth_method == 'bins':
            X = X[:, int(-pad[1] / smooth_width):, :]
        elif smooth_method == 'both':
            X = X[:, int(-pad[1] / smooth_width[1]):, :]
        else:
            X = X[:, int(-pad[1]):, :]
    ##now fit the model
    model, info = tt.cp_als(X, n_components, nonneg=False, tol=1e-5)
    print('Final reconstruction error : {}'.format(info['err_hist'][-1]))
    return model, info, trial_data
for x in dir_list:
    file_list = file_list + glob.glob(x + '/**/' + '*.h5',recursive=True)

file  = 4

this_dir = file_list[file].split(sep='/')[-2]
data_dir = os.path.dirname(file_list[file])
data = ephys_data(data_dir = data_dir ,file_id = file, use_chosen_units = True)
data.firing_rate_params = dict(zip(['step_size','window_size','total_time','calc_type','baks_len'],
                               [25,250,7000,'conv',700]))
data.get_data()
data.get_firing_rates()
data.get_normalized_firing()
data.firing_overview('off')

all_firing_array = np.asarray(data.all_normal_off_firing)

#taste = 2
X = all_firing_array.swapaxes(1,2) #data.all_normal_off_firing[:,:,100:200].swapaxes(1,2) #
rank = 7

# Fit CP tensor decomposition (two times).
U = tt.cp_als(X, rank=rank, verbose=True)

# Compare the low-dimensional factors from the two fits.
fig, _, _ = tt.plot_factors(U.factors)

## We should be able to see differences in tastes by using distance matrices on trial factors
trial_factors = U.factors.factors[-1]
trial_distances = dist_mat(trial_factors,trial_factors)
plt.figure();plt.imshow(trial_distances)
Ejemplo n.º 6
0
def tensor_PCA(data):
    n = 1
    fig = plt.figure(1)
    dm = data['DM']
    data = data['Data']
    predictors_correlated = []
    for s, session in enumerate(data):
        # Fit CP tensor decomposition (two times).
        DM = dm[s]
        R = 20

        U = tt.cp_als(session, rank=R, verbose=True)
        #V = tt.cp_als(session, rank=R, verbose=True)

        # Compare the low-dimensional factors from the two fits.
        #fig, ax, po = tt.plot_factors(U.factors)
        #tt.plot_factors(V.factors, fig=fig)
        #fig.suptitle("raw models")
        #fig.tight_layout()

        # Align the two fits and print a similarity score.
        #sim = tt.kruskal_align(U.factors, V.factors, permute_U=True, permute_V=True)
        #print(sim)

        # Plot the results again to see alignment.
        #fig, ax, po = tt.plot_factors(U.factors)
        #tt.plot_factors(V.factors, fig=fig)
        #fig.suptitle("aligned models")
        #fig.tight_layout()

        # Show plots.
        #plt.show()

        trial_factors = U.factors.factors[0]
        state = DM[:, 0]
        choices = DM[:, 1]
        reward = DM[:, 2]
        block = DM[:, 4]
        block_df = np.diff(block)

        correct_choice = np.where(choices == state)[0]
        correct = np.zeros(len(choices))
        correct[correct_choice] = 1

        a_since_block = []
        trials_since_block = []
        t = 0
        session_trials_since_block = []

        for st, s in enumerate(block):
            if state[st - 1] != state[st]:
                t = 0
            else:
                t += 1
            trials_since_block.append(t)

        session_trials_since_block.append(trials_since_block)

        t = 0
        for st, (s, c) in enumerate(zip(block, choices)):
            if state[st - 1] != state[st]:
                t = 0
                a_since_block.append(t)

            elif c == 1:
                t += 1
                a_since_block.append(t)
            else:
                a_since_block.append(0)

        negative_reward_count = []
        rew = 0
        block_df = np.append(block_df, 0)
        for r, b in zip(reward, block_df):

            if r == 0:
                rew += 1
                negative_reward_count.append(rew)
            elif r == 1:
                rew -= 1
                negative_reward_count.append(rew)
            if b != 0:
                rew = 0

        positive_reward_count = []
        rew = 0
        for r, b in zip(reward, block_df):

            if r == 1:
                rew += 1
                positive_reward_count.append(rew)
            elif r == 0:
                rew += 0
                positive_reward_count.append(rew)
            if b != 0:
                rew = 0
        positive_reward_count = np.asarray(positive_reward_count)
        negative_reward_count = np.asarray(negative_reward_count)
        choices_int = np.ones(len(reward))

        choices_int[np.where(choices == 0)] = -1
        reward_choice_int = choices_int * reward
        interaction_trial_latent = trials_since_block * state
        interaction_a_latent = a_since_block * state
        int_a_reward = a_since_block * reward

        interaction_trial_choice = trials_since_block * choices_int
        reward_trial_in_block = trials_since_block * positive_reward_count
        negative_reward_count_st = negative_reward_count * correct
        positive_reward_count_st = positive_reward_count * correct
        negative_reward_count_ch = negative_reward_count * choices
        positive_reward_count_ch = positive_reward_count * choices

        predictors_all = OrderedDict([
            ('Reward', reward), ('Choice', choices), ('Correct', correct),
            ('A in Block', a_since_block),
            ('A in Block x Reward', int_a_reward), ('State', state),
            ('Trial in Block', trials_since_block),
            ('Interaction State x Trial in Block', interaction_trial_latent),
            ('Interaction State x A count', interaction_a_latent),
            ('Choice x Trials in Block', interaction_trial_choice),
            ('Reward x Choice', reward_choice_int),
            ('No Reward Count in a Block', negative_reward_count),
            ('No Reward x Correct', negative_reward_count_st),
            ('Reward Count in a Block', positive_reward_count),
            ('Reward Count x Correct', positive_reward_count_st),
            ('No reward Count x Choice', negative_reward_count_ch),
            ('Reward Count x Choice', positive_reward_count_ch),
            ('Reward x Trial in Block', reward_trial_in_block)
        ])
        p = list(predictors_all.items())
        for t in range(trial_factors.shape[1]):

            for i in range(len(p)):
                corr = np.corrcoef(trial_factors[:, t], p[i][1])
                if abs(corr[1, 0]) > 0.65:
                    predictors_correlated.append(
                        (str(abs(corr[1, 0])) + ' ' + p[i][0]))
                    col = list(mcolors.CSS4_COLORS.keys())
                    col = np.asarray(col)
                    if 'x' in p[i][0]:
                        color = []
                        for ii, value in enumerate(p[i][1]):
                            value = np.int(value)
                            color.append(col[value])

                    else:
                        color_ind = np.asarray(np.where(p[i][1] == 0)[0])
                        color = np.asarray(
                            ['green' for i in range(len(p[i][1]))])
                        color[color_ind] = 'black'

                    #fig.add_subplot(5,6,n)
                    #n +=1

                    #plt.scatter(np.arange(len(trial_factors[:,t])),trial_factors[:,t], c = color, s = 1,label=color)
                    #plt.title(p[i][0])

                    if 'x Trial' in p[i][0]:
                        plt.figure()

                        plt.scatter(np.arange(len(trial_factors[:, t])),
                                    trial_factors[:, t],
                                    c=color,
                                    s=15,
                                    label=color)
                        plt.vlines(np.where(block_df != 0)[0],
                                   ymin=min(trial_factors[:, t]),
                                   ymax=max(trial_factors[:, t]),
                                   alpha=0.5)
                        x = trial_factors[:, t]
                        col_x = color
                        state_tial = p[i][1]
                        plt.title(p[i][0])

    fig.tight_layout()
    return predictors_correlated, x, col_x, state_tial
Created on Mon Jan 21 13:07:43 2019

@author: abuzarmahmood
"""

import tensortools as tt
import numpy as np
import matplotlib.pyplot as plt

# Make synthetic dataset.
I, J, K, R = 25, 25, 25, 4  # dimensions and rank
X = tt.randn_ktensor((I, J, K), rank=R).full()
X += np.random.randn(I, J, K)  # add noise

# Fit CP tensor decomposition (two times).
U = tt.cp_als(X, rank=R, verbose=True)
V = tt.cp_als(X, rank=R, verbose=True)

# Compare the low-dimensional factors from the two fits.
fig, _, _ = tt.plot_factors(U.factors)
tt.plot_factors(V.factors, fig=fig)

# Align the two fits and print a similarity score.
sim = tt.kruskal_align(U.factors, V.factors, permute_U=True, permute_V=True)
print(sim)

# Plot the results again to see alignment.
fig, ax, po = tt.plot_factors(U.factors)
tt.plot_factors(V.factors, fig=fig)

# Show plots.
                    this_dir + '/' + 'taste_' + str(taste)
        if not os.path.exists(plot_dir):
            os.makedirs(plot_dir)

        this_off = all_firing_array[taste, :, :, :]
        this_spikes = all_spikes_array[taste, :, :, :]

        # =============================================================================
        #         total_off = this_off[0,:,:]
        #         for nrn in range(1,this_off.shape[0]):
        #             total_off = np.concatenate((total_off,this_off[int(nrn),:,:]),axis=1)
        # =============================================================================

        # Tensor decomposition for trial clustering
        rank = 3  #X.shape[0]
        U = tt.cp_als(np.swapaxes(this_off, 1, 2), rank=rank, verbose=True)
        fig, ax, po = tt.plot_factors(U.factors)
        [nrn_f, time_f, trial_f] = U.factors.factors
        trial_dists = exposure.equalize_hist(dist_mat(trial_f, trial_f))

        #trial_dists = exposure.equalize_hist(dist_mat(total_off,total_off))

        # =============================================================================
        # reduced_off_pca = pca(n_components = 15).fit(total_off)
        # reduced_off = reduced_off_pca.transform(total_off)
        # trial_dists_red = dist_mat(reduced_off,reduced_off)
        # =============================================================================

        clf = kmeans(n_clusters=n_components, n_init=200)
        this_groups = clf.fit_predict(trial_dists)
#  \_____|_|\__,_|___/\__\___|_|       |_|_|  |_|\__,_|_|___/
#

# Clustering using tensor rank reduction
firing_inds = range(80,160)
lfp_inds = range(2000,4000)

taste = 0
this_firing_data = np.rollaxis(firing_array[taste,:,:,firing_inds],0,3) # nrn x trial x time
this_lfp_data = np.rollaxis(lfp_array[taste,:,lfp_inds],0,2)
this_spike_data = np.rollaxis(spikes_array[taste,:,:,lfp_inds],0,3)

rank = 7

# Fit CP tensor decomposition (two times).
U = tt.cp_als(this_firing_data.swapaxes(1,2), rank=rank, verbose=False)

# Compare the low-dimensional factors from the two fits.
fig, _, _ = tt.plot_factors(U.factors)

## We should be able to see differences in tastes by using distance matrices on trial factors
trial_factors = U.factors.factors[-1]
trial_distances = exposure.equalize_hist(dist_mat(trial_factors,trial_factors))
plt.figure();plt.imshow(trial_distances)

# Kmean clustering on trial factors
n_components = 4
clf = kmeans(n_clusters = n_components, n_init = 500)
this_groups = clf.fit_predict(trial_distances)

Ejemplo n.º 10
0
# =============================================================================
taste = 0
day3_data = day3_data[:, :, :, range(32)]
all_data = np.concatenate((day1_data, day3_data), axis=-1)
all_data_long = all_data[0, :, :, :]
for taste in range(1, all_data.shape[0]):
    all_data_long = np.concatenate((all_data_long, all_data[taste, :, :, :]),
                                   axis=-1)

# Fit CP tensor decomposition (two times).
rank = 10
repeats = 10
all_models = []
all_obj = []
for repeat in tqdm(range(repeats)):
    U = tt.cp_als(all_data_long, rank=rank, verbose=False)
    all_models.append(U)
    all_obj.append(U.obj)

U = all_models[np.argmin(all_obj)]

## We should be able to see differences in tastes by using distance matrices on trial factors
trial_factors = U.factors.factors[-1]
trial_distances = dist_mat(trial_factors, trial_factors)
plt.figure()
plt.imshow(exposure.equalize_hist(trial_distances))

# pca on trial factors and plot by taste
trial_labels = np.sort([0, 1, 2, 3, 4, 5] * 32)
reduced_trials_pca = pca(n_components=2).fit_transform(trial_factors)
plt.scatter(reduced_trials_pca[:, 0], reduced_trials_pca[:, 1], c=trial_labels)
Ejemplo n.º 11
0
from utils import *

time_factor = np.load("data/time_factor.npy")
neuron_factor = np.load("data/neuron_factor.npy")
trial_factor = np.load("data/trial_factor.npy")
latent = np.load("data/latent.npy")
observed = np.load("data/observed.npy")

# Specify the tensor, and the rank (np. of factors)
X, rank = observed, 3

# Perform CP decompositon using TensorLy
factors_tl = parafac(X, rank=rank)

# Perform CP decomposition using tensortools
U = tt.cp_als(X, rank=rank, verbose=False)
factors_tt = U.factors.factors

# Reconstruct M, with the result of each library
M_tl = reconstruct(factors_tl)
M_tt = reconstruct(factors_tt)

# plot the decomposed factors
plot_factors(factors_tl)
plot_factors(factors_tt)

def decompose_three_way(tensor, rank, max_iter=501, verbose=False):

    # a = np.random.random((rank, tensor.shape[0]))
    b = np.random.random((rank, tensor.shape[1]))
    c = np.random.random((rank, tensor.shape[2]))
Ejemplo n.º 12
0
import tensortools as tt
import numpy as np

# Make synthetic dataset.
I, J, K, R = 25, 25, 25, 4  # dimensions and rank
X = tt.randn_ktensor((I, J, K), rank=R).full()
X += np.random.randn(I, J, K)

# Fit CP tensor decomposition to first 20 trials.
U = tt.cp_als(X[:, :, :20], rank=R, verbose=True)

# Extend and re-initialize the factors along the final mode.
Uext = U.factors.copy()
Uext.factors[-1] = np.random.randn(K, R)
Uext.shape = (I, J, K)

# Fit model to the full dataset, only fitting the final set of factors.
V = tt.cp_als(X, rank=R, init=Uext, skip_modes=[0, 1], verbose=True)