Search in sources :

Example 1 with FilterSet

use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet 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;
}
Also used : SimpleRecombiner(uk.ac.sussex.gdsc.smlm.ga.SimpleRecombiner) SearchSpace(uk.ac.sussex.gdsc.smlm.search.SearchSpace) ArrayList(java.util.ArrayList) SearchDimension(uk.ac.sussex.gdsc.smlm.search.SearchDimension) FixedDimension(uk.ac.sussex.gdsc.smlm.search.FixedDimension) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) GenericDialog(ij.gui.GenericDialog) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) ParameterType(uk.ac.sussex.gdsc.smlm.results.filter.ParameterType) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) SimpleMutator(uk.ac.sussex.gdsc.smlm.ga.SimpleMutator) FilterScore(uk.ac.sussex.gdsc.smlm.results.filter.FilterScore)

Example 2 with FilterSet

use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet 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);
    }
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) ArrayList(java.util.ArrayList)

Example 3 with FilterSet

use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method score.

@Nullable
@Override
public SearchResult<FilterScore>[] score(double[][] points) {
    gaIteration++;
    SimpleFilterScore max = filterScoreOptimum;
    final FilterScoreResult[] scoreResults = scoreFilters(setStrength(new FilterSet(searchSpaceToFilters(points))), false);
    if (scoreResults == null) {
        return null;
    }
    @SuppressWarnings("unchecked") final SearchResult<FilterScore>[] scores = new SearchResult[scoreResults.length];
    for (int index = 0; index < scoreResults.length; index++) {
        final FilterScoreResult scoreResult = scoreResults[index];
        final SimpleFilterScore result = new SimpleFilterScore(scoreResult, true, scoreResult.criteria >= minCriteria);
        if (result.compareTo(max) < 0) {
            max = result;
        }
        scores[index] = new SearchResult<>(result.result.filter.getParameters(), result);
    }
    filterScoreOptimum = max;
    // Add the best filter to the table
    // This filter may not have been part of the scored subset so use the entire results set for
    // reporting
    final DirectFilter filter = max.result.filter;
    final FractionClassificationResult r = scoreFilter(filter, defaultMinimalFilter, gaResultsList, coordinateStore);
    final StringBuilder text = createResult(filter, r);
    add(text, gaIteration);
    gaWindow.accept(text.toString());
    return scores;
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) FractionClassificationResult(uk.ac.sussex.gdsc.core.match.FractionClassificationResult) SearchResult(uk.ac.sussex.gdsc.smlm.search.SearchResult) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 4 with FilterSet

use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method readFilterSets.

