Functions

Prerequisites

Before starting this lesson, you should be familiar with:

Learning Objectives

After completing this lesson, learners should be able to:
  • Wrap a piece of code into a function.

  • Understand the skeleton of a function.

Motivation

When a series of steps (i.e. algorithm) that has to be performed on an image or a set of images should be executed more than once, or when the script gets too long and repetitive, it is more efficient to wrap such series of steps into a “function”. This essentially means that you can reuse parts of code instead of rewriting it. A function is a block that has a specific name and can be called with inputs and can return values.

Concept map

graph TD IV("Input values") --> F("Function") F -->|generates| OV("Output values") F -->|contains| RC("Reusable code")



Figure


Components and working of a function within a script.



Activities

Add two numbers using a function

Learn how to create a simple function.

Do so by implementing a function that adds two numbers and returns the resulting sum.


Show activity for:  

skimage napari

# %%
# Write a function that adds two numbers

# %%
# Define a function to add numbers
def add_two_number(number1, number2):
    total_sum = number1 + number2
    return total_sum

# %%
# Call the function to add numbers and display result
total_sum = add_two_number(10, 5)
print("Result", total_sum)



Open an image using a function

Implement the following steps:

And then wrap these steps into a function that can be called for multiple images (i.e., image paths).


Show activity for:  

skimage napari

# %%
# This script has two parts: 
# - The first part opens two images and inspects their data type and values sequentially
# - The second part shows how to implement a function to do this task

# %%
# Import libraries and init napari
import napari
import numpy as np
import matplotlib.pyplot as plt
from OpenIJTIFF import open_ij_tiff

viewer = napari.Viewer()

# %%
# Part 1: write a script to open and inspect images

# %%
# Open one image and view it
image, *_ = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_a.tif')
viewer.add_image(image)

# %%
# Check the image's datatype
print(image.dtype)

# %%
# Check minimum and maximum pixel values
print("Min:", image.min()) # check minimum pixel value
print("Max:", image.max()) # check maximum pixel value

# %%
# Open second image and view it
image, *_ = open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_cells.tif')
viewer.add_image(image)

# %%
# Check the image's datatype
print(image.dtype)

# %%
# Check minimum and maximum pixel values
print("Min:", image.min()) # check minimum pixel value
print("Max:", image.max()) # check maximum pixel value

# %%
# Part 2: Write a function to perform above tasks
# NOTE: Please manually close any opened Napari Viewer Windows
viewer = napari.Viewer()

# %%
# Define a function to open and inspect an image
def open_image(image_path):
    image, *_ = open_ij_tiff(image_path)
    print(image.dtype)
    print("Min:", image.min())
    print("Max:", image.max())
    return image

image1 = open_image('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_intensity_clipping_issue_a.tif')
image2 = open_image('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_cells.tif')

# %%
# Display images
viewer.add_image(image1)
viewer.add_image(image2)

# %%
# Learning opportunity: 
# Modify the function such that it also shows the image in napari






Assessment

True or False

Solution

  • False. It cannot be used unless the variable is returned by this function.
  • True. In fact, calling the function multiple times is sort of the point of writing the function.

Explanations




Follow-up material

Recommended follow-up modules:

Learn more: