<jupyter_start><jupyter_text>Performance Benchmarking vs DeconvolutionLab2The purpose of this notebook is to examine performance differences between Flowdec and DeconvolutionLab2 on arbitrary test data.At TOW, this was executed on a system with these specs:- Windows 10 Pro- 64G RAM- Intel Xeon CPU (x2)- Nvidia GTX 1080 GPU (x2)<jupyter_code>import tempfile, os, warnings, timeit
import pandas as pd
import numpy as np
from skimage import io
from flowdec import data as fd_data
from flowdec import psf as fd_psf
from flowdec import restoration as fd_restoration

DL2_PATH = os.path.join(os.path.expanduser('~'), 
    'repos', 'misc', 'DeconvolutionLab2', 'target', 'DeconcolutionLab2_-0.1.0-SNAPSHOT.jar')
N_ITER = 25 # Number of RL iterations
# Generate a PSF and some random data to test executions times with
kernel = fd_psf.GibsonLanni(size_x=1024, size_y=1024, size_z=11).generate().astype(np.float32)
np.random.seed(1)
data = np.random.poisson(lam=100, size=kernel.shape).astype(kernel.dtype)
data.shape, kernel.shape, data.dtype, kernel.dtype
# Generate temporary files to store image data within
data_file, kernel_file = tempfile.mktemp('.tif', 'data-'), tempfile.mktemp('.tif', 'kernel-')

# Ignore low-contrast image warnings from skimage
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    io.imsave(data_file, data)
    io.imsave(kernel_file, kernel)
    
os.path.basename(data_file), os.path.basename(kernel_file)<jupyter_output><empty_output><jupyter_text>Benchmarking Functions<jupyter_code># Deconvolution Lab 2 Functions

# Setup system call to execute DL2 CLI
dl2_cmd = "java -cp {jar} DeconvolutionLab2 Run -image file {data} -psf file {psf} -algorithm RL {niter}"\
    .format(jar=DL2_PATH, data=data_file, psf=kernel_file, niter=N_ITER)
     
def run_dl2():
    !$dl2_cmd
# Flowdec Functions

algo = fd_restoration.RichardsonLucyDeconvolver(data.ndim).initialize()
acq = fd_data.Acquisition(data=data, kernel=kernel)

def run_flowdec_best_case():
    """ Use pre-defined TF graph and memory-resident data """
    algo.run(acq, niter=N_ITER)
    
def run_flowdec_worst_case():
    """ Redefine TF graph and load data from files """
    fd_restoration.RichardsonLucyDeconvolver(data.ndim).initialize().run(
        fd_data.Acquisition(data=io.imread(data_file), kernel=io.imread(kernel_file)),
        niter=N_ITER
    )
def benchmark(fn, n):
    times = np.array(timeit.repeat(fn, repeat=n, number=1)) * 1000
    return 'mean ± std  : {:.2f} ms ± {:.2f} ms\n'\
            'min - max   : {:.2f} ms - {:.2f} ms\n'\
            'n iterations: {}'\
        .format(times.mean(), times.std(), times.min(), times.max(), n)<jupyter_output><empty_output><jupyter_text>Benchmark Results<jupyter_code>print(benchmark(run_flowdec_best_case, 10))
print(benchmark(run_flowdec_worst_case, 10))
summary = benchmark(run_dl2, 10)
print(summary)<jupyter_output>mean ± std  : 40263.56 ms ± 820.71 ms
min - max   : 39318.80 ms - 41679.61 ms
n iterations: 10
{'fraction_non_alphanumeric': 0.12118160456529037, 'fraction_numerical': 0.02920443101711984, 'mean_word_length': 4.8203125, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '224294', 'license': 'Apache-2.0', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1032, 'n_words': 265, 'path': 'python/examples/notebooks/DeconvolutionLab2 - Benchmarking.ipynb', 'repo_name': 'eric-czech/flowdec', 'size': '2979'}