From 9ed0153c82709302e030c723fcde19e360c59ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Wed, 8 Jan 2025 20:39:27 -0500 Subject: [PATCH 1/2] ENH: Copy NIfTI image header when creating from image Copy the NIfTI image header of the primary source when creating another one from the former using the `nilearn.threshold_img` function. Make it conditional on the `nilearn` version as the warning is only being raised for `nilearn>=0.11`. Fixes: ``` nireports/tests/test_interfaces_segmentation.py::test_SimpleShowMaskRPT /home/runner/work/nireports/nireports/nireports/reportlets/mosaic.py:92: FutureWarning: From release 0.13.0 onwards, this function will, by default, copy the header of the input image to the output. Currently, the header is reset to the default Nifti1Header. To suppress this warning and use the new behavior, set `copy_header=True`. bbox_nii: SpatialImage = nlimage.threshold_img(bbox_nii, 1e-3) # type: ignore[no-redef] ``` raised for exmaple in: https://github.com/nipreps/nireports/actions/runs/12681153218/job/35344304375#step:12:355 --- nireports/reportlets/mosaic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/nireports/reportlets/mosaic.py b/nireports/reportlets/mosaic.py index 6f8cfb96..0f715762 100644 --- a/nireports/reportlets/mosaic.py +++ b/nireports/reportlets/mosaic.py @@ -35,12 +35,14 @@ import matplotlib as mpl import matplotlib.pyplot as plt import nibabel as nb +import nilearn import numpy as np import numpy.typing as npt from matplotlib.gridspec import GridSpec from nibabel.spatialimages import SpatialImage from nilearn import image as nlimage from nilearn.plotting import plot_anat +from packaging.version import Version from svgutils.transform import SVGFigure, fromstring from nireports.reportlets.utils import ( @@ -89,7 +91,10 @@ def plot_segs( ) if masked: - bbox_nii: SpatialImage = nlimage.threshold_img(bbox_nii, 1e-3) # type: ignore[no-redef] + if Version(nilearn.__version__) >= Version("0.11"): + bbox_nii: SpatialImage = nlimage.threshold_img(bbox_nii, 1e-3, copy_header=True) # type: ignore[no-redef] + else: + bbox_nii: SpatialImage = nlimage.threshold_img(bbox_nii, 1e-3) # type: ignore[no-redef] cuts = cuts_from_bbox(bbox_nii, cuts=7) out_files = [] From 7447d396bc9acccc56bd94ab5a27dfdeb4295b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Thu, 9 Jan 2025 10:18:05 -0500 Subject: [PATCH 2/2] STYLE: Use double quotes instead of single quotes Use double quotes instead of single quotes. Fixes style warnings raised for example in: https://github.com/nipreps/nireports/actions/runs/12692755852/job/35378867283?pr=158#step:8:22 --- nireports/assembler/report.py | 2 +- nireports/reportlets/modality/dwi.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nireports/assembler/report.py b/nireports/assembler/report.py index 774c2db4..d7d62459 100644 --- a/nireports/assembler/report.py +++ b/nireports/assembler/report.py @@ -285,7 +285,7 @@ def __init__( } meta_repl.update({kk: vv for kk, vv in metadata.items() if isinstance(vv, str)}) meta_repl.update(bids_filters) - expr = re.compile(f'{{({"|".join(meta_repl.keys())})}}') + expr = re.compile(f"{{({'|'.join(meta_repl.keys())})}}") for line in bootstrap_file.read_text().splitlines(keepends=False): if expr.search(line): diff --git a/nireports/reportlets/modality/dwi.py b/nireports/reportlets/modality/dwi.py index 1cbc1ed6..f8f848d9 100644 --- a/nireports/reportlets/modality/dwi.py +++ b/nireports/reportlets/modality/dwi.py @@ -80,7 +80,7 @@ def plot_dwi(dataobj, affine, gradient=None, **kwargs): if gradient is None else f"""\ $b$={gradient[3].astype(int)}, \ -$\\vec{{b}}$ = ({', '.join(str(v) for v in gradient[:3])})""" +$\\vec{{b}}$ = ({", ".join(str(v) for v in gradient[:3])})""" ), **kwargs, )