Search in sources :

Example 1 with Offset

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset in project GDSC-SMLM by aherbert.

the class CreateData method createImagePsf.

/**
 * Create a PSF model from the image that contains all the z-slices needed to draw the given
 * localisations.
 *
 * @param localisationSets the localisation sets
 * @return the image PSF model
 */
private ImagePsfModel createImagePsf(List<LocalisationModelSet> localisationSets) {
    final ImagePlus imp = WindowManager.getImage(settings.getPsfImageName());
    if (imp == null) {
        IJ.error(TITLE, "Unable to create the PSF model from image: " + settings.getPsfImageName());
        return null;
    }
    try {
        final ImagePSF psfSettings = ImagePsfHelper.fromString(imp.getProperty("Info").toString());
        if (psfSettings == null) {
            throw new IllegalStateException("Unknown PSF settings for image: " + imp.getTitle());
        }
        // Check all the settings have values
        if (psfSettings.getPixelSize() <= 0) {
            throw new IllegalStateException("Missing nmPerPixel calibration settings for image: " + imp.getTitle());
        }
        if (psfSettings.getPixelDepth() <= 0) {
            throw new IllegalStateException("Missing nmPerSlice calibration settings for image: " + imp.getTitle());
        }
        if (psfSettings.getCentreImage() <= 0) {
            throw new IllegalStateException("Missing zCentre calibration settings for image: " + imp.getTitle());
        }
        if (psfSettings.getFwhm() <= 0) {
            throw new IllegalStateException("Missing FWHM calibration settings for image: " + imp.getTitle());
        }
        // To save memory construct the Image PSF using only the slices that are within
        // the depth of field of the simulation
        double minZ = Double.POSITIVE_INFINITY;
        double maxZ = Double.NEGATIVE_INFINITY;
        for (final LocalisationModelSet l : localisationSets) {
            for (final LocalisationModel m : l.getLocalisations()) {
                final double z = m.getZ();
                if (minZ > z) {
                    minZ = z;
                }
                if (maxZ < z) {
                    maxZ = z;
                }
            }
        }
        final int nSlices = imp.getStackSize();
        // z-centre should be an index and not the ImageJ slice number so subtract 1
        final int zCentre = psfSettings.getCentreImage() - 1;
        // Calculate the start/end slices to cover the depth of field
        // This logic must match the ImagePSFModel.
        final double unitsPerSlice = psfSettings.getPixelDepth() / settings.getPixelPitch();
        // We assume the PSF was imaged axially with increasing z-stage position (moving the stage
        // closer to the objective). Thus higher z-coordinate are for higher slice numbers.
        int lower = (int) Math.round(minZ / unitsPerSlice) + zCentre;
        int upper = (int) Math.round(maxZ / unitsPerSlice) + zCentre;
        // Add extra to the range so that gradients can be computed.
        lower--;
        upper++;
        upper = (upper < 0) ? 0 : (upper >= nSlices) ? nSlices - 1 : upper;
        lower = (lower < 0) ? 0 : (lower >= nSlices) ? nSlices - 1 : lower;
        // Image PSF requires the z-centre for normalisation
        if (!(lower <= zCentre && upper >= zCentre)) {
            // Ensure we include the zCentre
            lower = Math.min(lower, zCentre);
            upper = Math.max(upper, zCentre);
        }
        final double noiseFraction = 1e-3;
        final float[][] image = extractImageStack(imp, lower, upper);
        final ImagePsfModel model = new ImagePsfModel(image, zCentre - lower, psfSettings.getPixelSize() / settings.getPixelPitch(), unitsPerSlice, noiseFraction);
        // Add the calibrated centres. The map will not be null
        final Map<Integer, Offset> map = psfSettings.getOffsetsMap();
        if (!map.isEmpty()) {
            final int sliceOffset = lower + 1;
            for (final Entry<Integer, Offset> entry : map.entrySet()) {
                model.setRelativeCentre(entry.getKey() - sliceOffset, entry.getValue().getCx(), entry.getValue().getCy());
            }
        } else {
            // Use the CoM if present
            final double cx = psfSettings.getXCentre();
            final double cy = psfSettings.getYCentre();
            if (cx != 0 || cy != 0) {
                for (int slice = 0; slice < image.length; slice++) {
                    model.setCentre(slice, cx, cy);
                }
            }
        }
        // Initialise the HWHM table so that it can be cloned
        model.initialiseHwhm();
        return model;
    } catch (final Exception ex) {
        IJ.error(TITLE, "Unable to create the image PSF model:\n" + ex.getMessage());
        return null;
    }
}
Also used : ImagePlus(ij.ImagePlus) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) IOException(java.io.IOException) DataException(uk.ac.sussex.gdsc.core.data.DataException) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) Offset(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocalisationModel(uk.ac.sussex.gdsc.smlm.model.LocalisationModel) ImagePSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF) LocalisationModelSet(uk.ac.sussex.gdsc.smlm.model.LocalisationModelSet) ImagePsfModel(uk.ac.sussex.gdsc.smlm.model.ImagePsfModel)

