<jupyter_start><jupyter_text>Unit Test GenerationExample deconvolutions of very basic 2 and 3 dimensional shapes, used largely for the purposes of building and debugging unit tests.<jupyter_code>%matplotlib inline
from scipy.stats import describe
from flowdec.nb import utils as nbutils
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Simple 2D Shape Test<jupyter_code>import numpy as np
import tensorflow as tf
from flowdec import tf_ops
from flowdec import data as fd_data
from flowdec import restoration as fd_restoration
from flowdec import validation as fd_validation
from flowdec import exec as tfd_exec
from skimage import restoration as sk_restoration
from skimage.exposure import rescale_intensity

x = np.zeros((3, 3))
x[1, 1] = 1
k = np.zeros((3, 3))
k[1, 1] = 1

# x = np.zeros((9, 9))
# x[3:6, 3:6] = 1
# k = np.zeros((9, 9))
# k[4, 4] = 1

# x = np.zeros((7, 7))
# x[2:5, 2:5] = 1
# k = np.zeros((7, 7))
# k[3, 3] = 1

# x = np.zeros((9, 9))
# x[3:6, 3:6] = 1
# k = np.zeros((7, 7))
# k[2:5, 2:5] = 1

# x = np.zeros((8, 8))
# x[3:5, 3:5] = 1
# k = np.zeros((7, 7))
# k[2:5, 2:5] = 1

# x = np.zeros((9, 9))
# x[3, 4] = 1
# x[4, 3:6] = 1
# x[5, 4] = 1
# k = np.zeros((9, 9))
# k[4, 4] = 1


acq = fd_data.Acquisition(data=x.astype(np.float32), kernel=k.astype(np.float32), actual=x.astype(np.float32))
acq = fd_validation.reblur(acq, scale=0.01)
acq.data = rescale_intensity(acq.data, out_range=(0., 1.))

res = fd_restoration.richardson_lucy(
    acq, 10, pad_mode='log2', real_domain_fft=False, 
    session_config=tf.ConfigProto(device_count={'GPU': 0}))
#res = sk_restoration.richardson_lucy(acq.data, acq.kernel, iterations=10, clip=False)
#res = tfdecon_exec.run_dl2(acq, 10, 'none')

res_bin = (res > res.mean()).astype(np.int64) 


fig, axs = plt.subplots(1, 5)
fig.set_size_inches(16, 5)
axs[0].imshow(acq.data)
axs[1].imshow(acq.actual)
axs[2].imshow(acq.kernel)
axs[3].imshow(res)
axs[4].imshow(res_bin)<jupyter_output>Normalizing kernel<jupyter_text>Simple 3D Shape Test<jupyter_code>from nb import utils as nbutils
import imp
imp.reload(fd_restoration)

x = np.zeros((7, 7, 7))
x[2:5, 2:5, 2:5] = 1
k = np.zeros((5, 5, 5))
k[2, 2, 2] = 1

acq = fd_data.Acquisition(data=x.astype(np.float32), kernel=k.astype(np.float32), actual=x.astype(np.float32))
acq = fd_validation.reblur(acq, scale=0.001)
acq.data = rescale_intensity(acq.data, out_range=(0., 1.))

res = fd_restoration.richardson_lucy(
    acq, 10, pad_mode='log2', real_domain_fft=False, 
    session_config=tf.ConfigProto(device_count={'GPU': 0}))
#res = sk_restoration.richardson_lucy(acq.data, acq.kernel, 10)
#res = tfdecon_exec.run_dl2(acq, 10, 'none')

res_bin = (res > res.mean()).astype(np.int64) 
nbutils.plot_img_preview(acq.data)
nbutils.plot_img_preview(res)<jupyter_output><empty_output><jupyter_text>Unit Test Case Debugging<jupyter_code># Load data for unit test case currently failing (to investigate)
import pickle
with open('/tmp/flowdec-ut-debug-data.pkl', 'rb') as fd:
    utd = pickle.load(fd)
utd.keys()
utd['data'].keys()
dacq = {
    'original': utd['acquisition'].actual, 
    'blurred': utd['acquisition'].data,
    'kernel': utd['acquisition'].kernel
}
dalg = {k: utd['data'][k] for k in utd['data'].keys()}
dall = {**dacq, **dalg}
utd['scores']
arrs = [(k, dall[k]) for k in dall]

fig, axs = plt.subplots(1, len(arrs))
fig.set_size_inches(16, 5)
for i in range(len(arrs)):
    axs[i].imshow(arrs[i][1].max(axis=0))
    axs[i].set_title(arrs[i][0])
from flowdec.validation import score, binarize
fig, axs = plt.subplots(1, len(arrs))
fig.set_size_inches(16, 5)
for i in range(len(arrs)):
    img = binarize(arrs[i][1]).max(axis=0)
    axs[i].imshow(img)
    axs[i].set_title(arrs[i][0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17465388711395102, 'fraction_numerical': 0.044994675186368474, 'mean_word_length': 5.261666666666667, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '183351', 'license': 'Apache-2.0', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1633, 'n_words': 346, 'path': 'python/examples/notebooks/Unit Test Generation.ipynb', 'repo_name': 'eric-czech/flowdec', 'size': '3756'}