Skip to content

Commit

Permalink
FIX: Move
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Dec 6, 2023
1 parent 90dee7a commit 335d6c8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
7 changes: 3 additions & 4 deletions examples/decoding/receptive_field_mtrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
.. _figure 1: https://www.frontiersin.org/articles/10.3389/fnhum.2016.00604/full#F1
.. _figure 2: https://www.frontiersin.org/articles/10.3389/fnhum.2016.00604/full#F2
.. _figure 5: https://www.frontiersin.org/articles/10.3389/fnhum.2016.00604/full#F5
""" # noqa: E501
"""

# Authors: Chris Holdgraf <[email protected]>
# Eric Larson <[email protected]>
Expand All @@ -26,9 +26,6 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

# %%
# sphinx_gallery_thumbnail_number = 3

from os.path import join

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -131,6 +128,8 @@
# across the scalp. We will recreate `figure 1`_ and `figure 2`_ from
# :footcite:`CrosseEtAl2016`.

# sphinx_gallery_thumbnail_number = 3

# Print mean coefficients across all time delays / channels (see Fig 1)
time_plot = 0.180 # For highlighting a specific time.
fig, ax = plt.subplots(figsize=(4, 8), layout="constrained")
Expand Down
109 changes: 54 additions & 55 deletions mne/tests/test_source_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,70 +562,69 @@ def test_stc_arithmetic():
@pytest.mark.parametrize("method", ("fft", "polyphase"))
def test_stc_methods(kind, method):
"""Test stc methods lh_data, rh_data, bin(), resample()."""
stc_ = read_source_estimate(fname_stc)
stc = read_source_estimate(fname_stc)

if kind == "vector":
# Make a vector version of the above source estimate
x = stc_.data[:, np.newaxis, :]
x = stc.data[:, np.newaxis, :]
yz = np.zeros((x.shape[0], 2, x.shape[2]))
stc_ = VectorSourceEstimate(
stc = VectorSourceEstimate(
np.concatenate((x, yz), 1),
stc_.vertices,
stc_.tmin,
stc_.tstep,
stc_.subject,
stc.vertices,
stc.tmin,
stc.tstep,
stc.subject,
)

for stc in [stc_]: # noop to keep diff small
# lh_data / rh_data
assert_array_equal(stc.lh_data, stc.data[: len(stc.lh_vertno)])
assert_array_equal(stc.rh_data, stc.data[len(stc.lh_vertno) :])
# lh_data / rh_data
assert_array_equal(stc.lh_data, stc.data[: len(stc.lh_vertno)])
assert_array_equal(stc.rh_data, stc.data[len(stc.lh_vertno) :])

# bin
binned = stc.bin(0.12)
a = np.mean(stc.data[..., : np.searchsorted(stc.times, 0.12)], axis=-1)
assert_array_equal(a, binned.data[..., 0])
# bin
binned = stc.bin(0.12)
a = np.mean(stc.data[..., : np.searchsorted(stc.times, 0.12)], axis=-1)
assert_array_equal(a, binned.data[..., 0])

stc = read_source_estimate(fname_stc)
stc.subject = "sample"
label_lh = read_labels_from_annot(
"sample", "aparc", "lh", subjects_dir=subjects_dir
)[0]
label_rh = read_labels_from_annot(
"sample", "aparc", "rh", subjects_dir=subjects_dir
)[0]
label_both = label_lh + label_rh
for label in (label_lh, label_rh, label_both):
assert isinstance(stc.shape, tuple) and len(stc.shape) == 2
stc_label = stc.in_label(label)
if label.hemi != "both":
if label.hemi == "lh":
verts = stc_label.vertices[0]
else: # label.hemi == 'rh':
verts = stc_label.vertices[1]
n_vertices_used = len(label.get_vertices_used(verts))
assert_equal(len(stc_label.data), n_vertices_used)
stc_lh = stc.in_label(label_lh)
pytest.raises(ValueError, stc_lh.in_label, label_rh)
label_lh.subject = "foo"
pytest.raises(RuntimeError, stc.in_label, label_lh)

stc_new = deepcopy(stc)
o_sfreq = 1.0 / stc.tstep
# note that using no padding for this STC reduces edge ringing...
stc_new.resample(2 * o_sfreq, npad=0, method=method)
assert stc_new.data.shape[1] == 2 * stc.data.shape[1]
assert stc_new.tstep == stc.tstep / 2
stc_new.resample(o_sfreq, npad=0, method=method)
assert stc_new.data.shape[1] == stc.data.shape[1]
assert stc_new.tstep == stc.tstep
if method == "fft":
# no low-passing so survives round-trip
assert_allclose(stc_new.data, stc.data, atol=1e-5)
else:
# low-passing means we need something more flexible
corr = np.corrcoef(stc_new.data.ravel(), stc.data.ravel())[0, 1]
assert 0.99 < corr < 1
stc = read_source_estimate(fname_stc)
stc.subject = "sample"
label_lh = read_labels_from_annot(
"sample", "aparc", "lh", subjects_dir=subjects_dir
)[0]
label_rh = read_labels_from_annot(
"sample", "aparc", "rh", subjects_dir=subjects_dir
)[0]
label_both = label_lh + label_rh
for label in (label_lh, label_rh, label_both):
assert isinstance(stc.shape, tuple) and len(stc.shape) == 2
stc_label = stc.in_label(label)
if label.hemi != "both":
if label.hemi == "lh":
verts = stc_label.vertices[0]
else: # label.hemi == 'rh':
verts = stc_label.vertices[1]
n_vertices_used = len(label.get_vertices_used(verts))
assert_equal(len(stc_label.data), n_vertices_used)
stc_lh = stc.in_label(label_lh)
pytest.raises(ValueError, stc_lh.in_label, label_rh)
label_lh.subject = "foo"
pytest.raises(RuntimeError, stc.in_label, label_lh)

stc_new = deepcopy(stc)
o_sfreq = 1.0 / stc.tstep
# note that using no padding for this STC reduces edge ringing...
stc_new.resample(2 * o_sfreq, npad=0, method=method)
assert stc_new.data.shape[1] == 2 * stc.data.shape[1]
assert stc_new.tstep == stc.tstep / 2
stc_new.resample(o_sfreq, npad=0, method=method)
assert stc_new.data.shape[1] == stc.data.shape[1]
assert stc_new.tstep == stc.tstep
if method == "fft":
# no low-passing so survives round-trip
assert_allclose(stc_new.data, stc.data, atol=1e-5)
else:
# low-passing means we need something more flexible
corr = np.corrcoef(stc_new.data.ravel(), stc.data.ravel())[0, 1]
assert 0.99 < corr < 1


@testing.requires_testing_data
Expand Down

0 comments on commit 335d6c8

Please sign in to comment.