Пример #1
0
def test_parcellations_transform_single_nifti_image():
    # Test with NiftiLabelsMasker extraction of timeseries data
    # after building a parcellations image

    # Here, data has ones. zeros will be considered as background labels
    # not foreground labels
    data = np.ones((10, 11, 12, 8))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3
    parcels = 5

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))

    for method in ['kmeans', 'ward', 'complete', 'average']:
        parcellator = Parcellations(method=method, n_parcels=parcels)
        parcellator.fit(fmri_img)
        # transform to signals
        signals = parcellator.transform(fmri_img)
        # Test if the signals extracted are of same shape as inputs
        # Here, we simply return numpy array for single subject input
        assert_equal(signals.shape, (fmri_img.shape[3], parcels))

        # Test for single subject but in a list.
        signals = parcellator.transform([fmri_img])
        assert_equal(signals.shape, (fmri_img.shape[3], parcels))
Пример #2
0
def test_parcellations_transform_single_nifti_image():
    # Test with NiftiLabelsMasker extraction of timeseries data
    # after building a parcellations image

    # Here, data has ones. zeros will be considered as background labels
    # not foreground labels
    data = np.ones((10, 11, 12, 8))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3
    parcels = 5

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))

    for method in ['kmeans', 'ward', 'complete', 'average', 'rena']:
        parcellator = Parcellations(method=method, n_parcels=parcels)
        parcellator.fit(fmri_img)
        # transform to signals
        signals = parcellator.transform(fmri_img)
        # Test if the signals extracted are of same shape as inputs
        # Here, we simply return numpy array for single subject input
        assert signals.shape == (fmri_img.shape[3], parcels)

        # Test for single subject but in a list.
        signals = parcellator.transform([fmri_img])
        assert signals.shape == (fmri_img.shape[3], parcels)
Пример #3
0
def test_parcellations_transform_single_nifti_image(method, n_parcel,
                                                    test_image_2):
    """Test with NiftiLabelsMasker extraction of timeseries data
    after building a parcellations image."""
    parcellator = Parcellations(method=method, n_parcels=n_parcel)
    parcellator.fit(test_image_2)
    # transform to signals
    signals = parcellator.transform(test_image_2)
    # Test if the signals extracted are of same shape as inputs
    # Here, we simply return numpy array for single subject input
    assert signals.shape == (test_image_2.shape[3], n_parcel)
    # Test for single subject but in a list.
    signals = parcellator.transform([test_image_2])
    assert signals.shape == (test_image_2.shape[3], n_parcel)
Пример #4
0
def test_inverse_transform_single_nifti_image():
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    methods = ['kmeans', 'ward', 'complete', 'average', 'rena']

    for method in methods:
        parcellate = Parcellations(method=method, n_parcels=5)
        # Fit
        parcellate.fit(fmri_img)
        assert parcellate.labels_img_ is not None
        # Transform
        fmri_reduced = parcellate.transform(fmri_img)
        assert isinstance(fmri_reduced, np.ndarray)
        # Shape matching with (scans, regions)
        assert fmri_reduced.shape, (10, 5)
        # Inverse transform
        fmri_compressed = parcellate.inverse_transform(fmri_reduced)
        # A single Nifti image for single subject input
        assert isinstance(fmri_compressed, nibabel.Nifti1Image)
        # returns shape of fmri_img
        assert fmri_compressed.shape, (10, 11, 12, 10)

        # fmri_reduced in a list
        fmri_compressed = parcellate.inverse_transform([fmri_reduced])
        # A single Nifti image for single subject input
        assert isinstance(fmri_compressed, nibabel.Nifti1Image)
        # returns shape of fmri_img
        assert fmri_compressed.shape, (10, 11, 12, 10)
Пример #5
0
def test_inverse_transform_single_nifti_image():
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    methods = ['kmeans', 'ward', 'complete', 'average']

    for method in methods:
        parcellate = Parcellations(method=method, n_parcels=5)
        # Fit
        parcellate.fit(fmri_img)
        assert_true(parcellate.labels_img_ is not None)
        # Transform
        fmri_reduced = parcellate.transform(fmri_img)
        assert_true(isinstance(fmri_reduced, np.ndarray))
        # Shape matching with (scans, regions)
        assert_true(fmri_reduced.shape, (10, 5))
        # Inverse transform
        fmri_compressed = parcellate.inverse_transform(fmri_reduced)
        # A single Nifti image for single subject input
        assert_true(isinstance(fmri_compressed, nibabel.Nifti1Image))
        # returns shape of fmri_img
        assert_true(fmri_compressed.shape, (10, 11, 12, 10))

        # fmri_reduced in a list
        fmri_compressed = parcellate.inverse_transform([fmri_reduced])
        # A single Nifti image for single subject input
        assert_true(isinstance(fmri_compressed, nibabel.Nifti1Image))
        # returns shape of fmri_img
        assert_true(fmri_compressed.shape, (10, 11, 12, 10))
