Search in sources :

Example 6 with Calibration

use of gdsc.smlm.results.Calibration in project GDSC-SMLM by aherbert.

the class CalibrateResults method showDialog.

private boolean showDialog(MemoryPeakResults results) {
    GenericDialog gd = new GenericDialog(TITLE);
    gd.addHelp(About.HELP_URL);
    Calibration calibration = results.getCalibration();
    boolean newCalibration = false;
    if (calibration == null) {
        newCalibration = true;
        calibration = new Calibration();
        gd.addMessage("No calibration found, using defaults");
    }
    gd.addStringField("Name", results.getName(), Math.max(Math.min(results.getName().length(), 60), 20));
    if (!newCalibration)
        gd.addCheckbox("Update_all_linked_results", updateAll);
    gd.addNumericField("Calibration (nm/px)", calibration.getNmPerPixel(), 2);
    gd.addNumericField("Gain (ADU/photon)", calibration.getGain(), 2);
    gd.addCheckbox("EM-CCD", calibration.isEmCCD());
    gd.addNumericField("Exposure_time (ms)", calibration.getExposureTime(), 2);
    gd.addNumericField("Camera_bias (ADUs)", calibration.getBias(), 2);
    gd.addNumericField("Read_noise (ADUs)", calibration.getReadNoise(), 2);
    gd.addNumericField("Amplification (ADUs/electron)", calibration.getAmplification(), 2);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    String name = gd.getNextString();
    if (!results.getName().equals(name)) {
        // If the name is changed then remove and add back to memory
        results = MemoryPeakResults.removeResults(results.getName());
        if (results != null) {
            results.setName(name);
            MemoryPeakResults.addResults(results);
        }
    }
    if (!newCalibration) {
        updateAll = gd.getNextBoolean();
        // To avoid changing all datasets with the same calibration we create a copy
        if (!updateAll) {
            newCalibration = true;
            calibration = calibration.clone();
        }
    }
    calibration.setNmPerPixel(Math.abs(gd.getNextNumber()));
    calibration.setGain(Math.abs(gd.getNextNumber()));
    calibration.setEmCCD(gd.getNextBoolean());
    calibration.setExposureTime(Math.abs(gd.getNextNumber()));
    calibration.setBias(Math.abs(gd.getNextNumber()));
    calibration.setReadNoise(Math.abs(gd.getNextNumber()));
    calibration.setAmplification(Math.abs(gd.getNextNumber()));
    if (newCalibration)
        results.setCalibration(calibration);
    return true;
}
Also used : GenericDialog(ij.gui.GenericDialog) ExtendedGenericDialog(ij.gui.ExtendedGenericDialog) Calibration(gdsc.smlm.results.Calibration)

Example 7 with Calibration

use of gdsc.smlm.results.Calibration in project GDSC-SMLM by aherbert.

the class ResultsImageSampler method getSample.

/**
	 * Gets the sample image. The image is a stack of the samples with an overlay of the localisation positions. The
	 * info property is set with details of the localisations and the image is calibrated.
	 *
	 * @param nNo
	 *            the number of samples with no localisations
	 * @param nLow
	 *            the number of samples with low localisations
	 * @param nHigh
	 *            the number of samples with high localisations
	 * @return the sample image (could be null if no samples were made)
	 */
