use of uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList in project GDSC-SMLM by aherbert.
the class TsfPeakResultsWriter method createSpotList.
private SpotList createSpotList() {
final SpotList.Builder builder = SpotList.newBuilder();
builder.setApplicationId(APPLICATION_ID);
builder.setNrSpots(size);
// Add the standard details the TSF supports. We use extensions to add GDSC SMLM data.
if (!TextUtils.isNullOrEmpty(getName())) {
builder.setName(getName());
}
if (getSource() != null) {
builder.setNrPixelsX(getSource().width);
builder.setNrPixelsY(getSource().height);
builder.setNrFrames(getSource().frames);
builder.setSource(singleLine(getSource().toXml()));
}
if (getBounds() != null) {
final ROI.Builder roiBuilder = builder.getRoiBuilder();
roiBuilder.setX(getBounds().x);
roiBuilder.setY(getBounds().y);
roiBuilder.setXWidth(getBounds().width);
roiBuilder.setYWidth(getBounds().height);
builder.setRoi(roiBuilder.build());
}
if (hasCalibration()) {
final CalibrationReader cr = getCalibrationReader();
if (cr.hasNmPerPixel()) {
builder.setPixelSize((float) cr.getNmPerPixel());
}
if (cr.hasExposureTime()) {
builder.setExposureTime(cr.getExposureTime());
}
if (cr.hasReadNoise()) {
builder.setReadNoise(cr.getReadNoise());
}
if (cr.hasBias()) {
builder.setBias(cr.getBias());
}
if (cr.hasCameraType()) {
builder.setCameraType(cameraTypeMap[cr.getCameraType().ordinal()]);
}
if (cr.hasDistanceUnit()) {
builder.setLocationUnits(locationUnitsMap[cr.getDistanceUnit().ordinal()]);
}
if (cr.hasIntensityUnit()) {
builder.setIntensityUnits(intensityUnitsMap[cr.getIntensityUnit().ordinal()]);
}
if (cr.hasAngleUnit()) {
builder.setThetaUnits(thetaUnitsMap[cr.getAngleUnit().ordinal()]);
}
// We can use some logic here to get the QE
if (cr.hasCountPerPhoton()) {
builder.setGain(cr.getCountPerPhoton());
final double qe = (cr.hasQuantumEfficiency()) ? cr.getQuantumEfficiency() : 1;
// e-/photon / count/photon => e-/count
final double ecf = qe / cr.getCountPerPhoton();
builder.addEcf(ecf);
builder.addQe(qe);
}
}
if (!TextUtils.isNullOrEmpty(getConfiguration())) {
builder.setConfiguration(singleLine(getConfiguration()));
}
if (getPsf() != null) {
try {
final Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
builder.setPSF(printer.print(getPsf()));
} catch (final InvalidProtocolBufferException ex) {
// This shouldn't happen so throw it
throw new NotImplementedException("Unable to serialise the PSF settings", ex);
}
}
// Have a property so the boxSize can be set
if (boxSize > 0) {
builder.setBoxSize(boxSize);
}
builder.setFitMode(fitMode);
final FluorophoreType.Builder typeBuilder = FluorophoreType.newBuilder();
typeBuilder.setId(1);
typeBuilder.setDescription("Default fluorophore");
typeBuilder.setIsFiducial(false);
builder.addFluorophoreTypes(typeBuilder.build());
return builder.build();
}
use of uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList in project GDSC-SMLM by aherbert.
the class ResultsManagerTest method checkWriteTsfMatchesRead.
@SuppressWarnings("null")
private static void checkWriteTsfMatchesRead(RandomSeed seed, int channels, int slices, int positions, int types) {
Assumptions.assumeFalse(java.awt.GraphicsEnvironment.isHeadless());
final String filename = createFile();
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
} catch (final Exception ex) {
closeOutput(out);
ex.printStackTrace();
Assertions.fail(ex.getMessage());
}
// Write the offsets used in the Tsf format
try {
@SuppressWarnings("resource") final DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(0);
dos.writeLong(0);
} catch (final IOException ex) {
closeOutput(out);
ex.printStackTrace();
Assertions.fail(ex.getMessage());
}
// Generate random spots
final UniformRandomProvider rand = RngUtils.create(seed.getSeed());
final int size = 100;
final Spot[] spots = new Spot[size];
for (int i = 1; i <= size; i++) {
final Spot.Builder builder = Spot.newBuilder();
builder.setChannel(nextInt(rand, channels));
builder.setSlice(nextInt(rand, slices));
builder.setPos(nextInt(rand, positions));
builder.setFluorophoreType(nextInt(rand, types));
// This is a required field but is ignored when reading
builder.setMolecule(i);
builder.setCluster(rand.nextInt(10));
builder.setFrame(nextInt(rand, 100));
builder.setXPosition(rand.nextInt(50));
builder.setYPosition(rand.nextInt(50));
builder.setBackground(rand.nextFloat());
builder.setIntensity(rand.nextFloat());
builder.setX(rand.nextFloat());
builder.setY(rand.nextFloat());
builder.setZ(rand.nextFloat());
builder.setWidth((float) (Gaussian2DFunction.SD_TO_FWHM_FACTOR * rand.nextDouble()));
final Spot spot = builder.build();
spots[i - 1] = spot;
try {
spot.writeDelimitedTo(out);
} catch (final IOException ex) {
closeOutput(out);
ex.printStackTrace();
Assertions.fail(ex.getMessage());
}
}
// Write the header
// Get the offset to the SpotList message
long offset = 0;
try {
// The offset is the amount to skip forward after reading the int
// magic number (4 bytes) and long offset (8 bytes)
// out.flush();
offset = out.getChannel().position() - 12;
} catch (final IOException ex) {
closeOutput(out);
ex.printStackTrace();
Assertions.fail(ex.getMessage());
}
// Record the SpotList message
final SpotList.Builder builder = SpotList.newBuilder();
builder.setApplicationId(1);
builder.setNrSpots(size);
builder.setLocationUnits(LocationUnits.PIXELS);
builder.setIntensityUnits(IntensityUnits.COUNTS);
builder.setFitMode(FitMode.ONEAXIS);
builder.setNrChannels(channels);
builder.setNrSlices(slices);
builder.setNrPos(positions);
for (int type = 1; type <= types; type++) {
final FluorophoreType.Builder typeBuilder = FluorophoreType.newBuilder();
typeBuilder.setId(type);
typeBuilder.setDescription("Type " + type);
typeBuilder.setIsFiducial(rand.nextDouble() < 0.5);
builder.addFluorophoreTypes(typeBuilder.build());
}
final SpotList spotList = builder.build();
try {
spotList.writeDelimitedTo(out);
} catch (final IOException ex) {
ex.printStackTrace();
Assertions.fail(ex.getMessage());
} finally {
closeOutput(out);
}
// Write the offset to the SpotList message into the offset position
try (RandomAccessFile f = new RandomAccessFile(new File(filename), "rw")) {
f.seek(4);
f.writeLong(offset);
} catch (final Exception ex) {
ex.printStackTrace();
Assertions.fail(ex.getMessage());
}
// Read each combination
for (int channel = 1; channel <= channels; channel++) {
for (int slice = 1; slice <= slices; slice++) {
for (int position = 1; position <= positions; position++) {
for (int type = 1; type <= types; type++) {
final StringBuilder sb = new StringBuilder();
sb.append(" channel=").append(channel);
sb.append(" slice=").append(slice);
sb.append(" position=").append(position);
sb.append(" fluorophore_type=[").append(type).append(":Type ").append(type).append(":fiducial=").append(builder.getFluorophoreTypes(type - 1).getIsFiducial()).append(']');
// This is needed to trick the Macro class into returning the options
// for the thread to the GenericDialog used in the ResultsManager
Thread.currentThread().setName("Run$_");
Macro.setOptions(sb.toString());
final MemoryPeakResults in = ResultsManager.loadInputResults(ResultsManager.INPUT_FILE, false, null, null, new ResultsManager.FilenameLoadOption(filename));
checkEqual(spots, channel, slice, position, type, in);
}
}
}
}
}
use of uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList in project GDSC-SMLM by aherbert.
the class TsfPeakResultsReader method isTsf.
/**
* Checks if is a binary TSF file by attempting to read the SpotList header.
*
* @param filename the filename
* @return true, if is a TSF file
*/
public static boolean isTsf(String filename) {
try (FileInputStream fi = new FileInputStream(filename);
DataInputStream di = new DataInputStream(fi)) {
// the file has an initial 0, then the offset (as long)
// to the position of spotList
final int magic = di.readInt();
if (magic != 0) {
// Magic number should be zero
return false;
}
final long offset = di.readLong();
if (offset < 0) {
// No offset record
return false;
}
if (fi.skip(offset) != offset) {
// Bad skip
return false;
}
final SpotList spotList = SpotList.parseDelimitedFrom(fi);
if (spotList != null) {
return true;
}
} catch (final IOException ex) {
// Fail
}
return false;
}
Aggregations