Keep it short and simple#

Python has amazing functionalities which allow us to write 21st century code. For example, we can collect functions and parameters in lists and the call them from a loop. While this is super cool from a programming point of view, it might not be easy to read code. The recommendation is: Keep it short and simple (KISS).

import numpy as np
from skimage.io import imread, imshow
from napari_segment_blobs_and_things_with_membranes import threshold_otsu, gaussian_blur, label

Exercise#

As an example, there are two code sections below. Without running the code - do you think both do the same?## Exercise: Without running the code - do you think both do the same?

image = imread("../../data/blobs.tif")

# define a list of functions and a corresponding list of arguments
functions = [gaussian_blur, threshold_otsu, label]
argument_lists = [[.5], [], []]

# go through functions and argument lists pair-wise
for function, argument_list in zip(functions, argument_lists):
    # execute function with given arguments
    image = function(image, *argument_list)

result1 = image
imshow(result1)
image = imread("../../data/blobs.tif")

blurred = gaussian_blur(image, 5)
binary = threshold_otsu(blurred)
labels = label(binary)

result2 = labels

imshow(result2)

If you are not sure, you can also directly check if the results are equal:

np.all(result1 == result2)