本文整理汇总了Python中nilearn._utils.testing.write_tmp_imgs函数的典型用法代码示例。如果您正苦于以下问题:Python write_tmp_imgs函数的具体用法?Python write_tmp_imgs怎么用?Python write_tmp_imgs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_tmp_imgs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_iter_img
def test_iter_img():
img_3d = nibabel.Nifti1Image(np.ones((3, 4, 5)), np.eye(4))
testing.assert_raises_regex(TypeError,
"Input data has incompatible dimensionality: "
"Expected dimension is 4D and you provided "
"a 3D image.",
image.iter_img, img_3d)
affine = np.array([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[0., 0., 0., 1.]])
img_4d, _ = testing.generate_fake_fmri(affine=affine)
for i, img in enumerate(image.iter_img(img_4d)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(compat.get_affine(img),
compat.get_affine(img_4d))
with testing.write_tmp_imgs(img_4d) as img_4d_filename:
for i, img in enumerate(image.iter_img(img_4d_filename)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(compat.get_affine(img),
compat.get_affine(img_4d))
# enables to delete "img_4d_filename" on windows
del img
img_3d_list = list(image.iter_img(img_4d))
for i, img in enumerate(image.iter_img(img_3d_list)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(compat.get_affine(img),
compat.get_affine(img_4d))
with testing.write_tmp_imgs(*img_3d_list) as img_3d_filenames:
for i, img in enumerate(image.iter_img(img_3d_filenames)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(compat.get_affine(img),
compat.get_affine(img_4d))
# enables to delete "img_3d_filename" on windows
del img
示例2: test_check_niimg_3d
def test_check_niimg_3d():
# check error for non-forced but necessary resampling
assert_raises_regex(TypeError, 'nibabel format',
_utils.check_niimg, 0)
# check error for non-forced but necessary resampling
assert_raises_regex(TypeError, 'empty object',
_utils.check_niimg, [])
# Test dimensionality error
img = Nifti1Image(np.zeros((10, 10, 10)), np.eye(4))
assert_raises_regex(TypeError,
"Input data has incompatible dimensionality: "
"Expected dimension is 3D and you provided a list "
"of 3D images \(4D\).",
_utils.check_niimg_3d, [img, img])
# Check that a filename does not raise an error
data = np.zeros((40, 40, 40, 1))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img, create_files=True) as filename:
_utils.check_niimg_3d(filename)
# check data dtype equal with dtype='auto'
img_check = _utils.check_niimg_3d(img, dtype='auto')
assert_equal(img.get_data().dtype.kind, img_check.get_data().dtype.kind)
示例3: test_joblib_cache
def test_joblib_cache():
if not LooseVersion(nibabel.__version__) > LooseVersion('1.1.0'):
# Old nibabel do not pickle
raise SkipTest
from sklearn.externals.joblib import hash, Memory
mask = np.zeros((40, 40, 40))
mask[20, 20, 20] = 1
mask_img = Nifti1Image(mask, np.eye(4))
with testing.write_tmp_imgs(mask_img, create_files=True)\
as filename:
masker = NiftiMasker(mask_img=filename)
masker.fit()
mask_hash = hash(masker.mask_img_)
masker.mask_img_.get_data()
assert_true(mask_hash == hash(masker.mask_img_))
# Test a tricky issue with memmapped joblib.memory that makes
# imgs return by inverse_transform impossible to save
cachedir = mkdtemp()
try:
masker.memory = Memory(cachedir=cachedir, mmap_mode='r',
verbose=0)
X = masker.transform(mask_img)
# inverse_transform a first time, so that the result is cached
out_img = masker.inverse_transform(X)
out_img = masker.inverse_transform(X)
out_img.to_filename(os.path.join(cachedir, 'test.nii'))
finally:
shutil.rmtree(cachedir, ignore_errors=True)
示例4: test_smooth_img
def test_smooth_img():
# This function only checks added functionalities compared
# to _smooth_array()
shapes = ((10, 11, 12), (13, 14, 15))
lengths = (17, 18)
fwhm = (1., 2., 3.)
img1, mask1 = testing.generate_fake_fmri(shape=shapes[0],
length=lengths[0])
img2, mask2 = testing.generate_fake_fmri(shape=shapes[1],
length=lengths[1])
for create_files in (False, True):
with testing.write_tmp_imgs(img1, img2,
create_files=create_files) as imgs:
# List of images as input
out = image.smooth_img(imgs, fwhm)
assert_true(isinstance(out, list))
assert_true(len(out) == 2)
for o, s, l in zip(out, shapes, lengths):
assert_true(o.shape == (s + (l,)))
# Single image as input
out = image.smooth_img(imgs[0], fwhm)
assert_true(isinstance(out, nibabel.Nifti1Image))
assert_true(out.shape == (shapes[0] + (lengths[0],)))
示例5: test_check_niimg
def test_check_niimg():
# check error for non-forced but necessary resampling
assert_raises_regexp(TypeError, 'image',
_utils.check_niimg, 0)
# check error for non-forced but necessary resampling
assert_raises_regexp(TypeError, 'image',
_utils.check_niimg, [])
# Test ensure_3d
# check error for non-forced but necessary resampling
assert_raises_regexp(TypeError, '3D',
_utils.check_niimg, ['test.nii', ], ensure_3d=True)
# Check that a filename does not raise an error
data = np.zeros((40, 40, 40, 2))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img, create_files=True) as filename:
_utils.check_niimg(filename)
# Test ensure_3d with a in-memory object
assert_raises_regexp(TypeError, '3D',
_utils.check_niimg, data, ensure_3d=True)
# Test ensure_3d with a non 3D image
assert_raises_regexp(TypeError, '3D',
_utils.check_niimg, data_img, ensure_3d=True)
# Test ensure_3d with a 4D image with a length 1 4th dim
data = np.zeros((40, 40, 40, 1))
data_img = Nifti1Image(data, np.eye(4))
_utils.check_niimg(data_img, ensure_3d=True)
示例6: test_check_niimg
def test_check_niimg():
with assert_raises(TypeError) as cm:
_utils.check_niimg(0)
assert_true('image' in cm.exception.message
or 'affine' in cm.exception.message)
with assert_raises(TypeError) as cm:
_utils.check_niimg([])
assert_true('image' in cm.exception.message
or 'affine' in cm.exception.message)
# Test ensure_3d
with assert_raises(TypeError) as cm:
_utils.check_niimg(['test.nii', ], ensure_3d=True)
assert_true('3D' in cm.exception.message)
# Check that a filename does not raise an error
data = np.zeros((40, 40, 40, 2))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img, create_files=True)\
as filename:
_utils.check_niimg(filename)
# Test ensure_3d with a in-memory object
with assert_raises(TypeError) as cm:
_utils.check_niimg(data, ensure_3d=True)
assert_true('3D' in cm.exception.message)
# Test ensure_3d with a 4D image with a length 1 4th dim
data = np.zeros((40, 40, 40, 1))
data_img = Nifti1Image(data, np.eye(4))
_utils.check_niimg(data_img, ensure_3d=True)
示例7: test_mean_img
def test_mean_img():
rng = np.random.RandomState(42)
data1 = np.zeros((5, 6, 7))
data2 = rng.rand(5, 6, 7)
data3 = rng.rand(5, 6, 7, 3)
affine = np.diag((4, 3, 2, 1))
img1 = nibabel.Nifti1Image(data1, affine=affine)
img2 = nibabel.Nifti1Image(data2, affine=affine)
img3 = nibabel.Nifti1Image(data3, affine=affine)
for imgs in ([img1, ],
[img1, img2],
[img2, img1, img2],
[img3, img1, img2], # Mixture of 4D and 3D images
):
arrays = list()
# Ground-truth:
for img in imgs:
img = img.get_data()
if img.ndim == 4:
img = np.mean(img, axis=-1)
arrays.append(img)
truth = np.mean(arrays, axis=0)
mean_img = image.mean_img(imgs)
assert_array_equal(mean_img.get_affine(), affine)
assert_array_equal(mean_img.get_data(), truth)
# Test with files
with testing.write_tmp_imgs(*imgs) as imgs:
mean_img = image.mean_img(imgs)
assert_array_equal(mean_img.get_affine(), affine)
assert_array_equal(mean_img.get_data(), truth)
示例8: test_iter_img
def test_iter_img():
img_3d = nibabel.Nifti1Image(np.ones((3, 4, 5)), np.eye(4))
testing.assert_raises_regex(TypeError, '4D Niimg-like',
image.iter_img, img_3d)
affine = np.array([[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[0., 0., 0., 1.]])
img_4d, _ = testing.generate_fake_fmri(affine=affine)
for i, img in enumerate(image.iter_img(img_4d)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(img.get_affine(),
img_4d.get_affine())
with testing.write_tmp_imgs(img_4d) as img_4d_filename:
for i, img in enumerate(image.iter_img(img_4d_filename)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(img.get_affine(),
img_4d.get_affine())
# enables to delete "img_4d_filename" on windows
del img
img_3d_list = list(image.iter_img(img_4d))
for i, img in enumerate(image.iter_img(img_3d_list)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(img.get_affine(),
img_4d.get_affine())
with testing.write_tmp_imgs(*img_3d_list) as img_3d_filenames:
for i, img in enumerate(image.iter_img(img_3d_filenames)):
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(img.get_data(),
expected_data_3d)
assert_array_equal(img.get_affine(),
img_4d.get_affine())
# enables to delete "img_3d_filename" on windows
del img
示例9: test_mask_3d
def test_mask_3d():
# Dummy mask
data = np.zeros((40, 40, 40, 2))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img, create_files=True)\
as filename:
masker = NiftiMasker(mask_img=filename)
assert_raises(TypeError, masker.fit)
示例10: test_with_files
def test_with_files():
# Standard masking
data = np.zeros((40, 40, 40, 2))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img) as filename:
masker = NiftiMasker()
masker.fit(filename)
masker.transform(filename)
示例11: test_check_niimg
def test_check_niimg():
assert_raises(TypeError, _utils.check_niimg, 0)
assert_raises(TypeError, _utils.check_niimg, [])
# Check that a filename does not raise an error
data = np.zeros((40, 40, 40, 2))
data[20, 20, 20] = 1
data_img = Nifti1Image(data, np.eye(4))
with testing.write_tmp_imgs(data_img, create_files=True)\
as filename:
_utils.check_niimg(filename)
示例12: test_with_globbing_patterns_with_single_subject
def test_with_globbing_patterns_with_single_subject():
# single subject
data, mask_img, _, _ = _make_canica_test_data(n_subjects=1)
n_components = 3
canica = CanICA(n_components=n_components, mask=mask_img)
with write_tmp_imgs(data[0], create_files=True, use_wildcards=True) as img:
input_image = _tmp_dir() + img
canica.fit(input_image)
components_img = canica.components_img_
assert_true(isinstance(components_img, nibabel.Nifti1Image))
# n_components = 3
check_shape = data[0].shape[:3] + (3,)
assert_true(components_img.shape, check_shape)
示例13: test_math_img
def test_math_img():
img1 = Nifti1Image(np.ones((10, 10, 10, 10)), np.eye(4))
img2 = Nifti1Image(np.zeros((10, 10, 10, 10)), np.eye(4))
expected_result = Nifti1Image(np.ones((10, 10, 10)), np.eye(4))
formula = "np.mean(img1, axis=-1) - np.mean(img2, axis=-1)"
for create_files in (True, False):
with testing.write_tmp_imgs(img1, img2,
create_files=create_files) as imgs:
result = math_img(formula, img1=imgs[0], img2=imgs[1])
assert_array_equal(result.get_data(),
expected_result.get_data())
assert_array_equal(result.affine, expected_result.affine)
assert_equal(result.shape, expected_result.shape)
示例14: test_resampling_error_checks
def test_resampling_error_checks():
shape = (3, 2, 5, 2)
target_shape = (5, 3, 2)
affine = np.eye(4)
data = np.random.randint(0, 10, shape)
img = Nifti1Image(data, affine)
# Correct parameters: no exception
resample_img(img, target_shape=target_shape, target_affine=affine)
resample_img(img, target_affine=affine)
with testing.write_tmp_imgs(img) as filename:
resample_img(filename, target_shape=target_shape, target_affine=affine)
# Missing parameter
assert_raises(ValueError, resample_img, img, target_shape=target_shape)
# Invalid shape
assert_raises(ValueError, resample_img, img, target_shape=(2, 3),
target_affine=affine)
# Invalid interpolation
interpolation = 'an_invalid_interpolation'
pattern = "interpolation must be either.+{0}".format(interpolation)
testing.assert_raises_regex(ValueError, pattern,
resample_img, img, target_shape=target_shape,
target_affine=affine,
interpolation="an_invalid_interpolation")
# Noop
target_shape = shape[:3]
img_r = resample_img(img, copy=False)
assert_equal(img_r, img)
img_r = resample_img(img, copy=True)
assert_false(np.may_share_memory(img_r.get_data(), img.get_data()))
np.testing.assert_almost_equal(img_r.get_data(), img.get_data())
np.testing.assert_almost_equal(img_r.get_affine(), img.get_affine())
img_r = resample_img(img, target_affine=affine, target_shape=target_shape,
copy=False)
assert_equal(img_r, img)
img_r = resample_img(img, target_affine=affine, target_shape=target_shape,
copy=True)
assert_false(np.may_share_memory(img_r.get_data(), img.get_data()))
np.testing.assert_almost_equal(img_r.get_data(), img.get_data())
np.testing.assert_almost_equal(img_r.get_affine(), img.get_affine())
示例15: test_joblib_cache
def test_joblib_cache():
from sklearn.externals.joblib import hash
# Dummy mask
mask = np.zeros((40, 40, 40))
mask[20, 20, 20] = 1
mask_img = Nifti1Image(mask, np.eye(4))
with write_tmp_imgs(mask_img, create_files=True) as filename:
masker = MultiNiftiMasker(mask_img=filename)
masker.fit()
mask_hash = hash(masker.mask_img_)
masker.mask_img_.get_data()
assert_true(mask_hash == hash(masker.mask_img_))
# enables to delete "filename" on windows
del masker