use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration in project GDSC-SMLM by aherbert.
the class CalibrationReaderTest method canGetIntensityConverter.
@Test
void canGetIntensityConverter() {
final Calibration.Builder builder = Calibration.newBuilder();
final IntensityCalibration.Builder intensityBuilder = builder.getIntensityCalibrationBuilder();
intensityBuilder.setIntensityUnit(IntensityUnit.PHOTON);
intensityBuilder.setCountPerPhoton(gain);
final Calibration c = builder.build();
final CalibrationReader reader = new CalibrationReader(c);
final TypeConverter<IntensityUnit> intensityConverter = reader.getIntensityConverter(IntensityUnit.COUNT);
Assertions.assertEquals(intensityConverter.from(), IntensityUnit.PHOTON);
Assertions.assertEquals(intensityConverter.to(), IntensityUnit.COUNT);
final TypeConverter<IntensityUnit> intensityConverter2 = CalibrationHelper.getIntensityConverter(c, IntensityUnit.COUNT);
Assertions.assertEquals(intensityConverter2.from(), IntensityUnit.PHOTON);
Assertions.assertEquals(intensityConverter2.to(), IntensityUnit.COUNT);
Assertions.assertEquals(intensityConverter.getFunction(), intensityConverter2.getFunction());
}
use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method checkEqual.
private static void checkEqual(ResultsFileFormat fileFormat, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, boolean sort, MemoryPeakResults expectedResults, MemoryPeakResults actualResults) {
Assertions.assertNotNull(actualResults, "Input results are null");
Assertions.assertEquals(expectedResults.size(), actualResults.size(), "Size differ");
final PeakResult[] expected = expectedResults.toArray();
final PeakResult[] actual = actualResults.toArray();
if (sort) {
// Results should be sorted by time
Arrays.sort(expected, (o1, o2) -> o1.getFrame() - o2.getFrame());
}
// TSF requires the bias be subtracted
// double bias = expectedResults.getCalibration().getBias();
final DoubleDoubleBiPredicate deltaD = TestHelper.doublesIsCloseTo(1e-5, 0);
final FloatFloatBiPredicate deltaF = TestHelper.floatsIsCloseTo(1e-5, 0);
for (int i = 0; i < actualResults.size(); i++) {
final PeakResult p1 = expected[i];
final PeakResult p2 = actual[i];
final ObjectArrayFormatSupplier msg = new ObjectArrayFormatSupplier("%s @ [" + i + "]", 1);
Assertions.assertEquals(p1.getFrame(), p2.getFrame(), msg.set(0, "Peak"));
if (fileFormat == ResultsFileFormat.MALK) {
TestAssertions.assertTest(p1.getXPosition(), p2.getXPosition(), deltaF, msg.set(0, "X"));
TestAssertions.assertTest(p1.getYPosition(), p2.getYPosition(), deltaF, msg.set(0, "Y"));
TestAssertions.assertTest(p1.getIntensity(), p2.getIntensity(), deltaF, msg.set(0, "Intensity"));
continue;
}
Assertions.assertEquals(p1.getOrigX(), p2.getOrigX(), msg.set(0, "Orig X"));
Assertions.assertEquals(p1.getOrigY(), p2.getOrigY(), msg.set(0, "Orig Y"));
Assertions.assertNotNull(p2.getParameters(), msg.set(0, "Params is null"));
if (showEndFrame) {
Assertions.assertEquals(p1.getEndFrame(), p2.getEndFrame(), msg.set(0, "End frame"));
}
if (showId) {
Assertions.assertEquals(p1.getId(), p2.getId(), msg.set(0, "ID"));
}
if (showDeviations) {
Assertions.assertNotNull(p2.getParameterDeviations(), msg.set(0, "Deviations"));
}
if (showCategory) {
Assertions.assertEquals(p1.getCategory(), p2.getCategory(), msg.set(0, "Category"));
}
// Binary should be exact for float numbers
if (fileFormat == ResultsFileFormat.BINARY) {
Assertions.assertEquals(p1.getOrigValue(), p2.getOrigValue(), msg.set(0, "Orig value"));
Assertions.assertEquals(p1.getError(), p2.getError(), msg.set(0, "Error"));
Assertions.assertEquals(p1.getNoise(), p2.getNoise(), msg.set(0, "Noise"));
Assertions.assertEquals(p1.getMeanIntensity(), p2.getMeanIntensity(), msg.set(0, "Mean intensity"));
Assertions.assertArrayEquals(p1.getParameters(), p2.getParameters(), msg.set(0, "Params"));
if (showDeviations) {
Assertions.assertArrayEquals(p1.getParameterDeviations(), p2.getParameterDeviations(), msg.set(0, "Params StdDev"));
}
if (showPrecision) {
Assertions.assertEquals(p1.getPrecision(), p2.getPrecision(), msg.set(0, "Precision"));
}
continue;
}
// Otherwise have an error
TestAssertions.assertTest(p1.getOrigValue(), p2.getOrigValue(), deltaF, msg.set(0, "Orig value"));
TestAssertions.assertTest(p1.getError(), p2.getError(), deltaD, msg.set(0, "Error"));
TestAssertions.assertTest(p1.getNoise(), p2.getNoise(), deltaF, msg.set(0, "Noise"));
TestAssertions.assertTest(p1.getMeanIntensity(), p2.getMeanIntensity(), deltaF, msg.set(0, "Mean intensity"));
TestAssertions.assertArrayTest(p1.getParameters(), p2.getParameters(), deltaF, msg.set(0, "Params"));
if (showDeviations) {
TestAssertions.assertArrayTest(p1.getParameterDeviations(), p2.getParameterDeviations(), deltaF, msg.set(0, "Params StdDev"));
}
if (showPrecision) {
// Handle NaN precisions
final double pa = p1.getPrecision();
final double pb = p2.getPrecision();
if (!Double.isNaN(pa) || !Double.isNaN(pb)) {
TestAssertions.assertTest(p1.getPrecision(), p2.getPrecision(), deltaD, msg.set(0, "Precision"));
}
}
}
// Check the header information
Assertions.assertEquals(expectedResults.getName(), actualResults.getName(), "Name");
Assertions.assertEquals(expectedResults.getConfiguration(), actualResults.getConfiguration(), "Configuration");
final Rectangle r1 = expectedResults.getBounds();
final Rectangle r2 = actualResults.getBounds();
if (r1 != null) {
Assertions.assertNotNull(r2, "Bounds");
Assertions.assertEquals(r1.x, r2.x, "Bounds x");
Assertions.assertEquals(r1.y, r2.y, "Bounds y");
Assertions.assertEquals(r1.width, r2.width, "Bounds width");
Assertions.assertEquals(r1.height, r2.height, "Bounds height");
} else {
Assertions.assertNull(r2, "Bounds");
}
final Calibration c1 = expectedResults.getCalibration();
final Calibration c2 = actualResults.getCalibration();
if (c1 != null) {
Assertions.assertNotNull(c2, "Calibration");
// Be lenient and allow no TimeUnit to match TimeUnit.FRAME
boolean ok = c1.equals(c2);
if (!ok && new CalibrationReader(c1).getTimeUnitValue() == TimeUnit.TIME_UNIT_NA_VALUE) {
switch(fileFormat) {
case BINARY:
case MALK:
case TEXT:
case TSF:
final CalibrationWriter writer = new CalibrationWriter(c1);
writer.setTimeUnit(TimeUnit.FRAME);
ok = writer.getCalibration().equals(c2);
break;
default:
// Do not assume frames for other file formats
break;
}
}
Assertions.assertTrue(ok, "Calibration");
} else {
Assertions.assertNull(c2, "Calibration");
}
final PSF p1 = expectedResults.getPsf();
final PSF p2 = actualResults.getPsf();
if (p1 != null) {
Assertions.assertNotNull(p2, "PSF");
Assertions.assertTrue(p1.equals(p2), "PSF");
} else {
Assertions.assertNull(p2, "PSF");
}
}
Aggregations