brain_pipe.preprocessing.brain.artifact.ArtifactRemovalMWF

class ArtifactRemovalMWF(data_key='data', fs_key='data_fs', reference_channels=(0, 1, 2, 32, 33, 34, 35, 36), delay=3, *args, **kwargs)

Bases: PipelineStep

Remove (eyeblink) artifacts with an MWF.

This code was based on the excellent library of Somers et al. [1] The matlab implementation can be found here

References

__init__(data_key='data', fs_key='data_fs', reference_channels=(0, 1, 2, 32, 33, 34, 35, 36), delay=3, *args, **kwargs)

Create an ArtifactRemovalMWF object.

Parameters:
  • data_key (str) – Key in the data_dict for the brain data

  • fs_key (str) – Key in the data_dict for the brain data sampling frequency.

  • reference_channels (Sequence[int]) – List of channel indices to use as reference channels. By default, the frontal channels of a Biosemi64 channel system are used.

  • delay (int) – Delay that has to be taken into account.

Methods

__init__([data_key, fs_key, ...])

Create an ArtifactRemovalMWF object.

apply_mwf(data, mwf_weights)

Apply the MWF.

check_symmetric(data[, rtol, atol])

Check whether a matrix is symmetric.

compute_mwf(data, mask)

Compute the MWF.

fix_symmetric(data)

Fix a matrix to be symmetric.

get_artifact_segments(data, fs)

Create a mask to select segments of data where artifacts are.

parse_dict_keys(key[, name, ...])

Parse a key or a sequence of keys.

sort_evd(eig_values, eig_vectors)

Sort the eigenvalues and eigenvectors.

stack_delayed(data, delay)

Stack delayed versions of the data.

apply_mwf(data, mwf_weights)

Apply the MWF.

Parameters:
  • data (np.ndarray) – Brain response data with shape of (channels, time)

  • mwf_weights (np.ndarray) – MWF weights with shape of (channels * (2 * delay + 1), channels)

Returns:

Filtered data with shape of (channels, time) and artifacts with shape of (channels, time).

Return type:

Tuple[np.ndarray, np.ndarray]

check_symmetric(data, rtol=1e-05, atol=1e-08)

Check whether a matrix is symmetric.

Parameters:
  • data (np.ndarray) – Matrix to check

  • rtol (float) – Relative tolerance

  • atol (float) – Absolute tolerance

Returns:

True if the matrix is symmetric, False otherwise.

Return type:

bool

compute_mwf(data, mask)

Compute the MWF.

Parameters:
  • data (np.ndarray) – Brain response data with shape of (channels, time)

  • mask (np.ndarray) – Mask of shape (time,) with True for artifact segments.

Returns:

The MWF with dimension (channels * (2 * delay + 1), channels)

Return type:

np.ndarray

fix_symmetric(data)

Fix a matrix to be symmetric.

Parameters:

data (np.ndarray) – Matrix to fix

Returns:

Symmetric matrix.

Return type:

np.ndarray

get_artifact_segments(data, fs)

Create a mask to select segments of data where artifacts are.

Parameters:
  • data (np.ndarray) – Brain response data with shape of (time, channels)

  • fs (int) – Sampling frequency of the data

Returns:

Mask of shape (time,) with True for artifact segments.

Return type:

np.ndarray

parse_dict_keys(key: str | Sequence[str] | Mapping[str, str], name='key', require_ordered_dict=False) OrderedDict[str, str]

Parse a key or a sequence of keys.

Parameters:
  • key (Union[str, Sequence[str], Mapping[str,str]]) – A key or a sequence of keys.

  • name (str) – The name of the key. Used for error messages.

  • require_ordered_dict (bool) – If True, the key must be an OrderedDict. If False, the key can also be an ordinary dict.

Returns:

A mapping of input keys to output keys.

Return type:

OrderedDict[str, str]

Raises:

TypeError – If the key is not a string, a sequence of strings or a mapping of strings. If the key is a mapping but require_ordered_dict is True and the mapping is not an OrderedDict.

sort_evd(eig_values, eig_vectors)

Sort the eigenvalues and eigenvectors.

Parameters:
  • eig_values (np.ndarray) – Eigenvalues

  • eig_vectors (np.ndarray) – Column eigenvectors

Returns:

Sorted eigenvalues and eigenvectors.

Return type:

Tuple[np.ndarray, np.ndarray]

stack_delayed(data, delay)

Stack delayed versions of the data.

Parameters:
  • data (np.ndarray) – Brain response data with shape of (channels, time)

  • delay

Returns:

Stacked data with shape of (channels * (2 * delay + 1), time), and the number of channels in the stacked data.

Return type:

Tuple[np.ndarray, int]