
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/01-basic-examples/06-plot_mareos_with_tsne.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_01-basic-examples_06-plot_mareos_with_tsne.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_01-basic-examples_06-plot_mareos_with_tsne.py:


Characterise a multisite problem with MAREoS
============================================

.. GENERATED FROM PYTHON SOURCE LINES 7-9

Imports
-------

.. GENERATED FROM PYTHON SOURCE LINES 9-23

.. code-block:: Python


    import matplotlib.pyplot as plt
    import pandas as pd
    import seaborn as sns
    from sklearn.manifold import TSNE

    from uniharmony import verbosity
    from uniharmony.datasets import load_MAREoS
    from uniharmony.plot import plot_2d_components_by_value, plot_2d_projection


    sns.set_theme(style="whitegrid")
    verbosity("warning")








.. GENERATED FROM PYTHON SOURCE LINES 24-27

Data generation
---------------
Let's load the MAREoS datasets, which simulates several datasets with and without Effects of Site (EoS)

.. GENERATED FROM PYTHON SOURCE LINES 27-34

.. code-block:: Python


    # Initialize a tSNE object
    tsne = TSNE(n_components=2, random_state=42, perplexity=30, max_iter=1000, learning_rate="auto")
    # Load the MAREoS dataset
    datasets = load_MAREoS()
    print(datasets.keys())





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Downloading file 'public_datasets.zip' from 'https://www.imardgroup.com/mareos-benchmark/public_datasets.zip' to '/home/runner/.cache/uniharmony'.
      0%|                                              | 0.00/3.66M [00:00<?, ?B/s]      3%|▉                                     | 96.3k/3.66M [00:00<00:04, 758kB/s]      7%|██▋                                   | 264k/3.66M [00:00<00:03, 1.08MB/s]     14%|█████▏                                | 497k/3.66M [00:00<00:02, 1.40MB/s]     27%|██████████▏                           | 977k/3.66M [00:00<00:01, 2.31MB/s]     48%|█████████████████▊                   | 1.77M/3.66M [00:00<00:00, 3.67MB/s]     92%|██████████████████████████████████   | 3.38M/3.66M [00:00<00:00, 6.63MB/s]      0%|                                              | 0.00/3.66M [00:00<?, ?B/s]    100%|█████████████████████████████████████| 3.66M/3.66M [00:00<00:00, 18.3GB/s]
    Unzipping contents of '/home/runner/.cache/uniharmony/public_datasets.zip' to '/home/runner/.cache/uniharmony/MAREoS'
    dict_keys(['eos_simple1', 'eos_simple2', 'eos_interaction1', 'eos_interaction2', 'true_simple1', 'true_simple2', 'true_interaction1', 'true_interaction2'])




.. GENERATED FROM PYTHON SOURCE LINES 35-36

Now let's play with tSNE and the plotting helper functions

.. GENERATED FROM PYTHON SOURCE LINES 36-75

.. code-block:: Python


    # EoS signal
    dataset = datasets["eos_simple1"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    tsne = TSNE(n_components=2, random_state=42, perplexity=30, max_iter=1000, learning_rate="auto")
    X_tsne = tsne.fit_transform(X)
    tsne_df_eos = pd.DataFrame({"comp1": X_tsne[:, 0], "comp2": X_tsne[:, 1], "site": sites, "target": y})

    # True signal
    dataset = datasets["true_simple1"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    tsne = TSNE(n_components=2, random_state=42, perplexity=30, max_iter=1000, learning_rate="auto")
    X_tsne = tsne.fit_transform(X)
    tsne_df_true = pd.DataFrame({"comp1": X_tsne[:, 0], "comp2": X_tsne[:, 1], "site": sites, "target": y})

    # Initialize figure
    fig, axes = plt.subplots(2, 2, figsize=(16, 14))

    # Plot 1: EoS By site
    ax1 = axes[0, 0]
    plot_2d_components_by_value(tsne_df_eos, "site", "tSNE", ax1)

    # Plot 2: EoS By target
    ax2 = axes[1, 0]
    plot_2d_components_by_value(tsne_df_eos, "target", "tSNE", ax2)

    # # Plot 3: True Signal By site
    ax3 = axes[0, 1]
    plot_2d_components_by_value(tsne_df_true, "site", "tSNE", ax3)

    # Plot 4: True Signal By target
    ax4 = axes[1, 1]
    plot_2d_components_by_value(tsne_df_true, "target", "tSNE", ax4)





.. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_001.png
   :alt: 2D Projection using tSNE - Colored by site, 2D Projection using tSNE - Colored by site, 2D Projection using tSNE - Colored by target, 2D Projection using tSNE - Colored by target
   :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 76-80

We see that, for the EoS signal, the main tSNE components are related with the sites, which are also realted with the targets.
On the other hand, there is not a clear relationship between the sites nor the target for the True signal.

Now let's use the ``plot_tsne`` funtion which can simplify the code and will allowd us a fast and simple exploration

.. GENERATED FROM PYTHON SOURCE LINES 82-98

.. code-block:: Python


    # EoS signal
    dataset = datasets["eos_simple2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)

    # True signal
    dataset = datasets["true_simple2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)





.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_002.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_003.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_003.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using tsne - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using tsne - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 99-115

.. code-block:: Python


    # EoS signal
    dataset = datasets["eos_simple2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)

    # True signal
    dataset = datasets["true_simple2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)





.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_004.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_004.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_005.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_005.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using tsne - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using tsne - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 116-133

.. code-block:: Python


    # EoS signal
    dataset = datasets["eos_interaction1"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)


    # True Signal
    dataset = datasets["true_interaction1"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)





.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_006.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_006.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_007.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_007.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using tsne - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using tsne - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))



.. GENERATED FROM PYTHON SOURCE LINES 134-149

.. code-block:: Python


    # EoS signal
    dataset = datasets["eos_interaction2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)


    # True signal
    dataset = datasets["true_interaction2"]
    X = dataset["X"]
    y = dataset["y"]
    sites = dataset["sites"]
    plot_2d_projection(X, y, sites, tsne)



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_008.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_008.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_009.png
         :alt: 2D Projection using tsne - Colored by site, 2D Projection using tsne - Colored by target
         :srcset: /auto_examples/01-basic-examples/images/sphx_glr_06-plot_mareos_with_tsne_009.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (<Figure size 1200x600 with 2 Axes>, array([<Axes: title={'center': '2D Projection using tsne - Colored by site'}, xlabel='Component 1', ylabel='Component 2'>,
           <Axes: title={'center': '2D Projection using tsne - Colored by target'}, xlabel='Component 1', ylabel='Component 2'>],
          dtype=object))




.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (1 minutes 0.655 seconds)


.. _sphx_glr_download_auto_examples_01-basic-examples_06-plot_mareos_with_tsne.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 06-plot_mareos_with_tsne.ipynb <06-plot_mareos_with_tsne.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 06-plot_mareos_with_tsne.py <06-plot_mareos_with_tsne.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 06-plot_mareos_with_tsne.zip <06-plot_mareos_with_tsne.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
