Search in sources :

Example 11 with LocalisationModel

use of uk.ac.sussex.gdsc.smlm.model.LocalisationModel 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 12 with LocalisationModel

use of uk.ac.sussex.gdsc.smlm.model.LocalisationModel in project GDSC-SMLM by aherbert.

the class CreateData method createPsfModel.

private PsfModel createPsfModel(double[] xyz) {
    // Create a set with a single model
    final List<LocalisationModelSet> localisationSets = new LocalList<>(1);
    final LocalisationModelSet set = new LocalisationModelSet(0, 0);
    final LocalisationModel m = new LocalisationModel(0, 0, xyz, 1, 0);
    set.add(m);
    localisationSets.add(set);
    return createPsfModel(localisationSets);
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) LocalisationModel(uk.ac.sussex.gdsc.smlm.model.LocalisationModel) LocalisationModelSet(uk.ac.sussex.gdsc.smlm.model.LocalisationModelSet)

Aggregations

LocalisationModel (uk.ac.sussex.gdsc.smlm.model.LocalisationModel)12 ReadHint (uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint)8 LocalisationModelSet (uk.ac.sussex.gdsc.smlm.model.LocalisationModelSet)6 TIntArrayList (gnu.trove.list.array.TIntArrayList)4 TIntHashSet (gnu.trove.set.hash.TIntHashSet)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 NullArgumentException (org.apache.commons.math3.exception.NullArgumentException)4 DataException (uk.ac.sussex.gdsc.core.data.DataException)4 ConversionException (uk.ac.sussex.gdsc.core.data.utils.ConversionException)4 ConfigurationException (uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException)4 FluorophoreSequenceModel (uk.ac.sussex.gdsc.smlm.model.FluorophoreSequenceModel)4 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)4 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)3 ImagePlus (ij.ImagePlus)3 StoredDataStatistics (uk.ac.sussex.gdsc.core.utils.StoredDataStatistics)3 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)3 ActivationEnergyImageModel (uk.ac.sussex.gdsc.smlm.model.ActivationEnergyImageModel)3 CompoundMoleculeModel (uk.ac.sussex.gdsc.smlm.model.CompoundMoleculeModel)3 ImageModel (uk.ac.sussex.gdsc.smlm.model.ImageModel)3