use of uk.ac.sussex.gdsc.core.data.utils.TypeConverter in project GDSC-SMLM by aherbert.
the class CreateData method setNoise.
/**
* Sets the noise in the results if missing.
*
* @param results the results
* @param imp the imp
*/
private static void setNoise(MemoryPeakResults results, ImagePlus imp) {
// Loaded results do not have noise
if (results.hasNoise()) {
return;
}
IJ.showStatus("Estimating noise ...");
// Compute noise per frame
final ImageStack stack = imp.getImageStack();
final int width = stack.getWidth();
final int height = stack.getHeight();
final IJImageSource source = new IJImageSource(imp);
final float[] noise = new float[source.getFrames() + 1];
source.setReadHint(ReadHint.SEQUENTIAL);
source.open();
for (int slice = 1; slice < noise.length; slice++) {
final float[] data = source.next();
// Use the trimmed method as there may be a lot of spots in the frame
noise[slice] = FitWorker.estimateNoise(data, width, height, NoiseEstimatorMethod.QUICK_RESIDUALS_LEAST_TRIMMED_OF_SQUARES);
}
// Statistics stats = Statistics.create(Arrays.copyOfRange(noise, 1, noise.length));
// System.out.printf("Noise = %.3f +/- %.3f (%d)\n", stats.getMean(),
// stats.getStandardDeviation(), stats.getN());
// Convert noise units from counts to the result format
final TypeConverter<IntensityUnit> c = results.getIntensityConverter(IntensityUnit.COUNT);
for (int i = 0; i < noise.length; i++) {
noise[i] = c.convertBack(noise[i]);
}
results.forEach((PeakResultProcedure) result -> {
if (result.getFrame() < noise.length) {
result.setNoise(noise[result.getFrame()]);
}
});
}
use of uk.ac.sussex.gdsc.core.data.utils.TypeConverter 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.core.data.utils.TypeConverter in project GDSC-SMLM by aherbert.
the class TraceExporter method exportSpotOn.
private void exportSpotOn(MemoryPeakResults results) {
try (BufferedWriter out = Files.newBufferedWriter(Paths.get(settings.directory, results.getName() + ".csv"))) {
out.write("frame,t,trajectory,x,y");
out.newLine();
final TypeConverter<TimeUnit> converter = UnitConverterUtils.createConverter(TimeUnit.FRAME, TimeUnit.SECOND, results.getCalibrationReader().getExposureTime());
@SuppressWarnings("resource") final BufferedWriter writer = out;
results.forEach(DistanceUnit.UM, (XyrResultProcedure) (x, y, result) -> {
try {
if (result.hasEndFrame()) {
final String sId = Integer.toString(result.getId());
final String sx = Float.toString(x);
final String sy = Float.toString(y);
for (int t = result.getFrame(); t <= result.getEndFrame(); t++) {
writer.write(Integer.toString(t));
writer.write(",");
writer.write(Float.toString(converter.convert(t)));
writer.write(",");
writer.write(sId);
writer.write(",");
writer.write(sx);
writer.write(",");
writer.write(sy);
writer.newLine();
}
} else {
writer.write(Integer.toString(result.getFrame()));
writer.write(",");
writer.write(Float.toString(converter.convert(result.getFrame())));
writer.write(",");
writer.write(Integer.toString(result.getId()));
writer.write(",");
writer.write(Float.toString(x));
writer.write(",");
writer.write(Float.toString(y));
writer.newLine();
}
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
});
} catch (final IOException | RuntimeException ex) {
handleException(ex);
}
}
use of uk.ac.sussex.gdsc.core.data.utils.TypeConverter in project GDSC-SMLM by aherbert.
the class TraceExporter method export.
private void export(MemoryPeakResults results) {
// Copy to allow manipulation
results = results.copy();
// Strip results with no trace Id
results.removeIf(result -> result.getId() <= 0);
// Sort by ID then time
results.sort(IdFramePeakResultComparator.INSTANCE);
// Split traces with big jumps and long tracks into smaller tracks
results = splitTraces(results);
results = splitLongTraces(results);
// Count each ID and remove short traces
int id = 0;
int count = 0;
int tracks = 0;
int maxLength = 0;
final TIntHashSet remove = new TIntHashSet();
for (int i = 0, size = results.size(); i < size; i++) {
final PeakResult result = results.get(i);
if (result.getId() != id) {
if (count < settings.minLength) {
remove.add(id);
} else {
tracks++;
maxLength = Math.max(maxLength, count);
}
count = 0;
id = result.getId();
}
count += getLength(result);
}
// Final ID
if (count < settings.minLength) {
remove.add(id);
} else {
tracks++;
maxLength = Math.max(maxLength, count);
}
if (!remove.isEmpty()) {
results.removeIf(result -> remove.contains(result.getId()));
results.sort(IdFramePeakResultComparator.INSTANCE);
}
if (settings.wobble > 0) {
// Just leave any exceptions to trickle up and kill the plugin
final TypeConverter<DistanceUnit> c = results.getDistanceConverter(DistanceUnit.NM);
final double w = c.convertBack(settings.wobble);
final UniformRandomProvider rng = UniformRandomProviders.create();
final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
final boolean is3D = results.is3D();
results.forEach((PeakResultProcedure) peakResult -> {
peakResult.setXPosition((float) (peakResult.getXPosition() + w * gauss.sample()));
peakResult.setYPosition((float) (peakResult.getYPosition() + w * gauss.sample()));
if (is3D) {
peakResult.setZPosition((float) (peakResult.getZPosition() + w * gauss.sample()));
}
});
}
if (settings.format == 2) {
exportVbSpt(results);
} else if (settings.format == 1) {
exportAnaDda(results);
} else {
exportSpotOn(results);
}
ImageJUtils.log("Exported %s: %s in %s", results.getName(), TextUtils.pleural(results.size(), "localisation"), TextUtils.pleural(tracks, "track"));
if (settings.showTraceLengths) {
// We store and index (count-1)
final int[] h = new int[maxLength];
id = 0;
for (int i = 0, size = results.size(); i < size; i++) {
final PeakResult result = results.get(i);
if (result.getId() != id) {
h[count - 1]++;
count = 0;
id = result.getId();
}
count += getLength(result);
}
h[count - 1]++;
final String title = TITLE + ": " + results.getName();
final Plot plot = new Plot(title, "Length", "Frequency");
plot.addPoints(SimpleArrayUtils.newArray(h.length, 1, 1.0f), SimpleArrayUtils.toFloat(h), Plot.BAR);
plot.setLimits(SimpleArrayUtils.findIndex(h, i -> i != 0), maxLength + 1, 0, Double.NaN);
ImageJUtils.display(title, plot);
}
}
use of uk.ac.sussex.gdsc.core.data.utils.TypeConverter in project GDSC-SMLM by aherbert.
the class SpotInspector method mouseClicked.
private void mouseClicked(MouseEvent event) {
if (id != currentId.get()) {
return;
}
// Show the result that was double clicked in the result table
if (event.getClickCount() > 1) {
final int rank = textPanel.getSelectionStart() + 1;
// Show the spot that was double clicked
final ImagePlus imp = WindowManager.getImage(TITLE);
if (imp != null && rank > 0 && rank <= imp.getStackSize()) {
imp.setSlice(rank);
if (imp.getWindow() != null) {
imp.getWindow().toFront();
}
final PeakResult r = rankedResults.get(rank - 1).peakResult;
final TypeConverter<DistanceUnit> dc = results.getDistanceConverter(DistanceUnit.PIXEL);
final float rx = dc.convert(r.getXPosition());
final float ry = dc.convert(r.getYPosition());
final int x = (int) rx;
final int y = (int) ry;
// Find bounds
final int minX = x - settings.radius;
final int minY = y - settings.radius;
// Require the Shift key to add all spots
if (!event.isShiftDown()) {
// Add the single clicked spot
imp.setRoi(new OffsetPointRoi(rx - minX, ry - minY));
return;
}
// Add all the spots
final int maxX = x + settings.radius + 1;
final int maxY = y + settings.radius + 1;
// Create ROIs
final HashSet<Point2D.Float> spots = new HashSet<>();
results.forEach(DistanceUnit.PIXEL, (XyResultProcedure) (xp, yp) -> {
if (xp > minX && xp < maxX && yp > minY && yp < maxY) {
// Use only unique points
spots.add(new Point2D.Float(xp - minX, yp - minY));
}
});
final int points = spots.size();
final float[] ox = new float[points];
final float[] oy = new float[points];
final Counter c = new Counter();
spots.forEach(p -> {
ox[c.getCount()] = p.x;
oy[c.getAndIncrement()] = p.y;
});
imp.setRoi(new OffsetPointRoi(ox, oy, points));
}
}
}
Aggregations