use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PsfEstimator method readDialog.
private boolean readDialog(ExtendedGenericDialog gd) {
initialPeakStdDev0 = gd.getNextNumber();
initialPeakStdDev1 = gd.getNextNumber();
initialPeakAngle = gd.getNextNumber();
settings.setNumberOfPeaks((int) gd.getNextNumber());
settings.setPValue(gd.getNextNumber());
settings.setUpdatePreferences(gd.getNextBoolean());
settings.setDebugPsfEstimator(gd.getNextBoolean());
settings.setIterate(gd.getNextBoolean());
settings.setShowHistograms(gd.getNextBoolean());
settings.setHistogramBins((int) gd.getNextNumber());
final FitConfiguration fitConfig = config.getFitConfiguration();
final CalibrationWriter calibration = fitConfig.getCalibrationWriter();
calibration.setCameraType(SettingsManager.getCameraTypeValues()[gd.getNextChoiceIndex()]);
fitConfig.setCalibration(calibration.getCalibration());
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());
if (extraOptions) {
interlacedData = pluginSettings.interlacedData = gd.getNextBoolean();
integrateFrames = pluginSettings.integrateFrames = (int) gd.getNextNumber();
}
// Some enum values are not supported
fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
config.setFailuresLimit((int) gd.getNextNumber());
config.setPassRate(gd.getNextNumber());
config.setIncludeNeighbours(gd.getNextBoolean());
config.setNeighbourHeightThreshold(gd.getNextNumber());
config.setResidualsThreshold(gd.getNextNumber());
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();
if (gd.invalidNumber()) {
return false;
}
// Check arguments
try {
ParameterUtils.isAboveZero("Initial SD0", initialPeakStdDev0);
ParameterUtils.isAboveZero("Initial SD1", initialPeakStdDev1);
ParameterUtils.isPositive("Initial angle", initialPeakAngle);
ParameterUtils.isPositive("Number of peaks", settings.getNumberOfPeaks());
ParameterUtils.isAboveZero("P-value", settings.getPValue());
ParameterUtils.isEqualOrBelow("P-value", settings.getPValue(), 0.5);
if (settings.getShowHistograms()) {
ParameterUtils.isPositive("Histogram bins", settings.getHistogramBins());
}
ParameterUtils.isAboveZero("Search width", config.getSearch());
ParameterUtils.isAboveZero("Fitting width", config.getFitting());
// Can be negative to disable
// Parameters.isPositive("Failures limit", config.getFailuresLimit());
ParameterUtils.isPositive("Neighbour height threshold", config.getNeighbourHeightThreshold());
ParameterUtils.isPositive("Residuals threshold", config.getResidualsThreshold());
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());
} catch (final IllegalArgumentException ex) {
IJ.error(TITLE, ex.getMessage());
return false;
}
if (fitConfig.getPsfType() == PSFType.ONE_AXIS_GAUSSIAN_2D && fitConfig.isFixedPsf()) {
final String msg = "ERROR: A width-fitting function must be selected (i.e. not fixed-width fitting)";
IJ.error(TITLE, msg);
log(msg);
return false;
}
SettingsManager.writeSettings(config, 0);
if (!PeakFit.configureSmartFilter(config, 0)) {
return false;
}
if (!PeakFit.configureDataFilter(config, 0)) {
return false;
}
if (!PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, 0)) {
return false;
}
// Extra parameters are needed for interlaced data
if (interlacedData) {
gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Interlaced data requires a repeating pattern of frames to process.\n" + "Describe the regular repeat of the data:\n \n" + "Start = The first frame that contains data\n" + "Block = The number of continuous frames containing data\n" + "Skip = The number of continuous frames to ignore before the next data\n \n" + "E.G. 2:9:1 = Data was imaged from frame 2 for 9 frames, 1 frame to ignore," + " then repeat.");
gd.addNumericField("Start", pluginSettings.dataStart, 0);
gd.addNumericField("Block", pluginSettings.dataBlock, 0);
gd.addNumericField("Skip", pluginSettings.dataSkip, 0);
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
if (!gd.wasCanceled()) {
dataStart = (int) gd.getNextNumber();
dataBlock = (int) gd.getNextNumber();
dataSkip = (int) gd.getNextNumber();
if (dataStart > 0 && dataBlock > 0 && dataSkip > 0) {
// Store options for next time
pluginSettings.interlacedData = true;
pluginSettings.dataStart = dataStart;
pluginSettings.dataBlock = dataBlock;
pluginSettings.dataSkip = dataSkip;
pluginSettings.save();
}
} else {
interlacedData = false;
}
}
return true;
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakFit method addCameraOptions.
/**
* Adds the camera options.
*
* @param gd the dialog
* @param options the options
* @param calibrationProvider the calibration provider
*/
public static void addCameraOptions(final ExtendedGenericDialog gd, final int options, final CalibrationProvider calibrationProvider) {
final CalibrationReader calibration = new CalibrationReader(calibrationProvider.getCalibration());
gd.addChoice("Camera_type", SettingsManager.getCameraTypeNames(), CalibrationProtosHelper.getName(calibration.getCameraType()), new OptionListener<Integer>() {
@Override
public boolean collectOptions(Integer field) {
final CalibrationWriter calibration = new CalibrationWriter(calibrationProvider.getCalibration());
final CameraType t = SettingsManager.getCameraTypeValues()[field];
if (calibration.getCameraType() != t) {
calibration.setCameraType(t);
calibrationProvider.saveCalibration(calibration.getCalibration());
}
return collectOptions(false);
}
@Override
public boolean collectOptions() {
return collectOptions(true);
}
private boolean collectOptions(boolean silent) {
final CalibrationWriter calibration = new CalibrationWriter(calibrationProvider.getCalibration());
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Camera type options", null);
if (calibration.isCcdCamera()) {
egd.addNumericField("Camera_bias", calibration.getBias(), 2, 6, "Count");
if (BitFlagUtils.anyNotSet(options, FLAG_NO_GAIN)) {
egd.addNumericField("Gain", calibration.getCountPerPhoton(), 4, 6, "Count/photon");
}
if (BitFlagUtils.anyNotSet(options, FLAG_NO_READ_NOISE)) {
egd.addNumericField("Read_noise", calibration.getReadNoise(), 4, 6, "Count");
}
if (BitFlagUtils.areSet(options, FLAG_QUANTUM_EFFICIENCY)) {
egd.addNumericField("Quantum_efficiency", calibration.getQuantumEfficiency(), 4, 6, "electron/photon");
}
} else if (calibration.isScmos()) {
final String[] models = CameraModelManager.listCameraModels(true);
egd.addChoice("Camera_model_name", models, calibration.getCameraModelName());
if (BitFlagUtils.areSet(options, FLAG_QUANTUM_EFFICIENCY)) {
egd.addNumericField("Quantum_efficiency", calibration.getQuantumEfficiency(), 4, 6, "electron/photon");
}
} else {
IJ.error("Unsupported camera type " + CalibrationProtosHelper.getName(calibration.getCameraType()));
return false;
}
egd.setSilent(silent);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
final Calibration old = calibration.getCalibration();
if (calibration.isCcdCamera()) {
calibration.setBias(Math.abs(egd.getNextNumber()));
if (BitFlagUtils.anyNotSet(options, FLAG_NO_GAIN)) {
calibration.setCountPerPhoton(Math.abs(egd.getNextNumber()));
}
if (BitFlagUtils.anyNotSet(options, FLAG_NO_READ_NOISE)) {
calibration.setReadNoise(Math.abs(egd.getNextNumber()));
}
if (BitFlagUtils.areSet(options, FLAG_QUANTUM_EFFICIENCY)) {
calibration.setQuantumEfficiency(Math.abs(egd.getNextNumber()));
}
} else if (calibration.isScmos()) {
// Note: Since this does not go through the FitConfiguration object the
// camera model is not invalidated. However any code using this function
// should later call configureFitSolver(...) which will set the camera model
// using the camera model name.
calibration.setCameraModelName(egd.getNextChoice());
if (BitFlagUtils.areSet(options, FLAG_QUANTUM_EFFICIENCY)) {
calibration.setQuantumEfficiency(Math.abs(egd.getNextNumber()));
}
}
final Calibration current = calibration.getCalibration();
final boolean changed = !old.equals(current);
if (changed) {
calibrationProvider.saveCalibration(current);
}
return changed;
}
});
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PeakFit method initialiseFitting.
/**
* Set-up the fitting using all the configured properties. Prepare the output results.
*
* @return true, if successful
*/
public boolean initialiseFitting() {
if (source == null) {
return false;
}
// Do this to ensure the serialised configuration is correct
updateFitConfiguration(config);
results.setSource(source);
String name = source.getName();
if (!TextUtils.isNullOrEmpty(resultsSuffix)) {
name += " " + resultsSuffix;
}
if (maximaIdentification) {
name += " (Maxima)";
} else if (fitMaxima) {
name += " (" + getSolverName() + " Fit Maxima)";
} else {
name += " (" + getSolverName() + ")";
}
results.setName(name);
results.setBounds(bounds);
// Calibration cal = calibration.clone();
// Account for the frame integration
// TODO - Should we change this so that if integrate frames is used then the data
// are converted to ExtendedPeakResult with a start and end frame
// cal.exposureTime *= integrateFrames;
// if (interlacedData)
// {
// cal.exposureTime *= ((double)dataBlock / (dataBlock + dataSkip));
// }
final CalibrationWriter cal = fitConfig.getCalibrationWriter();
cal.setTimeUnit(TimeUnit.FRAME);
results.setCalibration(cal.getCalibration());
results.setPsf(fitConfig.getPsf());
results.setConfiguration(SettingsManager.toJson(config.getFitEngineSettings()));
// This is added first as it cannot be closed. If the table is closed then the
// number of results at the end is reported incorrectly.
addMemoryResults(results, false);
addTableResults(results);
ResultsManager.addImageResults(results, resultsSettings.getResultsImageSettings(), bounds, (extraOptions) ? ResultsManager.FLAG_EXTRA_OPTIONS : 0);
addFileResults(results);
addDefaultResults(results);
results.begin();
if (simpleFit && settings.showImage) {
for (final PeakResults r : results.toArray()) {
if (r instanceof ImageJImagePeakResults) {
final ImagePlus i = ((ImageJImagePeakResults) r).getImagePlus();
ImageJUtils.log("Super-resolution image title = " + i.getTitle());
WindowManager.toFront(i.getWindow());
}
}
}
return true;
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PsfCreator method guessScale.
private void guessScale() {
final CalibrationWriter cw = CalibrationWriter.create(settings.getCalibration());
// It does not matter if we already have settings, try and update them anyway
final Calibration c = imp.getCalibration();
double scale = guessScale(c.getXUnit(), c.pixelWidth);
if (scale != 0) {
cw.setNmPerPixel(scale);
settings.setCalibration(cw.getBuilder());
}
scale = guessScale(c.getZUnit(), c.pixelDepth);
if (scale != 0) {
settings.setNmPerSlice(scale);
}
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.
the class PsfCreator method showAlignmentDialog.
private boolean showAlignmentDialog() {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("psf-creator"));
gd.addMessage("Use XYZ stack alignment to create a combined PSF");
final CalibrationWriter cw = CalibrationWriter.create(settings.getCalibration());
// These are ignored for reset
gd.addChoice("Alignment_mode", ALIGNMENT_MODE, settings.getAlignmentMode());
gd.addSlider("Z_radius (px)", 0, imp.getStackSize(), settings.getAlignmentZRadius());
gd.addChoice("PSF_type", PSF_TYPE, settings.getPsfType());
gd.addNumericField("nm_per_pixel", cw.getNmPerPixel(), 2, 6, "nm");
gd.addNumericField("nm_per_slice", settings.getNmPerSlice(), 0, 6, "nm");
PeakFit.addCameraOptions(gd, PeakFit.FLAG_NO_GAIN | PeakFit.FLAG_NO_READ_NOISE, cw);
// For reset
final LocalList<TextField> tf = new LocalList<>();
final LocalList<Checkbox> cb = new LocalList<>();
tf.add(gd.addAndGetSlider("Analysis_window", 0, 8, settings.getAnalysisWindow()));
tf.add(gd.addAndGetSlider("Smoothing", 0.1, 0.5, settings.getSmoothing()));
tf.add(gd.addAndGetSlider("CoM_z_window", 0, 8, settings.getComWindow()));
tf.add(gd.addAndGetSlider("CoM_border", 0, 0.5, settings.getComBorder()));
tf.add(gd.addAndGetSlider("Alignment_magnification", 1, 8, settings.getAlignmentMagnification()));
cb.add(gd.addAndGetCheckbox("Smooth_stack_signal", settings.getSmoothStackSignal()));
tf.add(gd.addAndGetSlider("Max_iterations", 1, 20, settings.getMaxIterations()));
if (settings.getInteractiveMode()) {
cb.add(gd.addAndGetCheckbox("Check_alignments", settings.getCheckAlignments()));
}
tf.add(gd.addAndGetNumericField("Sub-pixel_precision", settings.getSubPixelPrecision(), -2));
if (!settings.getInteractiveMode()) {
tf.add(gd.addAndGetNumericField("RMSD_XY_threshold", settings.getRmsdXyThreshold(), -2));
tf.add(gd.addAndGetNumericField("RMSD_Z_threshold", settings.getRmsdZThreshold(), -2));
tf.add(gd.addAndGetNumericField("CoM_shift_threshold", settings.getComShiftThreshold(), -2));
}
if (ImageJUtils.isShowGenericDialog()) {
gd.addAndGetButton("Reset", event -> {
final boolean interactive = PsfCreator.this.settings.getInteractiveMode();
final PSFCreatorSettings defaults = GuiProtosHelper.defaultPSFCreatorSettings;
int ti = 0;
int cbi = 0;
tf.get(ti++).setText(Double.toString(defaults.getAnalysisWindow()));
tf.get(ti++).setText(Double.toString(defaults.getSmoothing()));
tf.get(ti++).setText(Integer.toString(defaults.getComWindow()));
tf.get(ti++).setText(Double.toString(defaults.getComBorder()));
tf.get(ti++).setText(Integer.toString(defaults.getAlignmentMagnification()));
cb.get(cbi++).setState(defaults.getSmoothStackSignal());
tf.get(ti++).setText(Integer.toString(defaults.getMaxIterations()));
if (interactive) {
cb.get(cbi++).setState(defaults.getCheckAlignments());
}
tf.get(ti++).setText(Double.toString(defaults.getSubPixelPrecision()));
if (!interactive) {
tf.get(ti++).setText(Double.toString(defaults.getRmsdXyThreshold()));
tf.get(ti++).setText(Double.toString(defaults.getRmsdZThreshold()));
tf.get(ti++).setText(Double.toString(defaults.getComShiftThreshold()));
}
// Reset later options too
PsfCreator.this.settings.setPsfMagnification(defaults.getPsfMagnification());
PsfCreator.this.settings.setWindow(defaults.getWindow());
PsfCreator.this.settings.setSmoothStackSignal(defaults.getSmoothStackSignal());
PsfCreator.this.settings.setComBorder(defaults.getComBorder());
PsfCreator.this.settings.setCropBorder(0);
PsfCreator.this.settings.setCropStart(0);
PsfCreator.this.settings.setCropEnd(0);
});
}
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
settings.setAlignmentMode(gd.getNextChoiceIndex());
settings.setAlignmentZRadius((int) gd.getNextNumber());
settings.setPsfType(gd.getNextChoiceIndex());
cw.setNmPerPixel(gd.getNextNumber());
settings.setNmPerSlice(gd.getNextNumber());
cw.setCameraType(SettingsManager.getCameraTypeValues()[gd.getNextChoiceIndex()]);
settings.setAnalysisWindow((int) gd.getNextNumber());
settings.setSmoothing(gd.getNextNumber());
settings.setComWindow((int) gd.getNextNumber());
settings.setComBorder(gd.getNextNumber());
settings.setAlignmentMagnification((int) gd.getNextNumber());
settings.setSmoothStackSignal(gd.getNextBoolean());
settings.setMaxIterations((int) gd.getNextNumber());
if (settings.getInteractiveMode()) {
checkAlignments = gd.getNextBoolean();
settings.setCheckAlignments(checkAlignments);
}
settings.setSubPixelPrecision(gd.getNextNumber());
if (!settings.getInteractiveMode()) {
settings.setRmsdXyThreshold(gd.getNextNumber());
settings.setRmsdZThreshold(gd.getNextNumber());
settings.setComShiftThreshold(gd.getNextNumber());
}
gd.collectOptions();
nmPerPixel = cw.getNmPerPixel();
settings.setCalibration(cw.getBuilder());
SettingsManager.writeSettings(settings);
// Check arguments
try {
ParameterUtils.isPositive("nm/pixel", nmPerPixel);
ParameterUtils.isPositive("nm/slice", settings.getNmPerSlice());
// Since we do a local background estimation for each extracted PSF then we
// do not need the bias for non sCMOS cameras.
// if (!cw.isSCMOS())
// Parameters.isAboveZero("Bias", cw.getBias());
ParameterUtils.isEqualOrAbove("Projection magnification", settings.getAlignmentMagnification(), 1);
ParameterUtils.isEqualOrAbove("Max iterations", settings.getMaxIterations(), 1);
ParameterUtils.isEqualOrAbove("PSF magnification", settings.getPsfMagnification(), 1);
ParameterUtils.isAbove("Smoothing", settings.getSmoothing(), 0);
ParameterUtils.isBelow("Smoothing", settings.getSmoothing(), 1);
} catch (final IllegalArgumentException ex) {
IJ.error(TITLE, ex.getMessage());
return false;
}
return true;
}
Aggregations