Dear Mathieu, thank you for your answer!
I am trying to write some code according to the tutorials you mentioned, but still get the result I can’t interprete properly and use further. I’ve alredy tried mne.time_frequency.psd_welch() and mne.time_frequency.psd_array_welch() - and I feel I really need help with it.
Here is my code snippets
`#to get epochs
epochs = mne.make_fixed_length_epochs(raw,
duration=1.0,
preload=True,
reject_by_annotation=True,
proj=True, overlap=0.0, id=1,
verbose=None)`
# what I get
Not setting metadata
Not setting metadata
925 matching events found
No baseline correction applied
0 projection items activated
Loading data for 925 events and 250 original time points ...
0 bad epochs dropped
(Excuse me for this question, but I failed to find any info on what original time points may refer to. Could you please help with it? When I try the duration of an epoch = 1, I get 250 original time points, and when I change the duration to 0.1 I get 25. It obviously is connected with signal length - they seem to be equal. And I have to manipulate n_fft so that it is kept less than signal length - however, I am really far from understanding what I actually do. It only looks like some proportion for me - is it about ms, I mean, I get 250 ms for 1000 ms (as 1 s) and 25 ms for 100 ms (as 0.1 s)?..
In addition, when I try both of the functions above with n_fft = 256 I get the following error (and I couldn’t find any info about signal length and how to deal with it)
ValueError: If n_per_seg is None n_fft is not allowed to be > n_times. If you want zero-padding, you have to set n_per_seg to relevant length. Got n_fft of 256 while signal length is 250.
I wonder what relevant length may be - and how to set it properly if I am going to work with really short times, something like 100 ms as the idea is to use the code for alpha/beta ratios in a neurofeedback project)
# to get data (frequensies and other) for epochs
epochs_data = epochs.get_data()
print(epochs_data)
# what I get
[[ 4.89546426e-06 5.35417966e-06 9.02390286e-06 ... -2.74979240e-06
-4.89046426e-06 -3.05560266e-06]
[-7.48985153e-06 -3.66722320e-06 -9.14930799e-07 ... -2.29107700e-06
-4.73755913e-06 -1.52655133e-06]
[-8.56018746e-06 -4.43174886e-06 -2.29107700e-06 ... -1.50405133e-07
3.08310266e-07 2.60188726e-06]
# to get a mean PSD per epoch for alpha and betaI tried mne.time_frequency.psd_welch()
a_mean_psd_per_epoch = mne.time_frequency.psd_welch(epochs, fmin = 8, fmax = 12, tmin=None, tmax=None, n_fft=128, n_overlap=0, n_per_seg=None, picks=None, proj=False, n_jobs=1, reject_by_annotation=True, average='mean', window='hamming', verbose=None) # alpha band as from 8 to 12
print(a_mean_psd_per_epoch)
I guess I get an array of digits in scientific notation that should be organized as (n_epochs, n_channels, n_freqs), but I feel confused. I expected to see something like (from…time to …time fore epochs, some info on channels for channels, from…Hz to… Hz gfor frequences).
And still I wonder about how to get mean PSD for each epoch (I feel really ashamed when asking, but I’ve spent last several days trying and I feel I realy need help.)
What I tried in addition was mne.time_frequency.psd_array_welch()
a_mean_psd_per_epoch = mne.time_frequency.psd_array_welch(epochs_data, sfreq, fmin=8, fmax=12, n_fft=128, n_overlap=0, n_per_seg=None, n_jobs=1, average='mean', window='hamming', verbose=None) # alpha band as from 8 to 12
b_mean_psd_per_epoch = mne.time_frequency.psd_array_welch(epochs_data, sfreq, fmin=12, fmax=30, n_fft=128, n_overlap=0, n_per_seg=None, n_jobs=1, average='mean', window='hamming', verbose=None) # beta band as from 12 to 30
print(b_mean_psd_per_epoch)
# what I get
(array([[[2.65289317e-13, 1.09139047e-14, 6.59360158e-17, ...,
2.45742191e-17, 1.94163313e-17, 1.79952153e-17],
[2.09750394e-13, 1.82353709e-14, 7.30799291e-17, ...,
2.49602267e-17, 1.98825327e-17, 1.78651880e-17],
[1.62082601e-13, 2.85777765e-14, 2.15560421e-16, ...,
1.51339585e-16, 1.22775214e-16, 1.06054944e-16],
...,
[2.24421155e-13, 2.89985202e-15, 3.19432076e-17, ...,
7.78661742e-18, 6.37864350e-18, 5.88329187e-18],
[6.17609215e-15, 2.69767717e-15, 2.88291357e-18, ...,
3.38447309e-18, 3.27000383e-18, 2.41686553e-18],
[2.10243824e-13, 4.89146983e-15, 3.33343894e-16, ...,
1.97866715e-16, 1.65439481e-16, 1.47644537e-16]],
…
And I guess I don’t really get the difference between mne.time_frequency.psd_welch() and mne.time_frequency.psd_array_welch()
All the code above was written to finally get alpha/beta ratio for each epoch - and I hoped to do it like that:
#to get alpha/beta ratio
def get_ab_ratio(a_mean_psd_per_epoch, b_mean_psd_per_epoch):
return a_mean_psd_per_epoch/ b_mean_psd_per_epoch
for epoch in a_mean_psd_per_epoch
epoch(get_ab_ratio)
I understand this text is already really long and seems to be overloaded with questions, but I have one more. Is it somehow possible to get a dataframe with epochs info, PSD for each of them, and ab ratio for each of them?
my colab
my edf
I will be really thankful for help.
Best regards,
Arianna