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

FIX/DOC: read_ch_adjacency and "picks" argument #12066

Merged
merged 27 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0b6afb9
mne.channels.read_ch_adjacency 'picks' argument does not function as …
zubara Oct 4, 2023
915f3b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
e050c3d
Merge branch 'main' into main
zubara Oct 4, 2023
3e2adf7
updated devel.rst and test_channels.py to include channels subset sel…
zubara Oct 4, 2023
7e6fb48
Update mne/channels/channels.py
zubara Oct 4, 2023
311bde8
update to mne/channels/tests/test_channels.py
zubara Oct 4, 2023
7bfd028
merge with pre-commit changes
zubara Oct 4, 2023
e4abce2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
1e5b665
fixed typo in devel.rst
zubara Oct 4, 2023
f420981
Merge branch 'main' of https://github.com/zubara/mne-python
zubara Oct 4, 2023
5ef52e4
more typos to devel.rst
zubara Oct 4, 2023
ab01d2d
Docdict-based docstring in mne.channels.read_ch_adjacency
zubara Oct 4, 2023
7c72a26
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2023
6276831
Merge branch 'main' into main
zubara Oct 4, 2023
b8fde4f
style fixes to /utils/docs.py
zubara Oct 5, 2023
883eae0
Merge branch 'main' of https://github.com/zubara/mne-python
zubara Oct 5, 2023
ac2903e
Merge branch 'main' into main
zubara Oct 5, 2023
8a1c068
style fixes to /utils/docs.py#2
zubara Oct 5, 2023
45d3433
Merge branch 'main' of https://github.com/zubara/mne-python
zubara Oct 5, 2023
52b6863
Update mne/channels/tests/test_channels.py
zubara Oct 5, 2023
c851521
style fix in docs.py
zubara Oct 5, 2023
44ec2e1
Added: test_read_builtin_ch_adjacency_picks
zubara Oct 5, 2023
c91b178
merged test_channels
zubara Oct 5, 2023
c23239f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
65e5096
style fix
zubara Oct 5, 2023
56057dd
Merge branch 'main' of https://github.com/zubara/mne-python
zubara Oct 5, 2023
465add4
Merge branch 'main' into main
zubara Oct 5, 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
1 change: 1 addition & 0 deletions doc/changes/devel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Bugs
- Fix bugs with :func:`mne.preprocessing.realign_raw` where the start of ``other`` was incorrectly cropped; and onsets and durations in ``other.annotations`` were left unsynced with the resampled data (:gh:`11950` by :newcontrib:`Qian Chu`)
- Fix bug where ``encoding`` argument was ignored when reading annotations from an EDF file (:gh:`11958` by :newcontrib:`Andrew Gilbert`)
- Mark tests ``test_adjacency_matches_ft`` and ``test_fetch_uncompressed_file`` as network tests (:gh:`12041` by :newcontrib:`Maksym Balatsko`)
- Fix bug with :func:`mne.channels.read_ch_adjacency` (:gh:`11608` by :newcontrib:`Ivan Zubarev`)
- Fix bugs with saving splits for :class:`~mne.Epochs` (:gh:`11876` by `Dmitrii Altukhov`_)
- Fix bug with multi-plot 3D rendering where only one plot was updated (:gh:`11896` by `Eric Larson`_)
- Fix bug where subject birthdays were not correctly read by :func:`mne.io.read_raw_snirf` (:gh:`11912` by `Eric Larson`_)
Expand Down
12 changes: 3 additions & 9 deletions mne/channels/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,7 @@ def read_ch_adjacency(fname, picks=None):
You can retrieve the names of all
built-in channel adjacencies via
:func:`mne.channels.get_builtin_ch_adjacencies`.
%(picks_all)s
Picks must match the template.
%(picks_all_notypes)s

Returns
-------
Expand Down Expand Up @@ -1401,20 +1400,15 @@ def read_ch_adjacency(fname, picks=None):

nb = loadmat(fname)["neighbours"]
ch_names = _recursive_flatten(nb["label"], str)
picks = _picks_to_idx(len(ch_names), picks)
temp_info = create_info(ch_names, 1.0)
picks = _picks_to_idx(temp_info, picks, none="all")
neighbors = [_recursive_flatten(c, str) for c in nb["neighblabel"].flatten()]
assert len(ch_names) == len(neighbors)
adjacency = _ch_neighbor_adjacency(ch_names, neighbors)
# picking before constructing matrix is buggy
adjacency = adjacency[picks][:, picks]
ch_names = [ch_names[p] for p in picks]

