mne.preprocessing.
ICA
(n_components=None, max_pca_components=None, n_pca_components=None, noise_cov=None, random_state=None, method=’fastica’, fit_params=None, max_iter=200, verbose=None)[source]¶M/EEG signal decomposition using Independent Component Analysis (ICA).
This object can be used to estimate ICA components and then remove some from Raw or Epochs for data exploration or artifact correction.
Caveat! If supplying a noise covariance keep track of the projections available in the cov or in the raw object. For example, if you are interested in EOG or ECG artifacts, EOG and ECG projections should be temporally removed before fitting the ICA. You can say:
>> projs, raw.info['projs'] = raw.info['projs'], []
>> ica.fit(raw)
>> raw.info['projs'] = projs
Note
Methods implemented are FastICA (default), Infomax and Extended-Infomax. Infomax can be quite sensitive to differences in floating point arithmetic due to exponential non-linearity. Extended-Infomax seems to be more stable in this respect enhancing reproducibility and stability of results.
Parameters: | n_components : int | float | None
max_pca_components : int | None
n_pca_components : int | float
noise_cov : None | instance of mne.cov.Covariance
random_state : None | int | instance of np.random.RandomState
method : {‘fastica’, ‘infomax’, ‘extended-infomax’}
fit_params : dict | None.
max_iter : int, optional
verbose : bool, str, int, or None
|
---|
Attributes
current_fit | (str) Flag informing about which data type (raw or epochs) was used for the fit. |
ch_names | (list-like) Channel names resulting from initial picking. The number of components used for ICA decomposition. |
n_components_ |
(int) If fit, the actual number of components used for ICA decomposition. |
n_pca_components | (int) See above. |
max_pca_components | (int) The number of components used for PCA dimensionality reduction. |
verbose | (bool, str, int, or None) See above. |
pca_components_ |
(ndarray) If fit, the PCA components |
pca_mean_ |
(ndarray) If fit, the mean vector used to center the data before doing the PCA. |
pca_explained_variance_ |
(ndarray) If fit, the variance explained by each PCA component |
mixing_matrix_ |
(ndarray) If fit, the mixing matrix to restore observed data, else None. |
unmixing_matrix_ |
(ndarray) If fit, the matrix to unmix observed data, else None. |
exclude | (list) List of sources indices to exclude, i.e. artifact components identified throughout the ICA solution. Indices added to this list, will be dispatched to the .pick_sources methods. Source indices passed to the .pick_sources method via the ‘exclude’ argument are added to the .exclude attribute. When saving the ICA also the indices are restored. Hence, artifact components once identified don’t have to be added again. To dump this ‘artifact memory’ say: ica.exclude = [] |
info | (None | instance of Info) The measurement info copied from the object fitted. |
n_samples_ |
(int) the number of samples used on fit. |
labels_ |
(dict) A dictionary of independent component indices, grouped by types of independent components. This attribute is set by some of the artifact detection functions. |
Methods
__contains__ (ch_type) |
Check channel type membership. |
__hash__ () <==> hash(x) |
|
apply (inst[, include, exclude, …]) |
Remove selected components from the signal. |
copy () |
Copy the ICA object. |
detect_artifacts (raw[, start_find, …]) |
Run ICA artifacts detection workflow. |
find_bads_ecg (inst[, ch_name, threshold, …]) |
Detect ECG related components using correlation. |
find_bads_eog (inst[, ch_name, threshold, …]) |
Detect EOG related components using correlation. |
fit (inst[, picks, start, stop, decim, …]) |
Run the ICA decomposition on raw data. |
get_components () |
Get ICA topomap for components as numpy arrays. |
get_sources (inst[, add_channels, start, stop]) |
Estimate sources given the unmixing matrix. |
plot_components ([picks, ch_type, res, …]) |
Project unmixing matrix on interpolated sensor topogrpahy. |
plot_overlay (inst[, exclude, picks, start, …]) |
Overlay of raw and cleaned signals given the unmixing matrix. |
plot_properties (inst[, picks, axes, dB, …]) |
Display component properties. |
plot_scores (scores[, exclude, labels, …]) |
Plot scores related to detected components. |
plot_sources (inst[, picks, exclude, start, …]) |
Plot estimated latent sources given the unmixing matrix. |
save (fname) |
Store ICA solution into a fiff file. |
score_sources (inst[, target, score_func, …]) |
Assign score to components based on statistic or metric. |
__contains__
(ch_type)[source]¶Check channel type membership.
Parameters: | ch_type : str
|
---|---|
Returns: | in : bool
|
Examples
Channel type membership can be tested as:
>>> 'meg' in inst
True
>>> 'seeg' in inst
False
__hash__
() <==> hash(x)¶apply
(inst, include=None, exclude=None, n_pca_components=None, start=None, stop=None)[source]¶Remove selected components from the signal.
Given the unmixing matrix, transform data, zero out components, and inverse transform the data. This procedure will reconstruct M/EEG signals from which the dynamics described by the excluded components is subtracted.
Parameters: | inst : instance of Raw, Epochs or Evoked
include : array_like of int.
exclude : array_like of int.
n_pca_components : int | float | None
start : int | float | None
stop : int | float | None
|
---|
compensation_grade
¶The current gradient compensation grade.
detect_artifacts
(raw, start_find=None, stop_find=None, ecg_ch=None, ecg_score_func=’pearsonr’, ecg_criterion=0.1, eog_ch=None, eog_score_func=’pearsonr’, eog_criterion=0.1, skew_criterion=-1, kurt_criterion=-1, var_criterion=0, add_nodes=None)[source]¶Run ICA artifacts detection workflow.
Note. This is still experimental and will most likely change. Over the next releases. For maximum control use the workflow exposed in the examples.
Hints and caveats: - It is highly recommended to bandpass filter ECG and EOG data and pass them instead of the channel names as ecg_ch and eog_ch arguments. - please check your results. Detection by kurtosis and variance may be powerful but misclassification of brain signals as noise cannot be precluded. - Consider using shorter times for start_find and stop_find than for start and stop. It can save you much time.
Example invocation (taking advantage of the defaults):
ica.detect_artifacts(ecg_channel='MEG 1531', eog_channel='EOG 061')
Parameters: | raw : instance of Raw
start_find : int | float | None
stop_find : int | float | None
ecg_ch : str | ndarray | None
ecg_score_func : str | callable
ecg_criterion : float | int | list-like | slice
eog_ch : list | str | ndarray | None
eog_score_func : str | callable
eog_criterion : float | int | list-like | slice
skew_criterion : float | int | list-like | slice
kurt_criterion : float | int | list-like | slice
var_criterion : float | int | list-like | slice
add_nodes : list of ica_nodes
|
---|---|
Returns: | self : instance of ICA
|
find_bads_ecg
(inst, ch_name=None, threshold=None, start=None, stop=None, l_freq=8, h_freq=16, method=’ctps’, reject_by_annotation=True, verbose=None)[source]¶Detect ECG related components using correlation.
Note
If no ECG channel is available, routine attempts to create an artificial ECG based on cross-channel averaging.
Parameters: | inst : instance of Raw, Epochs or Evoked
ch_name : str
threshold : float
start : int | float | None
stop : int | float | None
l_freq : float
h_freq : float
method : {‘ctps’, ‘correlation’}
reject_by_annotation : bool
verbose : bool, str, int, or None
|
---|---|
Returns: | ecg_idx : list of int
scores : np.ndarray of float, shape (
|
See also
References
find_bads_eog
(inst, ch_name=None, threshold=3.0, start=None, stop=None, l_freq=1, h_freq=10, reject_by_annotation=True, verbose=None)[source]¶Detect EOG related components using correlation.
Detection is based on Pearson correlation between the filtered data and the filtered EOG channel. Thresholding is based on adaptive z-scoring. The above threshold components will be masked and the z-score will be recomputed until no supra-threshold component remains.
Parameters: | inst : instance of Raw, Epochs or Evoked
ch_name : str
threshold : int | float
start : int | float | None
stop : int | float | None
l_freq : float
h_freq : float
reject_by_annotation : bool
verbose : bool, str, int, or None
|
---|---|
Returns: | eog_idx : list of int
scores : np.ndarray of float, shape (
|
See also
fit
(inst, picks=None, start=None, stop=None, decim=None, reject=None, flat=None, tstep=2.0, reject_by_annotation=True, verbose=None)[source]¶Run the ICA decomposition on raw data.
Caveat! If supplying a noise covariance keep track of the projections available in the cov, the raw or the epochs object. For example, if you are interested in EOG or ECG artifacts, EOG and ECG projections should be temporally removed before fitting the ICA.
Parameters: | inst : instance of Raw, Epochs or Evoked
picks : array-like of int
start : int | float | None
stop : int | float | None
decim : int | None
reject : dict | None
flat : dict | None
tstep : float
reject_by_annotation : bool
verbose : bool, str, int, or None
|
---|---|
Returns: | self : instance of ICA
|
get_components
()[source]¶Get ICA topomap for components as numpy arrays.
Returns: | components : array, shape (n_channels, n_components)
|
---|
get_sources
(inst, add_channels=None, start=None, stop=None)[source]¶Estimate sources given the unmixing matrix.
This method will return the sources in the container format passed. Typical usecases:
Parameters: | inst : instance of Raw, Epochs or Evoked
add_channels : None | list of str
start : int | float | None
stop : int | float | None
|
---|---|
Returns: | sources : instance of Raw, Epochs or Evoked
|
plot_components
(picks=None, ch_type=None, res=64, layout=None, vmin=None, vmax=None, cmap=’RdBu_r’, sensors=True, colorbar=False, title=None, show=True, outlines=’head’, contours=6, image_interp=’bilinear’, head_pos=None, inst=None)[source]¶Project unmixing matrix on interpolated sensor topogrpahy.
Parameters: | picks : int | array-like | None
ch_type : ‘mag’ | ‘grad’ | ‘planar1’ | ‘planar2’ | ‘eeg’ | None
res : int
layout : None | Layout
vmin : float | callable | None
vmax : float | callable | None
cmap : matplotlib colormap | (colormap, bool) | ‘interactive’ | None
sensors : bool | str
colorbar : bool
title : str | None
show : bool
outlines : ‘head’ | ‘skirt’ | dict | None
contours : int | False | None
image_interp : str
head_pos : dict | None
inst : Raw | Epochs | None
|
---|---|
Returns: | fig : instance of matplotlib.pyplot.Figure or list
|
plot_overlay
(inst, exclude=None, picks=None, start=None, stop=None, title=None, show=True)[source]¶Overlay of raw and cleaned signals given the unmixing matrix.
This method helps visualizing signal quality and artifact rejection.
Parameters: | inst : instance of mne.io.Raw or mne.Evoked
exclude : array_like of int
picks : array-like of int | None (default)
start : int
stop : int
title : str
show : bool
|
---|---|
Returns: | fig : instance of pyplot.Figure
|
plot_properties
(inst, picks=None, axes=None, dB=True, plot_std=True, topomap_args=None, image_args=None, psd_args=None, figsize=None, show=True)[source]¶Display component properties.
Properties include the topography, epochs image, ERP/ERF, power spectrum, and epoch variance.
Parameters: | inst: instance of Epochs or Raw
picks : int | array-like of int | None
axes: list of matplotlib axes | None
dB: bool
plot_std: bool | float
topomap_args : dict | None
image_args : dict | None
psd_args : dict | None
figsize : array-like of size (2,) | None
show : bool
|
---|---|
Returns: | fig : list
|
Notes
New in version 0.13.
plot_scores
(scores, exclude=None, labels=None, axhline=None, title=’ICA component scores’, figsize=(12, 6), show=True)[source]¶Plot scores related to detected components.
Use this function to asses how well your score describes outlier sources and how well you were detecting them.
Parameters: | scores : array_like of float, shape (n ica components) | list of arrays
exclude : array_like of int
labels : str | list | ‘ecg’ | ‘eog’ | None
axhline : float
title : str
figsize : tuple of int
show : bool
|
---|---|
Returns: | fig : instance of matplotlib.pyplot.Figure
|
plot_sources
(inst, picks=None, exclude=None, start=None, stop=None, title=None, show=True, block=False, show_first_samp=False)[source]¶Plot estimated latent sources given the unmixing matrix.
Typical usecases:
Parameters: | inst : instance of mne.io.Raw, mne.Epochs, mne.Evoked
picks : int | array_like of int | None.
exclude : array_like of int
start : int
stop : int
title : str | None
show : bool
block : bool
show_first_samp : bool
|
---|---|
Returns: | fig : instance of pyplot.Figure
|
Notes
For raw and epoch instances, it is possible to select components for
exclusion by clicking on the line. The selected components are added to
ica.exclude
on close.
New in version 0.10.0.
save
(fname)[source]¶Store ICA solution into a fiff file.
Parameters: | fname : str
|
---|
score_sources
(inst, target=None, score_func=’pearsonr’, start=None, stop=None, l_freq=None, h_freq=None, reject_by_annotation=True, verbose=None)[source]¶Assign score to components based on statistic or metric.
Parameters: | inst : instance of Raw, Epochs or Evoked
target : array-like | ch_name | None
score_func : callable | str label
start : int | float | None
stop : int | float | None
l_freq : float
h_freq : float
reject_by_annotation : bool
verbose : bool, str, int, or None
|
---|---|
Returns: | scores : ndarray
|