Пример #6
0
def test_parcellations_transform_with_multi_confounds_multi_images(
        method, n_parcel, test_image_2):  # noqa: E501
    rng = np.random.RandomState(42)
    fmri_imgs = [test_image_2] * 3
    confounds = rng.standard_normal(size=(10, 3))
    confounds_list = [confounds] * 3
    parcellator = Parcellations(method=method, n_parcels=n_parcel)
    parcellator.fit(fmri_imgs)
    signals = parcellator.transform(fmri_imgs, confounds=confounds_list)
    assert isinstance(signals, list)
    # n_parcels=5, length of data=10
    assert signals[0].shape == (10, n_parcel)
Пример #7
0
def test_parcellations_transform_multi_nifti_images(method, n_parcel,
                                                    test_image_2):
    fmri_imgs = [test_image_2] * 3
    parcellator = Parcellations(method=method, n_parcels=n_parcel)
    parcellator.fit(fmri_imgs)
    # transform multi images to signals.
    # In return, we have length equal to the number of images
    signals = parcellator.transform(fmri_imgs)
    assert signals[0].shape == (test_image_2.shape[3], n_parcel)
    assert signals[1].shape == (test_image_2.shape[3], n_parcel)
    assert signals[2].shape == (test_image_2.shape[3], n_parcel)
    assert len(signals) == len(fmri_imgs)
Пример #8
0
def test_inverse_transform_single_nifti_image(method, n_parcel, test_image_2):
    parcellate = Parcellations(method=method, n_parcels=n_parcel)
    parcellate.fit(test_image_2)
    assert parcellate.labels_img_ is not None
    fmri_reduced = parcellate.transform(test_image_2)
    assert isinstance(fmri_reduced, np.ndarray)
    # Shape matching with (scans, regions)
    assert fmri_reduced.shape == (10, n_parcel)
    fmri_compressed = parcellate.inverse_transform(fmri_reduced)
    # A single Nifti image for single subject input
    assert isinstance(fmri_compressed, nibabel.Nifti1Image)
    # returns shape of fmri_img
    assert fmri_compressed.shape == test_image_2.shape
    # fmri_reduced in a list
    fmri_compressed = parcellate.inverse_transform([fmri_reduced])
    # A single Nifti image for single subject input
    assert isinstance(fmri_compressed, nibabel.Nifti1Image)
    # returns shape of fmri_img
    assert fmri_compressed.shape == test_image_2.shape
Пример #9
0
def test_parcellations_transform_with_multi_confounds_multi_images():
    rng = np.random.RandomState(42)
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    fmri_imgs = [fmri_img, fmri_img, fmri_img]

    confounds = rng.standard_normal(size=(10, 3))
    confounds_list = (confounds, confounds, confounds)

    for method in ['kmeans', 'ward', 'complete', 'average', 'rena']:
        parcellator = Parcellations(method=method, n_parcels=5)
        parcellator.fit(fmri_imgs)

        signals = parcellator.transform(fmri_imgs, confounds=confounds_list)
        assert isinstance(signals, list)
        # n_parcels=5, length of data=10
        assert signals[0].shape == (10, 5)
Пример #10
0
def test_parcellations_transform_multi_nifti_images():
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3
    parcels = 5

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    fmri_imgs = [fmri_img, fmri_img, fmri_img]

    for method in ['kmeans', 'ward', 'complete', 'average', 'rena']:
        parcellator = Parcellations(method=method, n_parcels=parcels)
        parcellator.fit(fmri_imgs)
        # transform multi images to signals. In return, we have length
        # equal to the number of images
        signals = parcellator.transform(fmri_imgs)
        assert signals[0].shape == (fmri_img.shape[3], parcels)
        assert signals[1].shape == (fmri_img.shape[3], parcels)
        assert signals[2].shape == (fmri_img.shape[3], parcels)

        assert len(signals) == len(fmri_imgs)
Пример #11
0
def test_parcellations_transform_multi_nifti_images():
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3
    parcels = 5

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    fmri_imgs = [fmri_img, fmri_img, fmri_img]

    for method in ['kmeans', 'ward', 'complete', 'average']:
        parcellator = Parcellations(method=method, n_parcels=parcels)
        parcellator.fit(fmri_imgs)
        # transform multi images to signals. In return, we have length
        # equal to the number of images
        signals = parcellator.transform(fmri_imgs)
        assert_equal(signals[0].shape, (fmri_img.shape[3], parcels))
        assert_equal(signals[1].shape, (fmri_img.shape[3], parcels))
        assert_equal(signals[2].shape, (fmri_img.shape[3], parcels))

        assert_equal(len(signals), len(fmri_imgs))
Пример #12
0
def test_parcellations_transform_with_multi_confounds_multi_images():
    rng = np.random.RandomState(0)
    data = np.ones((10, 11, 12, 10))
    data[6, 7, 8] = 2
    data[9, 10, 11] = 3

    fmri_img = nibabel.Nifti1Image(data, affine=np.eye(4))
    fmri_imgs = [fmri_img, fmri_img, fmri_img]

    confounds = rng.randn(*(10, 3))
    confounds_list = (confounds, confounds, confounds)

    for method in ['kmeans', 'ward', 'complete', 'average']:
        parcellator = Parcellations(method=method, n_parcels=5)
        parcellator.fit(fmri_imgs)

        signals = parcellator.transform(fmri_imgs,
                                        confounds=confounds_list)
        assert_true(isinstance(signals, list))
        # n_parcels=5, length of data=10
        assert_equal(signals[0].shape, (10, 5))