From a7c643dce2e57080a28bb76d32157ba5d4dc9a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Wed, 8 Jan 2025 20:27:56 -0500 Subject: [PATCH] ENH: Fix mosaic plot `plot_sagittal` parameter warning Fix mosaic plot `plot_sagittal` parameter warning: use a context manager to define the cases where errors or warnings are expected, and avoid the need for a partial function. Fixes: ``` nireports/tests/test_reportlets.py::test_mriqc_plot_mosaic[True-views24] nireports/tests/test_reportlets.py::test_mriqc_plot_mosaic[True-views26] /home/runner/work/nireports/nireports/nireports/tests/test_reportlets.py:360: UserWarning: Argument ``plot_sagittal`` for plot_mosaic() should not be used. testfunc() ``` raised for example in: https://github.com/nipreps/nireports/actions/runs/12681153218/job/35344304375#step:12:360 --- nireports/reportlets/mosaic.py | 4 +++- nireports/tests/test_reportlets.py | 29 +++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/nireports/reportlets/mosaic.py b/nireports/reportlets/mosaic.py index 0f715762..3514f7ad 100644 --- a/nireports/reportlets/mosaic.py +++ b/nireports/reportlets/mosaic.py @@ -553,7 +553,9 @@ def plot_mosaic( if plot_sagittal and views[1] is None and views[0] != "sagittal": warnings.warn( - "Argument ``plot_sagittal`` for plot_mosaic() should not be used.", stacklevel=2 + "Argument ``plot_sagittal`` for plot_mosaic() should not be used.", + category=UserWarning, + stacklevel=2, ) views = (views[0], "sagittal", None) diff --git a/nireports/tests/test_reportlets.py b/nireports/tests/test_reportlets.py index fc56a69d..d1a32640 100644 --- a/nireports/tests/test_reportlets.py +++ b/nireports/tests/test_reportlets.py @@ -22,8 +22,8 @@ # """Test reportlets module.""" +import contextlib import os -from functools import partial from itertools import permutations from pathlib import Path @@ -343,21 +343,22 @@ def test_mriqc_plot_mosaic(tmp_path, test_data_package, outdir, views, plot_sagi fname = f"mosaic_{'_'.join(v or 'none' for v in views)}_{plot_sagittal:d}.svg" - testfunc = partial( - plot_mosaic, - get("MNI152NLin6Asym", resolution=2, desc="LR", suffix="T1w"), - plot_sagittal=plot_sagittal, - views=views, - out_file=(outdir / fname) if outdir is not None else None, - title=f"A mosaic plotting example: views={views}, plot_sagittal={plot_sagittal}", - maxrows=5, - ) - if views[0] is None or ((views[1] is None) and (views[2] is not None)): - with pytest.raises(RuntimeError): - testfunc() + context = pytest.raises(RuntimeError) + elif plot_sagittal and views[1] is None and views[0] != "sagittal": + context = pytest.warns(UserWarning, match=r".*plot_sagittal.*should not be used") else: - testfunc() + context = contextlib.nullcontext() + + with context: + plot_mosaic( + get("MNI152NLin6Asym", resolution=2, desc="LR", suffix="T1w"), + plot_sagittal=plot_sagittal, + views=views, + out_file=(outdir / fname) if outdir is not None else None, + title=f"A mosaic plotting example: views={views}, plot_sagittal={plot_sagittal}", + maxrows=5, + ) def test_mriqc_plot_mosaic_2(tmp_path, test_data_package, outdir):