From f06fc3359350836da18cdab58d216f944816516a Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Mon, 17 Mar 2025 13:10:38 +0530 Subject: [PATCH 01/15] try_1 at fixing bug with stc.save #13158 --- mne/source_estimate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index deeb3a43ede..9fa13dddeba 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -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 @@ -1895,7 +1895,7 @@ def save(self, fname, ftype="stc", *, overwrite=False, verbose=None): ``"-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), + File format to use. Allowed values are ``"stc"`` (default is set to auto), ``"w"``, and ``"h5"``. The ``"w"`` format only supports a single time point. %(overwrite)s From cfbd4d1519eda8aa666ca11ccd59f300446f72d9 Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Tue, 18 Mar 2025 18:46:56 +0530 Subject: [PATCH 02/15] fixed stc.save() function, as requested in issue #13158 --- mne/source_estimate.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 9fa13dddeba..8974b4ddb43 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1884,7 +1884,7 @@ class SourceEstimate(_BaseSurfaceSourceEstimate): """ @verbose - def save(self, fname, ftype="auto", *, overwrite=False, verbose=None): + def save(self, fname, ftype=None, overwrite=False, verbose=None): """Save the source estimates to a file. Parameters @@ -1894,18 +1894,26 @@ def save(self, fname, ftype="auto", *, 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 is set to auto), - ``"w"``, and ``"h5"``. The ``"w"`` format only supports a single - time point. + ftype : str | None + File format to use. If None, the file format is inferred from the + file extension. 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 is None: + 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: + raise ValueError("Cannot infer file type, please specify ftype.") _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 +1926,8 @@ def save(self, fname, ftype="auto", *, 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( From 199d93d21797abdd45cf2fe7971faf59d51d713f Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Tue, 18 Mar 2025 22:13:45 +0530 Subject: [PATCH 03/15] changed ftype input from None to auto. --- mne/source_estimate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 8974b4ddb43..041160f3787 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1884,7 +1884,7 @@ class SourceEstimate(_BaseSurfaceSourceEstimate): """ @verbose - def save(self, fname, ftype=None, overwrite=False, verbose=None): + def save(self, fname, ftype="auto", overwrite=False, verbose=None): """Save the source estimates to a file. Parameters @@ -1895,7 +1895,7 @@ def save(self, fname, ftype=None, overwrite=False, verbose=None): ``"-lh.w"`` and ``"-rh.w"``) to the stem provided, for the left and the right hemisphere, respectively. ftype : str | None - File format to use. If None, the file format is inferred from the + File format to use. If "auto", the file format is inferred from the file extension. Allowed values are ``"stc"``, ``"w"``, and ``"h5"``. The ``"w"`` format only supports a single time point. %(overwrite)s @@ -1904,7 +1904,7 @@ def save(self, fname, ftype=None, overwrite=False, verbose=None): %(verbose)s """ fname = str(_check_fname(fname=fname, overwrite=True)) # checked below - if ftype is None: + if ftype == "auto": if fname.endswith((".stc", "-lh.stc", "-rh.stc")): ftype = "stc" elif fname.endswith((".w", "-lh.w", "-rh.w")): From e938d4fb37940a682dd61ff3fb9885aec04772f6 Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Tue, 18 Mar 2025 22:52:06 +0530 Subject: [PATCH 04/15] changed ftype input from None to auto. --- mne/tests/test_source_estimate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index e4fa5a36b25..fba47d27cb4 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -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)) From b5513e61ecc06efc62612d13928f3c7aa897149d Mon Sep 17 00:00:00 2001 From: Shresth Keshari Date: Sat, 22 Mar 2025 01:44:12 +0530 Subject: [PATCH 05/15] Update mne/source_estimate.py Co-authored-by: Daniel McCloy --- mne/source_estimate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 041160f3787..b170dc51fc4 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1895,9 +1895,9 @@ def save(self, fname, ftype="auto", overwrite=False, verbose=None): ``"-lh.w"`` and ``"-rh.w"``) to the stem provided, for the left and the right hemisphere, respectively. ftype : str | None - File format to use. If "auto", the file format is inferred from the - file extension. Allowed values are ``"stc"``, ``"w"``, and ``"h5"``. - The ``"w"`` format only supports a single time point. + 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 From 0f7283a9c7bb6f04792505711a1e252cf58ba5d1 Mon Sep 17 00:00:00 2001 From: Shresth Keshari Date: Sat, 22 Mar 2025 01:44:21 +0530 Subject: [PATCH 06/15] Update mne/source_estimate.py Co-authored-by: Daniel McCloy --- mne/source_estimate.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index b170dc51fc4..8dcdb5d97fc 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1912,7 +1912,10 @@ def save(self, fname, ftype="auto", overwrite=False, verbose=None): elif fname.endswith(".h5"): ftype = "h5" else: - raise ValueError("Cannot infer file type, please specify ftype.") + 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) :] From 6259404dca61a7e981a0b5188a688fb0abf473e2 Mon Sep 17 00:00:00 2001 From: Shresth Keshari Date: Sat, 22 Mar 2025 01:44:28 +0530 Subject: [PATCH 07/15] Update mne/source_estimate.py Co-authored-by: Daniel McCloy --- mne/source_estimate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 8dcdb5d97fc..02eec4ec15e 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1894,7 +1894,7 @@ def save(self, fname, ftype="auto", 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 | None + 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. From cb6db6a05965d575fa28f172b9ffad2b532bffbd Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Tue, 1 Apr 2025 23:28:17 +0530 Subject: [PATCH 08/15] minor fixes including spaces --- mne/tests/test_source_estimate.py | 4 ++-- sample.py | 38 ++++++++++++++++++++++++++++++ test.h5 | Bin 0 -> 15792 bytes 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 sample.py create mode 100644 test.h5 diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index fba47d27cb4..1cf87b3e462 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -480,8 +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) + 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)) diff --git a/sample.py b/sample.py new file mode 100644 index 00000000000..9e4eedcaef2 --- /dev/null +++ b/sample.py @@ -0,0 +1,38 @@ +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-lh.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}") \ No newline at end of file diff --git a/test.h5 b/test.h5 new file mode 100644 index 0000000000000000000000000000000000000000..c9095349ec1366843a06e1682fe50b485895ddba GIT binary patch literal 15792 zcmeHN4OCRe6`r@q?}9>1sj+FgiJE{ET~CqN9PC{ytEgnbL{p^-OJT)DSXS6jun8q< z6dDtMP$ODfYcy4JOjgrYJXVW>U>lCziiBSQ1z8ag)0le1pndPn+>LH327^6KncZ=bLV^a&D^<6enj{*AFq*KL@FL0WC-z6EJVy5@=z~C{ zKSD+@z8}-?EH~!`t(J(As2?foMw(selLs<59aIQJXs1L=6Q0LrjsA3s`5A(dYBHF! zEsN7kjDdcsL4kf!fw2E=0u7c8F@?) zfn`9|JMoolEBQsZo)#O>Eo0%RbSL zL}P+QvIqOfc8m2CV}{IU?nBseNKA?G>FM#=VjMw>Gauc|neWErv3*fWTD&v>$~p7V zpRxUAlQAJJ(Lg+%#zj73lM%!4iBpaMM}Q;15#R`L1ULd50geDifFr;W;0O#J1n!r& zy!trBf~5xr4={YDG6I;lbk4hLSUw72GA3rlsl|#!Cx{gDNE#IAC#88yH%xyK6418- zw9g#Uw_F5kRtNPhhr{^>_ANJwHLJ$w5_sHAp6qA6j&51OZx;bCCW z++xICdcSUk`jrAxucY}qeQU&ZZ_*Qe%MG^H`}vhO0|aUR{VG~TRp^-}N(REOXl{C7 z{R)j-V#tomG^AUM35E=bC)E?je-Pxz=$h)&?IPCno`fzOT zS>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VO|mGLyVttCsI_Up&a4IK*Ue<4zlViy|a78Pxrc}Jk2S7LYy2E_)4R4A&vk?fFr;W M;0SO8zDWrD8#@@VmjD0& literal 0 HcmV?d00001 From 3c0adaf88f0f0018e7fca8378d24c198dfd85b8d Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Tue, 1 Apr 2025 23:45:27 +0530 Subject: [PATCH 09/15] pytest test_source_estimate.py passes, changes were made to test_source_estimate as because of the changed file format, read_source_estimate was not recieving the proper path to the files. --- mne/tests/test_source_estimate.py | 2 +- sample.py | 2 +- test.h5 | Bin 15792 -> 15792 bytes 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index 1cf87b3e462..45913706173 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -481,7 +481,7 @@ def test_io_stc(tmp_path): """Test IO for STC files.""" stc = _fake_stc() stc.save(tmp_path/"tmp.stc", overwrite=True) - 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)) diff --git a/sample.py b/sample.py index 9e4eedcaef2..d3c3e6da5da 100644 --- a/sample.py +++ b/sample.py @@ -28,7 +28,7 @@ os.makedirs(output_dir, exist_ok=True) stc.save("test.h5",overwrite=True) -stc.save("test-lh.stc",overwrite=True) +stc.save("test.stc",overwrite=True) # # Save as .stc file # stc.save(f"{output_dir}/dummy", ftype="stc", overwrite=True) diff --git a/test.h5 b/test.h5 index c9095349ec1366843a06e1682fe50b485895ddba..75fb5ce75c239f6c57628d209e925d99ee413187 100644 GIT binary patch delta 908 zcmXZV{WF^f00(d_MY)Amw^V0qy3tK0IAiV6?Q6v<^HS&KX4~mHTx*V%O*NKH&`PWH zC3YocURuYF3yo9SNUKzwps7USrRfu%JSB*iXKLuWuHAmv=O6e;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-keshari Date: Wed, 2 Apr 2025 00:02:01 +0530 Subject: [PATCH 10/15] changes documented post successful local tests have run. --- doc/changes/devel/13165.bugfix.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 doc/changes/devel/13165.bugfix.rst diff --git a/doc/changes/devel/13165.bugfix.rst b/doc/changes/devel/13165.bugfix.rst new file mode 100644 index 00000000000..c1fe67081ce --- /dev/null +++ b/doc/changes/devel/13165.bugfix.rst @@ -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. \ No newline at end of file From 9bd1483fdfe1200a2503c0f5b9198593c9fafe62 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 1 Apr 2025 18:33:33 +0000 Subject: [PATCH 11/15] [autofix.ci] apply automated fixes --- sample.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sample.py b/sample.py index d3c3e6da5da..87a2d4147bd 100644 --- a/sample.py +++ b/sample.py @@ -1,3 +1,7 @@ +# Authors: The MNE-Python contributors. +# License: BSD-3-Clause +# Copyright the MNE-Python contributors. + import numpy as np from mne.source_estimate import SourceEstimate From 46168f91e8be9b8701f8b7645ed1de6f3f8e8bfa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Apr 2025 18:33:41 +0000 Subject: [PATCH 12/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/tests/test_source_estimate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index 45913706173..b35780bd6a3 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -480,8 +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", overwrite=True) - stc2 = read_source_estimate(tmp_path/"tmp") + stc.save(tmp_path / "tmp.stc", overwrite=True) + 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)) From 9e687d79b0c806bcf5c2349a29622b566f7cbf9a Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Wed, 2 Apr 2025 17:23:09 +0530 Subject: [PATCH 13/15] removed multiple bullet points from 13165.bugfix.rst, and removed redundant cases in 'endswith' while deciding file type during stc.save --- doc/changes/devel/13165.bugfix.rst | 5 +--- mne/source_estimate.py | 6 ++--- mne/tests/test_source_estimate.py | 2 +- sample.py | 42 ----------------------------- test.h5 | Bin 15792 -> 0 bytes 5 files changed, 5 insertions(+), 50 deletions(-) delete mode 100644 sample.py delete mode 100644 test.h5 diff --git a/doc/changes/devel/13165.bugfix.rst b/doc/changes/devel/13165.bugfix.rst index c1fe67081ce..b38af599bc8 100644 --- a/doc/changes/devel/13165.bugfix.rst +++ b/doc/changes/devel/13165.bugfix.rst @@ -1,4 +1 @@ -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. \ No newline at end of file +Fixes behavior of stc.save() filetypes and suffixes while saving through :meth:`mne.SourceEstimate.save` and in `test_source_estimate.py` by `Shresth Keshari`. \ No newline at end of file diff --git a/mne/source_estimate.py b/mne/source_estimate.py index 02eec4ec15e..420b1e36fa6 100644 --- a/mne/source_estimate.py +++ b/mne/source_estimate.py @@ -1884,7 +1884,7 @@ class SourceEstimate(_BaseSurfaceSourceEstimate): """ @verbose - def save(self, fname, ftype="auto", overwrite=False, verbose=None): + def save(self, fname, ftype="auto", *, overwrite=False, verbose=None): """Save the source estimates to a file. Parameters @@ -1905,9 +1905,9 @@ def save(self, fname, ftype="auto", overwrite=False, verbose=None): """ fname = str(_check_fname(fname=fname, overwrite=True)) # checked below if ftype == "auto": - if fname.endswith((".stc", "-lh.stc", "-rh.stc")): + if fname.endswith(".stc"): ftype = "stc" - elif fname.endswith((".w", "-lh.w", "-rh.w")): + elif fname.endswith(".w"): ftype = "w" elif fname.endswith(".h5"): ftype = "h5" diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index b35780bd6a3..e9d7a2fb43d 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -480,7 +480,7 @@ 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", overwrite=True) + stc.save(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) diff --git a/sample.py b/sample.py deleted file mode 100644 index 87a2d4147bd..00000000000 --- a/sample.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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}") \ No newline at end of file diff --git a/test.h5 b/test.h5 deleted file mode 100644 index 75fb5ce75c239f6c57628d209e925d99ee413187..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15792 zcmeHN3sjWH6`tMYv4VlM+MvejCeaj89<|VTH0j+$&|P`RK{%!DQF-hdpuB`tm)iQ4 zYSko=6Gg2`;?Y(Q8a#<6h#HM>K$OVKM{v<)RX{{mW7WJG`~UyU-RfE~DE8Da!#R8Z zxpU{vo$tT@%$>`u&<0O@#%-`05sOkuoQO(##GgB?6u(M2SjYA__8?<)IivrO(+3$5 z{|Fh#`2CoEN4e;zkPsqBqJE^T8?iXkCnYjC?Ntb9Ltcv%CmhRWjs8^0{0zZJOE+ZZ z7?U#683X-Ng96>8GNQQ0e^tzXy%8Jhs;s{zC`RHbjd>1B7__&XJG) zjP0}2lM^%a2GYl2T;wx08R&^moN@#>0vrL307rl$z!BgGa0EC490861N1*p0@UXn) zwILJ>mLBLm!0?$$2w>jQG4HNn`6z@bS#O#VAXG&93xQ%DNrM92q%?2og6S_pyt`I_ z_GJckEvIG80i(K>!{JmTyOtZvn$kSLQW}d5C}RW4 zg#4sf=s*7cLlr{SQfyHP22-0gq8M=&Y9e1QmrEU$OpiiF)M7g!k(99CN6k}8@G!7w zZc%%W-p8#_zmj9>l~|tZS|hG|V~2Jv*V|ej=2v|gAV|CKSCML}B93XIq$m7}=B9hr zuh7Up7;je-PxyV&%3i}*zJ5=eYifkGsR_rF%veo zo1t;m;lQNw4zP?Jv$(b3eaLy;YW{t2Cs+<%B3Z6~hv2f69kXsWK}*$}6BSp#fSNT4 z3931tLQ6y9&I5m~1Isfj_Ij?#2UX4O8m|p!;PZhic3hrY3k6eD(|x8MfsS?OXU|_+ z12+?v2LEM)6?{%4-8xZkgRhQE?Gt3G1@+i{FUS0<1yU}bf3IjjGo*ZT=_9@KMOc1) z|8LJH*T78IRU=|8SE23QS@OlT)vzOS&)OA7&9Lonw~9lzor6%@{QVYFJLJ|@rft$ULVo|j9!s{Fq0aZ6SC(mOVaMq3 zp-som!B?kNw&d+Q1QEuVg|b^`VDv^+ZYdmxUl;DGSXtNzW{($3#+?5QipCFqN!L~d zx4k@y{CzKg()0O(Y58Zt)_-knOKvOV4N-c?N49|});E4P==ufFeH+`)cbOG_^6{KDm+hUn(eFk zA>QeeR%jgk`KRu6r@_)bKG5jC88VkmyMDOeRw({WGgQy4gHLXU74AP<4O(yO@zjHR zp|a9nXDvGj1%IrFD85k#Q)Laeoo8PJ(;~$eIYX-9%$~#<fAb_Oc+GP!+GV-|hrU~QG$CLokj-9=F{kw4 zb?0)%s?=tv|8SQjZ$T%_$qR{UE^7vLVQZ($_w}GTxiDzLmLgacY7W>Las<>L>{Tvs zsfWn&H|M2{-wl_ta};^HW=Qk3HkH2635WJge(Rg?e9$b3(Z#*I7ILFboh}jlIdF^T zzD491JR9M3wDjYXP&laAHY)5xFzL(P=3Dmye$VUIiKPTg(?>-d_pQ{e_V){~njG#Y zBcqNx*9lxd-^rn25zeHqo4g%6amo?k2>f3oaDN=9M97p3`3;$bC1BMUFb`) ze;4$6A#o>ashu6mg=g`+EhBov%+z?J!G52{e(sjbHKXZR;!x<0T&K?v$J?NV9M2GkZak5X~2aU>wI0762jsQo1 LBft@Onh^LmX}7uH From 094c4560b3b5354e0a0a02371c1bc39885321531 Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Wed, 2 Apr 2025 17:33:07 +0530 Subject: [PATCH 14/15] removed multiple bullet points from 13165.bugfix.rst, and removed redundant cases in 'endswith' while deciding file type during stc.save --- doc/changes/devel/13165.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/devel/13165.bugfix.rst b/doc/changes/devel/13165.bugfix.rst index b38af599bc8..d6f4e2a4582 100644 --- a/doc/changes/devel/13165.bugfix.rst +++ b/doc/changes/devel/13165.bugfix.rst @@ -1 +1 @@ -Fixes behavior of stc.save() filetypes and suffixes while saving through :meth:`mne.SourceEstimate.save` and in `test_source_estimate.py` by `Shresth Keshari`. \ No newline at end of file +Fixes behavior of stc.save() filetypes and suffixes while saving through :meth:`SourceEstimate.save` method in `source_estimate.py` and in `test_source_estimate.py` by `Shresth Keshari`. \ No newline at end of file From 9d4b46cced211d314e57a847485fd3827fcff1f4 Mon Sep 17 00:00:00 2001 From: shresth-keshari Date: Thu, 3 Apr 2025 02:32:28 +0530 Subject: [PATCH 15/15] changes documented post successful local tests have run. --- doc/changes/devel/13165.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changes/devel/13165.bugfix.rst b/doc/changes/devel/13165.bugfix.rst index d6f4e2a4582..bbff6a554fd 100644 --- a/doc/changes/devel/13165.bugfix.rst +++ b/doc/changes/devel/13165.bugfix.rst @@ -1 +1 @@ -Fixes behavior of stc.save() filetypes and suffixes while saving through :meth:`SourceEstimate.save` method in `source_estimate.py` and in `test_source_estimate.py` by `Shresth Keshari`. \ No newline at end of file +Fixes behavior of stc.save() filetypes and suffixes while saving through SourceEstimate.save method in mne.source_estimate by `Shresth Keshari`_. \ No newline at end of file