use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class AstigmatismModelManager method showConfigurationDialog.
private boolean showConfigurationDialog() {
fitConfig = config.getFitConfiguration();
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("astigmatism-model-manager"));
gd.addMessage("Configuration settings for the single-molecule localisation microscopy plugins");
PeakFit.addCameraOptions(gd, fitConfig);
CalibrationWriter calibration = fitConfig.getCalibrationWriter();
gd.addNumericField("Calibration (nm/px)", calibration.getNmPerPixel(), 2);
// gd.addNumericField("Exposure_time (ms)", calibration.getExposureTime(), 2);
PeakFit.addPsfOptions(gd, fitConfig);
final FitEngineConfigurationProvider provider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
PeakFit.addFittingOptions(gd, provider);
gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
gd.addCheckbox("Log_fit_progress", pluginSettings.getLogFitProgress());
gd.addMessage("--- Peak filtering ---\nDiscard fits that shift; are too low; or expand/contract");
gd.addCheckbox("Smart_filter", fitConfig.isSmartFilter());
gd.addCheckbox("Disable_simple_filter", fitConfig.isDisableSimpleFilter());
gd.addSlider("Shift_factor", 0.01, 2, fitConfig.getCoordinateShiftFactor());
gd.addNumericField("Signal_strength", fitConfig.getSignalStrength(), 2);
gd.addNumericField("Min_photons", fitConfig.getMinPhotons(), 0);
gd.addSlider("Min_width_factor", 0, 0.99, fitConfig.getMinWidthFactor());
// Fitting may need to be extra wide
final double w = fitConfig.getMaxWidthFactor();
gd.addSlider("Width_factor", 1.01, Math.max(10, w), w);
PeakFit.addPrecisionOptions(gd, new PeakFit.SimpleFitConfigurationProvider(fitConfig));
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
// In case a template update the calibration
calibration = fitConfig.getCalibrationWriter();
calibration.setCameraType(SettingsManager.getCameraTypeValues()[gd.getNextChoiceIndex()]);
calibration.setNmPerPixel(gd.getNextNumber());
// Arbitrary
calibration.setExposureTime(100);
fitConfig.setCalibration(calibration.getCalibration());
fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
config.setFitting(gd.getNextNumber());
// Some enum values are not supported
fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
pluginSettings.setLogFitProgress(gd.getNextBoolean());
fitConfig.setSmartFilter(gd.getNextBoolean());
fitConfig.setDisableSimpleFilter(gd.getNextBoolean());
fitConfig.setCoordinateShiftFactor(gd.getNextNumber());
fitConfig.setSignalStrength(gd.getNextNumber());
fitConfig.setMinPhotons(gd.getNextNumber());
fitConfig.setMinWidthFactor(gd.getNextNumber());
fitConfig.setMaxWidthFactor(gd.getNextNumber());
fitConfig.setPrecisionThreshold(gd.getNextNumber());
gd.collectOptions();
// Check arguments
try {
ParameterUtils.isAboveZero("nm per pixel", calibration.getNmPerPixel());
ParameterUtils.isAboveZero("Initial SD0", fitConfig.getInitialXSd());
if (fitConfig.getPsf().getParametersCount() > 1) {
ParameterUtils.isAboveZero("Initial SD1", fitConfig.getInitialYSd());
}
ParameterUtils.isAboveZero("Fitting_width", config.getFitting());
if (!fitConfig.isSmartFilter()) {
ParameterUtils.isPositive("Coordinate Shift factor", fitConfig.getCoordinateShiftFactor());
ParameterUtils.isPositive("Signal strength", fitConfig.getSignalStrength());
ParameterUtils.isPositive("Min photons", fitConfig.getMinPhotons());
ParameterUtils.isPositive("Min width factor", fitConfig.getMinWidthFactor());
ParameterUtils.isPositive("Width factor", fitConfig.getMaxWidthFactor());
ParameterUtils.isPositive("Precision threshold", fitConfig.getPrecisionThreshold());
}
} catch (final IllegalArgumentException ex) {
IJ.error(TITLE, ex.getMessage());
return false;
}
if (gd.invalidNumber()) {
return false;
}
final int flags = PeakFit.FLAG_NO_SAVE;
if (!PeakFit.configureSmartFilter(config, flags)) {
return false;
}
PeakFit.configureFitSolver(config, null, null, flags);
return true;
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method canReadIntoPreferredUnits.
private static void canReadIntoPreferredUnits(RandomSeed seed, ResultsFileFormat fileFormat) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, false, false, false, false, false);
// Output in nm and count
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.NM);
cal.setIntensityUnit(IntensityUnit.COUNT);
if (fileFormat == ResultsFileFormat.TSF) {
// For now just support using the native float TSF value
cal.setNmPerPixel((float) cal.getNmPerPixel());
}
out.setCalibration(cal.getCalibration());
final String filename = createFile();
writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
final MemoryPeakResults in = readFile(filename, false, false);
// Change to preferred units
out.convertToUnits(MemoryPeakResults.PREFERRED_DISTANCE_UNIT, MemoryPeakResults.PREFERRED_INTENSITY_UNIT, MemoryPeakResults.PREFERRED_ANGLE_UNIT);
checkEqual(fileFormat, false, false, false, false, false, false, out, in);
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method createResults.
private static MemoryPeakResults createResults(UniformRandomProvider rg, int size, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory) {
final double bias = rg.nextDouble();
final boolean extended = showEndFrame || showId || showPrecision || showCategory;
final MemoryPeakResults results = new MemoryPeakResults(PsfHelper.create(PSFType.TWO_AXIS_AND_THETA_GAUSSIAN_2D));
while (size-- > 0) {
final int startFrame = rg.nextInt(size + 1);
final int origX = rg.nextInt(256);
final int origY = rg.nextInt(256);
final float origValue = rg.nextFloat();
final double error = rg.nextDouble();
final float noise = rg.nextFloat();
final float meanIntensity = rg.nextFloat();
final float[] params = createData(rg);
final float[] paramsStdDev = (showDeviations) ? createData(rg) : null;
if (extended) {
final AttributePeakResult r = new AttributePeakResult(startFrame, origX, origY, origValue, error, noise, meanIntensity, params, paramsStdDev);
if (showEndFrame) {
r.setEndFrame(startFrame + rg.nextInt(10));
}
if (showId) {
r.setId(size + 1);
}
if (showPrecision) {
r.setPrecision(rg.nextDouble());
}
if (showCategory) {
r.setCategory(size & 64);
}
results.add(r);
} else {
results.add(startFrame, origX, origY, origValue, error, noise, meanIntensity, params, paramsStdDev);
}
}
results.setName(RadixStringSampler.nextBase64String(rg, 16));
results.setConfiguration(RadixStringSampler.nextBase64String(rg, 16));
results.setBounds(new Rectangle(rg.nextInt(10), rg.nextInt(10), rg.nextInt(100), rg.nextInt(100)));
final CalibrationWriter cal = new CalibrationWriter();
cal.setNmPerPixel(rg.nextDouble());
cal.setCountPerPhoton(rg.nextDouble());
cal.setExposureTime(rg.nextDouble());
cal.setReadNoise(rg.nextDouble());
cal.setBias(bias);
cal.setQuantumEfficiency(rg.nextDouble());
// Subtract 1 to avoid the additional UNRECOGNISED enum value
cal.setCameraType(CameraType.values()[rg.nextInt(CameraType.values().length - 1)]);
cal.setDistanceUnit(DistanceUnit.values()[rg.nextInt(DistanceUnit.values().length - 1)]);
cal.setIntensityUnit(IntensityUnit.values()[rg.nextInt(IntensityUnit.values().length - 1)]);
cal.setAngleUnit(AngleUnit.values()[rg.nextInt(AngleUnit.values().length - 1)]);
results.setCalibration(cal.getCalibration());
return results;
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method writeMatchesRead.
// -=-=-=-=-
private static void writeMatchesRead(RandomSeed seed, boolean sequential, ResultsFileFormat fileFormat, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, boolean sort) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, showDeviations, showEndFrame, showId, showPrecision, showCategory);
if (fileFormat == ResultsFileFormat.MALK) {
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.NM);
cal.setIntensityUnit(IntensityUnit.PHOTON);
out.setCalibration(cal.getCalibration());
out.setPsf(PsfHelper.create(PSFType.CUSTOM));
}
if (fileFormat == ResultsFileFormat.TSF) {
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
// For now just support using the native float TSF datatype
cal.setNmPerPixel((float) cal.getNmPerPixel());
out.setCalibration(cal.getCalibration());
// TSF converts the width parameters so make sure they are not zero
out.forEach(new PeakResultProcedure() {
@Override
public void execute(PeakResult peakResult) {
check(peakResult.getParameters());
if (showDeviations) {
check(peakResult.getParameterDeviations());
}
}
private void check(float[] parameters) {
for (int i = 0; i < parameters.length; i++) {
if (parameters[i] == 0) {
parameters[i] = 0.1f;
}
}
}
});
}
// System.out.println(out.getCalibration());
// System.out.println(out.getPSF().toString());
//
// System.out.println(TextFormat.shortDebugString(out.getCalibration()));
//
// try
// {
// Printer printer = JsonFormat.printer()
// .omittingInsignificantWhitespace()
// //.includingDefaultValueFields()
// ;
// System.out.println(printer.print(out.getCalibration()));
// System.out.println(printer.print(out.getPSF()));
// }
// catch (InvalidProtocolBufferException e)
// {
// // This shouldn't happen so throw it
// }
final String filename = createFile();
writeFile(sequential, fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, out, filename);
final MemoryPeakResults in = readFile(filename, false);
checkEqual(fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, out, in);
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method canConvertMalkToNmAndPhotons.
@SeededTest
void canConvertMalkToNmAndPhotons(RandomSeed seed) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, false, false, false, false, false);
// Output in pixel and count
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.PIXEL);
cal.setIntensityUnit(IntensityUnit.COUNT);
out.setCalibration(cal.getCalibration());
out.setPsf(PsfHelper.create(PSFType.CUSTOM));
final String filename = createFile();
writeFile(false, ResultsFileFormat.MALK, false, false, false, false, false, false, out, filename);
final MemoryPeakResults in = readFile(filename, false);
// Change to nm and photon for the validation
out.convertToUnits(DistanceUnit.NM, IntensityUnit.PHOTON, null);
checkEqual(ResultsFileFormat.MALK, false, false, false, false, false, false, out, in);
}
Aggregations