featureextracter module

class featureextracter.FeatureExtracter(data_ori, srate, n_chan, n_pnts, n_trials, channel_names, tmin=0, tmax=[], freq_bands=[[2, 4], [4, 8], [8, 12], [15, 30], [30, 80]], freq_band_names=['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma'])

Bases: object

Feature Extracter Class. Each Time Features object possess one instance. Used to extracted feature from the original data (amplitude usually). For frequency-based features, frequency bands of interest are defined (freq_bands) and the mean of the feature on these bands is computed.

Attributes:
data_ori : array

Represents the amplitude data. Features are computed from this. Size : [n_chan, n_pnts, n_trials]

srate : float

Sampling rate (Hz)

n_chan : int

Number of channels in the data

n_pnts : int

Number of time points

n_trials : int

Number of trials

tmin : float (default 0)

Starting time (s)

tmax : float | None (default: None)

Ending time (s). If none, is computed from the number of points and the sampling rate.

channel_names : array

Name of each channel

freq_bands : array

Array of shape (n_freq_bands * 2) which defined the frequency bands of interest. Mean value of the frequency-based features are computed over these bands. Default value is : [[2, 4], [4, 8], [8, 12], [15, 30], [30, 80]]

freq_band_names : array

Name of the frequency bands. Default value is [‘Delta’, ‘Theta’, ‘Alpha’, ‘Beta’, ‘Gamma’]

Methods

bandpower_on_data([filt_type, filt_order, …])
Parameters:
cwt_on_data([wav_name, pfreqs, scale_type, …]) Apply the Continous Wavelet Transform to the data data_ori, return both power and phase average over the frequency bands.
dwt_on_data(wav_name[, scale_type, …]) Apply the Discrete Wavelet Transform on the data
filter_hilbert_on_data(center_freq, bandwidth) Estimate the phase of the data data_ori using band-pass filtering and the Hilbert transform.
stft_on_data([win_name, win_dur, overlap, …])
Parameters:
bandpower_on_data(filt_type='butterworth', filt_order=3, scale_type=[], base_tstart=[], base_tend=[])
Parameters:
filt_type :
filt_order :
scale_type :
base_tstart :
base_tend :
cwt_on_data(wav_name=[], pfreqs=[], scale_type=[], base_tstart=[], base_tend=[])

Apply the Continous Wavelet Transform to the data data_ori, return both power and phase average over the frequency bands. Uses the PyWavelets module

Parameters:
wav_name : str

Wavelet name

pfreqs : array

Pseudo frequency array to use for computing power and phase

scale_type : str

Scaling type. Can be :

  • ‘db_ratio’ : \(P_{norm} = 10 \cdot log10(\frac{P}{mean(P_{baseline})})\)
  • ‘percent_change’ : \(P_{norm} = 100 \cdot \frac{P - mean(P_{baseline})}{mean(P_{baseline})}\)
  • ‘z_transform’ : \(P_{norm} = \frac{P - mean(P_{baseline})}{std(P_{baseline})}\)
base_tstart : int

Baseline starting point (in sample)

base_tend : int

Baseline ending point (in sample)

dwt_on_data(wav_name, scale_type=[], base_tstart=[], base_tend=[])

Apply the Discrete Wavelet Transform on the data

Parameters:
wav_name

scale_type : str

scale_type : str

Scaling type. Can be :

  • ‘db_ratio’ : \(P_{norm} = 10 \cdot log10(\frac{P}{mean(P_{baseline})})\)
  • ‘percent_change’ : \(P_{norm} = 100 \cdot \frac{P - mean(P_{baseline})}{mean(P_{baseline})}\)
  • ‘z_transform’ : \(P_{norm} = \frac{P - mean(P_{baseline})}{std(P_{baseline})}\)
base_tstart : int

Baseline starting point (in sample)

base_tend : int

Baseline ending point (in sample)

filter_hilbert_on_data(center_freq, bandwidth, ftype='elliptic', forder=4, f_tolerance=[], noise_tolerance=[], n_monte_carlo=5)

Estimate the phase of the data data_ori using band-pass filtering and the Hilbert transform. The cos and sin of the phase angle are returned, the phase angle is 2-pi periodic, thus cannot be used directly as a feature in a classification or regression task. see phase_utils.compute_robust_estimation()

Parameters:
center_freq : array

Center frequency (in Hz) of the band-pass filters

