Search in sources :

Example 1 with SpotList

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();
}
Also used : FluorophoreType(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.FluorophoreType) NotImplementedException(uk.ac.sussex.gdsc.core.data.NotImplementedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SpotList(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList) CalibrationReader(uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader) Printer(com.google.protobuf.util.JsonFormat.Printer) ROI(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.ROI)

Example 2 with SpotList

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);
                }
            }
        }
    }
}
Also used : FluorophoreType(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.FluorophoreType) Spot(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.Spot) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) SpotList(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) FileOutputStream(java.io.FileOutputStream) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with SpotList

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;
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SpotList(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList) FileInputStream(java.io.FileInputStream)

Aggregations

SpotList (uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.SpotList)3 IOException (java.io.IOException)2 FluorophoreType (uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.FluorophoreType)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Printer (com.google.protobuf.util.JsonFormat.Printer)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)1 NotImplementedException (uk.ac.sussex.gdsc.core.data.NotImplementedException)1 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)1 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)1 ROI (uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.ROI)1 Spot (uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.Spot)1