use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class AbstractFilterTest method getIntSpeedData.
/**
* Create random int data.
*
* @param seed the seed
* @param size the number of datasets
* @return the array list of random data
*/
ArrayList<int[]> getIntSpeedData(RandomSeed seed, int size) {
final FloatData data = (FloatData) ConcurrentHashMap.computeIfAbsent(seed, AbstractFilterTest::createSpeedData);
final ArrayList<float[]> dataSet = data.dataSet;
if (dataSet.size() < size) {
final UniformRandomProvider rg = data.rg;
synchronized (dataSet) {
while (dataSet.size() < size) {
dataSet.add(createData(rg, primes[0], primes[0]));
}
}
}
final ArrayList<int[]> dataSet2 = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
final float[] f = dataSet.get(i);
final int[] d = new int[f.length];
for (int j = 0; j < d.length; j++) {
d[j] = (int) (4096 * f[j]);
}
dataSet2.add(d);
}
return dataSet2;
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class AbstractFilterTest method getSpeedData.
/**
* Create random float data.
*
* @param seed the seed
* @param size the number of datasets
* @return the array list of random data
*/
ArrayList<float[]> getSpeedData(RandomSeed seed, int size) {
final FloatData data = (FloatData) ConcurrentHashMap.computeIfAbsent(seed, AbstractFilterTest::createSpeedData);
final ArrayList<float[]> dataSet = data.dataSet;
if (dataSet.size() < size) {
final UniformRandomProvider rg = data.rg;
synchronized (dataSet) {
while (dataSet.size() < size) {
dataSet.add(createData(rg, primes[0], primes[0]));
}
}
}
final ArrayList<float[]> dataSet2 = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
dataSet2.add(dataSet.get(i).clone());
}
return dataSet2;
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class AverageFilterTest method checkIsCorrect.
private static void checkIsCorrect(RandomSeed seed, DataFilter filter) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
for (final int width : primes) {
for (final int height : primes) {
for (final float boxSize : boxSizes) {
for (final boolean internal : checkInternal) {
averageIsCorrect(rg, width, height, boxSize, internal, filter);
if (filter.isInterpolated) {
averageIsCorrect(rg, width, height, boxSize - 0.3f, internal, filter);
averageIsCorrect(rg, width, height, boxSize - 0.6f, internal, filter);
}
}
}
}
}
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class NormaliserTest method nonNormaliserCanCopyToOutDataWithBorder.
@SeededTest
void nonNormaliserCanCopyToOutDataWithBorder(RandomSeed seed) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
for (final int width : primes) {
for (final int height : primes) {
final float[] data = createData(rg, width, height);
for (final int boxSize : boxSizes) {
// logger.fine(() -> String.format("%dx%d : border=%d", width, height, boxSize);
// Assume fixed normaliser works
final FixedNormaliser n = new FixedNormaliser(1);
final NonNormaliser nn = new NonNormaliser();
final float[] e = new float[data.length];
final float[] o = new float[data.length];
n.normalise(data, e, width, height, boxSize);
nn.normalise(data, o, width, height, boxSize);
Assertions.assertArrayEquals(o, e, () -> String.format("%dx%d : border=%d", width, height, boxSize));
}
}
}
}
use of org.apache.commons.rng.UniformRandomProvider 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);
}
}
Aggregations