22# Authors: Nicolas Barascud <nicolas.barascud@gmail.com>
33# Maciej Szul <maciej.szul@isc.cnrs.fr>
44import numpy as np
5+ from pathlib import Path
56from numpy .lib .stride_tricks import sliding_window_view
67from scipy import linalg
78from scipy .signal import welch
@@ -264,7 +265,7 @@ def dss_line(X, fline, sfreq, nremove=1, nfft=1024, nkeep=None, blocksize=None,
264265
265266
266267def 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
0 commit comments