use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class SeriesImageSourceTest method canReadBigTiffNonSequentially.
private void canReadBigTiffNonSequentially(RandomSeed seed, boolean inMemory, boolean intelByteOrder) throws IOException {
final int n = 2;
final String[] filenames = createFilenames(n);
final ImageStack[] stacks = createSeries(filenames, intelByteOrder);
final SeriesImageSource source = new SeriesImageSource("Test", filenames);
if (!inMemory) {
// To force standard reading functionality
source.setBufferLimit(0);
}
source.setReadHint(ReadHint.NONSEQUENTIAL);
source.open();
Assertions.assertEquals(width, source.getWidth());
Assertions.assertEquals(height, source.getHeight());
Assertions.assertEquals(depth * n, source.getFrames());
final float[][] pixels = new float[n * depth][];
for (int i = 0, k = 0; i < stacks.length; i++) {
for (int j = 0; j < depth; j++) {
pixels[k++] = (float[]) stacks[i].getPixels(j + 1);
}
}
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
for (int i = 0; i < 3; i++) {
final int[] random = RandomUtils.sample(pixels.length / 2, pixels.length, r);
for (final int frame : random) {
// logger.fine(FunctionUtils.getSupplier("[%d] frame = %d", i, frame);
final float[] e = pixels[frame];
// 1-base index on the frame
final float[] o = source.get(frame + 1);
Assertions.assertArrayEquals(e, o);
}
}
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class FrcTest method canComputeMirrored.
@SeededTest
void canComputeMirrored(RandomSeed seed) {
// Sample lines through an image to create a structure.
final int size = 1024;
final double[][] data = new double[size * 2][];
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
}
// Create 2 images
final Rectangle bounds = new Rectangle(0, 0, size, size);
ImageJImagePeakResults i1 = createImage(bounds);
ImageJImagePeakResults i2 = createImage(bounds);
final int[] indices = SimpleArrayUtils.natural(data.length);
PermutationSampler.shuffle(r, indices);
for (final int i : indices) {
final ImageJImagePeakResults image = i1;
i1 = i2;
i2 = image;
image.add((float) data[i][0], (float) data[i][1], 1);
}
i1.end();
i2.end();
final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
// Test
final Frc frc = new Frc();
FloatProcessor[] fft1;
FloatProcessor[] fft2;
fft1 = frc.getComplexFft(ip1);
fft2 = frc.getComplexFft(ip2);
final float[] dataA1 = (float[]) fft1[0].getPixels();
final float[] dataB1 = (float[]) fft1[1].getPixels();
final float[] dataA2 = (float[]) fft2[0].getPixels();
final float[] dataB2 = (float[]) fft2[1].getPixels();
final float[] numeratorE = new float[dataA1.length];
final float[] absFft1E = new float[dataA1.length];
final float[] absFft2E = new float[dataA1.length];
Frc.compute(numeratorE, absFft1E, absFft2E, dataA1, dataB1, dataA2, dataB2);
Assertions.assertTrue(checkSymmetry(numeratorE, size), "numeratorE");
Assertions.assertTrue(checkSymmetry(absFft1E, size), "absFft1E");
Assertions.assertTrue(checkSymmetry(absFft2E, size), "absFft2E");
final float[] numeratorA = new float[dataA1.length];
final float[] absFft1A = new float[dataA1.length];
final float[] absFft2A = new float[dataA1.length];
Frc.computeMirrored(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
// for (int y=0, i=0; y<size; y++)
// for (int x=0; x<size; x++, i++)
// {
// logger.fine(FunctionUtils.getSupplier("[%d,%d = %d] %f ?= %f", x, y, i, numeratorE[i],
// numeratorA[i]);
// }
Assertions.assertArrayEquals(numeratorE, numeratorA, "numerator");
Assertions.assertArrayEquals(absFft1E, absFft1A, "absFft1");
Assertions.assertArrayEquals(absFft2E, absFft2A, "absFft2");
Frc.computeMirroredFast(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
// Check this.
for (int y = 1; y < size; y++) {
for (int x = 1, i = y * size + 1; x < size; x++, i++) {
Assertions.assertEquals(numeratorE[i], numeratorA[i], "numerator");
Assertions.assertEquals(absFft1E[i], absFft1A[i], "absFft1");
Assertions.assertEquals(absFft2E[i], absFft2A[i], "absFft2");
}
}
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class FrcTest method computeMirroredIsFaster.
@SeededTest
void computeMirroredIsFaster(RandomSeed seed) {
Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
// Sample lines through an image to create a structure.
final int N = 2048;
final double[][] data = new double[N * 2][];
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
for (int x = 0, y = 0, y2 = N, i = 0; x < N; x++, y++, y2--) {
data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
}
// Create 2 images
final Rectangle bounds = new Rectangle(0, 0, N, N);
ImageJImagePeakResults i1 = createImage(bounds);
ImageJImagePeakResults i2 = createImage(bounds);
final int[] indices = SimpleArrayUtils.natural(data.length);
PermutationSampler.shuffle(r, indices);
for (final int i : indices) {
final ImageJImagePeakResults image = i1;
i1 = i2;
i2 = image;
image.add((float) data[i][0], (float) data[i][1], 1);
}
i1.end();
i2.end();
final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
// Test
final Frc frc = new Frc();
FloatProcessor[] fft1;
FloatProcessor[] fft2;
fft1 = frc.getComplexFft(ip1);
fft2 = frc.getComplexFft(ip2);
final float[] dataA1 = (float[]) fft1[0].getPixels();
final float[] dataB1 = (float[]) fft1[1].getPixels();
final float[] dataA2 = (float[]) fft2[0].getPixels();
final float[] dataB2 = (float[]) fft2[1].getPixels();
final float[] numerator = new float[dataA1.length];
final float[] absFft1 = new float[dataA1.length];
final float[] absFft2 = new float[dataA1.length];
final TimingService ts = new TimingService(10);
ts.execute(new MyTimingTask("compute") {
@Override
public Object run(Object data) {
Frc.compute(numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
ts.execute(new MyTimingTask("computeMirrored") {
@Override
public Object run(Object data) {
Frc.computeMirrored(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
ts.execute(new MyTimingTask("computeMirroredFast") {
@Override
public Object run(Object data) {
Frc.computeMirroredFast(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
return null;
}
});
final int size = ts.getSize();
ts.repeat(size);
if (logger.isLoggable(Level.INFO)) {
logger.info(ts.getReport(size));
}
// The 'Fast' method may not always be faster so just log results
final TimingResult slow = ts.get(-3);
final TimingResult fast = ts.get(-2);
final TimingResult fastest = ts.get(-1);
logger.log(TestLogUtils.getTimingRecord(slow, fastest));
logger.log(TestLogUtils.getTimingRecord(fast, fastest));
// It should be faster than the non mirrored version
Assertions.assertTrue(ts.get(-1).getMean() <= ts.get(-3).getMean());
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class ImageConverterTest method canGetCropData.
@SeededTest
void canGetCropData(RandomSeed seed) {
final ImageConverterTestData data = (ImageConverterTestData) dataCache.computeIfAbsent(seed, ImageConverterTest::createData);
final byte[] bdata = data.bdata;
final short[] sdata = data.sdata;
final float[] fdata = data.fdata;
final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
final ImageExtractor ie = ImageExtractor.wrap(fdata, w, h);
for (int i = 0; i < 10; i++) {
final Rectangle bounds = ie.getBoxRegionBounds(10 + rand.nextInt(w - 20), 10 + rand.nextInt(h - 20), 5 + rand.nextInt(5));
final FloatProcessor ip = new FloatProcessor(w, h, fdata.clone());
ip.setRoi(bounds);
final float[] fe = (float[]) (ip.crop().getPixels());
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(bdata, w, h, bounds, null));
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(sdata, w, h, bounds, null));
Assertions.assertArrayEquals(fe, ImageJImageConverter.getData(fdata, w, h, bounds, null));
// Check the double format
final double[] de = SimpleArrayUtils.toDouble(fe);
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(bdata, w, h, bounds, null));
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(sdata, w, h, bounds, null));
Assertions.assertArrayEquals(de, ImageJImageConverter.getDoubleData(fdata, w, h, bounds, null));
}
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class ImageJImagePeakResultsTest method checkCanAddUsingDifferentMethods.
private void checkCanAddUsingDifferentMethods(RandomSeed seed, int displayFlags) {
final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
displayFlags |= ImageJImagePeakResults.DISPLAY_SIGNAL;
final ImageJImagePeakResults[] r = new ImageJImagePeakResults[8];
final PSF psf = PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D);
for (int i = 0; i < r.length; i++) {
r[i] = new ImageJImagePeakResults(title + i, bounds, 1);
r[i].setDisplayFlags(displayFlags);
r[i].setPsf(psf);
begin(r[i]);
}
final int size = 20;
final int[] t = new int[size];
final float[] x = new float[size];
final float[] y = new float[size];
final float[] v = new float[size];
for (int i = 0; i < size; i++) {
t[i] = i;
x[i] = (rand.nextFloat() * bounds.width);
y[i] = (rand.nextFloat() * bounds.height);
v[i] = (rand.nextFloat());
addPeakResult(r[0], x[i], y[i], v[i]);
addPeakResult(r[1], t[i], x[i], y[i], v[i]);
addValue(r[2], x[i], y[i], v[i]);
addValue(r[3], t[i], x[i], y[i], v[i]);
}
addPeakResults(r[4], x, y, v);
addPeakResults(r[5], t, x, y, v);
addValues(r[6], x, y, v);
addValues(r[7], t, x, y, v);
final float[][] image = new float[r.length][];
for (int i = 0; i < r.length; i++) {
r[i].end();
image[i] = getImage(r[i]);
logger.log(TestLogUtils.getRecord(Level.FINE, "[%d] = %s", i, Arrays.toString(image[i])));
}
// Test single value adds
float[] expecteds = image[0];
IntArrayFormatSupplier msg = new IntArrayFormatSupplier("Single add image %d", 1);
for (int i = 1; i < 4; i++) {
final float[] actuals = image[i];
Assertions.assertArrayEquals(expecteds, actuals, msg.set(0, i));
}
// Test multi value adds
expecteds = image[4];
msg = new IntArrayFormatSupplier("Multi add image %d", 1);
for (int i = 5; i < image.length; i++) {
final float[] actuals = image[i];
Assertions.assertArrayEquals(expecteds, actuals, msg.set(0, i));
}
// Test they are roughly the same (differences occur due to floating point summation
Assertions.assertArrayEquals(expecteds, image[0], 1e-5f, "Single != Multi");
}
Aggregations