use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class DriftCalculator method applyDriftCorrection.
private void applyDriftCorrection(MemoryPeakResults results, double[][] drift) {
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Apply drift correction to in-memory results?");
gd.addChoice("Update_method", Settings.UPDATE_METHODS, settings.updateMethod);
// Option to save the drift unless it was loaded from file
if (!Settings.DRIFT_FILE.equals(settings.method)) {
gd.addCheckbox("Save_drift", settings.saveDrift);
}
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
settings.updateMethod = gd.getNextChoiceIndex();
if (!Settings.DRIFT_FILE.equals(settings.method)) {
settings.saveDrift = gd.getNextBoolean();
saveDrift(calculatedTimepoints, lastdx, lastdy);
}
if (settings.updateMethod == 0) {
return;
}
final double[] dx = drift[0];
final double[] dy = drift[1];
if (settings.updateMethod == 1) {
// Update the results in memory
ImageJUtils.log("Applying drift correction to the results set: " + results.getName());
results.forEach((PeakResultProcedure) result -> {
result.setXPosition((float) (result.getXPosition() + dx[result.getFrame()]));
result.setYPosition((float) (result.getYPosition() + dy[result.getFrame()]));
});
} else {
// Create a new set of results
final MemoryPeakResults newResults = new MemoryPeakResults(results.size());
newResults.copySettings(results);
newResults.setName(results.getName() + " (Corrected)");
MemoryPeakResults.addResults(newResults);
final boolean truncate = settings.updateMethod == 3;
ImageJUtils.log("Creating %sdrift corrected results set: " + newResults.getName(), (truncate) ? "truncated " : "");
results.forEach((PeakResultProcedure) result -> {
if (truncate && (result.getFrame() < interpolationStart || result.getFrame() > interpolationEnd)) {
return;
}
result.setXPosition((float) (result.getXPosition() + dx[result.getFrame()]));
result.setYPosition((float) (result.getYPosition() + dy[result.getFrame()]));
newResults.add(result);
});
}
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class DriftCalculator method findSpots.
private static Spot[] findSpots(MemoryPeakResults results, Rectangle bounds, int[] limits) {
final LocalList<Spot> list = new LocalList<>(limits[1] - limits[0] + 1);
final float minx = bounds.x;
final float miny = bounds.y;
final float maxx = (float) bounds.x + bounds.width;
final float maxy = (float) bounds.y + bounds.height;
// Find spots within the ROI
results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
if (x > minx && x < maxx && y > miny && y < maxy) {
list.add(new Spot(result.getFrame(), x, y, result.getIntensity()));
}
});
// For each frame pick the strongest spot
Collections.sort(list, Spot::compare);
final LocalList<Spot> newList = new LocalList<>(list.size());
int currentT = -1;
for (final Spot spot : list) {
if (currentT != spot.time) {
newList.add(spot);
currentT = spot.time;
}
}
return newList.toArray(new Spot[0]);
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class CropResults method cropResults.
/**
* Apply the filters to the data.
*/
private void cropResults() {
final MemoryPeakResults newResults = createNewResults();
// These bounds are integer. But this is because the results are meant to come from an image.
final Rectangle integerBounds = results.getBounds(true);
// The crop bounds can be floating point...
// Border
final double border = settings.getBorder();
final double xx = integerBounds.x + border;
final double yy = integerBounds.y + border;
final double w = Math.max(0, integerBounds.width - 2 * border);
final double h = Math.max(0, integerBounds.height - 2 * border);
Rectangle2D pixelBounds = new Rectangle2D.Double(xx, yy, w, h);
// Bounding box
if (settings.getSelectRegion()) {
final Rectangle2D boxBounds = new Rectangle2D.Double(settings.getX(), settings.getY(), settings.getWidth(), settings.getHeight());
pixelBounds = pixelBounds.createIntersection(boxBounds);
}
// and create another intersection
if (myUseRoi) {
final ImagePlus imp = WindowManager.getImage(settings.getRoiImage());
if (imp != null && imp.getRoi() != null) {
final Rectangle roi = imp.getRoi().getBounds();
final int roiImageWidth = imp.getWidth();
final int roiImageHeight = imp.getHeight();
final double xscale = (double) roiImageWidth / integerBounds.width;
final double yscale = (double) roiImageHeight / integerBounds.height;
final Rectangle2D roiBounds = new Rectangle2D.Double(roi.x / xscale, roi.y / yscale, roi.width / xscale, roi.height / yscale);
pixelBounds = pixelBounds.createIntersection(roiBounds);
}
}
final Rectangle2D bounds = pixelBounds;
final Predicate<PeakResult> testZ = getZFilter();
// Copy the results if the origin is reset
final Consumer<PeakResult> consumer = settings.getResetOrigin() ? r -> newResults.add(r.copy()) : newResults::add;
if (bounds.getWidth() > 0 && bounds.getHeight() > 0) {
results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
if (bounds.contains(x, y) && testZ.test(result)) {
consumer.accept(result);
}
});
}
if (settings.getPreserveBounds()) {
newResults.setBounds(integerBounds);
} else {
// Get the lower and upper integer limits
final int x = (int) Math.floor(bounds.getX());
final int y = (int) Math.floor(bounds.getY());
final int ux = (int) Math.ceil(bounds.getX() + bounds.getWidth());
final int uy = (int) Math.ceil(bounds.getY() + bounds.getHeight());
// Ensure the width and height are at least 1
newResults.setBounds(new Rectangle(x, y, Math.max(1, ux - x), Math.max(1, uy - y)));
if (settings.getResetOrigin()) {
newResults.translate(-x, -y);
}
}
IJ.showStatus(newResults.size() + " Cropped localisations");
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class CropResults method roiCropResults.
private void roiCropResults() {
final MemoryPeakResults newResults = createNewResults();
// These bounds are integer. But this is because the results are meant to come from an image.
final Rectangle integerBounds = results.getBounds(true);
final ImagePlus imp = WindowManager.getImage(settings.getRoiImage());
if (imp == null) {
IJ.error(TITLE, "No ROI image: " + settings.getRoiImage());
return;
}
final CoordinatePredicate roiTest = CoordinatePredicateUtils.createContainsPredicate(imp.getRoi());
if (roiTest == null) {
IJ.error(TITLE, "Not an area ROI");
return;
}
// Scale the results to the size of the image with the ROI
final int roiImageWidth = imp.getWidth();
final int roiImageHeight = imp.getHeight();
final double ox = integerBounds.getX();
final double oy = integerBounds.getY();
final double xscale = roiImageWidth / integerBounds.getWidth();
final double yscale = roiImageHeight / integerBounds.getHeight();
final Predicate<PeakResult> testZ = getZFilter();
results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (x, y, result) -> {
if (roiTest.test((x - ox) * xscale, (y - oy) * yscale) && testZ.test(result)) {
newResults.add(result);
}
});
if (settings.getPreserveBounds()) {
newResults.setBounds(integerBounds);
} else {
newResults.setBounds(null);
newResults.getBounds(true);
}
IJ.showStatus(newResults.size() + " Cropped localisations");
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class CropResults method createNewResults.
private MemoryPeakResults createNewResults() {
final MemoryPeakResults newResults = new MemoryPeakResults();
newResults.copySettings(results);
newResults.setName(outputName);
MemoryPeakResults.addResults(newResults);
return newResults;
}
Aggregations