use of uk.ac.sussex.gdsc.smlm.ij.results.ResultsImageSampler in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method getSampler.
private static synchronized ResultsImageSampler getSampler(MemoryPeakResults results, ImagePlus imp) {
Pair<Integer, ResultsImageSampler> sampler = samplerRef.get();
if (sampler == null || imp.getID() != sampler.getKey() || sampler.getValue().getResults() != results) {
final ResultsImageSampler imageSampler = new ResultsImageSampler(results, imp.getImageStack(), 32);
imageSampler.analyse();
sampler = Pair.of(imp.getID(), imageSampler);
samplerRef.set(sampler);
}
return sampler.getValue();
}
use of uk.ac.sussex.gdsc.smlm.ij.results.ResultsImageSampler in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method saveTemplate.
/**
* Save PeakFit configuration template using the current benchmark settings.
*
* @param topFilterSummary the top filter summary
*/
private void saveTemplate(String topFilterSummary) {
final FitEngineConfiguration config = new FitEngineConfiguration();
if (!updateAllConfiguration(config, true)) {
IJ.log("Unable to create the template configuration");
return;
}
// Remove the PSF width to make the template generic
config.getFitConfiguration().setInitialPeakStdDev(0);
// Only get this once when doing iterative analysis
String filename;
final boolean localSaveTemplateIsSet = saveTemplateIsSet;
if (localSaveTemplateIsSet) {
filename = settings.templateFilename;
} else {
filename = getFilename("Template_File", settings.templateFilename);
saveTemplateIsSet = true;
}
if (filename != null) {
settings.templateFilename = filename;
Prefs.set(Settings.KEY_TEMPLATE_FILENAME, filename);
final TemplateSettings.Builder templateSettings = TemplateSettings.newBuilder();
getNotes(templateSettings, topFilterSummary);
templateSettings.setFitEngineSettings(config.getFitEngineSettings());
if (!SettingsManager.toJson(templateSettings.build(), filename, SettingsManager.FLAG_SILENT | SettingsManager.FLAG_JSON_WHITESPACE)) {
IJ.log("Unable to save the template configuration");
return;
}
// This need only be performed once as the sample image is the same for all iterations.
if (localSaveTemplateIsSet) {
return;
}
// Save some random frames from the test image data
final ImagePlus imp = CreateData.getImage();
if (imp == null) {
return;
}
// Get the number of frames
final ResultsImageSampler sampler = getSampler(results, imp);
if (!sampler.isValid()) {
return;
}
// Iteratively show the example until the user is happy.
// Yes = OK, No = Repeat, Cancel = Do not save
final String keyNo = "nNo";
final String keyLow = "nLower";
final String keyHigh = "nHigher";
if (ImageJUtils.isMacro()) {
// Collect the options if running in a macro
final String options = Macro.getOptions();
settings.countNo = Integer.parseInt(Macro.getValue(options, keyNo, Integer.toString(settings.countNo)));
settings.countLow = Integer.parseInt(Macro.getValue(options, keyLow, Integer.toString(settings.countLow)));
settings.countHigh = Integer.parseInt(Macro.getValue(options, keyHigh, Integer.toString(settings.countHigh)));
} else if (settings.countLow + settings.countHigh == 0) {
settings.countLow = settings.countHigh = 1;
}
final ImagePlus[] out = new ImagePlus[1];
out[0] = sampler.getSample(settings.countNo, settings.countLow, settings.countHigh);
if (!ImageJUtils.isMacro()) {
// Show the template results
final ConfigurationTemplate configTemplate = new ConfigurationTemplate();
// Interactively show the sample image data
final boolean[] close = new boolean[1];
final ImagePlus[] outImp = new ImagePlus[1];
if (out[0] != null) {
final WindowOrganiser windowOrganiser = new WindowOrganiser();
outImp[0] = display(out[0], windowOrganiser);
if (windowOrganiser.isNotEmpty()) {
close[0] = true;
// Zoom a bit
final ImageWindow iw = outImp[0].getWindow();
for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
iw.getCanvas().zoomIn(0, 0);
}
}
configTemplate.createResults(outImp[0]);
}
// TODO - fix this when a second sample is made as the results are not updated.
final ImageListener listener = new ImageListener() {
@Override
public void imageOpened(ImagePlus imp) {
// Do nothing
}
@Override
public void imageClosed(ImagePlus imp) {
// Do nothing
}
@Override
public void imageUpdated(ImagePlus imp) {
if (imp != null && imp == outImp[0]) {
configTemplate.updateResults(imp.getCurrentSlice());
}
}
};
ImagePlus.addImageListener(listener);
// Turn off the recorder when the dialog is showing
final boolean record = Recorder.record;
Recorder.record = false;
final NonBlockingGenericDialog gd = new NonBlockingGenericDialog(TITLE);
ImageJUtils.addMessage(gd, "Showing image data for the template example.\n \nSample Frames:\nEmpty = %d\n" + "Lower density = %d\nHigher density = %d\n", sampler.getNumberOfEmptySamples(), sampler.getNumberOfLowDensitySamples(), sampler.getNumberOfHighDensitySamples());
gd.addSlider(keyNo, 0, 10, settings.countNo);
gd.addSlider(keyLow, 0, 10, settings.countLow);
gd.addSlider(keyHigh, 0, 10, settings.countHigh);
gd.addDialogListener((genDialog, event) -> {
// image the user has not seen.
if (event == null) {
return true;
}
settings.countNo = (int) genDialog.getNextNumber();
settings.countLow = (int) genDialog.getNextNumber();
settings.countHigh = (int) genDialog.getNextNumber();
out[0] = sampler.getSample(settings.countNo, settings.countLow, settings.countHigh);
if (out[0] != null) {
final WindowOrganiser windowOrganiser = new WindowOrganiser();
outImp[0] = display(out[0], windowOrganiser);
if (windowOrganiser.isNotEmpty()) {
close[0] = true;
// Zoom a bit
final ImageWindow iw = outImp[0].getWindow();
for (int i = 7; i-- > 0 && Math.max(iw.getWidth(), iw.getHeight()) < 512; ) {
iw.getCanvas().zoomIn(0, 0);
}
}
configTemplate.createResults(outImp[0]);
}
return true;
});
gd.showDialog();
if (gd.wasCanceled()) {
out[0] = null;
// For the recorder
settings.countNo = settings.countLow = settings.countHigh = 0;
}
if (close[0]) {
// Because closing the image sets the stack pixels array to null
if (out[0] != null) {
out[0] = out[0].duplicate();
}
outImp[0].close();
}
configTemplate.closeResults();
ImagePlus.removeImageListener(listener);
if (record) {
Recorder.record = true;
Recorder.recordOption(keyNo, Integer.toString(settings.countNo));
Recorder.recordOption(keyLow, Integer.toString(settings.countLow));
Recorder.recordOption(keyHigh, Integer.toString(settings.countHigh));
}
}
if (out[0] == null) {
return;
}
final ImagePlus example = out[0];
filename = FileUtils.replaceExtension(filename, ".tif");
IJ.save(example, filename);
}
}
Aggregations