Search in sources :

Example 21 with Ticker

use of uk.ac.sussex.gdsc.core.logging.Ticker in project GDSC-SMLM by aherbert.

the class CubicSplineData method read.

/**
 * Read a tricubic spline from the input stream.
 *
 * <p>Note: For best performance a buffered input stream should be used.
 *
 * @param inputStream the input stream
 * @param progress the progress
 * @return the tricubic spline data
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static CubicSplineData read(InputStream inputStream, TrackProgress progress) throws IOException {
    // Read dimensions
    final DataInput in = new DataInputStream(inputStream);
    final int maxx = in.readInt();
    final int maxy = in.readInt();
    final int maxz = in.readInt();
    final Ticker ticker = Ticker.createStarted(progress, (long) maxx * maxy * maxz, false);
    // Read precision
    final boolean singlePrecision = in.readBoolean();
    final SplineReader reader = (singlePrecision) ? new FloatSplineReader() : new DoubleSplineReader();
    final int size = maxx * maxy;
    final CustomTricubicFunction[][] splines = new CustomTricubicFunction[maxz][maxx * maxy];
    for (int z = 0; z < maxz; z++) {
        for (int i = 0; i < size; i++) {
            splines[z][i] = reader.read(in);
            ticker.tick();
        }
    }
    ticker.stop();
    // Skip validation
    return new CubicSplineData(maxx, maxy, splines, false);
}
Also used : DataInput(java.io.DataInput) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) DataInputStream(java.io.DataInputStream) FloatCustomTricubicFunction(uk.ac.sussex.gdsc.core.math.interpolation.FloatCustomTricubicFunction) CustomTricubicFunction(uk.ac.sussex.gdsc.core.math.interpolation.CustomTricubicFunction)

Example 22 with Ticker

use of uk.ac.sussex.gdsc.core.logging.Ticker in project GDSC-SMLM by aherbert.

the class SpotAnalysis method runCreateProfile.

private void runCreateProfile(ImagePlus imp, Rectangle bounds, double psfWidth, double blur) {
    areaBounds = bounds;
    this.imp = imp;
    area = bounds.width * bounds.height;
    clearSelectedFrames();
    // Get a profile through the images
    IJ.showStatus("Calculating raw profile");
    final int nSlices = imp.getStackSize();
    final ImageStack rawSpot = new ImageStack(bounds.width, bounds.height, nSlices);
    final double[][] profile = extractSpotProfile(imp, bounds, rawSpot);
    // Retain the existing display range
    double min = 0;
    double max = Double.POSITIVE_INFINITY;
    if (rawImp != null) {
        min = rawImp.getDisplayRangeMin();
        max = rawImp.getDisplayRangeMax();
    }
    rawImp = showSpot(RAW_SPLOT_TITLE, rawSpot);
    if (max != Double.POSITIVE_INFINITY) {
        rawImp.setDisplayRange(min, max);
    }
    rawMean = profile[0];
    rawSd = profile[1];
    // Check if there are fitted results in memory
    addCandidateFrames(imp.getTitle());
    updateProfilePlots();
    if (blur > 0) {
        IJ.showStatus("Calculating blur ...");
        final ImageStack stack = imp.getImageStack();
        final ImageStack newStack = new ImageStack(stack.getWidth(), stack.getHeight(), stack.getSize());
        // Multi-thread the blur stage
        final ExecutorService threadPool = Executors.newFixedThreadPool(Prefs.getThreads());
        final List<Future<?>> futures = new LinkedList<>();
        final Ticker ticker = Ticker.create(new ImageJTrackProgress(true), nSlices, true);
        blurCount = 0;
        final int slices = 5;
        ImageJUtils.showSlowProgress(0, nSlices);
        for (int n = 1; n <= nSlices; n += slices) {
            futures.add(threadPool.submit(new BlurWorker(ticker, stack, n, slices, bounds, blur * psfWidth, newStack)));
        }
        IJ.showStatus("Calculating blur ... Finishing");
        threadPool.shutdown();
        ConcurrencyUtils.waitForCompletionUnchecked(futures);
        ImageJUtils.clearSlowProgress();
        IJ.showStatus("Calculating blur ... Drawing");
        final ImageStack blurSpot = new ImageStack(bounds.width, bounds.height, nSlices);
        extractSpotProfile(new ImagePlus("Blur", newStack), bounds, blurSpot);
        // Retain the existing display range
        max = Double.POSITIVE_INFINITY;
        if (blurImp != null) {
            min = blurImp.getDisplayRangeMin();
            max = blurImp.getDisplayRangeMax();
        }
        blurImp = showSpot(BLUR_SPOT_TITLE, blurSpot);
        if (max != Double.POSITIVE_INFINITY) {
            blurImp.setDisplayRange(min, max);
        }
        IJ.showStatus("");
    } else {
        blurImp = null;
    }
    // Add a z-projection of the blur/original image
    final ZProjector project = new ZProjector((blurImp == null) ? rawImp : blurImp);
    project.setMethod(ZProjector.AVG_METHOD);
    project.doProjection();
    showSpot(AVG_SPOT_TITLE, project.getProjection().getImageStack());
    if (!candidateFrames.isEmpty()) {
        // Set the first candidate frame
        rawImp.setSlice(candidateFrames.get(0));
    } else {
        updateCurrentSlice(rawImp.getCurrentSlice());
    }
    IJ.showStatus("");
}
Also used : ImageStack(ij.ImageStack) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) ImagePlus(ij.ImagePlus) Point(java.awt.Point) LinkedList(java.util.LinkedList) ZProjector(ij.plugin.ZProjector) ImageJTrackProgress(uk.ac.sussex.gdsc.core.ij.ImageJTrackProgress) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 23 with Ticker

use of uk.ac.sussex.gdsc.core.logging.Ticker in project GDSC-SMLM by aherbert.

the class CameraModelFisherInformationAnalysis method getAlpha.

private double[] getAlpha(final double[] photons, double[] exp, final BasePoissonFisherInformation fi, FiKey key) {
    final CameraType type = key.getType();
    final double[] alpha = new double[photons.length];
    if (!type.isFast()) {
        final int[] index;
        // Try and load from the cache
        final PoissonFisherInformationData data = load(key);
        if (data != null) {
            // Dump the samples
            final TDoubleArrayList meanList = new TDoubleArrayList(data.getAlphaSampleCount());
            final TDoubleArrayList alphalist = new TDoubleArrayList(data.getAlphaSampleCount());
            for (final AlphaSample sample : data.getAlphaSampleList()) {
                meanList.add(sample.getLog10Mean());
                alphalist.add(sample.getAlpha());
            }
            final double[] exp2 = meanList.toArray();
            final double[] alphas = alphalist.toArray();
            SortUtils.sortData(alphas, exp2, true, false);
            // Find any exponent not in the array
            final TIntArrayList list = new TIntArrayList(exp.length);
            for (int i = 0; i < exp.length; i++) {
                // Assume exp2 is sorted
                final int j = Arrays.binarySearch(exp2, exp[i]);
                if (j < 0) {
                    // Add to indices to compute
                    list.add(i);
                } else {
                    // Get alpha
                    alpha[i] = alphas[j];
                }
            }
            index = list.toArray();
        } else {
            // Compute all
            index = SimpleArrayUtils.natural(alpha.length);
        }
        if (index.length > 0) {
            IJ.showStatus("Computing " + getName(key));
            final int nThreads = Prefs.getThreads();
            if (es == null) {
                es = Executors.newFixedThreadPool(nThreads);
            }
            final Ticker ticker = ImageJUtils.createTicker(index.length, nThreads);
            final int nPerThread = (int) Math.ceil((double) index.length / nThreads);
            final LocalList<Future<?>> futures = new LocalList<>(nThreads);
            for (int i = 0; i < index.length; i += nPerThread) {
                final int start = i;
                final int end = Math.min(index.length, i + nPerThread);
                futures.add(es.submit(() -> {
                    final BasePoissonFisherInformation fi2 = fi.copy();
                    for (int ii = start; ii < end; ii++) {
                        final int j = index[ii];
                        alpha[j] = fi2.getAlpha(photons[j]);
                        ticker.tick();
                    }
                }));
            }
            ConcurrencyUtils.waitForCompletionUnchecked(futures);
            ImageJUtils.finished();
            save(key, exp, alpha);
        }
    } else {
        // Simple single threaded method.
        for (int i = 0; i < alpha.length; i++) {
            alpha[i] = fi.getAlpha(photons[i]);
        }
    }
    return alpha;
}
Also used : Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) AlphaSample(uk.ac.sussex.gdsc.smlm.data.config.FisherProtos.AlphaSample) TIntArrayList(gnu.trove.list.array.TIntArrayList) PoissonFisherInformationData(uk.ac.sussex.gdsc.smlm.data.config.FisherProtos.PoissonFisherInformationData) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) BasePoissonFisherInformation(uk.ac.sussex.gdsc.smlm.function.BasePoissonFisherInformation) Future(java.util.concurrent.Future)

Example 24 with Ticker

use of uk.ac.sussex.gdsc.core.logging.Ticker in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method msdAnalysis.

/**
 * Tabulate the observed MSD for different jump distances.
 *
 * @param points the points
 */
