Search in sources :

Example 21 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class DynamicMultipleTargetTracingTest method testTraceMoleculesDisableIntensityModel.

/**
 * Test trace molecules using 2 molecules. One is fixed and the other moves past it. The tracing
 * should assign the fixed molecule correctly as it has a low local diffusion rate.
 */
@Test
void testTraceMoleculesDisableIntensityModel() {
    final UniformRandomProvider rng = RngUtils.create(125631236L);
    final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
    // localisation precision (in pixels)
    final double s = 0.1;
    final SharedStateContinuousSampler intensity = SamplerUtils.createGaussianSampler(rng, 1000, 100);
    final MemoryPeakResults results = new MemoryPeakResults(100);
    final CalibrationWriter writer = results.getCalibrationWriterSafe();
    // 0.1 um pixels, 1 second exposure time
    writer.setDistanceUnit(DistanceUnit.PIXEL);
    writer.setNmPerPixel(100);
    writer.setExposureTime(1000);
    results.setCalibration(writer.getCalibration());
    // First molecule diffuses roughly across the field from top-left to bottom-right.
    // 5 frames is the default for local stats, 15 frames for trajectory removal.
    // Use 20 so we build local stats and can expire a trajectory.
    final int size = 20;
    final float x1 = size / 2 + 0.5f;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x1 + gauss.sample() * s), (float) (i + gauss.sample() * s), (float) (intensity.sample())));
    }
    // Second molecule is fixed in the centre with a same intensity
    final int x = size / 2;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x + gauss.sample() * s), (float) (x + gauss.sample() * s), (float) (intensity.sample())));
    }
    // Add a single molecule that will not connect to anything in the second frame.
    // This should create a trajectory that will expire.
    results.add(new PeakResult(1, x1, size, (float) (intensity.sample())));
    // Move centre to centre each jump => 0.1 um or 0.01 um^2
    // MSD = 4D => D = 0.01 / 4 = 0.0025
    final DmttConfiguration config = DmttConfiguration.newBuilder(0.0025).setDisableIntensityModel(true).setTemporalWindow(10).build();
    final List<Trace> traces = new DynamicMultipleTargetTracing(results).traceMolecules(config);
    // Should have 3 traces
    Assertions.assertEquals(3, traces.size());
    // Assert ids start from 1
    for (int i = 0; i < traces.size(); i++) {
        Assertions.assertEquals(i + 1, traces.get(i).getId());
    }
    // Traces should be 2 full length and 1 single peak
    Assertions.assertEquals(size, traces.get(0).size());
    Assertions.assertEquals(size, traces.get(1).size());
    Assertions.assertEquals(1, traces.get(2).size());
    // Do an analysis on the actual tracks.
    // One should be based in the centre and the other should have parts close to position (i,i)
    // for each frame i.
    final PeakResult[] peaks = results.toArray();
    // Assume traces are initially created using the input order of the results.
    final Trace t1 = traces.get(0);
    final Trace t2 = traces.get(1);
    for (int i = 0; i < size; i++) {
        Assertions.assertSame(peaks[i], t1.get(i));
        Assertions.assertSame(peaks[i + size], t2.get(i));
    }
}
Also used : SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 22 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class PeakResultsReaderTest method canReadAndSimplifyGaussian2DPsf.

private static void canReadAndSimplifyGaussian2DPsf(RandomSeed seed, ResultsFileFormat fileFormat) {
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final MemoryPeakResults out = createResults(rg, 1, false, false, false, false, false);
    final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
    cal.setDistanceUnit(MemoryPeakResults.PREFERRED_DISTANCE_UNIT);
    cal.setIntensityUnit(MemoryPeakResults.PREFERRED_INTENSITY_UNIT);
    cal.setAngleUnit(MemoryPeakResults.PREFERRED_ANGLE_UNIT);
    if (fileFormat == ResultsFileFormat.TSF) {
        // For now just support using the native float TSF value
        cal.setNmPerPixel((float) cal.getNmPerPixel());
    }
    out.setCalibration(cal.getCalibration());
    // Remove angle
    final int ia = PsfHelper.getGaussian2DAngleIndex(out.getPsf());
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.getParameters()[ia] = 0;
        }
    });
    final String filename = createFile();
    writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
    MemoryPeakResults in = readFile(filename, false, false);
    // Change to two-axis PSF
    out.setPsf(PsfHelper.create(PSFType.TWO_AXIS_GAUSSIAN_2D));
    final int twoAxisLength = PsfHelper.getParameterCount(out.getPsf()) + PeakResult.STANDARD_PARAMETERS;
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.resizeParameters(twoAxisLength);
        }
    });
    checkEqual(fileFormat, false, false, false, false, false, false, out, in);
    // Remove sy
    final int[] indices = PsfHelper.getGaussian2DWxWyIndices(out.getPsf());
    final int isx = indices[0];
    final int isy = indices[1];
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            final float[] p = peakResult.getParameters();
            p[isy] = p[isx];
        }
    });
    writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
    in = readFile(filename, false, false);
    // Change to one-axis PSF
    out.setPsf(PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D));
    final int oneAxisLength = PsfHelper.getParameterCount(out.getPsf()) + PeakResult.STANDARD_PARAMETERS;
    out.forEach(new PeakResultProcedure() {

        @Override
        public void execute(PeakResult peakResult) {
            peakResult.resizeParameters(oneAxisLength);
        }
    });
    checkEqual(fileFormat, false, false, false, false, false, false, out, in);
}
Also used : PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Example 23 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class DynamicMultipleTargetTracingTest method checkConstructorThrows.

