Common Spatial Patterns (CSP)

class bciflow.modules.sf.csp.csp(m_pairs: int = 2)[source]

Bases: object

n_electrodes

(int) The number of electrodes.

Type:

int

m_pairs

(int) The number of pairs of spatial filters to extract (default is 2).

Type:

int

W

(np.ndarray) The spatial filters.

Type:

numpy.ndarray

bands

(int) The number of bands used.

Type:

int

fit(eegdata: dict) np.ndarray[source]

Fits the CSP filter to the input data, calculating the spatial filters.

transform(eegdata: dict) dict[source]

Applies the learned spatial filters to the input data.

fit_transform(eegdata: dict) dict[source]

Combines fitting and transforming into a single step.

Example

>>> from bciflow.modules.sf.csp import csp
>>> import numpy as np
>>> csp_filter = csp(m_pairs=2)
>>> eegdata = {
        'X': np.random.rand(100, 5, 64),  # 100 samples, 5 bands, 64 electrodes
        'y': np.random.randint(0, 2, size=100)  # Binary classes
    }
>>> csp_filter.fit(eegdata)
>>> transformed_data = csp_filter.transform(eegdata)
>>> print(transformed_data['X'].shape)
W: ndarray = None
bands: int = None
fit(eegdata: dict) ndarray[source]

Fits the CSP filter to the input data, calculating the spatial filters.

Parameters:

eegdata (dict) – The input data containing ‘X’ (features) and ‘y’ (labels).

Returns:

self – The fitted CSP object with spatial filters stored in W.

Return type:

csp

Raises:

ValueError – If any of the input parameters are invalid

fit_transform(eegdata: dict) dict[source]

Combines fitting and transforming into a single step.

Parameters:

eegdata (dict) – The input data containing ‘X’ (features) and ‘y’ (labels).

Returns:

eegdata – The transformed data with ‘X’ containing the filtered features.

Return type:

dict

Raises:

ValueError – If the input data does not match the expected format or dimensions.

m_pairs: int = 2
n_electrodes: int = None
transform(eegdata: dict) dict[source]

Applies the learned spatial filters to the input data.

Parameters:

eegdata (dict) – The input data containing ‘X’ (features).

Returns:

eegdata – The transformed data with ‘X’ containing the filtered features.

Return type:

dict

Raises:

ValueError – If the input data does not match the expected format or dimensions.

For more details on CSP, refer to:
  • Ramoser, H., et al. (2000). Optimal Spatial Filtering of Single Trial EEG During Imagined Hand Movement. IEEE Transactions on Rehabilitation Engineering.