use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakFit method showSimpleDialog.
private int showSimpleDialog() {
// Just support circular fitting
fitConfig.setPsf(PsfProtosHelper.defaultOneAxisGaussian2DPSF);
fitConfig.setFixedPsf(false);
// TODO - Support sCMOS camera. This may be 'too difficult' as the
// user will need to have created a per-pixel calibration image
final CalibrationWriter calibration = fitConfig.getCalibrationWriter();
final boolean requireCalibration = requireCalibration(calibration);
if (requireCalibration && !showCalibrationWizard(calibration, true)) {
return DONE;
}
// Present dialog with simple output options: Image, Table
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("simple-fit"));
gd.addMessage("Fit single-molecule localisations");
if (!requireCalibration) {
gd.addCheckbox("Use_current_calibration", true);
}
gd.addCheckbox("Show_table", settings.showTable);
gd.addCheckbox("Show_image", settings.showImage);
gd.showDialog();
if (gd.wasCanceled()) {
return DONE;
}
boolean useCurrentCalibration = true;
if (!requireCalibration) {
useCurrentCalibration = gd.getNextBoolean();
}
settings.showTable = gd.getNextBoolean();
settings.showImage = gd.getNextBoolean();
if (!useCurrentCalibration && !showCalibrationWizard(calibration, false)) {
return DONE;
}
// Restore fitting to default settings but maintain the calibrated width
final double sd = fitConfig.getInitialXSd();
config = new FitEngineConfiguration();
fitConfig = config.getFitConfiguration();
fitConfig.setInitialPeakStdDev(sd);
// Allow to move 1 SD
fitConfig.setCoordinateShiftFactor(1);
resultsSettings = ResultsSettings.newBuilder();
// Do simple results output. We only need to set non-default values.
resultsSettings.getResultsInMemorySettingsBuilder().setInMemory(true);
if (settings.showTable) {
final ResultsTableSettings.Builder tableSettings = resultsSettings.getResultsTableSettingsBuilder();
tableSettings.setShowTable(true);
}
if (settings.showImage) {
final ResultsImageSettings.Builder imageSettings = resultsSettings.getResultsImageSettingsBuilder();
imageSettings.setImageType(ResultsImageType.DRAW_INTENSITY);
imageSettings.setScale(Math.ceil(1024.0 / Math.max(bounds.width, bounds.height)));
imageSettings.setWeighted(true);
imageSettings.setEqualised(true);
}
// Log the settings we care about:
IJ.log(LOG_SPACER);
IJ.log("Peak Fit");
IJ.log(LOG_SPACER);
ImageJUtils.log("Pixel pitch = %s", MathUtils.rounded(calibration.getNmPerPixel(), 4));
ImageJUtils.log("Exposure Time = %s", MathUtils.rounded(calibration.getExposureTime(), 4));
ImageJUtils.log("PSF width = %s", MathUtils.rounded(fitConfig.getInitialXSd(), 4));
// Save
fitConfig.setCalibration(calibration.getCalibration());
saveFitEngineSettings();
SettingsManager.writeSettings(resultsSettings.build());
return FLAGS;
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class FitConfiguration method init.
/**
* Initialise the instance.
*
* @param fitSettings the fit settings
* @param calibration the calibration
* @param psf the psf
*/
private void init(FitSettings.Builder fitSettings, Calibration.Builder calibration, PSF.Builder psf) {
this.fitSettings = fitSettings;
// Extract for convenience
this.calibration = new CalibrationWriter(calibration);
this.psf = psf;
fitSolverSettings = fitSettings.getFitSolverSettingsBuilder();
filterSettings = fitSettings.getFilterSettingsBuilder();
initialiseState();
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter 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.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method writeTextWithComputedPrecisionMatchesRead.
@SeededTest
void writeTextWithComputedPrecisionMatchesRead(RandomSeed seed) {
// Create without precision
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults results = createResults(rg, 200, false, false, false, false, false);
// Ensure units are OK for computing precision
final CalibrationWriter cw = results.getCalibrationWriter();
cw.setIntensityUnit(IntensityUnit.PHOTON);
cw.setDistanceUnit(DistanceUnit.PIXEL);
results.setCalibration(cw.getCalibration());
final String filename = createFile();
final TextFilePeakResults out = new TextFilePeakResults(filename, false, false, false, true);
// Compute precision
out.setComputePrecision(true);
out.copySettings(results);
out.begin();
out.addAll(Arrays.asList(results.toArray()));
out.end();
final MemoryPeakResults in = readFile(filename, false);
checkEqual(ResultsFileFormat.TEXT, false, false, false, true, false, false, results, in);
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter 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