Search in sources :

Example 1 with Parser

use of com.google.protobuf.util.JsonFormat.Parser in project grpc-java by grpc.

the class ProtoUtils method jsonMarshaller.

/**
   * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
   *
   * <p>This is an unstable API and has not been optimized yet for performance.
   */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786")
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
    final Parser parser = JsonFormat.parser();
    final Printer printer = JsonFormat.printer();
    return jsonMarshaller(defaultInstance, parser, printer);
}
Also used : Printer(com.google.protobuf.util.JsonFormat.Printer) Parser(com.google.protobuf.util.JsonFormat.Parser) ExperimentalApi(io.grpc.ExperimentalApi)

Example 2 with Parser

use of com.google.protobuf.util.JsonFormat.Parser in project grpc-java by grpc.

the class JsonMarshaller method jsonMarshaller.

/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
    final Parser parser = JsonFormat.parser();
    final Printer printer = JsonFormat.printer();
    return jsonMarshaller(defaultInstance, parser, printer);
}
Also used : Printer(com.google.protobuf.util.JsonFormat.Printer) Parser(com.google.protobuf.util.JsonFormat.Parser)

Example 3 with Parser

use of com.google.protobuf.util.JsonFormat.Parser in project GDSC-SMLM by aherbert.

the class TsfPeakResultsReader method createResults.

private MemoryPeakResults createResults() {
    // Limit the capacity since we may not need all the spots
    int capacity = 1000;
    if (spotList.hasNrSpots()) {
        capacity = (int) Math.min(100000, spotList.getNrSpots());
    }
    final MemoryPeakResults results = new MemoryPeakResults(capacity);
    // Create the type of Gaussian PSF
    if (spotList.hasFitMode()) {
        switch(spotList.getFitMode()) {
            case ONEAXIS:
                results.setPsf(PsfHelper.create(PSFType.ONE_AXIS_GAUSSIAN_2D));
                break;
            case TWOAXIS:
                results.setPsf(PsfHelper.create(PSFType.TWO_AXIS_GAUSSIAN_2D));
                break;
            case TWOAXISANDTHETA:
                results.setPsf(PsfHelper.create(PSFType.TWO_AXIS_AND_THETA_GAUSSIAN_2D));
                break;
            default:
                break;
        }
    }
    // Generic reconstruction
    String name;
    if (spotList.hasName()) {
        name = spotList.getName();
    } else {
        name = FileUtils.getName(filename);
    }
    // Append these if not using the defaults
    if (channel != 1 || slice != 0 || position != 0 || fluorophoreType != 1) {
        name = String.format("%s c=%d, s=%d, p=%d, ft=%d", name, channel, slice, position, fluorophoreType);
    }
    results.setName(name);
    // if (spotList.hasNrPixelsX() && spotList.hasNrPixelsY())
    // {
    // // Do not do this. The size of the camera may not map to the data bounds due
    // // to the support for position offsets.
    // results.setBounds(new Rectangle(0, 0, spotList.getNrPixelsX(), spotList.getNrPixelsY()));
    // }
    final CalibrationWriter cal = new CalibrationWriter();
    // Spots are associated with frames
    cal.setTimeUnit(TimeUnit.FRAME);
    if (spotList.hasPixelSize()) {
        cal.setNmPerPixel(spotList.getPixelSize());
    }
    if (spotList.getEcfCount() >= channel) {
        // ECF is per channel
        final double ecf = spotList.getEcf(channel - 1);
        // QE is per fluorophore type
        final double qe = (spotList.getQeCount() >= fluorophoreType) ? spotList.getQe(fluorophoreType - 1) : 1;
        // e-/photon / e-/count => count/photon
        cal.setCountPerPhoton(qe / ecf);
        cal.setQuantumEfficiency(qe);
    }
    if (isGdsc) {
        if (spotList.hasSource()) {
            // Deserialise
            results.setSource(ImageSource.fromXml(spotList.getSource()));
        }
        if (spotList.hasRoi()) {
            final ROI roi = spotList.getRoi();
            if (roi.hasX() && roi.hasY() && roi.hasXWidth() && roi.hasYWidth()) {
                results.setBounds(new Rectangle(roi.getX(), roi.getY(), roi.getXWidth(), roi.getYWidth()));
            }
        }
        if (spotList.hasGain()) {
            cal.setCountPerPhoton(spotList.getGain());
        }
        if (spotList.hasExposureTime()) {
            cal.setExposureTime(spotList.getExposureTime());
        }
        if (spotList.hasReadNoise()) {
            cal.setReadNoise(spotList.getReadNoise());
        }
        if (spotList.hasBias()) {
            cal.setBias(spotList.getBias());
        }
        if (spotList.hasCameraType()) {
            cal.setCameraType(cameraTypeMap.get(spotList.getCameraType()));
        } else {
            cal.setCameraType(null);
        }
        if (spotList.hasConfiguration()) {
            results.setConfiguration(spotList.getConfiguration());
        }
        // Allow restoring the GDSC PSF exactly
        if (spotList.hasPSF()) {
            try {
                final Parser parser = JsonFormat.parser();
                final PSF.Builder psfBuilder = PSF.newBuilder();
                parser.merge(spotList.getPSF(), psfBuilder);
                results.setPsf(psfBuilder.build());
            } catch (final InvalidProtocolBufferException ex) {
                logger.warning("Unable to deserialise the PSF settings");
            }
        }
    }
    if (spotList.hasLocationUnits()) {
        cal.setDistanceUnit(locationUnitsMap.get(spotList.getLocationUnits()));
        if (!spotList.hasPixelSize() && spotList.getLocationUnits() != LocationUnits.PIXELS) {
            logger.warning(() -> "TSF location units are not pixels and no pixel size calibration is available." + " The dataset will be constructed in the native units: " + spotList.getLocationUnits());
        }
    } else {
        cal.setDistanceUnit(null);
    }
    if (spotList.hasIntensityUnits()) {
        cal.setIntensityUnit(intensityUnitsMap.get(spotList.getIntensityUnits()));
        if (!spotList.hasGain() && spotList.getIntensityUnits() != IntensityUnits.COUNTS) {
            logger.warning(() -> "TSF intensity units are not counts and no gain calibration is available." + " The dataset will be constructed in the native units: " + spotList.getIntensityUnits());
        }
    } else {
        cal.setIntensityUnit(null);
    }
    if (spotList.hasThetaUnits()) {
        cal.setAngleUnit(thetaUnitsMap.get(spotList.getThetaUnits()));
    } else {
        cal.setAngleUnit(null);
    }
    results.setCalibration(cal.getCalibration());
    return results;
}
Also used : PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) Rectangle(java.awt.Rectangle) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) ROI(uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.ROI) Parser(com.google.protobuf.util.JsonFormat.Parser)

Aggregations

Parser (com.google.protobuf.util.JsonFormat.Parser)3 Printer (com.google.protobuf.util.JsonFormat.Printer)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ExperimentalApi (io.grpc.ExperimentalApi)1 Rectangle (java.awt.Rectangle)1 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)1 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)1 ROI (uk.ac.sussex.gdsc.smlm.tsf.TSFProtos.ROI)1