|
| 1 | +"""Robust detrending examples. |
| 2 | +
|
| 3 | +Some toy examples to showcase usage for ``meegkit.detrend`` module. |
| 4 | +
|
| 5 | +Robust referencing is adapted from [1]_. |
| 6 | +
|
| 7 | +References |
| 8 | +---------- |
| 9 | +.. [1] de Cheveigné, A., & Arzounian, D. (2018). Robust detrending, |
| 10 | + rereferencing, outlier detection, and inpainting for multichannel data. |
| 11 | + NeuroImage, 172, 903-912. |
| 12 | +
|
| 13 | +""" |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +from matplotlib.gridspec import GridSpec |
| 17 | + |
| 18 | +from meegkit.detrend import regress |
| 19 | + |
| 20 | +np.random.seed(9) |
| 21 | + |
| 22 | +# Simple regression example, no weights |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | +# fit random walk |
| 25 | +y = np.cumsum(np.random.randn(1000, 1), axis=0) |
| 26 | +x = np.arange(1000)[:, None] |
| 27 | +x = np.hstack([x, x ** 2, x ** 3]) |
| 28 | +[b, z] = regress(y, x) |
| 29 | + |
| 30 | +plt.figure(1) |
| 31 | +plt.plot(y, label='data') |
| 32 | +plt.plot(z, label='fit') |
| 33 | +plt.title('No weights') |
| 34 | +plt.legend() |
| 35 | + |
| 36 | +# Simple regression example, with weights |
| 37 | +# ----------------------------------------------------------------------------- |
| 38 | +y = np.cumsum(np.random.randn(1000, 1), axis=0) |
| 39 | +w = np.random.rand(*y.shape) |
| 40 | +[b, z] = regress(y, x, w) |
| 41 | + |
| 42 | +plt.figure(2) |
| 43 | +plt.plot(y, label='data') |
| 44 | +plt.plot(z, label='fit') |
| 45 | +plt.title('Weighted regression') |
| 46 | +plt.legend() |
| 47 | + |
| 48 | +# Downweight 1st half of the data |
| 49 | +# ----------------------------------------------------------------------------- |
| 50 | +y = np.cumsum(np.random.randn(1000, 1), axis=0) + 1000 |
| 51 | +w = np.ones(y.shape[0]) |
| 52 | +w[:500] = 0 |
| 53 | +[b, z] = regress(y, x, w) |
| 54 | + |
| 55 | +f = plt.figure(3, constrained_layout=True) |
| 56 | +gs = GridSpec(3, 1, figure=f) |
| 57 | +ax1 = f.add_subplot(gs[:2, 0]) |
| 58 | +ax1.plot(y, label='data') |
| 59 | +ax1.plot(z, label='fit') |
| 60 | +ax1.set_xticklabels('') |
| 61 | +ax1.set_title('Split-wise regression') |
| 62 | +ax1.legend() |
| 63 | +ax2 = f.add_subplot(gs[2, 0]) |
| 64 | +l, = ax2.plot(np.arange(1000), np.zeros(1000)) |
| 65 | +ax2.stackplot(np.arange(1000), w, labels=['weights'], color=l.get_color()) |
| 66 | +ax2.legend(loc=2) |
| 67 | + |
| 68 | +# Multichannel regression |
| 69 | +# ----------------------------------------------------------------------------- |
| 70 | +y = np.cumsum(np.random.randn(1000, 2), axis=0) |
| 71 | +w = np.ones(y.shape[0]) |
| 72 | +[b, z] = regress(y, x, w) |
| 73 | + |
| 74 | +plt.figure(4) |
| 75 | +plt.plot(y, label='data') |
| 76 | +plt.plot(z, ls=':', label='fit') |
| 77 | +plt.title('Channel-wise regression') |
| 78 | +plt.legend() |
| 79 | +plt.show() |
0 commit comments