|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Tests for io.""" |
| 3 | + |
| 4 | +import os |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | +from phys2cvr import regressors |
| 11 | + |
| 12 | +# ## Unit tests |
| 13 | + |
| 14 | + |
| 15 | +def test_create_legendre(): |
| 16 | + L = regressors.create_legendre(3, 10) |
| 17 | + assert L.shape == (10, 4) |
| 18 | + |
| 19 | + L = regressors.create_legendre(1, 5) |
| 20 | + assert np.allclose(L[:, 1], np.linspace(-1, 1, 5)) |
| 21 | + |
| 22 | + L = regressors.create_legendre(0, 7) |
| 23 | + assert L.shape == (7, 1) |
| 24 | + assert np.all(L[:, 0] == 1) |
| 25 | + |
| 26 | + |
| 27 | +@patch( |
| 28 | + 'phys2cvr.regressors.endtidal_interpolation', return_value=np.array([1, 2, 3, 4]) |
| 29 | +) |
| 30 | +@patch('phys2cvr.regressors.plot_two_timeseries') |
| 31 | +@patch('phys2cvr.regressors.convolve_signal', return_value=np.array([9, 8, 7, 6])) |
| 32 | +def test_compute_petco2hrf(mock_conv, mock_plot, mock_endtidal, testdir): |
| 33 | + out = os.path.join(testdir, 'Lune') |
| 34 | + co2 = np.array([1, 2, 3, 4]) |
| 35 | + pidx = np.array([0, 2]) |
| 36 | + r = regressors.compute_petco2hrf(co2, pidx, 1.0, out) |
| 37 | + assert np.all(r == np.array([9, 8, 7, 6])) |
| 38 | + os.remove(f'{out}_petco2.1D') |
| 39 | + |
| 40 | + |
| 41 | +def test_compute_petco2hrf_skip(testdir): |
| 42 | + out = os.path.join(testdir, 'Catherine') |
| 43 | + r = regressors.compute_petco2hrf( |
| 44 | + np.array([1, 2, 3]), |
| 45 | + np.array([0]), |
| 46 | + 1.0, |
| 47 | + out, |
| 48 | + comp_endtidal=False, |
| 49 | + response_function=None, |
| 50 | + ) |
| 51 | + assert np.allclose(r, np.array([-1, 0, 1])) # returns petco2 demeaned |
| 52 | + |
| 53 | + |
| 54 | +@patch('phys2cvr.regressors.x_corr', return_value=(None, 3, np.array([1, 2, 3]))) |
| 55 | +@patch('phys2cvr.regressors.plot_xcorr') |
| 56 | +def test_compute_bulk_shift(mock_plotx, mock_xcorr, testdir): |
| 57 | + out = os.path.join(testdir, 'Monoco') |
| 58 | + func = np.arange(10.0) |
| 59 | + pet = np.arange(20.0) |
| 60 | + s = regressors.compute_bulk_shift(func, pet, 1.0, out) |
| 61 | + assert s == 3 |
| 62 | + assert os.path.exists(f'{out}_optshift.1D') |
| 63 | + os.remove(f'{out}_optshift.1D') |
| 64 | + |
| 65 | + |
| 66 | +@patch('phys2cvr.regressors.export_regressor', return_value=np.zeros((5, 5))) |
| 67 | +def test_create_fine_shift_regressors(mock_export, testdir): |
| 68 | + out = os.path.join(testdir, 'Esquie') |
| 69 | + pet = np.arange(50.0) |
| 70 | + r = regressors.create_fine_shift_regressors(pet, 3, 2, 1.0, 10, 20, out) |
| 71 | + assert r.shape == (5, 5) |
| 72 | + |
| 73 | + |
| 74 | +@patch('phys2cvr.regressors.export_regressor', return_value=np.zeros((3, 3))) |
| 75 | +def test_create_fine_shift_regressors_padding(mock_export, testdir): |
| 76 | + out = os.path.join(testdir, 'Maelle') |
| 77 | + pet = np.arange(10.0) |
| 78 | + r = regressors.create_fine_shift_regressors(pet, 9, 4, 1.0, 5, 8, out) |
| 79 | + assert r.shape == (3, 3) |
| 80 | + |
| 81 | + |
| 82 | +@patch('phys2cvr.regressors.resample_signal_freqs', return_value=np.arange(20.0)) |
| 83 | +@patch('phys2cvr.regressors.plot_two_timeseries') |
| 84 | +@patch('phys2cvr.regressors.export_regressor', return_value=np.arange(10.0)) |
| 85 | +def test_create_physio_regressor(mock_exp, mock_plot, mock_bs, testdir): |
| 86 | + out = os.path.join(testdir, 'Sciel') |
| 87 | + func = np.arange(10.0) |
| 88 | + pet = np.arange(30.0) |
| 89 | + d, lags = regressors.create_physio_regressor( |
| 90 | + func, pet, 1.0, 1.0, out, lag_max=2, skip_xcorr=True |
| 91 | + ) |
| 92 | + assert d.shape == (10,) |
| 93 | + assert lags is not None |
| 94 | + |
| 95 | + |
| 96 | +@patch('phys2cvr.regressors.resample_signal_freqs', return_value=np.arange(20.0)) |
| 97 | +@patch('phys2cvr.regressors.export_regressor', return_value=np.arange(10.0)) |
| 98 | +def test_create_physio_regressor_no_lag_max(mock_exp, mock_bs, testdir): |
| 99 | + out = os.path.join(testdir, 'Francois') |
| 100 | + func = np.arange(10.0) |
| 101 | + pet = np.arange(30.0) |
| 102 | + d, lags = regressors.create_physio_regressor( |
| 103 | + func, pet, 1.0, 1.0, out, lag_max=None, skip_xcorr=True |
| 104 | + ) |
| 105 | + assert d.shape == (10,) |
| 106 | + assert lags is None |
| 107 | + os.remove(f'{out}_petco2hrf_vs_avgroi.png') |
| 108 | + |
| 109 | + |
| 110 | +# ## Break tests |
| 111 | + |
| 112 | + |
| 113 | +def test_break_compute_petco2hrf(): |
| 114 | + with pytest.raises(NotImplementedError) as errorinfo: |
| 115 | + regressors.compute_petco2hrf(np.zeros((2, 2)), np.array([0]), 1.0, 'x') |
| 116 | + assert 'Arrays with more' in str(errorinfo.value) |
0 commit comments