// // Script to test Autofocus alogrithms by testing them against the existing autofocus // Prashanth // import java.io.FileWriter; import java.io.IOException; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JFrame; import java.awt.Container; import java.awt.FlowLayout; import java.lang.Math; import mmcorej.StrVector; import org.micromanager.api.Autofocus; import org.micromanager.api.AcquisitionOptions; // Force load of the java plugin if not installed //acq.installAutofocusPlugin("Autofocus_"); int maxDisplacement = 30; int numTests = 11; String outputfilePath = "C://projects//micromanager1.3//DeviceAdapters//SimpleAF//autofocus_output.txt"; // file locations for storing the output sequence acqName = "test-af-visual"; rootDirName = "C:/acquisitionData"; // general parameters for autofocus tests numFrames = 3; String[] channels = {"AF-Channel"}; Color[] colors = {Color.GRAY}; numSlices = 1; intervalMs = 1000; exposure = 10; try { Autofocus af = gui.getAutofocus(); String autofocusname = mmc.getProperty("Core",mmc.getDevicePropertyNames("Core").get(0)); String ZStage = mmc.getFocusDevice(); //gui.message(ZStage); FileWriter writer = new FileWriter(outputfilePath,true); // Which file to write that data into double manualFocus = mmc.getPosition(ZStage); // Enter the point of manual focus here String Mesg = autofocusname+" test - All measurements of errors and error statistics in microns, \n---------------------------------------------------------------------------------------------"; Mesg += "\nManual best focus is : "+((double)Math.round(manualFocus*1000)/1000) +"\n"; Double [] displacements = new Double[numTests]; double step = (2*(double)maxDisplacement)/(double)numTests; Double [] err = new Double[numTests]; boolean addoneFlag = false; double displacement; // Calculate the standard deviation Double [] residuals = new Double[numTests]; Double sd = 0; // Calculate mean error Double signedmean = 0; // Calculate minimum error and maximum error Double minerror = 500000000.0; Double maxerror = -50000000.0; // Create the displacement array that the script willl have to run for(int i = 0; i < numTests; ++i) { displacement = (manualFocus - maxDisplacement) + i*(step); if(displacement == 0 || addoneFlag) { displacement = ((i+1)*step -maxDisplacement); addoneFlag = true; } displacements[i] = displacement; double z_stagedispfrombest = (- maxDisplacement) + i*(step); mmc.setPosition(ZStage,(displacement)); double start = System.currentTimeMillis(); af.fullFocus(); String valfull = "Not-Initialized"; StrVector properties = mmc.getDevicePropertyNames(autofocusname); for (int i=0; i maxerror) maxerror = err[i]; } Mesg += "Run report : ("+autofocusname+")\n************\n"; Mesg += "Average focus : "+(signedmean + manualFocus)+"\n"; Mesg += "Mean error : "+(signedmean)+" microns \n"; Mesg += "Highest Focus Point : "+((manualFocus - minerror))+" \n"; Mesg += "Lowest Focus Point : "+((manualFocus - maxerror))+" \n"; Mesg += "Span of the autofocus values is : "+((((double)Math.round((maxerror - minerror)*1000)))/1000)+" microns \n"; Double meanfocus = manualFocus + signedmean; // Find maximum deviation from mean focus Double maxdeviation = -1000000; double maxdeviationPos = 0; for(int i = 0; i < numTests; i++) { if(Math.abs((manualFocus+err[i]) - meanfocus) > maxdeviation) { maxdeviation = (manualFocus+err[i]) - meanfocus; maxdeviationPos = manualFocus+err[i]; } } Mesg += "Maximum deviation from mean computed focus is : "+maxdeviation+" microns \n"; for(int i = 0; i < numTests; ++i) { residuals[i] = (err[i] - signedmean)*(err[i] - signedmean); } for(int i = 0; i < numTests; ++i) { sd += residuals[i]/numTests; } sd = Math.sqrt(sd); Mesg += "The standard deviation of the errors (calculated with the signed mean) is : "+(sd)+" microns \n\n\n"; //Mesg += "The lowest AF value observed was "+lowest+" , and the highest AF value observed was "+highest+" span being "+(highest - lowest); Mesg+= "Properties for the device are: \n"; //String autofocusname = mmc.getProperty("Core",mmc.getDevicePropertyNames("Core").get(0)); StrVector properties = mmc.getDevicePropertyNames(autofocusname); //properties = mmc.getDevicePropertyNames(getProperty(properties.get(0)); for (int i=0; i