use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.
the class AstigmatismModelManager method viewModel.
private void viewModel() {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
final String[] models = listAstigmatismModels(false);
gd.addChoice("Model", models, pluginSettings.getSelected());
gd.addChoice("z_distance_unit", SettingsManager.getDistanceUnitNames(), pluginSettings.getZDistanceUnitValue());
gd.addChoice("s_distance_unit", SettingsManager.getDistanceUnitNames(), pluginSettings.getSDistanceUnitValue());
gd.addCheckbox("Show_depth_of_focus", pluginSettings.getShowDepthOfFocus());
gd.addCheckbox("Show_combined_width", pluginSettings.getShowCombinedWidth());
gd.addCheckbox("Show_PSF", pluginSettings.getShowPsf());
gd.addHelp(HelpUrls.getUrl("astigmatism-model-manager-view"));
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
final String name = gd.getNextChoice();
pluginSettings.setSelected(name);
pluginSettings.setZDistanceUnitValue(gd.getNextChoiceIndex());
pluginSettings.setSDistanceUnitValue(gd.getNextChoiceIndex());
pluginSettings.setShowDepthOfFocus(gd.getNextBoolean());
pluginSettings.setShowCombinedWidth(gd.getNextBoolean());
pluginSettings.setShowPsf(gd.getNextBoolean());
// Try and get the named resource
AstigmatismModel model = AstigmatismModelSettingsHolder.getSettings().getAstigmatismModelResourcesMap().get(name);
if (model == null) {
IJ.error(TITLE, "Failed to find astigmatism model: " + name);
return;
}
try {
model = convert(model, pluginSettings.getZDistanceUnit(), pluginSettings.getSDistanceUnit());
} catch (final ConversionException ex) {
ImageJUtils.log("Bad conversion (%s), defaulting to native model units", ex.getMessage());
}
ImageJUtils.log("Astigmatism model: %s\n%s", name, model);
// Plot the curve. Do this so we encompass twice the depth-of-field.
final double gamma = model.getGamma();
final double d = model.getD();
final double s0x = model.getS0X();
final double Ax = model.getAx();
final double Bx = model.getBx();
final double s0y = model.getS0Y();
final double Ay = model.getAy();
final double By = model.getBy();
final double range = Math.abs(gamma) + 1.5 * d;
final int n = 200;
final double step = range / n;
final double[] z = new double[2 * n + 1];
final double[] sx = new double[z.length];
final double[] sy = new double[z.length];
// Use the same class that is used during fitting
final HoltzerAstigmatismZModel m = HoltzerAstigmatismZModel.create(s0x, s0y, gamma, d, Ax, Bx, Ay, By);
for (int i = 0; i < z.length; i++) {
final double zz = -range + i * step;
z[i] = zz;
sx[i] = m.getSx(zz);
sy[i] = m.getSy(zz);
}
final String title = TITLE + " Width Curve";
final Plot plot = new Plot(title, "Z (" + UnitHelper.getShortName(model.getZDistanceUnit()) + ")", "Width (" + UnitHelper.getShortName(model.getSDistanceUnit()) + ")");
double[] limits = MathUtils.limits(sx);
limits = MathUtils.limits(limits, sy);
final double rangex = (z[z.length - 1] - z[0]) * 0.05;
final double rangey = (limits[1] - limits[0]) * 0.05;
final double miny = limits[0] - rangey;
final double maxy = limits[1] + rangey;
plot.setLimits(z[0] - rangex, z[z.length - 1] + rangex, miny, maxy);
plot.setColor(Color.RED);
plot.addPoints(z, sx, Plot.LINE);
plot.setColor(Color.BLUE);
plot.addPoints(z, sy, Plot.LINE);
plot.setColor(Color.YELLOW);
if (pluginSettings.getShowDepthOfFocus()) {
final double z0x = gamma;
final double z0y = -gamma;
plot.setColor(Color.RED.darker());
plot.drawDottedLine(z0x - d, miny, z0x - d, maxy, 4);
plot.drawDottedLine(z0x + d, miny, z0x + d, maxy, 4);
plot.setColor(Color.BLUE.darker());
plot.drawDottedLine(z0y - d, miny, z0y - d, maxy, 4);
plot.drawDottedLine(z0y + d, miny, z0y + d, maxy, 4);
}
String legend = "Sx\nSy";
if (pluginSettings.getShowCombinedWidth()) {
final double[] s = new double[z.length];
for (int i = 0; i < z.length; i++) {
s[i] = Gaussian2DPeakResultHelper.getStandardDeviation(sx[i], sy[i]);
}
plot.setColor(Color.GREEN);
plot.addPoints(z, s, Plot.LINE);
legend += "\tS";
}
plot.setColor(Color.BLACK);
plot.addLegend(legend);
plot.addLabel(0, 0, String.format("Model = %s (%s nm/pixel)", name, MathUtils.rounded(model.getNmPerPixel())));
ImageJUtils.display(title, plot);
if (!pluginSettings.getShowPsf()) {
return;
}
// Get pixel range using 3x[max SD]
final int width = 1 + 2 * ((int) Math.ceil(limits[1] * 3));
new ModelRenderer(name, model, m, range, width, plot).run();
}
use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog 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.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.
the class AstigmatismModelManager method save.
private boolean save(String name, AstigmatismModel.Builder model) {
pluginSettings.setModelName(name);
// Check existing names
final AstigmatismModelSettings settings = AstigmatismModelSettingsHolder.getSettings();
final Map<String, AstigmatismModel> map = settings.getAstigmatismModelResourcesMap();
if (map.containsKey(name)) {
name = suggest(map, name);
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Model name " + pluginSettings.getModelName() + " already exists.\n \nSuggest renaming to:");
gd.addStringField("Model_name", name);
gd.enableYesNoCancel("Rename", "Overwrite");
gd.showDialog(true);
if (gd.wasCanceled()) {
return false;
}
if (gd.wasOKed()) {
// Rename
pluginSettings.setModelName(name);
}
}
// Save the model
if (!AstigmatismModelSettingsHolder.setSettings(settings.toBuilder().putAstigmatismModelResources(pluginSettings.getModelName(), model.build()).build())) {
IJ.error(TITLE, "Failed to save the model");
return false;
}
return true;
}
use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.
the class CalibrateResults method showInputDialog.
private boolean showInputDialog() {
final int size = MemoryPeakResults.countMemorySize();
if (size == 0) {
IJ.error(TITLE, "There are no fitting results in memory");
return false;
}
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("calibrate-results"));
gd.addMessage("Select results to calibrate");
settings = Settings.load();
ResultsManager.addInput(gd, settings.inputOption, InputSource.MEMORY);
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
settings.inputOption = ResultsManager.getInputSource(gd);
settings.save();
return true;
}
use of uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog in project GDSC-SMLM by aherbert.
the class CameraModelFisherInformationAnalysis method plotFromCache.
private static void plotFromCache() {
// Build a list of curve stored in the cache
final String[] names = new String[cache.size()];
final PoissonFisherInformationData[] datas = new PoissonFisherInformationData[cache.size()];
int count = 0;
for (final Entry<FiKey, PoissonFisherInformationData> e : cache.entrySet()) {
names[count] = e.getKey().toString();
datas[count] = e.getValue();
count++;
}
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addChoice("Fisher_information", names, cachePlot);
gd.addChoice("Plot_point", POINT_OPTION, pointOption);
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
final int index = gd.getNextChoiceIndex();
pointOption = gd.getNextChoiceIndex();
cachePlot = names[index];
final PoissonFisherInformationData data = datas[index];
final FiKey key = new FiKey(data);
count = 0;
final double[] exp = new double[data.getAlphaSampleCount()];
final double[] alpha = new double[data.getAlphaSampleCount()];
for (final AlphaSample s : data.getAlphaSampleList()) {
exp[count] = s.getLog10Mean();
alpha[count] = s.getAlpha();
count++;
}
// Just in case
// Sort.sortArrays(alpha, exp, true);
// Test if we can use ImageJ support for a X log scale
final boolean logScaleX = ((float) Math.pow(10, exp[0]) != 0);
double[] x = exp;
String xTitle = "log10(photons)";
if (logScaleX) {
final double[] photons = new double[exp.length];
for (int i = 0; i < photons.length; i++) {
photons[i] = Math.pow(10, exp[i]);
}
x = photons;
xTitle = "photons";
}
// Get interpolation for alpha. Convert to base e.
final double[] logU = exp.clone();
final double scale = Math.log(10);
for (int i = 0; i < logU.length; i++) {
logU[i] *= scale;
}
final BasePoissonFisherInformation if1 = getInterpolatedPoissonFisherInformation(key.getType(), logU, alpha, null);
// Interpolate with 5 points per sample for smooth curve
final int n = 5;
final TDoubleArrayList iexp = new TDoubleArrayList();
final TDoubleArrayList iphotons = new TDoubleArrayList();
for (int i = 1; i < exp.length; i++) {
final int i_1 = i - 1;
final double h = (exp[i] - exp[i_1]) / n;
for (int j = 0; j < n; j++) {
final double e = exp[i_1] + j * h;
iexp.add(e);
iphotons.add(Math.pow(10, e));
}
}
iexp.add(exp[exp.length - 1]);
iphotons.add(Math.pow(10, exp[exp.length - 1]));
final double[] photons = iphotons.toArray();
final double[] ix = (logScaleX) ? photons : iexp.toArray();
final double[] ialpha1 = getAlpha(if1, photons);
final int pointShape = getPointShape(pointOption);
final String title = "Cached Relative Fisher Information";
final Plot plot = new Plot(title, xTitle, "Noise coefficient (alpha)");
plot.setLimits(x[0], x[x.length - 1], -0.05, 1.05);
if (logScaleX) {
plot.setLogScaleX();
}
plot.setColor(Color.blue);
plot.addPoints(ix, ialpha1, Plot.LINE);
// Option to show nodes
if (pointShape != -1) {
plot.addPoints(x, alpha, pointShape);
}
plot.setColor(Color.BLACK);
plot.addLabel(0, 0, cachePlot);
ImageJUtils.display(title, plot);
}
Aggregations