# import the necessary modules
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# for pretty plots
import seaborn as sns
sns.set()
# for reading in filenames
import glob
# for importing baterical images
import skimage.io
Let's look a sample image
# read in an example fluor image
im = skimage.io.imread("./data/ecoli_growth/ecoli_TRITC_05.tif")
# take a look at it
im
# show the image
plt.imshow(im)
plt.grid(False)
# make the image more interactive
%matplotlib notebook
plt.imshow(im)
plt.grid(False)
%matplotlib inline
# generate a histogram of pixel intensities
counts, bins = skimage.exposure.histogram(im)
# plot the histogram
plt.bar(bins, counts)
plt.yscale("log")
# label axes
plt.xlabel("pixel intensity")
plt.ylabel("number of pixels")
Threshold the image
# set the threshold value
threshold = 209
# apply this threshold and show the segmented image
im_thresh = im > threshold
plt.imshow(im_thresh)
plt.grid(False)
segment all the images
# extract the names of all the phase images
im_names = np.sort(glob.glob('./data/ecoli_growth/ecoli_TRITC_*.tif'))
im_names
# determine the number of frames
n_frames = len(im_names)
# initialize an array for storing bacterial areas
area_tot = np.zeros(n_frames)
# loop through all the images
for n in range(n_frames):
# read in the frame
im = skimage.io.imread(im_names[n])
# threshold the image
im_thresh = im > 209
# save the area in our array
area_tot[n] = im_thresh.sum()
area_tot
Plot the area over time and compare to various growth rates
# specify time resolution of imaging
dt = 5 # mins
# get x-axis for time values
times = dt * np.arange(n_frames)
# plot
plt.plot(times,area_tot, '.')
# plot exponential curves on top
N_0 = area_tot[0]
plt.plot(times,N_0*np.exp(0.025*times))
plt.plot(times,N_0*np.exp(0.03*times))
plt.plot(times,N_0*np.exp(0.035*times))
plt.legend(["data", "k=0.025", "k=0.03", "k=0.035"])
try different values of k and see what the corresponding errors are
# the number of differnet k's we want to try
n_points = 1000
# make the range of k's to test
ks = np.linspace(0.01,0.05, n_points)
# array of errors for the differnt values of k
errors = np.zeros(n_points)
# loop through each value of k, compute error
for i in range(n_points):
# compute theory curve for given k
theory_data = N_0*np.exp(ks[i]*times)
# compute the error
error = np.sum((theory_data - area_tot)**2)
# update error array
errors[i] = error
plot these errors over values of k
plt.plot(ks,errors)
plt.yscale("log")
find the best value of k
k_optimal = np.where(errors == np.min(errors))
k_fit = ks[k_optimal]
print(k_fit)
k_optimal