use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method saveFilters.
private static boolean saveFilters(String filename, ArrayList<Filter> filters) {
final ArrayList<FilterSet> filterList = new ArrayList<>(1);
// Add Range keyword to identify as a range filter set
filterList.add(new FilterSet("Range", filters));
try (FileOutputStream fos = new FileOutputStream(filename)) {
// Use the instance (not .toXML() method) to allow the exception to be caught
FilterXStreamUtils.getXStreamInstance().toXML(filterList, fos);
return true;
} catch (final Exception ex) {
IJ.log("Unable to save the filter set to file: " + ex.getMessage());
}
return false;
}
use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method runAnalysis.
private void runAnalysis(List<FilterSet> filterSets, ComplexFilterScore optimum, double rangeReduction) {
filterAnalysisResult.plots.clear();
filterAnalysisResult.bestFilter.clear();
getCoordinateStore();
filterAnalysisStopWatch = StopWatch.createStarted();
IJ.showStatus("Analysing filters ...");
int setNumber = 0;
final DirectFilter currentOptimum = (optimum != null) ? optimum.result.filter : null;
for (final FilterSet filterSet : filterSets) {
setNumber++;
if (filterAnalysis(filterSet, setNumber, currentOptimum, rangeReduction) < 0) {
break;
}
}
filterAnalysisStopWatch.stop();
ImageJUtils.finished();
final String timeString = filterAnalysisStopWatch.toString();
IJ.log("Filter analysis time : " + timeString);
}
use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method iterate.
private void iterate() {
// If this is run again immediately then provide options for reporting the results
final Map<String, ComplexFilterScore> localIterBestFilter = iterBestFilter.get();
// the result is from an iteration analysis.
if (filterAnalysisResult.bestFilter.equals(localIterBestFilter)) {
final GenericDialog gd = new GenericDialog(TITLE);
gd.enableYesNoCancel();
gd.addMessage("Iteration results are held in memory.\n \nReport these results?");
gd.addHelp(HelpUrls.getUrl("iterate-filter-analysis"));
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
if (gd.wasOKed()) {
reportIterationResults();
return;
}
}
// Show this dialog first so we can run fully automated after interactive dialogs
if (!showIterationDialog()) {
return;
}
// Total the time from the interactive plugins
long time = 0;
// Run the benchmark fit once interactively, keep the instance
final BenchmarkSpotFit fit = new BenchmarkSpotFit();
// than has been optimised before)
if (fit.resetMultiPathFilter() || invalidBenchmarkSpotFitResults(true)) {
fit.run(null);
if (!fit.isFinished()) {
// The plugin did not complete
return;
}
resetParametersFromFitting();
}
if (invalidBenchmarkSpotFitResults(false)) {
return;
}
if (spotFitResults.stopWatch != null) {
time += spotFitResults.stopWatch.getTime();
}
// Run filter analysis once interactively
if (!loadFitResults()) {
return;
}
// Collect parameters for optimising the parameters
if (!showDialog(FLAG_OPTIMISE_FILTER | FLAG_OPTIMISE_PARAMS)) {
return;
}
// Load filters from file
final List<FilterSet> filterSets = readFilterSets();
if (filterSets == null || filterSets.isEmpty()) {
IJ.error(TITLE, "No filters specified");
return;
}
ComplexFilterScore current = analyse(filterSets);
if (current == null) {
return;
}
time += filterAnalysisStopWatch.getTime();
current = analyseParameters(current);
if (current == null) {
return;
}
time += parameterAnalysisStopWatch.getTime();
// Time the non-interactive plugins as a continuous section
iterationStopWatch = StopWatch.createStarted();
// Remove the previous iteration results
iterBestFilter.set(null);
ImageJUtils.log(TITLE + " Iterating ...");
final IterationConvergenceChecker checker = new IterationConvergenceChecker(current);
// Iterate ...
boolean outerConverged = false;
int outerIteration = 1;
double outerRangeReduction = 1;
while (!outerConverged) {
if (settings.iterationConvergeBeforeRefit) {
// Optional inner loop so that the non-filter and filter parameters converge
// before a refit
boolean innerConverged = false;
double innerRangeReduction = 1;
// innerRangeReduction *= outerRangeReduction
while (!innerConverged) {
final ComplexFilterScore previous = current;
// Re-use the filters as the user may be loading a custom set.
current = analyse(filterSets, current, innerRangeReduction);
if (current == null) {
return;
}
final double[] previousParameters = createParameters();
current = analyseParameters(current, innerRangeReduction);
if (current == null) {
return;
}
final double[] currentParameters = createParameters();
innerConverged = checker.converged("Filter", previous, current, previousParameters, currentParameters);
}
// Check if we can continue (e.g. not max iterations or escape pressed)
if (!checker.canContinue) {
break;
}
}
// Do the fit (using the current optimum filter)
fit.run(current.result.filter, residualsThreshold, settings.failCount, settings.duplicateDistance, settings.duplicateDistanceAbsolute);
if (invalidBenchmarkSpotFitResults(false)) {
return;
}
if (!loadFitResults()) {
return;
}
// is centred around the current optimum.
if (settings.iterationMinRangeReduction < 1) {
// Linear interpolate down to the min range reduction
outerRangeReduction = MathUtils.max(settings.iterationMinRangeReduction, MathUtils.interpolateY(0, 1, settings.iterationMinRangeReductionIteration, settings.iterationMinRangeReduction, outerIteration++));
}
// Optimise the filter again.
final ComplexFilterScore previous = current;
// Re-use the filters as the user may be loading a custom set.
current = analyse(filterSets, current, outerRangeReduction);
if (current == null) {
break;
}
final double[] previousParameters = createParameters();
current = analyseParameters(current, outerRangeReduction);
if (current == null) {
return;
}
final double[] currentParameters = createParameters();
outerConverged = checker.converged("Fit+Filter", previous, current, previousParameters, currentParameters);
}
if (current != null) {
// Set-up the plugin so that it can be run again (in iterative mode)
// and the results reported for the top filter.
// If the user runs the non-iterative mode then the results will be lost.
iterBestFilter.set(filterAnalysisResult.bestFilter);
}
time += iterationStopWatch.getTime();
IJ.log("Iteration analysis time : " + TextUtils.millisToString(time));
showFinished();
}
use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method initialise.
@Override
public void initialise(List<? extends Chromosome<FilterScore>> individuals) {
gaIteration++;
gaScoreIndex = 0;
gaScoreResults = scoreFilters(setStrength(new FilterSet(populationToFilters(individuals))), false);
}
use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method expandFilters.
/**
* If filters have been provided in FiltersSets of 3 then expand the filters into a set assuming
* the three represent min:max:increment.
*
* @param filterSets the filter sets
* @return the expanded filter sets
*/
private List<FilterSet> expandFilters(List<FilterSet> filterSets) {
// Note:
// Do not clear the search range map and step size map when reading a new set of filters.
// The filters may be the same with slight modifications and so it is useful to keep the last
// settings.
final long[] expanded = new long[filterSets.size()];
final String[] name = new String[expanded.length];
int count = 0;
boolean doIt = false;
for (final FilterSet filterSet : filterSets) {
if (filterSet.size() == 3 && filterSet.allSameType()) {
name[count] = filterSet.getName();
// Check we have min:max:increment by counting the combinations
final Filter f1 = filterSet.getFilters().get(0);
final Filter f2 = filterSet.getFilters().get(1);
final Filter f3 = filterSet.getFilters().get(2);
final int n = f1.getNumberOfParameters();
final double[] parameters = new double[n];
final double[] parameters2 = new double[n];
final double[] increment = new double[n];
for (int i = 0; i < n; i++) {
parameters[i] = f1.getParameterValue(i);
parameters2[i] = f2.getParameterValue(i);
increment[i] = f3.getParameterValue(i);
}
final long combinations = countCombinations(parameters, parameters2, increment);
if (combinations > 1) {
expanded[count] = combinations;
doIt = true;
}
}
count++;
}
if (!doIt) {
return filterSets;
}
final GenericDialog gd = new GenericDialog(TITLE);
gd.hideCancelButton();
final StringBuilder sb = new StringBuilder("The filter file contains potential triples of min:max:increment.\n \n");
for (count = 0; count < expanded.length; count++) {
if (expanded[count] > 0) {
sb.append("Expand set [").append((count + 1)).append("]");
if (!TextUtils.isNullOrEmpty(name[count])) {
sb.append(" ").append(name[count]);
}
sb.append(" to ").append(expanded[count]).append(" filters\n");
}
}
gd.addMessage(sb.toString());
gd.addCheckbox("Expand_filters", settings.expandFilters);
gd.showDialog();
if (!gd.wasCanceled()) {
settings.expandFilters = gd.getNextBoolean();
if (!settings.expandFilters) {
return filterSets;
}
}
IJ.showStatus("Expanding filters ...");
final List<FilterSet> filterList2 = new ArrayList<>(filterSets.size());
for (final FilterSet filterSet : filterSets) {
count = filterList2.size();
if (expanded[count] == 0) {
filterList2.add(filterSet);
continue;
}
final Filter f1 = filterSet.getFilters().get(0);
final Filter f2 = filterSet.getFilters().get(1);
final Filter f3 = filterSet.getFilters().get(2);
final int n = f1.getNumberOfParameters();
final double[] parameters = new double[n];
final double[] parameters2 = new double[n];
final double[] increment = new double[n];
for (int i = 0; i < n; i++) {
parameters[i] = f1.getParameterValue(i);
parameters2[i] = f2.getParameterValue(i);
increment[i] = f3.getParameterValue(i);
}
final List<Filter> list = expandFilters(f1, parameters, parameters2, increment);
filterList2.add(new FilterSet(filterSet.getName(), list));
}
IJ.showStatus("");
ImageJUtils.log("Expanded input to %d filters in %s", countFilters(filterList2), TextUtils.pleural(filterList2.size(), "set"));
return filterList2;
}
Aggregations