def test_means(): N = 2 # Number of neurons B = 3 # Number of basis functions L = 10 # Length of basis functions basis = cosine_basis(B, L=L) / L regressions = [SparseBernoulliRegression(N, B, mu_b=-2, S_b=0.1) for n in range(N)] model = NonlinearAutoregressiveModel(N, regressions, basis=basis) X, Y = model.generate(T=1000, keep=False) model.add_data(Y) Xtest = model.data_list[0][0] assert np.allclose(X, Xtest) means = model.means model.data_list[0] = (X, Y) means2 = model.means assert np.allclose(means, means2) plt.figure() for n in range(N): plt.subplot(N,1,n+1) plt.plot(means[0][:,n], lw=4) plt.plot(means2[0][:,n], lw=1) tn = np.where(Y[:,n])[0] plt.plot(tn, np.ones_like(tn), 'ko') plt.ylim(-0.05, 1.1) plt.show()
def test_means(): N = 2 # Number of neurons B = 3 # Number of basis functions L = 10 # Length of basis functions basis = cosine_basis(B, L=L) / L regressions = [ SparseBernoulliRegression(N, B, mu_b=-2, S_b=0.1) for n in range(N) ] model = NonlinearAutoregressiveModel(N, regressions, basis=basis) X, Y = model.generate(T=1000, keep=False) model.add_data(Y) Xtest = model.data_list[0][0] assert np.allclose(X, Xtest) means = model.means model.data_list[0] = (X, Y) means2 = model.means assert np.allclose(means, means2) plt.figure() for n in range(N): plt.subplot(N, 1, n + 1) plt.plot(means[0][:, n], lw=4) plt.plot(means2[0][:, n], lw=1) tn = np.where(Y[:, n])[0] plt.plot(tn, np.ones_like(tn), 'ko') plt.ylim(-0.05, 1.1) plt.show()
def GLM(self, recordings_index, trial_index, brain_area): ''' https://github.com/slinderman/pyglm ''' from pyglm.models import SparseBernoulliGLM from pyglm.utils.basis import cosine_basis path = self.all_data_path + '/' + self.selected_recordings[ recordings_index] trials = np.load(path + '/' + 'trials.intervals.npy') rates, binned_spk_tr = self.convert_one_population_to_rates( recordings_index, trial_index, brain_area) #rates=rates.T.reshape(1,rates.shape[1],rates.shape[0]) print(binned_spk_tr.shape) binned_spk_tr_ = binned_spk_tr.reshape(binned_spk_tr.shape[0], binned_spk_tr.shape[2]) print(binned_spk_tr_.shape) N = 4 # Number of neurons B = 1 # Number of "basis functions" L = 10 # Autoregressive window of influence basis = cosine_basis(B=B, L=L) / L # Generate some data from a model with self inhibition model = SparseBernoulliGLM(N, basis=basis) model.add_data(binned_spk_tr_[:4, :].T) # Initialize the plot #_, _, handles = model.plot() # Run a Gibbs sampler N_samples = 100 lps = [] for itr in range(N_samples): model.resample_model() lps.append(model.log_likelihood()) print(lps) plt.plot(lps) model.plot()
from pybasicbayes.util.text import progprint_xrange from pyglm.utils.basis import cosine_basis from pyglm.plotting import plot_glm from models import LatentDistanceWeightsSparseBernoulliGLM T = 10000 # Number of time bins to generate N = 9 # Number of neurons B = 1 # Number of "basis functions" L = 100 # Autoregressive window of influence D = 2 # Dimensionality of the feature space # Create a cosine basis to model smooth influence of # spikes on one neuron on the later spikes of others. basis = cosine_basis(B=B, L=L) / L true_model = LatentDistanceWeightsSparseBernoulliGLM(N, basis=basis, regression_kwargs=dict( rho=0.7, S_w=1, mu_b=-2)) # Set the true locations to be on a grid w = 3 s = 1.24 x = s * (np.arange(N) % w) y = s * (np.arange(N) // w) L_D = np.hstack((x[:, None], y[:, None]))
from pyglm.utils.basis import cosine_basis from models_tv import SparseBernoulliGLM_f from hips.plotting.colormaps import harvard_colors sns.set_style("white") paper_rc = {'lines.linewidth': 2.5, 'lines.markersize': 10, 'font.size': 15, 'axes.labelsize':15, 'xtick.labelsize': 15, 'ytick.labelsize': 15} sns.set_context("paper", rc = paper_rc) plt.ion() T = 2000 # Number of time bins to generate N = 2 # Number of neurons B = 1 # Number of "basis functions" L = 2 # Autoregressive window of influence basis = cosine_basis(B=B, L=L, a=1.0 / 2) / L # Generate some data from a model with self inhibition # The model structure has the info for the network and regression we used true_model = \ SparseBernoulliGLM_f(T, N, B, basis=basis, regression_kwargs=dict(rho=1, mu_w=0, S_w=0.001, mu_b=-2, S_b=0.0001)) # Network randomly assigned weights # sine wave Fs = [1000, 2000, 3000] f = 5 sample = T x = np.arange(sample) # Randomly assiged network weights
plt.ion() from pybasicbayes.util.text import progprint_xrange from pyglm.utils.basis import cosine_basis from pyglm.plotting import plot_glm from pyglm.models import SparseBernoulliGLM T = 10000 # Number of time bins to generate N = 4 # Number of neurons B = 1 # Number of "basis functions" L = 100 # Autoregressive window of influence # Create a cosine basis to model smooth influence of # spikes on one neuron on the later spikes of others. basis = cosine_basis(B=B, L=L) / L # Generate some data from a model with self inhibition true_model = \ SparseBernoulliGLM(N, basis=basis, regression_kwargs=dict(S_w=10.0, mu_b=-2.)) for n in range(N): true_model.regressions[n].a[n] = True true_model.regressions[n].W[n,:] = -2.0 _, Y = true_model.generate(T=T, keep=True) # Plot the true model fig, axs, handles = true_model.plot() plt.pause(0.1) # Create a test model for fitting