From bd0d627f0a9eba3eb879ab660fdac5e5479beb9a Mon Sep 17 00:00:00 2001 From: Mathieu Scheltienne Date: Mon, 25 Apr 2022 22:01:36 +0200 Subject: [PATCH] MRG: Add support for channel types, add .DS_Store to .gitignore, add a conversion to str for fname (#4) * Add .DS_Store to .gitignore. * Adding ch_types argument. * Add conversion of fname to string to fix error raised by savemat when a pathlib.Path is provided instead. * Remove useless channel type mapping. * Add support even if ch_locs is not provided. --- .gitignore | 1 + eeglabio/epochs.py | 2 +- eeglabio/raw.py | 14 ++++++++++---- eeglabio/utils.py | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 0de50a9..5b9189d 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,4 @@ dmypy.json cython_debug/ /doc/generated/ +.DS_Store diff --git a/eeglabio/epochs.py b/eeglabio/epochs.py index 7d02168..415e786 100644 --- a/eeglabio/epochs.py +++ b/eeglabio/epochs.py @@ -117,4 +117,4 @@ def export_set(fname, data, sfreq, events, tmin, tmax, ch_names, event_id=None, icawinv=[], icasphere=[], icaweights=[]) - savemat(fname, eeg_d, appendmat=False) + savemat(str(fname), eeg_d, appendmat=False) diff --git a/eeglabio/raw.py b/eeglabio/raw.py index 71e4c6a..8b7e966 100644 --- a/eeglabio/raw.py +++ b/eeglabio/raw.py @@ -6,7 +6,7 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None, - ref_channels="common"): + ref_channels="common", ch_types=None): """Export continuous raw data to EEGLAB's .set format. Parameters @@ -34,6 +34,8 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None, specific reference set. Note that this parameter is only used to inform EEGLAB of the existing reference, this method will not reference the data for you. + ch_types : list of str | None + Channel types e.g. ‘EEG’, ‘MEG’, ‘EMG’, ‘ECG’, ‘Events’, .. See Also -------- @@ -47,18 +49,22 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None, data = data * 1e6 # convert to microvolts + # channel types + ch_types = np.array(ch_types) if ch_types is not None \ + else np.repeat('', len(ch_names)) + if ch_locs is not None: # get full EEGLAB coordinates to export full_coords = cart_to_eeglab(ch_locs) # convert to record arrays for MATLAB format chanlocs = fromarrays( - [ch_names, *full_coords.T, np.repeat('', len(ch_names))], + [ch_names, *full_coords.T, ch_types], names=["labels", "X", "Y", "Z", "sph_theta", "sph_phi", "sph_radius", "theta", "radius", "sph_theta_besa", "sph_phi_besa", "type"]) else: - chanlocs = fromarrays([ch_names], names=["labels"]) + chanlocs = fromarrays([ch_names, ch_types], names=["labels", "type"]) if isinstance(ref_channels, list): ref_channels = " ".join(ref_channels) @@ -75,4 +81,4 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None, names=["type", "latency", "duration"]) eeg_d['event'] = events - savemat(fname, eeg_d, appendmat=False) + savemat(str(fname), eeg_d, appendmat=False) diff --git a/eeglabio/utils.py b/eeglabio/utils.py index 9164dd4..356fbfd 100644 --- a/eeglabio/utils.py +++ b/eeglabio/utils.py @@ -188,7 +188,8 @@ def export_mne_raw(inst, fname): else: cart_coords = None + ch_types = inst.get_channel_types() annotations = [inst.annotations.description, inst.annotations.onset, inst.annotations.duration] export_set(fname, inst.get_data(), inst.info['sfreq'], - inst.ch_names, cart_coords, annotations) + inst.ch_names, cart_coords, annotations, ch_types=ch_types)