.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/multicsc/plot_somato_mu_waves.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_multicsc_plot_somato_mu_waves.py: =========================================================== Extracting :math:`\mu`-wave from the somato-sensory dataset =========================================================== This example illustrates how to learn rank-1 atoms [1]_ on the multivariate somato-sensorymotor dataset from :code:`mne`. The displayed results highlight the presence of :math:`\mu`-waves located in the SI cortex. .. [1] Dupré La Tour, T., Moreau, T., Jas, M., & Gramfort, A. (2018). `Multivariate Convolutional Sparse Coding for Electromagnetic Brain Signals `_. Advances in Neural Information Processing Systems (NIPS). .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python # Authors: Thomas Moreau # Mainak Jas # Tom Dupre La Tour # Alexandre Gramfort # # License: BSD (3-clause) .. GENERATED FROM PYTHON SOURCE LINES 24-25 Let us first define the parameters of our model. .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. code-block:: Python sfreq = 150. # Define the shape of the dictionary n_atoms = 25 n_times_atom = int(round(sfreq * 1.0)) # 1000. ms .. GENERATED FROM PYTHON SOURCE LINES 33-34 Next, we define the parameters for multivariate CSC .. GENERATED FROM PYTHON SOURCE LINES 34-56 .. code-block:: Python from alphacsc import BatchCDL cdl = BatchCDL( # Shape of the dictionary n_atoms=n_atoms, n_times_atom=n_times_atom, # Request a rank1 dictionary with unit norm temporal and spatial maps rank1=True, uv_constraint='separate', # Initialize the dictionary with random chunk from the data D_init='chunk', # rescale the regularization parameter to be 20% of lambda_max lmbd_max="scaled", reg=.2, # Number of iteration for the alternate minimization and cvg threshold n_iter=100, eps=1e-4, # solver for the z-step solver_z="lgcd", solver_z_kwargs={'tol': 1e-2, 'max_iter': 1000}, # solver for the d-step solver_d='alternate_adaptive', solver_d_kwargs={'max_iter': 300}, # Technical parameters verbose=1, random_state=0, n_jobs=6) .. GENERATED FROM PYTHON SOURCE LINES 57-60 Here, we load the data from the somato-sensory dataset and preprocess them in epochs. The epochs are selected around the stim, starting 2 seconds before and finishing 4 seconds after. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python from alphacsc.datasets.mne_data import load_data t_lim = (-2, 4) X, info = load_data(dataset='somato', epoch=t_lim, sfreq=sfreq) .. rst-class:: sphx-glr-script-out .. code-block:: none Using default location ~/mne_data for somato... 0%| | 0.00/611M [00:00 64). Consider setting rank to "auto" or setting it explicitly as an integer. cov = mne.compute_covariance(epochs_cov) Reducing data rank from 204 -> 204 Estimating covariance using EMPIRICAL Done. Number of samples used : 127412 [done] Not setting metadata 111 matching events found Setting baseline interval to [-2.001282051803185, 0.0] s Applying baseline correction (mode: mean) 0 projection items activated Using data from preloaded Raw for 111 events and 1803 original time points ... Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] Rejecting epoch based on EOG : ['EOG 061'] 8 bad epochs dropped NOTE: pick_types() is a legacy function. New code should use inst.pick(...). .. GENERATED FROM PYTHON SOURCE LINES 67-68 Fit the model and learn rank1 atoms .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python cdl.fit(X) .. rst-class:: sphx-glr-script-out .. code-block:: none .............. [BatchCDL] Converged after 14 iteration, (dz, du) = 8.765e-05, 8.726e-05 [BatchCDL] Fit in 271.3s .. GENERATED FROM PYTHON SOURCE LINES 71-73 Display the 4-th atom, which displays a :math:`\mu`-waveform in its temporal pattern. .. GENERATED FROM PYTHON SOURCE LINES 73-108 .. code-block:: Python import mne import numpy as np import matplotlib.pyplot as plt i_atom = 4 n_plots = 3 figsize = (n_plots * 5, 5.5) fig, axes = plt.subplots(1, n_plots, figsize=figsize, squeeze=False) # Plot the spatial map of the learn atom using mne topomap ax = axes[0, 0] u_hat = cdl.u_hat_[i_atom] mne.viz.plot_topomap(u_hat, info, axes=ax, show=False) ax.set(title='Learned spatial pattern') # Plot the temporal pattern of the learn atom ax = axes[0, 1] v_hat = cdl.v_hat_[i_atom] t = np.arange(v_hat.size) / sfreq ax.plot(t, v_hat) ax.set(xlabel='Time (sec)', title='Learned temporal waveform') ax.grid(True) # Plot the psd of the time atom ax = axes[0, 2] psd = np.abs(np.fft.rfft(v_hat)) ** 2 frequencies = np.linspace(0, sfreq / 2.0, len(psd)) ax.semilogy(frequencies, psd) ax.set(xlabel='Frequencies (Hz)', title='Power Spectral Density') ax.grid(True) ax.set_xlim(0, 30) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/multicsc/images/sphx_glr_plot_somato_mu_waves_001.png :alt: Learned spatial pattern, Learned temporal waveform, Power Spectral Density :srcset: /auto_examples/multicsc/images/sphx_glr_plot_somato_mu_waves_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (5 minutes 9.171 seconds) .. _sphx_glr_download_auto_examples_multicsc_plot_somato_mu_waves.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_somato_mu_waves.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_somato_mu_waves.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_somato_mu_waves.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_