Skip to content

Commit

Permalink
ENH: Fix mosaic plot plot_sagittal parameter warning
Browse files Browse the repository at this point in the history
Fix mosaic plot `plot_sagittal` parameter warning: define the warning
message so that it can be tested exactly and catch the warning where
applicable.

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
  • Loading branch information
jhlegarreta committed Jan 9, 2025
1 parent d88ca78 commit 691fbc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion nireports/reportlets/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
)
from nireports.tools.ndimage import load_api, rotate_affine, rotation2canonical

plot_sagittal_msg = "Argument ``plot_sagittal`` for plot_mosaic() should not be used."


def plot_segs(
image_nii: ty.Union[str, nb.Nifti1Image],
Expand Down Expand Up @@ -546,7 +548,7 @@ def plot_mosaic(
out_file = "mosaic.svg"

if plot_sagittal and views[1] is None and views[0] != "sagittal":
warn("Argument ``plot_sagittal`` for plot_mosaic() should not be used.", stacklevel=2)
warn(plot_sagittal_msg, category=UserWarning, stacklevel=2)
views = (views[0], "sagittal", None)

# Create mask for bounding box
Expand Down
39 changes: 26 additions & 13 deletions nireports/tests/test_reportlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""Test reportlets module."""

import os
import warnings
from functools import partial
from itertools import permutations
from pathlib import Path
Expand All @@ -34,7 +35,7 @@
from templateflow.api import get

from nireports.reportlets.modality.func import fMRIPlot
from nireports.reportlets.mosaic import plot_mosaic
from nireports.reportlets.mosaic import plot_sagittal_msg, plot_mosaic
from nireports.reportlets.nuisance import plot_carpet, plot_raincloud
from nireports.reportlets.surface import cifti_surfaces_plot
from nireports.reportlets.xca import compcor_variance_plot, plot_melodic_components
Expand Down Expand Up @@ -343,21 +344,33 @@ 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,
)
def _test_mriqc_plot_mosaic(outdir, views, plot_sagittal):
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):
if views[0] is None or ((views[1] is None) and (views[2] is not None)):
with pytest.raises(RuntimeError):
testfunc()
else:
testfunc()

if plot_sagittal:
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=plot_sagittal_msg,
category=UserWarning,
)
_test_mriqc_plot_mosaic(outdir, views, plot_sagittal)
else:
testfunc()
_test_mriqc_plot_mosaic(outdir, views, plot_sagittal)


def test_mriqc_plot_mosaic_2(tmp_path, test_data_package, outdir):
Expand Down

0 comments on commit 691fbc9

Please sign in to comment.