Skip to content

Commit

Permalink
fix: rewrite test to use fixtures and new data
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed May 1, 2024
1 parent acf036c commit 33bb75d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nireports/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
30 changes: 17 additions & 13 deletions nireports/tests/test_dwi.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -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.hstack([bvecs[~b0s_mask], bvals[~b0s_mask, None]])
_ = 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')

0 comments on commit 33bb75d

Please sign in to comment.