Dataset Management#

Load the tutorial dataset, modify it, store the result as a second dataset, switch between datasets, save one to disk, and delete it from memory. This is the scripted form of File > Load existing dataset, Datasets > ..., File > Save current dataset as, and Edit > Delete dataset(s) from memory.

Load the tutorial dataset into an empty dataset list.

import tempfile
from pathlib import Path

import matplotlib

matplotlib.use("Agg")

import eegprep
from eegprep import (  # noqa: E402
    EEGPrepSession,
    eeg_retrieve,
    pop_copyset,
    pop_delset,
    pop_editset,
    pop_loadset,
    pop_newset,
    pop_resample,
    pop_saveset,
)

REPO_ROOT = Path(eegprep.__file__).resolve().parents[2]  # sphinx-gallery defines no __file__
TUTORIAL_SET = REPO_ROOT / "sample_data" / "eeglab_data.set"

EEG = pop_loadset(str(TUTORIAL_SET))
ALLEEG, EEG, CURRENTSET, com = pop_newset([], EEG, 0, setname="tutorial continuous")
print("loaded:", EEG["setname"], EEG["data"].shape, "srate", EEG["srate"])
print("CURRENTSET:", CURRENTSET, "datasets in memory:", len(ALLEEG))
print("history:", com)
loaded: tutorial continuous (32, 30504) srate 128.0
CURRENTSET: 1 datasets in memory: 1
history: [ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, 'setname', 'tutorial continuous');

Modify the dataset, then store the result as a new dataset instead of overwriting dataset 1. This is what the pop_newset dialog does when you leave “Overwrite it in memory” unchecked.

EEG_resampled, resample_com = pop_resample(EEG, 64, return_com=True)
ALLEEG, EEG, CURRENTSET, com = pop_newset(ALLEEG, EEG_resampled, CURRENTSET, setname="tutorial 64 Hz", overwrite="off")
print(resample_com)
print("CURRENTSET:", CURRENTSET, "datasets in memory:", len(ALLEEG))
EEG = pop_resample( EEG, 64);
CURRENTSET: 2 datasets in memory: 2

The Datasets menu is built from this list.

session = EEGPrepSession()
session.ALLEEG = ALLEEG
session.store_current(EEG, index=CURRENTSET)
for index, label, selected in session.dataset_summaries():
    print(("* " if selected else "  ") + label)
  Dataset 1:tutorial continuous
* Dataset 2:tutorial 64 Hz

Switch back to dataset 1 (Datasets > Dataset 1:...), then forward again.

EEG, ALLEEG, CURRENTSET = eeg_retrieve(ALLEEG, 1)
print("retrieved:", EEG["setname"], EEG["data"].shape, "srate", EEG["srate"])
ALLEEG, EEG, CURRENTSET, com = pop_newset(ALLEEG, EEG, CURRENTSET, retrieve=2)
print("retrieved:", EEG["setname"], EEG["data"].shape, "srate", EEG["srate"])
print("history:", com)
retrieved: tutorial continuous (32, 30504) srate 128.0
retrieved: tutorial 64 Hz (32, 15252) srate 64.0
history: [ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, 'retrieve', 2);

Rename the current dataset (Edit > Dataset info). Editing EEG alone does not update ALLEEG; store it back with overwrite="on", which is the “Overwrite it in memory” checkbox of the pop_newset dialog.

EEG, editset_com = pop_editset(EEG, setname="tutorial 64 Hz (renamed)", return_com=True)
print(editset_com)
print("EEG:", EEG["setname"], "| ALLEEG[2]:", ALLEEG[1]["setname"])

ALLEEG, EEG, CURRENTSET, com = pop_newset(ALLEEG, EEG, CURRENTSET, overwrite="on")
print("after overwrite -> ALLEEG[2]:", ALLEEG[1]["setname"], "| datasets:", len(ALLEEG))
print("history:", com)
EEG = pop_editset(EEG, 'setname', 'tutorial 64 Hz (renamed)');
EEG: tutorial 64 Hz (renamed) | ALLEEG[2]: tutorial 64 Hz
after overwrite -> ALLEEG[2]: tutorial 64 Hz (renamed) | datasets: 2
history: [ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, CURRENTSET, 'overwrite', 'on');

Copy a dataset to a new slot (Edit > Copy current dataset).

ALLEEG, EEG, CURRENTSET, copy_com = pop_copyset(ALLEEG, 2, 3, return_com=True)
print("datasets in memory:", len(ALLEEG), "CURRENTSET:", CURRENTSET)
print(copy_com)
datasets in memory: 3 CURRENTSET: 3
[ALLEEG EEG CURRENTSET LASTCOM] = pop_copyset(ALLEEG, 2, 3);

Save the current dataset (File > Save current dataset as). A plain save writes a single .set file; savemode="twofiles" writes .set plus a float32 .fdt sidecar.

with tempfile.TemporaryDirectory() as tmpdir:
    one_file = Path(tmpdir) / "dataset_mgmt_one.set"
    two_file = Path(tmpdir) / "dataset_mgmt_two.set"
    pop_saveset(EEG, str(one_file))
    pop_saveset(EEG, str(two_file), savemode="twofiles")
    print("one-file:", sorted(p.name for p in Path(tmpdir).glob("dataset_mgmt_one.*")))
    print("two-file:", sorted(p.name for p in Path(tmpdir).glob("dataset_mgmt_two.*")))
    reloaded = pop_loadset(str(two_file))
    print("reloaded:", reloaded["setname"], reloaded["data"].shape)
one-file: ['dataset_mgmt_one.set']
two-file: ['dataset_mgmt_two.fdt', 'dataset_mgmt_two.set']
reloaded: tutorial 64 Hz (renamed) (32, 15252)

Delete datasets from memory (Edit > Delete dataset(s) from memory or File > Clear dataset(s)). As in EEGLAB, the slot is emptied in place, so the remaining datasets keep their numbers; trailing empty slots are dropped.

ALLEEG, delete_com = pop_delset(ALLEEG, [2])
print("dataset numbers still in memory:", [index for index, eeg in enumerate(ALLEEG, start=1) if eeg])
print(delete_com)
dataset numbers still in memory: [1, 3]
ALLEEG = pop_delset( ALLEEG, [2] );

Total running time of the script: (0 minutes 0.187 seconds)

Gallery generated by Sphinx-Gallery