Skip to content

Commit bf2091d

Browse files
committed
[FIX] changed annotations in dss line iter, added dirname and removed prefix in params
1 parent e44c4bb commit bf2091d

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

meegkit/dss.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Authors: Nicolas Barascud <nicolas.barascud@gmail.com>
33
# Maciej Szul <maciej.szul@isc.cnrs.fr>
44
import numpy as np
5+
from pathlib import Path
56
from numpy.lib.stride_tricks import sliding_window_view
67
from scipy import linalg
78
from scipy.signal import welch
@@ -264,7 +265,7 @@ def dss_line(X, fline, sfreq, nremove=1, nfft=1024, nkeep=None, blocksize=None,
264265

265266

266267
def dss_line_iter(data, fline, sfreq, win_sz=10, spot_sz=2.5,
267-
nfft=512, show=False, prefix="dss_iter", n_iter_max=100):
268+
nfft=512, show=False, dirname=None, extension=".png", n_iter_max=100):
268269
"""Remove power line artifact iteratively.
269270
270271
This method applies dss_line() until the artifact has been smoothed out
@@ -288,9 +289,12 @@ def dss_line_iter(data, fline, sfreq, win_sz=10, spot_sz=2.5,
288289
FFT size for the internal PSD calculation (default=512).
289290
show: bool
290291
Produce a visual output of each iteration (default=False).
291-
prefix : str
292-
Path and first part of the visualisation output file
293-
"{prefix}_{iteration number}.png" (default="dss_iter").
292+
dirname: str
293+
Path to the directory where visual outputs are saved when show is 'True'.
294+
If 'None', does not save the outputs. (default=None)
295+
extension: str
296+
Extension of the images filenames. Must be compatible with plt.savefig()
297+
function. (default=".png")
294298
n_iter_max : int
295299
Maximum number of iterations (default=100).
296300
@@ -357,26 +361,36 @@ def nan_basic_interp(array):
357361
y = mean_sens[freq_rn_ix]
358362
ax.flat[0].plot(freq_used, y)
359363
ax.flat[0].set_title("Mean PSD across trials")
364+
ax.flat[0].set_xlabel("Frequency (Hz)")
365+
ax.flat[0].set_ylabel("Power")
360366

361-
ax.flat[1].plot(freq_used, mean_psd_tf, c="gray")
362-
ax.flat[1].plot(freq_used, mean_psd, c="blue")
363-
ax.flat[1].plot(freq_used, clean_fit_line, c="red")
367+
ax.flat[1].plot(freq_used, mean_psd_tf, c="gray", label="Interpolated mean PSD")
368+
ax.flat[1].plot(freq_used, mean_psd, c="blue", label="Mean PSD")
369+
ax.flat[1].plot(freq_used, clean_fit_line, c="red", label="Fitted polynomial")
364370
ax.flat[1].set_title("Mean PSD across trials and sensors")
371+
ax.flat[1].set_xlabel("Frequency (Hz)")
372+
ax.flat[1].set_ylabel("Power")
373+
ax.flat[1].legend()
365374

366375
tf_ix = np.where(freq_used <= fline)[0][-1]
367-
ax.flat[2].plot(residuals, freq_used)
376+
ax.flat[2].plot(freq_used, residuals)
368377
color = "green"
369378
if mean_score <= 0:
370379
color = "red"
371-
ax.flat[2].scatter(residuals[tf_ix], freq_used[tf_ix], c=color)
380+
ax.flat[2].scatter(freq_used[tf_ix], residuals[tf_ix], c=color)
372381
ax.flat[2].set_title("Residuals")
382+
ax.flat[2].set_xlabel("Frequency (Hz)")
383+
ax.flat[2].set_ylabel("Power")
373384

374385
ax.flat[3].plot(np.arange(iterations + 1), aggr_resid, marker="o")
375-
ax.flat[3].set_title("Iterations")
386+
ax.flat[3].set_title("Aggregated residuals")
387+
ax.flat[3].set_xlabel("Iteration")
388+
ax.flat[3].set_ylabel("Power")
376389

377390
plt.tight_layout()
378-
plt.savefig(f"{prefix}_{iterations:03}.png")
379-
plt.close("all")
391+
if dirname is not None:
392+
plt.savefig(Path(dirname) / f"dss_iter_{iterations:03}{extension}")
393+
plt.show()
380394

381395
if mean_score <= 0:
382396
break

tests/test_dss.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,24 @@ def test_dss_line_iter():
132132
# # time x channel x trial sf=200 fline=50
133133

134134
sr = 200
135-
fline = 25
135+
fline = 50
136136
n_samples = 9000
137137
n_chans = 10
138138

139139
# 2D case, n_outputs == 1
140140
x, _ = create_line_data(n_samples, n_chans=n_chans, n_trials=1,
141141
noise_dim=10, SNR=2, fline=fline / sr)
142142
x = x[..., 0]
143+
# x = data
143144

144145
# RuntimeError when max iterations has been reached
145146
with pytest.raises(RuntimeError):
146147
out, _ = dss.dss_line_iter(x, fline + 1, sr,
147148
show=False, n_iter_max=2)
148149

149150
with TemporaryDirectory() as tmpdir:
150-
out, _ = dss.dss_line_iter(x, fline + .5, sr,
151-
prefix=os.path.join(tmpdir, "dss_iter_"),
152-
show=True)
151+
out, _ = dss.dss_line_iter(x, fline + 1, sr,
152+
show=True, dirname=tmpdir)
153153

154154
def _plot(before, after):
155155
f, ax = plt.subplots(1, 2, sharey=True)
@@ -171,7 +171,9 @@ def _plot(before, after):
171171
# # Test n_trials > 1 TODO
172172
x, _ = create_line_data(n_samples, n_chans=n_chans, n_trials=2,
173173
noise_dim=10, SNR=2, fline=fline / sr)
174-
out, _ = dss.dss_line_iter(x, fline, sr, show=False)
174+
with TemporaryDirectory() as tmpdir:
175+
out, _ = dss.dss_line_iter(x, fline, sr,
176+
show=True, dirname=tmpdir)
175177
plt.close("all")
176178

177179

0 commit comments

Comments
 (0)