bandwidth : array

Bandwidth (in Hz) of the band-pass filters

ftype : str

Filter type:

  • ‘butter’ : Butterworth filter
  • ‘elliptic’ (default) : Elliptic filter
  • ‘fir’ : Finite Impulse Response filter
forder : int

Filter order - default : 4

f_tolerance : float

Tolerance of the cut-off frequencies. A random number in the interval [-f_tolerance/2, +f_tolerance/2] will be added to the cut-off frequencies

noise_tolerance : float

Random noise from a uniform distribution in [-noise_tolerance/2, +noise_tolerance/2] will be added to the signal x_raw

n_monte_carlo : int

Number of monte carlo repetition

stft_on_data(win_name='hamming', win_dur=0.2, overlap=0.85, nfft=[], scale_type=[], base_tstart=[], base_tend=[])
Parameters:
win_name : str (default: ‘hamming’)

Window’s name

win_dur : float (default: 0.2)

Window’s duration (s)

overlap : float

Overlap - must be between 0 and 1 - Default: 0.85

nfft : int | None (default: None)

Number of frequencies used in the FFT

scale_type : str

Scaling type. Can be :

  • ‘db_ratio’ : \(P_{norm} = 10 \cdot log10(\frac{P}{mean(P_{baseline})})\)
  • ‘percent_change’ : \(P_{norm} = 100 \cdot \frac{P - mean(P_{baseline})}{mean(P_{baseline})}\)
  • ‘z_transform’ : \(P_{norm} = \frac{P - mean(P_{baseline})}{std(P_{baseline})}\)
base_tstart : int

Baseline starting point (in sample)

base_tend : int

Baseline ending point (in sample)

featureextracter.bandpower_1d(x, srate, freq_bands, filt_type='butterworth', filt_order=3, scale_type=[], base_tstart=[], base_tend=[])

Compute the power of input signal x in the different frequency bands defined by freq_bands. Signal x is first passed through a band-pass filter specified by filt_type and filt_order, output amplitude is then squared to get the power and the envelope is computed using the Hilbert transform

featureextracter.compute_band_mean(data, freqs, freq_bands, interp_method='linear', freqs_interp=[])

Compute the mean of data over the frequency bands defined by freq_bands. The original data array of shape [n_freqs * n_pnts] is first interpolated along the frequency axis before computing the mean. The new frequency values can be specified by the freqs_interp parameter. By default the freqs_interp parameter add 3 equally spaced points between each original frequency value.

Parameters:
data : array (size: n_freqs * n_pnts)

Time-frequency data

freqs : array (size: n_freqs)

Frequencies in data (in Hz)

freq_bands : array (size: n_freq_bands * 2)

Frequency bands of interest. Mean value are calculated on these bands.

interp_method : str (default: ‘linear’)

Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’) See :func: scipy.interpolated.interp1d

freqs_interp : array | None (default: none)

Frequencies (in Hz) used for interpolation. If none, 3 equally spaced frequency points are added between each original frequency value.

Returns:
data_band_mean : array (size: n_freq_bands * n_pnts)

Mean of the data over the frequency bands

featureextracter.cwt_1d(x, srate, freq_bands, freqs=array([ 3., 3.273378, 3.57166783, 3.89713963, 4.25227037, 4.63976276, 5.06256577, 5.52389713, 6.02726778, 6.57650857, 7.17579948, 7.82970137, 8.54319073, 9.32169752, 10.17114651, 11.0980024, 12.10931895, 13.21279273, 14.41682166, 15.73056893, 17.16403273, 18.72812235, 20.43474121, 22.2968774, 24.32870262, 26.54567994, 28.96468154, 31.60411707, 34.48407379, 37.62646946, 41.05521906, 44.79641689, 48.87853512, 53.33264044, 58.19263056, 63.49549213, 69.28158225, 75.59493562, 82.48359962, 90. ]), n_cycles=array([ 1., 1.06081836, 1.12533558, 1.19377664, 1.26638017, 1.34339933, 1.42510267, 1.51177507, 1.60371874, 1.70125428, 1.80472177, 1.91448198, 2.03091762, 2.15443469, 2.28546386, 2.42446202, 2.57191381, 2.72833338, 2.89426612, 3.07029063, 3.25702066, 3.45510729, 3.66524124, 3.88815518, 4.12462638, 4.37547938, 4.64158883, 4.92388263, 5.22334507, 5.54102033, 5.87801607, 6.23550734, 6.61474064, 7.01703829, 7.44380301, 7.89652287, 8.3767764, 8.88623816, 9.42668455, 10. ]), scale_type=[], base_tstart=[], base_tend=[])

