Created
October 22, 2014 15:01
-
-
Save CnrLwlss/362f1c2c194f8da1d99c to your computer and use it in GitHub Desktop.
A demo Colonyzer script: looking for cultures in a wider search space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from colonyzer2.functions import * | |
import time, sys, os, numpy, PIL | |
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") | |
print("Note that this script requires a Colonyzer.txt file (as generated by ColonyzerParametryzer) describing initial guess for culture array") | |
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") | |
# Lydall lab file naming convention | |
# First 15 characters in filename identify unique plates | |
# Remaining charaters can be used to store date, time etc. | |
barcRange=(0,15) | |
# Disabling lighting correction | |
correction=False | |
fixedThresh=0 | |
threshplots=False | |
start=time.time() | |
# Find image files which have yet to be analysed | |
barcdict=getBarcodes(os.getcwd(),barcRange) | |
# Setup output directories if not already present | |
rept=setupDirectories(barcdict) | |
if len(rept)>0: | |
print ("Newly created directories:") | |
for line in rept: | |
print rept | |
InsData=readInstructions(os.getcwd()) | |
while len(barcdict)>0: | |
BARCODE=barcdict.keys()[0] | |
print(BARCODE) | |
# Set this to zero to use true last image | |
# Note that image names already in reverse order | |
# Stepping back from final image avoids images with cracked agar | |
latestindex=int(round(0.1*len(barcdict[BARCODE]))) | |
LATESTIMAGE=barcdict[BARCODE][latestindex] | |
EARLIESTIMAGE=barcdict[BARCODE][-1] | |
print("Earliest: "+EARLIESTIMAGE) | |
print("Latest: "+LATESTIMAGE) | |
imRoot=EARLIESTIMAGE.split(".")[0] | |
# Generate pdf report about distributions | |
if threshplots: | |
pdf=PdfPages(BARCODE+'_HistogramReport.pdf') | |
# Indicate that barcode is currently being analysed, to allow parallel analysis | |
tmp=open(os.path.join(os.path.dirname(EARLIESTIMAGE),"Output_Data",os.path.basename(EARLIESTIMAGE).split(".")[0]+".out"),"w").close() | |
# Get latest image for thresholding and detecting culture locations | |
imN,arrN=openImage(LATESTIMAGE) | |
# Get earliest image for lighting gradient correction | |
im0,arr0=openImage(EARLIESTIMAGE) | |
# If we have ColonyzerParametryzer output for this filename, use it for initial culture location estimates | |
if os.path.basename(LATESTIMAGE) in InsData: | |
(candx,candy,dx,dy)=SetUp(InsData[os.path.basename(LATESTIMAGE)]) | |
# If there are multiple calibrations available, choose the best one based on date of image capture | |
elif any(isinstance(el, list) for el in InsData['default']): | |
imname=os.path.basename(LATESTIMAGE).split(".")[0] | |
imdate=imname[-19:-9] | |
(candx,candy,dx,dy)=SetUp(InsData['default'],imdate) | |
else: | |
(candx,candy,dx,dy)=SetUp(InsData['default']) | |
# Update guesses and initialise locations data frame | |
locationsN=locateCultures(candx,candy,dx,dy,arrN,search=0.5) | |
# Trim outer part of image to remove plate walls | |
trimmed_arr=arrN[max(0,min(locationsN.y)-dy):min(arr0.shape[0],(max(locationsN.y)+dy)),max(0,(min(locationsN.x)-dx)):min(arr0.shape[1],(max(locationsN.x)+dx))] | |
#showIm(trimmed_arr) | |
if fixedThresh!=0: | |
thresh=fixedThresh | |
else: | |
if threshplots: | |
(thresh,bindat)=automaticThreshold(trimmed_arr,BARCODE,pdf) | |
plotModel(bindat,label=BARCODE,pdf=pdf) | |
else: | |
(thresh,bindat)=automaticThreshold(trimmed_arr) | |
if threshplots: | |
pdf.close() | |
# Mask for identifying culture areas | |
maskN=numpy.ones(arrN.shape,dtype=numpy.bool) | |
maskN[arrN<thresh]=False | |
# Cut out pixels from EARLIESTIMAGE based on mask from LATESTIMAGE and fill in | |
pseudoempty=maskAndFill(arr0,maskN,5) | |
# Smooth (pseudo-)empty image | |
(correction_map,average_back)=makeCorrectionMap(pseudoempty,locationsN,printMess=correction) | |
for FILENAME in barcdict[BARCODE]: | |
im,arr=openImage(FILENAME) | |
if correction: | |
arr=arr*correction_map | |
# Correct for lighting differences between plates | |
arrsm=arr[numpy.min(locationsN.y):numpy.max(locationsN.y),numpy.min(locationsN.x):numpy.max(locationsN.x)] | |
masksm=maskN[numpy.min(locationsN.y):numpy.max(locationsN.y),numpy.min(locationsN.x):numpy.max(locationsN.x)] | |
meanPx=numpy.mean(arrsm[numpy.logical_not(masksm)]) | |
arr=arr+(average_back-meanPx) | |
threshadj=thresh+(average_back-meanPx) | |
mask=numpy.ones(arr.shape,dtype=numpy.bool) | |
mask[arrN<threshadj]=False | |
# Measure culture phenotypes | |
locations=measureSizeAndColour(locationsN,arr,im,mask,average_back,BARCODE,FILENAME[0:-4]) | |
# Write results to file | |
locations.to_csv(os.path.join(os.path.dirname(FILENAME),"Output_Data",os.path.basename(FILENAME).split(".")[0]+".out"),"\t",index=False,engine='python') | |
dataf=saveColonyzer(os.path.join(os.path.dirname(FILENAME),"Output_Data",os.path.basename(FILENAME).split(".")[0]+".dat"),locations,threshadj,dx,dy) | |
# Visual check of culture locations | |
imthresh=threshPreview(arr,threshadj,locations) | |
imthresh.save(os.path.join(os.path.dirname(FILENAME),"Output_Images",os.path.basename(FILENAME).split(".")[0]+".png")) | |
# Get ready for next image | |
print("Finished: "+FILENAME+" "+str(time.time()-start)+" s") | |
barcdict=getBarcodes(os.getcwd(),barcRange) | |
# Setup output directories if not already present | |
rept=setupDirectories(barcdict) | |
print("No more barcodes to analyse... I'm done.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment