Skip to content

Commit

Permalink
Test refactor (#83)
Browse files Browse the repository at this point in the history
* move tests on operators in folder.

* ci: split the test with the operators folder.

* test pynfft on local runner.

* local runner setup pynfft2

* use venv

* fix: skip 3D tests for slow transforms.

* fix import

* ci: remove pynfft from gpu

2D is done on CPU and is good enough.
  • Loading branch information
paquiteau authored Feb 28, 2024
1 parent e70ae2c commit 6f86fbb
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/test-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
shell: bash
run: |
export COVERAGE_FILE=coverage_${{ matrix.backend }}
pytest --backend ${{ matrix.backend }} --ref ${{ env.ref_backend }} --cov --disable-pytest-warnings --cov-branch --cov-report=term
pytest -k='operators' --backend ${{ matrix.backend }} --ref ${{ env.ref_backend }} --cov --disable-pytest-warnings --cov-branch --cov-report=term
- name: Upload coverage
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -111,7 +111,9 @@ jobs:
pip install torch --index-url https://download.pytorch.org/whl/cu118
pip install finufft
- name: Install backend
if: ${{ matrix.backend == 'gpunufft' || matrix.backend == 'cufinufft' }}
shell: bash
run: |
source $RUNNER_WORKSPACE/venv/bin/activate
Expand All @@ -120,13 +122,20 @@ jobs:
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib/{LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
pip install ${{ matrix.backend }}
- name: Install pynfft
if: ${{ matrix.backend == 'pynfft' }}
shell: bash
run: |
source $RUNNER_WORKSPACE/venv/bin/activate
pip install pynfft2
- name: Run Tests
shell: bash
run: |
cd $RUNNER_WORKSPACE/mri-nufft
source $RUNNER_WORKSPACE/venv/bin/activate
export COVERAGE_FILE=coverage_${{ matrix.backend }}
python -m pytest --ref ${{ env.ref_backend }} --backend ${{ matrix.backend }} --disable-pytest-warnings --cov --cov-branch --cov-report=term
python -m pytest -k='operators' --ref ${{ env.ref_backend }} --backend ${{ matrix.backend }} --disable-pytest-warnings --cov --cov-branch --cov-report=term
- name: Upload coverage
if: success()
Expand Down Expand Up @@ -168,7 +177,7 @@ jobs:
shell: bash
run: |
export COVERAGE_FILE=coverage_plots
pytest examples --cov --cov-branch --cov-report=term
pytest examples tests -k="not operators" --cov --cov-branch --cov-report=term
- name: Upload coverage
if: success()
Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions tests/operators/test_gpunufft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Specific test for gpunufft."""

import numpy as np
import numpy.testing as npt
from pytest_cases import parametrize, parametrize_with_cases

from case_trajectories import CasesTrajectories
from helpers import assert_correlate
from mrinufft.density import cell_count, voronoi, pipe
from mrinufft.density.utils import normalize_weights
from mrinufft._utils import proper_trajectory


def radial_distance(traj, shape):
"""Compute the radial distance of a trajectory."""
proper_traj = proper_trajectory(traj, normalize="unit")
weights = np.linalg.norm(proper_traj, axis=-1)
return weights


@parametrize("osf", [1, 1.25, 2])
@parametrize_with_cases(
"traj, shape",
cases=[
CasesTrajectories.case_nyquist_radial2D,
CasesTrajectories.case_nyquist_radial3D,
],
)
@parametrize(backend=["gpunufft"])
def test_pipe(backend, traj, shape, osf):
"""Test the pipe method."""
distance = radial_distance(traj, shape)
result = pipe(traj, shape, osf=osf, num_iterations=10)
result = result / np.mean(result)
distance = distance / np.mean(distance)
if osf != 2:
# If OSF < 2, we dont perfectly estimate
assert_correlate(result, distance, slope=1, slope_err=None, r_value_err=0.2)
else:
assert_correlate(result, distance, slope=1, slope_err=0.1, r_value_err=0.1)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the interfaces module."""

import numpy as np
import pytest
from pytest_cases import parametrize_with_cases, parametrize, fixture
from mrinufft import get_operator
from case_trajectories import CasesTrajectories
Expand Down Expand Up @@ -43,6 +44,8 @@ def operator(
n_coils=1,
):
"""Generate an operator."""
if backend in ["pynfft", "sigpy"] and kspace_locs.shape[-1] == 3:
pytest.skip("3D for slow cpu is not tested")
return get_operator(backend)(kspace_locs, shape, n_coils=n_coils, smaps=None)


Expand Down
File renamed without changes.
File renamed without changes.
22 changes: 0 additions & 22 deletions tests/test_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,3 @@ def test_voronoi(traj, shape):
result = result / np.mean(result)
distance = distance / np.mean(distance)
assert_correlate(result, distance, slope=1)


@parametrize("osf", [1, 1.25, 2])
@parametrize_with_cases(
"traj, shape",
cases=[
CasesTrajectories.case_nyquist_radial2D,
CasesTrajectories.case_nyquist_radial3D,
],
)
@parametrize(backend=["gpunufft"])
def test_pipe(backend, traj, shape, osf):
"""Test the pipe method."""
distance = radial_distance(traj, shape)
result = pipe(traj, shape, osf=osf, num_iterations=10)
result = result / np.mean(result)
distance = distance / np.mean(distance)
if osf != 2:
# If OSF < 2, we dont perfectly estimate
assert_correlate(result, distance, slope=1, slope_err=None, r_value_err=0.2)
else:
assert_correlate(result, distance, slope=1, slope_err=0.1, r_value_err=0.1)

0 comments on commit 6f86fbb

Please sign in to comment.