use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ResultsManager method load.
private static void load(String path) {
// to get the calibration.
if (Recorder.record) {
Recorder.setCommand("Results Manager");
Recorder.recordOption("input", INPUT_FILE);
Recorder.recordOption("input_file", path);
Recorder.recordOption("image", SettingsManager.getResultsImageTypeNames()[ResultsImageType.DRAW_NONE_VALUE]);
Recorder.recordOption("results_file", "[]");
Recorder.recordOption("save_to_memory");
}
final MemoryPeakResults results = loadInputResults(INPUT_FILE, true, null, null, new FilenameLoadOption(path));
if (MemoryPeakResults.isEmpty(results)) {
IJ.error(TITLE, "No results could be loaded from " + path);
} else {
if (addResultsToMemory(results, path) && Recorder.record) {
Recorder.saveCommand();
}
}
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ResultsManager method runClearMemory.
private static void runClearMemory(String arg) {
if (MemoryPeakResults.isMemoryEmpty()) {
IJ.error(TITLE, "There are no fitting results in memory");
IJ.showStatus("");
return;
}
Collection<MemoryPeakResults> allResults;
boolean removeAll = false;
String helpKey = "clear-memory-results";
if (arg.contains("multi")) {
helpKey += "-multi";
final MultiDialog md = createMultiDialog(TITLE);
md.setSelected(lastSelected.get());
md.setHelpUrl(HelpUrls.getUrl(helpKey));
md.showDialog();
if (md.wasCancelled()) {
return;
}
final List<String> selected = md.getSelectedResults();
if (selected.isEmpty()) {
return;
}
lastSelected.set(selected);
allResults = new ArrayList<>(selected.size());
for (final String name : selected) {
final MemoryPeakResults r = MemoryPeakResults.getResults(name);
if (r != null) {
allResults.add(r);
}
}
} else {
removeAll = true;
allResults = MemoryPeakResults.getAllResults();
}
if (allResults.isEmpty()) {
return;
}
long memorySize = 0;
int size = 0;
for (final MemoryPeakResults results : allResults) {
memorySize += MemoryPeakResults.estimateMemorySize(results);
size += results.size();
}
final String memory = TextUtils.bytesToString(memorySize);
final String count = TextUtils.pleural(size, "result");
final String sets = TextUtils.pleural(allResults.size(), "set");
final GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage(String.format("Do you want to remove %s from memory (%s, %s)?", count, sets, memory));
gd.addHelp(HelpUrls.getUrl(helpKey));
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
if (removeAll) {
MemoryPeakResults.clearMemory();
} else {
for (final MemoryPeakResults results : allResults) {
MemoryPeakResults.removeResults(results.getName());
}
}
SummariseResults.clearSummaryTable();
ImageJUtils.log("Cleared %s (%s, %s)", count, sets, memory);
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class PeakFit method getSingleFrame.
/**
* Gets the single frame containing all the results (if they are all in a single frame), else 0.
*
* @param results the results (must not be empty)
* @return the single frame (or zero)
*/
private static int getSingleFrame(MemoryPeakResults results) {
final FrameCounter counter = new FrameCounter(results.getFirstFrame());
// The counter will return true (stop execution) if a new frame
results.forEach((PeakResultProcedureX) peakResult -> counter.advance(peakResult.getFrame()));
if (counter.currentFrame() != counter.previousFrame()) {
return 0;
}
return counter.currentFrame();
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class PeakFit method runMaximaFitting.
/**
* Load the selected results from memory. All multiple frame results are added directly to the
* results. All single frame results are added to a list of candidate maxima per frame and fitted
* using the configured parameters.
*/
private void runMaximaFitting() {
final MemoryPeakResults memoryResults = ResultsManager.loadInputResults(settings.inputOption, false, DistanceUnit.PIXEL);
if (memoryResults == null || memoryResults.size() == 0) {
log("No results for maxima fitting");
return;
}
// The total frames (for progress reporting)
int totalFrames;
// A function that can convert a frame into a set of candidate indices
final IntFunction<int[]> frameToMaxIndices;
// The frames to process (should be sorted ascending)
Supplier<IntStream> frames;
// Support fitting all time frames with the same results.
if (settings.fitAcrossAllFrames) {
// Check if the input spans multiple frames
if (getSingleFrame(memoryResults) == 0) {
final int min = memoryResults.getMinFrame();
final int max = memoryResults.getMaxFrame();
final GenericDialog gd = new GenericDialog(TITLE);
gd.enableYesNoCancel();
gd.hideCancelButton();
ImageJUtils.addMessage(gd, "Candidate maxima for fitting span multiple frames (%d-%d).\n \n" + "Please confirm the %s are correct.", min, max, TextUtils.pleural(memoryResults.size(), "candidate"));
gd.showDialog();
if (!gd.wasOKed()) {
return;
}
}
final int[] maxIndices = getMaxIndices(Arrays.asList(memoryResults.toArray()));
// This may not work correctly if using for example a series image source that
// incorrectly estimates the number of frames
totalFrames = source.getFrames();
frameToMaxIndices = frame -> maxIndices;
frames = () -> IntStream.rangeClosed(1, totalFrames);
} else {
// Build a map between the time-frame and the results in that frame.
final Map<Integer, List<PeakResult>> map = Arrays.stream(memoryResults.toArray()).parallel().filter(peakResult -> peakResult.getFrame() == peakResult.getEndFrame()).collect(Collectors.groupingBy(PeakResult::getFrame));
totalFrames = map.size();
// Build a function that can convert a frame into a set of candidate indices
frameToMaxIndices = frame -> getMaxIndices(map.get(frame));
frames = () -> map.keySet().stream().mapToInt(Integer::intValue).sorted();
}
final ImageStack stack = (extraSettings.showProcessedFrames) ? new ImageStack(bounds.width, bounds.height) : null;
// Use the FitEngine to allow multi-threading.
final FitEngine engine = createFitEngine(getNumberOfThreads(totalFrames));
if (engine == null) {
return;
}
final int step = ImageJUtils.getProgressInterval(totalFrames);
// No crop bounds are supported.
// To pre-process data for noise estimation
final boolean isFitCameraCounts = fitConfig.isFitCameraCounts();
final CameraModel cameraModel = fitConfig.getCameraModel();
runTime = System.nanoTime();
final AtomicBoolean shutdown = new AtomicBoolean();
final String format = String.format("Slice: %%d / %d (Results=%%d)", totalFrames);
frames.get().forEachOrdered(slice -> {
if (shutdown.get() || escapePressed()) {
shutdown.set(true);
return;
}
final float[] data = source.get(slice);
if (data == null) {
shutdown.set(true);
return;
}
if (slice % step == 0) {
if (ImageJUtils.showStatus(() -> String.format(format, slice, results.size()))) {
IJ.showProgress(slice, totalFrames);
}
}
// We must pre-process the data before noise estimation
final float[] data2 = data.clone();
if (isFitCameraCounts) {
cameraModel.removeBias(data2);
} else {
cameraModel.removeBiasAndGain(data2);
}
final float noise = FitWorker.estimateNoise(data2, source.getWidth(), source.getHeight(), config.getNoiseMethod());
if (stack != null) {
stack.addSlice(String.format("Frame %d - %d", source.getStartFrameNumber(), source.getEndFrameNumber()), data);
}
// Get the frame number from the source to allow for interlaced and aggregated data
engine.run(createMaximaFitJob(frameToMaxIndices.apply(slice), source.getStartFrameNumber(), source.getEndFrameNumber(), data, bounds, noise));
});
engine.end(shutdown.get());
time = engine.getTime();
runTime = System.nanoTime() - runTime;
if (stack != null) {
ImageJUtils.display("Processed frames", stack);
}
showResults();
source.close();
}
use of uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewer method getPoints.
/**
* Gets the points.
*
* @param results the results
* @param settings the settings
* @return the points
*/
static LocalList<Point3f> getPoints(MemoryPeakResults results, ImageJ3DResultsViewerSettingsOrBuilder settings) {
final LocalList<Point3f> points = new LocalList<>(results.size());
if (results.is3D()) {
results.forEach(DistanceUnit.NM, (XyzResultProcedure) (x, y, z) -> {
points.push(new Point3f(x, y, z));
});
} else {
results.forEach(DistanceUnit.NM, (XyResultProcedure) (x, y) -> {
points.push(new Point3f(x, y, 0));
});
final double range = settings.getDepthRange();
if (range > 0 && results.size() > 1) {
final DepthMode mode = DepthMode.forNumber(settings.getDepthMode());
final double min = -settings.getDepthRange() / 2;
switch(mode) {
case DITHER:
final SplitMix r = SplitMix.new64(settings.getDitherSeed());
for (int i = points.size(); i-- > 0; ) {
points.unsafeGet(i).z += (min + r.nextDouble() * range);
}
break;
case INTENSITY:
// Rank by intensity, highest first
final StandardResultProcedure p = new StandardResultProcedure(results);
p.getI();
final int[] indices = SimpleArrayUtils.natural(results.size());
SortUtils.sortIndices(indices, p.intensity, true);
final double inc = range / indices.length;
for (int i = 0; i < indices.length; i++) {
// The standard rendering has +z going away so put the highest rank at min
points.unsafeGet(indices[i]).z += (min + i * inc);
}
break;
case NONE:
break;
default:
throw new IllegalStateException("Unknown depth mode: " + mode);
}
}
}
return points;
}
Aggregations