Search in sources :

Example 1 with UnicodeReader

use of uk.ac.sussex.gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class About method installResource.

/**
 * Install resource.
 *
 * @param resource the resource
 * @param ijDirectory the ij directory
 * @param destinationName the destination name
 * @param resourceTitle the resource title
 * @param helpKey the help key
 * @param notes the notes
 * @param options the options
 * @return -1 on error, 0 if installed, 1 if removed
 */
private static int installResource(String resource, String ijDirectory, String destinationName, String resourceTitle, String helpKey, String notes, ConfigureOption... options) {
    final Class<About> resourceClass = About.class;
    final String dir = IJ.getDirectory(ijDirectory);
    if (dir == null) {
        IJ.error("Unable to locate " + ijDirectory + " directory");
        return -1;
    }
    final EnumSet<ConfigureOption> opt = EnumSet.of(options[0], options);
    GenericDialog gd = new GenericDialog(TITLE);
    final String filename = dir + destinationName;
    final boolean fileExists = new File(filename).exists();
    final StringBuilder sb = new StringBuilder();
    sb.append("Configure resource '").append(resourceTitle).append("' at:\n \n").append(filename);
    if (notes != null) {
        sb.append("\n \n").append(uk.ac.sussex.gdsc.core.utils.TextUtils.wrap(notes, 80));
    }
    gd.addHelp(HelpUrls.getUrl(helpKey));
    gd.addMessage(sb.toString());
    // Configure the options
    String[] choices = new String[3];
    final ConfigureOption[] optChoices = new ConfigureOption[choices.length];
    int count = 0;
    if (opt.contains(ConfigureOption.INSTALL)) {
        choices[count] = ConfigureOption.INSTALL.toString();
        if (fileExists) {
            choices[count] += " (overwrite)";
        }
        optChoices[count] = ConfigureOption.INSTALL;
        count++;
    }
    if (opt.contains(ConfigureOption.EDIT)) {
        choices[count] = ConfigureOption.EDIT.toString();
        if (fileExists) {
            choices[count] += " (overwrite)";
        }
        optChoices[count] = ConfigureOption.EDIT;
        count++;
    }
    if (opt.contains(ConfigureOption.REMOVE) && fileExists) {
        choices[count] = ConfigureOption.REMOVE.toString();
        optChoices[count] = ConfigureOption.REMOVE;
        count++;
    }
    if (count == 0) {
        return -1;
    }
    choices = Arrays.copyOf(choices, count);
    gd.addChoice("Option", choices, choices[0]);
    gd.showDialog();
    if (gd.wasCanceled()) {
        return -1;
    }
    final ConfigureOption choice = optChoices[gd.getNextChoiceIndex()];
    if (choice == ConfigureOption.REMOVE) {
        try {
            Files.delete(Paths.get(filename));
            return 1;
        } catch (final SecurityException | IOException ex) {
            IJ.error("Unable to remove existing file");
        }
        return -1;
    }
    // Read the file
    final LinkedList<String> contents = new LinkedList<>();
    try (BufferedReader input = new BufferedReader(new UnicodeReader(resourceClass.getResourceAsStream(resource), null))) {
        String line;
        while ((line = input.readLine()) != null) {
            contents.add(line);
        }
    } catch (final IOException ex) {
        IJ.error("Unable to install " + resourceTitle + ".\n \n" + ex.getMessage());
        return -1;
    }
    if (choice == ConfigureOption.EDIT) {
        // Allow the user to edit the file contents
        gd = new GenericDialog(TITLE);
        gd.addMessage("Edit the file contents before install:");
        sb.setLength(0);
        for (final String line : contents) {
            sb.append(line).append('\n');
        }
        gd.addTextAreas(sb.toString(), null, 20, 80);
        gd.showDialog();
        if (gd.wasOKed()) {
            contents.clear();
            final String text = gd.getNextText();
            for (final String line : text.split("\n")) {
                contents.add(line);
            }
        }
    }
    // Install the file
    try (BufferedWriter output = Files.newBufferedWriter(Paths.get(filename))) {
        for (final String content : contents) {
            output.write(content);
            output.newLine();
        }
    } catch (final IOException ex) {
        IJ.error("Unable to install " + resourceTitle + ".\n \n" + ex.getMessage());
    }
    return 0;
}
Also used : IOException(java.io.IOException) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) LinkedList(java.util.LinkedList) BufferedWriter(java.io.BufferedWriter) GenericDialog(ij.gui.GenericDialog) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 2 with UnicodeReader

