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]
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)
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()
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)
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. plt.show() # ============================================================================= # =============================================================================
@author: Yongjie Zhu """ import tensortools as tt import matplotlib.pyplot as plt import scipy.io as scio import numpy as np from mne.viz import plot_connectivity_circle Sim = scio.loadmat("simulatedData.mat") X = Sim[ 'Sim'] # X[i,j,k] represents the connectivity value in time points k, freq bin j and paires (nodes) i freq_index = np.linspace(3, 45, 169) # Fit CP tensor decomposition . U = tt.ncp_hals(X, rank=3, verbose=True) # _bcd or _als for options fig, ax, po = tt.plot_factors(U.factors) plt.show() # visulazation Nroi = 64 # number of rois R = 3 # number of components for index in range(R): # temporal courses plt.subplot(R, R, index * R + 1) plt.plot(U.factors.factors[2][:, index]) if not index: plt.title('Temporal courses') plt.xlabel('Time/s') # spectra plt.subplot(R, R, index * R + 2) plt.plot(freq_index, U.factors.factors[1][:, index]) if not index:
# colors = range(np.int(X.shape[1]))/np.max(range(np.int(X.shape[1]))) # for time in (trial_num+1)*np.arange(np.int(X.shape[1])): # ax.scatter(X_embedded[time,0],X_embedded[time,1],c='red') # ============================================================================= # Fit CP tensor decomposition (two times). rank = 5 repeats = 30 all_models = [] all_obj = [] for repeat in tqdm(range(repeats)): U = tt.cp_als(X, rank=rank, verbose=False) all_models.append(U) all_obj.append(U.obj) U = all_models[np.argmin(all_obj)] 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(exposure.equalize_hist(trial_distances)) ## Cluster trials using tsne trial_labels = np.sort([0, 1, 2, 3] * 15) X_embedded = tsne(n_components=2, perplexity=40).fit_transform(trial_factors) plt.figure() plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=trial_labels)
def run_pipeline(filename): data = lm.loadmat(filename) session_name = os.path.basename(filename)[0:-4] (good_cells, pos_edges, trial_idx, spikelocations, spike_idx, location_vec) = prepareData(data) n_trials = 30 n_cells = len(good_cells) shape = (n_cells, len(pos_edges) - 1, n_trials) counts = np.zeros(shape, dtype=float) _fast_bin(counts, trial_idx, spikelocations, spike_idx) occupancy = np.zeros((len(pos_edges) - 1, n_trials), dtype=float) _fast_occ(occupancy, data['trial'] - 1, location_vec) for iT in range(n_trials): tmp = occupancy[:, iT] idx_v = np.flatnonzero(tmp) idx_n = np.flatnonzero(tmp == 0) tmp[idx_n] = np.interp(idx_n, idx_v, tmp[idx_v]) occupancy[:, iT] = tmp spMapN = np.zeros(counts.shape) for iC in range(n_cells): spMapN[iC, :, :] = np.divide(counts[iC, :, :], occupancy) spMapN = spi.gaussian_filter(spMapN, (0, 2, 0)) n_cells = len(good_cells) n_bins = len(pos_edges) - 1 spFlat = np.zeros((n_cells, n_trials * n_bins)) for iC in range(n_cells): spFlat[iC, :] = spMapN[iC, :, :].ravel(order='F') #spFlat = spFlat-spFlat.mean(axis=1)[:,np.newaxis] spFlat = normalize(spFlat, axis=0, norm='l2') for iC in range(n_cells): for iT in range(n_trials): start = iT * n_bins stop = (iT + 1) * n_bins trial_idx = np.arange(start, stop) tmp = spFlat[iC, trial_idx] spMapN[iC, :, iT] = tmp R = 5 # Fit CP tensor decomposition (two times). U = tt.ncp_bcd(spMapN, rank=R, verbose=False) V = tt.ncp_bcd(spMapN, rank=R, verbose=False) # 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() fig.savefig('C:\\temp\\try3\\' + session_name + '_tca.png') ff = np.matmul(np.transpose(spFlat), spFlat) plt.figure() ax = plt.imshow(ff) plt.colorbar() plt.axvline(x=n_bins * 20, color='red', ls='--', linewidth=1) plt.axvline(x=n_bins * 21, color='green', ls='--', linewidth=1) plt.axhline(y=n_bins * 20, color='red', ls='--', linewidth=1) plt.axhline(y=n_bins * 21, color='green', ls='--', linewidth=1) plt.savefig('C:\\temp\\try3\\' + session_name + '_cov.png') plt.close('all')