Search in sources :

Example 11 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class PeakResultsReader method readMalk.

private MemoryPeakResults readMalk() {
    final MemoryPeakResults results = createResults();
    if (TextUtils.isNullOrEmpty(name)) {
        results.setName(FileUtils.getName(filename));
    }
    try (FileInputStream fis = new FileInputStream(filename);
        BufferedReader input = new BufferedReader(new UnicodeReader(fis, null))) {
        final ProgressReporter reporter = createProgressReporter(fis);
        String line;
        int errors = 0;
        // Skip the header
        while ((line = input.readLine()) != null) {
            if (line.isEmpty()) {
                continue;
            }
            if (line.charAt(0) != '#') {
                // This is the first record
                if (!addMalkResult(results, line)) {
                    errors = 1;
                }
                break;
            }
        }
        while ((line = input.readLine()) != null) {
            if (line.isEmpty() || line.charAt(0) == '#') {
                continue;
            }
            if (!addMalkResult(results, line) && ++errors >= 10) {
                break;
            }
            reporter.showProgress();
        }
    } catch (final IOException ex) {
        logError(ex);
    }
    // The calibration may not be null if this was a GDSC MALK file since that has a header.
    if (calibration == null) {
        calibration = new CalibrationWriter();
        // Default assumption is nm
        calibration.setDistanceUnit(DistanceUnit.NM);
        // MALK uses photons
        calibration.setIntensityUnit(IntensityUnit.PHOTON);
        results.setCalibration(getCalibration());
    }
    return results;
}
Also used : BufferedReader(java.io.BufferedReader) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 12 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class PeakResultsReader method readNStorm.

private MemoryPeakResults readNStorm() {
    final MemoryPeakResults results = createResults();
    results.setName(FileUtils.getName(filename));
    try (FileInputStream fis = new FileInputStream(filename);
        BufferedReader input = new BufferedReader(new UnicodeReader(fis, null))) {
        final ProgressReporter reporter = createProgressReporter(fis);
        String line;
        int errors = 0;
        // The single line header
        final String header = input.readLine();
        if (header == null) {
            throw new IOException("NStorm header missing");
        }
        // NStorm files added more column fields for later formats.
        // If the header contains 'Photons' then this can be used to determine the gain
        boolean readPhotons = header.contains("\tPhotons\t");
        while ((line = input.readLine()) != null) {
            if (line.isEmpty()) {
                continue;
            }
            final PeakResult result = createNStormResult(line, readPhotons);
            if (result != null) {
                results.add(result);
                // Just read the photons from the first 100
                if (readPhotons) {
                    readPhotons = results.size() < 100;
                }
            } else if (++errors >= 10) {
                break;
            }
            reporter.showProgress();
        }
    } catch (final IOException ex) {
        logError(ex);
    }
    // The following relationship holds when length == 1:
    // intensity = height * 2 * pi * sd0 * sd1 / pixel_pitch^2
    // => Pixel_pitch = sqrt(height * 2 * pi * sd0 * sd1 / intensity)
    // Try and create a calibration
    final Statistics pixelPitch = new Statistics();
    results.forEach(new PeakResultProcedureX() {

        static final double TWO_PI = 2 * Math.PI;

        @Override
        public boolean execute(PeakResult peakResult) {
            if (peakResult.getFrame() == peakResult.getEndFrame()) {
                final float height = peakResult.getOrigValue();
                final float intensity = peakResult.getParameter(PeakResult.INTENSITY);
                final float sd0 = peakResult.getParameter(INDEX_SX);
                final float sd1 = peakResult.getParameter(INDEX_SY);
                pixelPitch.add(Math.sqrt(height * TWO_PI * sd0 * sd1 / intensity));
                // Stop when we have enough for a good guess
                return (pixelPitch.getN() > 100);
            }
            return false;
        }
    });
    // Determine the gain using the photons column
    final Statistics gain = new Statistics();
    results.forEach((PeakResultProcedureX) peakResult -> {
        double photons = peakResult.getError();
        if (photons != 0) {
            peakResult.setError(0);
            gain.add(peakResult.getIntensity() / photons);
            return false;
        }
        return true;
    });
    // TODO - Support all the NSTORM formats: one-axis, two-axis, rotated, 3D.
    // Is this information in the header?
    // We could support setting the PSF as a Gaussian2D with one/two axis SD.
    // This would mean updating all the result params if it is a one axis PSF.
    // For now just record it as a 2 axis PSF.
    // Create a calibration
    calibration = new CalibrationWriter();
    // NSTORM data is in counts when the Photons column is present.
    // Q. Is it in counts when this column is not present?
    calibration.setIntensityUnit(IntensityUnit.COUNT);
    calibration.setDistanceUnit(DistanceUnit.NM);
    if (pixelPitch.getN() > 0) {
        final double nmPerPixel = pixelPitch.getMean();
        calibration.setNmPerPixel(nmPerPixel);
    }
    if (gain.getN() > 0 && gain.getStandardError() < 1e-3) {
        calibration.setCountPerPhoton(gain.getMean());
    }
    results.setCalibration(calibration.getCalibration());
    return results;
}
Also used : Rectangle(java.awt.Rectangle) DataInputStream(java.io.DataInputStream) AngleUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.AngleUnit) Calibration(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX) Scanner(java.util.Scanner) PSF(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF) IntensityUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit) PSFType(uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSFType) TrackProgress(uk.ac.sussex.gdsc.core.logging.TrackProgress) Level(java.util.logging.Level) Matcher(java.util.regex.Matcher) NotNull(uk.ac.sussex.gdsc.core.annotation.NotNull) Locale(java.util.Locale) XStreamUtils(uk.ac.sussex.gdsc.smlm.utils.XStreamUtils) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) NoSuchElementException(java.util.NoSuchElementException) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType) IOException(java.io.IOException) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) FileInputStream(java.io.FileInputStream) Logger(java.util.logging.Logger) EOFException(java.io.EOFException) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) Objects(java.util.Objects) BitFlagUtils(uk.ac.sussex.gdsc.core.utils.BitFlagUtils) JsonFormat(com.google.protobuf.util.JsonFormat) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable) FileUtils(uk.ac.sussex.gdsc.core.utils.FileUtils) PsfHelper(uk.ac.sussex.gdsc.smlm.data.config.PsfHelper) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) FileChannel(java.nio.channels.FileChannel) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) Statistics(uk.ac.sussex.gdsc.core.utils.Statistics) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) PeakResultProcedureX(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedureX)

