Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Improve docstring format and support floats for conductivity in make_bem_model #12020

Merged
merged 40 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c728bd7
start changing coreg GUI to support inst directly
mscheltienne Sep 27, 2023
99670f6
fix style
mscheltienne Sep 27, 2023
bedf004
support conductivity as float
mscheltienne Sep 27, 2023
bb44491
better format
mscheltienne Sep 27, 2023
165c9d2
better format
mscheltienne Sep 27, 2023
b3e5eb7
better style
mscheltienne Sep 27, 2023
acf4932
fix type
mscheltienne Sep 27, 2023
238a462
better bib style
mscheltienne Sep 27, 2023
d9ab5f4
better bib style
mscheltienne Sep 27, 2023
2d01909
add r to docstring
mscheltienne Sep 27, 2023
6093719
Revert "start changing coreg GUI to support inst directly"
mscheltienne Sep 27, 2023
2959a10
use constrained_layout for plot_white
mscheltienne Sep 27, 2023
f4a8f62
fix style in covariance tutorial
mscheltienne Sep 27, 2023
66f19b6
improve citation style
mscheltienne Sep 27, 2023
fa7404f
better style
mscheltienne Sep 27, 2023
44d22ef
add ref to overview doc
mscheltienne Sep 27, 2023
3b36277
update link to something useful
mscheltienne Sep 27, 2023
1b44318
use note and code-block
mscheltienne Sep 27, 2023
9d950be
Update doc/_includes/bem_model.rst
mscheltienne Sep 27, 2023
c7dfecc
add x-ref [ci skip]
mscheltienne Sep 27, 2023
a9673ca
Merge branch 'dev' of https://github.com/mscheltienne/mne-python into…
mscheltienne Sep 27, 2023
13effea
include perma-link [ci skip]
mscheltienne Sep 27, 2023
f9462d9
add x-ref [ci skip]
mscheltienne Sep 27, 2023
5087b42
add x-ref [ci skip]
mscheltienne Sep 27, 2023
5356d09
better doc [ci skip]
mscheltienne Sep 27, 2023
651b031
fix typo [ci skip]
mscheltienne Sep 27, 2023
95035ce
add argument n_jobs to setup_volume_source_space [ci skip]
mscheltienne Sep 27, 2023
495b771
rm backslashes for bibliography
mscheltienne Sep 28, 2023
e41951a
Update mne/cov.py
mscheltienne Sep 28, 2023
e6c83b3
rm unneeded r"""
mscheltienne Sep 28, 2023
42044bf
Merge branch 'main' into dev
mscheltienne Sep 28, 2023
719c952
improvements to SourceSpaces
mscheltienne Sep 28, 2023
3317fac
fix style
mscheltienne Sep 28, 2023
866d9c3
check for constrained layout
mscheltienne Sep 28, 2023
d82af6e
Merge branch 'dev' of https://github.com/mscheltienne/mne-python into…
mscheltienne Sep 28, 2023
a721bb4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 28, 2023
0ca563a
fix typo
mscheltienne Sep 28, 2023
7f31f6a
apply suggestion from larsoner [ci skip]
mscheltienne Sep 28, 2023
aae79b9
remove layout section
mscheltienne Sep 28, 2023
66ed3d2
Merge branch 'dev' of https://github.com/mscheltienne/mne-python into…
mscheltienne Sep 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/_includes/bem_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Using the watershed algorithm

The watershed algorithm [Segonne *et al.*,
2004] is part of the FreeSurfer software.
The name of the program is mri_watershed .
The name of the program is ``mri_watershed``.
Its use in the MNE environment is facilitated by the script
:ref:`mne watershed_bem`.

Expand All @@ -39,6 +39,7 @@ a file called :file:`bem/watershed/ws.mgz` which contains the brain MRI
volume. Furthermore, ``mne watershed_bem`` script converts the scalp surface to
fif format and saves the result to :file:`bem/{<subject>}-head.fif`.

.. _bem_flash_algorithm:

Using FLASH images
~~~~~~~~~~~~~~~~~~
Expand All @@ -50,7 +51,7 @@ reconstructions but it is strongly recommended that they are collected at the
same time with the MPRAGEs or at least with the same scanner. For easy
co-registration, the images should have FOV, matrix, slice thickness, gap, and
slice orientation as the MPRAGE data. For information on suitable pulse
sequences, see :footcite:`FischlEtAl2004`.
sequences, see :footcite:t:`FischlEtAl2004`.

Creation of the BEM meshes using this method involves the following steps:

Expand Down
2 changes: 1 addition & 1 deletion doc/_includes/forward.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ MEG/EEG and MRI coordinate systems
:class:`~mne.SourceSpaces`, etc), information about the coordinate frame is
encoded as a constant integer value. The meaning of those integers is
determined `in the source code
<https://github.com/mne-tools/mne-python/blob/main/mne/io/constants.py#L186-L197>`__.
<https://github.com/mne-tools/mne-python/blob/079c868240a898204bf82b2f1bf0e04cdee75da1/mne/_fiff/constants.py#L263-L275>`__.

The coordinate systems used in MNE software (and FreeSurfer) and their
relationships are depicted in :ref:`coordinate_system_figure`. Except for the
Expand Down
2 changes: 1 addition & 1 deletion doc/_includes/ssp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ the brain, it is necessary to apply the projection to the forward solution in
the course of inverse computations.

For more information on SSP, please consult the references listed in
:footcite:`TescheEtAl1995,UusitaloIlmoniemi1997`.
:footcite:t:`TescheEtAl1995,UusitaloIlmoniemi1997`.

Estimation of the noise subspace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
49 changes: 30 additions & 19 deletions mne/bem.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@


class ConductorModel(dict):
"""BEM or sphere model."""
"""BEM or sphere model.

