Search in sources :

Example 31 with LocalList

use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.

the class MultivariateGaussianMixtureExpectationMaximizationTest method canCreateMixedMultivariateGaussianDistribution.

@SeededTest
void canCreateMixedMultivariateGaussianDistribution(RandomSeed seed) {
    // Will be normalised
    final double[] weights = { 1, 1 };
    final double[][] means = new double[2][];
    final double[][][] covariances = new double[2][][];
    final double[][] data1 = { { 1, 2 }, { 2.5, 1.5 }, { 3.5, 1.0 } };
    final double[][] data2 = { { 4, 2 }, { 3.5, -1.5 }, { -3.5, 1.0 } };
    means[0] = getColumnMeans(data1);
    covariances[0] = getCovariance(data1);
    means[1] = getColumnMeans(data2);
    covariances[1] = getCovariance(data2);
    // Create components. This does not have to be zero based.
    final LocalList<double[]> list = new LocalList<>();
    list.addAll(Arrays.asList(data1));
    list.addAll(Arrays.asList(data2));
    final double[][] data = list.toArray(new double[0][]);
    final int[] components = { -1, -1, -1, 3, 3, 3 };
    // Randomise the data
    for (int n = 0; n < 3; n++) {
        final long start = n + seed.getSeedAsLong();
        // This relies on the shuffle being the same
        RandomUtils.shuffle(data, RngUtils.create(start));
        RandomUtils.shuffle(components, RngUtils.create(start));
        final MixtureMultivariateGaussianDistribution dist = MultivariateGaussianMixtureExpectationMaximization.createMixed(data, components);
        Assertions.assertArrayEquals(new double[] { 0.5, 0.5 }, dist.getWeights());
        final MultivariateGaussianDistribution[] distributions = dist.getDistributions();
        Assertions.assertEquals(weights.length, distributions.length);
        final DoubleDoubleBiPredicate test = TestHelper.doublesAreClose(1e-8);
        for (int i = 0; i < means.length; i++) {
            TestAssertions.assertArrayTest(means[i], distributions[i].getMeans(), test);
            TestAssertions.assertArrayTest(covariances[i], distributions[i].getCovariances(), test);
        }
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) MixtureMultivariateGaussianDistribution(uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution) MultivariateGaussianDistribution(uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution.MultivariateGaussianDistribution) MixtureMultivariateGaussianDistribution(uk.ac.sussex.gdsc.smlm.math3.distribution.fitting.MultivariateGaussianMixtureExpectationMaximization.MixtureMultivariateGaussianDistribution) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 32 with LocalList

use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.

the class DynamicMultipleTargetTracingTest method testTrajectoryWithNoOnFrames.

@Test
void testTrajectoryWithNoOnFrames() {
    final PeakResult p1 = new PeakResult(0, 1, 2);
    final PeakResult p2 = new PeakResult(1, 2, 3);
    final PeakResult p3 = new PeakResult(2, 3, 4);
    final PeakResult p4 = new PeakResult(3, 4, 5);
    final Trajectory t = new Trajectory(42, p1);
    Assertions.assertEquals(42, t.getId());
    Assertions.assertSame(p1, t.getLast(-1));
    Assertions.assertEquals(1, t.size());
    Assertions.assertThrows(NullPointerException.class, () -> t.onSize());
    t.add(p2);
    t.add(p3);
    t.add(p4);
    Assertions.assertSame(p4, t.getLast(-1));
    Assertions.assertSame(p3, t.getLast(-2));
    Assertions.assertSame(p2, t.getLast(-3));
    Assertions.assertSame(p1, t.getLast(-4));
    Assertions.assertEquals(4, t.size());
    final LocalList<PeakResult> list = new LocalList<>();
    t.forLast(2, list::add);
    Assertions.assertEquals(2, list.size());
    Assertions.assertSame(p3, list.pop());
    Assertions.assertSame(p4, list.pop());
    final Trace trace = t.toTrace();
    Assertions.assertEquals(4, trace.size());
    Assertions.assertEquals(42, trace.getId());
    // Test setting the gap
    final int frame = t.getLast(-1).getFrame();
    for (int i = 1; i <= 3; i++) {
        t.reset(frame + i);
        Assertions.assertEquals(i - 1, t.gap);
    }
    // Test setting the local statistics
    Assertions.assertFalse(t.isLocalIntensity);
    t.setLocalDiffusion(10, 9);
    Assertions.assertEquals(10, t.r2);
    Assertions.assertFalse(t.isLocalDiffusion);
    t.setLocalDiffusion(9, 10);
    Assertions.assertEquals(9, t.r2);
    Assertions.assertTrue(t.isLocalDiffusion);
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Trajectory(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.Trajectory) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 33 with LocalList

use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterUsingForEach.

// Speed test forEach verses equivalent eval() function calls
@SpeedTag
@Test
void functionIsFasterUsingForEach() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
    final LocalList<double[]> params = new LocalList<>();
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double cz1 : testcz1) {
                        for (final double[] w1 : testw1) {
                            for (final double angle1 : testangle1) {
                                final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
                                params.add(a);
                            }
                        }
                    }
                }
            }
        }
    }
    final double[][] x = params.toArray(new double[0][]);
    final int runs = 10000 / x.length;
    final TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    ts.execute(new ForEachTimingTask(f1, x, 2));
    ts.execute(new ForEachTimingTask(f1, x, 1));
    ts.execute(new ForEachTimingTask(f1, x, 0));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 1; i <= 3; i++) {
        final TimingResult slow = ts.get(-i - 3);
        final TimingResult fast = ts.get(-i);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 34 with LocalList

use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.

the class PeakResultTableModel method createTableStructure.

/**
 * Called when the structure of the table should be created. If the structure has not changed or
 * no live table are attached then this does nothing.
 *
 * @param changed Set to true if a property controlling the structure has changed
 */
private void createTableStructure(boolean changed) {
    if (changed) {
        columnsComputed.set(false);
    }
    // so it can be turned off by PeakResultTableModelFrame.
    if (liveCount.get() == 0 || columnsComputed.get()) {
        return;
    }
    columnsComputed.set(true);
    rounder = RounderUtils.create(tableSettings.getRoundingPrecision());
    // Create the converters
    final PeakResultConversionHelper helper = new PeakResultConversionHelper(calibration, psf);
    helper.setIntensityUnit(tableSettings.getIntensityUnit());
    helper.setDistanceUnit(tableSettings.getDistanceUnit());
    helper.setAngleUnit(tableSettings.getAngleUnit());
    final Converter[] converters = helper.getConverters();
    final Converter ic = converters[PeakResult.INTENSITY];
    final String[] paramNames = helper.getNames();
    final String[] unitNames = helper.getUnitNames();
    // Calibration tableCalibration = (helper.isCalibrationChanged()) ? helper.getCalibration() :
    // calibration;
    // Organise the data columns.
    // This is done as per the IJTablePeakResults for consistency
    final LocalList<PeakResultData<?>> valuesList = new LocalList<>();
    final LocalList<String> namesList = new LocalList<>();
    rowCounter = tableSettings.getShowRowCounter();
    if (rowCounter) {
        valuesList.add(new PeakResultDataFrame());
        namesList.add("#");
    }
    valuesList.add(new PeakResultDataFrame());
    addName(valuesList, namesList);
    if (showEndFrame) {
        valuesList.add(new PeakResultDataEndFrame());
        addName(valuesList, namesList);
    }
    if (showId) {
        valuesList.add(new PeakResultDataId());
        addName(valuesList, namesList);
    }
    if (showCategory) {
        valuesList.add(new PeakResultDataCategory());
        addName(valuesList, namesList);
    }
    if (tableSettings.getShowFittingData()) {
        valuesList.add(new PeakResultDataOrigX());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataOrigY());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataOrigValue());
        addName(valuesList, namesList);
        valuesList.add(new PeakResultDataError());
        addName(valuesList, namesList);
    }
    if (tableSettings.getShowNoiseData()) {
        // Must be converted
        valuesList.add(new PeakResultDataFloat() {

            @Override
            public Float getValue(PeakResult result) {
                return ic.convert(result.getNoise());
            }
        });
        addName("Noise", namesList, unitNames[PeakResult.INTENSITY]);
        valuesList.add(new PeakResultDataFloat() {

            @Override
            public Float getValue(PeakResult result) {
                return ic.convert(result.getMeanIntensity());
            }
        });
        addName("Mean" + paramNames[PeakResult.INTENSITY], namesList, unitNames[PeakResult.INTENSITY]);
        valuesList.add(new PeakResultDataSnr());
        addName(valuesList, namesList);
    }
    int[] outIndices = SimpleArrayUtils.natural(converters.length);
    if (!showZ) {
        final TIntArrayList list = new TIntArrayList(outIndices);
        list.remove(PeakResult.Z);
        outIndices = list.toArray();
    }
    for (final int i : outIndices) {
        // Must be converted
        valuesList.add(new PeakResultDataParameterConverter(converters[i], i));
        addName(paramNames[i], namesList, unitNames[i]);
        if (showDeviations) {
            valuesList.add(new PeakResultDataParameterDeviationConverter(converters[i], i));
            namesList.add("+/-");
        }
    }
    if (tableSettings.getShowPrecision()) {
        PeakResultDataPrecision precision = null;
        try {
            final Gaussian2DPeakResultCalculator calculator = Gaussian2DPeakResultHelper.create(getPsf(), calibration, Gaussian2DPeakResultHelper.LSE_PRECISION);
            precision = new PeakResultDataPrecision() {

                @Override
                public Double getValue(PeakResult result) {
                    if (result.hasPrecision()) {
                        return result.getPrecision();
                    }
                    if (calculator != null) {
                        return calculator.getLsePrecision(result.getParameters(), result.getNoise());
                    }
                    return 0.0;
                }
            };
        } catch (final ConfigurationException | ConversionException ex) {
        // Ignore
        }
        if (precision == null) {
            precision = new PeakResultDataPrecision();
        }
        valuesList.add(precision);
        namesList.add("Precision (nm)");
    }
    values = valuesList.toArray(new PeakResultData<?>[0]);
    names = namesList.toArray(new String[0]);
    fireTableStructureChanged();
}
Also used : PeakResultDataId(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataId) PeakResultDataParameterConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterConverter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Gaussian2DPeakResultCalculator(uk.ac.sussex.gdsc.smlm.results.Gaussian2DPeakResultCalculator) PeakResultDataOrigValue(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigValue) ConfigurationException(uk.ac.sussex.gdsc.smlm.data.config.ConfigurationException) PeakResultDataParameterDeviationConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterDeviationConverter) PeakResultDataParameterConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterConverter) Converter(uk.ac.sussex.gdsc.core.data.utils.Converter) PeakResultDataPrecision(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataPrecision) PeakResultData(uk.ac.sussex.gdsc.smlm.results.PeakResultData) ConversionException(uk.ac.sussex.gdsc.core.data.utils.ConversionException) PeakResultDataFrame(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFrame) PeakResultDataFloat(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFloat) PeakResultDataSnr(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataSnr) PeakResultDataParameterDeviationConverter(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataParameterDeviationConverter) TIntArrayList(gnu.trove.list.array.TIntArrayList) PeakResultDataEndFrame(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataEndFrame) PeakResultDataOrigX(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigX) PeakResultDataError(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataError) PeakResultDataFloat(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataFloat) PeakResultDataOrigY(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataOrigY) PeakResultDataCategory(uk.ac.sussex.gdsc.smlm.results.data.PeakResultDataCategory) PeakResultConversionHelper(uk.ac.sussex.gdsc.smlm.results.PeakResultConversionHelper)

