Hi Alex
Here is the code snippet. I am reading from the EDF dataset that you have loaded as a sample and is also available here.
EPOCH = 30.
FMIN = 1.
FMAX = 45.
print('PSG file: %s' % psgFileListing.iloc[0]['psgInputFile'])
print('Annotation file: %s' % hypnogramFileListing.iloc[0]['hypnogramOutputFile'] )
hngFile['onset'] = pd.to_datetime(hngFile['onset'])
firstOnset = hngFile.iloc[0]['onset']
hngFile['onset'] = hngFile['onset'] - firstOnset
hngFile['onset'] = hngFile['onset'].dt.total_seconds()
onsets = (hngFile['onset']).to_numpy()
durations = hngFile['duration'].to_numpy()
descriptions = hngFile['description'].to_numpy()
hngFile = mne.Annotations(onset=onsets, # in seconds
duration=durations, # in seconds, too
description=descriptions)
psgFile.set_annotations(hngFile, emit_warning=True)
tmax = EPOCH - 1. / psgFile.info['sfreq']
channels = psgFile.info['ch_names']
picks = list(filter(lambda c: match(EEG_CHANNEL_REGEX, c), channels))
events, _ = mne.events_from_annotations(psgFile,event_id=ANNOTATIONS,chunk_duration=EPOCH)
epochs = mne.Epochs(raw=psgFile,events=events,event_id=ANNOTATIONS,picks=picks,tmin=0., tmax=tmax, baseline=None, on_missing='ignore', preload=True)
###############################################
# remove frequencies above FMAX e.g. 50/60 Hz power outlet signal
# need to do for all picks
###############################################
epochs = epochs.filter(FMIN,FMAX,picks=picks)
###############################################
# compute power spectrum returns ndarray nEpochs x nChannels x nFrequencies
# following this article we need to set window=4.0 as a configuration parameter for welch
# https://raphaelvallat.com/bandpower.html
###############################################
epochPs = epochs.compute_psd(method='welch', picks=picks, fmin=FMIN, fmax=FMAX, **{'window': 4.0})
picks=['EEG Fpz-Cz', 'EEG Pz-Oz']
and here is the trace:
Connected to pydev debugger (build 212.5457.46)
## processing study edf
subject SC400
PSG file: data/sleep/sleep-edf/input/edf/SC4001E0-PSG.edf
Annotation file: data/sleep/sleep-edf/output/SC400_1_hypnogram.csv
Used Annotations descriptions: ['N1', 'N2', 'N3', 'R', 'W']
Not setting metadata
2650 matching events found
No baseline correction applied
0 projection items activated
Loading data for 2650 events and 3000 original time points ...
0 bad epochs dropped
Setting up band-pass filter from 1 - 45 Hz
FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Upper passband edge: 45.00 Hz
- Upper transition bandwidth: 5.00 Hz (-6 dB cutoff frequency: 47.50 Hz)
- Filter length: 331 samples (3.310 sec)
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 5300 out of 5300 | elapsed: 1.2s finished
Effective window size : 2.560 (s)
Traceback (most recent call last):
File "/Users/rellis/Library/Application Support/JetBrains/IntelliJIdea2021.2/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/rellis/Library/Application Support/JetBrains/IntelliJIdea2021.2/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/rellis/dev/git/[project]/analysis/power-spectrum-measures.py", line 152, in <module>
epochPs = epochs.compute_psd(method='welch', picks=picks, fmin=FMIN, fmax=FMAX, **{'window': 4.0})
File "<decorator-gen-277>", line 12, in compute_psd
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/epochs.py", line 2031, in compute_psd
return EpochsSpectrum(
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/spectrum.py", line 1042, in __init__
self._compute_spectra(data, fmin, fmax, n_jobs, method_kw, verbose)
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/spectrum.py", line 380, in _compute_spectra
result = self._psd_func(
File "<decorator-gen-199>", line 12, in psd_array_welch
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/psd.py", line 184, in psd_array_welch
f_spect = parallel(my_spect_func(d, func=func, freq_sl=freq_sl,
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/psd.py", line 184, in <genexpr>
f_spect = parallel(my_spect_func(d, func=func, freq_sl=freq_sl,
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/psd.py", line 73, in _spect_func
spect = np.apply_along_axis(
File "<__array_function__ internals>", line 5, in apply_along_axis
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/numpy/lib/shape_base.py", line 379, in apply_along_axis
res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/mne/time_frequency/psd.py", line 52, in _decomp_aggregate_mask
_, _, spect = func(epoch)
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/scipy/signal/spectral.py", line 737, in spectrogram
window, nperseg = _triage_segments(window, nperseg,
File "/Users/rellis/opt/anaconda3/envs/rob-nme/lib/python3.9/site-packages/scipy/signal/spectral.py", line 1969, in _triage_segments
raise ValueError('window must be 1-D')
ValueError: window must be 1-D
python-BaseException
Process finished with exit code 1