-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectro_artifacts.py
57 lines (40 loc) · 1.73 KB
/
spectro_artifacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import pandas as pd
from params import *
from bibliotheque import *
from preproc import convert_vhdr_job
meta = get_metadata()
p = {
'wsize_sec':1, # welch window size in seconds
'overlap_prop':0.75, # proportion of overlap between windows
'nfft_factor':2, # zero-padding : nfft = nperseg * nfft_factor
'lowcut':10,
'highcut':500,
'chans':['Fz','Pz','Oz','FC5','FC6','ECG','RespiNasale']
}
for sub in subject_keys:
sess = ['baseline'] + list(meta.loc[sub,:])
fig, axs = plt.subplots(nrows = len(p['chans']), ncols = len(sess), figsize = (20,25), constrained_layout = True)
fig.suptitle(sub, fontsize = 20)
for c, ses in enumerate(sess):
run_key = f'{sub}_{ses}'
raw = convert_vhdr_job.get(run_key)['raw']
srate = raw.attrs['srate']
nperseg = int(srate * p['wsize_sec'])
overlap_prop = p['overlap_prop']
noverlap = nperseg // (1 / overlap_prop)
nfft = nperseg * p['nfft_factor']
for r, chan in enumerate(p['chans']):
ax = axs[r,c]
sig = raw.sel(chan = chan).values
f, t , Sxx = signal.spectrogram(sig, fs = srate , nperseg = nperseg , noverlap = noverlap, nfft = nfft, scaling = 'spectrum')
ax.pcolormesh(t,f,np.log(Sxx))
ax.set_title(f'{ses} - {chan}')
if c == 0:
ax.set_ylabel('Freq [Hz]')
if r == len(p['chans']) -1:
ax.set_xlabel('Time [s]')
fig.savefig(base_folder / 'Figures' / 'Autres' / 'artefact_mouche' / f'{sub}.png', dpi = 300)
plt.close()