本文整理汇总了Python中nilearn._utils.testing.generate_fake_fmri函数的典型用法代码示例。如果您正苦于以下问题:Python generate_fake_fmri函数的具体用法?Python generate_fake_fmri怎么用?Python generate_fake_fmri使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了generate_fake_fmri函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_generate_fake_fmri
def test_generate_fake_fmri():
shapes = [(6, 6, 7), (10, 11, 12)]
lengths = [16, 20]
kinds = ['noise', 'step']
n_blocks = [None, 1, 4]
block_size = [None, 4]
block_type = ['classification', 'regression']
rand_gen = np.random.RandomState(3)
for shape, length, kind, n_block, bsize, btype in itertools.product(
shapes, lengths, kinds, n_blocks, block_size, block_type):
if n_block is None:
fmri, mask = generate_fake_fmri(
shape=shape, length=length, kind=kind,
n_blocks=n_block, block_size=bsize,
block_type=btype,
rand_gen=rand_gen)
else:
fmri, mask, target = generate_fake_fmri(
shape=shape, length=length, kind=kind,
n_blocks=n_block, block_size=bsize,
block_type=btype,
rand_gen=rand_gen)
assert_equal(fmri.shape[:-1], shape)
assert_equal(fmri.shape[-1], length)
if n_block is not None:
assert_equal(target.size, length)
assert_raises(ValueError, generate_fake_fmri, length=10, n_blocks=10,
block_size=None, rand_gen=rand_gen)
示例2: 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],)))
示例3: test_index_img
def test_index_img():
img_3d = nibabel.Nifti1Image(np.ones((3, 4, 5)), np.eye(4))
testing.assert_raises_regex(TypeError, '4D Niimg-like',
image.index_img, img_3d, 0)
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)
fourth_dim_size = img_4d.shape[3]
tested_indices = (list(range(fourth_dim_size)) +
[slice(2, 8, 2), [1, 2, 3, 2], [],
(np.arange(fourth_dim_size) % 3) == 1])
for i in tested_indices:
this_img = image.index_img(img_4d, i)
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(this_img.get_data(),
expected_data_3d)
assert_array_equal(this_img.get_affine(),
img_4d.get_affine())
for i in [fourth_dim_size, - fourth_dim_size - 1,
[0, fourth_dim_size],
np.repeat(True, fourth_dim_size + 1)]:
testing.assert_raises_regex(
IndexError,
'out of bounds|invalid index|out of range',
image.index_img, img_4d, i)
示例4: test_index_img
def test_index_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.index_img, img_3d, 0)
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)
fourth_dim_size = img_4d.shape[3]
tested_indices = (list(range(fourth_dim_size)) +
[slice(2, 8, 2), [1, 2, 3, 2], [],
(np.arange(fourth_dim_size) % 3) == 1])
for i in tested_indices:
this_img = image.index_img(img_4d, i)
expected_data_3d = img_4d.get_data()[..., i]
assert_array_equal(this_img.get_data(),
expected_data_3d)
assert_array_equal(compat.get_affine(this_img),
compat.get_affine(img_4d))
for i in [fourth_dim_size, - fourth_dim_size - 1,
[0, fourth_dim_size],
np.repeat(True, fourth_dim_size + 1)]:
testing.assert_raises_regex(
IndexError,
'out of bounds|invalid index|out of range|boolean index',
image.index_img, img_4d, i)
示例5: 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],)))
# Check corner case situations when fwhm=0, See issue #1537
# Test whether function smooth_img raises a warning when fwhm=0.
assert_warns(UserWarning, image.smooth_img, img1, fwhm=0.)
# Test output equal when fwhm=None and fwhm=0
out_fwhm_none = image.smooth_img(img1, fwhm=None)
out_fwhm_zero = image.smooth_img(img1, fwhm=0.)
assert_array_equal(out_fwhm_none.get_data(), out_fwhm_zero.get_data())
data1 = np.zeros((10, 11, 12))
data1[2:4, 1:5, 3:6] = 1
data2 = np.zeros((13, 14, 15))
data2[2:4, 1:5, 3:6] = 9
img1_nifti2 = nibabel.Nifti2Image(data1, affine=np.eye(4))
img2_nifti2 = nibabel.Nifti2Image(data2, affine=np.eye(4))
out = image.smooth_img([img1_nifti2, img2_nifti2], fwhm=1.)
示例6: 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
示例7: 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
示例8: test_high_variance_confounds
def test_high_variance_confounds():
# See also test_signals.test_high_variance_confounds()
# There is only tests on what is added by image.high_variance_confounds()
# compared to signal.high_variance_confounds()
shape = (40, 41, 42)
length = 17
n_confounds = 10
img, mask_img = testing.generate_fake_fmri(shape=shape, length=length)
confounds1 = image.high_variance_confounds(img, mask_img=mask_img,
percentile=10.,
n_confounds=n_confounds)
assert_true(confounds1.shape == (length, n_confounds))
# No mask.
confounds2 = image.high_variance_confounds(img, percentile=10.,
n_confounds=n_confounds)
assert_true(confounds2.shape == (length, n_confounds))
示例9: test_matrix_orientation
def test_matrix_orientation():
"""Test if processing is performed along the correct axis."""
# the "step" kind generate heavyside-like signals for each voxel.
# all signals being identical, standardizing along the wrong axis
# would leave a null signal. Along the correct axis, the step remains.
fmri, mask = testing.generate_fake_fmri(shape=(40, 41, 42), kind="step")
masker = NiftiMasker(mask_img=mask, standardize=True, detrend=True)
timeseries = masker.fit_transform(fmri)
assert(timeseries.shape[0] == fmri.shape[3])
assert(timeseries.shape[1] == mask.get_data().sum())
std = timeseries.std(axis=0)
assert(std.shape[0] == timeseries.shape[1]) # paranoid
assert(not np.any(std < 0.1))
# Test inverse transform
masker = NiftiMasker(mask_img=mask, standardize=False, detrend=False)
masker.fit()
timeseries = masker.transform(fmri)
recovered = masker.inverse_transform(timeseries)
np.testing.assert_array_almost_equal(recovered.get_data(), fmri.get_data())
示例10: test_pd_index_img
def test_pd_index_img():
# confirm indices from pandas dataframes are handled correctly
if 'pandas' not in sys.modules:
raise SkipTest
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)
fourth_dim_size = img_4d.shape[3]
rng = np.random.RandomState(0)
arr = rng.rand(fourth_dim_size) > 0.5
df = pd.DataFrame({"arr": arr})
np_index_img = image.index_img(img_4d, arr)
pd_index_img = image.index_img(img_4d, df)
assert_array_equal(np_index_img.get_data(),
pd_index_img.get_data())
示例11: test_nifti_maps_masker_2
def test_nifti_maps_masker_2():
# Test resampling in NiftiMapsMasker
affine = np.eye(4)
shape1 = (10, 11, 12) # fmri
shape2 = (13, 14, 15) # mask
shape3 = (16, 17, 18) # maps
n_regions = 9
length = 3
fmri11_img, _ = generate_random_img(shape1, affine=affine,
length=length)
_, mask22_img = generate_random_img(shape2, affine=affine,
length=length)
maps33_img, _ = \
testing.generate_maps(shape3, n_regions, affine=affine)
mask_img_4d = nibabel.Nifti1Image(np.ones((2, 2, 2, 2), dtype=np.int8),
affine=np.diag((4, 4, 4, 1)))
# verify that 4D mask arguments are refused
masker = NiftiMapsMasker(maps33_img, mask_img=mask_img_4d)
testing.assert_raises_regex(DimensionError,
"Input data has incompatible dimensionality: "
"Expected dimension is 3D and you provided "
"a 4D image.",
masker.fit)
# Test error checking
assert_raises(ValueError, NiftiMapsMasker, maps33_img,
resampling_target="mask")
assert_raises(ValueError, NiftiMapsMasker, maps33_img,
resampling_target="invalid")
# Target: mask
masker = NiftiMapsMasker(maps33_img, mask_img=mask22_img,
resampling_target="mask")
masker.fit()
np.testing.assert_almost_equal(get_affine(masker.mask_img_),
get_affine(mask22_img))
assert_equal(masker.mask_img_.shape, mask22_img.shape)
np.testing.assert_almost_equal(get_affine(masker.mask_img_),
get_affine(masker.maps_img_))
assert_equal(masker.mask_img_.shape, masker.maps_img_.shape[:3])
transformed = masker.transform(fmri11_img)
assert_equal(transformed.shape, (length, n_regions))
fmri11_img_r = masker.inverse_transform(transformed)
np.testing.assert_almost_equal(get_affine(fmri11_img_r),
get_affine(masker.maps_img_))
assert_equal(fmri11_img_r.shape, (masker.maps_img_.shape[:3] + (length,)))
# Target: maps
masker = NiftiMapsMasker(maps33_img, mask_img=mask22_img,
resampling_target="maps")
masker.fit()
np.testing.assert_almost_equal(get_affine(masker.maps_img_),
get_affine(maps33_img))
assert_equal(masker.maps_img_.shape, maps33_img.shape)
np.testing.assert_almost_equal(get_affine(masker.mask_img_),
get_affine(masker.maps_img_))
assert_equal(masker.mask_img_.shape, masker.maps_img_.shape[:3])
transformed = masker.transform(fmri11_img)
assert_equal(transformed.shape, (length, n_regions))
fmri11_img_r = masker.inverse_transform(transformed)
np.testing.assert_almost_equal(get_affine(fmri11_img_r),
get_affine(masker.maps_img_))
assert_equal(fmri11_img_r.shape, (masker.maps_img_.shape[:3] + (length,)))
# Test with clipped maps: mask does not contain all maps.
# Shapes do matter in that case
affine1 = np.eye(4)
shape1 = (10, 11, 12)
shape2 = (8, 9, 10) # mask
affine2 = np.diag((2, 2, 2, 1)) # just for mask
shape3 = (16, 18, 20) # maps
n_regions = 9
length = 21
fmri11_img, _ = generate_random_img(shape1, affine=affine1, length=length)
_, mask22_img = testing.generate_fake_fmri(shape2, length=1,
affine=affine2)
# Target: maps
maps33_img, _ = \
testing.generate_maps(shape3, n_regions, affine=affine1)
masker = NiftiMapsMasker(maps33_img, mask_img=mask22_img,
resampling_target="maps")
masker.fit()
#.........这里部分代码省略.........
示例12: test_signal_extraction_with_maps_and_labels
def test_signal_extraction_with_maps_and_labels():
shape = (4, 5, 6)
n_regions = 7
length = 8
# Generate labels
labels = list(range(n_regions + 1)) # 0 is background
labels_img = generate_labeled_regions(shape, n_regions, labels=labels)
labels_data = labels_img.get_data()
# Convert to maps
maps_data = np.zeros(shape + (n_regions,))
for n, l in enumerate(labels):
if n == 0:
continue
maps_data[labels_data == l, n - 1] = 1
maps_img = nibabel.Nifti1Image(maps_data, labels_img.affine)
# Generate fake data
fmri_img, _ = generate_fake_fmri(shape=shape, length=length,
affine=labels_img.affine)
# Extract signals from maps and labels: results must be identical.
maps_signals, maps_labels = signal_extraction.img_to_signals_maps(
fmri_img, maps_img)
labels_signals, labels_labels = signal_extraction.img_to_signals_labels(
fmri_img, labels_img)
np.testing.assert_almost_equal(maps_signals, labels_signals)
# Same thing with a mask, containing only 3 regions.
mask_data = (labels_data == 1) + (labels_data == 2) + (labels_data == 5)
mask_img = nibabel.Nifti1Image(mask_data.astype(np.int8),
labels_img.affine)
labels_signals, labels_labels = signal_extraction.img_to_signals_labels(
fmri_img, labels_img, mask_img=mask_img)
maps_signals, maps_labels = signal_extraction.img_to_signals_maps(
fmri_img, maps_img, mask_img=mask_img)
np.testing.assert_almost_equal(maps_signals, labels_signals)
assert_true(maps_signals.shape[1] == n_regions)
assert_true(maps_labels == list(range(len(maps_labels))))
assert_true(labels_signals.shape == (length, n_regions))
assert_true(labels_labels == labels[1:])
# Inverse operation (mostly smoke test)
labels_img_r = signal_extraction.signals_to_img_labels(
labels_signals, labels_img, mask_img=mask_img)
assert_true(labels_img_r.shape == shape + (length,))
maps_img_r = signal_extraction.signals_to_img_maps(
maps_signals, maps_img, mask_img=mask_img)
assert_true(maps_img_r.shape == shape + (length,))
# Check that NaNs in regions inside mask are preserved
region1 = labels_data == 2
indices = [ind[:1] for ind in np.where(region1)]
fmri_img.get_data()[indices + [slice(None)]] = float('nan')
labels_signals, labels_labels = signal_extraction.img_to_signals_labels(
fmri_img, labels_img, mask_img=mask_img)
assert_true(np.all(np.isnan(labels_signals[:, labels_labels.index(2)])))
示例13: test_largest_cc_img
def test_largest_cc_img():
""" Check the extraction of the largest connected component, for niftis
Similiar to smooth_img tests for largest connected_component_img, here also
only the added features for largest_connected_component are tested.
"""
# Test whether dimension of 3Dimg and list of 3Dimgs are kept.
shapes = ((10, 11, 12), (13, 14, 15))
regions = [1, 3]
img1 = testing.generate_labeled_regions(shape=shapes[0],
n_regions=regions[0])
img2 = testing.generate_labeled_regions(shape=shapes[1],
n_regions=regions[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 = largest_connected_component_img(imgs)
assert_true(isinstance(out, list))
assert_true(len(out) == 2)
for o, s in zip(out, shapes):
assert_true(o.shape == (s))
# Single image as input
out = largest_connected_component_img(imgs[0])
assert_true(isinstance(out, Nifti1Image))
assert_true(out.shape == (shapes[0]))
# Test whether 4D Nifti throws the right error.
img_4D = testing.generate_fake_fmri(shapes[0], length=17)
assert_raises(DimensionError, largest_connected_component_img, img_4D)
# tests adapted to non-native endian data dtype
img1_change_dtype = nibabel.Nifti1Image(img1.get_data().astype('>f8'),
affine=img1.affine)
img2_change_dtype = nibabel.Nifti1Image(img2.get_data().astype('>f8'),
affine=img2.affine)
for create_files in (False, True):
with testing.write_tmp_imgs(img1_change_dtype, img2_change_dtype,
create_files=create_files) as imgs:
# List of images as input
out = largest_connected_component_img(imgs)
assert_true(isinstance(out, list))
assert_true(len(out) == 2)
for o, s in zip(out, shapes):
assert_true(o.shape == (s))
# Single image as input
out = largest_connected_component_img(imgs[0])
assert_true(isinstance(out, Nifti1Image))
assert_true(out.shape == (shapes[0]))
# Test the output with native and without native
out_native = largest_connected_component_img(img1)
out_non_native = largest_connected_component_img(img1_change_dtype)
np.testing.assert_equal(out_native.get_data(), out_non_native.get_data())