use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class ResultsManager method cropToRoi.
private MemoryPeakResults cropToRoi(MemoryPeakResults results) {
if (roiBounds == null)
return results;
// Adjust bounds relative to input results image
double xscale = roiImageWidth / results.getBounds().width;
double yscale = roiImageHeight / results.getBounds().height;
roiBounds.x /= xscale;
roiBounds.width /= xscale;
roiBounds.y /= yscale;
roiBounds.height /= yscale;
float minX = (int) (roiBounds.x);
float maxX = (int) Math.ceil(roiBounds.x + roiBounds.width);
float minY = (int) (roiBounds.y);
float maxY = (int) Math.ceil(roiBounds.y + roiBounds.height);
// Create a new set of results within the bounds
MemoryPeakResults newResults = new MemoryPeakResults();
newResults.begin();
for (PeakResult peakResult : results.getResults()) {
float x = peakResult.params[Gaussian2DFunction.X_POSITION];
float y = peakResult.params[Gaussian2DFunction.Y_POSITION];
if (x < minX || x > maxX || y < minY || y > maxY)
continue;
newResults.add(peakResult);
}
newResults.end();
newResults.copySettings(results);
newResults.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
return newResults;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PeakFit method processResults.
private boolean processResults(FitEngine engine, ArrayList<PeakResult> sliceCandidates, int slice) {
// Process results
int[] maxIndices = new int[sliceCandidates.size()];
int count = 0;
ArrayList<PeakResult> processedResults = new ArrayList<PeakResult>(sliceCandidates.size());
for (PeakResult result : sliceCandidates) {
// Add ExtendedPeakResults to the results if they span multiple frames (they are the result of previous fitting).
if (result instanceof ExtendedPeakResult && result.getFrame() != result.getEndFrame()) {
processedResults.add(result);
} else {
// Fit single frame results.
maxIndices[count++] = result.origX + bounds.width * result.origY;
}
}
if (!processedResults.isEmpty())
this.results.addAll(processedResults);
if (count != 0) {
float[] data = source.get(slice);
if (data == null)
return false;
FitParameters fitParams = new FitParameters();
fitParams.maxIndices = Arrays.copyOf(maxIndices, count);
FitJob job = new ParameterisedFitJob(fitParams, slice, data, bounds);
engine.run(job);
}
return true;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysis method runSimulation.
private void runSimulation() {
TITLE += " Simulation";
if (!showSimulationDialog())
return;
long start = System.currentTimeMillis();
RandomDataGenerator rdg = getRandomDataGenerator();
// Draw the molecule positions
Utils.showStatus("Simulating molecules ...");
float[][][] molecules = new float[3][][];
MemoryPeakResults[] results = new MemoryPeakResults[3];
Rectangle bounds = new Rectangle(0, 0, sim_size, sim_size);
for (int c = 0; c < 3; c++) {
molecules[c] = simulateMolecules(rdg, c);
// Create a dataset to store the activations
MemoryPeakResults r = new MemoryPeakResults();
r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
r.setBounds(bounds);
r.setName(TITLE + " C" + (c + 1));
results[c] = r;
}
// Simulate activation
Utils.showStatus("Simulating activations ...");
for (int c = 0; c < 3; c++) simulateActivations(rdg, molecules, c, results);
// Combine
Utils.showStatus("Producing simulation output ...");
MemoryPeakResults r = new MemoryPeakResults();
r.setCalibration(new Calibration(sim_nmPerPixel, 1, 100));
r.setBounds(new Rectangle(0, 0, sim_size, sim_size));
r.setName(TITLE);
ImageProcessor[] images = new ImageProcessor[3];
for (int c = 0; c < 3; c++) {
ArrayList<PeakResult> list = (ArrayList<PeakResult>) results[c].getResults();
r.addAllf(list);
// Draw the unmixed activations
IJImagePeakResults image = ImagePeakResultsFactory.createPeakResultsImage(ResultsImage.LOCALISATIONS, true, true, TITLE, bounds, sim_nmPerPixel, 1, 1024.0 / sim_size, 0, ResultsMode.ADD);
image.setLiveImage(false);
image.setDisplayImage(false);
image.begin();
image.addAll(list);
image.end();
images[c] = image.getImagePlus().getProcessor();
}
displayComposite(images, TITLE);
// Add to memory. Set the composite dataset first.
MemoryPeakResults.addResults(r);
for (int c = 0; c < 3; c++) MemoryPeakResults.addResults(results[c]);
// TODO:
// Show an image of what it looks like with no unmixing, i.e. colours allocated
// from the frame
Utils.showStatus("Simulation complete: " + Utils.timeToString(System.currentTimeMillis() - start));
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PeakFit method setup.
/*
* (non-Javadoc)
*
* @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
*/
public int setup(String arg, ImagePlus imp) {
SMLMUsageTracker.recordPlugin(this.getClass(), arg);
plugin_flags = FLAGS;
extraOptions = Utils.isExtraOptions();
maximaIdentification = (arg != null && arg.contains("spot"));
fitMaxima = (arg != null && arg.contains("maxima"));
simpleFit = (arg != null && arg.contains("simple"));
boolean runSeries = (arg != null && arg.contains("series"));
ImageSource imageSource = null;
if (fitMaxima) {
imp = null;
// The image source will be found from the peak results.
if (!showMaximaDialog())
return DONE;
MemoryPeakResults results = ResultsManager.loadInputResults(inputOption, false);
if (results == null || results.size() == 0) {
IJ.error(TITLE, "No results could be loaded");
return DONE;
}
// Check for single frame
singleFrame = results.getHead().getFrame();
for (PeakResult result : results.getResults()) {
if (singleFrame != result.getFrame()) {
singleFrame = 0;
break;
}
}
imageSource = results.getSource();
plugin_flags |= NO_IMAGE_REQUIRED;
} else if (runSeries) {
imp = null;
// Select input folder
String inputDirectory;
inputDirectory = IJ.getDirectory("Select image series ...");
//inputDirectory = getInputDirectory("Select image series ...");
if (inputDirectory == null)
return DONE;
// Load input series ...
SeriesOpener series;
if (extraOptions)
series = new SeriesOpener(inputDirectory, true, numberOfThreads);
else
series = new SeriesOpener(inputDirectory);
if (series.getNumberOfImages() == 0) {
IJ.error(TITLE, "No images in the selected directory:\n" + inputDirectory);
return DONE;
}
SeriesImageSource seriesImageSource = new SeriesImageSource(getName(series.getImageList()), series);
seriesImageSource.setLogProgress(true);
if (extraOptions) {
numberOfThreads = Math.max(1, series.getNumberOfThreads());
seriesImageSource.setNumberOfThreads(numberOfThreads);
}
imageSource = seriesImageSource;
plugin_flags |= NO_IMAGE_REQUIRED;
} else {
if (imp == null) {
IJ.noImage();
return DONE;
}
// Check it is not a previous result
if (imp.getTitle().endsWith(IJImagePeakResults.IMAGE_SUFFIX)) {
IJImageSource tmpImageSource = null;
// Check the image to see if it has an image source XML structure in the info property
Object o = imp.getProperty("Info");
Pattern pattern = Pattern.compile("Source: (<.*IJImageSource>.*<.*IJImageSource>)", Pattern.DOTALL);
Matcher match = pattern.matcher((o == null) ? "" : o.toString());
if (match.find()) {
ImageSource source = ImageSource.fromXML(match.group(1));
if (source instanceof IJImageSource) {
tmpImageSource = (IJImageSource) source;
if (!tmpImageSource.open()) {
tmpImageSource = null;
} else {
imp = WindowManager.getImage(tmpImageSource.getName());
}
}
}
if (tmpImageSource == null) {
// Look for a parent using the title
String parentTitle = imp.getTitle().substring(0, imp.getTitle().length() - IJImagePeakResults.IMAGE_SUFFIX.length() - 1);
ImagePlus parentImp = WindowManager.getImage(parentTitle);
if (parentImp != null) {
tmpImageSource = new IJImageSource(parentImp);
imp = parentImp;
}
}
String message = "The selected image may be a previous fit result";
if (tmpImageSource != null) {
// are missing
if (!Utils.isNullOrEmpty(tmpImageSource.getName()))
message += " of: \n \n" + tmpImageSource.getName();
message += " \n \nFit the parent?";
} else
message += " \n \nDo you want to continue?";
YesNoCancelDialog d = new YesNoCancelDialog(null, TITLE, message);
if (tmpImageSource == null) {
if (!d.yesPressed())
return DONE;
} else {
if (d.yesPressed())
imageSource = tmpImageSource;
if (d.cancelPressed())
return DONE;
}
}
if (imageSource == null)
imageSource = new IJImageSource(imp);
}
time = -1;
if (!initialiseImage(imageSource, getBounds(imp), false)) {
IJ.error(TITLE, "Failed to initialise the source image: " + imageSource.getName());
return DONE;
}
int flags = showDialog(imp);
if ((flags & DONE) == 0) {
// Repeat so that we pass in the selected option for ignoring the bounds.
// This should not be necessary since it is set within the readDialog method.
//if (ignoreBoundsForNoise)
// initialiseImage(imageSource, bounds, ignoreBoundsForNoise);
initialiseFitting();
}
return flags;
}
use of gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class ResultsMatchCalculator method getTimepoints.
/**
* Merge the time points from the two sets into a single sorted list of unique time points
*
* @param actualPoints
* @param predictedPoints
* @return
*/
@SuppressWarnings("unused")
private int[] getTimepoints(List<PeakResult> actualPoints, List<PeakResult> predictedPoints) {
TIntHashSet set = new TIntHashSet();
for (PeakResult r : actualPoints) set.add(r.getFrame());
for (PeakResult r : predictedPoints) set.add(r.getFrame());
int[] t = set.toArray();
Arrays.sort(t);
return t;
}
Aggregations