# make sure MEG channel names contain space after "MEG"
for idx, ch_name in enumerate(ch_names):
if ch_name.startswith("MEG") and not ch_name[3] == " ":
ch_name = ch_name.replace("MEG", "MEG ")
ch_names[idx] = ch_name

return adjacency, ch_names


Expand Down
25 changes: 20 additions & 5 deletions mne/channels/tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ def test_get_builtin_ch_adjacencies():
assert len(name_and_description) == 2


@pytest.mark.parametrize("name", get_builtin_ch_adjacencies())
@pytest.mark.parametrize("picks", ["pick-slice", "pick-arange", "pick-names"])
def test_read_builtin_ch_adjacency_picks(name, picks):
"""Test picking channel subsets when reading builtin adjacency matrices."""
ch_adjacency, ch_names = read_ch_adjacency(name)
assert_equal(ch_adjacency.shape[0], len(ch_names))
subset_names = ch_names[::2]
if picks == "pick-slice":
subset = slice(None, None, 2)
elif picks == "pick-arange":
subset = np.arange(0, len(ch_names), 2)
else:
assert picks == "pick-names"
subset = subset_names

ch_subset_adjacency, ch_subset_names = read_ch_adjacency(name, subset)
assert_array_equal(ch_subset_names, subset_names)


def test_read_ch_adjacency(tmp_path):
"""Test reading channel adjacency templates."""
a = partial(np.array, dtype="<U7")
Expand All @@ -262,6 +281,7 @@ def test_read_ch_adjacency(tmp_path):
savemat(mat_fname, mat, oned_as="row")

ch_adjacency, ch_names = read_ch_adjacency(mat_fname)

x = ch_adjacency
assert_equal(x.shape[0], len(ch_names))
assert_equal(x.shape, (3, 3))
Expand Down Expand Up @@ -328,11 +348,6 @@ def test_read_ch_adjacency(tmp_path):
savemat(mat_fname, mat, oned_as="row")
pytest.raises(ValueError, read_ch_adjacency, mat_fname)

# Try reading all built-in FieldTrip neighbors
for name in get_builtin_ch_adjacencies():
ch_adjacency, ch_names = read_ch_adjacency(name)
assert_equal(ch_adjacency.shape[0], len(ch_names))


def _download_ft_neighbors(target_dir):
"""Download the known neighbors from FieldTrip."""
Expand Down
20 changes: 15 additions & 5 deletions mne/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3358,11 +3358,16 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
_picks_header = f"picks : {_picks_types}"
_picks_desc = "Channels to include."
_picks_int = "Slices and lists of integers will be interpreted as channel " "indices."
_picks_str = """In lists, channel *type* strings
(e.g., ``['meg', 'eeg']``) will pick channels of those
types, channel *name* strings (e.g., ``['MEG0111', 'MEG2623']``
will pick the given channels. Can also be the string values
"all" to pick all channels, or "data" to pick :term:`data channels`.
_picks_str_types = """channel *type* strings (e.g., ``['meg', 'eeg']``) will
pick channels of those types,"""
_picks_str_names = """channel *name* strings (e.g., ``['MEG0111', 'MEG2623']``
will pick the given channels."""
_picks_str_values = """Can also be the string values "all" to pick
all channels, or "data" to pick :term:`data channels`."""
_picks_str = f"""In lists, {_picks_str_types} {_picks_str_names}
{_picks_str_values}
None (default) will pick"""
_picks_str_notypes = f"""In lists, {_picks_str_names}
None (default) will pick"""
_reminder = (
"Note that channels in ``info['bads']`` *will be included* if "
Expand All @@ -3373,13 +3378,18 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
noref = f"(excluding reference MEG channels). {reminder}"
picks_base = f"""{_picks_header}
{_picks_desc} {_picks_int} {_picks_str}"""
picks_base_notypes = f"""picks : list of int | list of str | slice | None
{_picks_desc} {_picks_int} {_picks_str_notypes}"""
docdict["picks_all"] = _reflow_param_docstring(f"{picks_base} all channels. {reminder}")
docdict["picks_all_data"] = _reflow_param_docstring(
f"{picks_base} all data channels. {reminder}"
)
docdict["picks_all_data_noref"] = _reflow_param_docstring(
f"{picks_base} all data channels {noref}"
)
docdict["picks_all_notypes"] = _reflow_param_docstring(
f"{picks_base_notypes} all channels. {reminder}"
)
docdict["picks_base"] = _reflow_param_docstring(picks_base)
docdict["picks_good_data"] = _reflow_param_docstring(
f"{picks_base} good data channels. {reminder}"
Expand Down
Loading