コード例 #1
0
ファイル: test_html_stat_map.py プロジェクト: zhiye9/nilearn
def test_save_sprite():
    """This test covers _save_sprite as well as _bytesIO_to_base64
    """

    # Generate a simulated volume with a square inside
    data = np.random.RandomState(0).uniform(size=140).reshape(7, 5, 4)
    mask = np.zeros((7, 5, 4), dtype=int)
    mask[1:-1, 1:-1, 1:-1] = 1
    # Save the sprite using BytesIO
    sprite_io = BytesIO()
    html_stat_map._save_sprite(data,
                               sprite_io,
                               vmin=0,
                               vmax=1,
                               mask=mask,
                               format='png')

    # Load the sprite back in base64
    sprite_base64 = html_stat_map._bytesIO_to_base64(sprite_io)

    decoded_io = BytesIO()
    decoded_io.write(base64.b64decode(sprite_base64))
    decoded_io.seek(0)
    img = plt.imread(decoded_io, format="png")
    correct_img = np.ma.array(html_stat_map._data_to_sprite(data),
                              mask=html_stat_map._data_to_sprite(mask))
    correct_img = plt.Normalize(0, 1)(correct_img)
    cmapped = plt.get_cmap("Greys")(correct_img)
    assert np.allclose(img, cmapped, atol=.1)
コード例 #2
0
def test_save_cmap():
    """This test covers _save_cmap as well as _bytesIO_to_base64
    """

    # Save the cmap using BytesIO
    cmap_io = BytesIO()
    html_stat_map._save_cm(cmap_io, 'cold_hot', format='raw', n_colors=2)

    # Load the colormap back in base64
    cmap_base64 = html_stat_map._bytesIO_to_base64(cmap_io)

    # Check the colormap is correct
    assert cmap_base64 == '//////////8=\n'
コード例 #3
0
def test_save_cmap():
    """This test covers _save_cmap as well as _bytesIO_to_base64
    """

    # Save the cmap using BytesIO
    cmap_io = BytesIO()
    html_stat_map._save_cm(cmap_io, 'cold_hot', format='raw', n_colors=2)

    # Load the colormap back in base64
    cmap_base64 = html_stat_map._bytesIO_to_base64(cmap_io)

    # Check the colormap is correct
    assert cmap_base64 == '//////////8=\n'
コード例 #4
0
def test_save_cmap():
    """This test covers _save_cmap as well as _bytesIO_to_base64
    """

    # Save the cmap using BytesIO
    cmap_io = BytesIO()
    html_stat_map._save_cm(cmap_io, 'cold_hot', format='png', n_colors=2)

    # Load the colormap back in base64
    cmap_base64 = html_stat_map._bytesIO_to_base64(cmap_io)

    # Check the colormap is correct
    assert cmap_base64.startswith('iVBORw0KG')
    assert cmap_base64.endswith('ElFTkSuQmCC')
コード例 #5
0
ファイル: test_html_stat_map.py プロジェクト: zhiye9/nilearn
def test_save_cmap(cmap, n_colors):
    """This test covers _save_cmap as well as _bytesIO_to_base64
    """
    # Save the cmap using BytesIO
    cmap_io = BytesIO()
    html_stat_map._save_cm(cmap_io, cmap, format='png', n_colors=n_colors)

    # Load the colormap back in base64
    cmap_base64 = html_stat_map._bytesIO_to_base64(cmap_io)

    decoded_io = BytesIO()
    decoded_io.write(base64.b64decode(cmap_base64))
    decoded_io.seek(0)
    img = plt.imread(decoded_io, format="png")
    expected = plt.get_cmap(cmap)(np.linspace(0, 1, n_colors))
    assert np.allclose(img, expected, atol=.1)
コード例 #6
0
def test_save_sprite():
    """This test covers _save_sprite as well as _bytesIO_to_base64
    """

    # Generate a simulated volume with a square inside
    data = np.zeros([2, 1, 1])
    data[0, 0, 0] = 1
    mask = data > 0

    # Save the sprite using BytesIO
    sprite_io = BytesIO()
    html_stat_map._save_sprite(data, sprite_io, vmin=0, vmax=1,
                               mask=mask, format='raw')

    # Load the sprite back in base64
    sprite_base64 = html_stat_map._bytesIO_to_base64(sprite_io)

    # Check the sprite is correct
    assert sprite_base64 == '////AP////8=\n'
コード例 #7
0
def test_save_sprite():
    """This test covers _save_sprite as well as _bytesIO_to_base64
    """

    # Generate a simulated volume with a square inside
    data = np.zeros([2, 1, 1])
    data[0, 0, 0] = 1
    mask = data > 0

    # Save the sprite using BytesIO
    sprite_io = BytesIO()
    html_stat_map._save_sprite(data,
                               sprite_io,
                               vmin=0,
                               vmax=1,
                               mask=mask,
                               format='raw')

    # Load the sprite back in base64
    sprite_base64 = html_stat_map._bytesIO_to_base64(sprite_io)

    # Check the sprite is correct
    assert sprite_base64 == '////AP////8=\n'