From 19b3c8e819d16a582e8169134a1602604105de09 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Wed, 25 Sep 2024 14:45:51 +0200 Subject: [PATCH] add participant data sanitizing --- mne_bids/write.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mne_bids/write.py b/mne_bids/write.py index b7cf34d02..ca9373729 100644 --- a/mne_bids/write.py +++ b/mne_bids/write.py @@ -511,6 +511,27 @@ def _participants_tsv(raw, subject_id, fname, overwrite=False): } ) + # Make sure that all entries to data are lists that + # contain scalars (i.e. not further lists). Fix if possible + for key in data.keys(): + cur_value = data[key] + if not isinstance(cur_value, list): + cur_value = [cur_value] + + # Check if all values are scalars + new_value = [] + for cur_item in cur_value: + if isinstance(cur_item, list | tuple | np.ndarray): + if len(cur_item) == 1: + new_value.append(cur_item[0]) + else: + raise ValueError(f"Value for key {key} is a list with more " + f"than one element. This is not supported.") + else: + new_value.append(cur_item) + + data[key] = new_value + if os.path.exists(fname): orig_data = _from_tsv(fname) # whether the new data exists identically in the previous data