use of uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method showDialog.
private boolean showDialog() {
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addHelp(HelpUrls.getUrl("fit-spot-data"));
ImageJUtils.addMessage(gd, "Fit candidate spots in the benchmark image created by " + CreateData.TITLE + " plugin\nand identified by the " + BenchmarkSpotFilter.TITLE + " plugin.\nPSF width = %s nm (Square pixel adjustment = %s nm)\n \n" + "Configure the fitting:", MathUtils.rounded(simulationParameters.sd), MathUtils.rounded(getSa()));
gd.addSlider("Fraction_positives", 50, 100, settings.fractionPositives);
gd.addSlider("Fraction_negatives_after_positives", 0, 100, settings.fractionNegativesAfterAllPositives);
gd.addSlider("Min_negatives_after_positives", 0, 30, settings.negativesAfterAllPositives);
gd.addSlider("Match_distance", 0.5, 3.5, settings.distance);
gd.addSlider("Lower_distance", 0, 3.5, settings.lowerDistance);
gd.addSlider("Match_signal", 0, 3.5, settings.signalFactor);
gd.addSlider("Lower_signal", 0, 3.5, settings.lowerSignalFactor);
final FitEngineConfigurationProvider fitEngineConfigurationProvider = new PeakFit.SimpleFitEngineConfigurationProvider(config);
// Collect options for fitting
final double sa = getSa();
final FitConfiguration fitConfig = config.getFitConfiguration();
fitConfig.setInitialPeakStdDev(MathUtils.round(sa / simulationParameters.pixelPitch));
PeakFit.addPsfOptions(gd, fitConfig);
PeakFit.addFittingOptions(gd, fitEngineConfigurationProvider);
gd.addChoice("Fit_solver", SettingsManager.getFitSolverNames(), FitProtosHelper.getName(fitConfig.getFitSolver()));
gd.addMessage("Multi-path filter (used to pick optimum results during fitting)");
// Allow loading the best filter for these results
final boolean benchmarkSettingsCheckbox = BenchmarkSpotFitResult.fitResultsId.get() == BenchmarkFilterAnalysis.getLastFittingId();
Checkbox cbBenchmark = null;
if (benchmarkSettingsCheckbox) {
// This should always be an opt-in decision. Otherwise the user cannot use the previous
// settings.
cbBenchmark = gd.addAndGetCheckbox("Benchmark_settings", false);
}
gd.addTextAreas(XmlUtils.convertQuotes(multiFilter.toXml()), null, 6, 60);
textFailLimit = gd.addAndGetNumericField("Fail_limit", config.getFailuresLimit(), 0);
cbIncludeNeighbours = gd.addAndGetCheckbox("Include_neighbours", config.isIncludeNeighbours());
gd.addAndGetSlider("Neighbour_height", 0.01, 1, config.getNeighbourHeightThreshold());
textNeighbourHeight = gd.getLastTextField();
cbComputeDoublets = gd.addAndGetCheckbox("Compute_doublets", settings.computeDoublets);
PeakFit.addDuplicateDistanceOptions(gd, fitEngineConfigurationProvider);
gd.addCheckbox("Show_score_histograms", settings.showFilterScoreHistograms);
gd.addCheckbox("Show_correlation", settings.showCorrelation);
gd.addCheckbox("Plot_rank_by_intensity", settings.rankByIntensity);
gd.addCheckbox("Save_filter_range", settings.saveFilterRange);
if (extraOptions) {
// No extra options
}
// Add a mouse listener to the config file field
if (cbBenchmark != null && ImageJUtils.isShowGenericDialog()) {
taFilterXml = gd.getTextArea1();
cbBenchmark.addItemListener(this);
}
gd.showDialog();
if (gd.wasCanceled()) {
return false;
}
settings.fractionPositives = Math.abs(gd.getNextNumber());
settings.fractionNegativesAfterAllPositives = Math.abs(gd.getNextNumber());
settings.negativesAfterAllPositives = (int) Math.abs(gd.getNextNumber());
settings.distance = Math.abs(gd.getNextNumber());
settings.lowerDistance = Math.abs(gd.getNextNumber());
settings.signalFactor = Math.abs(gd.getNextNumber());
settings.lowerSignalFactor = Math.abs(gd.getNextNumber());
fitConfig.setPsfType(PeakFit.getPsfTypeValues()[gd.getNextChoiceIndex()]);
config.setFitting(gd.getNextNumber());
// Some enum values are not supported
fitConfig.setFitSolver(SettingsManager.getFitSolverValues()[gd.getNextChoiceIndex()]);
boolean myUseBenchmarkSettings = false;
if (benchmarkSettingsCheckbox) {
// useBenchmarkSettings =
myUseBenchmarkSettings = gd.getNextBoolean();
}
// Read dialog settings
final String xml = gd.getNextText();
final int failLimit = (int) gd.getNextNumber();
final boolean includeNeighbours = gd.getNextBoolean();
final double neighbourHeightThreshold = gd.getNextNumber();
final boolean myComputeDoublets = gd.getNextBoolean();
final double myDuplicateDistance = gd.getNextNumber();
gd.collectOptions();
MultiPathFilter myMultiFilter = null;
if (myUseBenchmarkSettings && !ImageJUtils.isShowGenericDialog()) {
// Only copy the benchmark settings if not interactive
final FitEngineConfiguration tmp = new FitEngineConfiguration();
final FitConfiguration tmpFitConfig = tmp.getFitConfiguration();
// Collect the residuals threshold
tmpFitConfig.setComputeResiduals(true);
if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
config.setFailuresLimit(tmp.getFailuresLimit());
config.setIncludeNeighbours(tmp.isIncludeNeighbours());
config.setNeighbourHeightThreshold(tmp.getNeighbourHeightThreshold());
settings.computeDoublets = (tmp.getResidualsThreshold() < 1);
config.setDuplicateDistance(tmp.getDuplicateDistance());
config.setDuplicateDistanceAbsolute(tmp.getDuplicateDistanceAbsolute());
final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
final double residualsThreshold = tmp.getResidualsThreshold();
myMultiFilter = new MultiPathFilter(primaryFilter, FitWorker.createMinimalFilter(tmpFitConfig.getFilterPrecisionMethod()), residualsThreshold);
}
} else {
myMultiFilter = MultiPathFilter.fromXml(xml);
config.setFailuresLimit(failLimit);
config.setIncludeNeighbours(includeNeighbours);
config.setNeighbourHeightThreshold(neighbourHeightThreshold);
settings.computeDoublets = myComputeDoublets;
config.setDuplicateDistance(myDuplicateDistance);
}
if (myMultiFilter == null) {
gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("The multi-path filter was invalid.\n \nContinue with a default filter?");
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.showDialog();
if (!gd.wasOKed()) {
return false;
}
} else {
multiFilter = myMultiFilter;
}
if (settings.computeDoublets) {
config.setResidualsThreshold(0);
fitConfig.setComputeResiduals(true);
} else {
config.setResidualsThreshold(1);
fitConfig.setComputeResiduals(false);
}
settings.showFilterScoreHistograms = gd.getNextBoolean();
settings.showCorrelation = gd.getNextBoolean();
settings.rankByIntensity = gd.getNextBoolean();
settings.saveFilterRange = gd.getNextBoolean();
// Avoid stupidness, i.e. things that move outside the fit window and are bad widths
// TODO - Fix this for simple or smart filter...
fitConfig.setDisableSimpleFilter(false);
// Realistically we cannot fit lower than this
fitConfig.setMinPhotons(15);
// Disable shift as candidates may be re-mapped to alternative candidates so the initial
// position is wrong.
fitConfig.setCoordinateShiftFactor(0);
fitConfig.setMinWidthFactor(1.0 / 5);
fitConfig.setMaxWidthFactor(5);
// Disable the direct filter
fitConfig.setDirectFilter(null);
if (extraOptions) {
// No extra options
}
if (gd.invalidNumber()) {
return false;
}
if (settings.lowerDistance > settings.distance) {
settings.lowerDistance = settings.distance;
}
if (settings.lowerSignalFactor > settings.signalFactor) {
settings.lowerSignalFactor = settings.signalFactor;
}
// Distances relative to sa (not s) as this is the same as the BenchmarkSpotFilter plugin
distanceInPixels = settings.distance * sa / simulationParameters.pixelPitch;
lowerDistanceInPixels = settings.lowerDistance * sa / simulationParameters.pixelPitch;
// Copy simulation defaults if a new simulation.
// if (lastSimulationId.get() != simulationParameters.id) {
// This is needed to configure the fit solver.
fitConfig.setNmPerPixel(simulationParameters.pixelPitch);
fitConfig.setGain(simulationParameters.gain);
fitConfig.setQuantumEfficiency(simulationParameters.qe);
fitConfig.setReadNoise(simulationParameters.readNoise);
fitConfig.setBias(simulationParameters.bias);
fitConfig.setCameraType(simulationParameters.cameraType);
fitConfig.setCameraModel(CreateData.getCameraModel(simulationParameters));
// }
if (!PeakFit.configurePsfModel(config)) {
return false;
}
return PeakFit.configureFitSolver(config, IJImageSource.getBounds(imp), null, (extraOptions) ? PeakFit.FLAG_EXTRA_OPTIONS : 0);
}
use of uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method itemStateChanged.
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getSource() instanceof Checkbox) {
final Checkbox checkbox = (Checkbox) event.getSource();
int failLimit;
boolean includeNeighbours;
double neighbourHeightThrehsold;
boolean computeDoublets;
MultiPathFilter myMultiFilter;
if (checkbox.getState()) {
final FitEngineConfiguration tmp = new FitEngineConfiguration();
final FitConfiguration tmpFitConfig = tmp.getFitConfiguration();
// Collect residuals threshold
tmpFitConfig.setComputeResiduals(true);
if (BenchmarkFilterAnalysis.updateConfiguration(tmp, false)) {
failLimit = tmp.getFailuresLimit();
includeNeighbours = tmp.isIncludeNeighbours();
neighbourHeightThrehsold = tmp.getNeighbourHeightThreshold();
computeDoublets = tmp.getResidualsThreshold() < 1;
final DirectFilter primaryFilter = tmpFitConfig.getSmartFilter();
final double residualsThreshold = tmp.getResidualsThreshold();
myMultiFilter = new MultiPathFilter(primaryFilter, FitWorker.createMinimalFilter(tmpFitConfig.getFilterPrecisionMethod()), residualsThreshold);
} else {
IJ.log("Failed to update settings using the filter analysis");
checkbox.setState(false);
return;
}
} else {
failLimit = config.getFailuresLimit();
includeNeighbours = config.isIncludeNeighbours();
neighbourHeightThrehsold = config.getNeighbourHeightThreshold();
computeDoublets = BenchmarkSpotFit.this.settings.computeDoublets;
myMultiFilter = multiFilter;
}
// Update the dialog
taFilterXml.setText(myMultiFilter.toXml());
textFailLimit.setText("" + failLimit);
cbIncludeNeighbours.setState(includeNeighbours);
textNeighbourHeight.setText(MathUtils.rounded(neighbourHeightThrehsold));
cbComputeDoublets.setState(computeDoublets);
}
}
use of uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method initialiseScoring.
/**
* Initialise the results list used for scoring the filters. This is shared with the genetic
* algorithm.
*
* @param filterSet the filter set
*/
private void initialiseScoring(FilterSet filterSet) {
// Initialise with the candidate true and false negative scores
gaResultsListToScore = gaResultsList;
gaSubset = false;
if (filterSet.size() < 2) {
return;
}
if (filterSet.allSameType() && debug) {
// Would there be a speed increase if the results list were partitioned into multiple sets
// by filtering with not just the weakest but a step set of weaker and weaker filters.
// This could be done using, e.g. precision, to partition the filters into a range.
// // Plot the cumulative histogram of precision for the filters in the set.
// try
// {
// StoredDataStatistics stats = new StoredDataStatistics(filterSet.size());
// for (Filter f : filterSet.getFilters())
// {
// IMultiFilter f2 = (IMultiFilter) f;
// stats.add(f2.getPrecision());
// }
// double[][] h1 = Maths.cumulativeHistogram(stats.getValues(), false);
// String title = TITLE + " Cumul Precision";
// Plot plot = new Plot(title, "Precision", "Frequency");
// // Find limits
// double[] xlimit = Maths.limits(h1[0]);
// plot.setLimits(xlimit[0] - 1, xlimit[1] + 1, 0, Maths.max(h1[1]) * 1.05);
// plot.addPoints(h1[0], h1[1], Plot.BAR);
// Utils.display(title, plot);
// }
// catch (ClassCastException e)
// {
//
// }
// Debug the limits of all parameters
final double[] lower = filterSet.getFilters().get(0).getParameters().clone();
final double[] upper = lower.clone();
for (final Filter f : filterSet.getFilters()) {
final double[] point = f.getParameters();
for (int j = 0; j < lower.length; j++) {
if (lower[j] > point[j]) {
lower[j] = point[j];
}
if (upper[j] < point[j]) {
upper[j] = point[j];
}
}
}
final StringBuilder sb = new StringBuilder("Scoring (");
sb.append(filterSet.size()).append("):");
for (int j = 0; j < lower.length; j++) {
sb.append(' ').append(MathUtils.rounded(lower[j])).append('-').append(MathUtils.rounded(upper[j]));
}
ImageJUtils.log(sb.toString());
}
final Filter weakest = filterSet.createWeakestFilter();
if (weakest != null) {
gaSubset = true;
gaResultsListToScore = createMpf((DirectFilter) weakest, defaultMinimalFilter).filterSubset(gaResultsList, createFailCounter(settings.failCount), true);
// MultiPathFilter.resetValidationFlag(ga_resultsListToScore);
// ga_resultsListToScore = ga_resultsList;
// System.out.printf("Weakest %d => %d : %s\n", count(ga_resultsList),
// count(ga_resultsListToScore),
// weakest.getName());
}
}
use of uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method filterAnalysis.
private int filterAnalysis(FilterSet filterSet, int setNumber, DirectFilter currentOptimum, double rangeReduction) {
// Check if the filters are the same so allowing optimisation
final boolean allSameType = filterSet.allSameType();
this.gaResultsList = fitResultData.resultsList;
Chromosome<FilterScore> best = null;
String algorithm = "";
// All the search algorithms use search dimensions.
// Create search dimensions if needed (these are used for testing if the optimum is at the
// limit).
searchScoreFilter = null;
strengthLower = null;
strengthUpper = null;
FixedDimension[] originalDimensions = null;
boolean rangeInput = false;
boolean[] disabled = null;
double[][] seed = null;
// This flag is set if the analysis is not interactive. This occurs when running after the
// first iteration of an iterative analysis.
boolean nonInteractive = false;
if (allSameType) {
// There should always be 1 filter
searchScoreFilter = (DirectFilter) filterSet.getFilters().get(0);
final int n = searchScoreFilter.getNumberOfParameters();
// Option to configure a range
rangeInput = filterSet.getName().contains("Range");
final double[] range = new double[n];
if (rangeInput && filterSet.size() == 4) {
originalDimensions = new FixedDimension[n];
// This is used as min/lower/upper/max
final Filter minF = searchScoreFilter;
final Filter lowerF = filterSet.getFilters().get(1);
final Filter upperF = filterSet.getFilters().get(2);
final Filter maxF = filterSet.getFilters().get(3);
for (int i = 0; i < n; i++) {
final double min = minF.getParameterValue(i);
final double lower = lowerF.getParameterValue(i);
final double upper = upperF.getParameterValue(i);
range[i] = upper - lower;
final double max = maxF.getParameterValue(i);
final double minIncrement = searchScoreFilter.getParameterIncrement(i);
try {
originalDimensions[i] = new FixedDimension(min, max, minIncrement, lower, upper);
} catch (final IllegalArgumentException ex) {
ImageJUtils.log(TITLE + " : Unable to configure dimension [%d] %s: " + ex.getMessage(), i, searchScoreFilter.getParameterName(i));
originalDimensions = null;
rangeInput = false;
break;
}
}
}
if (rangeInput && (filterSet.size() == 3 || filterSet.size() == 2)) {
originalDimensions = new FixedDimension[n];
// This is used as lower/upper[/increment]
final Filter lowerF = searchScoreFilter;
final Filter upperF = filterSet.getFilters().get(1);
for (int i = 0; i < n; i++) {
// Do not disable if the increment is not set. This is left to the user to decide
// which parameters to optimise with the enabled checkboxes in the dialog.
final double lower = lowerF.getParameterValue(i);
final double upper = upperF.getParameterValue(i);
range[i] = upper - lower;
final ParameterType type = searchScoreFilter.getParameterType(i);
final double min = BenchmarkSpotFit.getMin(type);
final double max = BenchmarkSpotFit.getMax(type);
final double minIncrement = searchScoreFilter.getParameterIncrement(i);
try {
originalDimensions[i] = new FixedDimension(min, max, minIncrement, lower, upper);
} catch (final IllegalArgumentException ex) {
ImageJUtils.log(TITLE + " : Unable to configure dimension [%d] %s: " + ex.getMessage(), i, searchScoreFilter.getParameterName(i));
originalDimensions = null;
rangeInput = false;
break;
}
}
}
// Get the dimensions from the filters
if (originalDimensions == null) {
originalDimensions = new FixedDimension[n];
// Allow inputing a filter set (e.g. saved from previous optimisation)
// Find the limits in the current scores
final double[] lower = searchScoreFilter.getParameters().clone();
final double[] upper = lower.clone();
// Allow the SearchSpace algorithms to be seeded with an initial population
// for the first evaluation of the optimum. This is done when the input filter
// set is not a range.
seed = new double[filterSet.size()][];
int count = 0;
for (final Filter f : filterSet.getFilters()) {
final double[] point = f.getParameters();
seed[count++] = point;
for (int j = 0; j < lower.length; j++) {
if (lower[j] > point[j]) {
lower[j] = point[j];
}
if (upper[j] < point[j]) {
upper[j] = point[j];
}
}
}
// Get the search dimensions from the data.
// Min/max must be set using values from BenchmarkSpotFit.
boolean hasRange = false;
for (int i = 0; i < n; i++) {
if (lower[i] == upper[i]) {
// Not enabled
originalDimensions[i] = new FixedDimension(lower[i]);
continue;
}
hasRange = true;
final ParameterType type = searchScoreFilter.getParameterType(i);
double min = BenchmarkSpotFit.getMin(type);
double max = BenchmarkSpotFit.getMax(type);
final double minIncrement = searchScoreFilter.getParameterIncrement(i);
if (min > lower[i]) {
min = lower[i];
}
if (max < upper[i]) {
max = upper[i];
}
try {
originalDimensions[i] = new FixedDimension(min, max, minIncrement, lower[i], upper[i]);
} catch (final IllegalArgumentException ex) {
ImageJUtils.log(TITLE + " : Unable to configure dimension [%d] %s: " + ex.getMessage(), i, searchScoreFilter.getParameterName(i));
originalDimensions = null;
break;
}
}
if (!hasRange || originalDimensions == null) {
// Failed to work out the dimensions. No optimisation will be possible.
originalDimensions = null;
// Sort so that the filters are in a nice order for reporting
filterSet.sort();
// This will not be used when the dimensions are null
seed = null;
}
}
if (originalDimensions != null) {
// Use the current optimum if we are doing a range optimisation
if (currentOptimum != null && rangeInput && currentOptimum.getType().equals(searchScoreFilter.getType()) && settings.evolve != 0) {
// Suppress dialogs and use the current settings
nonInteractive = true;
final double[] p = currentOptimum.getParameters();
// Range search uses SearchDimension and we must centre on the optimum after creation.
for (int i = 0; i < originalDimensions.length; i++) {
final double centre = p[i];
double rangeFactor = 0;
if (originalDimensions[i].isActive()) {
// Set the range around the centre.
// This uses the range for each param when we read the filters.
rangeFactor = range[i];
// Optionally reduce the width of the dimensions.
if (rangeReduction > 0 && rangeReduction < 1) {
rangeFactor *= rangeReduction;
}
}
final double lower = centre - rangeFactor * 0.5;
final double upper = centre + rangeFactor * 0.5;
originalDimensions[i] = originalDimensions[i].create(lower, upper);
}
}
// Store the dimensions so we can do an 'at limit' check
disabled = new boolean[originalDimensions.length];
strengthLower = new double[originalDimensions.length];
strengthUpper = new double[originalDimensions.length];
for (int i = 0; i < disabled.length; i++) {
disabled[i] = !originalDimensions[i].isActive();
strengthLower[i] = originalDimensions[i].lower;
strengthUpper[i] = originalDimensions[i].upper;
}
}
} else {
// Sort so that the filters are in a nice order for reporting
filterSet.sort();
}
analysisStopWatch = StopWatch.createStarted();
if (settings.evolve == 1 && originalDimensions != null) {
// Collect parameters for the genetic algorithm
pauseFilterTimer();
// Remember the step size settings
double[] stepSize = stepSizeMap.get(setNumber);
if (stepSize == null || stepSize.length != searchScoreFilter.length()) {
stepSize = searchScoreFilter.mutationStepRange().clone();
for (int j = 0; j < stepSize.length; j++) {
stepSize[j] *= settings.delta;
}
// See if the same number of parameters have been optimised in other algorithms
final boolean[] enabled = searchRangeMap.get(setNumber);
if (enabled != null && enabled.length == stepSize.length) {
for (int j = 0; j < stepSize.length; j++) {
if (!enabled[j]) {
stepSize[j] *= -1;
}
}
}
}
GenericDialog gd = null;
final int[] indices = searchScoreFilter.getChromosomeParameters();
boolean runAlgorithm = nonInteractive;
if (!nonInteractive) {
// Ask the user for the mutation step parameters.
gd = new GenericDialog(TITLE);
final String prefix = setNumber + "_";
gd.addMessage("Configure the genetic algorithm for [" + setNumber + "] " + filterSet.getName());
gd.addNumericField(prefix + "Population_size", settings.populationSize, 0);
gd.addNumericField(prefix + "Failure_limit", settings.failureLimit, 0);
gd.addNumericField(prefix + "Tolerance", settings.tolerance, -1);
gd.addNumericField(prefix + "Converged_count", settings.convergedCount, 0);
gd.addSlider(prefix + "Mutation_rate", 0.05, 1, settings.mutationRate);
gd.addSlider(prefix + "Crossover_rate", 0.05, 1, settings.crossoverRate);
gd.addSlider(prefix + "Mean_children", 0.05, 3, settings.meanChildren);
gd.addSlider(prefix + "Selection_fraction", 0.05, 0.5, settings.selectionFraction);
gd.addCheckbox(prefix + "Ramped_selection", settings.rampedSelection);
gd.addCheckbox(prefix + "Save_option", settings.saveOption);
gd.addMessage("Configure the step size for each parameter");
for (int j = 0; j < indices.length; j++) {
// Do not mutate parameters that were not expanded, i.e. the input did not vary them.
final double step = (originalDimensions[indices[j]].isActive()) ? stepSize[j] : 0;
gd.addNumericField(getDialogName(prefix, searchScoreFilter, indices[j]), step, 2);
}
gd.showDialog();
runAlgorithm = !gd.wasCanceled();
}
if (runAlgorithm) {
// Used to create random sample
final FixedDimension[] dimensions = Arrays.copyOf(originalDimensions, originalDimensions.length);
if (!nonInteractive) {
if (gd == null) {
throw new IllegalStateException("The dialog has not been shown");
}
settings.populationSize = (int) Math.abs(gd.getNextNumber());
if (settings.populationSize < 10) {
settings.populationSize = 10;
}
settings.failureLimit = (int) Math.abs(gd.getNextNumber());
settings.tolerance = gd.getNextNumber();
// Allow negatives
settings.convergedCount = (int) gd.getNextNumber();
settings.mutationRate = Math.abs(gd.getNextNumber());
settings.crossoverRate = Math.abs(gd.getNextNumber());
settings.meanChildren = Math.abs(gd.getNextNumber());
settings.selectionFraction = Math.abs(gd.getNextNumber());
settings.rampedSelection = gd.getNextBoolean();
settings.saveOption = gd.getNextBoolean();
for (int j = 0; j < indices.length; j++) {
stepSize[j] = gd.getNextNumber();
}
// Store for repeat analysis
stepSizeMap.put(setNumber, stepSize);
}
if (disabled == null) {
disabled = new boolean[originalDimensions.length];
}
for (int j = 0; j < indices.length; j++) {
// A zero step size will keep the parameter but prevent range mutation.
if (stepSize[j] < 0) {
dimensions[indices[j]] = new FixedDimension(searchScoreFilter.getDisabledParameterValue(indices[j]));
disabled[indices[j]] = true;
}
}
// Create the genetic algorithm
final UniformRandomProvider rng = UniformRandomProviders.create();
final SimpleMutator<FilterScore> mutator = new SimpleMutator<>(rng, settings.mutationRate);
// Override the settings with the step length, a min of zero and the configured upper
final double[] upper = searchScoreFilter.upperLimit();
mutator.overrideChromosomeSettings(stepSize, new double[stepSize.length], upper);
final Recombiner<FilterScore> recombiner = new SimpleRecombiner<>(rng, settings.crossoverRate, settings.meanChildren);
SelectionStrategy<FilterScore> selectionStrategy;
// If the initial population is huge ensure that the first selection culls to the correct
// size
final int selectionMax = (int) (settings.selectionFraction * settings.populationSize);
if (settings.rampedSelection) {
selectionStrategy = new RampedSelectionStrategy<>(rng, settings.selectionFraction, selectionMax);
} else {
selectionStrategy = new SimpleSelectionStrategy<>(rng, settings.selectionFraction, selectionMax);
}
final ToleranceChecker<FilterScore> gaChecker = new InterruptChecker(settings.tolerance, settings.tolerance * 1e-3, settings.convergedCount);
// Create new random filters if the population is initially below the population size
List<Filter> filters = filterSet.getFilters();
if (filterSet.size() < settings.populationSize) {
filters = new ArrayList<>(settings.populationSize);
// Add the existing filters if they are not a range input file
if (!rangeInput) {
filters.addAll(filterSet.getFilters());
}
// Add current optimum to seed
if (currentOptimum != null && nonInteractive) {
filters.add(currentOptimum);
}
// The GA does not use the min interval grid so sample without rounding
final double[][] sample = SearchSpace.sampleWithoutRounding(dimensions, settings.populationSize - filters.size(), null);
filters.addAll(searchSpaceToFilters(sample));
}
gaPopulation = new Population<>(filters);
gaPopulation.setPopulationSize(settings.populationSize);
gaPopulation.setFailureLimit(settings.failureLimit);
selectionStrategy.setTracker(this);
// Evolve
algorithm = Settings.EVOLVE_OPTIONS[settings.evolve];
gaStatusPrefix = algorithm + " [" + setNumber + "] " + filterSet.getName() + " ... ";
gaIteration = 0;
gaPopulation.setTracker(this);
createGaWindow();
resumeFilterTimer();
best = gaPopulation.evolve(mutator, recombiner, this, selectionStrategy, gaChecker);
// In case optimisation was stopped
IJ.resetEscape();
if (best != null) {
// The GA may produce coordinates off the min interval grid
best = enumerateMinInterval(best, stepSize, indices);
// Now update the filter set for final assessment
filterSet = new FilterSet(filterSet.getName(), populationToFilters(gaPopulation.getIndividuals()));
// Option to save the filters
if (settings.saveOption) {
saveFilterSet(filterSet, setNumber, !nonInteractive);
}
}
} else {
resumeFilterTimer();
}
}
if ((settings.evolve == 2 || settings.evolve == 4) && originalDimensions != null) {
// Collect parameters for the range search algorithm
pauseFilterTimer();
final boolean isStepSearch = settings.evolve == 4;
// The step search should use a multi-dimension refinement and no range reduction
SearchSpace.RefinementMode myRefinementMode = SearchSpace.RefinementMode.MULTI_DIMENSION;
// Remember the enabled settings
boolean[] enabled = searchRangeMap.get(setNumber);
final int n = searchScoreFilter.getNumberOfParameters();
if (enabled == null || enabled.length != n) {
enabled = new boolean[n];
Arrays.fill(enabled, true);
// See if the same number of parameters have been optimised in other algorithms
final double[] stepSize = stepSizeMap.get(setNumber);
if (stepSize != null && enabled.length == stepSize.length) {
for (int j = 0; j < stepSize.length; j++) {
if (stepSize[j] < 0) {
enabled[j] = false;
}
}
}
}
GenericDialog gd = null;
boolean runAlgorithm = nonInteractive;
if (!nonInteractive) {
// Ask the user for the search parameters.
gd = new GenericDialog(TITLE);
final String prefix = setNumber + "_";
gd.addMessage("Configure the " + Settings.EVOLVE_OPTIONS[settings.evolve] + " algorithm for [" + setNumber + "] " + filterSet.getName());
gd.addSlider(prefix + "Width", 1, 5, settings.rangeSearchWidth);
if (!isStepSearch) {
gd.addCheckbox(prefix + "Save_option", settings.saveOption);
gd.addNumericField(prefix + "Max_iterations", settings.maxIterations, 0);
final String[] modes = SettingsManager.getNames((Object[]) SearchSpace.RefinementMode.values());
gd.addSlider(prefix + "Reduce", 0.01, 0.99, settings.rangeSearchReduce);
gd.addChoice("Refinement", modes, modes[settings.refinementMode]);
}
gd.addNumericField(prefix + "Seed_size", settings.seedSize, 0);
// Add choice of fields to optimise
for (int i = 0; i < n; i++) {
gd.addCheckbox(getDialogName(prefix, searchScoreFilter, i), enabled[i]);
}
gd.showDialog();
runAlgorithm = !gd.wasCanceled();
}
if (runAlgorithm) {
final SearchDimension[] dimensions = new SearchDimension[n];
if (!nonInteractive) {
if (gd == null) {
throw new IllegalStateException("The dialog has not been shown");
}
settings.rangeSearchWidth = (int) gd.getNextNumber();
if (!isStepSearch) {
settings.saveOption = gd.getNextBoolean();
settings.maxIterations = (int) gd.getNextNumber();
settings.rangeSearchReduce = gd.getNextNumber();
settings.refinementMode = gd.getNextChoiceIndex();
}
settings.seedSize = (int) gd.getNextNumber();
for (int i = 0; i < n; i++) {
enabled[i] = gd.getNextBoolean();
}
searchRangeMap.put(setNumber, enabled);
}
if (!isStepSearch) {
myRefinementMode = SearchSpace.RefinementMode.values()[settings.refinementMode];
}
for (int i = 0; i < n; i++) {
if (enabled[i]) {
try {
dimensions[i] = originalDimensions[i].create(settings.rangeSearchWidth);
dimensions[i].setPad(true);
// Prevent range reduction so that the step search just does a single refinement step
dimensions[i].setReduceFactor((isStepSearch) ? 1 : settings.rangeSearchReduce);
// Centre on current optimum
if (nonInteractive && currentOptimum != null) {
dimensions[i].setCentre(currentOptimum.getParameterValue(i));
}
} catch (final IllegalArgumentException ex) {
IJ.error(TITLE, String.format("Unable to configure dimension [%d] %s: " + ex.getMessage(), i, searchScoreFilter.getParameterName(i)));
return -1;
}
} else {
dimensions[i] = new SearchDimension(searchScoreFilter.getDisabledParameterValue(i));
}
}
if (disabled == null) {
disabled = new boolean[originalDimensions.length];
}
for (int i = 0; i < disabled.length; i++) {
disabled[i] = !dimensions[i].active;
}
// Check the number of combinations is OK
long combinations = SearchSpace.countCombinations(dimensions);
if (!nonInteractive && combinations > 10000) {
gd = new GenericDialog(TITLE);
ImageJUtils.addMessage(gd, "%d combinations for the configured dimensions.\n \nClick 'Yes' to optimise.", combinations);
gd.enableYesNoCancel();
gd.hideCancelButton();
gd.showDialog();
if (!gd.wasOKed()) {
combinations = 0;
}
}
if (combinations == 0) {
resumeFilterTimer();
} else {
algorithm = Settings.EVOLVE_OPTIONS[settings.evolve] + " " + settings.rangeSearchWidth;
gaStatusPrefix = algorithm + " [" + setNumber + "] " + filterSet.getName() + " ... ";
gaIteration = 0;
filterScoreOptimum = null;
final SearchSpace ss = new SearchSpace();
ss.setTracker(this);
if (settings.seedSize > 0) {
double[][] sample;
// Add current optimum to seed
if (nonInteractive && currentOptimum != null) {
sample = new double[1][];
sample[0] = currentOptimum.getParameters();
seed = merge(seed, sample);
}
final int size = (seed == null) ? 0 : seed.length;
// Sample without rounding as the seed will be rounded
sample = SearchSpace.sampleWithoutRounding(dimensions, settings.seedSize - size, null);
seed = merge(seed, sample);
}
// Note: If we have an optimum and we are not seeding this should not matter as the
// dimensions have been centred on the current optimum
ss.seed(seed);
final ConvergenceChecker<FilterScore> checker = new InterruptConvergenceChecker(0, 0, settings.maxIterations);
createGaWindow();
resumeFilterTimer();
final SearchResult<FilterScore> optimum = ss.search(dimensions, this, checker, myRefinementMode);
// In case optimisation was stopped
IJ.resetEscape();
if (optimum != null) {
best = ((SimpleFilterScore) optimum.getScore()).result.filter;
// Now update the filter set for final assessment
filterSet = new FilterSet(filterSet.getName(), searchSpaceToFilters((DirectFilter) best, ss.getSearchSpace()));
// Option to save the filters
if (settings.saveOption) {
saveFilterSet(filterSet, setNumber, !nonInteractive);
}
}
}
} else {
resumeFilterTimer();
}
}
if (settings.evolve == 3 && originalDimensions != null) {
// Collect parameters for the enrichment search algorithm
pauseFilterTimer();
boolean[] enabled = searchRangeMap.get(setNumber);
final int n = searchScoreFilter.getNumberOfParameters();
if (enabled == null || enabled.length != n) {
enabled = new boolean[n];
Arrays.fill(enabled, true);
// See if the same number of parameters have been optimised in other algorithms
final double[] stepSize = stepSizeMap.get(setNumber);
if (stepSize != null && enabled.length == stepSize.length) {
for (int j = 0; j < stepSize.length; j++) {
if (stepSize[j] < 0) {
enabled[j] = false;
}
}
}
}
GenericDialog gd = null;
boolean runAlgorithm = nonInteractive;
if (!nonInteractive) {
// Ask the user for the search parameters.
gd = new GenericDialog(TITLE);
final String prefix = setNumber + "_";
gd.addMessage("Configure the enrichment search algorithm for [" + setNumber + "] " + filterSet.getName());
gd.addCheckbox(prefix + "Save_option", settings.saveOption);
gd.addNumericField(prefix + "Max_iterations", settings.maxIterations, 0);
gd.addNumericField(prefix + "Converged_count", settings.convergedCount, 0);
gd.addNumericField(prefix + "Samples", settings.enrichmentSamples, 0);
gd.addSlider(prefix + "Fraction", 0.01, 0.99, settings.enrichmentFraction);
gd.addSlider(prefix + "Padding", 0, 0.99, settings.enrichmentPadding);
// Add choice of fields to optimise
for (int i = 0; i < n; i++) {
gd.addCheckbox(getDialogName(prefix, searchScoreFilter, i), enabled[i]);
}
gd.showDialog();
runAlgorithm = !gd.wasCanceled();
}
if (runAlgorithm) {
final FixedDimension[] dimensions = Arrays.copyOf(originalDimensions, originalDimensions.length);
if (!nonInteractive && gd != null) {
settings.saveOption = gd.getNextBoolean();
settings.maxIterations = (int) gd.getNextNumber();
settings.convergedCount = (int) gd.getNextNumber();
settings.enrichmentSamples = (int) gd.getNextNumber();
settings.enrichmentFraction = gd.getNextNumber();
settings.enrichmentPadding = gd.getNextNumber();
for (int i = 0; i < n; i++) {
enabled[i] = gd.getNextBoolean();
}
searchRangeMap.put(setNumber, enabled);
}
for (int i = 0; i < n; i++) {
if (!enabled[i]) {
dimensions[i] = new FixedDimension(searchScoreFilter.getDisabledParameterValue(i));
}
}
if (disabled == null) {
disabled = new boolean[originalDimensions.length];
}
for (int i = 0; i < disabled.length; i++) {
disabled[i] = !dimensions[i].active;
}
algorithm = Settings.EVOLVE_OPTIONS[settings.evolve];
gaStatusPrefix = algorithm + " [" + setNumber + "] " + filterSet.getName() + " ... ";
gaIteration = 0;
filterScoreOptimum = null;
final SearchSpace ss = new SearchSpace();
ss.setTracker(this);
// Add current optimum to seed
if (nonInteractive && currentOptimum != null) {
final double[][] sample = new double[1][];
sample[0] = currentOptimum.getParameters();
seed = merge(seed, sample);
}
ss.seed(seed);
final ConvergenceChecker<FilterScore> checker = new InterruptConvergenceChecker(0, 0, settings.maxIterations, settings.convergedCount);
createGaWindow();
resumeFilterTimer();
final SearchResult<FilterScore> optimum = ss.enrichmentSearch(dimensions, this, checker, settings.enrichmentSamples, settings.enrichmentFraction, settings.enrichmentPadding);
// In case optimisation was stopped
IJ.resetEscape();
if (optimum != null) {
best = ((SimpleFilterScore) optimum.getScore()).result.filter;
// Now update the filter set for final assessment
filterSet = new FilterSet(filterSet.getName(), searchSpaceToFilters((DirectFilter) best, ss.getSearchSpace()));
// Option to save the filters
if (settings.saveOption) {
saveFilterSet(filterSet, setNumber, !nonInteractive);
}
}
} else {
resumeFilterTimer();
}
}
IJ.showStatus("Analysing [" + setNumber + "] " + filterSet.getName() + " ...");
// Do not support plotting if we used optimisation
final double[] xValues = (best != null || isHeadless || (settings.plotTopN == 0)) ? null : new double[filterSet.size()];
final double[] yValues = (xValues != null) ? new double[xValues.length] : null;
SimpleFilterScore max = null;
// It can just assess the top 1 required for the summary.
if (best != null) {
// Only assess the top 1 filter for the summary
final List<Filter> list = new ArrayList<>();
list.add((DirectFilter) best);
filterSet = new FilterSet(filterSet.getName(), list);
}
// Score the filters and report the results if configured.
final FilterScoreResult[] scoreResults = scoreFilters(setUncomputedStrength(filterSet), settings.showResultsTable);
if (scoreResults == null) {
return -1;
}
analysisStopWatch.stop();
for (int index = 0; index < scoreResults.length; index++) {
final FilterScoreResult scoreResult = scoreResults[index];
if (xValues != null && yValues != null) {
xValues[index] = scoreResult.filter.getNumericalValue();
yValues[index] = scoreResult.score;
}
final SimpleFilterScore result = new SimpleFilterScore(scoreResult, allSameType, scoreResult.criteria >= minCriteria);
if (result.compareTo(max) < 0) {
max = result;
}
}
if (max == null) {
return -1;
}
if (settings.showResultsTable) {
addToResultsWindow(scoreResults);
}
// Check the top filter against the limits of the original dimensions
char[] atLimit = null;
if (allSameType && originalDimensions != null) {
if (disabled == null) {
disabled = new boolean[originalDimensions.length];
}
final DirectFilter filter = max.result.filter;
final int[] indices = filter.getChromosomeParameters();
atLimit = new char[indices.length];
final StringBuilder sb = new StringBuilder(200);
for (int j = 0; j < indices.length; j++) {
atLimit[j] = ComplexFilterScore.WITHIN;
final int p = indices[j];
if (disabled[p]) {
continue;
}
final double value = filter.getParameterValue(p);
final double lowerLimit = originalDimensions[p].getLower();
final double upperLimit = originalDimensions[p].getUpper();
final int c1 = Double.compare(value, lowerLimit);
if (c1 <= 0) {
atLimit[j] = ComplexFilterScore.FLOOR;
sb.append(" : ").append(filter.getParameterName(p)).append(' ').append(atLimit[j]).append('[').append(MathUtils.rounded(value));
if (c1 == -1) {
atLimit[j] = ComplexFilterScore.BELOW;
sb.append("<").append(MathUtils.rounded(lowerLimit));
}
sb.append("]");
} else {
final int c2 = Double.compare(value, upperLimit);
if (c2 >= 0) {
atLimit[j] = ComplexFilterScore.CEIL;
sb.append(" : ").append(filter.getParameterName(p)).append(' ').append(atLimit[j]).append('[').append(MathUtils.rounded(value));
if (c2 == 1) {
atLimit[j] = ComplexFilterScore.ABOVE;
sb.append(">").append(MathUtils.rounded(upperLimit));
}
sb.append("]");
}
}
}
if (sb.length() > 0) {
if (max.criteriaPassed) {
ImageJUtils.log("Warning: Top filter (%s @ %s|%s) [%s] at the limit of the expanded range%s", filter.getName(), MathUtils.rounded((invertScore) ? -max.score : max.score), MathUtils.rounded((invertCriteria) ? -minCriteria : minCriteria), limitFailCount + fitResultData.limitRange, sb.toString());
} else {
ImageJUtils.log("Warning: Top filter (%s @ -|%s) [%s] at the limit of the expanded range%s", filter.getName(), MathUtils.rounded((invertCriteria) ? -max.criteria : max.criteria), limitFailCount + fitResultData.limitRange, sb.toString());
}
}
}
// Note that max should never be null since this method is not run with an empty filter set
// We may have no filters that pass the criteria
final String type = max.result.filter.getType();
if (!max.criteriaPassed) {
ImageJUtils.log("Warning: Filter does not pass the criteria: %s : Best = %s using %s", type, MathUtils.rounded((invertCriteria) ? -max.criteria : max.criteria), max.result.filter.getName());
return 0;
}
// This could be an option?
final boolean allowDuplicates = true;
// No requirement to be the same type to store for later analysis.
// All filter sets should be able to have a best
// filter irrespective of whether they were the same type or not.
final ComplexFilterScore newFilterScore = new ComplexFilterScore(max.result, atLimit, algorithm, analysisStopWatch.getTime(), "", 0);
addBestFilter(type, allowDuplicates, newFilterScore);
// Add spacer at end of each result set
if (isHeadless) {
if (settings.showResultsTable && filterSet.size() > 1) {
IJ.log("");
}
} else {
if (settings.showResultsTable && filterSet.size() > 1) {
resultsWindow.append("");
}
if (settings.plotTopN > 0 && xValues != null) {
// Check the xValues are unique. Since the filters have been sorted by their
// numeric value we only need to compare adjacent entries.
boolean unique = true;
for (int ii = 0; ii < xValues.length - 1; ii++) {
if (xValues[ii] == xValues[ii + 1]) {
unique = false;
break;
}
}
String xAxisName = filterSet.getValueName();
if (unique) {
// Check the values all refer to the same property
for (final Filter filter : filterSet.getFilters()) {
if (!xAxisName.equals(filter.getNumericalValueName())) {
unique = false;
break;
}
}
}
if (!unique) {
// If not unique then renumber them and use an arbitrary label
xAxisName = "Filter";
for (int ii = 0; ii < xValues.length; ii++) {
xValues[ii] = ii + 1.0;
}
}
final String title = filterSet.getName();
// Check if a previous filter set had the same name, update if necessary
final NamedPlot plot = getNamedPlot(title);
if (plot == null) {
filterAnalysisResult.plots.add(new NamedPlot(title, xAxisName, xValues, yValues));
} else {
plot.updateValues(xAxisName, xValues, yValues);
}
if (filterAnalysisResult.plots.size() > settings.plotTopN) {
Collections.sort(filterAnalysisResult.plots, NamedPlot::compare);
filterAnalysisResult.plots.remove(filterAnalysisResult.plots.size() - 1);
}
}
}
return 0;
}
use of uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method saveFilter.
private void saveFilter(DirectFilter filter) {
// Save the filter to file
final String filename = getFilename("Best_Filter_File", settings.filterFilename);
if (filename != null) {
settings.filterFilename = filename;
Prefs.set(Settings.KEY_FILTER_FILENAME, filename);
final List<Filter> filters = new ArrayList<>(1);
filters.add(filter);
final FilterSet filterSet = new FilterSet(filter.getName(), filters);
saveFilterSet(filterSet, filename);
}
}
Aggregations