use of uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration in project gdsc-smlm by aherbert.
the class BenchmarkFit method showDialog.
private boolean showDialog() {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
settings = Settings.load();
// Save back by reference
settings.save();
final double sa = getSa();
ImageJUtils.addMessage(gd, "Fits the benchmark image created by CreateData plugin.\nPSF width = %s, adjusted = %s", MathUtils.rounded(benchmarkParameters.sd / benchmarkParameters.pixelPitch), MathUtils.rounded(sa));
final FitEngineConfiguration config = SettingsManager.readFitEngineConfiguration(0);
fitConfig = config.getFitConfiguration();
fitConfig.setNmPerPixel(benchmarkParameters.pixelPitch);
// For each new benchmark width, reset the PSF width to the square pixel adjustment
if (settings.lastId != benchmarkParameters.id) {
settings.lastId = benchmarkParameters.id;
fitConfig.setInitialPeakStdDev(benchmarkParameters.sd / benchmarkParameters.pixelPitch);
// The adjusted width is only relevant when using a single point approximation
// for a Gaussian over the pixel. Using the ERF function computes the actual
// integral over the pixel.
// fitConfig.setInitialPeakStdDev(sa);
// Set the PSF. This requires the CreateData plugin to store the most appropriate
// PSF used for the simulation.
fitConfig.setPsf(benchmarkParameters.psf);
}
gd.addSlider("Region_size", 2, 20, settings.regionSize);
PeakFit.addPsfOptions(gd, fitConfig);
gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
gd.addChoice("Origin_XY", Settings.ORIGIN_XY, settings.originXY, new OptionListener<Integer>() {
@Override
public boolean collectOptions(Integer value) {
settings.originXY = value;
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
if (settings.originXY != 2) {
return false;
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Origin XY");
egd.addNumericField("Offset_X", settings.offsetX, 2, 6, "nm");
egd.addNumericField("Offset_Y", settings.offsetY, 2, 6, "nm");
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
settings.offsetX = gd.getNextNumber();
settings.offsetY = gd.getNextNumber();
return true;
}
});
gd.addChoice("Origin_Z", Settings.ORIGIN_Z, settings.originZ, new OptionListener<Integer>() {
@Override
public boolean collectOptions(Integer value) {
settings.originZ = value;
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
if (settings.originZ != 2) {
return false;
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Origin Z");
egd.addNumericField("Offset_Z", settings.offsetZ, 2, 6, "nm");
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
settings.offsetZ = gd.getNextNumber();
return true;
}
});
gd.addCheckbox("Zero_offset", settings.zeroOffset);
gd.addNumericField("Offset_points", settings.offsetPoints, 0, new OptionListener<Double>() {
@Override
public boolean collectOptions(Double value) {
settings.offsetPoints = Math.max(0, value);
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
if (settings.offsetPoints == 0) {
return false;
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Offset range");
egd.addSlider("Offset_range_x", 0, 2.5, settings.offsetRangeX);
egd.addSlider("Offset_range_y", 0, 2.5, settings.offsetRangeY);
egd.addSlider("Offset_range_z", 0, 2.5, settings.offsetRangeZ);
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
settings.offsetRangeX = Math.max(0, egd.getNextNumber());
settings.offsetRangeY = Math.max(0, egd.getNextNumber());
settings.offsetRangeZ = Math.max(0, egd.getNextNumber());
return true;
}
});
gd.addCheckbox("Background_fitting", settings.backgroundFitting, new OptionListener<Boolean>() {
@Override
public boolean collectOptions(Boolean value) {
settings.backgroundFitting = value;
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
if (!settings.backgroundFitting) {
return false;
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Background fitting");
egd.addCheckbox("Estimate_background", settings.estimateBackground);
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
settings.estimateBackground = egd.getNextBoolean();
return true;
}
});
gd.addMessage("Signal fitting can be disabled for " + PsfProtosHelper.getName(PSFType.ONE_AXIS_GAUSSIAN_2D) + " function");
gd.addCheckbox("Signal_fitting", settings.signalFitting, new OptionListener<Boolean>() {
@Override
public boolean collectOptions(Boolean value) {
settings.signalFitting = value;
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
if (!settings.signalFitting) {
return false;
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Signal fitting");
egd.addCheckbox("Estimate_signal", settings.estimateSignal);
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
settings.estimateSignal = egd.getNextBoolean();
return true;
}
});
gd.addCheckbox("Show_histograms", settings.showHistograms);
gd.addCheckbox("Save_raw_data", settings.saveRawData);
gd.addHelp(HelpUrls.getUrl("fit-benchmark-data"));
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
settings.regionSize = (int) Math.abs(gd.getNextNumber());
fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
// Some enum values are not supported
fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
settings.originXY = gd.getNextChoiceIndex();
settings.originZ = gd.getNextChoiceIndex();
settings.zeroOffset = gd.getNextBoolean();
settings.offsetPoints = Math.max(0, gd.getNextNumber());
settings.backgroundFitting = gd.getNextBoolean();
settings.signalFitting = gd.getNextBoolean();
settings.showHistograms = gd.getNextBoolean();
settings.saveRawData = gd.getNextBoolean();
gd.collectOptions();
// Do this before the call to is3D()
if (!PeakFit.configurePsfModel(config)) {
return false;
}
getStartPoints(fitConfig.is3D());
if (startPoints.length == 0) {
IJ.error(TITLE, "No initial fitting positions");
return false;
}
if (settings.regionSize < 1) {
settings.regionSize = 1;
}
if (gd.invalidNumber()) {
return false;
}
// Initialise the correct calibration
final CalibrationWriter calibration = new CalibrationWriter(fitConfig.getCalibration());
calibration.setNmPerPixel(benchmarkParameters.pixelPitch);
calibration.setCountPerPhoton(benchmarkParameters.gain);
calibration.setQuantumEfficiency(benchmarkParameters.qe);
calibration.setBias(benchmarkParameters.bias);
calibration.setCameraType(benchmarkParameters.cameraType);
calibration.setReadNoise(benchmarkParameters.readNoise);
calibration.setExposureTime(1000);
fitConfig.setCalibration(calibration.getCalibration());
fitConfig.setCameraModelName(benchmarkParameters.cameraModelName);
if (!PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, 0)) {
return false;
}
if (settings.showHistograms) {
final ExtendedGenericDialog gd2 = new ExtendedGenericDialog(TITLE);
gd2.addMessage("Select the histograms to display");
gd2.addNumericField("Histogram_bins", settings.histogramBins, 0);
final double[] convert = getConversionFactors();
for (int i = 0; i < DISPLAY_HISTOGRAMS.length; i++) {
if (convert[i] != 0) {
gd2.addCheckbox(NAMES[i].replace(' ', '_'), DISPLAY_HISTOGRAMS[i]);
}
}
gd2.showDialog();
if (gd2.wasCanceled()) {
return false;
}
settings.histogramBins = (int) Math.abs(gd2.getNextNumber());
for (int i = 0; i < DISPLAY_HISTOGRAMS.length; i++) {
if (convert[i] != 0) {
DISPLAY_HISTOGRAMS[i] = gd2.getNextBoolean();
}
}
}
return true;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration in project gdsc-smlm by aherbert.
the class BenchmarkFilterAnalysis method createResults.
/**
* Create peak results.
*
* @param filterResults The results from running the filter (or null)
* @param filter the filter
* @param withBorder true to use the border of the spot filter
* @return the memory peak results
*/
MemoryPeakResults createResults(PreprocessedPeakResult[] filterResults, DirectFilter filter, boolean withBorder) {
if (filterResults == null) {
final MultiPathFilter multiPathFilter = createMpf(filter, DEFAULT_MINIMUM_FILTER);
filterResults = filterResults(multiPathFilter);
}
final MemoryPeakResults newResults = new MemoryPeakResults();
newResults.copySettings(this.results);
newResults.setName(TITLE);
if (withBorder) {
// To produce the same results as the PeakFit plugin we must implement the border
// functionality used in the FitWorker. This respects the border of the spot filter.
final FitEngineConfiguration config = FitEngineConfiguration.create();
updateAllConfiguration(config);
final MaximaSpotFilter spotFilter = config.createSpotFilter();
final int border = spotFilter.getBorder();
final Rectangle bounds = getBounds();
final int borderLimitX = bounds.x + bounds.width - border;
final int borderLimitY = bounds.y + bounds.height - border;
for (final PreprocessedPeakResult spot : filterResults) {
if (spot.getX() > border && spot.getX() < borderLimitX && spot.getY() > border && spot.getY() < borderLimitY) {
final double[] p = spot.toGaussian2DParameters();
final float[] params = new float[p.length];
for (int j = 0; j < p.length; j++) {
params[j] = (float) p[j];
}
final int frame = spot.getFrame();
final int origX = (int) p[Gaussian2DFunction.X_POSITION];
final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
}
}
} else {
for (final PreprocessedPeakResult spot : filterResults) {
final double[] p = spot.toGaussian2DParameters();
final float[] params = new float[p.length];
for (int j = 0; j < p.length; j++) {
params[j] = (float) p[j];
}
final int frame = spot.getFrame();
final int origX = (int) p[Gaussian2DFunction.X_POSITION];
final int origY = (int) p[Gaussian2DFunction.Y_POSITION];
newResults.add(frame, origX, origY, 0, 0, spot.getNoise(), spot.getMeanSignal(), params, null);
}
}
return newResults;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration in project GDSC-SMLM by aherbert.
the class SpotFinderPreview method showDialog.
@Override
public int showDialog(ImagePlus imp, String command, PlugInFilterRunner pfr) {
settings = Settings.load();
this.overlay = imp.getOverlay();
this.imp = imp;
// Saved by reference so do it once now
settings.save();
// The image is locked by the PlugInFilterRunner so unlock it to allow scroll.
// This should be OK as the image data is not modified and only the overlay is
// adjusted. If another plugin changes the image then the preview should update
// the overlay and it will be obvious to the user to turn this plugin off.
imp.unlock();
config = SettingsManager.readFitEngineConfiguration(0);
fitConfig = config.getFitConfiguration();
gd = new NonBlockingExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("spot-finder-preview"));
gd.addMessage("Preview candidate maxima");
final String[] templates = ConfigurationTemplate.getTemplateNames(true);
gd.addChoice("Template", templates, templates[0]);
final String[] models = CameraModelManager.listCameraModels(true);
gd.addChoice("Camera_model_name", models, fitConfig.getCameraModelName());
PeakFit.addPsfOptions(gd, (FitConfigurationProvider) () -> fitConfig);
final PeakFit.SimpleFitEngineConfigurationProvider provider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
PeakFit.addDataFilterOptions(gd, provider);
gd.addChoice("Spot_filter_2", SettingsManager.getDataFilterMethodNames(), config.getDataFilterMethod(1, settings.defaultDataFilterMethod).ordinal());
PeakFit.addRelativeParameterOptions(gd, new RelativeParameterProvider(2.5, 4.5, "Smoothing_2", provider) {
@Override
void setAbsolute(boolean absolute) {
final FitEngineConfiguration c = fitEngineConfigurationProvider.getFitEngineConfiguration();
final DataFilterMethod m = c.getDataFilterMethod(1, settings.defaultDataFilterMethod);
final double smooth = c.getDataFilterParameterValue(1, settings.defaultSmooth);
c.setDataFilter(m, smooth, absolute, 1);
}
@Override
boolean isAbsolute() {
return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterAbsolute(1, false);
}
@Override
double getValue() {
return fitEngineConfigurationProvider.getFitEngineConfiguration().getDataFilterParameterValue(1, settings.defaultSmooth);
}
});
PeakFit.addSearchOptions(gd, provider);
PeakFit.addBorderOptions(gd, provider);
// Find if this image was created with ground truth data
if (imp.getID() == CreateData.getImageId()) {
final MemoryPeakResults results = CreateData.getResults();
if (results != null) {
gd.addSlider("Match_distance", 0, 2.5, settings.distance);
gd.addSlider("Lower_match_distance (%)", 0, 100, settings.lowerDistance);
gd.addCheckbox("Multiple_matches", settings.multipleMatches);
gd.addCheckbox("Show_TP", settings.showTP);
gd.addCheckbox("Show_FP", settings.showFP);
gd.addMessage("");
label = (Label) gd.getMessage();
final boolean integerCoords = false;
actualCoordinates = ResultsMatchCalculator.getCoordinates(results, integerCoords);
}
}
if (label == null) {
// If no ground truth data add options to show the spots by their rank
// and number of neighbours
gd.addSlider("Top_N", 0, 100, settings.topN);
topNScrollBar = gd.getLastScrollbar();
gd.addSlider("Select", 0, 100, settings.select);
selectScrollBar = gd.getLastScrollbar();
gd.addSlider("Neigbour_radius", 0, 10, settings.neighbourRadius);
}
ImageListener imageListener = null;
if (ImageJUtils.isShowGenericDialog()) {
// Listen for changes in the dialog options
gd.addOptionCollectedListener(event -> {
// Just run on the current processor
if (preview) {
run(imp.getProcessor());
}
});
// Listen for changes to an image
imageListener = new ImageAdapter() {
@Override
public void imageUpdated(ImagePlus imp) {
if (SpotFinderPreview.this.imp.getID() == imp.getID() && preview && imp.getCurrentSlice() != currentSlice && filter != null) {
run(imp.getProcessor(), filter);
}
}
};
ImagePlus.addImageListener(imageListener);
// Support template settings
final Vector<TextField> numerics = gd.getNumericFields();
final Vector<Choice> choices = gd.getChoices();
final Iterator<TextField> nu = numerics.iterator();
final Iterator<Choice> ch = choices.iterator();
final Choice textTemplate = ch.next();
textTemplate.removeItemListener(gd);
textTemplate.removeKeyListener(gd);
textTemplate.addItemListener(this::itemStateChanged);
textCameraModelName = ch.next();
textPsf = ch.next();
textDataFilterType = ch.next();
textDataFilterMethod = ch.next();
textSmooth = nu.next();
textDataFilterMethod2 = ch.next();
textSmooth2 = nu.next();
textSearch = nu.next();
textBorder = nu.next();
}
gd.addPreviewCheckbox(pfr);
gd.addDialogListener(this::dialogItemChanged);
gd.setOKLabel("Save");
gd.setCancelLabel("Close");
gd.showDialog();
if (imageListener != null) {
ImagePlus.removeImageListener(imageListener);
}
if (!gd.wasCanceled() && !SettingsManager.writeSettings(config, SettingsManager.FLAG_SILENT)) {
IJ.error(TITLE, "Failed to save settings");
}
// Reset
imp.setOverlay(overlay);
return DONE;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration in project GDSC-SMLM by aherbert.
the class ConfigurationTemplate method createInlineTemplates.
private static Map<String, Template> createInlineTemplates() {
final Map<String, Template> inlineTemplates = new LinkedHashMap<>();
// Add the default.
// This will be a simple least squares estimator applicable to non-calibrated data.
final FitEngineConfiguration config = new FitEngineConfiguration();
addInlineTemplate(inlineTemplates, "Default", config);
return inlineTemplates;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitEngineConfiguration in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method updateConfiguration.
/**
* Updates the given configuration using the latest settings used in benchmarking.
*
* @param targetConfiguration the configuration
* @return true, if successful
*/
public static boolean updateConfiguration(FitEngineConfiguration targetConfiguration) {
final FitConfiguration targetFitConfiguration = targetConfiguration.getFitConfiguration();
// Q. Why are the settings set to themselves?
// Removed this for now.
// targetFitConfiguration.setPsf(targetFitConfiguration.getPsf());
// targetFitConfiguration.setFitSolverSettings(targetFitConfiguration.getFitSolverSettings());
// targetFitConfiguration.setFilterSettings(targetFitConfiguration.getFilterSettings());
final FitEngineConfiguration sourceConfig = Settings.lastSettings.get().config;
final FitConfiguration sourceFitConfiguration = sourceConfig.getFitConfiguration();
// Set the fit engine settings manually to avoid merging all child settings
// i.e. do not do a global update using:
// targetConfiguration.setFitEngineSettings(sourceConfig.getFitEngineSettings());
targetFitConfiguration.setPsf(sourceFitConfiguration.getPsf());
targetFitConfiguration.setFitSolverSettings(sourceFitConfiguration.getFitSolverSettings());
targetFitConfiguration.setFilterSettings(sourceFitConfiguration.getFilterSettings());
targetConfiguration.setFitting(sourceConfig.getFitting());
targetConfiguration.setIncludeNeighbours(sourceConfig.isIncludeNeighbours());
targetConfiguration.setNeighbourHeightThreshold(sourceConfig.getNeighbourHeightThreshold());
targetConfiguration.setDuplicateDistance(sourceConfig.getDuplicateDistance());
targetConfiguration.setDuplicateDistanceAbsolute(sourceConfig.getDuplicateDistanceAbsolute());
if (getComputeDoublets()) {
targetConfiguration.setResidualsThreshold(0);
targetFitConfiguration.setComputeResiduals(true);
} else {
targetConfiguration.setResidualsThreshold(1);
targetFitConfiguration.setComputeResiduals(false);
}
// We used simple filtering.
targetFitConfiguration.setSmartFilter(false);
return true;
}
Aggregations