use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class PeakResultStoreTest method canStoreResults.
@SuppressWarnings("null")
private static void canStoreResults(RandomSeed seed, PeakResultStore store) {
final boolean isList = store instanceof PeakResultStoreList;
final PeakResultStoreList storeList = (isList) ? (PeakResultStoreList) store : null;
PeakResult result;
final UniformRandomProvider r = RngUtils.create(seed.getSeed());
PeakResult[] list = new PeakResult[20];
int size = 0;
Assertions.assertEquals(size, store.size(), "Not empty on construction");
Assertions.assertEquals(size, store.toArray().length, "Not empty list");
// Can store data in order
for (int i = 0; i < 10; i++) {
result = create(r);
list[size++] = result;
store.add(result);
}
assertEquals(list, size, store);
// Can sort
if (isList) {
Arrays.sort(list, 0, size, FrameIdPeakResultComparator.INSTANCE);
storeList.sort();
for (int i = 0; i < size; i++) {
Assertions.assertTrue(list[i] == storeList.get(i), "List entry not same reference");
}
final Comparator<PeakResult> c = new Comparator<PeakResult>() {
@Override
public int compare(PeakResult o1, PeakResult o2) {
return o1.getOrigX() - o2.getOrigX();
}
};
Arrays.sort(list, 0, size, c);
storeList.sort(c);
for (int i = 0; i < size; i++) {
Assertions.assertTrue(list[i] == storeList.get(i), "List entry not same reference after sort");
}
}
// Can trim to size
store.trimToSize();
assertEquals(list, size, store);
// Can remove null results
store.add(null);
result = create(r);
list[size++] = result;
store.add(result);
store.add(null);
for (int i = 0; i < 3; i++) {
result = create(r);
list[size++] = result;
store.add(result);
}
Assertions.assertTrue(size != store.size(), "Same size after adding null results");
store.removeIf(Objects::isNull);
assertEquals(list, size, store);
// Can clear
size = 0;
store.clear();
assertEquals(list, size, store);
// Can add collection
for (int i = 0; i < 10; i++) {
result = create(r);
list[size++] = result;
}
store.addCollection(Arrays.asList(Arrays.copyOf(list, size)));
assertEquals(list, size, store);
// Can add array
size = 0;
store.clear();
for (int i = 0; i < 10; i++) {
result = create(r);
list[size++] = result;
}
store.addArray(Arrays.copyOf(list, size));
assertEquals(list, size, store);
// Can add store
size = 0;
store.clear();
for (int i = 0; i < 10; i++) {
result = create(r);
list[size++] = result;
}
final PeakResultStore store2 = store.copy();
store2.addArray(Arrays.copyOf(list, size));
// Ensure the store is not at full capacity
store2.remove(list[--size]);
store.addStore(store2);
assertEquals(list, size, store);
// Can copy
final PeakResultStore copy = store.copy();
// Assertions.assertNotEquals(copy, store);
Assertions.assertTrue(copy != store, "Copy is the same reference");
assertEquals(list, size, store);
// Can remove single
for (int j = 0; j < 5; j++) {
size = 0;
store.clear();
PeakResult toRemove = null;
for (int i = 0; i < 5; i++) {
result = create(r);
if (j != i) {
list[size++] = result;
} else {
toRemove = result;
}
store.add(result);
}
Assertions.assertTrue(size != store.size(), "Same size after adding extra single result");
store.remove(toRemove);
assertEquals(list, size, store);
}
// Can remove collection
for (int j = 0; j < 5; j++) {
size = 0;
store.clear();
for (int i = 0; i < 20; i++) {
result = create(r);
list[size++] = result;
store.add(result);
}
final int[] indices = RandomUtils.sample(3, size, r);
Arrays.sort(indices);
final PeakResult[] list1 = new PeakResult[size - indices.length];
final PeakResult[] toRemove = new PeakResult[indices.length];
for (int i = 0; i < indices.length; i++) {
toRemove[i] = list[indices[i]];
}
for (int i = 0, k = 0; i < size; i++) {
if (Arrays.binarySearch(indices, i) < 0) {
list1[k++] = list[i];
}
}
store.removeCollection(Arrays.asList(toRemove));
assertEquals(list1, list1.length, store);
}
// Can remove range
if (!isList) {
return;
}
size = 0;
store.clear();
for (int i = 0; i < 20; i++) {
result = create(r);
list[size++] = result;
store.add(result);
}
// From the end
storeList.remove(size - 1, size - 1);
assertEquals(list, --size, store);
storeList.remove(size - 2, size - 1);
size -= 2;
assertEquals(list, size, store);
// From the start
storeList.remove(0, 0);
list = Arrays.copyOfRange(list, 1, size);
size = list.length;
assertEquals(list, size, store);
storeList.remove(0, 1);
list = Arrays.copyOfRange(list, 2, size);
size = list.length;
assertEquals(list, size, store);
// From the middle
storeList.remove(3, 3);
System.arraycopy(list, 4, list, 3, size - 4);
size--;
assertEquals(list, size, store);
storeList.remove(3, 4);
System.arraycopy(list, 5, list, 3, size - 5);
size -= 2;
assertEquals(list, size, store);
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method canReadIntoPreferredUnits.
private static void canReadIntoPreferredUnits(RandomSeed seed, ResultsFileFormat fileFormat) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, false, false, false, false, false);
// Output in nm and count
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.NM);
cal.setIntensityUnit(IntensityUnit.COUNT);
if (fileFormat == ResultsFileFormat.TSF) {
// For now just support using the native float TSF value
cal.setNmPerPixel((float) cal.getNmPerPixel());
}
out.setCalibration(cal.getCalibration());
final String filename = createFile();
writeFile(false, fileFormat, false, false, false, false, false, false, out, filename);
final MemoryPeakResults in = readFile(filename, false, false);
// Change to preferred units
out.convertToUnits(MemoryPeakResults.PREFERRED_DISTANCE_UNIT, MemoryPeakResults.PREFERRED_INTENSITY_UNIT, MemoryPeakResults.PREFERRED_ANGLE_UNIT);
checkEqual(fileFormat, false, false, false, false, false, false, out, in);
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method writeMatchesRead.
// -=-=-=-=-
private static void writeMatchesRead(RandomSeed seed, boolean sequential, ResultsFileFormat fileFormat, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, boolean sort) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, showDeviations, showEndFrame, showId, showPrecision, showCategory);
if (fileFormat == ResultsFileFormat.MALK) {
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.NM);
cal.setIntensityUnit(IntensityUnit.PHOTON);
out.setCalibration(cal.getCalibration());
out.setPsf(PsfHelper.create(PSFType.CUSTOM));
}
if (fileFormat == ResultsFileFormat.TSF) {
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
// For now just support using the native float TSF datatype
cal.setNmPerPixel((float) cal.getNmPerPixel());
out.setCalibration(cal.getCalibration());
// TSF converts the width parameters so make sure they are not zero
out.forEach(new PeakResultProcedure() {
@Override
public void execute(PeakResult peakResult) {
check(peakResult.getParameters());
if (showDeviations) {
check(peakResult.getParameterDeviations());
}
}
private void check(float[] parameters) {
for (int i = 0; i < parameters.length; i++) {
if (parameters[i] == 0) {
parameters[i] = 0.1f;
}
}
}
});
}
// System.out.println(out.getCalibration());
// System.out.println(out.getPSF().toString());
//
// System.out.println(TextFormat.shortDebugString(out.getCalibration()));
//
// try
// {
// Printer printer = JsonFormat.printer()
// .omittingInsignificantWhitespace()
// //.includingDefaultValueFields()
// ;
// System.out.println(printer.print(out.getCalibration()));
// System.out.println(printer.print(out.getPSF()));
// }
// catch (InvalidProtocolBufferException e)
// {
// // This shouldn't happen so throw it
// }
final String filename = createFile();
writeFile(sequential, fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, out, filename);
final MemoryPeakResults in = readFile(filename, false);
checkEqual(fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, out, in);
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method canConvertMalkToNmAndPhotons.
@SeededTest
void canConvertMalkToNmAndPhotons(RandomSeed seed) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 200, false, false, false, false, false);
// Output in pixel and count
final CalibrationWriter cal = new CalibrationWriter(out.getCalibration());
cal.setDistanceUnit(DistanceUnit.PIXEL);
cal.setIntensityUnit(IntensityUnit.COUNT);
out.setCalibration(cal.getCalibration());
out.setPsf(PsfHelper.create(PSFType.CUSTOM));
final String filename = createFile();
writeFile(false, ResultsFileFormat.MALK, false, false, false, false, false, false, out, filename);
final MemoryPeakResults in = readFile(filename, false);
// Change to nm and photon for the validation
out.convertToUnits(DistanceUnit.NM, IntensityUnit.PHOTON, null);
checkEqual(ResultsFileFormat.MALK, false, false, false, false, false, false, out, in);
}
use of org.apache.commons.rng.UniformRandomProvider in project GDSC-SMLM by aherbert.
the class PeakResultsReaderTest method checkScannerMatchesNonScanner.
private static void checkScannerMatchesNonScanner(RandomSeed seed, boolean showDeviations, boolean showEndFrame, boolean showId, boolean showPrecision, boolean showCategory, boolean sort) {
final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
final MemoryPeakResults out = createResults(rg, 1000, showDeviations, showEndFrame, showId, showPrecision, showCategory);
final String filename = createFile();
final ResultsFileFormat fileFormat = ResultsFileFormat.TEXT;
writeFile(false, fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, out, filename);
final MemoryPeakResults in = readFile(filename, false);
final MemoryPeakResults in2 = readFile(filename, true);
checkEqual(fileFormat, showDeviations, showEndFrame, showId, showPrecision, showCategory, sort, in, in2);
}
Aggregations