File: ica.rst

package info (click to toggle)
python-mne 0.13.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 92,032 kB
  • ctags: 8,249
  • sloc: python: 84,750; makefile: 205; sh: 15
file content (118 lines) | stat: -rw-r--r-- 4,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
.. _ica:

Independent Component Analysis (ICA)
####################################

.. contents:: Contents
   :local:
   :depth: 2

Many M/EEG signals including biological artifacts reflect non-Gaussian
processes. Therefore PCA-based artifact rejection will likely perform worse at
separating the signal from noise sources.
MNE-Python supports identifying artifacts and latent components using temporal ICA.
MNE-Python implements the :class:`mne.preprocessing.ICA` class that facilitates applying ICA
to MEG and EEG data. It supports FastICA, the infomax, and the extended informax algorithm.
It allows whitening the data using a fast randomized PCA algorithmd. Furthermore,
multiple sensor types are supported by pre-whitening / rescaling. Bad data segments can be excluded
from the model fitting by `reject` parameter in :class:`mne.preprocessing.ICA.fit`.

For convenience, :class:`mne.preprocessing.ICA` implements methods for
    - automated detection of ECG and EOG artifacts
        - :meth:`mne.preprocessing.ICA.find_bads_ecg`
        - :meth:`mne.preprocessing.ICA.find_bads_eog`
    - visualization
        - :meth:`mne.preprocessing.ICA.plot_components` for mapping the spatial sensitvity of a comopnent
        - :meth:`mne.preprocessing.ICA.plot_sources` for component related time series
        - :meth:`mne.preprocessing.ICA.plot_scores` for scores on which component detection is based upon
        - :meth:`mne.preprocessing.ICA.plot_overlay` for showing differences between raw and processed data
    - persistence
        :meth:`mne.preprocessing.ICA.save` for writing the ICA solution into a fif file.
    - integration with MNE-Python object system
        :meth:`mne.preprocessing.ICA.get_sources` for putting component related time series in MNE data structures.

Concepts
========

ICA finds directions in the feature space corresponding to projections with high non-Gaussianity.

- not necessarily orthogonal in the original feature space, but orthogonal in the whitened feature space.
- In contrast, PCA finds orthogonal directions in the raw feature
  space that correspond to directions accounting for maximum variance.
- or differently, if data only reflect Gaussian processes ICA and PCA are equivalent.


**Example**: Imagine 3 instruments playing simultaneously and 3 microphones
recording mixed signals. ICA can be used to recover the sources ie. what is played by each instrument.

ICA employs a very simple model: $X = AS$ where $X$ is our observations, $A$ is the mixing matrix and $S$ is the vector of independent (latent) sources.

The challenge is to recover A and S from X.


First generate simulated data
-----------------------------

.. code:: python

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import signal

    from sklearn.decomposition import FastICA, PCA

    np.random.seed(0)  # set seed for reproducible results
    n_samples = 2000
    time = np.linspace(0, 8, n_samples)

    s1 = np.sin(2 * time)  # Signal 1 : sinusoidal signal
    s2 = np.sign(np.sin(3 * time))  # Signal 2 : square signal
    s3 = signal.sawtooth(2 * np.pi * time)  # Signal 3: sawtooth signal

    S = np.c_[s1, s2, s3]
    S += 0.2 * np.random.normal(size=S.shape)  # Add noise

    S /= S.std(axis=0)  # Standardize data
    # Mix data
    A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]])  # Mixing matrix
    X = np.dot(S, A.T)  # Generate observations

Now try to recover the sources
------------------------------

.. code:: python

    # compute ICA
    ica = FastICA(n_components=3)
    S_ = ica.fit_transform(X)  # Get the estimated sources
    A_ = ica.mixing_  # Get estimated mixing matrix

    # compute PCA
    pca = PCA(n_components=3)
    H = pca.fit_transform(X)  # estimate PCA sources

    plt.figure(figsize=(9, 6))

    models = [X, S, S_, H]
    names = ['Observations (mixed signal)',
             'True Sources',
             'ICA estimated sources',
             'PCA estimated sources']
    colors = ['red', 'steelblue', 'orange']

    for ii, (model, name) in enumerate(zip(models, names), 1):
        plt.subplot(4, 1, ii)
        plt.title(name)
        for sig, color in zip(model.T, colors):
            plt.plot(sig, color=color)

    plt.tight_layout()
    plt.show()



.. image:: ../pics/ICA_primer.png


:math:`\rightarrow` PCA fails at recovering our "instruments" since the
related signals reflect non-Gaussian processes.