.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/example_mcca.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_example_mcca.py: Multiway canonical correlation analysis (mCCA) ============================================== Find a set of components which are shared between different datasets. This example walks through three qualitatively different cases: 1. no shared structure across datasets, 2. some shared structure, 3. fully shared structure. The goal is to see how the transformed covariance changes as shared structure becomes stronger. Uses meegkit.cca.mcca(). References ---------- .. [1] de Cheveigne, A., Parra, L. C., & Bialek, W. (2018). Multiset canonical correlation analysis. NeuroImage, 186, 728-740. .. GENERATED FROM PYTHON SOURCE LINES 24-63 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from meegkit import cca rng = np.random.default_rng(5) def plot_mcca_case(A, C, x, title, interpretation): """Plot a standard diagnostic panel for one mCCA scenario.""" z = x.dot(A) fig, axes = plt.subplots(1, 3, figsize=(12, 4)) axes[0].imshow(A, aspect="auto") axes[0].set_title(f"{title}\ntransform matrix") axes[0].set_xlabel("Components") axes[0].set_ylabel("Original channels") axes[1].imshow(A.T.dot(C.dot(A)), aspect="auto") axes[1].set_title("Covariance of\ntransformed data") axes[1].set_xlabel("Components") axes[1].set_ylabel("Components") axes[2].imshow(x.T.dot(x.dot(A)), aspect="auto") axes[2].set_title("Cross-correlation between\nraw & transformed data") axes[2].set_xlabel("transformed") axes[2].set_ylabel("raw") fig.tight_layout() fig2, ax = plt.subplots(1, 1, figsize=(6, 3)) ax.plot(np.mean(z ** 2, axis=0), "o-") ax.set_title(f"{title}\nmean power of transformed components") ax.set_xlabel("Component") ax.set_ylabel("Mean power") ax.grid(True, ls=":", alpha=.4) fig2.tight_layout() print(f"{title}: {interpretation}") .. GENERATED FROM PYTHON SOURCE LINES 64-68 First example ----------------------------------------------------------------------------- We create 3 uncorrelated data sets. There should be no common structure between them. .. GENERATED FROM PYTHON SOURCE LINES 70-71 Build data .. GENERATED FROM PYTHON SOURCE LINES 71-78 .. code-block:: Python x1 = rng.standard_normal((10000, 10)) x2 = rng.standard_normal((10000, 10)) x3 = rng.standard_normal((10000, 10)) x = np.hstack((x1, x2, x3)) C = np.dot(x.T, x) print(f"Aggregated data covariance shape: {C.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregated data covariance shape: (30, 30) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Apply CCA .. GENERATED FROM PYTHON SOURCE LINES 80-87 .. code-block:: Python [A, score, AA] = cca.mcca(C, 10) plot_mcca_case( A, C, x, title="Case 1: independent datasets", interpretation="No component should dominate strongly because nothing is shared.", ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_001.png :alt: Case 1: independent datasets transform matrix, Covariance of transformed data, Cross-correlation between raw & transformed data :srcset: /auto_examples/images/sphx_glr_example_mcca_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_002.png :alt: Case 1: independent datasets mean power of transformed components :srcset: /auto_examples/images/sphx_glr_example_mcca_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Case 1: independent datasets: No component should dominate strongly because nothing is shared. .. GENERATED FROM PYTHON SOURCE LINES 88-91 Second example ----------------------------------------------------------------------------- Now Create 3 data sets with some shared parts. .. GENERATED FROM PYTHON SOURCE LINES 93-94 Build data .. GENERATED FROM PYTHON SOURCE LINES 94-102 .. code-block:: Python x1 = rng.standard_normal((10000, 5)) x2 = rng.standard_normal((10000, 5)) x3 = rng.standard_normal((10000, 5)) x4 = rng.standard_normal((10000, 5)) x = np.hstack((x2, x1, x3, x1, x4, x1)) C = np.dot(x.T, x) print(f"Aggregated data covariance shape: {C.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregated data covariance shape: (30, 30) .. GENERATED FROM PYTHON SOURCE LINES 103-104 Apply mCCA .. GENERATED FROM PYTHON SOURCE LINES 104-111 .. code-block:: Python A, score, AA = cca.mcca(C, 10) plot_mcca_case( A, C, x, title="Case 2: partially shared datasets", interpretation="Shared blocks should produce a clearer low-rank structure.", ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_003.png :alt: Case 2: partially shared datasets transform matrix, Covariance of transformed data, Cross-correlation between raw & transformed data :srcset: /auto_examples/images/sphx_glr_example_mcca_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_004.png :alt: Case 2: partially shared datasets mean power of transformed components :srcset: /auto_examples/images/sphx_glr_example_mcca_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Case 2: partially shared datasets: Shared blocks should produce a clearer low-rank structure. .. GENERATED FROM PYTHON SOURCE LINES 112-118 Third example ----------------------------------------------------------------------------- Finally let's create 3 identical 10-channel data sets. Only 10 worthwhile components should be found, and the transformed dataset should perfectly explain all the variance (empty last two block-columns in the cross-correlation plot). .. GENERATED FROM PYTHON SOURCE LINES 120-121 Build data .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: Python x1 = rng.standard_normal((10000, 10)) x = np.hstack((x1, x1, x1)) C = np.dot(x.T, x) print(f"Aggregated data covariance shape: {C.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregated data covariance shape: (30, 30) .. GENERATED FROM PYTHON SOURCE LINES 127-128 Compute mCCA .. GENERATED FROM PYTHON SOURCE LINES 128-138 .. code-block:: Python A, score, AA = cca.mcca(C, 10) plot_mcca_case( A, C, x, title="Case 3: identical datasets", interpretation=( "Shared structure is maximal, so the dominant components should be " "very clear." ), ) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_005.png :alt: Case 3: identical datasets transform matrix, Covariance of transformed data, Cross-correlation between raw & transformed data :srcset: /auto_examples/images/sphx_glr_example_mcca_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_example_mcca_006.png :alt: Case 3: identical datasets mean power of transformed components :srcset: /auto_examples/images/sphx_glr_example_mcca_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Case 3: identical datasets: Shared structure is maximal, so the dominant components should be very clear. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.152 seconds) .. _sphx_glr_download_auto_examples_example_mcca.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_mcca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_mcca.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_mcca.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_