Commit 08b36456 authored by Andrey Filippov's avatar Andrey Filippov

modified other distance measurements (like for temperature coefficient)

to use lens aberration model
parent 77c3afd4
......@@ -46,6 +46,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import FocusingField.FocusingFieldMeasurement;
//import FocusingField.MeasuredSample;
import Jama.Matrix; // Download here: http://math.nist.gov/javanumerics/jama/
......@@ -4388,6 +4389,7 @@ if (MORE_BUTTONS) {
String path=dFile+Prefs.getFileSeparator()+lensPrefix+CAMERAS.getLastTimestampUnderscored()+"-focus.csv";
if (MASTER_DEBUG_LEVEL>0) System.out.println ("Saving focusing log data to "+path);
MOTORS.listHistory(
FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics && (FOCUSING_FIELD!=null),
path, // on screen, path - to csv
FOCUS_MEASUREMENT_PARAMETERS.lensSerial,
FOCUS_MEASUREMENT_PARAMETERS.comment,
......@@ -4473,6 +4475,7 @@ if (MORE_BUTTONS) {
if (label.equals("List History")) {
DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
MOTORS.listHistory(
FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics && (FOCUSING_FIELD!=null),
null, // on screen, path - to csv
FOCUS_MEASUREMENT_PARAMETERS.lensSerial,
FOCUS_MEASUREMENT_PARAMETERS.comment,
......@@ -5268,7 +5271,7 @@ if (MORE_BUTTONS) {
checkSerialAndRestore(); // returns true if did not change or was restored
boolean modeAverage=label.equals("Focus Average");
boolean noTiltScan=true;
boolean useLMA=true;
// boolean useLMA=true;
DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
MOTORS.focusingHistory.optimalMotorPosition( // recalculate calibration to estimate current distance from center PSF
FOCUS_MEASUREMENT_PARAMETERS,
......@@ -5285,7 +5288,8 @@ if (MORE_BUTTONS) {
}
gd.addCheckbox("Erase previous measurement history",modeAverage);
gd.addCheckbox("Allow tilt scan when looking for the best fit",!noTiltScan);
gd.addCheckbox("Use LMA calculations for focus/tilt",useLMA);
gd.addCheckbox ("Use lens aberration model (if available) for focal distance and tilts", FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics);
// gd.addCheckbox("Use LMA calculations for focus/tilt",useLMA);
double scanMinutes=modeAverage?2.0:30.0;
gd.addNumericField("Measure for ", scanMinutes , 1,5," minutes");
gd.showDialog();
......@@ -5297,7 +5301,17 @@ if (MORE_BUTTONS) {
}
if (gd.getNextBoolean()) MOTORS.clearHistory();
noTiltScan=!gd.getNextBoolean();
useLMA=gd.getNextBoolean();
FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics=gd.getNextBoolean();
// Only try to read history if useLMAMetrics is set
if (FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics && (FOCUSING_FIELD==null)) {
if (DEBUG_LEVEL>0) System.out.println("FOCUSING_FIELD==null, trying to restore from the previously saved file");
if (!restoreFocusingHistory(false)) { // try to restore from the saved history file
System.out.println("Failed to restore history, disabling use of lens aberration model");
}
FOCUSING_FIELD.setDebugLevel(DEBUG_LEVEL);
}
boolean useLMA=FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics && (FOCUSING_FIELD!=null);
// int startHistPos=MOTORS.historySize();
scanMinutes=gd.getNextNumber();
long startTime=System.nanoTime();
......@@ -5319,6 +5333,14 @@ if (MORE_BUTTONS) {
pX0,
pY0);
}
if (useLMA){
FOCUSING_FIELD.testQualB(false); // optimize qualB, store results in this.qualBOptimizationResults
if (MASTER_DEBUG_LEVEL>0) {
System.out.println("Optimal absolute Zc="+FOCUSING_FIELD.qualBOptimizationResults[0]);
System.out.println("Optimal Tx="+FOCUSING_FIELD.qualBOptimizationResults[1]);
System.out.println("Optimal Ty="+FOCUSING_FIELD.qualBOptimizationResults[2]);
}
}
while (System.nanoTime()<endTime){
moveAndMaybeProbe(
true, // just move, not probe
......@@ -5350,7 +5372,6 @@ if (MORE_BUTTONS) {
}
// LMA version
FocusingField ff= null;
if (useLMA){
ff= new FocusingField(
......@@ -5371,22 +5392,34 @@ if (MORE_BUTTONS) {
ff,
(runs==0)?0:(MOTORS.historySize()-runs),
MOTORS.historySize()); // all newly acquired
// TODO: Remove after checking average
/*
if (modeAverage && (FOCUSING_FIELD!=null)){ // calculate/show average over the last run - only in "average" mode
double [] ZTM=FOCUSING_FIELD.averageZTM(noTiltScan,ff); // no tilt scan - faster
double [] ZTM=FOCUSING_FIELD.averageZTM(
noTiltScan,
ff, // no tilt scan - faster
true); // noMotors
if (MASTER_DEBUG_LEVEL>0) {
String msg="Failed to calulate average focus/tilt";
if (ZTM!=null) msg="Average:\n"+
"Relative focal shift "+IJ.d2s(ZTM[0],3)+"um (absolute - "+IJ.d2s(ZTM[0]+FOCUSING_FIELD.qualBOptimizationResults[0],3)+"um)\n"+
"Relative horizontal tilt "+IJ.d2s(ZTM[1],3)+"um/mm (absolute - "+IJ.d2s(ZTM[1]+FOCUSING_FIELD.qualBOptimizationResults[1],3)+"um.mm)\n"+
"Relative vertical tilt "+IJ.d2s(ZTM[2],3)+"um/mm (absolute - "+IJ.d2s(ZTM[2]+FOCUSING_FIELD.qualBOptimizationResults[2],3)+"um.mm)\n"+
"Suggested M1 "+IJ.d2s(ZTM[3],0)+"steps\n"+
"Suggested M2 "+IJ.d2s(ZTM[4],0)+"steps\n"+
"Suggested M3 "+IJ.d2s(ZTM[5],0)+"steps";
if (ZTM!=null) {
msg="Average:\n"+
"Relative focal shift "+IJ.d2s(ZTM[0],3)+"um (absolute - "+IJ.d2s(ZTM[0]+FOCUSING_FIELD.qualBOptimizationResults[0],3)+"um)\n"+
"Relative horizontal tilt "+IJ.d2s(ZTM[1],3)+"um/mm (absolute - "+IJ.d2s(ZTM[1]+FOCUSING_FIELD.qualBOptimizationResults[1],3)+"um.mm)\n"+
"Relative vertical tilt "+IJ.d2s(ZTM[2],3)+"um/mm (absolute - "+IJ.d2s(ZTM[2]+FOCUSING_FIELD.qualBOptimizationResults[2],3)+"um.mm)\n";
if (ZTM.length>3) {
msg+="Suggested M1 "+IJ.d2s(ZTM[3],0)+"steps\n"+
"Suggested M2 "+IJ.d2s(ZTM[4],0)+"steps\n"+
"Suggested M3 "+IJ.d2s(ZTM[5],0)+"steps";
}
}
System.out.println(msg);
IJ.showMessage(msg);
}
}
*/
//
}
if (FOCUS_MEASUREMENT_PARAMETERS.saveResults) {
String dir=getResultsPath(FOCUS_MEASUREMENT_PARAMETERS);
File dFile=new File(dir);
......@@ -5415,6 +5448,7 @@ if (MORE_BUTTONS) {
sensorHeight=FOCUSING_FIELD.sensorHeight;
}
MOTORS.listHistory(
useLMA,
path, // on screen, path - to csv
FOCUS_MEASUREMENT_PARAMETERS.serialNumber,
FOCUS_MEASUREMENT_PARAMETERS.lensSerial,
......@@ -5442,11 +5476,13 @@ if (MORE_BUTTONS) {
if (!modeAverage) {
double [] lastKT=MOTORS.focusingHistory.temperatureLinearApproximation(
useLMA,
runs, // number of last samples from history to use, 0 - use all
FOCUS_MEASUREMENT_PARAMETERS.lensDistanceWeightK,
FOCUS_MEASUREMENT_PARAMETERS.lensDistanceWeightY
);
double [] allKT=MOTORS.focusingHistory.temperatureLinearApproximation(
useLMA,
0, // number of last samples from history to use, 0 - use all
FOCUS_MEASUREMENT_PARAMETERS.lensDistanceWeightK,
FOCUS_MEASUREMENT_PARAMETERS.lensDistanceWeightY
......@@ -5467,6 +5503,7 @@ if (MORE_BUTTONS) {
IJ.showMessage("Info",msg);
}
// Now in LMA mode - recalculate and overwrite
/*
if (useLMA){
ff= new FocusingField(
EYESIS_CAMERA_PARAMETERS.getSensorWidth(),
......@@ -5486,7 +5523,12 @@ if (MORE_BUTTONS) {
if (MASTER_DEBUG_LEVEL>0){
System.out.println ("*** Calculating focal distance shift for each of "+MOTORS.historySize()+" recorded measurments ***");
}
double [][] allZTM=FOCUSING_FIELD.getAllZTM(noTiltScan,ff); // no tilt scan (supposed to be adjusted
boolean noMotors=true;
double [][] allZTM=FOCUSING_FIELD.getAllZTM(
noTiltScan,
ff, // no tilt scan (supposed to be adjusted
noMotors); // boolean noMotors) // do not calculate correction
lastKT=MOTORS.focusingHistory.temperatureLinearApproximation(
allZTM,
runs
......@@ -5511,6 +5553,7 @@ if (MORE_BUTTONS) {
IJ.showMessage("Info",msg);
}
}
*/
}
saveCurrentConfig();
return;
......@@ -9125,6 +9168,12 @@ if (MORE_BUTTONS) {
double [][][] sampleCoord=null;
if (FOCUSING_FIELD!=null){
sampleCoord=FOCUSING_FIELD.getSampleCoord();
FOCUSING_FIELD.testQualB(false); // optimize qualB, store results in this.qualBOptimizationResults
if (MASTER_DEBUG_LEVEL>0) {
System.out.println("Optimal absolute Zc="+FOCUSING_FIELD.qualBOptimizationResults[0]);
System.out.println("Optimal Tx="+FOCUSING_FIELD.qualBOptimizationResults[1]);
System.out.println("Optimal Ty="+FOCUSING_FIELD.qualBOptimizationResults[2]);
}
} else {
sampleCoord=FOCUS_MEASUREMENT_PARAMETERS.sampleCoordinates(
FOCUS_MEASUREMENT_PARAMETERS.result_PX0,
......@@ -9154,7 +9203,12 @@ if (MORE_BUTTONS) {
//get measurement
FocusingField.FocusingFieldMeasurement fFMeasurement=MOTORS.getThisFFMeasurement(FOCUSING_FIELD);
// calculate z, tx, ty, m1,m2,m3
double [] zTxTyM1M2M3 = FOCUSING_FIELD.adjustLMA(false,fFMeasurement,false); // allow tilt scan
double [] zTxTyM1M2M3 = FOCUSING_FIELD.adjustLMA(
false, // allow tilt scan
fFMeasurement,
false, // parallel move
true, // boolean noQualB, // do not re-claculate testQualB
false); // boolean noAdjust); // do not calculate correction
// show dialog: Apply, re-calculate, exit
int [] currentMotors=fFMeasurement.motors;
int [] newMotors=currentMotors.clone();
......@@ -9279,7 +9333,13 @@ if (MORE_BUTTONS) {
DEBUG_LEVEL=MASTER_DEBUG_LEVEL;
FOCUSING_FIELD.setDebugLevel(DEBUG_LEVEL);
if (parallelMove){ // ignore/recalculate newMotors data
zTxTyM1M2M3 = FOCUSING_FIELD.adjustLMA(false,fFMeasurement,true); // recalculate with parallel move only, allow tilt scan
zTxTyM1M2M3 = FOCUSING_FIELD.adjustLMA(
false,// disable tilt scan
fFMeasurement,
true, // recalculate with parallel move only
false, // boolean noQualB, // do not re-claculate testQualB - OPTIMIZE to do once
false); // boolean noAdjust); // do not calculate correction
newMotors=currentMotors.clone();
if (zTxTyM1M2M3!=null){
newMotors[0]=(int) Math.round(zTxTyM1M2M3[3]);
......@@ -11114,6 +11174,34 @@ if (MORE_BUTTONS) {
debugLevel,
loopDebugLevel);
// System.out.println(">>"+focusingMotors.historySize()+": "+focusingMotors.curpos[0]+", "+focusingMotors.curpos[1]+", "+focusingMotors.curpos[2]);
int ca=6;
double [] zTxTy=null; // {Double.NaN,Double.NaN,Double.NaN};
double oldTx=Double.NaN,oldTy=Double.NaN,oldFarNear=Double.NaN;
if (metrics!=null ){
oldFarNear=metrics[ca][0];
oldTx= metrics[ca][1];
oldTy= metrics[ca][2];
}
if (FOCUS_MEASUREMENT_PARAMETERS.useLMAMetrics && (FOCUSING_FIELD!=null)){
FocusingField.FocusingFieldMeasurement fFMeasurement= FOCUSING_FIELD.getFocusingFieldMeasurement(
ts, //focusingState.getTimestamp(),
focusMeasurementParameters.sensorTemperature,
focusingMotors.readElphel10364Motors(), //focusingHistory.getPosition(), //null?
rFullResults[0]); //focusingState.getSamples());
zTxTy=FOCUSING_FIELD.adjustLMA(
true, // false, // noTiltScan
fFMeasurement,
false, // parallel move
true, // boolean noQualB, // do not re-claculate testQualB
true); // boolean noAdjust); // do not calculate correction
if (zTxTy!=null){
metrics[ca][0]=zTxTy[0]; // was far/near
metrics[ca][1]=zTxTy[1];
metrics[ca][2]=zTxTy[2];
}
}
focusingMotors.addToHistory(ts,focusMeasurementParameters.sensorTemperature,metrics,rFullResults[0]);
// System.out.println("focusMeasurementParameters.lensDistanceWeightK="+focusMeasurementParameters.lensDistanceWeightK);
......@@ -11121,7 +11209,7 @@ if (MORE_BUTTONS) {
if ((debugLevel>0) && (metrics!=null)){
// see if lens is calibrated
double [] resolutions={1.0/metrics[1][6],1.0/metrics[5][6],1.0/metrics[2][6]}; // R,G,B
double fDistance=focusingMotors.focusingHistory.getLensDistance(
double fDistanceOld=focusingMotors.focusingHistory.getLensDistance(
resolutions, // {R-sharpness,G-sharpness,B-sharpness}
true, // boolean absolute, // return absolutely calibrated data
focusMeasurementParameters.lensDistanceWeightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
......@@ -11129,18 +11217,25 @@ if (MORE_BUTTONS) {
debugLevel
);
int ca=6;
// int ca=6;
String sZTxTy="";
if (zTxTy!=null){
sZTxTy="z="+IJ.d2s(zTxTy[0],3)+"um"+" tX="+IJ.d2s(zTxTy[1],3)+"um/mm"+" tY="+IJ.d2s(zTxTy[2],3)+"um/mm";
}
int [] actualPosition=focusingMotors.focusingHistory.getPosition();
System.out.println("##"+focusingMotors.historySize()+": "+actualPosition[0]+", "+actualPosition[1]+", "+actualPosition[2]+
": fDistance="+IJ.d2s(fDistance,3)+"um"+
" Far/Near="+IJ.d2s(metrics[ca][0],3)+
" Tilt X="+IJ.d2s(metrics[ca][1],3)+
" Tilt Y="+IJ.d2s(metrics[ca][2],3)+
": "+
sZTxTy+
" fDistanceOld="+IJ.d2s(fDistanceOld,3)+"um"+
" Far/Near="+IJ.d2s(oldFarNear,3)+
" Tilt X="+IJ.d2s(oldTx,3)+
" Tilt Y="+IJ.d2s(oldTy,3)+
" R50% average="+IJ.d2s(metrics[ca][3],3)+" sensor pixels,"+
" A50% average="+IJ.d2s(metrics[ca][4],3)+" sensor pixels,"+
" B50% average="+IJ.d2s(metrics[ca][5],3)+" sensor pixels,"+
" R50%Center="+IJ.d2s(metrics[ca][6],3)+" sensor pixels"+
" temp="+focusMeasurementParameters.sensorTemperature);
double fDistance=(zTxTy==null)?fDistanceOld:zTxTy[0];
if (debugLevel>1){
String [] compColors={"green","red","blue","green","green","green","AVERAGE"};
for (int c=0;c<metrics.length-1;c++) if (metrics[c]!=null){
......@@ -11162,7 +11257,6 @@ if (MORE_BUTTONS) {
focusMeasurementParameters.result_B50=metrics[ca][5]; // last measured B50 (simailar, but R^4 are averaged)
focusMeasurementParameters.result_RC50=metrics[ca][6]; // last measured RC50(R50 calculated only for the 2 center samples)
}
}
......
......@@ -2977,7 +2977,25 @@ public class CalibrationHardwareInterface {
fullResults
);
}
public FocusingField.FocusingFieldMeasurement getThisFFMeasurement(
FocusingField focusingField,
String sTimestamp,
double temperature,
double[][] psfMetrics,
double [][][][] fullResults){
return focusingField.getFocusingFieldMeasurement(
sTimestamp, //focusingState.getTimestamp(),
temperature, //focusingState.getTemperature(),
this.curpos, //focusingState.motorsPos,
fullResults); //focusingState.getSamples());
}
/*
* FocusingHistory.FocusingState focusingState
public void addToHistory( String sTimestamp,double temperature, double[][] psfMetrics){ // null OK
this.focusingHistory.add(
sTimestamp,
......@@ -3004,6 +3022,7 @@ public class CalibrationHardwareInterface {
}
public void listHistory(
boolean useLMA,
String path,
String lensSerial,
String comment,
......@@ -3015,6 +3034,7 @@ public class CalibrationHardwareInterface {
double weightY // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
){
listHistory(
useLMA,
path,
"",
lensSerial,
......@@ -3033,6 +3053,7 @@ public class CalibrationHardwareInterface {
}
public void listHistory(
boolean useLMA,
String path,
String serialNumber,
String lensSerial,
......@@ -3051,6 +3072,7 @@ public class CalibrationHardwareInterface {
){
this.focusingHistory.list(
useLMA,
path,
serialNumber,
lensSerial,
......@@ -3084,6 +3106,7 @@ public class CalibrationHardwareInterface {
pY0,
sampleCoord);
}
public FocusingField.FocusingFieldMeasurement getThisFFMeasurement(FocusingField focusingField){
return getThisFFMeasurement(focusingField, -1);
}
......@@ -3895,10 +3918,18 @@ public class CalibrationHardwareInterface {
if (this.history.size()<1) return null;
return getCenterResolutions(this.history.size()-1);
}
public double [] getCenterResolutions(int index){
public double [] getCenterResolutions(int index){
if ((index<0) || (index>=this.history.size())) return null;
return this.history.get(index).getCenterResolutions();
}
}
public double [] getzTxTy(int index){
if ((index<0) || (index>=this.history.size())) return null;
return this.history.get(index).getzTxTy();
}
public double [] getzTxTy(){
if (this.history.size()<1) return null;
return getzTxTy(this.history.size()-1);
}
public int size(){
return this.history.size();
}
......@@ -5067,6 +5098,7 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
* @return array of 2 elements - {length at 0C, microns/degree}
*/
public double [] temperatureLinearApproximation(
boolean useLMA,
int numSamples, // number of last samples from history to use, 0 - use all
double lensDistanceWeightK, // used in distance calculation
double lensDistanceWeightY // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
......@@ -5079,15 +5111,19 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
int firstSample=this.history.size()-numSamples;
for (int nSample=0;nSample<numSamples;nSample++){
resolutions= this.history.get(firstSample+nSample).getCenterResolutions();
data[nSample][0]=this.history.get(firstSample+nSample).getTemperature();
data[nSample][1]=getLensDistance(
resolutions, // {R-sharpness,G-sharpness,B-sharpness}
true, // boolean absolute, // return absolutely calibrated data
lensDistanceWeightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
lensDistanceWeightY, // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
1 //debugLevel
);
if (useLMA){
data[nSample][1]=this.history.get(firstSample+nSample).getzTxTy()[0];
} else {
resolutions= this.history.get(firstSample+nSample).getCenterResolutions();
data[nSample][1]=getLensDistance(
resolutions, // {R-sharpness,G-sharpness,B-sharpness}
true, // boolean absolute, // return absolutely calibrated data
lensDistanceWeightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
lensDistanceWeightY, // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
1 //debugLevel
);
}
}
PolynomialApproximation pa= new PolynomialApproximation(debugLevel);
double [] polyCoeff=pa.polynomialApproximation1d(data, 1); // just linear
......@@ -5524,6 +5560,7 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
// todo - add "probe around" - 6/3 points, +/- for each direction (fraction of sigma?)
public void list(
boolean useLMA,
String path,
String lensSerial, // if null - do not add average
String comment,
......@@ -5533,8 +5570,10 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
double weightRatioBlueToGreen,
double weightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
double weightY){ // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
list (path,
"",
list (
useLMA,
path,
"", // serial; number
lensSerial, // if null - do not add average
comment,
showIndividualComponents,
......@@ -5641,6 +5680,7 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
}
public void list(
boolean useLMA,
String path,
String serialNumber,
String lensSerial, // if null - do not add average
......@@ -5701,15 +5741,22 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
if (showIndividualComponents) {
for (int i=0;i<this.history.size();i++){
FocusingState focusingState=this.history.get(i);
double [] zTxTy=useLMA?focusingState.getzTxTy():null;
double [][] metrics=focusingState.getMetrics(weightRatioRedToGreen,weightRatioBlueToGreen);
double [][] resolution=focusingState.getSharpness(weightRatioRedToGreen,weightRatioBlueToGreen);
double dist= getLensDistance(
double dist= (zTxTy==null)?getLensDistance(
focusingState.getCenterResolutions(), // {R-sharpness,G-sharpness,B-sharpness}
true, //boolean absolute, // return absolutely calibrated data
weightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
weightY, // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
1); //int debugLevel
1): //int debugLevel
zTxTy[0];
double [] averageMetrics=metrics[3];
if (zTxTy!=null){
averageMetrics=metrics[3].clone(); // to modify w/o changing original
averageMetrics[1]=zTxTy[1]; // tiltX
averageMetrics[2]=zTxTy[2]; // tiltY
}
double [] averageResolution=resolution[3];
sb.append((i+1)+"\t");
String timestamp=focusingState.getTimestamp();
......@@ -5779,9 +5826,15 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
for (int i=0;i<this.history.size();i++){
// int parIndex=0;
FocusingState focusingState=this.history.get(i);
double [] zTxTy=useLMA?focusingState.getzTxTy():null;
double [][] metrics=focusingState.getMetrics(weightRatioRedToGreen,weightRatioBlueToGreen);
double [][] resolution=focusingState.getSharpness(weightRatioRedToGreen,weightRatioBlueToGreen);
double [] averageMetrics=metrics[3];
if (zTxTy!=null){
averageMetrics=metrics[3].clone(); // to modify w/o changing original
averageMetrics[1]=zTxTy[1]; // tiltX
averageMetrics[2]=zTxTy[2]; // tiltY
}
double [] averageResolution=resolution[3];
if (!justSummary) sb.append((i+1)+"\t");
String timestamp=focusingState.getTimestamp();
......@@ -5806,12 +5859,13 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
sums[4]+=focusingState.motorsPos[0];
sums[5]+=focusingState.motorsPos[1];
sums[6]+=focusingState.motorsPos[2];
double dist= getLensDistance(
focusingState.getCenterResolutions(), // {R-sharpness,G-sharpness,B-sharpness}
true, //boolean absolute, // return absolutely calibrated data
weightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
weightY, // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
1); //int debugLevel
double dist= (zTxTy==null)?getLensDistance(
focusingState.getCenterResolutions(), // {R-sharpness,G-sharpness,B-sharpness}
true, //boolean absolute, // return absolutely calibrated data
weightK, // 0.0 - all 3 component errors are combined with the same weights. 1.0 - proportional to squared first derivatives
weightY, // R-frac, Y-frac have the same scale regardless of the sharpness, but not Y. This is to balance Y contribution
1): //int debugLevel
zTxTy[0];
if (Double.isNaN(dist)){
if (!justSummary) sb.append("\t---");
} else {
......@@ -6199,7 +6253,19 @@ if (debugLevel>=debugThreshold) System.out.println(i+" "+diff[0]+" "+diff[1]+" "
1.0,
weightRatioBlueToGreen);
}
// alternative mode (from aberration model) ising metrics[6][]
public double [] getzTxTy(){
if (this.psfMetricses==null){
return null;
}
if ((this.psfMetricses.length<7) || (this.psfMetricses[6]==null)) {
System.out.println("BUG? psfMetrics does not have line 6 with lens berration model z, tx, ty");
return null;
}
double [] zTxTy={this.psfMetricses[6][0],this.psfMetricses[6][1],this.psfMetricses[6][2]};
return zTxTy;
}
public double [][] getMetrics(
double weightRed,
double weightGreen,
......
......@@ -5121,20 +5121,39 @@ public boolean LevenbergMarquardt(
public double [][] getAllZTM(
boolean noTiltScan,
FocusingField ff){
double [][] result =new double[ff.measurements.size()][6];
for (int i=0;i<result.length;i++) result[i]=adjustLMA(noTiltScan,ff.measurements.get(i),false);
FocusingField ff,
boolean noMotors){
double [][] result =new double[ff.measurements.size()][];
for (int i=0;i<result.length;i++) result[i]=adjustLMA(
noTiltScan,
ff.measurements.get(i),
false, // boolean parallelMove,
true, // boolean noQualB, // do not re-claculate testQualB
noMotors); // boolean noAdjust) // do not calculate correction
return result;
}
public double [] averageZTM(// results relative to optimal
boolean noTiltScan,
FocusingField ff){
double [] result =new double[6];
FocusingField ff,
boolean noMotors){
if (debugLevel>0) System.out.println("Calculating optimal focal/tilt, qualBOptimizeMode="+this.qualBOptimizeMode);
testQualB(false); // optimize qualB, store results in this.qualBOptimizationResults
if (debugLevel>0) {
System.out.println("Optimal absolute Zc="+this.qualBOptimizationResults[0]);
System.out.println("Optimal Tx="+this.qualBOptimizationResults[1]);
System.out.println("Optimal Ty="+this.qualBOptimizationResults[2]);
}
double [] result =new double[noMotors?3:6];
for (int i=0;i<result.length;i++) result[i]=0.0;
int num=0;
for (FocusingFieldMeasurement measurement:ff.measurements){
double [] ZTM = adjustLMA(noTiltScan,measurement,false);
double [] ZTM = adjustLMA(
noTiltScan,
measurement,
false, // boolean parallelMove,
true, // boolean noQualB, // do not re-claculate testQualB
noMotors); // boolean noAdjust) // do not calculate correction
if (ZTM!=null) {
for (int i=0;i<result.length;i++) result[i]+=ZTM[i];
num++;
......@@ -5147,13 +5166,17 @@ public boolean LevenbergMarquardt(
public double [] adjustLMA ( // result relative to optimal
boolean noTiltScan,
FocusingFieldMeasurement measurement,
boolean parallelMove){
if (debugLevel>0) System.out.println("Calculating optimal focal/tilt, qualBOptimizeMode="+this.qualBOptimizeMode);
testQualB(false); // optimize qualB, store results in this.qualBOptimizationResults
if (debugLevel>0) {
System.out.println("Optimal absolute Zc="+this.qualBOptimizationResults[0]);
System.out.println("Optimal Tx="+this.qualBOptimizationResults[1]);
System.out.println("Optimal Ty="+this.qualBOptimizationResults[2]);
boolean parallelMove,
boolean noQualB, // do not re-claculate testQualB
boolean noAdjust){ // do not calculate correction
if (!noQualB) {
if (debugLevel>0) System.out.println("Calculating optimal focal/tilt, qualBOptimizeMode="+this.qualBOptimizeMode);
testQualB(false); // optimize qualB, store results in this.qualBOptimizationResults
if (debugLevel>0) {
System.out.println("Optimal absolute Zc="+this.qualBOptimizationResults[0]);
System.out.println("Optimal Tx="+this.qualBOptimizationResults[1]);
System.out.println("Optimal Ty="+this.qualBOptimizationResults[2]);
}
}
if (!testMeasurement(
measurement,
......@@ -5166,55 +5189,49 @@ public boolean LevenbergMarquardt(
if (debugLevel>0) System.out.println("adjustLMA() failed");
return null;
}
double [] result=new double [6];
// double [] best_qb_corr= fieldFitting.getBestQualB(
// k_red,
// k_blue,
// true);
double [] result=new double [noAdjust?3:6];
double [] zTilts=getCenterZTxTy(measurement);
result[0]=zTilts[0]-this.qualBOptimizationResults[0]; //best_qb_corr[0];
result[1]=zTilts[1]-this.qualBOptimizationResults[1];
result[2]=zTilts[2]-this.qualBOptimizationResults[2];
double [] zm=null;
// if (parallelMove){
if (!noAdjust) {
double [] zm=null;
zm=new double [3];
for (int i=0;i<zm.length;i++) zm[i]=fieldFitting.mechanicalFocusingModel.mToZm(measurement.motors[i], i);
// }
if (this.debugLevel>0){
System.out.println("Current linearized motor positions, center="+(0.25*zm[0]+0.25*zm[1]+0.5*zm[2]));
for (int i=0;i<zm.length;i++) {
System.out.println(i+": "+zm[i]+" um");
if (this.debugLevel>0){
System.out.println("Current linearized motor positions, center="+(0.25*zm[0]+0.25*zm[1]+0.5*zm[2]));
for (int i=0;i<zm.length;i++) {
System.out.println(i+": "+zm[i]+" um");
}
double [] rzm=new double [3];
for (int i=0;i<zm.length;i++) rzm[i]=fieldFitting.mechanicalFocusingModel.zmToM(zm[i], i);
System.out.println("Checking back to motor positions, center="+(0.25*rzm[0]+0.25*rzm[1]+0.5*rzm[2])+
"steps (current="+(0.25*measurement.motors[0]+0.25*measurement.motors[1]+0.5*measurement.motors[2])+" steps)");
for (int i=0;i<zm.length;i++) {
System.out.println(i+": "+rzm[i]+" steps (was "+measurement.motors[i]+" steps)");
}
}
double [] dm= getAdjustedMotors(
parallelMove?zm:null,
targetRelFocalShift+this.qualBOptimizationResults[0] , //targetRelFocalShift+best_qb_corr[0],
this.qualBOptimizationResults[1], //0.0, // targetTiltX, // for testing, normally should be 0 um/mm
this.qualBOptimizationResults[2], //0.0, // targetTiltY,
true); // motor steps
if ((dm!=null) && (debugLevel>1)){
System.out.println("Suggested motor positions: "+IJ.d2s(dm[0],0)+":"+IJ.d2s(dm[1],0)+":"+IJ.d2s(dm[2],0));
}
double [] rzm=new double [3];
for (int i=0;i<zm.length;i++) rzm[i]=fieldFitting.mechanicalFocusingModel.zmToM(zm[i], i);
System.out.println("Checking back to motor positions, center="+(0.25*rzm[0]+0.25*rzm[1]+0.5*rzm[2])+
"steps (current="+(0.25*measurement.motors[0]+0.25*measurement.motors[1]+0.5*measurement.motors[2])+" steps)");
for (int i=0;i<zm.length;i++) {
System.out.println(i+": "+rzm[i]+" steps (was "+measurement.motors[i]+" steps)");
if (dm!=null) {
result[3]=dm[0];
result[4]=dm[1];
result[5]=dm[2];
} else {
result[3]=Double.NaN;
result[4]=Double.NaN;
result[5]=Double.NaN;
}
}
double [] dm= getAdjustedMotors(
parallelMove?zm:null,
targetRelFocalShift+this.qualBOptimizationResults[0] , //targetRelFocalShift+best_qb_corr[0],
this.qualBOptimizationResults[1], //0.0, // targetTiltX, // for testing, normally should be 0 um/mm
this.qualBOptimizationResults[2], //0.0, // targetTiltY,
true); // motor steps
if ((dm!=null) && (debugLevel>1)){
System.out.println("Suggested motor positions: "+IJ.d2s(dm[0],0)+":"+IJ.d2s(dm[1],0)+":"+IJ.d2s(dm[2],0));
}
if (dm!=null) {
result[3]=dm[0];
result[4]=dm[1];
result[5]=dm[2];
} else {
result[3]=Double.NaN;
result[4]=Double.NaN;
result[5]=Double.NaN;
}
return result;
return result;
}
// add tx, ty?
......@@ -5310,7 +5327,7 @@ public boolean LevenbergMarquardt(
zTxTy[0]=fieldFitting.mechanicalFocusingModel.getValue(MECH_PAR.z0);
zTxTy[1]=fieldFitting.mechanicalFocusingModel.getValue(MECH_PAR.tx);
zTxTy[2]=fieldFitting.mechanicalFocusingModel.getValue(MECH_PAR.ty);
if (debugLevel>0) System.out.println("testMeasurement(), run "+n+" (z="+zTxTy[0]+" tx="+zTxTy[1]+" ty="+zTxTy[2]+")");
if (debugLevel>1) System.out.println("testMeasurement(), run "+n+" (z="+zTxTy[0]+" tx="+zTxTy[1]+" ty="+zTxTy[2]+")");
boolean [] was2PrevEnable=(wasPrevEnable==null)?null:wasPrevEnable.clone();
wasPrevEnable=(prevEnable==null)?null:prevEnable.clone();
this.lambda=this.adjustmentInitialLambda;
......@@ -5340,8 +5357,8 @@ public boolean LevenbergMarquardt(
break;
}
if (!changedEnable) {
if (debugLevel>0) System.out.println("No filter cnange, finished in "+(n+1)+" step"+((n==0)?"":"s"));
if (debugLevel>0) {
if (debugLevel>1) System.out.println("No filter cnange, finished in "+(n+1)+" step"+((n==0)?"":"s"));
if (debugLevel>1) {
System.out.println("=== Absolute shift/tilt from the measuremet ===");
for (int i=0;i<fieldFitting.mechanicalFocusingModel.paramValues.length;i++){
if ((fieldFitting.mechanicalSelect==null) || fieldFitting.mechanicalSelect[i] ) {
......@@ -9609,7 +9626,7 @@ f_corr: d_fcorr/d_zcorr=0, other: a, reff, kx -> ar[1], ar[2], ar[3], ar[4]
String msg="QualB="+this.currentQualB+" ("+this.firstQualB+") "+
" at "+ IJ.d2s(0.000000001*(System.nanoTime()-this.qStartTime),3);
if (debugLevel>0) System.out.println("qStepLevenbergMarquardtAction() "+msg);
if (debugLevel>1) System.out.println("qStepLevenbergMarquardtAction() "+msg);
// if (this.updateStatus) IJ.showStatus(msg);
if (updateStatus){
IJ.showStatus("Done: Step #"+this.iterationStepNumber+
......
......@@ -195,6 +195,7 @@ public class LensAdjustment {
public String gridGeometryFile="";
public String initialCalibrationFile="";
public String focusingHistoryFile="";
public boolean useLMAMetrics=true; // measure/report focal distance and tilts using lens model/LMA (when available)
public String strategyFile="";
public String resultsSuperDirectory=""; // directory with subdirectories named as serial numbers to stro results
public int EEPROM_channel=1; // EEPROM channel to read serial number from
......@@ -430,6 +431,7 @@ public class LensAdjustment {
String strategyFile,
String resultsSuperDirectory, // directory with subdirectories named as serial numbers to stro results
String focusingHistoryFile,
boolean useLMAMetrics, // measure/report focal distance and tilts using lens model/LMA (when available)
int EEPROM_channel, // EEPROM channel to read serial number from
boolean saveResults, // save focusing results
boolean showResults, // show focusing (includingh intermediate) results
......@@ -577,6 +579,7 @@ public class LensAdjustment {
this.strategyFile=strategyFile;
this.resultsSuperDirectory=resultsSuperDirectory; // directory with subdirectories named as serial numbers to stro results
this.focusingHistoryFile=focusingHistoryFile;
this.useLMAMetrics=useLMAMetrics; // measure/report focal distance and tilts using lens model/LMA (when available)
this.EEPROM_channel=EEPROM_channel; // EEPROM channel to read serial number from
this.saveResults=saveResults; // save focusing results
this.showResults=showResults; // show focusing (includingh intermediate) results
......@@ -725,6 +728,7 @@ public class LensAdjustment {
this.strategyFile,
this.resultsSuperDirectory, // directory with subdirectories named as serial numbers to stro results
this.focusingHistoryFile,
this.useLMAMetrics, // measure/report focal distance and tilts using lens model/LMA (when available)
this.EEPROM_channel,// EEPROM channel to read serial number from
this.saveResults, // save focusing results
this.showResults, // show focusing (includingh intermediate) results
......@@ -871,6 +875,7 @@ public class LensAdjustment {
properties.setProperty(prefix+"strategyFile",this.strategyFile+"");
properties.setProperty(prefix+"resultsSuperDirectory",this.resultsSuperDirectory+"");
properties.setProperty(prefix+"focusingHistoryFile",this.focusingHistoryFile+"");
properties.setProperty(prefix+"useLMAMetrics",this.useLMAMetrics+"");
properties.setProperty(prefix+"serialNumber",this.serialNumber+"");
if (!Double.isNaN(this.sensorTemperature))properties.setProperty(prefix+"sensorTemperature",this.sensorTemperature+"");
if (!Double.isNaN(this.result_lastKT))properties.setProperty(prefix+"result_lastKT",this.result_lastKT+"");
......@@ -1029,6 +1034,9 @@ public class LensAdjustment {
this.resultsSuperDirectory=properties.getProperty(prefix+"resultsSuperDirectory");
if (properties.getProperty(prefix+"focusingHistoryFile")!=null)
this.focusingHistoryFile=properties.getProperty(prefix+"focusingHistoryFile");
if (properties.getProperty(prefix+"useLMAMetrics")!=null)
this.useLMAMetrics=Boolean.parseBoolean(properties.getProperty(prefix+"useLMAMetrics"));
if (properties.getProperty(prefix+"serialNumber")!=null)
this.serialNumber=properties.getProperty(prefix+"serialNumber");
......@@ -1441,7 +1449,8 @@ public class LensAdjustment {
gd.addStringField ("Initial camera intrinsic/extrinsic parametres file", this.initialCalibrationFile,40);
gd.addStringField ("Levenberg-Marquardt algorithm strategy file", this.strategyFile,40);
gd.addStringField ("Focusing results superdirectory (individual will be named by serial numbers)", this.resultsSuperDirectory,40);
gd.addStringField ("Measurement history (acquired during \"Scan Calib LMA\" file", this.focusingHistoryFile,80);
gd.addStringField ("Measurement history (acquired during \"Scan Calib LMA\") file", this.focusingHistoryFile,80);
gd.addCheckbox ("Use lens aberration model (if available) for focal distance and tilts", this.useLMAMetrics);
gd.addNumericField("EEPROM channel to read sensor serial number from", this.EEPROM_channel, 0,4,"");
gd.addCheckbox ("Save SFE focusing results (including intermediate) ", this.saveResults);
gd.addCheckbox ("Show SFE focusing results (including intermediate) ", this.showResults);
......@@ -1618,6 +1627,8 @@ public class LensAdjustment {
this.strategyFile= gd.getNextString();
this.resultsSuperDirectory= gd.getNextString();
this.focusingHistoryFile= gd.getNextString();
this.useLMAMetrics = gd.getNextBoolean();
this.EEPROM_channel= (int) gd.getNextNumber();
this.saveResults= gd.getNextBoolean();
this.showResults= gd.getNextBoolean();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment