From d78490501a8e3204d78b5516ed6d7657bf30cbc1 Mon Sep 17 00:00:00 2001 From: Aasma Gupta Date: Tue, 17 Mar 2026 01:17:18 +0530 Subject: [PATCH 1/5] ENH: Add optional rank parameter to Report.add_covariance --- mne/report/report.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/mne/report/report.py b/mne/report/report.py index 4ba59cf8ea4..1fd72f2c66b 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -1721,7 +1721,16 @@ def add_trans( ) @fill_doc - def add_covariance(self, cov, *, info, title, tags=("covariance",), replace=False): + def add_covariance( + self, + cov, + *, + info, + title, + rank=None, + tags=("covariance",), + replace=False, + ): """Add covariance to the report. Parameters @@ -1732,6 +1741,11 @@ def add_covariance(self, cov, *, info, title, tags=("covariance",), replace=Fals The `~mne.Info` corresponding to ``cov``. title : str The title corresponding to the `~mne.Covariance` object. + rank : int | None + The rank of the covariance matrix. If provided, it will be displayed + in the report above the covariance plots. + + .. versionadded:: 1.8 %(tags_report)s %(replace_report)s @@ -1739,7 +1753,21 @@ def add_covariance(self, cov, *, info, title, tags=("covariance",), replace=Fals ----- .. versionadded:: 0.24.0 """ + tags = _check_tags(tags) + + # Display rank if provided + if rank is not None: + html = f"

Rank: {rank}