Compute the Continuous Wavelet Transform for a 1 dimension input array x

Parameters:
x : array

Input array - must have 1 dimension

srate : int

Sampling frequency

freq_bands : array (size: n_freq_bands * 2)

Frequency bands of interest. Mean value of both power and phase are calculated on these bands.

freqs : array

Pseudo frequency array to use for computing power and phase

n_cycles : int | array

Number of cycles for each wavelet

scale_type : str

Scaling type. Can be :

  • ‘db_ratio’ : \(P_{norm} = 10 \cdot log10(\frac{P}{mean(P_{baseline})})\)
  • ‘percent_change’ : \(P_{norm} = 100 \cdot \frac{P - mean(P_{baseline})}{mean(P_{baseline})}\)
  • ‘z_transform’ : \(P_{norm} = \frac{P - mean(P_{baseline})}{std(P_{baseline})}\)
base_tstart : int

Baseline starting point (in sample)

base_tend : int

Baseline ending point (in sample)

Returns:
coeffs_power : array (size: n_freqs * n_pnts)

Power extracted from the wavelet coefficients

coeffs_power_bands : array (size: n_freq_bands * n_pnts)

Mean of coeffs_power over the frequency bands defined in freq_bands

phase : array (size: n_freqs * n_pnts)

Phase angle

phase_bands : array (size: n_freq_bands * n_pnts)

Mean of phase over the frequency bands defined in freq_bands

featureextracter.dwt_1d(x, srate, wav_name, do_plot=0, scale_type=[], base_tstart=[], base_tend=[])

Discrete wavelet transform for input 1D array.

Parameters:
x : array

Input 1D array

srate : float

Sampling rate (Hz)

wav_name : str

Wavelet name

do_plot : bool

If True, plot the results

scale_type : str

Normalization type - can be ‘db_ratio’, ‘percent_change’, or ‘z_transform’

base_tstart : float

Baseline starting time (s)

base_tend : float

Baseline ending time (s)

Returns:
coeffs_upsampled, pfreq_bands
featureextracter.get_bandpass_filter(srate, cutoff_freqs, filt_type='butterworth', filt_order=[])
featureextracter.stft_1d(x, srate, freq_bands, win_name='hamming', win_dur=[], overlap=0.9, nfft=256, fmin=[], scale_type=[], base_tstart=[], base_tend=[])

Compute the short-time Fourier transform

Parameters:
x : array

Input vector

srate : float

Sampling frequency (Hz)

freq_bands : array (size: n_freq_bands * 2)

Frequency bands of interest. Mean value of both power and phase are calculated on these bands.

win_name : str (default: ‘hamming’)

Window’s name

win_dur : float (default: [])

Window’s duration (s)

overlap : float

Overlap - must be between 0 and 1 - Default: 0.9

nfft : int | None (default: 256)

Number of frequencies used in the FFT

fmin : float

Starting frequency

scale_type : str

Normalization type - can be ‘db_ratio’, ‘percent_change’, or ‘z_transform’

base_tstart : float

Baseline starting time (s)

base_tend : float

Baseline ending time (s)

Returns:
pxx, pxx_bands_interp, phase_bands_interp
featureextracter.tf_scaling(tf_power_mat, base_ind_start, base_ind_end, scale_type)

Scale / Normalize the time-frequency map given the baseline time.

Parameters:
tf_power_mat : array (size n_channels * n_pnts)

Time frequency matrix

base_ind_start : int

Baseline starting point (in sample)

base_ind_end : int

Baseline ending point (in sample).

scale_type : str

Scaling type. Can be :

  • ‘db_ratio’ : \(P_{norm} = 10 \cdot log10(\frac{P}{mean(P_{baseline})})\)
  • ‘percent_change’ : \(P_{norm} = 100 \cdot \frac{P - mean(P_{baseline})}{mean(P_{baseline})}\)
  • ‘z_transform’ : \(P_{norm} = \frac{P - mean(P_{baseline})}{std(P_{baseline})}\)
Returns:
tf_power_mat_norm : array

Normalized time-frequency map

featureextracter.zero_tf_edges(tf_mat, fs, freqs, n_period=2)