-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Changes from 16 commits
f06fc33
cfbd4d1
b9f0e44
199d93d
e938d4f
f96a03b
b5513e6
0f7283a
6259404
7a401c4
cb6db6a
3c0adaf
02467e2
9bd1483
46168f9
9aee7e7
9e687d7
094c456
9d4b46c
316be19
b0b979d
faaed3f
c8d5bbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fixes behavior of stc.save() as mentioned in issue #13158 :meth:`stc.save` and in `test_source_estimate.py` by `Shresth Keshari`. | ||
- Changed filetype to "auto" | ||
- saving a `.stc` file automatically saves two files with suffix `-lh.stc` and `-rh.stc`, which is intuitive and removes any confusion. | ||
- there were problems with pytest later on, which were tackled by minor tweaks. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
shresth-keshari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Save the source estimates to a file. | ||
|
||
Parameters | ||
|
@@ -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")): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified, as the first element in each |
||
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) :] | ||
|
||
|
@@ -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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,9 +480,8 @@ def attempt_assignment(stc, attr, val): | |
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") | ||
|
||
stc.save(tmp_path / "tmp.stc", overwrite=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is |
||
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)) | ||
|
shresth-keshari marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Authors: The MNE-Python contributors. | ||
# License: BSD-3-Clause | ||
# Copyright the MNE-Python contributors. | ||
|
||
import numpy as np | ||
from mne.source_estimate import SourceEstimate | ||
|
||
# Create dummy data for the SourceEstimate | ||
n_vertices_lh = 10 # Number of vertices in the left hemisphere | ||
n_vertices_rh = 12 # Number of vertices in the right hemisphere | ||
n_times = 5 # Number of time points | ||
|
||
# Random data for the left and right hemispheres | ||
data = np.random.rand(n_vertices_lh + n_vertices_rh, n_times) | ||
|
||
# Vertices for the left and right hemispheres | ||
vertices = [np.arange(n_vertices_lh), np.arange(n_vertices_rh)] | ||
|
||
# Time parameters | ||
tmin = 0.0 # Start time in seconds | ||
tstep = 0.1 # Time step in seconds | ||
|
||
# Subject name | ||
subject = "sample_subject" | ||
|
||
# Create a SourceEstimate object | ||
stc = SourceEstimate(data, vertices, tmin, tstep, subject=subject) | ||
|
||
# Save the SourceEstimate in different formats | ||
output_dir = "./output_files" # Directory to save the files | ||
import os | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
stc.save("test.h5",overwrite=True) | ||
stc.save("test.stc",overwrite=True) | ||
# # Save as .stc file | ||
# stc.save(f"{output_dir}/dummy", ftype="stc", overwrite=True) | ||
|
||
# # Save as .h5 file | ||
# stc.save(f"{output_dir}/dummy.h5", ftype="h5", overwrite=True) | ||
|
||
# print(f"Dummy files saved in {output_dir}") |
shresth-keshari marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.