" + self._add_html_element( + html=html, + title="Covariance rank", + tags=tags, + section=title, + replace=replace, + div_klass="covariance", + ) + self._add_cov( cov=cov, info=info, From 1c689006e6e3adc7e889bdee46a52f6186440662 Mon Sep 17 00:00:00 2001 From: Aasma Gupta Date: Tue, 17 Mar 2026 01:29:55 +0530 Subject: [PATCH 2/5] DOC/TEST: Update report tutorial and tests for rank parameter --- mne/report/tests/test_report.py | 14 ++++++++++++++ tutorials/intro/70_report.py | 11 ++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mne/report/tests/test_report.py b/mne/report/tests/test_report.py index 5fa47d63914..5be068c846d 100644 --- a/mne/report/tests/test_report.py +++ b/mne/report/tests/test_report.py @@ -1360,3 +1360,17 @@ def test_gif(tmp_path): bad_name.write_bytes(b"") with pytest.raises(ValueError, match="Allowed values"): r.add_image(bad_name, "fname") + +def test_add_covariance_rank(): + from mne import create_info + from mne.cov import make_ad_hoc_cov + from mne.report import Report + + info = create_info(ch_names=["EEG 001"], sfreq=1000, ch_types=["eeg"]) + cov = make_ad_hoc_cov(info) + + report = Report() + report.add_covariance(cov, info=info, title="Test Cov", rank=1) + + html = "".join(report.html) + assert "Rank" in html \ No newline at end of file diff --git a/tutorials/intro/70_report.py b/tutorials/intro/70_report.py index 31133610e2c..f2e24eef07f 100644 --- a/tutorials/intro/70_report.py +++ b/tutorials/intro/70_report.py @@ -164,7 +164,16 @@ cov_path = sample_dir / "sample_audvis-cov.fif" report = mne.Report(title="Covariance example") -report.add_covariance(cov=cov_path, info=raw_path, title="Covariance") + +# Optionally display the covariance rank in the report +rank = 60 + +report.add_covariance( + cov, + info=info, + title="Noise covariance", + rank=rank, +) report.save("report_cov.html", overwrite=True) # %% From 5cf51d9eb374b054010d8ba8c98cde6694e91fa2 Mon Sep 17 00:00:00 2001 From: Aasma Gupta Date: Tue, 17 Mar 2026 14:21:27 +0530 Subject: [PATCH 3/5] DOC: Add changelog entry and update versionadded --- doc/changes/dev/13761.newfeature.rst | 1 + mne/report/report.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 doc/changes/dev/13761.newfeature.rst diff --git a/doc/changes/dev/13761.newfeature.rst b/doc/changes/dev/13761.newfeature.rst new file mode 100644 index 00000000000..53ee932c6fc --- /dev/null +++ b/doc/changes/dev/13761.newfeature.rst @@ -0,0 +1 @@ +Add optional ``rank`` parameter to ``Report.add_covariance`` to display covariance rank in reports by `Aasma Gupta`. \ No newline at end of file diff --git a/mne/report/report.py b/mne/report/report.py index 1fd72f2c66b..3a92eee47e7 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -1745,7 +1745,7 @@ def add_covariance( The rank of the covariance matrix. If provided, it will be displayed in the report above the covariance plots. - .. versionadded:: 1.8 + .. versionadded:: 1.12 %(tags_report)s %(replace_report)s From 6dbe73915270ea370fc28bb06c74a04cc5d844fc Mon Sep 17 00:00:00 2001 From: Aasma Gupta Date: Tue, 17 Mar 2026 14:35:33 +0530 Subject: [PATCH 4/5] TEST/DOC: Fix covariance test and tutorial --- mne/report/tests/test_report.py | 7 +++++-- tutorials/intro/70_report.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mne/report/tests/test_report.py b/mne/report/tests/test_report.py index 5be068c846d..daeb689d734 100644 --- a/mne/report/tests/test_report.py +++ b/mne/report/tests/test_report.py @@ -1361,7 +1361,7 @@ def test_gif(tmp_path): with pytest.raises(ValueError, match="Allowed values"): r.add_image(bad_name, "fname") -def test_add_covariance_rank(): +def test_add_covariance_rank(tmp_path): from mne import create_info from mne.cov import make_ad_hoc_cov from mne.report import Report @@ -1372,5 +1372,8 @@ def test_add_covariance_rank(): report = Report() report.add_covariance(cov, info=info, title="Test Cov", rank=1) - html = "".join(report.html) + fname = tmp_path / "report.html" + report.save(fname, open_browser=False) + + html = fname.read_text(encoding="utf-8") assert "Rank" in html \ No newline at end of file diff --git a/tutorials/intro/70_report.py b/tutorials/intro/70_report.py index f2e24eef07f..9a7cef129f4 100644 --- a/tutorials/intro/70_report.py +++ b/tutorials/intro/70_report.py @@ -169,9 +169,9 @@ rank = 60 report.add_covariance( - cov, - info=info, - title="Noise covariance", + cov=cov_path, + info=raw_path, + title="Covariance", rank=rank, ) report.save("report_cov.html", overwrite=True) From 256c2543c7f4a6acc4a12d6e51ed30c3b3af8a10 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2026 09:06:37 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/report/report.py | 3 +-- mne/report/tests/test_report.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mne/report/report.py b/mne/report/report.py index 3a92eee47e7..ac923b8c6d5 100644 --- a/mne/report/report.py +++ b/mne/report/report.py @@ -1744,7 +1744,7 @@ def add_covariance( rank : int | None The rank of the covariance matrix. If provided, it will be displayed in the report above the covariance plots. - + .. versionadded:: 1.12 %(tags_report)s %(replace_report)s @@ -1753,7 +1753,6 @@ def add_covariance( ----- .. versionadded:: 0.24.0 """ - tags = _check_tags(tags) # Display rank if provided diff --git a/mne/report/tests/test_report.py b/mne/report/tests/test_report.py index daeb689d734..8b456cd04b4 100644 --- a/mne/report/tests/test_report.py +++ b/mne/report/tests/test_report.py @@ -1361,6 +1361,7 @@ def test_gif(tmp_path): with pytest.raises(ValueError, match="Allowed values"): r.add_image(bad_name, "fname") + def test_add_covariance_rank(tmp_path): from mne import create_info from mne.cov import make_ad_hoc_cov @@ -1376,4 +1377,4 @@ def test_add_covariance_rank(tmp_path): report.save(fname, open_browser=False) html = fname.read_text(encoding="utf-8") - assert "Rank" in html \ No newline at end of file + assert "Rank" in html