@Test
void checkConstructorThrows() {
    Assertions.assertThrows(NullPointerException.class, () -> new DynamicMultipleTargetTracing(null), "null results");
    final MemoryPeakResults results = new MemoryPeakResults();
    Assertions.assertThrows(IllegalArgumentException.class, () -> new DynamicMultipleTargetTracing(results), "Empty results");
    int frame = 0;
    final float x = 1;
    final float y = 2;
    final float intensity = 3;
    results.add(new PeakResult(frame++, x, y, intensity));
    Assertions.assertThrows(ConfigurationException.class, () -> new DynamicMultipleTargetTracing(results), "No calibration");
    final CalibrationWriter writer = results.getCalibrationWriterSafe();
    results.setCalibration(writer.getCalibration());
    Assertions.assertThrows(ConversionException.class, () -> new DynamicMultipleTargetTracing(results), "No distance calibration");
    writer.setDistanceUnit(DistanceUnit.PIXEL);
    writer.setNmPerPixel(100);
    results.setCalibration(writer.getCalibration());
    Assertions.assertThrows(IllegalArgumentException.class, () -> new DynamicMultipleTargetTracing(results), "No time calibration");
    writer.setExposureTime(50);
    results.setCalibration(writer.getCalibration());
    // Should be OK
    new DynamicMultipleTargetTracing(results).traceMolecules(DmttConfiguration.newBuilder(0.2).build());
}
Also used : CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 24 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class DynamicMultipleTargetTracingTest method testTraceMoleculesDisableLocalDiffusionModel.

/**
 * Test trace molecules using 2 molecules. One is fixed and the other moves past it. The tracing
 * should assign the fixed molecule correctly as it has a low local diffusion rate.
 */
@Test
void testTraceMoleculesDisableLocalDiffusionModel() {
    // The test is not very robust and fails 20% of the time. A fixed seed corrects this.
    final UniformRandomProvider rng = RngUtils.create(0x12345L);
    final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
    // localisation precision (in pixels)
    final double s = 0.1;
    final SharedStateContinuousSampler intensity1 = SamplerUtils.createGaussianSampler(rng, 1000, 100);
    final SharedStateContinuousSampler intensity2 = SamplerUtils.createGaussianSampler(rng, 500, 50);
    final MemoryPeakResults results = new MemoryPeakResults(100);
    final CalibrationWriter writer = results.getCalibrationWriterSafe();
    // 0.1 um pixels, 1 second exposure time
    writer.setDistanceUnit(DistanceUnit.PIXEL);
    writer.setNmPerPixel(100);
    writer.setExposureTime(1000);
    results.setCalibration(writer.getCalibration());
    // First molecule diffuses roughly across the field from top-left to bottom-right.
    // 5 frames is the default for local stats, 15 frames for trajectory removal.
    // Use 20 so we build local stats and can expire a trajectory.
    final int size = 20;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (i + gauss.sample() * s), (float) (i + gauss.sample() * s), (float) (intensity1.sample())));
    }
    // Second molecule is fixed in the centre with a lower intensity (allow
    // correct matching when tracks overlap)
    final int x = size / 2;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x + gauss.sample() * s), (float) (x + gauss.sample() * s), (float) (intensity2.sample())));
    }
    // Add a single molecule that will not connect to anything in the second frame.
    // This should create a trajectory that will expire.
    results.add(new PeakResult(1, size, size, (float) (intensity1.sample())));
    // Move centre to centre each jump => sqrt(2 * 0.1^2) = 0.141 um or 0.02 um^2
    // MSD = 4D => D = 0.02 / 4 = 0.005
    final DmttConfiguration config = DmttConfiguration.newBuilder(0.005).setDisableLocalDiffusionModel(true).setTemporalWindow(10).build();
    final List<Trace> traces = new DynamicMultipleTargetTracing(results).traceMolecules(config);
    // Should have 3 traces
    Assertions.assertEquals(3, traces.size());
    // Assert ids start from 1
    for (int i = 0; i < traces.size(); i++) {
        Assertions.assertEquals(i + 1, traces.get(i).getId());
    }
    // Traces should be 2 full length and 1 single peak
    Assertions.assertEquals(size, traces.get(0).size());
    Assertions.assertEquals(size, traces.get(1).size());
    Assertions.assertEquals(1, traces.get(2).size());
    // Do an analysis on the actual tracks.
    // One should be based in the centre and the other should have parts close to position (i,i)
    // for each frame i.
    final PeakResult[] peaks = results.toArray();
    // Assume traces are initially created using the input order of the results.
    final Trace t1 = traces.get(0);
    final Trace t2 = traces.get(1);
    for (int i = 0; i < size; i++) {
        Assertions.assertSame(peaks[i], t1.get(i));
        Assertions.assertSame(peaks[i + size], t2.get(i));
    }
}
Also used : SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 25 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class DynamicMultipleTargetTracingTest method testTraceMolecules.

