Don’t repeat yourself#
Sometimes, we copy & paste code multiple times to process images quickly. Long-term this code duplication is not good for code quality, because if we want to change something, we need to change it in muliple places and may forget about some. Thus, preventing repetitive code is key. The software design priniciple is called Don’t repeat yourself.
import pyclesperanto_prototype as cle
image = cle.imread("../../data/BBBC007_batch/17P1_POS0013_D_1UL.tif")
labels = cle.voronoi_otsu_labeling(image, spot_sigma=3)
number_of_nuclei = labels.max()
number_of_nuclei
44.0
image = cle.imread("../../data/BBBC007_batch/20P1_POS0005_D_1UL.tif")
labels = cle.voronoi_otsu_labeling(image, spot_sigma=3)
number_of_nuclei = labels.max()
number_of_nuclei
41.0
image = cle.imread("../../data/BBBC007_batch/20P1_POS0007_D_1UL.tif")
labels = cle.voronoi_otsu_labeling(image, spot_sigma=3)
number_of_nuclei = labels.max()
number_of_nuclei
73.0
If we wanted to now see how changing the spot_sigma
parameter above influences the result, we would need to change this value three times. When the code becomes even longer, it may happend that we forget to change it in one place.
For-loops instead of code repetition#
One way of preventing code-repetition are for-loops.
folder = "../../data/BBBC007_batch/"
files = ["17P1_POS0013_D_1UL.tif",
"20P1_POS0005_D_1UL.tif",
"20P1_POS0007_D_1UL.tif"]
for file in files:
image = cle.imread(folder + file)
labels = cle.voronoi_otsu_labeling(
image,
spot_sigma=3)
number_of_nuclei = labels.max()
print(file, number_of_nuclei)
17P1_POS0013_D_1UL.tif 44.0
20P1_POS0005_D_1UL.tif 41.0
20P1_POS0007_D_1UL.tif 73.0
Functions instead of code-repetion#
We can gain even more flexibility by introducing so called helper functions, that help us by doing one dedicated thing, for example counting nuclei:
def count_nuclei(image, spot_sigma=3):
labels = cle.voronoi_otsu_labeling(
image,
spot_sigma=spot_sigma)
number_of_nuclei = labels.max()
return number_of_nuclei
count_nuclei(cle.imread(folder + files[0]))
44.0
count_nuclei(cle.imread(folder + files[1]))
41.0
count_nuclei(cle.imread(folder + files[2]))
73.0
count_nuclei(cle.imread(folder + files[2]), spot_sigma=5)
68.0