@Nullable
@SuppressWarnings("unchecked")
private List<FilterSet> readFilterSets() {
    if (extraOptions) {
        final MultiPathFilter multiFilter = BenchmarkSpotFit.getMultiFilter();
        if (multiFilter != null) {
            final IDirectFilter f = multiFilter.getFilter();
            if (f instanceof DirectFilter) {
                final GenericDialog gd = new GenericDialog(TITLE);
                gd.addMessage("Use an identical filter to " + BenchmarkSpotFit.TITLE);
                gd.enableYesNoCancel();
                gd.hideCancelButton();
                gd.showDialog();
                if (gd.wasOKed()) {
                    final List<FilterSet> filterSets = new ArrayList<>(1);
                    final List<Filter> filters = new ArrayList<>(1);
                    filters.add((DirectFilter) f);
                    final FilterSet filterSet = new FilterSet(filters);
                    filterSets.add(filterSet);
                    resetParametersFromFitting();
                    createResultsPrefix2();
                    return filterSets;
                }
            }
        }
    }
    GUIFilterSettings filterSettings = SettingsManager.readGuiFilterSettings(0);
    final String filename = ImageJUtils.getFilename("Filter_File", filterSettings.getFilterSetFilename());
    if (filename != null) {
        IJ.showStatus("Reading filters ...");
        filterSettings = filterSettings.toBuilder().setFilterSetFilename(filename).build();
        // Allow the filters to be cached
        final Triple<String, Long, List<FilterSet>> filterCache = lastFilterList.get();
        if (isSameFile(filename, filterCache)) {
            final GenericDialog gd = new GenericDialog(TITLE);
            gd.hideCancelButton();
            gd.addMessage("The same filter file was selected.");
            gd.addCheckbox("Re-use_filters", settings.reUseFilters);
            gd.showDialog();
            if (!gd.wasCanceled()) {
                settings.reUseFilters = gd.getNextBoolean();
                if (settings.reUseFilters) {
                    SettingsManager.writeSettings(filterSettings);
                    return filterCache.getRight();
                }
            }
        }
        final File file = new File(filename);
        try (BufferedReader input = new BufferedReader(new UnicodeReader(new FileInputStream(file), null))) {
            // Use the instance so we can catch the exception
            final Object o = FilterXStreamUtils.getXStreamInstance().fromXML(input);
            if (!(o instanceof List<?>)) {
                IJ.log("No filter sets defined in the specified file: " + filename);
                return null;
            }
            SettingsManager.writeSettings(filterSettings);
            List<FilterSet> filterSets = (List<FilterSet>) o;
            if (containsStandardFilters(filterSets)) {
                IJ.log("Filter sets must contain 'Direct' filters");
                return null;
            }
            // Check they are not empty lists
            final List<FilterSet> filterSets2 = new LinkedList<>();
            for (final FilterSet filterSet : filterSets) {
                if (filterSet.size() != 0) {
                    filterSets2.add(filterSet);
                } else {
                    IJ.log("Filter set empty: " + filterSet.getName());
                }
            }
            if (filterSets2.isEmpty()) {
                IJ.log("All Filter sets are empty");
                return null;
            }
            // Maintain the same list type
            filterSets.clear();
            filterSets.addAll(filterSets2);
            // Option to enumerate filters
            filterSets = expandFilters(filterSets);
            // Save for re-use
            lastFilterList.set(Triple.of(filename, getLastModified(file), filterSets));
            return filterSets;
        } catch (final Exception ex) {
            IJ.log("Unable to load the filter sets from file: " + ex.getMessage());
        } finally {
            IJ.showStatus("");
        }
    }
    return null;
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) ArrayList(java.util.ArrayList) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) GUIFilterSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.GUIFilterSettings) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) GenericDialog(ij.gui.GenericDialog) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) SettingsList(uk.ac.sussex.gdsc.core.utils.SettingsList) List(java.util.List) LinkedList(java.util.LinkedList) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) File(java.io.File) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 5 with FilterSet

use of uk.ac.sussex.gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method saveFilterSet.

private static void saveFilterSet(FilterSet filterSet, String filename) {
    try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8")) {
        final List<FilterSet> list = new ArrayList<>(1);
        list.add(filterSet);
        // Use the instance so we can catch the exception
        out.write(FilterXStreamUtils.getXStreamInstance().toXML(list));
    } catch (final Exception ex) {
        IJ.log("Unable to save the filter sets to file: " + ex.getMessage());
    }
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)

Aggregations

FilterSet (uk.ac.sussex.gdsc.smlm.results.filter.FilterSet)17 Filter (uk.ac.sussex.gdsc.smlm.results.filter.Filter)9 DirectFilter (uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter)7 IDirectFilter (uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter)7 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 AndFilter (uk.ac.sussex.gdsc.smlm.results.filter.AndFilter)5 OrFilter (uk.ac.sussex.gdsc.smlm.results.filter.OrFilter)5 PrecisionFilter (uk.ac.sussex.gdsc.smlm.results.filter.PrecisionFilter)5 PrecisionHysteresisFilter (uk.ac.sussex.gdsc.smlm.results.filter.PrecisionHysteresisFilter)5 SnrFilter (uk.ac.sussex.gdsc.smlm.results.filter.SnrFilter)5 SnrHysteresisFilter (uk.ac.sussex.gdsc.smlm.results.filter.SnrHysteresisFilter)5 TraceFilter (uk.ac.sussex.gdsc.smlm.results.filter.TraceFilter)5 WidthFilter (uk.ac.sussex.gdsc.smlm.results.filter.WidthFilter)5 GenericDialog (ij.gui.GenericDialog)4 NonBlockingGenericDialog (ij.gui.NonBlockingGenericDialog)4 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)4 MaximaSpotFilter (uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter)4 MultiPathFilter (uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter)4 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)3