Skip to content

Commit 8ca59e4

Browse files
[EHN] creating the metainformation file to allow lazy loading by default (#688)
* [EHN] creating the metainformation file to allow lazy loading by default * [EHN] whats_new file * [EHN] moving to the father class * updating the whats_new.rst file * fixing the tests * [EHN] moving to utils and changing to tests * Apply suggestions from code review Signed-off-by: Bru <b.aristimunha@gmail.com> * mistake with whats_new.rst file --------- Signed-off-by: Bru <b.aristimunha@gmail.com>
1 parent ae5c715 commit 8ca59e4

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/source/whats_new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Develop branch - 1.2.1
1818
Enhancements
1919
~~~~~~~~~~~~
2020
- Reordering the examples in the documentation (:gh:`807` by `Bruno Aristimunha`_)
21+
- Creating the meta information for the BIDS converted datasets (:gh:`688` by `Bruno Aristimunha`_)
2122

2223

2324
Bugs

moabb/datasets/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Utils for easy database selection."""
22

33
import inspect
4+
from pathlib import Path
45

56
import mne
7+
import mne_bids
68
import numpy as np
79
from mne import create_info
810
from mne.io import RawArray
@@ -323,3 +325,29 @@ def stim_channels_with_selected_ids(
323325
raw_with_stim = raw.copy().add_channels([stim_raw], force_update_info=True)
324326

325327
return raw_with_stim
328+
329+
330+
def bids_metainfo(bids_path: Path) -> dict:
331+
"""Create metadata for the BIDS dataset.
332+
333+
To allow lazy loading of the metadata, we store the metadata in a JSON file
334+
in the root of the BIDS dataset.
335+
336+
Parameters
337+
----------
338+
bids_path : Path
339+
The path to the BIDS dataset.
340+
"""
341+
json_data = {}
342+
343+
paths = mne_bids.find_matching_paths(
344+
root=bids_path,
345+
datatypes="eeg",
346+
)
347+
348+
for path in paths:
349+
uid = path.fpath.name
350+
json_data[uid] = path.entities
351+
json_data[uid]["fpath"] = str(path.fpath)
352+
353+
return json_data

moabb/tests/test_datasets.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import mne
88
import numpy as np
9+
import pandas as pd
910
import pytest
1011

1112
import moabb.datasets as db
@@ -20,7 +21,7 @@
2021
from moabb.datasets.compound_dataset import CompoundDataset
2122
from moabb.datasets.compound_dataset.utils import compound_dataset_list
2223
from moabb.datasets.fake import FakeDataset, FakeVirtualRealityDataset
23-
from moabb.datasets.utils import block_rep, dataset_list
24+
from moabb.datasets.utils import bids_metainfo, block_rep, dataset_list
2425
from moabb.paradigms import P300
2526
from moabb.utils import aliases_list
2627

@@ -208,6 +209,14 @@ def test_cache_dataset(self):
208209
assert len(expected) == len(cm.output)
209210
for i, regex in enumerate(expected):
210211
self.assertRegex(cm.output[i], regex)
212+
213+
metainfo = bids_metainfo(tempdir)
214+
dataframe = pd.DataFrame(metainfo).T
215+
subjects = dataframe["subject"].unique()
216+
self.assertEqual(len(subjects), 1)
217+
self.assertEqual(subjects[0], "1")
218+
self.assertEqual(len(dataframe["session"].unique()), 2)
219+
211220
shutil.rmtree(tempdir)
212221

213222
def test_dataset_accept(self):

0 commit comments

Comments
 (0)