error using mne.compute_covariance and evoked response

Hi,

A follow up question to my previous one. I want to compute the covariance using mne.compute_covariance using the code snippets below

import numpy as np
import mne
import os.path as op
from mne.datasets import eegbci, fetch_fsaverage
from mne.minimum_norm import apply_inverse, make_inverse_operator

data = np.random.rand(24,251)  #Toy-data

# Download fsaverage files
fs_dir = fetch_fsaverage(verbose=True)
subjects_dir = op.dirname(fs_dir)

# The files live in:
subject = "fsaverage"
trans = "fsaverage"  # MNE has a built-in fsaverage transformation
src = op.join(fs_dir, "bem", "fsaverage-ico-5-src.fif")
bem = op.join(fs_dir, "bem", "fsaverage-5120-5120-5120-bem-sol.fif")

ch_names = ["Fp1", "Fp2", "F3", "F4", "C3", "C4", "P3", "P4", "O1", "O2", "F7", "F8", "T7", "T8", "P7", "P8", "Fz", "Cz", "Pz", "M1", "M2", "AFz", "CPz", "POz"]

montage = mne.channels.make_standard_montage('standard_1020')
info = mne.create_info(ch_names=ch_names, sfreq=1000, ch_types='eeg').set_montage(montage, match_case=False)

raw = mne.io.RawArray(data,info)
raw.set_eeg_reference(projection=True)

# Setting up source space and compute forward
fwd = mne.make_forward_solution(raw.info, trans=trans, src=src, bem=bem, eeg=True, mindist=5.0, n_jobs=None)

eventEEG = np.array([[ 0., 0., 0.], [251., 0., 1.]], dtype=int)

baseline = (0, 0)
reject = dict(eeg=150e-6)
epochs = mne.Epochs(raw, eventEEG,event_id=None,
                    tmin=0,tmax=251,proj=True,picks=("eeg"),
                    baseline=baseline,reject=reject)

noise_cov = mne.compute_covariance(epochs, tmin=0, tmax=251,
                                  method=["shrunk", "empirical"], rank=None, verbose=True)

# Compute the evoked response
evoked = epochs.average().pick("eeg")
evoked.plot(time_unit="s")
evoked.plot_topomap(times=np.linspace(0.05, 0.25, 5), ch_type="eeg")

