n_iter = 10 # Parameter of the alpha-stable distribution for the alpha-CSC model. # 0 < alpha < 2 # A value of 2 would correspond to the Gaussian noise model, as in vanilla CSC. alpha = 1.2 ############################################################################### # First, we fit a CSC model on the clean data. Interestingly, we obtain # prototypical waveforms of the signal on which we can clearly see the CFC. from alphacsc import learn_d_z, learn_d_z_weighted X = data_clean _, _, d_hat, z_hat, _ = learn_d_z(X, n_iter=n_iter, **common_params) plot_atoms(d_hat) ############################################################################### # Then, if we fit a CSC model on the dirty data, the model is strongly affected # by the artifacts, and we cannot see CFC anymore in the temporal waveforms. X = data_dirty _, _, d_hat, z_hat, _ = learn_d_z(X, n_iter=n_iter, **common_params) plot_atoms(d_hat) ############################################################################### # Finally, If we fit an alpha-CSC model on the dirty data, the model is less
############################################################################### # Note that the atoms don't always have the same amplitude or occur at the # same time instant. # # Now, we run vanilla CSC on the data. from alphacsc import learn_d_z # noqa random_state = 60 pobj, times, d_hat, z_hat, reg = learn_d_z(X, n_atoms, n_times_atom, reg=reg, n_iter=n_iter, solver_d_kwargs=dict(factr=100), random_state=random_state, n_jobs=1, verbose=1) print('Vanilla CSC') ############################################################################### # Finally, let's compare the results. import matplotlib.pyplot as plt # noqa plt.figure() plt.plot(d_hat.T) plt.plot(ds_true.T, 'k--')
from alphacsc import learn_d_z params = dict( n_atoms=3, n_times_atom=int(sfreq * 1.0), # 1000. ms reg=5., n_iter=10, solver_z='l-bfgs', solver_z_kwargs=dict(factr=1e9), solver_d_kwargs=dict(factr=1e2), random_state=42, n_jobs=5, verbose=1) _, _, d_hat, z_hat, _ = learn_d_z(data, **params) ############################################################################### # Plot the temporal patterns. Interestingly, we obtain prototypical # waveforms of the signal on which we can clearly see the CFC. n_atoms, n_times_atom = d_hat.shape n_columns = min(6, n_atoms) n_rows = int(np.ceil(n_atoms // n_columns)) figsize = (4 * n_columns, 3 * n_rows) fig, axes = plt.subplots(n_rows, n_columns, figsize=figsize, sharey=True) axes = axes.ravel() for kk in range(n_atoms): ax = axes[kk] time = np.arange(n_times_atom) / sfreq
X_new -= np.mean(X_new) X_new /= np.std(X_new) ############################################################################### # The convolutions can result in edge artifacts at the edges of the trials. # Therefore, we discount the contributions from the edges by windowing the # trials. from numpy import hamming X_new *= hamming(n_times)[None, :] ############################################################################### # Of course, in a data-limited setting we want to use as much of the data as # possible. If this is the case, you can set `overlap` to non-zero (for example # half the epoch length). # # Now, we run regular CSC since the trials are not too noisy from alphacsc import learn_d_z pobj, times, d_hat, z_hat, reg = learn_d_z(X_new, n_atoms, n_times_atom, reg=reg, n_iter=n_iter, random_state=random_state, n_jobs=1) ############################################################################### # Let's look at the atoms now. plt.figure() plt.plot(d_hat.T) plt.show()
############################################################################### # Now, we run vanilla CSC on the data. from functools import partial # noqa from alphacsc import learn_d_z, update_d_block # noqa random_state = 60 func = partial(update_d_block, projection='dual') pobj, times, d_hat, Z_hat = learn_d_z(X, n_atoms, n_times_atom, func_d=func, reg=reg, n_iter=n_iter, solver_d_kwargs=dict(factr=100), random_state=random_state, n_jobs=1, solver_z='l_bfgs', verbose=1) print('Vanilla CSC') ############################################################################### # and then alpha CSC on the same data from alphacsc import learn_d_z_weighted # noqa d_hat_mcem, z_hat_mcem, Tau = learn_d_z_weighted( X, n_atoms,