Example 13 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter 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)

Example 14 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class TraceDiffusion method checkCalibration.

/**
 * Check the results have a calibrated exposure time and pixel pitch. If not then show a dialog to
 * collect the calibration.
 *
 * @param results the results
 * @return True if calibrated
 */
private static boolean checkCalibration(MemoryPeakResults results) {
    if (results.getCalibration() == null || !results.getCalibrationReader().hasExposureTime() || !results.getCalibrationReader().hasNmPerPixel()) {
        final CalibrationWriter cal = results.getCalibrationWriterSafe();
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("Uncalibrated results! Please enter the calibration:");
        gd.addNumericField("Exposure_time (ms)", cal.getExposureTime(), 2);
        gd.addNumericField("Pixel_pitch (nm)", cal.getNmPerPixel(), 2);
        gd.showDialog();
        if (gd.wasCanceled() || gd.invalidNumber()) {
            return false;
        }
        cal.setExposureTime(gd.getNextNumber());
        cal.setNmPerPixel(gd.getNextNumber());
        if (cal.getExposureTime() <= 0 || cal.getNmPerPixel() <= 0) {
            return false;
        }
        results.setCalibration(cal.getCalibration());
    }
    return true;
}
Also used : CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Example 15 with CalibrationWriter

use of uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter in project GDSC-SMLM by aherbert.

the class TrackPopulationAnalysis method checkCalibration.

/**
 * Check the results have a calibrated exposure time and pixel pitch. If not then show a dialog to
 * collect the calibration.
 *
 * @param results the results
 * @return True if calibrated
 */
private static boolean checkCalibration(MemoryPeakResults results) {
    if (results.getCalibration() == null || !results.getCalibrationReader().hasExposureTime() || !results.getCalibrationReader().hasNmPerPixel()) {
        final CalibrationWriter cal = results.getCalibrationWriterSafe();
        final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
        gd.addMessage("Uncalibrated results! Please enter the calibration:");
        gd.addNumericField("Exposure_time", cal.getExposureTime(), 2, 6, "ms");
        gd.addNumericField("Pixel_pitch", cal.getNmPerPixel(), 2, 6, "nm");
        gd.showDialog();
        if (gd.wasCanceled() || gd.invalidNumber()) {
            return false;
        }
        cal.setExposureTime(gd.getNextNumber());
        cal.setNmPerPixel(gd.getNextNumber());
        if (cal.getExposureTime() <= 0 || cal.getNmPerPixel() <= 0) {
            return false;
        }
        results.setCalibration(cal.getCalibration());
    }
    return true;
}
Also used : CalibrationWriter(uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)

Aggregations

CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)45 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)18 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)8 Rectangle (java.awt.Rectangle)7 Calibration (uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.Calibration)7 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)7 Test (org.junit.jupiter.api.Test)5 PSF (uk.ac.sussex.gdsc.smlm.data.config.PSFProtos.PSF)5 FitConfiguration (uk.ac.sussex.gdsc.smlm.engine.FitConfiguration)5 CalibrationReader (uk.ac.sussex.gdsc.smlm.data.config.CalibrationReader)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 Checkbox (java.awt.Checkbox)3 TextField (java.awt.TextField)3 IOException (java.io.IOException)3 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)3 IntensityUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.IntensityUnit)3 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)3 PeakResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure)3 ImagePlus (ij.ImagePlus)2 Calibration (ij.measure.Calibration)2