# error using mne.compute\_covariance and evoked response

**URL:** <https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093>\
**Category:** Support & Discussions\
**Tags:** covariance\
**Created:** [January 5, 2024, 7:38pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093 "2024-01-05T19:38:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![codZeus](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@codZeus](https://mne.discourse.group/u/codZeus)\
**Post date:** [January 5, 2024, 7:38pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/1 "2024-01-05T19:38:22Z")

</div>

Hi,

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

```python
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

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [January 5, 2024, 8:57pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/2 "2024-01-05T20:57:33Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![codZeus](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@codZeus](https://mne.discourse.group/u/codZeus)\
**Post date:** [January 5, 2024, 9:53pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/3 "2024-01-05T21:53:06Z")

</div>

I have done as requested. Thanks

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [January 8, 2024, 6:02pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/4 "2024-01-08T18:02:33Z")

</div>

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

```python
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:

```python
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`:

```python
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.

---

<div class="post-metadata">

**Author:** ![codZeus](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@codZeus](https://mne.discourse.group/u/codZeus)\
**Post date:** [January 8, 2024, 6:34pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/5 "2024-01-08T18:34:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [January 8, 2024, 6:41pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/6 "2024-01-08T18:41:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![codZeus](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@codZeus](https://mne.discourse.group/u/codZeus)\
**Post date:** [January 8, 2024, 6:46pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/7 "2024-01-08T18:46:39Z")

</div>

okay, I am trying it out now.

---

<div class="post-metadata">

**Author:** ![codZeus](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@codZeus](https://mne.discourse.group/u/codZeus)\
**Post date:** [January 8, 2024, 6:50pm UTC](https://mne.discourse.group/t/error-using-mne-compute-covariance-and-evoked-response/8093/8 "2024-01-08T18:50:21Z")

</div>

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

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

```

the issue is solved.
