Search in sources :

Example 51 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class PulseActivationAnalysis method runSimulation.

private void runSimulation() {
    title += " Simulation";
    if (!showSimulationDialog()) {
        return;
    }
    final long start = System.currentTimeMillis();
    final UniformRandomProvider rng = getUniformRandomProvider();
    // Draw the molecule positions
    ImageJUtils.showStatus("Simulating molecules ...");
    final float[][][] molecules = new float[3][][];
    final MemoryPeakResults[] channelResults = new MemoryPeakResults[3];
    final Calibration calibration = CalibrationHelper.create(settings.nmPerPixel, 1, 100);
    final Rectangle bounds = new Rectangle(settings.size, settings.size);
    for (int c = 0; c < 3; c++) {
        molecules[c] = simulateMolecules(rng, c);
        // Create a dataset to store the activations
        final MemoryPeakResults r = new MemoryPeakResults();
        r.setCalibration(calibration);
        r.setBounds(bounds);
        r.setName(title + " C" + (c + 1));
        channelResults[c] = r;
    }
    // Simulate activation
    ImageJUtils.showStatus("Simulating activations ...");
    for (int c = 0; c < 3; c++) {
        simulateActivations(rng, molecules, c, channelResults);
    }
    // Combine
    ImageJUtils.showStatus("Producing simulation output ...");
    final MemoryPeakResults r = new MemoryPeakResults();
    r.setCalibration(calibration);
    r.setBounds((Rectangle) bounds.clone());
    r.setName(title);
    final ImageProcessor[] images = new ImageProcessor[3];
    for (int c = 0; c < 3; c++) {
        final PeakResult[] list = channelResults[c].toArray();
        r.addAll(list);
        // Draw the unmixed activations
        final ResultsImageSettings.Builder builder = ResultsImageSettings.newBuilder().setImageType(ResultsImageType.DRAW_LOCALISATIONS).setImageMode(ResultsImageMode.IMAGE_ADD).setWeighted(true).setEqualised(true).setImageSizeMode(ResultsImageSizeMode.IMAGE_SIZE).setImageSize(1024);
        final ImageJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(builder, title, bounds, settings.nmPerPixel);
        image.setCalibration(calibration);
        image.setLiveImage(false);
        image.setDisplayImage(false);
        image.begin();
        image.addAll(list);
        image.end();
        images[c] = image.getImagePlus().getProcessor();
    }
    displayComposite(images, title);
    // Add to memory. Set the composite dataset first.
    MemoryPeakResults.addResults(r);
    for (int c = 0; c < 3; c++) {
        MemoryPeakResults.addResults(channelResults[c]);
    }
    // TODO:
    // Show an image of what it looks like with no unmixing, i.e. colours allocated
    // from the frame
    ImageJUtils.showStatus("Simulation complete: " + TextUtils.millisToString(System.currentTimeMillis() - start));
}
Also used : Rectangle(java.awt.Rectangle) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) ResultsImageSettings(uk.ac.sussex.gdsc.smlm.data.config.ResultsProtos.ResultsImageSettings) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) IdPeakResult(uk.ac.sussex.gdsc.smlm.results.IdPeakResult) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) ImageProcessor(ij.process.ImageProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SplittableUniformRandomProvider(uk.ac.sussex.gdsc.core.utils.rng.SplittableUniformRandomProvider) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Example 52 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider 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 53 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class PoissonGradientProcedureTest method crlbIsHigherWithPrecomputed.

@SeededTest
void crlbIsHigherWithPrecomputed(RandomSeed seed) {
    final int iter = 10;
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final ErfGaussian2DFunction func = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(1, 10, 10, GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
    final double[] a = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
    final int n = func.getNumberOfGradients();
    // Get a background
    final double[] b = new double[func.size()];
    for (int i = 0; i < b.length; i++) {
        b[i] = nextUniform(rng, 1, 2);
    }
    for (int i = 0; i < iter; i++) {
        a[Gaussian2DFunction.BACKGROUND] = nextUniform(rng, 0.1, 0.3);
        a[Gaussian2DFunction.SIGNAL] = nextUniform(rng, 100, 300);
        a[Gaussian2DFunction.X_POSITION] = nextUniform(rng, 4, 6);
        a[Gaussian2DFunction.Y_POSITION] = nextUniform(rng, 4, 6);
        a[Gaussian2DFunction.X_SD] = nextUniform(rng, 1, 1.3);
        a[Gaussian2DFunction.Y_SD] = nextUniform(rng, 1, 1.3);
        final PoissonGradientProcedure p1 = PoissonGradientProcedureUtils.create(func);
        p1.computeFisherInformation(a);
        final PoissonGradientProcedure p2 = PoissonGradientProcedureUtils.create(OffsetGradient1Function.wrapGradient1Function(func, b));
        p2.computeFisherInformation(a);
        final FisherInformationMatrix m1 = new FisherInformationMatrix(p1.getLinear(), n);
        final FisherInformationMatrix m2 = new FisherInformationMatrix(p2.getLinear(), n);
        final double[] crlb1 = m1.crlb();
        final double[] crlb2 = m2.crlb();
        Assertions.assertNotNull(crlb1);
        Assertions.assertNotNull(crlb2);
        // Arrays.toString(crlb2));
        for (int j = 0; j < n; j++) {
            Assertions.assertTrue(crlb1[j] < crlb2[j]);
        }
    }
}
Also used : ErfGaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction) FisherInformationMatrix(uk.ac.sussex.gdsc.smlm.fitting.FisherInformationMatrix) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 54 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class LogFactorialTest method testLogFactorialDouble.

/**
 * Test the factorial of a fractional number against Commons Math logGamma(1+n).
 */
@SeededTest
void testLogFactorialDouble(RandomSeed seed) {
    final UniformRandomProvider rng = RngUtils.create(seed.getSeed());
    final DoubleDoubleBiPredicate tol = TestHelper.doublesAreClose(1e-14);
    for (int i = 0; i < 200; i++) {
        final double n = rng.nextDouble() * 200;
        final double expected = n <= 1.5 ? Gamma.logGamma1p(n) : Gamma.logGamma(1 + n);
        TestAssertions.assertTest(expected, LogFactorial.value(n), tol, () -> Double.toString(n));
    }
}
Also used : DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 55 with UniformRandomProvider

use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.

the class PoissonCalculatorTest method instanceAndFastMethodIsApproximatelyEqualToStaticMethod.

@SeededTest
void instanceAndFastMethodIsApproximatelyEqualToStaticMethod(RandomSeed seed) {
    final DoubleEquality eq = new DoubleEquality(3e-4, 0);
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    // Test for different x. The calculator approximation begins
    final int n = 100;
    final double[] u = new double[n];
    final double[] x = new double[n];
    double expected;
    double observed;
    for (final double testx : new double[] { Math.nextDown(PoissonCalculator.APPROXIMATION_X), PoissonCalculator.APPROXIMATION_X, Math.nextUp(PoissonCalculator.APPROXIMATION_X), PoissonCalculator.APPROXIMATION_X * 1.1, PoissonCalculator.APPROXIMATION_X * 2, PoissonCalculator.APPROXIMATION_X * 10 }) {
        final String X = Double.toString(testx);
        Arrays.fill(x, testx);
        final PoissonCalculator pc = new PoissonCalculator(x);
        expected = PoissonCalculator.maximumLogLikelihood(x);
        observed = pc.getMaximumLogLikelihood();
        logger.log(TestLogUtils.getRecord(LOG_LEVEL, "[%s] Instance MaxLL = %g vs %g (error = %g)", X, expected, observed, DoubleEquality.relativeError(expected, observed)));
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(expected, observed), () -> "Instance Max LL not equal: x=" + X);
        observed = PoissonCalculator.fastMaximumLogLikelihood(x);
        logger.log(TestLogUtils.getRecord(LOG_LEVEL, "[%s] Fast MaxLL = %g vs %g (error = %g)", X, expected, observed, DoubleEquality.relativeError(expected, observed)));
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(expected, observed), () -> "Fast Max LL not equal: x=" + X);
        // Generate data around the value
        for (int i = 0; i < n; i++) {
            u[i] = x[i] + rg.nextDouble() - 0.5;
        }
        expected = PoissonCalculator.logLikelihood(u, x);
        observed = pc.logLikelihood(u);
        logger.log(TestLogUtils.getRecord(LOG_LEVEL, "[%s] Instance LL = %g vs %g (error = %g)", X, expected, observed, DoubleEquality.relativeError(expected, observed)));
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(expected, observed), () -> "Instance LL not equal: x=" + X);
        observed = PoissonCalculator.fastLogLikelihood(u, x);
        logger.log(TestLogUtils.getRecord(LOG_LEVEL, "[%s] Fast LL = %g vs %g (error = %g)", X, expected, observed, DoubleEquality.relativeError(expected, observed)));
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(expected, observed), () -> "Fast LL not equal: x=" + X);
        expected = PoissonCalculator.logLikelihoodRatio(u, x);
        observed = pc.getLogLikelihoodRatio(observed);
        logger.log(TestLogUtils.getRecord(LOG_LEVEL, "[%s] Instance LLR = %g vs %g (error = %g)", X, expected, observed, DoubleEquality.relativeError(expected, observed)));
        Assertions.assertTrue(eq.almostEqualRelativeOrAbsolute(expected, observed), () -> "Instance LLR not equal: x=" + X);
    }
}
Also used : DoubleEquality(uk.ac.sussex.gdsc.core.utils.DoubleEquality) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)213 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)145 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)17 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)14 Rectangle (java.awt.Rectangle)13 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)13 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)12 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)10 ArrayList (java.util.ArrayList)10 NormalizedGaussianSampler (org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)9 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)8 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)8 ContinuousSampler (org.apache.commons.rng.sampling.distribution.ContinuousSampler)6 ImageExtractor (uk.ac.sussex.gdsc.core.utils.ImageExtractor)6 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)6 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)6 FloatProcessor (ij.process.FloatProcessor)5 ErfGaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.erf.ErfGaussian2DFunction)5 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)5 File (java.io.File)4