/**
 * Test trace molecules using 2 molecules. One is fixed and the other moves across it. The tracing
 * should assign the fixed molecule correctly as it has a low local diffusion rate and different
 * intensity.
 */
@Test
void testTraceMolecules() {
    // The test is not very robust and fails 10% of the time. A fixed seed corrects this.
    final UniformRandomProvider rng = RngUtils.create(0x12345L);
    final NormalizedGaussianSampler gauss = SamplerUtils.createNormalizedGaussianSampler(rng);
    // localisation precision (in pixels)
    final double s = 0.1;
    final SharedStateContinuousSampler intensity1 = SamplerUtils.createGaussianSampler(rng, 1000, 100);
    final SharedStateContinuousSampler intensity2 = SamplerUtils.createGaussianSampler(rng, 500, 50);
    final MemoryPeakResults results = new MemoryPeakResults(100);
    final CalibrationWriter writer = results.getCalibrationWriterSafe();
    // 0.1 um pixels, 1 second exposure time
    writer.setDistanceUnit(DistanceUnit.PIXEL);
    writer.setNmPerPixel(100);
    writer.setExposureTime(1000);
    results.setCalibration(writer.getCalibration());
    // First molecule diffuses roughly across the field from top-left to bottom-right.
    // 5 frames is the default for local stats, 15 frames for trajectory removal.
    // Use 20 so we build local stats and can expire a trajectory.
    final int size = 20;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (i + gauss.sample() * s), (float) (i + gauss.sample() * s), (float) (intensity1.sample())));
    }
    // Second molecule is fixed in the centre with a lower intensity (allow
    // correct matching when tracks overlap)
    final int x = size / 2;
    for (int i = 0; i < size; i++) {
        results.add(new PeakResult(i, (float) (x + gauss.sample() * s), (float) (x + gauss.sample() * s), (float) (intensity2.sample())));
    }
    // Add a single molecule that will not connect to anything in the second frame.
    // This should create a trajectory that will expire.
    results.add(new PeakResult(1, size, size, (float) (intensity1.sample())));
    // 1 diffuses top-left to bottom-right.
    // 2 is fixed in the centre.
    // 3 is in the bottom-right for 1 frame.
    // 
    // 1
    // 1
    // 1
    // 12
    // 1
    // 1
    // 13
    // 
    // Molecule 3 can sometimes connect to the long lifetime molecules once they have been alive
    // long enough to create a local probability model. The default lifetime is 5 frames.
    // Setting this to 10 frames allows a better local model to be created.
    // Move centre to centre each jump => sqrt(2 * 0.1^2) = 0.141 um or 0.02 um^2
    // MSD = 4D => D = 0.02 / 4 = 0.005
    final DmttConfiguration config = DmttConfiguration.newBuilder(0.005).setTemporalWindow(10).build();
    final List<Trace> traces = new DynamicMultipleTargetTracing(results).traceMolecules(config);
    // Should have 3 traces
    Assertions.assertEquals(3, traces.size());
    // Assert ids start from 1
    for (int i = 0; i < traces.size(); i++) {
        Assertions.assertEquals(i + 1, traces.get(i).getId());
    }
    // Traces should be 2 full length and 1 single peak
    Assertions.assertEquals(size, traces.get(0).size());
    Assertions.assertEquals(size, traces.get(1).size());
    Assertions.assertEquals(1, traces.get(2).size());
    // Do an analysis on the actual tracks.
    // One should be based in the centre and the other should have parts close to position (i,i)
    // for each frame i.
    final PeakResult[] peaks = results.toArray();
    // Assume traces are initially created using the input order of the results.
    final Trace t1 = traces.get(0);
    final Trace t2 = traces.get(1);
    for (int i = 0; i < size; i++) {
        Assertions.assertSame(peaks[i], t1.get(i));
        Assertions.assertSame(peaks[i + size], t2.get(i));
    }
}
Also used : SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) DmttConfiguration(uk.ac.sussex.gdsc.smlm.results.DynamicMultipleTargetTracing.DmttConfiguration) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Aggregations

CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)45 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)18 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)8 Rectangle (java.awt.Rectangle)7 Calibration (uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration)7 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)7 Test (org.junit.jupiter.api.Test)5 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)5 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)5 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 Checkbox (java.awt.Checkbox)3 TextField (java.awt.TextField)3 IOException (java.io.IOException)3 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)3 IntensityUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit)3 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)3 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)3 ImagePlus (ij.ImagePlus)2 Calibration (ij.measure.Calibration)2