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;
}
}
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);
}
Aggregations