use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class CreateData method saveFixedAndMoving.
private void saveFixedAndMoving(MemoryPeakResults results) {
if (simpleMode || benchmarkMode || spotMode) {
return;
}
if (settings.getDiffusionRate() <= 0 || settings.getFixedFraction() >= 1) {
return;
}
final MemoryPeakResults fixedResults = copyMemoryPeakResults("Fixed");
final MemoryPeakResults movingResults = copyMemoryPeakResults("Moving");
final PeakResult[] peakResults = results.toArray();
// Sort using the ID
Arrays.sort(peakResults, new Comparator<PeakResult>() {
@Override
public int compare(PeakResult o1, PeakResult o2) {
return o1.getId() - o2.getId();
}
});
MemoryPeakResults currentResults = movingResults;
final FrameCounter counter = new FrameCounter(-1);
for (final PeakResult p : peakResults) {
if (counter.advance(p.getId())) {
currentResults = (movingMolecules.contains(p.getId())) ? movingResults : fixedResults;
}
currentResults.add(p);
}
movingResults.end();
fixedResults.end();
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class CreateData method loadBenchmarkData.
/**
* Load benchmark data using an open image and a XYZ text file.
*/
private void loadBenchmarkData() {
if (!showLoadDialog()) {
// resetMemory();
return;
}
// Load the image
final ImagePlus imp = WindowManager.getImage(benchmarkImage);
if (imp == null) {
IJ.error(TITLE, "No benchmark image: " + benchmarkImage);
// resetMemory();
return;
}
// Load the results
final MemoryPeakResults results = getSimulationResults();
if (results == null) {
IJ.error(TITLE, "No benchmark results: " + benchmarkResultsName);
// resetMemory();
return;
}
results.setName(imp.getTitle() + " (Results)");
results.setBounds(new Rectangle(0, 0, imp.getWidth(), imp.getHeight()));
final IJImageSource imageSource = new IJImageSource(imp);
results.setSource(imageSource);
// Load the settings as these are used in the dialog
settings = SettingsManager.readCreateDataSettings(0).toBuilder();
simulationParameters = showSimulationParametersDialog(imp, results);
if (simulationParameters != null) {
// Convert data to allow analysis as if a Gaussian2D PSF
final boolean isGaussian2D = PsfHelper.isGaussian2D(results.getPsf());
if (isGaussian2D) {
Gaussian2DPeakResultHelper.addMeanIntensity(results.getPsf(), results);
} else if (simulationParameters.sd > 0) {
final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.NM);
final PSF.Builder psf = PsfProtosHelper.getDefaultPsf(PSFType.ONE_AXIS_GAUSSIAN_2D).toBuilder();
psf.getParametersBuilder(0).setValue(dc.convertBack(simulationParameters.sd));
results.setPsf(psf.build());
// Update all the results. This assumes the results do not have data for a custom PSF,
// i.e. the parameters only have [t,i,x,y,z]
final LocalList<PeakResult> newResults = new LocalList<>(results.size());
final float sd = (float) dc.convertBack(simulationParameters.sd);
final double meanFactor = Gaussian2DPeakResultHelper.getMeanSignalUsingP05(1, sd, sd);
results.forEach((PeakResultProcedure) r -> {
final PeakResult peak = r.resize(PeakResult.STANDARD_PARAMETERS + 1);
peak.setMeanIntensity((float) (peak.getIntensity() * meanFactor));
peak.setParameter(PeakResult.STANDARD_PARAMETERS, sd);
newResults.add(peak);
});
results.begin();
results.addAll(newResults);
results.end();
}
setBackground(results);
setNoise(results, imp);
setBenchmarkResults(imp, results);
IJ.showStatus("Loaded " + TextUtils.pleural(results.size(), "result"));
} else {
resetMemory();
}
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PsfImagePeakResults method addAll.
@Override
public void addAll(PeakResult[] results) {
if (!imageActive) {
return;
}
// TODO - Make this more efficient. It could use worker threads to increase speed.
int counter = 0;
for (final PeakResult result : results) {
addPeak(result.getFrame(), result.getNoise(), result.getParameters());
if (++counter % 64 == 0) {
updateImage();
if (!imageActive) {
return;
}
}
}
updateImage();
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class PeakResultTableModelFrame method doSourceOverlay.
private void doSourceOverlay() {
final PeakResultTableModel model = getModel();
if (model == null) {
return;
}
final PeakResult[] list = table.getSelectedData();
if (list.length == 0) {
return;
}
final ImageSource source = model.getSource();
if (source == null) {
return;
}
final String title = source.getOriginal().getName();
final ImagePlus imp = WindowManager.getImage(title);
if (imp == null) {
return;
}
// Assumes 3D stack (no channel/time)
if (imp.getNDimensions() > 3) {
return;
}
try {
final TypeConverter<DistanceUnit> converter = CalibrationHelper.getDistanceConverter(model.getCalibration(), DistanceUnit.PIXEL);
final Overlay o = new Overlay();
if (list.length == 1) {
final PeakResult p = list[0];
final PointRoi roi = new OffsetPointRoi(converter.convert(p.getXPosition()), converter.convert(p.getYPosition()));
roi.setPointType(3);
roi.setPosition(p.getFrame());
o.add(roi);
} else {
Arrays.sort(list, FramePeakResultComparator.INSTANCE);
final TFloatArrayList ox = new TFloatArrayList(list.length);
final TFloatArrayList oy = new TFloatArrayList(list.length);
int frame = list[0].getFrame() - 1;
for (int i = 0; i < list.length; i++) {
if (frame != list[i].getFrame()) {
if (ox.size() > 0) {
final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
roi.setPointType(3);
roi.setPosition(frame);
ox.resetQuick();
oy.resetQuick();
o.add(roi);
}
frame = list[i].getFrame();
}
ox.add(converter.convert(list[i].getXPosition()));
oy.add(converter.convert(list[i].getYPosition()));
}
if (ox.size() > 0) {
final PointRoi roi = new OffsetPointRoi(ox.toArray(), oy.toArray());
roi.setPointType(3);
roi.setPosition(frame);
o.add(roi);
}
}
imp.setOverlay(o);
final PeakResult p = list[0];
imp.setSlice(p.getFrame());
ImageJUtils.adjustSourceRect(imp, 0, (int) converter.convert(p.getXPosition()), (int) converter.convert(p.getYPosition()));
imp.getWindow().toFront();
} catch (final ConversionException ex) {
// Ignore
}
}
use of uk.ac.sussex.gdsc.smlm.results.PeakResult in project GDSC-SMLM by aherbert.
the class ResultsImageSampler method createResultSamples.
/**
* Creates the result samples. Do this by storing the coordinates at the region index.
*/
private void createResultSamples() {
final TLongObjectHashMap<ResultsSample> map = new TLongObjectHashMap<>(results.size());
ResultsSample next = ResultsSample.create(-1);
// For SNR computation
Object[] pixelArray = stack.getImageArray();
int width = stack.getWidth();
int height = stack.getHeight();
float[] buffer = null;
// Use a null float[] as this is not used for the getBoxRegionBounds method
ImageExtractor ie = ImageExtractor.wrap(null, width, height);
for (final PeakResult p : results.toArray()) {
// Avoid invalid slices
if (p.getFrame() < 1 || p.getFrame() > stack.getSize()) {
continue;
}
// Avoid low SNR results. Get the SNR using a 3x3 region around the spot.
final Rectangle bounds = ie.getBoxRegionBounds((int) p.getXPosition(), (int) p.getYPosition(), 1);
buffer = ImageJImageConverter.getData(pixelArray[p.getFrame()], width, height, bounds, buffer);
final Statistics stats = new Statistics();
stats.add(buffer, 0, bounds.width * bounds.height);
// SNR will be NaN if the region has no size
final double snr = stats.getMean() / stats.getStandardDeviation();
if (snr > 3) {
final long index = getIndex(p.getXPosition(), p.getYPosition(), p.getFrame());
ResultsSample current = map.putIfAbsent(index, next);
if (current == null) {
// If the return value is null then this is a new insertion.
// Set the current value as the one we just added and create the next insertion object.
current = next;
current.index = index;
next = ResultsSample.create(-1);
}
current.add(p);
}
}
// Create an array of all the sample entries.
// This is used to sample regions by density.
data = map.values(new ResultsSample[map.size()]);
}
Aggregations