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

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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):
Copy link
Member

Choose a reason for hiding this comment

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

Please don't remove the *

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thanks for pointing out,
I had noticed that a couple of days back, added "*" as suggested, but still, when I run pytest, it fails with the same message as mentioned above.
Well, if I test the function using a sample stc file, it works perfectly.
I guess the problem is not limited only to this, and needs some other tiny fix.

"""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", "-lh.stc", "-rh.stc")):
ftype = "stc"
elif fname.endswith((".w", "-lh.w", "-rh.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
1 change: 0 additions & 1 deletion mne/tests/test_source_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ def test_io_stc(tmp_path):
stc = _fake_stc()
stc.save(tmp_path / "tmp.stc")
stc2 = read_source_estimate(tmp_path / "tmp.stc")

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
Loading