Incompatible python/numpy/mne versions for mne.time_frequency.tfr_morlet()

  • MNE version: 1.5.1
  • NumPy version: 1.26.1
  • Python version: 3.9.13

Hello, I’m having an issue with getting the sample code found at the following url to run: Time-frequency on simulated data (Multitaper vs. Morlet vs. Stockwell vs. Hilbert) — MNE 1.6.1 documentation

The line power = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles, return_itc=False) gives me the error ‘ValueError: arange: cannot compute length’. I suspect this is an incompatibility issue between my python/numpy/mne versions --if so, which versions should I be using? Or is something else the problem? Any advice would be greatly appreciated!

Here is the code from the above page copied below, if it’s helpful:

# Test with sample data from the mne website

# Authors: Hari Bharadwaj <hari@nmr.mgh.harvard.edu>
#          Denis Engemann <denis.engemann@gmail.com>
#          Chris Holdgraf <choldgraf@berkeley.edu>
#          Alex Rockhill <aprockhill@mailbox.org>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import numpy as np
from matplotlib import pyplot as plt

from mne import Epochs, create_info
from mne.baseline import rescale
from mne.io import RawArray
from mne.time_frequency import (
    AverageTFR,
    tfr_array_morlet,
    tfr_morlet,
    tfr_multitaper,
    tfr_stockwell,
)
from mne.viz import centers_to_edges

print(__doc__)

sfreq = 1000.0
ch_names = ["SIM0001", "SIM0002"]
ch_types = ["grad", "grad"]
info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)

n_times = 1024  # Just over 1 second epochs
n_epochs = 40
seed = 42
rng = np.random.RandomState(seed)
data = rng.randn(len(ch_names), n_times * n_epochs + 200)  # buffer

# Add a 50 Hz sinusoidal burst to the noise and ramp it.
t = np.arange(n_times, dtype=np.float64) / sfreq
signal = np.sin(np.pi * 2.0 * 50.0 * t)  # 50 Hz sinusoid signal
signal[np.logical_or(t < 0.45, t > 0.55)] = 0.0  # hard windowing
on_time = np.logical_and(t >= 0.45, t <= 0.55)
signal[on_time] *= np.hanning(on_time.sum())  # ramping
data[:, 100:-100] += np.tile(signal, n_epochs)  # add signal

raw = RawArray(data, info)
events = np.zeros((n_epochs, 3), dtype=int)
events[:, 0] = np.arange(n_epochs) * n_times
epochs = Epochs(
    raw,
    events,
    dict(sin50hz=0),
    tmin=0,
    tmax=n_times / sfreq,
    reject=dict(grad=4000),
    baseline=None,
)

epochs.average().plot()

fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharey=True, layout="constrained")
all_n_cycles = [1, 3, freqs / 2.0]
for n_cycles, ax in zip(all_n_cycles, axs):
    power = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles, return_itc=False)
    power.plot(
        [0],
        baseline=(0.0, 0.1),
        mode="mean",
        vmin=vmin,
        vmax=vmax,
        axes=ax,
        show=False,
        colorbar=False,
    )
    n_cycles = "scaled by freqs" if not isinstance(n_cycles, int) else n_cycles
    ax.set_title(f"Sim: Using Morlet wavelet, n_cycles = {n_cycles}")

Personally I’m struggling to end up with the same versions in my local environment. But if I do:

$ mamba create -n my_env_name python=3.9.13
$ mamba activate my_env_name
$ pip install  numpy==1.26.1  # must use pip; 1.26.1 not avail on conda forge
$ mamba install mne=1.5.1  # this upgrades numpy to 1.26.3

then I can successfully run the example. So probably the simplest fix is to update numpy to 1.26.3. Is that an option for you?