use of uk.ac.sussex.gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class LoadLocalisations method loadLocalisations.

/**
 * Load localisations.
 *
 * <p>Any calibration will be written to the Calibration.Builder contained in the settings (see
 * {@link uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.LoadLocalisationsSettings.Builder#getCalibrationBuilder()
 * LoadLocalisationsSettings.Builder.getCalibrationBuilder()}).
 *
 * @param settings the settings
 * @return the localisation list
 */
public static LocalisationList loadLocalisations(LoadLocalisationsSettings.Builder settings) {
    if (!getFields(settings)) {
        return null;
    }
    final LocalisationList localisations = new LocalisationList(settings.getCalibration());
    final String comment = settings.getComment();
    final boolean hasComment = !TextUtils.isNullOrEmpty(comment);
    int errors = 0;
    int count = 0;
    int headerCount = Math.max(0, settings.getHeaderLines());
    try (BufferedReader input = new BufferedReader(new UnicodeReader(new FileInputStream(settings.getLocalisationsFilename()), null))) {
        final Pattern p = Pattern.compile(settings.getDelimiter());
        final int it = settings.getFieldT();
        final int iid = settings.getFieldId();
        final int ic = settings.getFieldCategory();
        final int ix = settings.getFieldX();
        final int iy = settings.getFieldY();
        final int iz = settings.getFieldZ();
        final int ii = settings.getFieldI();
        final int isx = settings.getFieldSx();
        final int isy = settings.getFieldSy();
        final int ip = settings.getFieldPrecision();
        String line;
        while ((line = input.readLine()) != null) {
            // Skip header
            if (headerCount-- > 0) {
                continue;
            }
            // Skip empty lines
            if (line.length() == 0) {
                continue;
            }
            // Skip comments
            if (hasComment && line.startsWith(comment)) {
                continue;
            }
            count++;
            final String[] fields = p.split(line);
            final Localisation l = new Localisation();
            try {
                if (it >= 0) {
                    l.time = Integer.parseInt(fields[it].trim());
                }
                if (iid >= 0) {
                    l.id = Integer.parseInt(fields[iid].trim());
                }
                if (ic >= 0) {
                    l.category = Integer.parseInt(fields[ic].trim());
                }
                l.x = Float.parseFloat(fields[ix].trim());
                l.y = Float.parseFloat(fields[iy].trim());
                if (iz >= 0) {
                    l.z = Float.parseFloat(fields[iz].trim());
                }
                if (ii >= 0) {
                    l.intensity = Float.parseFloat(fields[ii].trim());
                }
                if (isx >= 0) {
                    l.sy = l.sx = Float.parseFloat(fields[isx].trim());
                }
                if (isy >= 0) {
                    l.sy = Float.parseFloat(fields[isy].trim());
                }
                if (ip >= 0) {
                    l.precision = Float.parseFloat(fields[ip].trim());
                }
                localisations.add(l);
            } catch (final NumberFormatException | IndexOutOfBoundsException ex) {
                // Log the first error line.
                if (errors++ == 0) {
                    ImageJUtils.log("%s error on record number %d: '%s'. %s: %s", TITLE, count, line, ex.getClass().getSimpleName(), ex.getMessage());
                }
            }
        }
    } catch (final IOException ex) {
        ImageJUtils.log("%s IO error: %s", TITLE, ex.getMessage());
    }
    if (errors != 0) {
        ImageJUtils.log("%s has %d / %d error lines", TITLE, errors, count);
    }
    return localisations;
}
Also used : Pattern(java.util.regex.Pattern) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader)

Example 3 with UnicodeReader

use of uk.ac.sussex.gdsc.core.utils.UnicodeReader 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 4 with UnicodeReader

use of uk.ac.sussex.gdsc.core.utils.UnicodeReader 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 5 with UnicodeReader

use of uk.ac.sussex.gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method readFilterSets.

