Repository navigation
Description
Description of the problem
It looks like mne.io.read_raw_edf() might be using too much memory, at least if I interpret the output of Memray correctly. For example, loading this 163MB EDF file with preload=False consumes 691MB of RAM. With preload=True, the process requires 1.28GB. I've compared this to edfio.read_edf(), which uses only 319MB to read the file (corresponding to preload=True).
Steps to reproduce
- Install Memray using
pip install memray - Save the following code in a file called
mwe.py:import mne raw = mne.io.read_raw_edf("example_nl22_edfc.edf", preload=True)
- Run
memray run mwe.py - Run
memray tree memray-mwe.py.XXXXX.bin(replaceXXXXXwith the actual number in the file name)
Link to data
https://osf.io/download/jasgc/
Expected results
Memory consumption is not much higher than the file size (e.g. around 320MB like edfio.read_edf()). Or at least not much higher than the size of the data (620MB) if preloaded, and much lower if not preloaded.
Actual results
Memory consumption is 4x higher, and even for preload=False it is more than 2x higher than loading the entire file into memory with edfio.
Additional information
Platform Linux-6.7.1-arch1-1-x86_64-with-glibc2.38
Python 3.12.1 (main, Jan 24 2024, 20:39:08) [GCC 13.2.1 20230801]
Executable /home/clemens/.pyenv/versions/3.12.1/envs/mne/bin/python3.12
CPU (8 cores)
Memory 15.4 GB
Core
├☑ mne 1.7.0.dev79+ge0af51dec (devel, latest release is 1.6.1)
├☑ numpy 1.26.3 (OpenBLAS 0.3.23.dev with 8 threads)
├☑ scipy 1.12.0
├☑ matplotlib 3.8.2 (backend=TkAgg)
├☑ pooch 1.8.0
└☑ jinja2 3.1.3
Numerical (optional)
└☐ unavailable sklearn, numba, nibabel, nilearn, dipy, openmeeg, cupy, pandas
Visualization (optional)
└☐ unavailable pyvista, pyvistaqt, vtk, qtpy, ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify
Ecosystem (optional)
└☐ unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo
Activity
cbrnr commentedon Jan 25, 2024
Update: I should mention that the EDF file contains 193 channels with 421,070 samples each, which equals 193 * 421,070 = 81,266,510 float64 elements. 81,266,510 * 8 / 1024**2 ≈ 620MB.
drammock commentedon Jan 25, 2024
Sounds like edfio is using lower precision? is that enough of an explanation here, or do you want to keep digging?
cbrnr commentedon Jan 25, 2024
No,
edfiois also using double precision. Maybe something is off with the memray output, or maybe I'm reading it incorrectly. Don't you think it is odd that we use the memory required to hold the entire data whenpreload=False? And twice that amount forpreload=True?drammock commentedon Jan 25, 2024
yes I do. Sorry, my last reply was from a phone, I lost track of the fact that 691MB was the
preload=Falsecase. That is indeed weird / worth digging into.drammock commentedon Jan 25, 2024
But wait, I thought you said
edfiocorresponds to thepreload=Truecase? I think you've said each of these things, and it seems like at least one of them must be wrong:cbrnr commentedon Jan 25, 2024
Yeah, the last point must be incorrect, but that's what memray told me. I was probably misinterpreting the output, or maybe I screwed up the profiling somehow. I'll try to replicate.
cbrnr commentedon Jan 26, 2024
I tried another memory profiler, this time
memory-profiler, but I seem to get the same strange results:Running this script (saved as
mwe.py) withand then
produces this plot:
and output
The deltas for MNE do make some sense though, although ~140MB for
preload=Falseand ~776MB forpreload=Trueseem a bit too high, but not that concerning.So I guess all good on MNE's side here! Sorry for the noise!
cbrnr commentedon Jan 26, 2024
FWIW, the reason why
edfiouses less memory is because it loads theint16signals (as stored in an EDF file). It only converts tofloat64upon request (when accessing the signals).drammock commentedon Jan 26, 2024
Thanks for following up. Loading as int16 would be a big change for us that would be inconsistent with what we do elsewhere, so I'm afraid we may never match edfio mem usage
cbrnr commentedon Jan 26, 2024
That's totally fine. Their usage is a bit delayed, because once you start working with the actual data, you have to convert to float64.
hoechenberger commentedon Jan 29, 2024
Nice detective work!! 🕵️