public ImagePlus getSample(int nNo, int nLow, int nHigh) {
    ImageStack out = new ImageStack(size, size);
    if (!isValid())
        return null;
    list.clearf();
    // empty
    for (int i : Random.sample(nNo, no.length, r)) list.add(ResultsSample.createEmpty(no[i]));
    // low
    for (int i : Random.sample(nLow, lower, r)) list.add(data[i]);
    // high
    for (int i : Random.sample(nHigh, upper, r)) list.add(data[i + lower]);
    if (list.isEmpty())
        return null;
    double nmPerPixel = 1;
    if (results.getCalibration() != null) {
        Calibration calibration = results.getCalibration();
        if (calibration.hasNmPerPixel()) {
            nmPerPixel = calibration.getNmPerPixel();
        }
    }
    // Sort descending by number in the frame
    ResultsSample[] sample = list.toArray(new ResultsSample[list.size()]);
    Arrays.sort(sample, rcc);
    int[] xyz = new int[3];
    Rectangle stackBounds = new Rectangle(stack.getWidth(), stack.getHeight());
    Overlay overlay = new Overlay();
    float[] ox = new float[10], oy = new float[10];
    StringBuilder sb = new StringBuilder();
    if (nmPerPixel == 1)
        sb.append("Sample X Y Z Signal\n");
    else
        sb.append("Sample X(nm) Y(nm) Z(nm) Signal\n");
    for (ResultsSample s : sample) {
        getXYZ(s.index, xyz);
        // Construct the region to extract
        Rectangle target = new Rectangle(xyz[0], xyz[1], size, size);
        target = target.intersection(stackBounds);
        if (target.width == 0 || target.height == 0)
            continue;
        // Extract the frame
        int slice = xyz[2];
        ImageProcessor ip = stack.getProcessor(slice);
        // Cut out the desired pixels (some may be blank if the block overruns the source image)
        ImageProcessor ip2 = ip.createProcessor(size, size);
        for (int y = 0; y < target.height; y++) for (int x = 0, i = y * size, index = (y + target.y) * ip.getWidth() + target.x; x < target.width; x++, i++, index++) {
            ip2.setf(i, ip.getf(index));
        }
        int size = s.size();
        if (size > 0) {
            int position = out.getSize() + 1;
            // Create an ROI with the localisations
            for (int i = 0; i < size; i++) {
                PeakResult p = s.list.get(i);
                ox[i] = p.getXPosition() - xyz[0];
                oy[i] = p.getYPosition() - xyz[1];
                sb.append(position).append(' ');
                sb.append(Utils.rounded(ox[i] * nmPerPixel)).append(' ');
                sb.append(Utils.rounded(oy[i] * nmPerPixel)).append(' ');
                // Z can be stored in the error field
                sb.append(Utils.rounded(p.error * nmPerPixel)).append(' ');
                sb.append(Utils.rounded(p.getSignal())).append('\n');
            }
            PointRoi roi = new PointRoi(ox, oy, size);
            roi.setPosition(position);
            overlay.add(roi);
        }
        out.addSlice(String.format("Frame=%d @ %d,%d px (n=%d)", slice, xyz[0], xyz[1], size), ip2.getPixels());
    }
    if (out.getSize() == 0)
        return null;
    ImagePlus imp = new ImagePlus("Sample", out);
    imp.setOverlay(overlay);
    // Note: Only the info property can be saved to a TIFF file
    imp.setProperty("Info", sb.toString());
    if (nmPerPixel != 1) {
        ij.measure.Calibration cal = new ij.measure.Calibration();
        cal.setUnit("nm");
        cal.pixelHeight = cal.pixelWidth = nmPerPixel;
        imp.setCalibration(cal);
    }
    return imp;
}
Also used : ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) Calibration(gdsc.smlm.results.Calibration) ImagePlus(ij.ImagePlus) PeakResult(gdsc.smlm.results.PeakResult) ImageProcessor(ij.process.ImageProcessor) Overlay(ij.gui.Overlay) PointRoi(ij.gui.PointRoi)

Example 8 with Calibration

use of gdsc.smlm.results.Calibration in project GDSC-SMLM by aherbert.

the class IJAbstractPeakResults method setCalibration.

public void setCalibration(double nmPerPixel, double gain) {
    Calibration calibration = new Calibration();
    calibration.setNmPerPixel(nmPerPixel);
    calibration.setGain(gain);
    setCalibration(calibration);
}
Also used : Calibration(gdsc.smlm.results.Calibration)

Example 9 with Calibration

use of gdsc.smlm.results.Calibration in project GDSC-SMLM by aherbert.

the class FIRE method canCalculatePrecision.

private boolean canCalculatePrecision(MemoryPeakResults results) {
    // Calibration is required to compute the precision
    Calibration cal = results.getCalibration();
    if (cal == null)
        return false;
    if (!cal.hasNmPerPixel() || !cal.hasGain() || !cal.hasEMCCD())
        return false;
    // Check all have a width and signal
    PeakResult[] data = results.toArray();
    for (int i = 0; i < data.length; i++) {
        PeakResult p = data[i];
        if (p.getSD() <= 0 || p.getSignal() <= 0)
            return true;
    }
    // Check for variable width that is not 1 and a variable signal
    for (int i = 0; i < data.length; i++) {
        PeakResult p = data[i];
        // Check this is valid
        if (p.getSD() != 1) {
            // Check the rest for a different value
            float w1 = p.getSD();
            float s1 = p.getSignal();
            for (int j = i + 1; j < data.length; j++) {
                PeakResult p2 = data[j];
                if (p2.getSD() != 1 && p2.getSD() != w1 && p2.getSignal() != s1)
                    return true;
            }
            // All the results are the same, this is not valid
            break;
        }
    }
    return false;
}
Also used : Calibration(gdsc.smlm.results.Calibration) PeakResult(gdsc.smlm.results.PeakResult) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint)