Example 2 with Offset

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset in project GDSC-SMLM by aherbert.

the class PsfDrift method createImagePsf.

private ImagePsfModel createImagePsf(int lower, int upper, double scale) {
    final int zCentre = psfSettings.getCentreImage();
    final double unitsPerPixel = 1.0 / scale;
    // So we can move from -depth to depth
    final double unitsPerSlice = 1;
    // Extract data uses index not slice number as arguments so subtract 1
    final double noiseFraction = 1e-3;
    final float[][] image = CreateData.extractImageStack(imp, lower - 1, upper - 1);
    final ImagePsfModel model = new ImagePsfModel(image, zCentre - lower, unitsPerPixel, unitsPerSlice, noiseFraction);
    // Add the calibrated centres
    final Map<Integer, Offset> oldOffset = psfSettings.getOffsetsMap();
    if (settings.useOffset && !oldOffset.isEmpty()) {
        final int sliceOffset = lower;
        for (final Entry<Integer, Offset> entry : oldOffset.entrySet()) {
            model.setRelativeCentre(entry.getKey() - sliceOffset, entry.getValue().getCx(), entry.getValue().getCy());
        }
    } else {
        // Use the CoM if present
        final double cx = psfSettings.getXCentre();
        final double cy = psfSettings.getYCentre();
        if (cx != 0 || cy != 0) {
            for (int slice = 0; slice < image.length; slice++) {
                model.setCentre(slice, cx, cy);
            }
        }
    }
    return model;
}
Also used : ImagePsfModel(uk.ac.sussex.gdsc.smlm.model.ImagePsfModel) Offset(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset)

Example 3 with Offset

use of uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset in project GDSC-SMLM by aherbert.

the class PsfDrift method computeDrift.

