From 7d85b2457b352d98def41fee7f25fbe6e18ac216 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Wed, 1 May 2024 09:57:26 +0200 Subject: [PATCH] fix: rewrite test to use fixtures and new data --- nireports/conftest.py | 2 +- nireports/tests/test_dwi.py | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/nireports/conftest.py b/nireports/conftest.py index ae016f4c..de82b989 100644 --- a/nireports/conftest.py +++ b/nireports/conftest.py @@ -33,7 +33,7 @@ # disable ET os.environ['NO_ET'] = '1' -_datadir = (Path(__file__).parent / "tests" / "data").resolve(strict=True) +_datadir = (Path(__file__).parent / "tests" / "data").absolute() niprepsdev_path = os.getenv( "TEST_DATA_HOME", str(Path.home() / ".cache" / "nipreps-dev") ) diff --git a/nireports/tests/test_dwi.py b/nireports/tests/test_dwi.py index a57a0123..d0d0e007 100644 --- a/nireports/tests/test_dwi.py +++ b/nireports/tests/test_dwi.py @@ -1,7 +1,7 @@ # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- # vi: set ft=python sts=4 ts=4 sw=4 et: # -# Copyright 2023 The NiPreps Developers +# Copyright 2024 The NiPreps Developers # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -20,26 +20,30 @@ # # https://www.nipreps.org/community/licensing/ # -from pathlib import Path +"""Test DWI reportlets.""" + +import pytest import numpy as np from matplotlib import pyplot as plt -from dipy.io import read_bvals_bvecs -from dipy.core.gradients import gradient_table from nireports.reportlets.modality.dwi import plot_gradients -def test_plot_gradients(tmp_path): +@pytest.mark.parametrize( + 'dwi_btable', + ['ds000114_singleshell', 'hcph_multishell', 'ds004737_dsi'], +) +def test_plot_gradients(tmp_path, testdata_path, dwi_btable, outdir): + """Check the plot of DWI gradients.""" + + bvecs = np.loadtxt(testdata_path / f'{dwi_btable}.bvec').T + bvals = np.loadtxt(testdata_path / f'{dwi_btable}.bval') - fbval = "./mriqc/data/testdata/hcp_bvals" - fbvec = "./mriqc/data/testdata/hcp_bvecs" - _bvals, _bvecs = read_bvals_bvecs(fbval, fbvec) - gtab = gradient_table(_bvals, _bvecs) - bvecs = gtab.bvecs[~gtab.b0s_mask] - bvals = gtab.bvals[~gtab.b0s_mask] + b0s_mask = bvals < 50 - gradients = np.vstack([bvecs.T, bvals]) + gradients = np.vstack([bvecs[:, ~b0s_mask], bvals[~b0s_mask]]) _ = plot_gradients(gradients) - plt.savefig(Path(tmp_path) / "gradients.png") + if outdir is not None: + plt.savefig(outdir / f'{dwi_btable}.svg', bbox_inches='tight')