forked from mne-tools/mne-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into export-evokeds-mff
* upstream/main: [MRG] change utils.logger.warning -> utils.warn (mne-tools#9434) FIX : rank computation from info now uses SSS proc history if only grad or mag are present (mne-tools#9435) MRG: Enable interpolation for all fNIRS types (mne-tools#9431) FIX: brain save_movie (mne-tools#9426) ENH: Add mne.export (mne-tools#9427) ENH: Test more on pre [skip circle] (mne-tools#9423) MRG, ENH: Speed up brain test (mne-tools#9422) MAINT: Update URL [ci skip] MNT: Reduce number of calls to _update (mne-tools#9407) MRG: Tutorial improvements (mne-tools#9416)
- Loading branch information
Showing
42 changed files
with
557 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
Exporting | ||
================ | ||
|
||
:py:mod:`mne.export`: | ||
|
||
.. automodule:: mne.export | ||
:no-members: | ||
:no-inherited-members: | ||
|
||
.. currentmodule:: mne.export | ||
|
||
.. autosummary:: | ||
:toctree: generated/ | ||
|
||
export_epochs | ||
export_raw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._export import export_raw, export_epochs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
# Authors: MNE Developers | ||
# | ||
# License: BSD (3-clause) | ||
|
||
import numpy as np | ||
|
||
from ..utils import _check_eeglabio_installed | ||
_check_eeglabio_installed() | ||
import eeglabio.raw # noqa: E402 | ||
import eeglabio.epochs # noqa: E402 | ||
|
||
|
||
def _export_raw(fname, raw): | ||
# load data first | ||
raw.load_data() | ||
|
||
# remove extra epoc and STI channels | ||
drop_chs = ['epoc'] | ||
if not (raw.filenames[0].endswith('.fif')): | ||
drop_chs.append('STI 014') | ||
|
||
ch_names = [ch for ch in raw.ch_names if ch not in drop_chs] | ||
cart_coords = _get_als_coords_from_chs(raw.info['chs'], drop_chs) | ||
|
||
annotations = [raw.annotations.description, | ||
raw.annotations.onset, | ||
raw.annotations.duration] | ||
eeglabio.raw.export_set( | ||
fname, data=raw.get_data(picks=ch_names), sfreq=raw.info['sfreq'], | ||
ch_names=ch_names, ch_locs=cart_coords, annotations=annotations) | ||
|
||
|
||
def _export_epochs(fname, epochs): | ||
_check_eeglabio_installed() | ||
# load data first | ||
epochs.load_data() | ||
|
||
# remove extra epoc and STI channels | ||
drop_chs = ['epoc', 'STI 014'] | ||
ch_names = [ch for ch in epochs.ch_names if ch not in drop_chs] | ||
cart_coords = _get_als_coords_from_chs(epochs.info['chs'], drop_chs) | ||
|
||
eeglabio.epochs.export_set( | ||
fname, data=epochs.get_data(picks=ch_names), | ||
sfreq=epochs.info['sfreq'], events=epochs.events, | ||
tmin=epochs.tmin, tmax=epochs.tmax, ch_names=ch_names, | ||
event_id=epochs.event_id, ch_locs=cart_coords) | ||
|
||
|
||
def _get_als_coords_from_chs(chs, drop_chs=None): | ||
"""Extract channel locations in ALS format (x, y, z) from a chs instance. | ||
Returns | ||
------- | ||
None if no valid coordinates are found (all zeros) | ||
""" | ||
if drop_chs is None: | ||
drop_chs = [] | ||
cart_coords = np.array([d['loc'][:3] for d in chs | ||
if d['ch_name'] not in drop_chs]) | ||
if cart_coords.any(): # has coordinates | ||
# (-y x z) to (x y z) | ||
cart_coords[:, 0] = -cart_coords[:, 0] # -y to y | ||
# swap x (1) and y (0) | ||
cart_coords[:, [0, 1]] = cart_coords[:, [1, 0]] | ||
else: | ||
cart_coords = None | ||
return cart_coords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# -*- coding: utf-8 -*- | ||
# Authors: MNE Developers | ||
# | ||
# License: BSD (3-clause) | ||
|
||
import os.path as op | ||
|
||
from ..utils import verbose, _validate_type | ||
|
||
|
||
@verbose | ||
def export_raw(fname, raw, fmt='auto', verbose=None): | ||
"""Export Raw to external formats. | ||
Supported formats: EEGLAB (set, uses :mod:`eeglabio`) | ||
%(export_warning)s | ||
Parameters | ||
---------- | ||
%(export_params_fname)s | ||
raw : instance of Raw | ||
The raw instance to export. | ||
%(export_params_fmt)s | ||
%(verbose)s | ||
Notes | ||
----- | ||
%(export_eeglab_note)s | ||
""" | ||
supported_export_formats = { # format : extensions | ||
'eeglab': ('set',), | ||
'edf': ('edf',), | ||
'brainvision': ('eeg', 'vmrk', 'vhdr',) | ||
} | ||
fmt = _infer_check_export_fmt(fmt, fname, supported_export_formats) | ||
|
||
if fmt == 'eeglab': | ||
from ._eeglab import _export_raw | ||
_export_raw(fname, raw) | ||
elif fmt == 'edf': | ||
raise NotImplementedError('Export to EDF format not implemented.') | ||
elif fmt == 'brainvision': | ||
raise NotImplementedError('Export to BrainVision not implemented.') | ||
|
||
|
||
@verbose | ||
def export_epochs(fname, epochs, fmt='auto', verbose=None): | ||
"""Export Epochs to external formats. | ||
Supported formats: EEGLAB (set, uses :mod:`eeglabio`) | ||
%(export_warning)s | ||
Parameters | ||
---------- | ||
%(export_params_fname)s | ||
epochs : instance of Epochs | ||
The epochs to export. | ||
%(export_params_fmt)s | ||
%(verbose)s | ||
Notes | ||
----- | ||
%(export_eeglab_note)s | ||
""" | ||
supported_export_formats = { | ||
'eeglab': ('set',), | ||
'edf': ('edf',), | ||
'brainvision': ('eeg', 'vmrk', 'vhdr',) | ||
} | ||
fmt = _infer_check_export_fmt(fmt, fname, supported_export_formats) | ||
|
||
if fmt == 'eeglab': | ||
from ._eeglab import _export_epochs | ||
_export_epochs(fname, epochs) | ||
elif fmt == 'edf': | ||
raise NotImplementedError('Export to EDF format not implemented.') | ||
elif fmt == 'brainvision': | ||
raise NotImplementedError('Export to BrainVision not implemented.') | ||
|
||
|
||
def _infer_check_export_fmt(fmt, fname, supported_formats): | ||
"""Infer export format from filename extension if auto. | ||
Raises error if fmt is auto and no file extension found, | ||
then checks format against supported formats, raises error if format is not | ||
supported. | ||
Parameters | ||
---------- | ||
fmt : str | ||
Format of the export, will only infer the format from filename if fmt | ||
is auto. | ||
fname : str | ||
Name of the target export file, only used when fmt is auto. | ||
supported_formats : dict of str : tuple/list | ||
Dictionary containing supported formats (as keys) and each format's | ||
corresponding file extensions in a tuple/list (e.g. 'eeglab': ('set',)) | ||
""" | ||
_validate_type(fmt, str, 'fmt') | ||
fmt = fmt.lower() | ||
if fmt == "auto": | ||
fmt = op.splitext(fname)[1] | ||
if fmt: | ||
fmt = fmt[1:].lower() | ||
# find fmt in supported formats dict's tuples | ||
fmt = next((k for k, v in supported_formats.items() if fmt in v), | ||
fmt) # default to original fmt for raising error later | ||
else: | ||
raise ValueError(f"Couldn't infer format from filename {fname}" | ||
" (no extension found)") | ||
|
||
if fmt not in supported_formats: | ||
supported = [] | ||
for format, extensions in supported_formats.items(): | ||
ext_str = ', '.join(f'*.{ext}' for ext in extensions) | ||
supported.append(f'{format} ({ext_str})') | ||
|
||
supported_str = ', '.join(supported) | ||
raise ValueError(f"Format '{fmt}' is not supported. " | ||
f"Supported formats are {supported_str}.") | ||
return fmt |
Oops, something went wrong.