diff --git a/tests/test_dtifit.py b/tests/test_dtifit.py index 9e9f80ac07808fc5343ebabae4e3e974e5162225..00fc058ffca6dabc4f70a0e15459748681d0d539 100644 --- a/tests/test_dtifit.py +++ b/tests/test_dtifit.py @@ -169,12 +169,12 @@ def test_DTIFitTensor(): l2file = op.join(testdir, 'dti_L2.nii') l3file = op.join(testdir, 'dti_L3.nii') - v1 = tests.make_random_image(v1file, (5, 5, 5, 3)).get_data() - v2 = tests.make_random_image(v2file, (5, 5, 5, 3)).get_data() - v3 = tests.make_random_image(v3file, (5, 5, 5, 3)).get_data() - l1 = tests.make_random_image(l1file, (5, 5, 5)) .get_data() - l2 = tests.make_random_image(l2file, (5, 5, 5)) .get_data() - l3 = tests.make_random_image(l3file, (5, 5, 5)) .get_data() + v1 = np.asanyarray(tests.make_random_image(v1file, (5, 5, 5, 3)).dataobj) + v2 = np.asanyarray(tests.make_random_image(v2file, (5, 5, 5, 3)).dataobj) + v3 = np.asanyarray(tests.make_random_image(v3file, (5, 5, 5, 3)).dataobj) + l1 = np.asanyarray(tests.make_random_image(l1file, (5, 5, 5)) .dataobj) + l2 = np.asanyarray(tests.make_random_image(l2file, (5, 5, 5)) .dataobj) + l3 = np.asanyarray(tests.make_random_image(l3file, (5, 5, 5)) .dataobj) dtiobj = dtifit.DTIFitTensor(testdir) diff --git a/tests/test_ensure.py b/tests/test_ensure.py index 961377d39b841b52cb6d804deff119021edd526c..a8ce2e68bec571c5f3dde218ade22355e51cf664 100644 --- a/tests/test_ensure.py +++ b/tests/test_ensure.py @@ -27,7 +27,7 @@ def test_ensureIsImage(): for l in loaded: assert isinstance(l, nib.nifti1.Nifti1Image) - assert np.all(img.get_data() == l.get_data()) + assert np.all(np.asanyarray(img.dataobj) == np.asanyarray(l.dataobj)) l = None loaded = None diff --git a/tests/test_image.py b/tests/test_image.py index 3bb77d01a3d8f6e7d559fc79796db7097c04615e..9686f8739d7aacaf94898f80a433affc9d094311 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -823,7 +823,7 @@ def _test_Image_changeData(imgtype): img.register('name3', onDataRange, 'dataRange') # Calculate the actual data range - data = img.nibImage.get_data() + data = np.asanyarray(img.nibImage.dataobj) dmin = data.min() dmax = data.max() drange = dmax - dmin @@ -1135,7 +1135,7 @@ def _test_Image_init_xform(imgtype): # an image created off a # header should have # identical sform/qform - fimg = fslimage.Image(img.get_data(), header=img.header) + fimg = fslimage.Image(np.asanyarray(img.dataobj), header=img.header) fsform, fsform_code = fimg.header.get_sform(True) fqform, fqform_code = fimg.header.get_qform(True) @@ -1153,7 +1153,7 @@ def _test_Image_init_xform(imgtype): # set to that xform, # qform to None, and # and codes set to (s2, q0) - fimg = fslimage.Image(img.get_data(), xform=sform) + fimg = fslimage.Image(np.asanyarray(img.dataobj), xform=sform) fsform, fsform_code = fimg.header.get_sform(True) fqform, fqform_code = fimg.header.get_qform(True) @@ -1174,7 +1174,7 @@ def _test_Image_init_xform(imgtype): rxform = affine.compose(np.random.random(3), np.random.random(3), np.random.random(3)) - fimg = fslimage.Image(img.get_data(), + fimg = fslimage.Image(np.asanyarray(img.dataobj), header=img.header, xform=rxform) diff --git a/tests/test_imagewrapper.py b/tests/test_imagewrapper.py index fc78da8460f3f1d7ed20364206d3ef12cbae0934..868245fefd24738197b47c2eab4e3214ffc28837 100644 --- a/tests/test_imagewrapper.py +++ b/tests/test_imagewrapper.py @@ -790,7 +790,7 @@ def _test_ImageWrapper_write_out(niters, seed, threaded): wrapper[tuple(sliceobjs)] = newData _ImageWraper_busy_wait(wrapper) - expLo, expHi = coverageDataRange(img.get_data(), cov, slices) + expLo, expHi = coverageDataRange(np.asanyarray(img.dataobj), cov, slices) newLo, newHi = wrapper.dataRange # print('Old range: {} - {}'.format(clo, chi)) @@ -917,10 +917,10 @@ def _test_ImageWrapper_write_in_overlap(niters, seed, threaded): print('Expected range: {} - {}'.format(expLo, expHi)) print('New range: {} - {}'.format(newLo, newHi)) - print('Slice min/max: {} - {}'.format(img.get_data()[tuple(sliceobjs)].min(), - img.get_data()[tuple(sliceobjs)].max())) - print('Data min/max: {} - {}'.format(img.get_data().min(), - img.get_data().max())) + print('Slice min/max: {} - {}'.format(np.asanyarray(img.dataobj)[tuple(sliceobjs)].min(), + np.asanyarray(img.dataobj)[tuple(sliceobjs)].max())) + print('Data min/max: {} - {}'.format(np.asanyarray(img.dataobj).min(), + np.asanyarray(img.dataobj).max())) assert np.all(newCov == expCov) diff --git a/tests/test_immv_imcp.py b/tests/test_immv_imcp.py index dd1273d78e162a3ee96638cfba4b12850d1b080f..3e324841a118b16482d314251384188b78ad614b 100644 --- a/tests/test_immv_imcp.py +++ b/tests/test_immv_imcp.py @@ -14,6 +14,7 @@ import os import shutil import tempfile +import numpy as np import nibabel as nib import fsl.utils.imcp as imcp @@ -31,7 +32,7 @@ def print(*args, **kwargs): def makeImage(filename): - return hash(make_random_image(filename).get_data().tobytes()) + return hash(np.asanyarray(make_random_image(filename).dataobj).tobytes()) def checkImageHash(filename, datahash): @@ -39,7 +40,7 @@ def checkImageHash(filename, datahash): """ img = nib.load(filename) - assert hash(img.get_data().tobytes()) == datahash + assert hash(np.asanyarray(img.dataobj).tobytes()) == datahash def checkFilesToExpect(files, outdir, outputType, datahashes): diff --git a/tests/test_mghimage.py b/tests/test_mghimage.py index f34f635c17759eb9803d34c0405d4a2d93e4a9eb..e2ad7739f4796b41297090d09882d0ef5cac6dfe 100644 --- a/tests/test_mghimage.py +++ b/tests/test_mghimage.py @@ -31,7 +31,7 @@ def test_MGHImage(): v2s = nbimg.header.get_vox2ras_tkr() w2s = affine.concat(v2s, affine.invert(nbimg.affine)) - assert np.all(np.isclose(img[:], nbimg.get_data())) + assert np.all(np.isclose(img[:], np.asanyarray(nbimg.dataobj))) assert np.all(np.isclose(img.voxToWorldMat, nbimg.affine)) assert np.all(np.isclose(img.voxToSurfMat, v2s)) assert np.all(np.isclose(img.surfToVoxMat, affine.invert(v2s))) @@ -44,7 +44,7 @@ def test_MGHImage(): # Load from an in-memory nibabel object img = fslmgh.MGHImage(nbimg) - assert np.all(np.isclose(img[:], nbimg.get_data())) + assert np.all(np.isclose(img[:], np.asanyarray(nbimg.dataobj))) assert np.all(np.isclose(img.voxToWorldMat, nbimg.affine)) assert img.dataSource is None assert img.mghImageFile is None diff --git a/tests/test_scripts/test_immv_imcp.py b/tests/test_scripts/test_immv_imcp.py index 607a7da5d761d69c824f5d85f27751b8bf5a395d..b5a2f595cdbc501b894e7d264fd3fa47467c07df 100644 --- a/tests/test_scripts/test_immv_imcp.py +++ b/tests/test_scripts/test_immv_imcp.py @@ -269,7 +269,7 @@ def test_imcp_script_shouldPass(move=False): for inf in infiles: img = nib.load(op.join(tindir, inf), mmap=False) - imghash = hash(img.get_data().tobytes()) + imghash = hash(np.asanyarray(img.dataobj).tobytes()) img = None imageHashes.append(imghash) diff --git a/tests/test_wrapperutils.py b/tests/test_wrapperutils.py index 601e9f8ffa25a423d05ae26360708f11ad7eab88..e4ffb90b20b92bf8e830b5d834cc2cf2013f7c12 100644 --- a/tests/test_wrapperutils.py +++ b/tests/test_wrapperutils.py @@ -231,8 +231,8 @@ def test_fileOrImage(): @wutils.fileOrImage('img1', 'img2', 'output') def func(img1, **kwargs): - img1 = nib.load(img1).get_data() - img2 = nib.load(kwargs['img2']).get_data() + img1 = np.asanyarray(nib.load(img1).dataobj) + img2 = np.asanyarraynib.load(kwargs['img2']).dataobj) output = nib.nifti1.Nifti1Image(img1 * img2, np.eye(4)) nib.save(output, kwargs['output']) @@ -247,43 +247,43 @@ def test_fileOrImage(): # file file file func('img1.nii', img2='img2.nii', output='output.nii') - assert np.all(nib.load('output.nii').get_data() == expected) + assert np.all(np.asanyarray(nib.load('output.nii').dataobj) == expected) os.remove('output.nii') # file file array result = func('img1.nii', img2='img2.nii', output=wutils.LOAD)['output'] - assert np.all(result.get_data() == expected) + assert np.all(np.asanyarrary(result.dataobj) == expected) # file array file func('img1.nii', img2=img2, output='output.nii') - assert np.all(nib.load('output.nii').get_data() == expected) + assert np.all(np.asanyarray(nib.load('output.nii').dataobj) == expected) os.remove('output.nii') # file array array result = func('img1.nii', img2=img2, output=wutils.LOAD)['output'] - assert np.all(result.get_data() == expected) + assert np.all(np.asanyarray(result.dataobj) == expected) # array file file func(img1, img2='img2.nii', output='output.nii') - assert np.all(nib.load('output.nii').get_data() == expected) + assert np.all(np.asanyarray(nib.load('output.nii').dataobj) == expected) os.remove('output.nii') # array file array result = func(img1, img2='img2.nii', output=wutils.LOAD)['output'] - assert np.all(result.get_data() == expected) + assert np.all(np.asanyarray(result.dataobj) == expected) # array array file func(img1, img2=img2, output='output.nii') - assert np.all(nib.load('output.nii').get_data() == expected) + assert np.all(np.asanyarray(nib.load('output.nii').dataobj) == expected) os.remove('output.nii') # array array array result = func(img1, img2=img2, output=wutils.LOAD)['output'] - assert np.all(result.get_data() == expected) + assert np.all(np.asanyarray(result.dataobj) == expected) # in-memory image, file, file result = func(img3, img2='img2.nii', output='output.nii') - assert np.all(nib.load('output.nii').get_data() == expected) + assert np.all(np.asanyarray(nib.load('output.nii').dataobj) == expected) os.remove('output.nii') # fslimage, file, load @@ -307,7 +307,7 @@ def test_fileOrImage(): # nib.image, nib.image, load result = func(img1, img2=img2, output=wutils.LOAD)['output'] assert isinstance(result, nib.nifti1.Nifti1Image) - assert np.all(result.get_data()[:] == expected) + assert np.all(np.asanyarray(result.dataobj)[:] == expected) def test_fileOrThing_sequence(): @@ -357,7 +357,7 @@ def test_fileOrThing_outprefix(): @wutils.fileOrImage('img', outprefix='output_base') def basefunc(img, output_base): - img = nib.load(img).get_data() + img = np.asanyarray(nib.load(img).dataobj) out1 = nib.nifti1.Nifti1Image(img * 5, np.eye(4)) out2 = nib.nifti1.Nifti1Image(img * 10, np.eye(4)) @@ -368,31 +368,31 @@ def test_fileOrThing_outprefix(): with tempdir.tempdir() as td: img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4)) - exp1 = img.get_data() * 5 - exp2 = img.get_data() * 10 + exp1 = np.asanyarray(img.dataobj) * 5 + exp2 = np.asanyarray(img.dataobj) * 10 nib.save(img, 'img.nii') basefunc('img.nii', 'myout') - assert np.all(nib.load('myout_times5.nii.gz') .get_data() == exp1) - assert np.all(nib.load('myout_times10.nii.gz').get_data() == exp2) + assert np.all(np.asanyarray(nib.load('myout_times5.nii.gz') .dataobj) == exp1) + assert np.all(np.asanyarray(nib.load('myout_times10.nii.gz').dataobj) == exp2) cleardir(td, 'myout*') basefunc(img, 'myout') - assert np.all(nib.load('myout_times5.nii.gz') .get_data() == exp1) - assert np.all(nib.load('myout_times10.nii.gz').get_data() == exp2) + assert np.all(np.asanyarray(nib.load('myout_times5.nii.gz') .dataobj) == exp1) + assert np.all(np.asanyarray(nib.load('myout_times10.nii.gz').dataobj) == exp2) cleardir(td, 'myout*') res = basefunc(img, 'myout', myout_times5=wutils.LOAD) - assert np.all(res['myout_times5'].get_data() == exp1) + assert np.all(np.asanyarray(res['myout_times5'].dataobj) == exp1) cleardir(td, 'myout*') res = basefunc(img, 'myout', myout_times10=wutils.LOAD) - assert np.all(res['myout_times10'].get_data() == exp2) + assert np.all(np.asanyarray(res['myout_times10'].dataobj) == exp2) cleardir(td, 'myout*') res = basefunc(img, 'myout', myout=wutils.LOAD) - assert np.all(res['myout_times5'] .get_data() == exp1) - assert np.all(res['myout_times10'].get_data() == exp2) + assert np.all(np.asanyarray(res['myout_times5'] .dataobj) == exp1) + assert np.all(np.asanyarray(res['myout_times10'].dataobj) == exp2) cleardir(td, 'myout*') @@ -402,7 +402,7 @@ def test_fileOrThing_outprefix_differentTypes(): def func(img, outpref): img = nib.load(img) - img = nib.nifti1.Nifti1Image(img.get_data() * 2, np.eye(4)) + img = nib.nifti1.Nifti1Image(np.asanyarray(img.dataobj) * 2, np.eye(4)) text = '1234567890' nib.save(img, '{}_image.nii.gz' .format(outpref)) @@ -412,23 +412,23 @@ def test_fileOrThing_outprefix_differentTypes(): with tempdir.tempdir() as td: img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4)) - expi = img.get_data() * 2 + expi = np.asanyarray(img.dataobj) * 2 expt = '1234567890' func(img, 'myout') - assert np.all(nib.load('myout_image.nii.gz') .get_data() == expi) + assert np.all(np.asanyarray(nib.load('myout_image.nii.gz') .dataobj) == expi) with open('myout_text.txt', 'rt') as f: assert f.read().strip() == expt cleardir(td, 'myout*') res = func(img, 'myout', myout_image=wutils.LOAD) assert list(res.keys()) == ['myout_image'] - assert np.all(res['myout_image'].get_data() == expi) + assert np.all(np.asanyarray(res['myout_image'].dataobj) == expi) cleardir(td, 'myout*') res = func(img, 'myout', myout=wutils.LOAD) assert list(res.keys()) == ['myout_image'] - assert np.all(res['myout_image'].get_data() == expi) + assert np.all(np.asanyarray(res['myout_image'].dataobj) == expi) cleardir(td, 'myout*') res = func(img, 'myout', myout_text=wutils.LOAD) @@ -445,8 +445,8 @@ def test_fileOrThing_outprefix_directory(): @wutils.fileOrImage('img', outprefix='outpref') def func(img, outpref): img = nib.load(img) - img2 = nib.nifti1.Nifti1Image(img.get_data() * 2, np.eye(4)) - img4 = nib.nifti1.Nifti1Image(img.get_data() * 4, np.eye(4)) + img2 = nib.nifti1.Nifti1Image(np.asanyarray(img.dataobj) * 2, np.eye(4)) + img4 = nib.nifti1.Nifti1Image(np.asanyarray(img.dataobj) * 4, np.eye(4)) outdir = op.abspath('{}_imgs'.format(outpref)) @@ -457,8 +457,8 @@ def test_fileOrThing_outprefix_directory(): with tempdir.tempdir() as td: img = nib.nifti1.Nifti1Image(np.array([[1, 2], [3, 4]]), np.eye(4)) - exp2 = img.get_data() * 2 - exp4 = img.get_data() * 4 + exp2 = np.asanyarray(img.dataobj) * 2 + exp4 = np.asanyarray(img.dataobj) * 4 res = func(img, 'myout') assert len(res) == 0 @@ -469,17 +469,17 @@ def test_fileOrThing_outprefix_directory(): res = func(img, 'myout', myout_imgs=wutils.LOAD) assert len(res) == 2 - assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2) - assert np.all(res[op.join('myout_imgs', 'img4')].get_data() == exp4) + assert np.all(np.asanyarray(res[op.join('myout_imgs', 'img2')].dataobj) == exp2) + assert np.all(np.asanyarray(res[op.join('myout_imgs', 'img4')].dataobj) == exp4) res = func(img, 'myout', **{op.join('myout_imgs', 'img2') : wutils.LOAD}) assert len(res) == 1 - assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2) + assert np.all(np.asanyarray(res[op.join('myout_imgs', 'img2')].dataobj) == exp2) res = func(img, 'myout', **{op.join('myout_imgs', 'img') : wutils.LOAD}) assert len(res) == 2 - assert np.all(res[op.join('myout_imgs', 'img2')].get_data() == exp2) - assert np.all(res[op.join('myout_imgs', 'img4')].get_data() == exp4) + assert np.all(np.asanyarray(res[op.join('myout_imgs', 'img2')].dataobj) == exp2) + assert np.all(np.asanyarray(res[op.join('myout_imgs', 'img4')].dataobj) == exp4) os.mkdir('foo') res = func(img, op.join('foo', 'myout')) @@ -492,8 +492,8 @@ def test_fileOrThing_outprefix_directory(): os.mkdir('foo') res = func(img, op.join('foo', 'myout'), **{op.join('foo', 'myout') : wutils.LOAD}) assert len(res) == 2 - assert np.all(res[op.join('foo', 'myout_imgs', 'img2')].get_data() == exp2) - assert np.all(res[op.join('foo', 'myout_imgs', 'img4')].get_data() == exp4) + assert np.all(np.asanyarray(res[op.join('foo', 'myout_imgs', 'img2')].dataobj) == exp2) + assert np.all(np.asanyarray(res[op.join('foo', 'myout_imgs', 'img4')].dataobj) == exp4) def test_chained_fileOrImageAndArray(): @@ -503,7 +503,7 @@ def test_chained_fileOrImageAndArray(): image = nib.load(image) array = np.loadtxt(array) - outimg = nib.nifti1.Nifti1Image(image.get_data() * 2, np.eye(4)) + outimg = nib.nifti1.Nifti1Image(np.asanyarray(image.dataobj) * 2, np.eye(4)) np.savetxt(outarray, array * 2) outimg.to_filename(outimage) @@ -511,7 +511,7 @@ def test_chained_fileOrImageAndArray(): image = nib.nifti1.Nifti1Image(np.array([[1, 2], [ 3, 4]]), np.eye(4)) array = np.array([[5, 6, 7, 8]]) - expimg = nib.nifti1.Nifti1Image(image.get_data() * 2, np.eye(4)) + expimg = nib.nifti1.Nifti1Image(np.asanyarray(image.dataobj) * 2, np.eye(4)) exparr = array * 2 with tempdir.tempdir(): @@ -520,31 +520,31 @@ def test_chained_fileOrImageAndArray(): np.savetxt('array.txt', array) func('image.nii', 'array.txt', 'outimg.nii', 'outarr.txt') - assert np.all(nib.load('outimg.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('outimg.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('outarr.txt') == exparr) func('image.nii', array, 'outimg.nii', 'outarr.txt') - assert np.all(nib.load('outimg.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('outimg.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('outarr.txt') == exparr) func( image, 'array.txt', 'outimg.nii', 'outarr.txt') - assert np.all(nib.load('outimg.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('outimg.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('outarr.txt') == exparr) func( image, array, 'outimg.nii', 'outarr.txt') - assert np.all(nib.load('outimg.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('outimg.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('outarr.txt') == exparr) res = func(image, array, wutils.LOAD, 'outarr.txt') - assert np.all(res['outimage'].get_data() == expimg.get_data()) + assert np.all(np.asanyarray(res['outimage'].dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('outarr.txt') == exparr) res = func(image, array, 'outimg.nii', wutils.LOAD) - assert np.all(nib.load('outimg.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('outimg.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(res['outarray'] == exparr) res = func(image, array, wutils.LOAD, wutils.LOAD) - assert np.all(res['outimage'].get_data() == expimg.get_data()) + assert np.all(np.asanyarray(res['outimage'].dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(res['outarray'] == exparr) @@ -561,7 +561,7 @@ def test_fileOrThing_chained_outprefix(): image = nib.load(image) array = np.loadtxt(array) - outimg = nib.nifti1.Nifti1Image(image.get_data() * 2, np.eye(4)) + outimg = nib.nifti1.Nifti1Image(np.asanyarray(image.dataobj) * 2, np.eye(4)) outarr = array * 2 np.savetxt('{}_array.txt'.format(out), outarr) @@ -570,17 +570,17 @@ def test_fileOrThing_chained_outprefix(): image = nib.nifti1.Nifti1Image(np.array([[1, 2], [ 3, 4]]), np.eye(4)) array = np.array([[5, 6, 7, 8]]) - expimg = nib.nifti1.Nifti1Image(image.get_data() * 2, np.eye(4)) + expimg = nib.nifti1.Nifti1Image(np.asanyarray(image.dataobj) * 2, np.eye(4)) exparr = array * 2 with tempdir.tempdir(): func(image, array, 'myout') - assert np.all(nib.load('myout_image.nii').get_data() == expimg.get_data()) + assert np.all(np.asanyarray(nib.load('myout_image.nii').dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(np.loadtxt('myout_array.txt') == exparr) res = func(image, array, wutils.LOAD) - assert np.all(res['out_image'].get_data() == expimg.get_data()) + assert np.all(np.asanyarray(res['out_image'].dataobj) == np.asanyarray(expimg.dataobj)) assert np.all(res['out_array'] == exparr)