private void computeDrift() {
    // Create a grid of XY offset positions between 0-1 for PSF insert
    final double[] grid = new double[settings.gridSize];
    for (int i = 0; i < grid.length; i++) {
        grid[i] = (double) i / settings.gridSize;
    }
    // Configure fitting region
    final int w = 2 * settings.regionSize + 1;
    centrePixel = w / 2;
    // Check region size using the image PSF
    final double newPsfWidth = imp.getWidth() / settings.scale;
    if (Math.ceil(newPsfWidth) > w) {
        ImageJUtils.log(TITLE + ": Fitted region size (%d) is smaller than the scaled PSF (%.1f)", w, newPsfWidth);
    }
    // Create robust PSF fitting settings
    final double a = psfSettings.getPixelSize() * settings.scale;
    final double sa = PsfCalculator.squarePixelAdjustment(psfSettings.getPixelSize() * (psfSettings.getFwhm() / Gaussian2DFunction.SD_TO_FWHM_FACTOR), a);
    fitConfig.setInitialPeakStdDev(sa / a);
    fitConfig.setBackgroundFitting(settings.backgroundFitting);
    fitConfig.setNotSignalFitting(false);
    fitConfig.setComputeDeviations(false);
    fitConfig.setDisableSimpleFilter(true);
    // Create the PSF over the desired z-depth
    final int depth = (int) Math.round(settings.zDepth / psfSettings.getPixelDepth());
    int startSlice = psfSettings.getCentreImage() - depth;
    int endSlice = psfSettings.getCentreImage() + depth;
    final int nSlices = imp.getStackSize();
    startSlice = MathUtils.clip(1, nSlices, startSlice);
    endSlice = MathUtils.clip(1, nSlices, endSlice);
    final ImagePsfModel psf = createImagePsf(startSlice, endSlice, settings.scale);
    final int minz = startSlice - psfSettings.getCentreImage();
    final int maxz = endSlice - psfSettings.getCentreImage();
    final int nZ = maxz - minz + 1;
    final int gridSize2 = grid.length * grid.length;
    total = nZ * gridSize2;
    // Store all the fitting results
    final int nStartPoints = getNumberOfStartPoints();
    results = new double[total * nStartPoints][];
    // TODO - Add ability to iterate this, adjusting the current offset in the PSF
    // each iteration
    // Create a pool of workers
    final int threadCount = Prefs.getThreads();
    final Ticker ticker = ImageJUtils.createTicker(total, threadCount, "Fitting...");
    final BlockingQueue<Job> jobs = new ArrayBlockingQueue<>(threadCount * 2);
    final List<Thread> threads = new LinkedList<>();
    for (int i = 0; i < threadCount; i++) {
        final Worker worker = new Worker(jobs, psf, w, fitConfig, ticker);
        final Thread t = new Thread(worker);
        threads.add(t);
        t.start();
    }
    // Fit
    outer: for (int z = minz, i = 0; z <= maxz; z++) {
        for (int x = 0; x < grid.length; x++) {
            for (int y = 0; y < grid.length; y++, i++) {
                if (IJ.escapePressed()) {
                    break outer;
                }
                put(jobs, new Job(z, grid[x], grid[y], i));
            }
        }
    }
    // If escaped pressed then do not need to stop the workers, just return
    if (ImageJUtils.isInterrupted()) {
        ImageJUtils.finished();
        return;
    }
    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < threads.size(); i++) {
        put(jobs, new Job());
    }
    // 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("Unexpected interrupt", ex);
        }
    }
    threads.clear();
    ImageJUtils.finished();
    // Plot the average and SE for the drift curve
    // Plot the recall
    final double[] zPosition = new double[nZ];
    final double[] avX = new double[nZ];
    final double[] seX = new double[nZ];
    final double[] avY = new double[nZ];
    final double[] seY = new double[nZ];
    final double[] recall = new double[nZ];
    for (int z = minz, i = 0; z <= maxz; z++, i++) {
        final Statistics statsX = new Statistics();
        final Statistics statsY = new Statistics();
        for (int s = 0; s < nStartPoints; s++) {
            int resultPosition = i * gridSize2 + s * total;
            final int endResultPosition = resultPosition + gridSize2;
            while (resultPosition < endResultPosition) {
                if (results[resultPosition] != null) {
                    statsX.add(results[resultPosition][0]);
                    statsY.add(results[resultPosition][1]);
                }
                resultPosition++;
            }
        }
        zPosition[i] = z * psfSettings.getPixelDepth();
        avX[i] = statsX.getMean();
        seX[i] = statsX.getStandardError();
        avY[i] = statsY.getMean();
        seY[i] = statsY.getStandardError();
        recall[i] = (double) statsX.getN() / (nStartPoints * gridSize2);
    }
    // Find the range from the z-centre above the recall limit
    int centre = 0;
    for (int slice = startSlice, i = 0; slice <= endSlice; slice++, i++) {
        if (slice == psfSettings.getCentreImage()) {
            centre = i;
            break;
        }
    }
    if (recall[centre] < settings.recallLimit) {
        return;
    }
    int start = centre;
    int end = centre;
    for (int i = centre; i-- > 0; ) {
        if (recall[i] < settings.recallLimit) {
            break;
        }
        start = i;
    }
    for (int i = centre; ++i < recall.length; ) {
        if (recall[i] < settings.recallLimit) {
            break;
        }
        end = i;
    }
    final int iterations = 1;
    LoessInterpolator loess = null;
    if (settings.smoothing > 0) {
        loess = new LoessInterpolator(settings.smoothing, iterations);
    }
    final double[][] smoothx = displayPlot("Drift X", "X (nm)", zPosition, avX, seX, loess, start, end);
    final double[][] smoothy = displayPlot("Drift Y", "Y (nm)", zPosition, avY, seY, loess, start, end);
    displayPlot("Recall", "Recall", zPosition, recall, null, null, start, end);
    windowOrganiser.tile();
    // Ask the user if they would like to store them in the image
    final GenericDialog gd = new GenericDialog(TITLE);
    gd.enableYesNoCancel();
    gd.hideCancelButton();
    startSlice = psfSettings.getCentreImage() - (centre - start);
    endSlice = psfSettings.getCentreImage() + (end - centre);
    ImageJUtils.addMessage(gd, "Save the drift to the PSF?\n \nSlices %d (%s nm) - %d (%s nm) above recall limit", startSlice, MathUtils.rounded(zPosition[start]), endSlice, MathUtils.rounded(zPosition[end]));
    gd.addMessage("Optionally average the end points to set drift outside the limits.\n" + "(Select zero to ignore)");
    gd.addSlider("Number_of_points", 0, 10, settings.positionsToAverage);
    gd.showDialog();
    if (gd.wasOKed()) {
        settings.positionsToAverage = Math.abs((int) gd.getNextNumber());
        final Map<Integer, Offset> oldOffset = psfSettings.getOffsetsMap();
        final boolean useOldOffset = settings.useOffset && !oldOffset.isEmpty();
        final LocalList<double[]> offset = new LocalList<>();
        final double pitch = psfSettings.getPixelSize();
        int index = 0;
        for (int i = start, slice = startSlice; i <= end; slice++, i++) {
            index = findCentre(zPosition[i], smoothx, index);
            if (index == -1) {
                ImageJUtils.log("Failed to find the offset for depth %.2f", zPosition[i]);
                continue;
            }
            // The offset should store the difference to the centre in pixels so divide by the pixel
            // pitch
            double cx = smoothx[1][index] / pitch;
            double cy = smoothy[1][index] / pitch;
            if (useOldOffset) {
                final Offset o = oldOffset.get(slice);
                if (o != null) {
                    cx += o.getCx();
                    cy += o.getCy();
                }
            }
            offset.add(new double[] { slice, cx, cy });
        }
        addMissingOffsets(startSlice, endSlice, nSlices, offset);
        final Offset.Builder offsetBuilder = Offset.newBuilder();
        final ImagePSF.Builder imagePsfBuilder = psfSettings.toBuilder();
        for (final double[] o : offset) {
            final int slice = (int) o[0];
            offsetBuilder.setCx(o[1]);
            offsetBuilder.setCy(o[2]);
            imagePsfBuilder.putOffsets(slice, offsetBuilder.build());
        }
        imagePsfBuilder.putNotes(TITLE, String.format("Solver=%s, Region=%d", PeakFit.getSolverName(fitConfig), settings.regionSize));
        imp.setProperty("Info", ImagePsfHelper.toString(imagePsfBuilder));
    }
}
Also used : LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ImagePSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) GenericDialog(ij.gui.GenericDialog) Ticker(uk.ac.sussex.gdsc.core.logging.Ticker) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) LinkedList(java.util.LinkedList) Offset(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) ImagePsfModel(uk.ac.sussex.gdsc.smlm.model.ImagePsfModel)

Aggregations

Offset (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.Offset)3 ImagePsfModel (uk.ac.sussex.gdsc.smlm.model.ImagePsfModel)3 ImagePSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.ImagePSF)2 ImagePlus (ij.ImagePlus)1 GenericDialog (ij.gui.GenericDialog)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)1 LoessInterpolator (org.apache.commons.math3.analysis.interpolation.LoessInterpolator)1 NullArgumentException (org.apache.commons.math3.exception.NullArgumentException)1 DataException (uk.ac.sussex.gdsc.core.data.DataException)1 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)1 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)1 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)1 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)1 ConfigurationException (uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException)1