See :func:`~mne.make_bem_model` and :func:`~mne.make_bem_solution` to create a
:class:`mne.bem.ConductorModel`.
"""

def __repr__(self): # noqa: D105
if self["is_sphere"]:
Expand Down Expand Up @@ -646,30 +650,32 @@ def make_bem_model(
):
"""Create a BEM model for a subject.

Use :func:`~mne.make_bem_solution` to turn the returned surfaces into a
:class:`~mne.bem.ConductorModel` suitable for forward calculation.

.. note:: To get a single layer bem corresponding to the --homog flag in
the command line tool set the ``conductivity`` parameter
to a list/tuple with a single value (e.g. [0.3]).
to a float (e.g. ``0.3``).

Parameters
----------
subject : str
The subject.
%(subject)s
ico : int | None
The surface ico downsampling to use, e.g. ``5=20484``, ``4=5120``,
``3=1280``. If None, no subsampling is applied.
conductivity : array of int, shape (3,) or (1,)
conductivity : float | array of float of shape (3,) or (1,)
The conductivities to use for each shell. Should be a single element
for a one-layer model, or three elements for a three-layer model.
Defaults to ``[0.3, 0.006, 0.3]``. The MNE-C default for a
single-layer model would be ``[0.3]``.
single-layer model is ``[0.3]``.
%(subjects_dir)s
%(verbose)s

Returns
-------
surfaces : list of dict
The BEM surfaces. Use `make_bem_solution` to turn these into a
`~mne.bem.ConductorModel` suitable for forward calculation.
The BEM surfaces. Use :func:`~mne.make_bem_solution` to turn these into a
:class:`~mne.bem.ConductorModel` suitable for forward calculation.

