Skip to content

Fixes behavior of stc.save() as mentioned in issue #13158 #13165

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

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f06fc33
try_1 at fixing bug with stc.save #13158
shresth-keshari Mar 17, 2025
cfbd4d1
fixed stc.save() function, as requested in issue #13158
shresth-keshari Mar 18, 2025
b9f0e44
Merge branch 'gsoc_commits' of https://github.com/shresth-keshari/mne…
shresth-keshari Mar 18, 2025
199d93d
changed ftype input from None to auto.
shresth-keshari Mar 18, 2025
e938d4f
changed ftype input from None to auto.
shresth-keshari Mar 18, 2025
f96a03b
Merge branch 'mne-tools:main' into gsoc_commits
shresth-keshari Mar 21, 2025
b5513e6
Update mne/source_estimate.py
shresth-keshari Mar 21, 2025
0f7283a
Update mne/source_estimate.py
shresth-keshari Mar 21, 2025
6259404
Update mne/source_estimate.py
shresth-keshari Mar 21, 2025
7a401c4
Merge branch 'mne-tools:main' into gsoc_commits
shresth-keshari Apr 1, 2025
cb6db6a
minor fixes including spaces
shresth-keshari Apr 1, 2025
3c0adaf
pytest test_source_estimate.py passes, changes were made to test_sour…
shresth-keshari Apr 1, 2025
02467e2
changes documented post successful local tests have run.
shresth-keshari Apr 1, 2025
9bd1483
[autofix.ci] apply automated fixes
autofix-ci[bot] Apr 1, 2025
46168f9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2025
9aee7e7
Merge branch 'main' into gsoc_commits
shresth-keshari Apr 1, 2025
9e687d7
removed multiple bullet points from 13165.bugfix.rst, and removed red…
shresth-keshari Apr 2, 2025
094c456
removed multiple bullet points from 13165.bugfix.rst, and removed red…
shresth-keshari Apr 2, 2025
9d4b46c
changes documented post successful local tests have run.
shresth-keshari Apr 2, 2025
316be19
Merge branch 'mne-tools:main' into gsoc_commits
shresth-keshari Apr 3, 2025
b0b979d
Merge branch 'main' into gsoc_commits
larsoner Apr 23, 2025
faaed3f
Merge branch 'main' into gsoc_commits
shresth-keshari Apr 27, 2025
c8d5bbb
Merge branch 'main' into gsoc_commits
shresth-keshari May 2, 2025
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/13165.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes behavior of stc.save() filetypes and suffixes while saving through SourceEstimate.save method in mne.source_estimate by `Shresth Keshari`_.
25 changes: 19 additions & 6 deletions mne/source_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ class SourceEstimate(_BaseSurfaceSourceEstimate):
"""

@verbose
def save(self, fname, ftype="stc", *, overwrite=False, verbose=None):
def save(self, fname, ftype="auto", *, overwrite=False, verbose=None):
"""Save the source estimates to a file.

Parameters
Expand All @@ -1894,18 +1894,29 @@ def save(self, fname, ftype="stc", *, overwrite=False, verbose=None):
spaces are obtained by adding ``"-lh.stc"`` and ``"-rh.stc"`` (or
``"-lh.w"`` and ``"-rh.w"``) to the stem provided, for the left and
the right hemisphere, respectively.
ftype : str
File format to use. Allowed values are ``"stc"`` (default),
``"w"``, and ``"h5"``. The ``"w"`` format only supports a single
time point.
ftype : "auto" | "stc" | "w" | "h5"
File format to use. If "auto", the file format will be inferred from the
file extension if possible. Other allowed values are ``"stc"``, ``"w"``, and
``"h5"``. The ``"w"`` format only supports a single time point.
%(overwrite)s

.. versionadded:: 1.0
%(verbose)s
"""
fname = str(_check_fname(fname=fname, overwrite=True)) # checked below
if ftype == "auto":
if fname.endswith(".stc"):
ftype = "stc"
elif fname.endswith(".w"):
ftype = "w"
elif fname.endswith(".h5"):
ftype = "h5"
else:
logger.info(
"Cannot infer file type from `fname`; falling back to `.stc` format"
)
ftype = "stc"
_check_option("ftype", ftype, ["stc", "w", "h5"])

lh_data = self.data[: len(self.lh_vertno)]
rh_data = self.data[-len(self.rh_vertno) :]

Expand All @@ -1918,6 +1929,8 @@ def save(self, fname, ftype="stc", *, overwrite=False, verbose=None):
"real numbers before saving."
)
logger.info("Writing STC to disk...")
if fname.endswith(".stc"):
fname = fname[:-4]
fname_l = str(_check_fname(fname + "-lh.stc", overwrite=overwrite))
fname_r = str(_check_fname(fname + "-rh.stc", overwrite=overwrite))
_write_stc(
Expand Down
3 changes: 1 addition & 2 deletions mne/tests/test_source_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ def test_io_stc(tmp_path):
"""Test IO for STC files."""
stc = _fake_stc()
stc.save(tmp_path / "tmp.stc")
stc2 = read_source_estimate(tmp_path / "tmp.stc")

stc2 = read_source_estimate(tmp_path / "tmp")
assert_array_almost_equal(stc.data, stc2.data)
assert_array_almost_equal(stc.tmin, stc2.tmin)
assert_equal(len(stc.vertices), len(stc2.vertices))
Expand Down