use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF in project GDSC-SMLM by aherbert.
the class PeakFit method addPsfOptions.
/**
* Adds the PSF options.
*
* <p>Note that if an astigmatic PSF is selected then the model must be created with
* {@link #configurePsfModel(FitEngineConfiguration, int)}.
*
* @param gd the dialog
* @param fitConfigurationProvider the fit configuration provider
*/
public static void addPsfOptions(final ExtendedGenericDialog gd, final FitConfigurationProvider fitConfigurationProvider) {
final FitConfiguration fitConfig = fitConfigurationProvider.getFitConfiguration();
gd.addChoice("PSF", getPsfTypeNames(), PsfProtosHelper.getName(fitConfig.getPsfType()), new OptionListener<Integer>() {
@Override
public boolean collectOptions(Integer field) {
final FitConfiguration fitConfig = fitConfigurationProvider.getFitConfiguration();
fitConfig.setPsfType(PeakFit.getPsfTypeValues()[field]);
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
final FitConfiguration localFitConfig = fitConfigurationProvider.getFitConfiguration();
final PSFType psfType = localFitConfig.getPsfType();
final ExtendedGenericDialog egd = new ExtendedGenericDialog("PSF Options", null);
PSF oldPsf = null;
if (psfType == PSFType.ASTIGMATIC_GAUSSIAN_2D) {
// The PSF is entirely defined in the model
String[] list = AstigmatismModelManager.listAstigmatismModels(false, localFitConfig.getCalibrationReader().getNmPerPixel(), 0.1);
// In case the calibration has not been updated
if (list.length == 0) {
list = AstigmatismModelManager.listAstigmatismModels(false, true);
}
egd.addChoice("Z-model", list, localFitConfig.getPsfModelName());
} else {
// Collect the PSF parameters
oldPsf = localFitConfig.getPsf();
for (int i = 0; i < oldPsf.getParametersCount(); i++) {
final PSFParameter p = oldPsf.getParameters(i);
egd.addNumericField(String.format("PSF_parameter_%d (%s)", i + 1, p.getName()), p.getValue(), 3);
}
if (psfType == PSFType.ONE_AXIS_GAUSSIAN_2D) {
egd.addCheckbox("Fixed", localFitConfig.isFixedPsf());
}
}
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
if (psfType == PSFType.ASTIGMATIC_GAUSSIAN_2D) {
// The PSF is entirely defined in the model
localFitConfig.setPsfModelName(egd.getNextChoice());
return true;
}
@SuppressWarnings("null") final PSF.Builder b = oldPsf.toBuilder();
final int n = b.getParametersCount();
for (int i = 0; i < n; i++) {
b.getParametersBuilder(i).setValue(egd.getNextNumber());
}
final PSF newPsf = b.build();
localFitConfig.setPsf(newPsf);
boolean changed = !oldPsf.equals(newPsf);
if (psfType == PSFType.ONE_AXIS_GAUSSIAN_2D) {
final boolean newFixed = egd.getNextBoolean();
changed = changed || (newFixed != localFitConfig.isFixedPsf());
localFitConfig.setFixedPsf(newFixed);
}
return changed;
}
});
}
use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF in project GDSC-SMLM by aherbert.
the class Gaussian2DPeakResultHelperTest method canComputePixelAmplitude.
@Test
void canComputePixelAmplitude() {
final float[] x = new float[] { 0f, 0.1f, 0.3f, 0.5f, 0.7f, 1f };
final float[] s = new float[] { 0.8f, 1f, 1.5f, 2.2f };
final float[] paramsf = new float[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
paramsf[Gaussian2DFunction.BACKGROUND] = 0;
paramsf[Gaussian2DFunction.SIGNAL] = 105;
final Gaussian2DFunction f = GaussianFunctionFactory.create2D(1, 1, 1, GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
final SimpleRegression r = new SimpleRegression(false);
for (final float tx : x) {
for (final float ty : x) {
for (final float sx : s) {
for (final float sy : s) {
paramsf[Gaussian2DFunction.X_POSITION] = tx;
paramsf[Gaussian2DFunction.Y_POSITION] = ty;
paramsf[Gaussian2DFunction.X_SD] = sx;
paramsf[Gaussian2DFunction.Y_SD] = sy;
// Get the answer using a single pixel image
// Note the Gaussian2D functions set the centre of the pixel as 0,0 so offset
final double[] params = SimpleArrayUtils.toDouble(paramsf);
params[Gaussian2DFunction.X_POSITION] -= 0.5;
params[Gaussian2DFunction.Y_POSITION] -= 0.5;
f.initialise0(params);
final double e = f.eval(0);
final PSF psf = PsfHelper.create(PSFType.TWO_AXIS_GAUSSIAN_2D);
final CalibrationWriter calibration = new CalibrationWriter();
calibration.setCountPerPhoton(1);
calibration.setIntensityUnit(IntensityUnit.PHOTON);
calibration.setNmPerPixel(1);
calibration.setDistanceUnit(DistanceUnit.PIXEL);
final Gaussian2DPeakResultCalculator calc = Gaussian2DPeakResultHelper.create(psf, calibration, Gaussian2DPeakResultHelper.AMPLITUDE | Gaussian2DPeakResultHelper.PIXEL_AMPLITUDE);
final double o1 = calc.getAmplitude(paramsf);
final double o2 = calc.getPixelAmplitude(paramsf);
// logger.fine(FunctionUtils.getSupplier("e=%f, o1=%f, o2=%f", e, o1, o2));
Assertions.assertEquals(e, o2, 1e-3);
r.addData(e, o1);
}
}
}
}
// logger.fine(FunctionUtils.getSupplier("Regression: pixel amplitude vs amplitude = %f,
// slope=%f, n=%d", r.getR(), r.getSlope(),
// r.getN()));
// The simple amplitude over estimates the actual pixel amplitude
Assertions.assertTrue(r.getSlope() > 1);
}
use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method checkEqual.
private static void checkEqual(ResultsFileFormat fileFormat, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, boolean sort, MemoryPeakResults expectedResults, MemoryPeakResults actualResults) {
Assertions.assertNotNull(actualResults, "Input results are null");
Assertions.assertEquals(expectedResults.size(), actualResults.size(), "Size differ");
final PeakResult[] expected = expectedResults.toArray();
final PeakResult[] actual = actualResults.toArray();
if (sort) {
// Results should be sorted by time
Arrays.sort(expected, (o1, o2) -> o1.getFrame() - o2.getFrame());
}
// TSF requires the bias be subtracted
// double bias = expectedResults.getCalibration().getBias();
final DoubleDoubleBiPredicate deltaD = TestHelper.doublesIsCloseTo(1e-5, 0);
final FloatFloatBiPredicate deltaF = TestHelper.floatsIsCloseTo(1e-5, 0);
for (int i = 0; i < actualResults.size(); i++) {
final PeakResult p1 = expected[i];
final PeakResult p2 = actual[i];
final ObjectArrayFormatSupplier msg = new ObjectArrayFormatSupplier("%s @ [" + i + "]", 1);
Assertions.assertEquals(p1.getFrame(), p2.getFrame(), msg.set(0, "Peak"));
if (fileFormat == ResultsFileFormat.MALK) {
TestAssertions.assertTest(p1.getXPosition(), p2.getXPosition(), deltaF, msg.set(0, "X"));
TestAssertions.assertTest(p1.getYPosition(), p2.getYPosition(), deltaF, msg.set(0, "Y"));
TestAssertions.assertTest(p1.getIntensity(), p2.getIntensity(), deltaF, msg.set(0, "Intensity"));
continue;
}
Assertions.assertEquals(p1.getOrigX(), p2.getOrigX(), msg.set(0, "Orig X"));
Assertions.assertEquals(p1.getOrigY(), p2.getOrigY(), msg.set(0, "Orig Y"));
Assertions.assertNotNull(p2.getParameters(), msg.set(0, "Params is null"));
if (showEndFrame) {
Assertions.assertEquals(p1.getEndFrame(), p2.getEndFrame(), msg.set(0, "End frame"));
}
if (showId) {
Assertions.assertEquals(p1.getId(), p2.getId(), msg.set(0, "ID"));
}
if (showDeviations) {
Assertions.assertNotNull(p2.getParameterDeviations(), msg.set(0, "Deviations"));
}
if (showCategory) {
Assertions.assertEquals(p1.getCategory(), p2.getCategory(), msg.set(0, "Category"));
}
// Binary should be exact for float numbers
if (fileFormat == ResultsFileFormat.BINARY) {
Assertions.assertEquals(p1.getOrigValue(), p2.getOrigValue(), msg.set(0, "Orig value"));
Assertions.assertEquals(p1.getError(), p2.getError(), msg.set(0, "Error"));
Assertions.assertEquals(p1.getNoise(), p2.getNoise(), msg.set(0, "Noise"));
Assertions.assertEquals(p1.getMeanIntensity(), p2.getMeanIntensity(), msg.set(0, "Mean intensity"));
Assertions.assertArrayEquals(p1.getParameters(), p2.getParameters(), msg.set(0, "Params"));
if (showDeviations) {
Assertions.assertArrayEquals(p1.getParameterDeviations(), p2.getParameterDeviations(), msg.set(0, "Params StdDev"));
}
if (showPrecision) {
Assertions.assertEquals(p1.getPrecision(), p2.getPrecision(), msg.set(0, "Precision"));
}
continue;
}
// Otherwise have an error
TestAssertions.assertTest(p1.getOrigValue(), p2.getOrigValue(), deltaF, msg.set(0, "Orig value"));
TestAssertions.assertTest(p1.getError(), p2.getError(), deltaD, msg.set(0, "Error"));
TestAssertions.assertTest(p1.getNoise(), p2.getNoise(), deltaF, msg.set(0, "Noise"));
TestAssertions.assertTest(p1.getMeanIntensity(), p2.getMeanIntensity(), deltaF, msg.set(0, "Mean intensity"));
TestAssertions.assertArrayTest(p1.getParameters(), p2.getParameters(), deltaF, msg.set(0, "Params"));
if (showDeviations) {
TestAssertions.assertArrayTest(p1.getParameterDeviations(), p2.getParameterDeviations(), deltaF, msg.set(0, "Params StdDev"));
}
if (showPrecision) {
// Handle NaN precisions
final double pa = p1.getPrecision();
final double pb = p2.getPrecision();
if (!Double.isNaN(pa) || !Double.isNaN(pb)) {
TestAssertions.assertTest(p1.getPrecision(), p2.getPrecision(), deltaD, msg.set(0, "Precision"));
}
}
}
// Check the header information
Assertions.assertEquals(expectedResults.getName(), actualResults.getName(), "Name");
Assertions.assertEquals(expectedResults.getConfiguration(), actualResults.getConfiguration(), "Configuration");
final Rectangle r1 = expectedResults.getBounds();
final Rectangle r2 = actualResults.getBounds();
if (r1 != null) {
Assertions.assertNotNull(r2, "Bounds");
Assertions.assertEquals(r1.x, r2.x, "Bounds x");
Assertions.assertEquals(r1.y, r2.y, "Bounds y");
Assertions.assertEquals(r1.width, r2.width, "Bounds width");
Assertions.assertEquals(r1.height, r2.height, "Bounds height");
} else {
Assertions.assertNull(r2, "Bounds");
}
final Calibration c1 = expectedResults.getCalibration();
final Calibration c2 = actualResults.getCalibration();
if (c1 != null) {
Assertions.assertNotNull(c2, "Calibration");
// Be lenient and allow no TimeUnit to match TimeUnit.FRAME
boolean ok = c1.equals(c2);
if (!ok && new CalibrationReader(c1).getTimeUnitValue() == TimeUnit.TIME_UNIT_NA_VALUE) {
switch(fileFormat) {
case BINARY:
case MALK:
case TEXT:
case TSF:
final CalibrationWriter writer = new CalibrationWriter(c1);
writer.setTimeUnit(TimeUnit.FRAME);
ok = writer.getCalibration().equals(c2);
break;
default:
// Do not assume frames for other file formats
break;
}
}
Assertions.assertTrue(ok, "Calibration");
} else {
Assertions.assertNull(c2, "Calibration");
}
final PSF p1 = expectedResults.getPsf();
final PSF p2 = actualResults.getPsf();
if (p1 != null) {
Assertions.assertNotNull(p2, "PSF");
Assertions.assertTrue(p1.equals(p2), "PSF");
} else {
Assertions.assertNull(p2, "PSF");
}
}
Aggregations