From 6605b63d2cd958e078fe7c8e7377dfd235588583 Mon Sep 17 00:00:00 2001 From: romquentin Date: Mon, 3 May 2021 17:32:25 +0200 Subject: [PATCH 1/5] add create_maksed_weight --- meegkit/detrend.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/meegkit/detrend.py b/meegkit/detrend.py index b829a1a5..9b3ca605 100644 --- a/meegkit/detrend.py +++ b/meegkit/detrend.py @@ -298,3 +298,39 @@ def _plot_detrend(x, y, w): ax2.set_ylabel('ch. weights') ax2.set_xlabel('samples') plt.show() + + +def create_masked_weight(x, events, tmin, tmax, sfreq): + + """Create a weight matrix (n_channels * n_times) with masked + periods (value of zero) in order to mask the trials of interest during + detrending + https://www.sciencedirect.com/science/article/pii/S0165027021000157. + + Parameters + ---------- + x : ndarray, shape=(n_times, n_channels) + Raw data matrix. + events : ndarray, shape=(n_events) + Time samples of the events + tmin : float + Start time before event (in seconds) + tmax : float + End time after event (in seconds) + sfreq : float + The sampling frequency of the data x + Returns + ------- + weights : ndarray, shape=(n_times, n_channels) + Weight for each channel and each time sample (zero is masked) + + """ + + if len(x.shape) != 2: + raise ValueError('The shape of x has to be (n_times * n_channels)') + + weights = np.ones(x.shape) + for event in events: + weights[:, event + tmin * sfreq: event + tmax * sfreq + 1] = 0 + + return weights From c19dfc5d1c9b241a79ffbbd65fe174a6a4ec6783 Mon Sep 17 00:00:00 2001 From: romquentin Date: Wed, 5 May 2021 12:40:19 +0200 Subject: [PATCH 2/5] Formatting docstring --- meegkit/detrend.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/meegkit/detrend.py b/meegkit/detrend.py index 6eba8f39..49a4ff5d 100644 --- a/meegkit/detrend.py +++ b/meegkit/detrend.py @@ -300,10 +300,10 @@ def _plot_detrend(x, y, w): def create_masked_weight(x, events, tmin, tmax, sfreq): - """Create a weight matrix (n_channels * n_times) with masked periods (value of zero) in order to mask the trials of interest during - detrending + detrending. + https://www.sciencedirect.com/science/article/pii/S0165027021000157. Parameters @@ -322,9 +322,7 @@ def create_masked_weight(x, events, tmin, tmax, sfreq): ------- weights : ndarray, shape=(n_times, n_channels) Weight for each channel and each time sample (zero is masked) - """ - if len(x.shape) != 2: raise ValueError('The shape of x has to be (n_times * n_channels)') From 29fc48c07a751a9b55069af72e4448fe2874c7db Mon Sep 17 00:00:00 2001 From: romquentin Date: Tue, 11 May 2021 10:29:06 +0200 Subject: [PATCH 3/5] format public function --- meegkit/detrend.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/meegkit/detrend.py b/meegkit/detrend.py index 49a4ff5d..046fc276 100644 --- a/meegkit/detrend.py +++ b/meegkit/detrend.py @@ -300,7 +300,9 @@ def _plot_detrend(x, y, w): def create_masked_weight(x, events, tmin, tmax, sfreq): - """Create a weight matrix (n_channels * n_times) with masked + """Output a weight matrix for trial-masked detrending. + + Create a weight matrix (n_channels * n_times) with masked periods (value of zero) in order to mask the trials of interest during detrending. From d9d371cdf3f64b9f9041f80a7509b227129672bb Mon Sep 17 00:00:00 2001 From: romquentin Date: Tue, 11 May 2021 12:58:00 +0200 Subject: [PATCH 4/5] add test + correct dimension of weights --- meegkit/detrend.py | 6 +++--- tests/test_detrend.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/meegkit/detrend.py b/meegkit/detrend.py index 046fc276..4764c70c 100644 --- a/meegkit/detrend.py +++ b/meegkit/detrend.py @@ -302,7 +302,7 @@ def _plot_detrend(x, y, w): def create_masked_weight(x, events, tmin, tmax, sfreq): """Output a weight matrix for trial-masked detrending. - Create a weight matrix (n_channels * n_times) with masked + Create a weight matrix (n_times * n_channels) with masked periods (value of zero) in order to mask the trials of interest during detrending. @@ -330,6 +330,6 @@ def create_masked_weight(x, events, tmin, tmax, sfreq): weights = np.ones(x.shape) for event in events: - weights[:, event + tmin * sfreq: event + tmax * sfreq + 1] = 0 - + weights[int(event + tmin * sfreq): int(event + tmax * sfreq + 1), + :] = 0 return weights diff --git a/tests/test_detrend.py b/tests/test_detrend.py index 477e6cb6..3419ec97 100644 --- a/tests/test_detrend.py +++ b/tests/test_detrend.py @@ -1,7 +1,7 @@ """Test robust detrending.""" import numpy as np -from meegkit.detrend import regress, detrend, reduce_ringing +from meegkit.detrend import regress, detrend, reduce_ringing, create_masked_weight from scipy.signal import butter, lfilter @@ -89,6 +89,16 @@ def test_detrend(show=False): x += 2 * np.sin(2 * np.pi * np.arange(1000) / 200)[:, None] y, _, _ = detrend(x, 5, basis='sinusoids', show=True) + # trial-masked detrending + trend = np.linspace(0, 100, 1000)[:, None] + data = 3 * np.random.randn(*trend.shape) + data[:100, :] = 100 + x = trend + data + events = np.arange(30, 970, 40) + tmin, tmax, sfreq = -0.2, 0.3, 20 + w = create_masked_weight(x, events, tmin, tmax, sfreq) + y, _, _ = detrend(x, 1, w, basis='polynomials', show=show) + def test_ringing(): """Test reduce_ringing function.""" From f6ae62fe422d90174d46a391963c902ca6ee82f3 Mon Sep 17 00:00:00 2001 From: nbara <10333715+nbara@users.noreply.github.com> Date: Wed, 12 May 2021 15:26:48 +0200 Subject: [PATCH 5/5] docstring --- meegkit/detrend.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/meegkit/detrend.py b/meegkit/detrend.py index 4764c70c..0b882e25 100644 --- a/meegkit/detrend.py +++ b/meegkit/detrend.py @@ -302,34 +302,40 @@ def _plot_detrend(x, y, w): def create_masked_weight(x, events, tmin, tmax, sfreq): """Output a weight matrix for trial-masked detrending. - Create a weight matrix (n_times * n_channels) with masked + Creates a (n_times, n_channels) weight matrix with masked periods (value of zero) in order to mask the trials of interest during - detrending. - - https://www.sciencedirect.com/science/article/pii/S0165027021000157. + detrending [1]_. Parameters ---------- x : ndarray, shape=(n_times, n_channels) Raw data matrix. events : ndarray, shape=(n_events) - Time samples of the events + Time samples of the events. tmin : float - Start time before event (in seconds) + Start time before event (in seconds). tmax : float - End time after event (in seconds) + End time after event (in seconds). sfreq : float - The sampling frequency of the data x + The sampling frequency of the data. + Returns ------- weights : ndarray, shape=(n_times, n_channels) - Weight for each channel and each time sample (zero is masked) + Weight for each channel and each time sample (zero is masked). + + References + ---------- + .. [1] van Driel, J., Olivers, C. N., & Fahrenfort, J. J. (2021). High-pass + filtering artifacts in multivariate classification of neural time series + data. Journal of Neuroscience Methods, 352, 109080. + """ - if len(x.shape) != 2: - raise ValueError('The shape of x has to be (n_times * n_channels)') + if x.ndim != 2: + raise ValueError('The shape of x must be (n_times, n_channels)') weights = np.ones(x.shape) - for event in events: - weights[int(event + tmin * sfreq): int(event + tmax * sfreq + 1), - :] = 0 + for e in events: + weights[int(e + tmin * sfreq): int(e + tmax * sfreq + 1), :] = 0 + return weights