See Also
--------
Expand All @@ -682,9 +688,11 @@ def make_bem_model(
-----
.. versionadded:: 0.10.0
"""
conductivity = np.array(conductivity, float)
conductivity = np.atleast_1d(conductivity).astype(float)
if conductivity.ndim != 1 or conductivity.size not in (1, 3):
raise ValueError("conductivity must be 1D array-like with 1 or 3 " "elements")
raise ValueError(
"conductivity must be a float or a 1D array-like with 1 or 3 elements"
)
Comment on lines -685 to +695
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First code-change in this PR, supporting floats directly for conductivity instead of array of shape (1, ) for single-layer model.

subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
subject_dir = subjects_dir / subject
bem_dir = subject_dir / "bem"
Expand Down Expand Up @@ -851,7 +859,8 @@ def make_sphere_model(
center will be calculated from the digitization points in info.
head_radius : float | str | None
If float, compute spherical shells for EEG using the given radius.
If 'auto', estimate an appropriate radius from the dig points in Info,
If ``'auto'``, estimate an appropriate radius from the dig points in the
:class:`~mne.Info` provided by the argument ``info``.
If None, exclude shells (single layer sphere model).
%(info)s Only needed if ``r0`` or ``head_radius`` are ``'auto'``.
relative_radii : array-like
Expand Down Expand Up @@ -1200,6 +1209,8 @@ def make_watershed_bem(
):
"""Create BEM surfaces using the FreeSurfer watershed algorithm.

See :ref:`bem_watershed_algorithm` for additional information.

Parameters
----------
subject : str
Expand All @@ -1209,9 +1220,9 @@ def make_watershed_bem(
volume : str
Defaults to T1.
atlas : bool
Specify the --atlas option for mri_watershed.
Specify the ``--atlas option`` for ``mri_watershed``.
gcaatlas : bool
Specify the --brain_atlas option for mri_watershed.
Specify the ``--brain_atlas`` option for ``mri_watershed``.
preflood : int
Change the preflood height.
show : bool
Expand Down Expand Up @@ -1425,7 +1436,7 @@ def read_bem_surfaces(
patch_stats : bool, optional (default False)
Calculate and add cortical patch statistics to the surfaces.
s_id : int | None
If int, only read and return the surface with the given s_id.
If int, only read and return the surface with the given ``s_id``.
An error will be raised if it doesn't exist. If None, all
surfaces are read and returned.
%(on_defects)s
Expand All @@ -1436,7 +1447,7 @@ def read_bem_surfaces(
Returns
-------
surf: list | dict
A list of dictionaries that each contain a surface. If s_id
A list of dictionaries that each contain a surface. If ``s_id``
is not None, only the requested surface will be returned.

See Also
Expand Down Expand Up @@ -1964,8 +1975,7 @@ def convert_flash_mris(

Parameters
----------
subject : str
Subject name.
%(subject)s
flash30 : bool | list of SpatialImage or path-like | SpatialImage | path-like
If False do not use 30-degree flip angle data.
The list of flash 5 echos to use. If True it will look for files
Expand Down Expand Up @@ -2084,10 +2094,11 @@ def make_flash_bem(
):
"""Create 3-Layer BEM model from prepared flash MRI images.

See :ref:`bem_flash_algorithm` for additional information.

Parameters
----------
subject : str
Subject name.
%(subject)s
overwrite : bool
Write over existing .surf files in bem folder.
show : bool
Expand Down
14 changes: 6 additions & 8 deletions mne/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def compute_covariance(
rank=None,
verbose=None,
):
"""Estimate noise covariance matrix from epochs.
r"""Estimate noise covariance matrix from epochs.
mscheltienne marked this conversation as resolved.
Show resolved Hide resolved

The noise covariance is typically estimated on pre-stimulus periods
when the stimulus onset is defined from events.
Expand Down Expand Up @@ -980,8 +980,8 @@ def compute_covariance(
Returns
-------
cov : instance of Covariance | list
The computed covariance. If method equals 'auto' or is a list of str
and return_estimators equals True, a list of covariance estimators is
The computed covariance. If method equals ``'auto'`` or is a list of str
and ``return_estimators=True``, a list of covariance estimators is
returned (sorted by log-likelihood, from high to low, i.e. from best
to worst).

Expand Down Expand Up @@ -1009,16 +1009,14 @@ def compute_covariance(
.. versionadded:: 0.16
* ``'ledoit_wolf'``
The Ledoit-Wolf estimator, which uses an
empirical formula for the optimal shrinkage value
:footcite:`LedoitWolf2004`.
empirical formula for the optimal shrinkage value :footcite:`LedoitWolf2004`.
* ``'oas'``
The OAS estimator :footcite:`ChenEtAl2010`, which uses a different
empricial formula for the optimal shrinkage value.

.. versionadded:: 0.16
* ``'shrunk'``
Like 'ledoit_wolf', but with cross-validation
for optimal alpha.
Like 'ledoit_wolf', but with cross-validation for optimal alpha.
* ``'pca'``
Probabilistic PCA with low rank :footcite:`TippingBishop1999`.
* ``'factor_analysis'``
Expand All @@ -1040,7 +1038,7 @@ def compute_covariance(
The ``method`` parameter allows to regularize the covariance in an
automated way. It also allows to select between different alternative
estimation algorithms which themselves achieve regularization.
Details are described in :footcite:`EngemannGramfort2015`.
Details are described in :footcite:t:`EngemannGramfort2015`.

For more information on the advanced estimation methods, see
:ref:`the sklearn manual <sklearn:covariance>`.
Expand Down
10 changes: 6 additions & 4 deletions mne/forward/_make_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,15 @@ def make_forward_solution(
src : path-like | instance of SourceSpaces
Either a path to a source space file or a loaded or generated
:class:`~mne.SourceSpaces`.
bem : path-like | dict
bem : path-like | ConductorModel
Filename of the BEM (e.g., ``"sample-5120-5120-5120-bem-sol.fif"``) to
use, or a loaded sphere model (dict).
use, or a loaded :class:`~mne.bem.ConductorModel`. See
:func:`~mne.make_bem_model` and :func:`~mne.make_bem_solution` to create a
:class:`mne.bem.ConductorModel`.
meg : bool
If True (Default), include MEG computations.
If True (default), include MEG computations.
eeg : bool
If True (Default), include EEG computations.
If True (default), include EEG computations.
mindist : float
Minimum distance of sources from inner skull surface (in mm).
ignore_ref : bool
Expand Down
14 changes: 8 additions & 6 deletions mne/minimum_norm/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ def apply_inverse(
use_cps=True,
verbose=None,
):
"""Apply inverse operator to evoked data.
r"""Apply inverse operator to evoked data.
mscheltienne marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
Expand Down Expand Up @@ -1896,15 +1896,17 @@ def make_inverse_operator(
%(info_not_none)s
Specifies the channels to include. Bad channels (in ``info['bads']``)
are not used.
forward : dict
Forward operator.
forward : Forward
mscheltienne marked this conversation as resolved.
Show resolved Hide resolved
Forward operator. See :func:`~mne.make_forward_solution` to create the operator.
noise_cov : instance of Covariance
The noise covariance matrix.
The noise covariance matrix. See :func:`~mne.compute_raw_covariance` and
:func:`~mne.compute_covariance` to compute the noise covariance matrix on
:classl:`~mne.io.Raw` and :class:`~mne.Epochs` respectively.
%(loose)s
%(depth)s
fixed : bool | 'auto'
Use fixed source orientations normal to the cortical mantle. If True,
the loose parameter must be "auto" or 0. If 'auto', the loose value
the loose parameter must be ``"auto"`` or ``0``. If ``'auto'``, the loose value
is used.
%(rank_none)s
%(use_cps)s
Expand Down Expand Up @@ -1951,7 +1953,7 @@ def make_inverse_operator(
and without this information.

For depth weighting, 0.8 is generally good for MEG, and between 2 and 5
is good for EEG, see :footcite:`LinEtAl2006a`.
is good for EEG, see :footcite:t:`LinEtAl2006a`.

References
----------
Expand Down
21 changes: 14 additions & 7 deletions mne/source_space/_source_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,8 @@ def setup_volume_source_space(
add_interpolator=True,
sphere_units="m",
single_volume=False,
n_jobs=None,
*,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second code change, add the argument n_jobs to setup_volume_source_space used by _make_volume_source_space.

mscheltienne marked this conversation as resolved.
Show resolved Hide resolved
verbose=None,
):
"""Set up a volume source space with grid spacing or discrete source space.
Expand All @@ -1662,12 +1664,12 @@ def setup_volume_source_space(
subject : str | None
Subject to process. If None, the path to the MRI volume must be
absolute to get a volume source space. If a subject name
is provided the T1.mgz file will be found automatically.
is provided the ``T1.mgz`` file will be found automatically.
Defaults to None.
pos : float | dict
Positions to use for sources. If float, a grid will be constructed
with the spacing given by ``pos`` in mm, generating a volume source
space. If dict, pos['rr'] and pos['nn'] will be used as the source
space. If dict, ``pos['rr']`` and ``pos['nn']`` will be used as the source
space locations (in meters) and normals, respectively, creating a
discrete source space.

Expand All @@ -1679,21 +1681,24 @@ def setup_volume_source_space(
volume source space can then be morphed onto the MRI volume
using this interpolator. If pos is a dict, this cannot be None.
If subject name is provided, ``pos`` is a float or ``volume_label``
are not provided then the ``mri`` parameter will default to 'T1.mgz'
are not provided then the ``mri`` parameter will default to ``'T1.mgz'``
or ``aseg.mgz``, respectively, else it will stay None.
sphere : ndarray, shape (4,) | ConductorModel | None
Define spherical source space bounds using origin and radius given
by (ox, oy, oz, rad) in ``sphere_units``.
by ``(Ox, Oy, Oz, rad)`` in ``sphere_units``.
Only used if ``bem`` and ``surface`` are both None. Can also be a
spherical ConductorModel, which will use the origin and radius.
None (the default) uses a head-digitization fit.
bem : path-like | None | ConductorModel
Define source space bounds using a BEM file (specifically the inner
skull surface) or a ConductorModel for a 1-layer of 3-layers BEM.
skull surface) or a :class:`~mne.bem.ConductorModel` for a 1-layer of 3-layers
BEM. See :func:`~mne.make_bem_model` and :func:`~mne.make_bem_solution` to
create a :class:`~mne.bem.ConductorModel`. If provided, ``surface`` must be
None.
surface : path-like | dict | None
Define source space bounds using a FreeSurfer surface file. Can
also be a dictionary with entries ``'rr'`` and ``'tris'``, such as
those returned by :func:`mne.read_surface`.
those returned by :func:`mne.read_surface`. If provided, ``bem`` must be None.
mindist : float
Exclude points closer than this distance (mm) to the bounding surface.
exclude : float
Expand Down Expand Up @@ -1725,6 +1730,7 @@ def setup_volume_source_space(
when many labels are used.

.. versionadded:: 0.21
%(n_jobs)s
mscheltienne marked this conversation as resolved.
Show resolved Hide resolved
%(verbose)s

Returns
Expand Down Expand Up @@ -1781,7 +1787,7 @@ def setup_volume_source_space(
)

if bem is not None and surface is not None:
raise ValueError('Only one of "bem" and "surface" should be ' "specified")
raise ValueError("Only one of 'bem' and 'surface' should be specified.")

if mri is None and subject is not None:
if volume_label is not None:
Expand Down Expand Up @@ -1908,6 +1914,7 @@ def setup_volume_source_space(
mindist,
mri,
volume_label,
n_jobs=n_jobs,
vol_info=vol_info,
single_volume=single_volume,
)
Expand Down
2 changes: 1 addition & 1 deletion mne/time_frequency/multitaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def psd_array_multitaper(
r"""Compute power spectral density (PSD) using a multi-taper method.

The power spectral density is computed with DPSS
tapers\ :footcite:p:`Slepian1978`.
tapers :footcite:p:`Slepian1978`.

Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions mne/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2542,8 +2542,8 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
_method_psd = r"""
method : ``'welch'`` | ``'multitaper'``{}
Spectral estimation method. ``'welch'`` uses Welch's
method\ :footcite:p:`Welch1967`, ``'multitaper'`` uses DPSS
tapers\ :footcite:p:`Slepian1978`.{}
method :footcite:p:`Welch1967`, ``'multitaper'`` uses DPSS
tapers :footcite:p:`Slepian1978`.{}
"""
docdict["method_plot_psd_auto"] = _method_psd.format(
" | ``'auto'``",
Expand Down Expand Up @@ -4660,7 +4660,7 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
docdict[
"trans"
] = f"""
trans : path-like | dict | instance of Transform | None
trans : path-like | dict | instance of Transform | ``"fsaverage"`` | None
{_trans_base}
If trans is None, an identity matrix is assumed.
"""
Expand Down
Loading
Loading