@Nullable
@SuppressWarnings("unchecked")
private List<FilterSet> readFilterSets() {
    if (extraOptions) {
        final MultiPathFilter multiFilter = BenchmarkSpotFit.getMultiFilter();
        if (multiFilter != null) {
            final IDirectFilter f = multiFilter.getFilter();
            if (f instanceof DirectFilter) {
                final GenericDialog gd = new GenericDialog(TITLE);
                gd.addMessage("Use an identical filter to " + BenchmarkSpotFit.TITLE);
                gd.enableYesNoCancel();
                gd.hideCancelButton();
                gd.showDialog();
                if (gd.wasOKed()) {
                    final List<FilterSet> filterSets = new ArrayList<>(1);
                    final List<Filter> filters = new ArrayList<>(1);
                    filters.add((DirectFilter) f);
                    final FilterSet filterSet = new FilterSet(filters);
                    filterSets.add(filterSet);
                    resetParametersFromFitting();
                    createResultsPrefix2();
                    return filterSets;
                }
            }
        }
    }
    GUIFilterSettings filterSettings = SettingsManager.readGuiFilterSettings(0);
    final String filename = ImageJUtils.getFilename("Filter_File", filterSettings.getFilterSetFilename());
    if (filename != null) {
        IJ.showStatus("Reading filters ...");
        filterSettings = filterSettings.toBuilder().setFilterSetFilename(filename).build();
        // Allow the filters to be cached
        final Triple<String, Long, List<FilterSet>> filterCache = lastFilterList.get();
        if (isSameFile(filename, filterCache)) {
            final GenericDialog gd = new GenericDialog(TITLE);
            gd.hideCancelButton();
            gd.addMessage("The same filter file was selected.");
            gd.addCheckbox("Re-use_filters", settings.reUseFilters);
            gd.showDialog();
            if (!gd.wasCanceled()) {
                settings.reUseFilters = gd.getNextBoolean();
                if (settings.reUseFilters) {
                    SettingsManager.writeSettings(filterSettings);
                    return filterCache.getRight();
                }
            }
        }
        final File file = new File(filename);
        try (BufferedReader input = new BufferedReader(new UnicodeReader(new FileInputStream(file), null))) {
            // Use the instance so we can catch the exception
            final Object o = FilterXStreamUtils.getXStreamInstance().fromXML(input);
            if (!(o instanceof List<?>)) {
                IJ.log("No filter sets defined in the specified file: " + filename);
                return null;
            }
            SettingsManager.writeSettings(filterSettings);
            List<FilterSet> filterSets = (List<FilterSet>) o;
            if (containsStandardFilters(filterSets)) {
                IJ.log("Filter sets must contain 'Direct' filters");
                return null;
            }
            // Check they are not empty lists
            final List<FilterSet> filterSets2 = new LinkedList<>();
            for (final FilterSet filterSet : filterSets) {
                if (filterSet.size() != 0) {
                    filterSets2.add(filterSet);
                } else {
                    IJ.log("Filter set empty: " + filterSet.getName());
                }
            }
            if (filterSets2.isEmpty()) {
                IJ.log("All Filter sets are empty");
                return null;
            }
            // Maintain the same list type
            filterSets.clear();
            filterSets.addAll(filterSets2);
            // Option to enumerate filters
            filterSets = expandFilters(filterSets);
            // Save for re-use
            lastFilterList.set(Triple.of(filename, getLastModified(file), filterSets));
            return filterSets;
        } catch (final Exception ex) {
            IJ.log("Unable to load the filter sets from file: " + ex.getMessage());
        } finally {
            IJ.showStatus("");
        }
    }
    return null;
}
Also used : FilterSet(uk.ac.sussex.gdsc.smlm.results.filter.FilterSet) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) ArrayList(java.util.ArrayList) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) UnicodeReader(uk.ac.sussex.gdsc.core.utils.UnicodeReader) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException) GUIFilterSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.GUIFilterSettings) Filter(uk.ac.sussex.gdsc.smlm.results.filter.Filter) IDirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.IDirectFilter) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) DirectFilter(uk.ac.sussex.gdsc.smlm.results.filter.DirectFilter) MaximaSpotFilter(uk.ac.sussex.gdsc.smlm.filters.MaximaSpotFilter) GenericDialog(ij.gui.GenericDialog) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) MultiPathFilter(uk.ac.sussex.gdsc.smlm.results.filter.MultiPathFilter) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) SettingsList(uk.ac.sussex.gdsc.core.utils.SettingsList) List(java.util.List) LinkedList(java.util.LinkedList) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) File(java.io.File) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Aggregations

BufferedReader (java.io.BufferedReader)12 UnicodeReader (uk.ac.sussex.gdsc.core.utils.UnicodeReader)12 IOException (java.io.IOException)11 FileInputStream (java.io.FileInputStream)10 GenericDialog (ij.gui.GenericDialog)3 File (java.io.File)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Pattern (java.util.regex.Pattern)2 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)2 CalibrationWriter (uk.ac.sussex.gdsc.smlm.data.config.CalibrationWriter)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 NonBlockingGenericDialog (ij.gui.NonBlockingGenericDialog)1 Dimension (java.awt.Dimension)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 Panel (java.awt.Panel)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1