""" Folder watcher used for smart imaging It first pop up a user input to choose the method to use for smart imaging anf if we are in a test mode (in this case we have to define an image folder and the job files will be saved in a subfolder) Then it calls Config method from the corresponding script for smart imaging that pops up another user input window to configure this selected method. Once this configuration is closed, the main script calls the Run method from the subscript with every image it founs while watching the folder The Run method perform the smart imaging detection and return the objective coordinates for the high magnification acquisition. NB : the compution of the corrdiantes is done in the subscript since it has access to the image and template dimension. It could be done in the main script below but this would mean returning a lot of variables fron the subscript The main script use this corrdiantes to geenrate the JobFile before going to the next image. This is repeated until the max number of images set in the configuration window is reached (or until the macro is killed, otherwise the folder watching keeps going on) This version is made for IM04 only sonce the metadata for objective... are extracted from the filename """ #@PrefService prefs import os, time from os.path import join,isdir,isfile from ij import IJ from fiji.util.gui import GenericDialogPlus from java.lang.System import getProperty from ast import literal_eval ##################################################################################### # Create function that will write the job files def WriteJob(ImageName,X,Y): global JobFolder,doSwitch ''' Function that will be call with each image after detection of the region of interest X,Y : Objective coordinates in mm with 3 decimal digits for high objective imaging ''' # Write job file for higher magnification for each processed image IJ.log('Writing job file') print 'Writing job file' # Extract Metadata before writing job file - IM4 name convention here ! Well = ImageName[1:5] # A1 for instance WellNum = int(ImageName[106:111]) # 1 to 96, convert to int to remove leading 0 PixelSize_Low = ImageName[34:39] ObjIdx_Low = PixToObj[PixelSize_Low] # Get objective index from pixel size read from image name Obj_Low = ObjToMag[ObjIdx_Low] # Magnification '2X','4X','10X' or '20X' #Power = int(ImageName[43:47]) #Integration = int(ImageName[51:55]) Path_TmpFileName = os.path.join(JobFolder, ImageName+".tmp") # first created as a tmp file to prevent machine from reading itthen rename as .job JobFile = open(Path_TmpFileName,'w') JobFile.write("SetWellNo({})\n".format(WellNum)) JobFile.write("SetMeta('Well Coordinate','{}')\n".format(Well)) JobFile.write("SetMeta('Well Pos in Well','1')\n") JobFile.write("GotoXY({:.3f},{:.3f},'Abs')\n".format(X,Y)) # x,y as float 3-decimal, force the trailing 0 if 0 as last decimal JobFile.write("SetMeta('Objective','{}')\n".format(HighObj_Mag)) # '2X','4X','10X' or '20X' JobFile.write("SetMeta('Objective Pixel Size','{}')\n".format(PixelSize_High)) JobFile.write("SetObjective({})\n".format(ObjIdx_High)) ''' JobFile.write("SetLight(6,10,10,0.000)\n") JobFile.write("AcquireAutofocus(7,100.000,2,'True','True')\n") JobFile.write("SetLight(6,50,10,0.000,'Flash')\n") JobFile.write("AcquireAutofocus(6,20.000,2,'True','True')\n") JobFile.write("SetLight(6,50,10,0.000)\n") JobFile.write("Acquire(1,0.000,1,'D:\IMAGING-DATA\IMAGES\SCRIPTS','True','SIdata')\n") # Images will be saved in a subfolder SIdata ''' JobFile.write(Script+'\n') # Switching off the light JobFile.write("SetLight(0,0,0,0)\n") if doSwitch: # Going back to previous obj. JobFile.write("SetMeta('Objective','{}')\n".format(Obj_Low)) JobFile.write("SetMeta('Objective Pixel Size','{}')\n".format(PixelSize_Low)) JobFile.write("SetObjective({})\n".format(ObjIdx_Low)) else: # The switch should be done for the very last well ? pass JobFile.close() # Rename from tmp to job Path_JobFileName = Path_TmpFileName[:-4]+'.job' # Check that the job file is not already existing if isfile(Path_JobFileName): IJ.log('Overwriting existing JobFile') print 'Overwriting existing JobFile' os.remove(Path_JobFileName) # Finally rename from .tmp to .job os.rename(Path_TmpFileName,Path_JobFileName) ######################################################################################## # START # Try to recover previous input from persistence Method0 = prefs.get("SImethod","Template matching") CountMax0 = prefs.getInt("nWell",96) # this one fails Test0 = prefs.getInt("Test",True) # for Boolean use getInt (see forum for bug) Path_ImageFolder0 = prefs.get("ImagePathSI","ImageFolder") HighObj_Mag0 = prefs.get("HighMag","10x") doJob0 = prefs.getInt("WriteJob",False) doSwitch0 = prefs.getInt("SwitchObj",True) template = """SetLight(6,10,10,0.000) AcquireAutofocus(7,100.000,2,'True','True') SetLight(6,50,10,0.000,'Flash') AcquireAutofocus(6,20.000,2,'True','True') SetLight(6,50,10,0.000) Acquire(1,0.000,1,'D:\IMAGING-DATA\IMAGES\SCRIPTS','True','SIdata') """ Script0 = prefs.get(None,"SIscript",template) # Input GUI Win = GenericDialogPlus("Smart Imaging") Win.addChoice("Smart imaging method",["Template matching","Keypoint matching","Center of mass"],Method0) Win.addNumericField("Number of well to process",CountMax0,0) Win.addCheckbox("Test mode (if not on the microscope)",Test0) Win.addDirectoryField("Directory to watch (in test mode)",Path_ImageFolder0) Win.addChoice("Objective for smart imaging",["2x","4x","10x","20x"],HighObj_Mag0) Win.addCheckbox("Write job files", doJob0) # JobFile are written anyway if we are not in Test mode Win.addCheckbox("Switch to previous objective after each smart imaging job", doSwitch0) # Add a text field with the smart imaging script Win.addMessage("Copy/Paste a smart imaging script in the field below or use a template : ") Win.addTextAreas(Script0,None,5,100) # Add help button Disclaimer = '''
The smart imaging is the implementation of feedback microscopy on Acquifer's imaging machine.
The idea is to use image recognition scripts to automatically detect the ROI in the low magnification images,
and perform the corresponding high magnification acquisition. See documentation.