Example 10 with Calibration

use of gdsc.smlm.results.Calibration in project GDSC-SMLM by aherbert.

the class PCPALMClusters method doClustering.

/**
	 * Extract the results from the PCPALM molecules using the area ROI and then do clustering to obtain the histogram
	 * of molecules per cluster.
	 * 
	 * @return
	 */
private HistogramData doClustering() {
    // Perform clustering analysis to generate the histogram of cluster sizes
    PCPALMAnalysis analysis = new PCPALMAnalysis();
    ArrayList<Molecule> molecules = analysis.cropToRoi(WindowManager.getCurrentImage());
    if (molecules.size() < 2) {
        error("No results within the crop region");
        return null;
    }
    Utils.log("Using %d molecules (Density = %s um^-2) @ %s nm", molecules.size(), Utils.rounded(molecules.size() / analysis.croppedArea), Utils.rounded(distance));
    long s1 = System.nanoTime();
    ClusteringEngine engine = new ClusteringEngine(1, clusteringAlgorithm, new IJTrackProgress());
    if (multiThread)
        engine.setThreadCount(Prefs.getThreads());
    engine.setTracker(new IJTrackProgress());
    IJ.showStatus("Clustering ...");
    ArrayList<Cluster> clusters = engine.findClusters(convertToPoint(molecules), distance);
    IJ.showStatus("");
    if (clusters == null) {
        Utils.log("Aborted");
        return null;
    }
    nMolecules = molecules.size();
    Utils.log("Finished : %d total clusters (%s ms)", clusters.size(), Utils.rounded((System.nanoTime() - s1) / 1e6));
    // Save cluster centroids to a results set in memory. Then they can be plotted.
    MemoryPeakResults results = new MemoryPeakResults(clusters.size());
    results.setName(TITLE);
    // Set an arbitrary calibration so that the lifetime of the results is stored in the exposure time
    // The results will be handled as a single mega-frame containing all localisation. 
    results.setCalibration(new Calibration(100, 1, PCPALMMolecules.seconds * 1000));
    // Make the standard deviation such that the Gaussian volume will be 95% at the distance threshold
    final float sd = (float) (distance / 1.959964);
    int id = 0;
    for (Cluster c : clusters) {
        results.add(new ExtendedPeakResult((float) c.x, (float) c.y, sd, c.n, ++id));
    }
    MemoryPeakResults.addResults(results);
    // Get the data for fitting
    float[] values = new float[clusters.size()];
    for (int i = 0; i < values.length; i++) values[i] = clusters.get(i).n;
    float yMax = (int) Math.ceil(Maths.max(values));
    int nBins = (int) (yMax + 1);
    float[][] hist = Utils.calcHistogram(values, 0, yMax, nBins);
    HistogramData histogramData = (calibrateHistogram) ? new HistogramData(hist, frames, area, units) : new HistogramData(hist);
    saveHistogram(histogramData);
    return histogramData;
}
Also used : ExtendedPeakResult(gdsc.smlm.results.ExtendedPeakResult) IJTrackProgress(gdsc.core.ij.IJTrackProgress) Cluster(gdsc.core.clustering.Cluster) Calibration(gdsc.smlm.results.Calibration) ClusterPoint(gdsc.core.clustering.ClusterPoint) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ClusteringEngine(gdsc.core.clustering.ClusteringEngine)

Aggregations

Calibration (gdsc.smlm.results.Calibration)24 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)8 ExtendedGenericDialog (ij.gui.ExtendedGenericDialog)8 PeakResult (gdsc.smlm.results.PeakResult)6 GenericDialog (ij.gui.GenericDialog)6 FitEngineConfiguration (gdsc.smlm.engine.FitEngineConfiguration)5 FitConfiguration (gdsc.smlm.fitting.FitConfiguration)4 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)4 ImagePlus (ij.ImagePlus)4 Rectangle (java.awt.Rectangle)4 IJTablePeakResults (gdsc.smlm.ij.results.IJTablePeakResults)3 ExtendedPeakResult (gdsc.smlm.results.ExtendedPeakResult)3 ImageStack (ij.ImageStack)3 ImageProcessor (ij.process.ImageProcessor)3 Statistics (gdsc.core.utils.Statistics)2 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)2 IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)2 LocalisationModel (gdsc.smlm.model.LocalisationModel)2 MoleculeModel (gdsc.smlm.model.MoleculeModel)2 File (java.io.File)2