.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/csc/plot_simulate_csc.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_csc_plot_simulate_csc.py: ============================= Vanilla CSC on simulated data ============================= This example demonstrates `vanilla` CSC on simulated data. Note that `vanilla` CSC is just a special case of alphaCSC with alpha=2. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python # Authors: Mainak Jas # Tom Dupre La Tour # Umut Simsekli # Alexandre Gramfort # # License: BSD (3-clause) .. GENERATED FROM PYTHON SOURCE LINES 18-19 Let us first define the parameters of our model. .. GENERATED FROM PYTHON SOURCE LINES 19-28 .. code-block:: Python n_times_atom = 64 # L n_times = 512 # T n_atoms = 2 # K n_trials = 100 # N n_iter = 50 reg = 0.1 .. GENERATED FROM PYTHON SOURCE LINES 29-30 Here, we simulate the data .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python from alphacsc.simulate import simulate_data # noqa random_state_simulate = 1 X, ds_true, z_true = simulate_data(n_trials, n_times, n_times_atom, n_atoms, random_state_simulate) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Add some noise and corrupt some trials .. GENERATED FROM PYTHON SOURCE LINES 39-53 .. code-block:: Python from scipy.stats import levy_stable # noqa from alphacsc import check_random_state # noqa # Add stationary noise: fraction_corrupted = 0.02 n_corrupted_trials = int(fraction_corrupted * n_trials) rng = check_random_state(random_state_simulate) X += 0.01 * rng.randn(*X.shape) idx_corrupted = rng.randint(0, n_trials, size=n_corrupted_trials) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Let us look at the first 10 trials to see how they look. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python from alphacsc.viz.callback import plot_data # noqa plot_data([X[:10]]) .. image-sg:: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_001.png :alt: plot simulate csc :srcset: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-64 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. .. GENERATED FROM PYTHON SOURCE LINES 64-75 .. code-block:: Python 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') .. rst-class:: sphx-glr-script-out .. code-block:: none V_0/50 /github/workspace/alphacsc/update_d.py:224: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.) lhs_c_copy[0] += lambd /github/workspace/alphacsc/update_d.py:172: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.) lambd_hats[k] = lambd_hat ................................................. Vanilla CSC .. GENERATED FROM PYTHON SOURCE LINES 76-77 Finally, let's compare the results. .. GENERATED FROM PYTHON SOURCE LINES 77-83 .. code-block:: Python import matplotlib.pyplot as plt # noqa plt.figure() plt.plot(d_hat.T) plt.plot(ds_true.T, 'k--') .. image-sg:: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_002.png :alt: plot simulate csc :srcset: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. GENERATED FROM PYTHON SOURCE LINES 84-85 We can also visualize the learned activations .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python plot_data([z[:10] for z in z_hat], ['stem'] * n_atoms) .. image-sg:: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_003.png :alt: plot simulate csc :srcset: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 89-93 Note if the data is corrupted with impulsive noise, this method may not be the best. Check out our :ref:`example using alphacsc ` to learn how to deal with such data. .. GENERATED FROM PYTHON SOURCE LINES 93-107 .. code-block:: Python alpha = 1.2 noise_level = 0.005 X[idx_corrupted] += levy_stable.rvs(alpha, 0, loc=0, scale=noise_level, size=(n_corrupted_trials, n_times), random_state=random_state_simulate) 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) plt.figure() plt.plot(d_hat.T) plt.plot(ds_true.T, 'k--') plt.show() .. image-sg:: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_004.png :alt: plot simulate csc :srcset: /auto_examples/csc/images/sphx_glr_plot_simulate_csc_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none V_0/50 ................................................. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 34.976 seconds) .. _sphx_glr_download_auto_examples_csc_plot_simulate_csc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simulate_csc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simulate_csc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simulate_csc.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_