use of uk.ac.sussex.gdsc.ij.foci.FindFociProcessorOptions.AlgorithmOption in project gdsc by aherbert.
the class FindFociOptimiser_PlugIn method runOptimiser.
/**
* Enumerate the parameters for FindFoci on the provided image.
*
* <p>Returns null if the image is invalid, there are no ROI points inside the mask, the algorithm
* was cancelled or cannot produce results.
*
* @param imp The image
* @param mask The mask
* @param ticker the ticker
* @return The results
*/
@Nullable
private OptimiserResult runOptimiser(ImagePlus imp, ImagePlus mask, Ticker ticker) {
if (invalidImage(imp)) {
return null;
}
final AssignedPoint[] roiPoints = extractRoiPoints(imp, mask);
if (roiPoints.length == 0) {
IJ.showMessage("Error", "Image must have a point ROI or corresponding ROI file");
return null;
}
final ArrayList<Result> results = new ArrayList<>(combinations);
// Set the threshold for assigning points matches as a fraction of the image size
final double distanceThreshold = getDistanceThreshold(imp, settings.matchSearchMethod, settings.matchSearchDistance);
final ToDoubleBiFunction<Coordinate, Coordinate> distanceFunction = CoordinateUtils.getSquaredDistanceFunction(imp.getCalibration(), is3D(roiPoints));
// The stopwatch for the total run-time
final StopWatch sw = new StopWatch();
// The total time for analysis
long analysisTime = 0;
final FindFociBaseProcessor ff = new FindFoci_PlugIn().createFindFociProcessor(imp);
final FindFociProcessorOptions processorOptions = new FindFociProcessorOptions(true);
// Only one supported peak method.
// The parameter values for absolute height and relative height are on a different scale
// and using both methods is not yet supported.
processorOptions.setPeakMethod(settings.peakMethod);
processorOptions.setMaxPeaks(settings.maxPeaks);
int id = 0;
for (int blurCount = 0; blurCount < blurArray.length; blurCount++) {
final double blur = blurArray[blurCount];
processorOptions.setGaussianBlur(blur);
final StopWatch sw0 = new StopWatch();
final ImagePlus imp2 = ff.blur(imp, blur);
sw0.stop();
// Iterate over the options
int thresholdMethodIndex = 0;
for (int b = 0; b < backgroundMethodArray.length; b++) {
processorOptions.setBackgroundMethod(backgroundMethodArray[b]);
if (backgroundMethodArray[b] == BackgroundMethod.AUTO_THRESHOLD) {
processorOptions.setThresholdMethod(thresholdMethodArray[thresholdMethodIndex++]);
}
final StatisticsMethod[] statisticsMethods = backgroundMethodHasStatisticsMode(backgroundMethodArray[b]) ? statisticsMethodArray : new StatisticsMethod[] { StatisticsMethod.ALL };
for (final StatisticsMethod statisticsMethod : statisticsMethods) {
processorOptions.setStatisticsMethod(statisticsMethod);
final StopWatch sw1 = sw0.create();
final FindFociInitResults initResults = ff.findMaximaInit(imp, imp2, mask, processorOptions);
sw1.stop();
if (initResults == null) {
return null;
}
FindFociInitResults searchInitArray = null;
for (double backgroundParameter = backgroundParameterMinArray[b]; backgroundParameter <= backgroundParameterMax; backgroundParameter += backgroundParameterInterval) {
// Use zero when there is no parameter
processorOptions.setBackgroundParameter((backgroundMethodHasParameter(backgroundMethodArray[b])) ? backgroundParameter : 0);
// Log on first blur iteration
boolean logBackground = (blurCount == 0) && !multiMode;
for (int s = 0; s < searchMethodArray.length; s++) {
processorOptions.setSearchMethod(searchMethodArray[s]);
for (double searchParameter = searchParameterMinArray[s]; searchParameter <= searchParameterMax; searchParameter += searchParameterInterval) {
// Use zero when there is no parameter
processorOptions.setSearchParameter((searchMethodHasParameter(searchMethodArray[s])) ? searchParameter : 0);
searchInitArray = ff.copyForStagedProcessing(initResults, searchInitArray);
final StopWatch sw2 = sw1.create();
final FindFociSearchResults searchArray = ff.findMaximaSearch(searchInitArray, processorOptions);
sw2.stop();
if (searchArray == null) {
return null;
}
FindFociInitResults mergeInitArray = null;
if (logBackground) {
// Log the background level on the first occurrence
final float backgroundLevel = searchInitArray.stats.background;
logBackground = false;
IJ.log(String.format("Background level - %s %s: %s = %g", backgroundMethodArray[b].getDescription(), backgroundMethodHasStatisticsMode(backgroundMethodArray[b]) ? "(" + statisticsMethod + ") " : "", ((backgroundMethodHasParameter(backgroundMethodArray[b])) ? IJ.d2s(backgroundParameter, 2) : processorOptions.getThresholdMethod().getDescription()), backgroundLevel));
}
// Note: Currently only 1 PeakMethod is supported so there is no iteration over this
for (double peakParameter = peakParameterMin; peakParameter <= peakParameterMax; peakParameter += peakParameterInterval) {
processorOptions.setPeakParameter(peakParameter);
final StopWatch sw3 = sw2.create();
final FindFociMergeTempResults mergePeakResults = ff.findMaximaMergePeak(searchInitArray, searchArray, processorOptions);
sw3.stop();
for (int minSize = minSizeMin; minSize <= minSizeMax; minSize += minSizeInterval) {
processorOptions.setMinSize(minSize);
final StopWatch sw4 = sw3.create();
final FindFociMergeTempResults mergeSizeResults = ff.findMaximaMergeSize(searchInitArray, mergePeakResults, processorOptions);
sw4.stop();
for (final EnumSet<AlgorithmOption> options : optionsArray) {
processorOptions.setOptions(options);
mergeInitArray = ff.copyForStagedProcessing(searchInitArray, mergeInitArray);
final StopWatch sw5 = sw4.create();
final FindFociMergeResults mergeArray = ff.findMaximaMergeFinal(mergeInitArray, mergeSizeResults, processorOptions);
sw5.stop();
if (mergeArray == null) {
return null;
}
for (final SortMethod sortMethod : sortMethodArray) {
processorOptions.setSortMethod(sortMethod);
for (int c = 0; c < centreMethodArray.length; c++) {
processorOptions.setCentreMethod(centreMethodArray[c]);
for (double centreParameter = centreParameterMinArray[c]; centreParameter <= centreParameterMaxArray[c]; centreParameter += centreParameterIntervalArray[c]) {
processorOptions.setCentreParameter(centreParameter);
final StopWatch sw6 = sw5.create();
final FindFociResults peakResults = ff.findMaximaResults(mergeInitArray, mergeArray, processorOptions);
final long time = sw6.stop();
ticker.tick();
if (peakResults != null) {
// Get the results
// The analysis time is not included in the speed-up factor
final long start = System.nanoTime();
final Parameters runOptions = new Parameters(processorOptions);
final Result result = analyseResults(id, roiPoints, peakResults.results, distanceThreshold, runOptions, time, settings.beta, distanceFunction);
results.add(result);
analysisTime += System.nanoTime() - start;
}
id++;
if (IJ.escapePressed()) {
return null;
}
}
}
}
}
}
}
}
}
}
}
}
}
sw.stop();
// All possible results sort methods are highest first
sortResults(results, settings.resultsSortMethod);
return new OptimiserResult(results, sw.getTime(), analysisTime);
}
Aggregations