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)
def test_data_to_sprite(): # Simulate data and turn into sprite data = np.zeros([8, 8, 8]) data[2:6, 2:6, 2:6] = 1 sprite = html_stat_map._data_to_sprite(data) # Generate ground truth for the sprite Z = np.zeros([8, 8]) Zr = np.zeros([2, 8]) Cr = np.tile(np.array([[0, 0, 1, 1, 1, 1, 0, 0]]), [4, 1]) C = np.concatenate((Zr, Cr, Zr), axis=0) gtruth = np.concatenate((np.concatenate( (Z, Z, C), axis=1), np.concatenate( (C, C, C), axis=1), np.concatenate((Z, Z, Z), axis=1)), axis=0) assert sprite.shape == gtruth.shape, "shape of sprite not as expected" assert (sprite == gtruth).all(), "simulated sprite not as expected"
def test_data_to_sprite(): # Simulate data and turn into sprite data = np.zeros([8, 8, 8]) data[2:6, 2:6, 2:6] = 1 sprite = html_stat_map._data_to_sprite(data) # Generate ground truth for the sprite Z = np.zeros([8, 8]) Zr = np.zeros([2, 8]) Cr = np.tile(np.array([[0, 0, 1, 1, 1, 1, 0, 0]]), [4, 1]) C = np.concatenate((Zr, Cr, Zr), axis=0) gtruth = np.concatenate((np.concatenate((Z, Z, C), axis=1), np.concatenate((C, C, C), axis=1), np.concatenate((Z, Z, Z), axis=1)), axis=0) assert sprite.shape == gtruth.shape, "shape of sprite not as expected" assert (sprite == gtruth).all(), "simulated sprite not as expected"