From ff6bc1b1a59b8cc1c7de12bb1130b78958741349 Mon Sep 17 00:00:00 2001 From: Jack Zhang Date: Wed, 14 Apr 2021 04:19:14 -0700 Subject: [PATCH] Add sphinx docs. --- .gitignore | 1 + README.md | 2 +- docs/Makefile | 20 ++++++ docs/_templates/autosummary/class.rst | 9 +++ docs/_templates/autosummary/function.rst | 7 +++ docs/api.rst | 21 +++++++ docs/conf.py | 78 ++++++++++++++++++++++++ docs/epochs.rst | 17 ++++++ docs/examples.rst | 20 ++++++ docs/index.rst | 23 +++++++ docs/install.rst | 15 +++++ docs/make.bat | 35 +++++++++++ docs/raw.rst | 17 ++++++ docs/utils.rst | 20 ++++++ eeglabio/__init__.py | 3 +- eeglabio/epochs.py | 14 +++-- eeglabio/raw.py | 12 ++-- eeglabio/utils.py | 70 +++++++++++---------- setup.py | 2 +- 19 files changed, 342 insertions(+), 44 deletions(-) create mode 100644 docs/Makefile create mode 100644 docs/_templates/autosummary/class.rst create mode 100644 docs/_templates/autosummary/function.rst create mode 100644 docs/api.rst create mode 100644 docs/conf.py create mode 100644 docs/epochs.rst create mode 100644 docs/examples.rst create mode 100644 docs/index.rst create mode 100644 docs/install.rst create mode 100644 docs/make.bat create mode 100644 docs/raw.rst create mode 100644 docs/utils.rst diff --git a/.gitignore b/.gitignore index 3de86a0..fcef46d 100644 --- a/.gitignore +++ b/.gitignore @@ -142,3 +142,4 @@ dmypy.json # Cython debug symbols cython_debug/ +/docs/generated/ diff --git a/README.md b/README.md index 8ab8492..573620f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # eeglabio -Python I/O support for EEGLAB files +I/O support for EEGLAB files in Python. ### Installation diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d4bb2cb --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/_templates/autosummary/class.rst b/docs/_templates/autosummary/class.rst new file mode 100644 index 0000000..82b7549 --- /dev/null +++ b/docs/_templates/autosummary/class.rst @@ -0,0 +1,9 @@ +{{ fullname | escape | underline}} + +.. currentmodule:: {{ module }} + +.. autoclass:: {{ objname }} + :special-members: __contains__,__getitem__,__iter__,__len__,__add__,__sub__,__mul__,__div__,__neg__,__hash__ + :members: + +.. _sphx_glr_backreferences_{{ fullname }}: diff --git a/docs/_templates/autosummary/function.rst b/docs/_templates/autosummary/function.rst new file mode 100644 index 0000000..ef7a9a8 --- /dev/null +++ b/docs/_templates/autosummary/function.rst @@ -0,0 +1,7 @@ +{{ fullname | escape | underline}} + +.. currentmodule:: {{ module }} + +.. autofunction:: {{ objname }} + +.. _sphx_glr_backreferences_{{ fullname }}: diff --git a/docs/api.rst b/docs/api.rst new file mode 100644 index 0000000..47729b5 --- /dev/null +++ b/docs/api.rst @@ -0,0 +1,21 @@ +============= +API Reference +============= + +This is the reference for classes (``CamelCase`` names) and functions +(``underscore_case`` names) of eeglabio. + +.. container:: d-none + + :py:mod:`eeglabio`: + + .. automodule:: eeglabio + :no-members: + :no-inherited-members: + +.. toctree:: + :maxdepth: 2 + + raw + epochs + utils diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..ba059eb --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,78 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import os +import sys +currdir = os.path.dirname(__file__) +sys.path.append(os.path.abspath(os.path.join(currdir, '..'))) + +import eeglabio + +# -- Project information ----------------------------------------------------- + +project = 'eeglabio' +copyright = '2021, Jack Zhang' +author = 'Jack Zhang' + +# The full version, including alpha/beta/rc tags +release = eeglabio.__version__ + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.autosummary', + 'sphinx.ext.intersphinx', + 'numpydoc', + 'sphinx_copybutton', +] + +intersphinx_mapping = { + 'python': ('https://docs.python.org/3', None), + 'numpy': ('https://numpy.org/devdocs', None), + 'scipy': ('https://scipy.github.io/devdocs', None), + 'mne': ('https://mne.tools/dev', None) +} + +numpydoc_attributes_as_param_list = True +numpydoc_xref_param_type = True + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +autosummary_generate = True + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +# html_theme = 'alabaster' +html_theme = "pydata_sphinx_theme" +html_theme_options = { + "github_url": "https://github.com/jackz314/eeglabio", + "show_prev_next": True, +} + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] diff --git a/docs/epochs.rst b/docs/epochs.rst new file mode 100644 index 0000000..f73ad70 --- /dev/null +++ b/docs/epochs.rst @@ -0,0 +1,17 @@ + +Epochs +====== + + +:py:mod:`eeglabio.epochs`: + +.. automodule:: eeglabio.epochs + :no-members: + :no-inherited-members: + +.. currentmodule:: eeglabio.epochs + +.. autosummary:: + :toctree: generated/ + + export_set \ No newline at end of file diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 0000000..6dda13f --- /dev/null +++ b/docs/examples.rst @@ -0,0 +1,20 @@ +Example Usage with `MNE `_ +============================================= + +Export from :class:`mne.Epochs` to EEGLAB (``.set``): + + .. code-block:: python + + import mne + from eeglabio.utils import export_mne_epochs + epochs = mne.Epochs(...) + export_mne_epochs(epochs, "file_name.set") + +Export from :class:`mne.io.Raw` to EEGLAB (``.set``): + + .. code-block:: python + + import mne + from eeglabio.utils import export_mne_raw + raw = mne.io.read_raw(...) + export_mne_raw(raw, "file_name.set") diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..3d6c123 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,23 @@ +.. eeglabio documentation master file, created by + sphinx-quickstart on Tue Apr 13 18:47:08 2021. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +I/O support for `EEGLAB `_ files in Python +=============================================================== + +.. toctree:: + :maxdepth: 3 + :caption: Contents: + + Installation + Examples + API Reference + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/install.rst b/docs/install.rst new file mode 100644 index 0000000..ad70bb7 --- /dev/null +++ b/docs/install.rst @@ -0,0 +1,15 @@ +Installation +============ + +eeglabio requires Python version 3.6 or higher. +You can install MNE-Python using ``pip`` from `PyPI `_: + + .. code-block:: console + + pip install eeglabio # dependencies are numpy, scipy + +Alternatively, from `Test PyPI `_: + + .. code-block:: console + + pip install -i https://test.pypi.org/simple/ eeglabio \ No newline at end of file diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..2119f51 --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=_build + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/raw.rst b/docs/raw.rst new file mode 100644 index 0000000..1aec1bd --- /dev/null +++ b/docs/raw.rst @@ -0,0 +1,17 @@ + +Raw +=== + + +:py:mod:`eeglabio.raw`: + +.. automodule:: eeglabio.raw + :no-members: + :no-inherited-members: + +.. currentmodule:: eeglabio.raw + +.. autosummary:: + :toctree: generated/ + + export_set \ No newline at end of file diff --git a/docs/utils.rst b/docs/utils.rst new file mode 100644 index 0000000..0bf0397 --- /dev/null +++ b/docs/utils.rst @@ -0,0 +1,20 @@ + +Utils +===== + + +:py:mod:`eeglabio.utils`: + +.. automodule:: eeglabio.utils + :no-members: + :no-inherited-members: + +.. currentmodule:: eeglabio.utils + +.. autosummary:: + :toctree: generated/ + + cart_to_eeglab + cart_to_eeglab_sph + export_mne_epochs + export_mne_raw diff --git a/eeglabio/__init__.py b/eeglabio/__init__.py index 469420a..e4aebd7 100644 --- a/eeglabio/__init__.py +++ b/eeglabio/__init__.py @@ -1,5 +1,6 @@ from ._version import __version__ from . import epochs from . import raw +from . import utils -__all__ = [epochs, raw] +__all__ = ['epochs', 'raw', 'utils'] diff --git a/eeglabio/epochs.py b/eeglabio/epochs.py index 4acdfb7..691731a 100644 --- a/eeglabio/epochs.py +++ b/eeglabio/epochs.py @@ -13,12 +13,12 @@ def export_set(fname, data, sfreq, events, tmin, tmax, ch_names, ---------- fname : str Name of the export file. - data : np.ndarray, shape (n_epochs, n_channels, n_samples) + data : numpy.ndarray, shape (n_epochs, n_channels, n_samples) Data array containing epochs. Follows the same format as MNE Epochs' data array. sfreq : int sample frequency of data - events : np.ndarray, shape (n_events, 3) + events : numpy.ndarray, shape (n_events, 3) Event array, the first column contains the event time in samples, the second column contains the value of the stim channel immediately before the event/step, and the third column contains the event id. @@ -32,13 +32,17 @@ def export_set(fname, data, sfreq, events, tmin, tmax, ch_names, event_id : dict Names of conditions corresponding to event ids (last column of events). If None, event names will default to string versions of the event ids. - ch_locs : np.ndarray, shape (n_channels, 3) + ch_locs : numpy.ndarray, shape (n_channels, 3) Array containing channel locations in Cartesian coordinates (x, y, z) + See Also + -------- + .raw.export_set + Notes ----- - Channel locations are expanded to the full EEGLAB format - For more details see .io.utils.cart_to_eeglab_full_coords + Channel locations are expanded to the full EEGLAB format. + For more details see :func:`.utils.cart_to_eeglab_sph`. """ data = data * 1e6 # convert to microvolts diff --git a/eeglabio/raw.py b/eeglabio/raw.py index 4e0d584..5545aa9 100644 --- a/eeglabio/raw.py +++ b/eeglabio/raw.py @@ -12,14 +12,14 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None): ---------- fname : str Name of the export file. - data : np.ndarray, shape (n_epochs, n_channels, n_samples) + data : numpy.ndarray, shape (n_epochs, n_channels, n_samples) Data array containing epochs. Follows the same format as MNE Epochs' data array. sfreq : int sample frequency of data ch_names : list of str Channel names. - ch_locs : np.ndarray, shape (n_channels, 3) + ch_locs : numpy.ndarray, shape (n_channels, 3) Array containing channel locations in Cartesian coordinates (x, y, z) annotations : list, shape (3, n_annotations) List containing three annotation subarrays: @@ -28,10 +28,14 @@ def export_set(fname, data, sfreq, ch_names, ch_locs=None, annotations=None): third array (float) is duration (in seconds) This roughly follows MNE's Annotations structure. + See Also + -------- + .epochs.export_set + Notes ----- - Channel locations are expanded to the full EEGLAB format - For more details see .utils.cart_to_eeglab_full_coords + Channel locations are expanded to the full EEGLAB format. + For more details see :func:`.utils.cart_to_eeglab_sph`. """ data = data * 1e6 # convert to microvolts diff --git a/eeglabio/utils.py b/eeglabio/utils.py index 046e5d7..2a07006 100644 --- a/eeglabio/utils.py +++ b/eeglabio/utils.py @@ -4,23 +4,27 @@ def _xyz_cart_to_eeglab_sph(x, y, z): """Convert Cartesian coordinates to EEGLAB spherical coordinates. - Also see https://github.com/sccn/eeglab/blob/develop/functions/sigprocfunc/convertlocs.m - Parameters ---------- - x : np.ndarray, shape (n_points, ) + x : numpy.ndarray, shape (n_points, ) Array of x coordinates - y : np.ndarray, shape (n_points, ) + y : numpy.ndarray, shape (n_points, ) Array of y coordinates - z : np.ndarray, shape (n_points, ) + z : numpy.ndarray, shape (n_points, ) Array of z coordinates Returns ------- - sph_pts : np.ndarray, shape (n_points, 7) + sph_pts : numpy.ndarray, shape (n_points, 7) Array containing points in spherical coordinates (sph_theta, sph_phi, sph_radius, theta, radius, - sph_theta_besa, sph_phi_besa) + sph_theta_besa, sph_phi_besa) + + See Also + -------- + https://github.com/sccn/eeglab/blob/develop/functions/sigprocfunc/convertlocs.m + + https://www.mathworks.com/help/matlab/ref/cart2sph.html """ # noqa: E501 assert len(x) == len(y) == len(z) @@ -67,22 +71,27 @@ def topo2sph(theta, radius): return out -def _cart_to_eeglab_sph(cart): +def cart_to_eeglab_sph(cart): """Convert Cartesian coordinates to EEGLAB spherical coordinates. - - Also see https://github.com/sccn/eeglab/blob/develop/functions/sigprocfunc/convertlocs.m - + Implementation is based on + `EEGLAB's convertlocs `_ + and Matlab's `cart2sph `_ + Parameters ---------- - cart : np.ndarray, shape (n_points, 3) + cart : ndarray, shape (n_points, 3) Array containing points in Cartesian coordinates (x, y, z) Returns ------- - sph_pts : np.ndarray, shape (n_points, 7) + sph_pts : ndarray, shape (n_points, 7) Array containing points in spherical coordinates (sph_theta, sph_phi, sph_radius, theta, radius, - sph_theta_besa, sph_phi_besa) + sph_theta_besa, sph_phi_besa) + + See Also + -------- + cart_to_eeglab """ # noqa: E501 # based on transforms.py's _cart_to_sph() @@ -97,31 +106,32 @@ def cart_to_eeglab(cart): Parameters ---------- - cart : np.ndarray, shape (n_points, 3) + cart : numpy.ndarray, shape (n_points, 3) Array containing points in Cartesian coordinates (x, y, z) Returns ------- - full_coords : np.ndarray, shape (n_channels, 10) - xyz + spherical and polar coords - see cart_to_eeglab_full_coords for more detail. + full_coords : numpy.ndarray, shape (n_channels, 10) + xyz + spherical and polar coords. See :func:`cart_to_eeglab_sph` for + more detail. + + See Also + -------- + cart_to_eeglab_sph """ - return np.append(cart, _cart_to_eeglab_sph(cart), 1) # hstack + return np.append(cart, cart_to_eeglab_sph(cart), 1) # hstack def export_mne_epochs(inst, fname): - """Export Epochs to EEGLAB's .set format. + """Export MNE's Epochs instance to EEGLAB's .set format using + :func:`.epochs.export_set`. + Parameters ---------- inst : mne.BaseEpochs Epochs instance to save fname : str Name of the export file. - - Notes - ----- - Channel locations are expanded to the full EEGLAB format - For more details see .io.utils.cart_to_eeglab_full_coords """ from .epochs import export_set # load data first @@ -147,19 +157,15 @@ def export_mne_epochs(inst, fname): def export_mne_raw(inst, fname): - """Export Raw to EEGLAB's .set format. + """Export MNE's Raw instance to EEGLAB's .set format using + :func:`.raw.export_set`. Parameters ---------- - inst : mne.BaseRaw + inst : mne.io.BaseRaw Raw instance to save fname : str Name of the export file. - - Notes - ----- - Channel locations are expanded to the full EEGLAB format - For more details see pyeeglab.utils._cart_to_eeglab_full_coords """ from .raw import export_set # load data first diff --git a/setup.py b/setup.py index f6279e2..d02b727 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ version=version, author="Jack Zhang", author_email="zhangmengyu10@gmail.com", - description="Python I/O support for EEGLAB files", + description="I/O support for EEGLAB files in Python", license="BSD (3-clause)", long_description=long_description, long_description_content_type="text/markdown",