private void msdAnalysis(ArrayList<Point> points) {
    if (myMsdAnalysisSteps == 0) {
        return;
    }
    IJ.showStatus("MSD analysis ...");
    IJ.showProgress(1, myMsdAnalysisSteps);
    // This will only be fast if the list is an array
    final Point[] list = points.toArray(new Point[0]);
    // Compute the base MSD
    final Point origin = new Point(0, 0, 0);
    double sum = origin.distance2(list[0]);
    int count = 1;
    for (int i = 1; i < list.length; i++) {
        final Point last = list[i - 1];
        final Point current = list[i];
        if (last.id == current.id) {
            sum += last.distance2(current);
        } else {
            sum += origin.distance2(current);
        }
        count++;
    }
    // Create a new set of points that have coordinates that
    // are the rolling average over the number of aggregate steps
    final DoubleRollingArray x = new DoubleRollingArray(pluginSettings.aggregateSteps);
    final DoubleRollingArray y = new DoubleRollingArray(pluginSettings.aggregateSteps);
    int id = 0;
    int length = 0;
    for (final Point p : points) {
        if (p.id != id) {
            x.clear();
            y.clear();
        }
        id = p.id;
        x.add(p.x);
        y.add(p.y);
        // Only create a point if the full aggregation size is reached
        if (x.isFull()) {
            list[length++] = new Point(id, x.getAverage(), y.getAverage());
        }
    }
    // Q - is this useful?
    final double p = myPrecision / settings.getPixelPitch();
    final UniformRandomProvider rng = UniformRandomProviders.create();
    final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
    final int totalSteps = (int) Math.ceil(settings.getSeconds() * settings.getStepsPerSecond() - pluginSettings.aggregateSteps);
    final int limit = Math.min(totalSteps, myMsdAnalysisSteps);
    final Ticker ticker = ImageJUtils.createTicker(limit, 1);
    final TextWindow msdTable = createMsdTable((sum / count) * settings.getStepsPerSecond() / conversionFactor);
    try (BufferedTextWindow bw = new BufferedTextWindow(msdTable)) {
        bw.setIncrement(0);
        for (int step = 1; step <= myMsdAnalysisSteps; step++) {
            sum = 0;
            count = 0;
            for (int i = step; i < length; i++) {
                final Point last = list[i - step];
                final Point current = list[i];
                if (last.id == current.id) {
                    if (p == 0) {
                        sum += last.distance2(current);
                        count++;
                    } else {
                        // is the same if enough samples are present
                        for (int ii = 1; ii-- > 0; ) {
                            sum += last.distance2(current, p, gauss);
                            count++;
                        }
                    }
                }
            }
            if (count == 0) {
                break;
            }
            bw.append(addResult(step, sum, count));
            ticker.tick();
        }
    }
    IJ.showProgress(1);
}
Also used : TextWindow(ij.text.TextWindow) BufferedTextWindow(uk.ac.sussex.gdsc.core.ij.BufferedTextWindow) BufferedTextWindow(uk.ac.sussex.gdsc.core.ij.BufferedTextWindow) DoubleRollingArray(uk.ac.sussex.gdsc.core.utils.DoubleRollingArray) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)

