use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class Fire method createImages.
/**
* Creates the images to use for the FIRE calculation. This must be called after
* {@link #initialise(MemoryPeakResults, MemoryPeakResults)}.
*
* @param fourierImageScale the fourier image scale (set to zero to auto compute)
* @param imageSize the image size
* @param useSignal Use the localisation signal to weight the intensity. The default uses a value
* of 1 per localisation.
* @return the fire images
*/
public FireImages createImages(double fourierImageScale, int imageSize, boolean useSignal) {
if (results == null) {
return null;
}
final SignalProvider signalProvider = (useSignal && (results.hasIntensity())) ? new PeakSignalProvider() : new FixedSignalProvider();
// Draw images using the existing IJ routines.
final Rectangle bounds = new Rectangle((int) Math.ceil(dataBounds.getWidth()), (int) Math.ceil(dataBounds.getHeight()));
final ResultsImageSettings.Builder builder = ResultsImageSettings.newBuilder().setImageType(ResultsImageType.DRAW_NONE).setWeighted(true).setEqualised(false).setImageMode(ResultsImageMode.IMAGE_ADD);
if (fourierImageScale > 0) {
builder.setImageSizeMode(ResultsImageSizeMode.SCALED);
builder.setScale(fourierImageScale);
} else {
builder.setImageSizeMode(ResultsImageSizeMode.IMAGE_SIZE);
builder.setImageSize(imageSize);
}
ImageJImagePeakResults image1 = createPeakResultsImage(bounds, builder, "IP1");
ImageJImagePeakResults image2 = createPeakResultsImage(bounds, builder, "IP2");
final float minx = (float) dataBounds.getX();
final float miny = (float) dataBounds.getY();
if (this.results2 != null) {
// Two image comparison
final ImageJImagePeakResults i1 = image1;
results.forEach((PeakResultProcedure) result -> {
final float x = result.getXPosition() - minx;
final float y = result.getYPosition() - miny;
i1.add(x, y, signalProvider.getSignal(result));
});
final ImageJImagePeakResults i2 = image2;
results2.forEach((PeakResultProcedure) result -> {
final float x = result.getXPosition() - minx;
final float y = result.getYPosition() - miny;
i2.add(x, y, signalProvider.getSignal(result));
});
} else {
// Block sampling.
// Ensure we have at least 2 even sized blocks.
int blockSize = Math.min(results.size() / 2, Math.max(1, settings.blockSize));
int nblocks = (int) Math.ceil((double) results.size() / blockSize);
while (nblocks <= 1 && blockSize > 1) {
blockSize /= 2;
nblocks = (int) Math.ceil((double) results.size() / blockSize);
}
if (nblocks <= 1) {
// This should not happen since the results should contain at least 2 localisations
return null;
}
if (blockSize != settings.blockSize) {
IJ.log(pluginTitle + " Warning: Changed block size to " + blockSize);
}
final Counter i = new Counter();
final Counter block = new Counter();
final int finalBlockSize = blockSize;
final PeakResult[][] blocks = new PeakResult[nblocks][blockSize];
results.forEach((PeakResultProcedure) result -> {
if (i.getCount() == finalBlockSize) {
block.increment();
i.reset();
}
blocks[block.getCount()][i.getAndIncrement()] = result;
});
// Truncate last block
blocks[block.getCount()] = Arrays.copyOf(blocks[block.getCount()], i.getCount());
final int[] indices = SimpleArrayUtils.natural(block.getCount() + 1);
if (settings.randomSplit) {
MathArrays.shuffle(indices);
}
for (final int index : indices) {
// Split alternating so just rotate
final ImageJImagePeakResults image = image1;
image1 = image2;
image2 = image;
for (final PeakResult p : blocks[index]) {
final float x = p.getXPosition() - minx;
final float y = p.getYPosition() - miny;
image.add(x, y, signalProvider.getSignal(p));
}
}
}
image1.end();
final ImageProcessor ip1 = image1.getImagePlus().getProcessor();
image2.end();
final ImageProcessor ip2 = image2.getImagePlus().getProcessor();
if (settings.maxPerBin > 0 && signalProvider instanceof FixedSignalProvider) {
// We can eliminate over-sampled pixels
for (int i = ip1.getPixelCount(); i-- > 0; ) {
if (ip1.getf(i) > settings.maxPerBin) {
ip1.setf(i, settings.maxPerBin);
}
if (ip2.getf(i) > settings.maxPerBin) {
ip2.setf(i, settings.maxPerBin);
}
}
}
return new FireImages(ip1, ip2, nmPerUnit / image1.getScale());
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class FilterMolecules method run.
@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "No localisations in memory");
return;
}
if (!showDialog()) {
return;
}
// Load the results
MemoryPeakResults results = ResultsManager.loadInputResults(settings.inputOption, false, null, null);
if (MemoryPeakResults.isEmpty(results)) {
IJ.error(TITLE, "No results could be loaded");
return;
}
// Allow reordering when filtering
results = results.copy();
if (settings.removeSingles) {
results.removeIf(p -> p.getId() <= 0);
final TIntIntHashMap count = new TIntIntHashMap(results.size());
results.forEach((PeakResultProcedure) r -> count.adjustOrPutValue(r.getId(), 1, 1));
results.removeIf(p -> count.get(p.getId()) == 1);
if (results.isEmpty()) {
IJ.error(TITLE, "No results after filtering singles");
return;
}
}
switch(settings.filterMode) {
case D:
new FilterDiffusionCoefficient().run(results);
break;
default:
IJ.error(TITLE, "Unknown filter mode: " + settings.filterMode);
}
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class Fire method showFrcTimeEvolution.
private void showFrcTimeEvolution(String name, double fireNumber, ThresholdMethod thresholdMethod, double fourierImageScale, int imageSize) {
IJ.showStatus("Calculating FRC time evolution curve...");
// Sort by time
results.sort();
final int nSteps = 10;
int maxT = results.getLastFrame();
if (maxT == 0) {
maxT = results.size();
}
final int step = maxT / nSteps;
final TDoubleArrayList x = new TDoubleArrayList();
final TDoubleArrayList y = new TDoubleArrayList();
double yMin = fireNumber;
double yMax = fireNumber;
final MemoryPeakResults newResults = new MemoryPeakResults();
newResults.copySettings(results);
int index = 0;
final PeakResult[] list = results.toArray();
for (int t = step; t <= maxT - step; t += step) {
while (index < list.length) {
final PeakResult r = list[index];
if (r.getFrame() <= t) {
newResults.add(r);
index++;
} else {
break;
}
}
x.add(t);
final Fire f = this.copy();
final FireResult result = f.calculateFireNumber(fourierMethod, samplingMethod, thresholdMethod, fourierImageScale, imageSize);
final double fire = (result == null) ? 0 : result.fireNumber;
y.add(fire);
yMin = Math.min(yMin, fire);
yMax = Math.max(yMax, fire);
}
// Add the final fire number
x.add(maxT);
y.add(fireNumber);
final double[] xValues = x.toArray();
final double[] yValues = y.toArray();
String units = "px";
if (results.getCalibration() != null) {
nmPerUnit = results.getNmPerPixel();
units = "nm";
}
final String title = name + " FRC Time Evolution";
final Plot plot = new Plot(title, "Frames", "Resolution (" + units + ")");
final double range = Math.max(1, yMax - yMin) * 0.05;
plot.setLimits(xValues[0], xValues[xValues.length - 1], yMin - range, yMax + range);
plot.setColor(Color.red);
plot.addPoints(xValues, yValues, Plot.CONNECTED_CIRCLES);
ImageJUtils.display(title, plot);
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewer method createRankById.
private static float[] createRankById(MemoryPeakResults results) {
if (results.hasId()) {
final TFloatArrayList list = new TFloatArrayList(results.size());
results.forEach((PeakResultProcedure) r -> list.add(r.getId()));
return list.toArray();
}
return null;
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class OverlayResults method run.
@Override
public void run(String arg) {
SmlmUsageTracker.recordPlugin(this.getClass(), arg);
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "There are no fitting results in memory");
return;
}
names = new String[MemoryPeakResults.getResultNames().size() + 1];
ids = new int[names.length];
int count = 0;
names[count++] = "(None)";
for (final MemoryPeakResults results : MemoryPeakResults.getAllResults()) {
if (results.getSource() != null && results.getSource().getOriginal() instanceof IJImageSource) {
final IJImageSource source = (IJImageSource) (results.getSource().getOriginal());
final ImagePlus imp = WindowManager.getImage(source.getName());
if (imp != null) {
ids[count] = imp.getID();
names[count++] = results.getName();
}
}
}
if (count == 1) {
IJ.error(TITLE, "There are no result images available");
return;
}
names = Arrays.copyOf(names, count);
Thread thread = null;
Worker worker = null;
final NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
settings = Settings.load();
settings.save();
gd.addMessage("Overlay results on current image frame");
gd.addChoice("Results", names, (settings.name == null) ? "" : settings.name);
gd.addCheckbox("Show_table", settings.showTable);
gd.addMessage("");
gd.addHelp(HelpUrls.getUrl("overlay-results"));
gd.hideCancelButton();
gd.setOKLabel("Close");
if (!(IJ.isMacro() || java.awt.GraphicsEnvironment.isHeadless())) {
worker = new Worker();
choice = (Choice) gd.getChoices().get(0);
choice.addItemListener(worker);
checkbox = (Checkbox) gd.getCheckboxes().get(0);
checkbox.addItemListener(worker);
label = (Label) gd.getMessage();
// Initialise
worker.refresh();
// Listen for changes to an image
ImagePlus.addImageListener(worker);
thread = new Thread(worker);
thread.setDaemon(true);
thread.start();
}
gd.showDialog();
if (worker != null) {
ImagePlus.removeImageListener(worker);
}
if (!gd.wasCanceled()) {
settings.name = gd.getNextChoice();
settings.showTable = gd.getNextBoolean();
}
if (thread != null && worker != null) {
worker.running = false;
inbox.close(true);
try {
thread.join(0);
} catch (final InterruptedException ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Unexpected interruption", ex);
Thread.currentThread().interrupt();
}
}
}
Aggregations