mne-tools / mne-connectivity

Connectivity algorithms that leverage the MNE-Python API.
https://mne.tools/mne-connectivity/dev/index.html
BSD 3-Clause "New" or "Revised" License
68 stars 34 forks source link

Memory issues introduced in spectral_connectivity_time by #138 #174

Closed ruuskas closed 6 months ago

ruuskas commented 7 months ago

Describe the bug

It appears that upon the addition of multivariate connectivity methods in #138, the following was introduced on line 483 in mne_connectivity/spectral/time.py.

conn_patterns[m] = np.full((n_epochs, 2, len(source_idx), n_freqs),
                           np.nan)

and this was later changed to

conn_patterns[m] = np.full(
    (n_epochs, 2, n_cons, max_n_channels, n_freqs), np.nan)

in #142.

This leads to memory problems with real data. In my current case, the array becomes of size (7, 2, 100128, 100128, 32), which would require 32 TiB of memory. This problem does not occur with version 0.5.

Steps to reproduce

Try running the standard example with large number of signals (448 in my case) and frequencies.

Expected results

Code should run normally.

Actual results

I receive the following error:

File "<decorator-gen-290>", line 12, in spectral_connectivity_time
  File "*/miniconda3/envs/connectivity-pipeline/lib/python3.12/site-packages/mne_connectivity/spectral/time.py", line 543, in spectral_connectivity_time
    conn_patterns[m] = np.full(
                       ^^^^^^^^
  File "*/miniconda3/envs/connectivity-pipeline/lib/python3.12/site-packages/numpy/core/numeric.py", line 329, in full
    a = empty(shape, dtype, order)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 32.7 TiB for an array with shape (7, 2, 100128, 100128, 32) and data type float64

Additional information

I'm running MNE-Python v1.6 and MNE-Connectivity v0.6.

Pinging here already @adam2392 and @tsbinns. For a quick fix, I guess the conn_patterns array could be initialized only if a multivariate method is used?

tsbinns commented 7 months ago

Sorry for this @ruuskas, was an oversight on my part. I just submitted PR #175 to fix this by only initialising the array when it is required.