Example 25 with Ticker

use of uk.ac.sussex.gdsc.core.logging.Ticker in project GDSC-SMLM by aherbert.

the class BenchmarkSpotFit method runFitting.

private BenchmarkSpotFitResult runFitting() {
    // Extract all the results in memory into a list per frame. This can be cached
    boolean refresh = false;
    Pair<Integer, TIntObjectHashMap<List<Coordinate>>> coords = coordinateCache.get();
    if (coords.getKey() != simulationParameters.id) {
        // Do not get integer coordinates
        // The Coordinate objects will be PeakResultPoint objects that store the original PeakResult
        // from the MemoryPeakResults
        coords = Pair.of(simulationParameters.id, ResultsMatchCalculator.getCoordinates(results, false));
        coordinateCache.set(coords);
        refresh = true;
    }
    final TIntObjectHashMap<List<Coordinate>> actualCoordinates = coords.getValue();
    // Extract all the candidates into a list per frame. This can be cached if the settings have not
    // changed
    final int width = (config.isIncludeNeighbours()) ? config.getFittingWidth() : 0;
    CandidateData candidateData = candidateDataCache.get();
    if (refresh || candidateData == null || candidateData.differentSettings(filterResult.id, settings, width)) {
        candidateData = subsetFilterResults(filterResult.filterResults, width);
        candidateDataCache.set(candidateData);
    }
    final StopWatch stopWatch = StopWatch.createStarted();
    final ImageStack stack = imp.getImageStack();
    clearFitResults();
    // Save results to memory
    final MemoryPeakResults peakResults = new MemoryPeakResults();
    peakResults.copySettings(this.results);
    peakResults.setName(TITLE);
    config.configureOutputUnits();
    final FitConfiguration fitConfig = config.getFitConfiguration();
    peakResults.setCalibration(fitConfig.getCalibration());
    MemoryPeakResults.addResults(peakResults);
    // Create a pool of workers
    final int nThreads = Prefs.getThreads();
    final BlockingQueue<Integer> jobs = new ArrayBlockingQueue<>(nThreads * 2);
    final List<Worker> workers = new LinkedList<>();
    final List<Thread> threads = new LinkedList<>();
    final Ticker ticker = ImageJUtils.createTicker(stack.getSize(), nThreads, "Fitting frames ...");
    final PeakResults syncResults = SynchronizedPeakResults.create(peakResults, nThreads);
    for (int i = 0; i < nThreads; i++) {
        final Worker worker = new Worker(jobs, stack, actualCoordinates, candidateData.filterCandidates, syncResults, ticker);
        final Thread t = new Thread(worker);
        workers.add(worker);
        threads.add(t);
        t.start();
    }
    // Fit the frames
    final long startTime = System.nanoTime();
    for (int i = 1; i <= stack.getSize(); i++) {
        put(jobs, i);
    }
    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < threads.size(); i++) {
        put(jobs, -1);
    }
    // Wait for all to finish
    for (int i = 0; i < threads.size(); i++) {
        try {
            threads.get(i).join();
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new ConcurrentRuntimeException(ex);
        }
    }
    final long runTime = System.nanoTime() - startTime;
    threads.clear();
    ImageJUtils.finished();
    if (ImageJUtils.isInterrupted()) {
        return null;
    }
    stopWatch.stop();
    final String timeString = stopWatch.toString();
    IJ.log("Spot fit time : " + timeString);
    IJ.showStatus("Collecting results ...");
    if (fitConfig.isFitCameraCounts()) {
        // Convert to photons for consistency
        results.convertToPreferredUnits();
    }
    final TIntObjectHashMap<FilterCandidates> fitResults = new TIntObjectHashMap<>();
    for (final Worker w : workers) {
        fitResults.putAll(w.results);
    }
    // Assign a unique ID to each result
    int count = 0;
    // Materialise into an array since we use it twice
    final FilterCandidates[] candidates = fitResults.values(new FilterCandidates[fitResults.size()]);
    for (final FilterCandidates result : candidates) {
        for (int i = 0; i < result.fitResult.length; i++) {
            final MultiPathFitResult fitResult = result.fitResult[i];
            count += count(fitResult.getSingleFitResult());
            count += count(fitResult.getMultiFitResult());
            count += count(fitResult.getDoubletFitResult());
            count += count(fitResult.getMultiDoubletFitResult());
        }
    }
    final PreprocessedPeakResult[] preprocessedPeakResults = new PreprocessedPeakResult[count];
    count = 0;
    for (final FilterCandidates result : candidates) {
        for (int i = 0; i < result.fitResult.length; i++) {
            final MultiPathFitResult fitResult = result.fitResult[i];
            count = store(fitResult.getSingleFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getMultiFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getDoubletFitResult(), count, preprocessedPeakResults);
            count = store(fitResult.getMultiDoubletFitResult(), count, preprocessedPeakResults);
        }
    }
    final BenchmarkSpotFitResult newSpotFitResults = new BenchmarkSpotFitResult(simulationParameters.id, fitResults);
    newSpotFitResults.distanceInPixels = distanceInPixels;
    newSpotFitResults.lowerDistanceInPixels = lowerDistanceInPixels;
    newSpotFitResults.stopWatch = stopWatch;
    summariseResults(newSpotFitResults, runTime, preprocessedPeakResults, count, candidateData, actualCoordinates);
    IJ.showStatus("");
    spotFitResults.set(newSpotFitResults);
    return newSpotFitResults;
}
Also used : MultiPathFitResult(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFitResult) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResults(uk.ac.sussex.gdsc.smlm.results.PeakResults) SynchronizedPeakResults(uk.ac.sussex.gdsc.smlm.results.SynchronizedPeakResults) BasePreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.BasePreprocessedPeakResult) PreprocessedPeakResult(uk.ac.sussex.gdsc.smlm.results.filter.PreprocessedPeakResult) FitWorker(uk.ac.sussex.gdsc.smlm.engine.FitWorker) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) ImageStack(ij.ImageStack) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) PeakResultPoint(uk.ac.sussex.gdsc.smlm.results.PeakResultPoint) BasePoint(uk.ac.sussex.gdsc.core.match.BasePoint) LinkedList(java.util.LinkedList) StopWatch(org.apache.commons.lang3.time.StopWatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) Coordinate(uk.ac.sussex.gdsc.core.match.Coordinate) FitConfiguration(uk.ac.sussex.gdsc.smlm.engine.FitConfiguration) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap)

Aggregations

Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)33 LinkedList (java.util.LinkedList)16 Future (java.util.concurrent.Future)12 ImageStack (ij.ImageStack)11 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)10 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)10 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)9 ExecutorService (java.util.concurrent.ExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ImagePlus (ij.ImagePlus)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)6 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)6 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)5 ImageProcessor (ij.process.ImageProcessor)5 TextWindow (ij.text.TextWindow)5 Point (java.awt.Point)5 Rectangle (java.awt.Rectangle)5 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)5