use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.
the class DoubletAnalysis method showDialog.
/**
* Show dialog.
*
* @return true, if successful
*/
private boolean showDialog() {
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
final String helpKey = "doublet-analysis";
settings = Settings.load();
config = configRef.get().createCopy();
final FitConfiguration fitConfig = config.getFitConfiguration();
final double sa = getSa();
ImageJUtils.addMessage(gd, "Fits the benchmark image created by CreateData plugin.\nPSF width = %s, adjusted = %s", MathUtils.rounded(simulationParameters.sd / simulationParameters.pixelPitch), MathUtils.rounded(sa));
// For each new benchmark width, reset the PSF width to the square pixel adjustment
if (lastId.get() != simulationParameters.id) {
final double w = sa;
settings.matchDistance = w * Gaussian2DFunction.SD_TO_HWHM_FACTOR;
settings.lowerDistance = 0.5 * settings.matchDistance;
fitConfig.setInitialPeakStdDev(w);
final CalibrationWriter cal = new CalibrationWriter(fitConfig.getCalibration());
cal.setNmPerPixel(simulationParameters.pixelPitch);
cal.setCountPerPhoton(simulationParameters.gain);
cal.setQuantumEfficiency(simulationParameters.qe);
cal.setExposureTime(100);
cal.setReadNoise(simulationParameters.readNoise);
cal.setBias(simulationParameters.bias);
cal.setCameraType(simulationParameters.cameraType);
fitConfig.setCameraModel(CreateData.getCameraModel(simulationParameters));
fitConfig.setCalibration(cal.getCalibration());
}
// Support for using templates
final String[] templates = ConfigurationTemplate.getTemplateNames(true);
gd.addChoice("Template", templates, templates[0]);
// Allow the settings from the benchmark analysis to be used
gd.addCheckbox("Benchmark_settings", settings.useBenchmarkSettings);
// Collect options for fitting
PeakFit.addPsfOptions(gd, fitConfig);
final PeakFit.SimpleFitEngineConfigurationProvider provider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
PeakFit.addDataFilterOptions(gd, provider);
PeakFit.addSearchOptions(gd, provider);
PeakFit.addBorderOptions(gd, provider);
PeakFit.addFittingOptions(gd, provider);
gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), fitConfig.getFitSolver().ordinal());
gd.addSlider("Iteration_increase", 1, 4.5, settings.iterationIncrease);
gd.addCheckbox("Ignore_with_neighbours", settings.ignoreWithNeighbours);
gd.addCheckbox("Show_overlay", settings.showOverlay);
gd.addCheckbox("Show_histograms", settings.showHistograms);
gd.addCheckbox("Show_results", settings.showResults);
gd.addCheckbox("Show_Jaccard_Plot", settings.showJaccardPlot);
gd.addCheckbox("Use_max_residuals", settings.useMaxResiduals);
gd.addNumericField("Match_distance", settings.matchDistance, 2);
gd.addNumericField("Lower_distance", settings.lowerDistance, 2);
gd.addNumericField("Signal_factor", settings.signalFactor, 2);
gd.addNumericField("Lower_factor", settings.lowerSignalFactor, 2);
gd.addChoice("Matching", Settings.MATCHING_METHODS, settings.matchingMethod);
// Add a mouse listener to the config file field
if (ImageJUtils.isShowGenericDialog()) {
final Vector<TextField> numerics = gd.getNumericFields();
final Vector<Choice> choices = gd.getChoices();
final Iterator<TextField> nu = numerics.iterator();
final Iterator<Choice> ch = choices.iterator();
ch.next().addItemListener(this);
final Checkbox b = (Checkbox) gd.getCheckboxes().get(0);
b.addItemListener(this);
textPsf = ch.next();
textDataFilterType = ch.next();
textDataFilter = ch.next();
textSmooth = nu.next();
textSearch = nu.next();
textBorder = nu.next();
textFitting = nu.next();
textFitSolver = ch.next();
// Iteration increase
nu.next();
textMatchDistance = nu.next();
textLowerDistance = nu.next();
textSignalFactor = nu.next();
textLowerFactor = nu.next();
}
gd.addHelp(HelpUrls.getUrl(helpKey));
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
// Ignore the template
gd.getNextChoice();
settings.useBenchmarkSettings = gd.getNextBoolean();
fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
config.setDataFilterType(gd.getNextChoiceIndex());
config.setDataFilter(gd.getNextChoiceIndex(), Math.abs(gd.getNextNumber()), false, 0);
config.setSearch(gd.getNextNumber());
config.setBorder(gd.getNextNumber());
config.setFitting(gd.getNextNumber());
// Some enum values are not supported
fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
// Avoid stupidness. Note: We are mostly ignoring the validation result and
// checking the results for the doublets manually.
// Realistically we cannot fit lower than this
fitConfig.setMinPhotons(15);
// Set the width factors to help establish bounds for bounded fitters
fitConfig.setMinWidthFactor(1.0 / 10);
fitConfig.setMaxWidthFactor(10);
settings.iterationIncrease = gd.getNextNumber();
settings.ignoreWithNeighbours = gd.getNextBoolean();
settings.showOverlay = gd.getNextBoolean();
settings.showHistograms = gd.getNextBoolean();
settings.showResults = gd.getNextBoolean();
settings.showJaccardPlot = gd.getNextBoolean();
settings.useMaxResiduals = gd.getNextBoolean();
settings.matchDistance = Math.abs(gd.getNextNumber());
settings.lowerDistance = Math.abs(gd.getNextNumber());
settings.signalFactor = Math.abs(gd.getNextNumber());
settings.lowerSignalFactor = Math.abs(gd.getNextNumber());
settings.matchingMethod = gd.getNextChoiceIndex();
gd.collectOptions();
settings.save();
configRef.set(config);
if (gd.invalidNumber()) {
return false;
}
if (settings.lowerDistance > settings.matchDistance) {
settings.lowerDistance = settings.matchDistance;
}
if (settings.lowerSignalFactor > settings.signalFactor) {
settings.lowerSignalFactor = settings.signalFactor;
}
if (settings.useBenchmarkSettings && !updateFitConfiguration(config)) {
return false;
}
boolean configure = true;
if (settings.useBenchmarkSettings) {
// Only configure the fit solver if not in a macro
configure = Macro.getOptions() == null;
}
if (configure && !PeakFit.configurePsfModel(config)) {
return false;
}
if (configure && !PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, PeakFit.FLAG_NO_SAVE)) {
return false;
}
lastId.set(simulationParameters.id);
if (settings.showHistograms) {
gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Select the histograms to display");
for (int i = 0; i < Settings.NAMES.length; i++) {
gd.addCheckbox(Settings.NAMES[i].replace(' ', '_'), settings.displayHistograms[i]);
}
for (int i = 0; i < Settings.NAMES2.length; i++) {
gd.addCheckbox(Settings.NAMES2[i].replace(' ', '_'), settings.displayHistograms[i + Settings.NAMES.length]);
}
gd.addHelp(HelpUrls.getUrl(helpKey));
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
for (int i = 0; i < settings.displayHistograms.length; i++) {
settings.displayHistograms[i] = gd.getNextBoolean();
}
}
return true;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.
the class SettingsManager method writeSettings.
/**
* Write to a settings file in the settings directory.
*
* @param fitEngineConfiguration the fit engine configuration
* @param flags the flags
* @return true, if successful
*/
public static boolean writeSettings(FitEngineConfiguration fitEngineConfiguration, int flags) {
final FitConfiguration fitConfig = fitEngineConfiguration.getFitConfiguration();
// This is fail fast
boolean result = writeSettings(fitEngineConfiguration.getFitEngineSettings(), flags);
result &= writeSettings(fitConfig.getCalibration(), flags);
result &= writeSettings(fitConfig.getPsf(), flags);
return result;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.
the class ConfigurationTemplateTest method canCreateResourceTemplate.
@Test
void canCreateResourceTemplate() throws IOException {
// This is not really a test.
// It creates some templates so that they can be put in the resources folder.
final File tmpFile = File.createTempFile("template", ".json.txt");
tmpFile.deleteOnExit();
final FitEngineConfiguration config = new FitEngineConfiguration();
final FitConfiguration fitConfig = config.getFitConfiguration();
fitConfig.setMaxIterations(150);
fitConfig.setMinWidthFactor(0.2);
fitConfig.setMinWidthFactor(5);
fitConfig.setSmartFilter(true);
fitConfig.setDirectFilter(new MultiFilter2(0, 22, 0.56, 2.55, 3.3, 0, 31, 0, 0));
config.setSearch(0.607);
config.setBorder(1);
config.setFitting(3);
config.setFailuresLimit(3);
config.setIncludeNeighbours(true);
config.setNeighbourHeightThreshold(0.3);
config.setResidualsThreshold(0.4);
config.setDataFilterType(DataFilterType.SINGLE);
config.setDataFilter(DataFilterMethod.GAUSSIAN, 0.425, false, 0);
// System.out.println(tmpFile.getPath());
final TemplateSettings.Builder builder = TemplateSettings.newBuilder();
builder.setFitEngineSettings(config.getFitEngineSettings());
Assertions.assertTrue(SettingsManager.toJson(builder.build(), tmpFile, SettingsManager.FLAG_JSON_WHITESPACE));
}
use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.
the class PeakFit method configurePsfModel.
/**
* Show a dialog to configure the PSF model. The updated settings are saved to the settings file.
*
* <p>If the configuration is for a 3D PSF then a dialog to configure the z model is shown.
*
* @param config the config
* @param flags the flags
* @return true, if successful
*/
public static boolean configurePsfModel(FitEngineConfiguration config, int flags) {
final FitConfiguration fitConfig = config.getFitConfiguration();
if (fitConfig.getPsfTypeValue() != PSFType.ASTIGMATIC_GAUSSIAN_2D_VALUE) {
return true;
}
// Get the astigmatism z-model
final AstigmatismModel model = AstigmatismModelManager.getModel(fitConfig.getPsfModelName());
if (model == null) {
IJ.error(TITLE, "Failed to load the model: " + fitConfig.getPsfModelName());
return false;
}
// Conversion to the correct units in pixels is done within the FitConfiguration object.
fitConfig.setAstigmatismModel(model);
if (BitFlagUtils.anyNotSet(flags, FLAG_NO_SAVE)) {
SettingsManager.writeSettings(config, 0);
}
return true;
}
use of uk.ac.sussex.gdsc.smlm.engine.FitConfiguration in project GDSC-SMLM by aherbert.
the class PeakFit method updateFitConfiguration.
/**
* Updates the configuration for peak fitting. Configures the calculation of residuals, logging
* and peak validation.
*
* @param config the config
* @return true, if successful
*/
private boolean updateFitConfiguration(FitEngineConfiguration config) {
final FitConfiguration localFitConfig = config.getFitConfiguration();
// Adjust the settings that are relevant within the fitting configuration.
localFitConfig.setComputeResiduals(config.getResidualsThreshold() < 1);
logger = (resultsSettings.getLogProgress()) ? ImageJPluginLoggerHelper.getLogger(getClass()) : null;
localFitConfig.setLog(logger);
if (resultsSettings.getShowDeviations()) {
// Note: This may already by true if the deviations are needed for the smart filter
localFitConfig.setComputeDeviations(resultsSettings.getShowDeviations());
}
config.configureOutputUnits();
return checkCameraModel(localFitConfig, source.getBounds(), bounds, true);
}
Aggregations