def test__smooth_array():
    """Test smoothing of images: _smooth_array()"""
    # Impulse in 3D
    data = np.zeros((40, 41, 42))
    data[20, 20, 20] = 1

    # fwhm divided by any test affine must be odd. Otherwise assertion below
    # will fail. ( 9 / 0.6 = 15 is fine)
    fwhm = 9
    test_affines = (np.eye(4), np.diag((1, 1, -1, 1)), np.diag((.6, 1, .6, 1)))
    for affine in test_affines:
        filtered = image._smooth_array(data, affine, fwhm=fwhm, copy=True)
        assert_false(np.may_share_memory(filtered, data))

        # We are expecting a full-width at half maximum of
        # fwhm / voxel_size:
        vmax = filtered.max()
        above_half_max = filtered > .5 * vmax
        for axis in (0, 1, 2):
            proj = np.any(np.any(np.rollaxis(above_half_max, axis=axis),
                                 axis=-1),
                          axis=-1)
            np.testing.assert_equal(proj.sum(),
                                    fwhm / np.abs(affine[axis, axis]))

    # Check that NaNs in the data do not propagate
    data[10, 10, 10] = np.NaN
    filtered = image._smooth_array(data,
                                   affine,
                                   fwhm=fwhm,
                                   ensure_finite=True,
                                   copy=True)
    assert_true(np.all(np.isfinite(filtered)))

    # Check copy=False.
    for affine in test_affines:
        data = np.zeros((40, 41, 42))
        data[20, 20, 20] = 1
        image._smooth_array(data, affine, fwhm=fwhm, copy=False)

        # We are expecting a full-width at half maximum of
        # fwhm / voxel_size:
        vmax = data.max()
        above_half_max = data > .5 * vmax
        for axis in (0, 1, 2):
            proj = np.any(np.any(np.rollaxis(above_half_max, axis=axis),
                                 axis=-1),
                          axis=-1)
            np.testing.assert_equal(proj.sum(),
                                    fwhm / np.abs(affine[axis, axis]))

    # Check fwhm='fast'
    for affine in test_affines:
        np.testing.assert_equal(image._smooth_array(data, affine, fwhm='fast'),
                                image._fast_smooth_array(data))
Beispiel #2
0
def test__smooth_array():
    """Test smoothing of images: _smooth_array()"""
    # Impulse in 3D
    data = np.zeros((40, 41, 42))
    data[20, 20, 20] = 1

    # fwhm divided by any test affine must be odd. Otherwise assertion below
    # will fail. ( 9 / 0.6 = 15 is fine)
    fwhm = 9
    test_affines = (np.eye(4), np.diag((1, 1, -1, 1)),
                    np.diag((.6, 1, .6, 1)))
    for affine in test_affines:
        filtered = image._smooth_array(data, affine,
                                         fwhm=fwhm, copy=True)
        assert_false(np.may_share_memory(filtered, data))

        # We are expecting a full-width at half maximum of
        # fwhm / voxel_size:
        vmax = filtered.max()
        above_half_max = filtered > .5 * vmax
        for axis in (0, 1, 2):
            proj = np.any(np.any(np.rollaxis(above_half_max,
                          axis=axis), axis=-1), axis=-1)
            np.testing.assert_equal(proj.sum(),
                                    fwhm / np.abs(affine[axis, axis]))

    # Check that NaNs in the data do not propagate
    data[10, 10, 10] = np.NaN
    filtered = image._smooth_array(data, affine, fwhm=fwhm,
                                   ensure_finite=True, copy=True)
    assert_true(np.all(np.isfinite(filtered)))

    # Check copy=False.
    for affine in test_affines:
        data = np.zeros((40, 41, 42))
        data[20, 20, 20] = 1
        image._smooth_array(data, affine, fwhm=fwhm, copy=False)

        # We are expecting a full-width at half maximum of
        # fwhm / voxel_size:
        vmax = data.max()
        above_half_max = data > .5 * vmax
        for axis in (0, 1, 2):
            proj = np.any(np.any(np.rollaxis(above_half_max,
                          axis=axis), axis=-1), axis=-1)
            np.testing.assert_equal(proj.sum(),
                                    fwhm / np.abs(affine[axis, axis]))

    # Check fwhm='fast'
    for affine in test_affines:
        np.testing.assert_equal(image._smooth_array(data, affine, fwhm='fast'),
                                image._fast_smooth_array(data))
Beispiel #3
0
def plot_contour_atlas(niimgs, labels, cut_coords=None,
                     title=None, percentile=99):
    legend_lines = []
    slicer = plot_bg(cut_coords, title)
    atlas = np.vstack([niimg.get_data()[np.newaxis] for niimg in niimgs])
    # atlas = StandardScaler().fit_transform(atlas.T).T
    affine = niimgs[0].get_affine()
    for i, (label, data) in enumerate(zip(labels, atlas)):
        data = np.array(_smooth_array(data, affine, 5), copy=True)
        data[data < 0] = 0
        color = np.array(pl.cm.Set1(float(i) / (len(labels) - 1)))
        # data, affine = niimg.get_data(), niimg.get_affine()
        # affine = niimg.get_affine()
        level = scoreatpercentile(data.ravel(), percentile)
        slicer.contour_map(data, affine, levels=(level, ),
                           linewidth=2.5, colors=(color, ))
        slicer.plot_map(data, affine, threshold=level,
                        cmap=alpha_cmap(color))
        legend_lines.append(pl.Line2D([0, 0], [0, 0],
                            color=color, linewidth=4))

    ax = slicer.axes['z'].ax.get_figure().add_axes([.80, .1, .15, .8])
    pl.axis('off')
    ax.legend(legend_lines, labels, loc='center right',
              prop=dict(size=4), title='Labels',
              borderaxespad=0,
              bbox_to_anchor=(1 / .85, .5))
    atlas = np.rollaxis(atlas, 0, 4)
    return nb.Nifti1Image(atlas, affine=affine)