The error I get says
ValueError Traceback (most recent call last) Cell In[18], line 1 ----> 1 noise_cov = mne.compute_covariance(epochs, tmin=0, tmax=251, 2 method=[“shrunk”, “empirical”], rank=None, verbose=True) File :10, in compute_covariance(epochs, keep_sample_mean, tmin, tmax, projs, method, method_params, cv, scalings, n_jobs, return_estimators, on_mismatch, rank, verbose) File [c:\Users\Starboy\AppData\Local\Programs\Python\Python312\Lib\site-packages\mne\cov.py:1157](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1157), in compute_covariance(failed resolving arguments) [1154](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1154) else: [1155](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1155) epochs = epochs[0] → [1157](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1157) epochs = np.hstack(epochs) [1158](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1158) n_samples_tot = epochs.shape[-1] [1159](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/mne/cov.py:1159) _check_n_samples(n_samples_tot, len(picks_meeg)) File [c:\Users\Starboy\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy\core\shape_base.py:359](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy/core/shape_base.py:359), in hstack(tup, dtype, casting) [357](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy/core/shape_base.py:357) return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting) [358](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy/core/shape_base.py:358) else: → [359](file:///C:/Users/Starboy/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy/core/shape_base.py:359) return _nx.concatenate(arrs, 1, dtype=dtype, casting=casting)
ValueError: need at least one array to concatenate

If you comment out the noise_cov, there is also an error in the evoked response.

An explanation and solution will be appreciated. Thanks

Could you please do a few things to help us out:

  1. format your code: there is a button that looks like </> in the edit interface; edit the post, highlight the code lines and then push that button.
  2. provide the entire traceback instead of just the error message. Without the traceback we don’t know which line or function call is the one causing the error
  3. provide a complete, minimal working example such that any user could copy-paste the code into their own python terminal and see the same error that you see. This means you can’t use variables like chNames that you defined elsewhere, and (if you’re not creating simulated data in the code) you should use one of MNE’s built-in datasets rather than your own data (or, if you can only get the error with your own data, provide a download link for a file that has the problem, and include code to load that file along with whatever other analysis code leads to the error)
1 Like

I have done as requested. Thanks

Thanks for providing a runnable code snippet with proper formatting. Here is a shorter script that still yields the error:

import mne
import numpy as np

data = np.random.rand(24, 251)  # Toy-data
ch_names = [
    "Fp1", "Fp2", "F3", "F4", "C3", "C4", "P3", "P4", "O1", "O2", "F7", "F8", "T7",
    "T8", "P7", "P8", "Fz", "Cz", "Pz", "M1", "M2", "AFz", "CPz", "POz",
]

montage = mne.channels.make_standard_montage("standard_1020")
info = mne.create_info(ch_names=ch_names, sfreq=1000, ch_types="eeg").set_montage(
    montage, match_case=False
)

raw = mne.io.RawArray(data, info)
raw.set_eeg_reference(projection=True)

eventEEG = np.array([[0.0, 0.0, 0.0], [251.0, 0.0, 1.0]], dtype=int)

baseline = (0, 0)
reject = dict(eeg=150e-6)
epochs = mne.Epochs(
    raw,
    eventEEG,
    event_id=None,
    tmin=0,
    tmax=251,
    proj=True,
    picks=("eeg"),
    baseline=baseline,
    reject=reject,
)

noise_cov = mne.compute_covariance(
    epochs, tmin=0, tmax=251, method=["shrunk", "empirical"], rank=None, verbose=True
)

(notably it doesn’t create the Forward operator, which takes a long time and isn’t necessary for the part of the code that is erroring. Check for things like this next time; excluding unnecessary lines is part of what makes example code minimal)

When I run this, I see the following output:

Creating RawArray with float64 data, n_channels=24, n_times=251
    Range : 0 ... 250 =      0.000 ...     0.250 secs
Ready.
EEG channel type selected for re-referencing
Adding average EEG reference projection.
1 projection items deactivated
Average reference projection was added, but has not been applied yet. Use the apply_proj method to apply it.
Not setting metadata
2 matching events found
Applying baseline correction (mode: mean)
Created an SSP operator (subspace dimension = 1)
1 projection items activated
Using data from preloaded Raw for 1 events and 251001 original time points ...
1 bad epochs dropped
<ipython-input-1-fd8db493038c>:34: RuntimeWarning: All epochs were dropped!
You might need to alter reject/flat-criteria or drop bad channels to avoid this. You can use Epochs.plot_drop_log() to see which channels are responsible for the dropping of epochs.
  noise_cov = mne.compute_covariance(
Using data from preloaded Raw for 1 events and 251001 original time points ...
1 bad epochs dropped
<ipython-input-1-fd8db493038c>:34: RuntimeWarning: All epochs were dropped!
You might need to alter reject/flat-criteria or drop bad channels to avoid this. You can use Epochs.plot_drop_log() to see which channels are responsible for the dropping of epochs.
  noise_cov = mne.compute_covariance(
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 34
     21 reject = dict(eeg=150e-6)
     22 epochs = mne.Epochs(
     23     raw,
     24     eventEEG,
   (...)
     31     reject=reject,
     32 )
---> 34 noise_cov = mne.compute_covariance(
     35     epochs, tmin=0, tmax=251, method=["shrunk", "empirical"], rank=None, verbose=True
     36 )

File <decorator-gen-270>:10, in compute_covariance(epochs, keep_sample_mean, tmin, tmax, projs, method, method_params, cv, scalings, n_jobs, return_estimators, on_mismatch, rank, verbose)

File /opt/mne/python/mne/cov.py:1157, in compute_covariance(***failed resolving arguments***)
   1154 else:
   1155     epochs = epochs[0]
-> 1157 epochs = np.hstack(epochs)
   1158 n_samples_tot = epochs.shape[-1]
   1159 _check_n_samples(n_samples_tot, len(picks_meeg))

File /opt/mambaforge/envs/mnedev/lib/python3.11/site-packages/numpy/core/shape_base.py:359, in hstack(tup, dtype, casting)
    357     return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
    358 else:
--> 359     return _nx.concatenate(arrs, 1, dtype=dtype, casting=casting)

ValueError: need at least one array to concatenate

The key information is the RuntimeWarning that happens a few lines above the ValueError:

RuntimeWarning: All epochs were dropped!
You might need to alter reject/flat-criteria or drop bad channels to avoid this. You can use Epochs.plot_drop_log() to see which channels are responsible for the dropping of epochs.

You cannot compute the covariance when you have zero epochs.

that’s true. I also found out that the data was no showing in the epoch.

Looking closer: the error happens even if skipping the reject param to Epochs. The problem is your tmin/tmax. You’re asking for an epoch that is 251 seconds long, but your data is only 0.25 seconds long. If I change tmax=0.2 for example, it works.

okay, I am trying it out now.

I tried changing the time and still had the ValueError: need at least one array to concatenate.
However, if i use

mne.make_fixed_length_epochs(raw, duration=0.251, preload=False)

the issue is solved.