ECC from bitmap images๏ƒ

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from PIL import Image
import pyEulerCurves as pyecc

Load one image๏ƒ

image_plank = Image.open("data/Planck.png")
plank_np = np.uint8(image_plank)
print(plank_np.shape)
image_plank
(500, 1000)
../_images/31d25b7924134bc9127c09200447728e6bb4877b162c2ee9aa6778f609dd56b8.png
plank_np.max()
171
trans = pyecc.ECC_from_bitmap(periodic_boundary=False, workers=2)  # number of CPU cores
%%time
ecc = trans.fit_transform(image_plank)
CPU times: user 86.5 ms, sys: 34.2 ms, total: 121 ms
Wall time: 854 ms
fig, axs = plt.subplots(1)

im0 = pyecc.plot_euler_curve(ecc, axs, with_lines=True)
axs.set_title("Planck")
plt.show()
../_images/a3d75ef597115d1b3097718d2f64ef2ed9f2e6dd5da6b5027e8a3b18b7196f79.png
# load the ECC computed by CHUNKYeuler and check if they are equal
# https://bitbucket.org/hubwag/chunkyeuler/src/master/

ch_ECC = pd.read_csv(
    "data/Planck_t_u8_500x1000.raw.euler", sep=" ", header=None
).values.astype(float)

assert pyecc.difference_ECC(ecc, ch_ECC, 1000) == 0

Lets stack togheter 50 random frames๏ƒ

# # uncomment to generate a new image
# np.random.seed(42)

# how_many_layers = 50
# sidex = 100
# sidey = 200

# image_np = np.random.randint(0, 256, size=(how_many_layers, sidey, sidex))

# image_np.shape

# with open('data/50x200x100.npy', 'wb') as f:
#     np.save(f, image_np)
# load the 3d array
with open("data/50x200x100.npy", "rb") as f:
    image_np = np.load(f)

image_np.shape
(50, 200, 100)
# this is the first frame
im = Image.fromarray(np.uint8(image_np[0]))
im
../_images/32156f04972e4ca1293e405bdd9af9527c8edf111dff1365114285f0eaa24957.png
# # save images to disk
# for i in (range(len(image_np))):
#     current_img = Image.fromarray(np.uint8(image_np[i]))
#     current_img.save('to_convert/{:05d}.png'.format(i))
%%time
trans = pyecc.ECC_from_bitmap(periodic_boundary=False, 
                              workers=2    # number of CPU cores
                              )
ecc = trans.fit_transform(image_np)
CPU times: user 21.3 ms, sys: 20.9 ms, total: 42.2 ms
Wall time: 1.39 s
fig, axs = plt.subplots(1)

im0 = pyecc.plot_euler_curve(ecc, axs, with_lines=True)
axs.set_title("{}".format(image_np.shape))
plt.show()
../_images/c965a3ad085311b1a2b63869eb6088971e109f4b8b4788a88e04d502203f723d.png
# load the ECC computed by CHUNKYeuler and check if they are equal
# https://bitbucket.org/hubwag/chunkyeuler/src/master/

ch_ECC = pd.read_csv(
    "data/00000.png_t_u8_50x200x100.from_stack.raw.euler", sep=" ", header=None
).values.astype(float)

assert pyecc.difference_ECC(ch_ECC, ecc, 1000) == 0

Periodic boundary conditions๏ƒ

# load the 3d array
with open("data/50x200x100.npy", "rb") as f:
    image_np = np.load(f)

image_np.shape
(50, 200, 100)
%%time
# periodic boundary only on the x axis
boundary = [False, False, True]

trans = pyecc.ECC_from_bitmap(periodic_boundary=boundary, 
                              workers=2    # number of CPU cores
                              )
ecc_1T = trans.fit_transform(image_np)
CPU times: user 18 ms, sys: 20.2 ms, total: 38.2 ms
Wall time: 1.5 s
fig, axs = plt.subplots(1)

im0 = pyecc.plot_euler_curve(ecc_1T, axs, with_lines=True)
axs.set_title("{}\n{}".format(image_np.shape, boundary))
plt.show()
../_images/9ab40db28ab9c09438593388720b1367ff39add9eba587fae26f24ec03fd086b.png
%%time
# periodic boundary everywhere
boundary = [True, True, True]

trans = pyecc.ECC_from_bitmap(periodic_boundary=boundary, 
                              workers=2    # number of CPU cores
                              )
ecc_3T = trans.fit_transform(image_np)
CPU times: user 16.7 ms, sys: 20.5 ms, total: 37.1 ms
Wall time: 1.33 s
fig, axs = plt.subplots(1)

im0 = pyecc.plot_euler_curve(ecc_3T, axs, with_lines=True)
axs.set_title("{}\n{}".format(image_np.shape, boundary))
plt.show()
../_images/4500a3b98d1ed3a2caad7ff3aadc6ca77995eb266e828406f2429b6f4cc462c7.png
# we expect the two curves to be different
assert pyecc.difference_ECC(ecc_1T, ecc_3T, 1000) != 0
print(pyecc.difference_ECC(ecc_1T, ecc_3T, 1000))
16758417.0