|
| 1 | +import time |
| 2 | +import numpy as np |
| 3 | +import nibabel as nb |
| 4 | +from nipype.interfaces import nilearn as nl |
| 5 | +from .. import images as im |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize('nvols, nmasks, ext, factor', [ |
| 11 | + (1000, 10, '.nii', 2), |
| 12 | + (1000, 10, '.nii.gz', 5), |
| 13 | + (200, 3, '.nii', 1.1), |
| 14 | + (200, 3, '.nii.gz', 2), |
| 15 | + (200, 10, '.nii', 1.1), |
| 16 | + (200, 10, '.nii.gz', 2), |
| 17 | + ]) |
| 18 | +def test_signal_extraction_equivalence(tmpdir, nvols, nmasks, ext, factor): |
| 19 | + orig_dir = tmpdir.chdir() |
| 20 | + |
| 21 | + vol_shape = (64, 64, 40) |
| 22 | + |
| 23 | + img_fname = 'img' + ext |
| 24 | + masks_fname = 'masks' + ext |
| 25 | + |
| 26 | + random_data = np.random.random(size=vol_shape + (nvols,)) * 2000 |
| 27 | + random_mask_data = np.random.random(size=vol_shape + (nmasks,)) < 0.2 |
| 28 | + |
| 29 | + nb.Nifti1Image(random_data, np.eye(4)).to_filename(img_fname) |
| 30 | + nb.Nifti1Image(random_mask_data.astype(np.uint8), np.eye(4)).to_filename(masks_fname) |
| 31 | + |
| 32 | + se1 = nl.SignalExtraction(in_file=img_fname, label_files=masks_fname, |
| 33 | + class_labels=['a%d' % i for i in range(nmasks)], |
| 34 | + out_file='nlsignals.tsv') |
| 35 | + se2 = im.SignalExtraction(in_file=img_fname, label_files=masks_fname, |
| 36 | + class_labels=['a%d' % i for i in range(nmasks)], |
| 37 | + out_file='imsignals.tsv') |
| 38 | + |
| 39 | + tic = time.time() |
| 40 | + se1.run() |
| 41 | + toc = time.time() |
| 42 | + se2.run() |
| 43 | + toc2 = time.time() |
| 44 | + |
| 45 | + tab1 = np.loadtxt('nlsignals.tsv', skiprows=1) |
| 46 | + tab2 = np.loadtxt('imsignals.tsv', skiprows=1) |
| 47 | + |
| 48 | + assert np.allclose(tab1, tab2) |
| 49 | + |
| 50 | + t1 = toc - tic |
| 51 | + t2 = toc2 - toc |
| 52 | + |
| 53 | + assert t2 < t1 / factor |
0 commit comments