Example 35 with LocalList

use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.

the class Shape3DHelper method createDisc.

/**
 * Creates the disc. This is copied from MeshMaker but the duplication of the vertices for both
 * sides on the disc is removed.
 *
 * @param x the x
 * @param y the y
 * @param z the z
 * @param nx the nx
 * @param ny the ny
 * @param nz the nz
 * @param radius the radius
 * @param edgePoints the edge points
 * @return the list
 */
public static List<Point3f> createDisc(final double x, final double y, final double z, final double nx, final double ny, final double nz, final double radius, final int edgePoints) {
    double ax;
    double ay;
    double az;
    if (Math.abs(nx) >= Math.abs(ny)) {
        final double scale = 1 / Math.sqrt(nx * nx + nz * nz);
        ax = -nz * scale;
        ay = 0;
        az = nx * scale;
    } else {
        final double scale = 1 / Math.sqrt(ny * ny + nz * nz);
        ax = 0;
        ay = nz * scale;
        az = -ny * scale;
    }
    /*
     * Now to find the other vector in that plane, do the cross product of (ax,ay,az) with
     * (nx,ny,nz)
     */
    double bx = (ay * nz - az * ny);
    double by = (az * nx - ax * nz);
    double bz = (ax * ny - ay * nx);
    final double bScale = 1 / Math.sqrt(bx * bx + by * by + bz * bz);
    bx *= bScale;
    by *= bScale;
    bz *= bScale;
    final double[] circleX = new double[edgePoints + 1];
    final double[] circleY = new double[edgePoints + 1];
    final double[] circleZ = new double[edgePoints + 1];
    for (int i = 0; i < edgePoints + 1; ++i) {
        final double angle = (i * 2 * Math.PI) / edgePoints;
        final double c = Math.cos(angle);
        final double s = Math.sin(angle);
        circleX[i] = x + radius * c * ax + radius * s * bx;
        circleY[i] = y + radius * c * ay + radius * s * by;
        circleZ[i] = z + radius * c * az + radius * s * bz;
    }
    final LocalList<Point3f> list = new LocalList<>();
    final Point3f centre = new Point3f((float) x, (float) y, (float) z);
    for (int i = 0; i < edgePoints; ++i) {
        final Point3f t2 = new Point3f((float) circleX[i], (float) circleY[i], (float) circleZ[i]);
        final Point3f t3 = new Point3f((float) circleX[i + 1], (float) circleY[i + 1], (float) circleZ[i + 1]);
        list.add(centre);
        list.add(t2);
        list.add(t3);
    // We do not duplicate the triangle for both sides as we render the object as 2D
    // with setBackFaceNormalFlip(true)
    // list.add(centre);
    // list.add(t3);
    // list.add(t2);
    }
    return list;
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) Point3f(org.scijava.vecmath.Point3f)

Aggregations

LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)70 PeakResult (uk.ac.sussex.gdsc.smlm.results.PeakResult)19 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)15 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)14 ImagePlus (ij.ImagePlus)13 Point (java.awt.Point)13 Future (java.util.concurrent.Future)13 Plot (ij.gui.Plot)11 ExecutorService (java.util.concurrent.ExecutorService)11 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)11 TIntArrayList (gnu.trove.list.array.TIntArrayList)10 IJ (ij.IJ)10 PlugIn (ij.plugin.PlugIn)10 Rectangle (java.awt.Rectangle)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)10 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)10 Color (java.awt.Color)9 Arrays (java.util.Arrays)9 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)9