Search in sources :

Example 6 with UnicodeReader

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

the class PeakResultsReader method getHeader.

/**
	 * @return The header from the results file
	 */
public String getHeader() {
    if (header == null) {
        BufferedReader input = null;
        try {
            FileInputStream fis = new FileInputStream(filename);
            input = new BufferedReader(new UnicodeReader(fis, null));
            StringBuffer sb = new StringBuffer();
            String line;
            int count = 0;
            while ((line = input.readLine()) != null) {
                count++;
                if (count == 1) {
                    // The NSTORM file format does not have comment characters but does have a single header line
                    if (line.startsWith("Channel Name")) {
                        sb.append(line).append("\n");
                        break;
                    }
                    // User may try and load the text saved directly from the ImageJ Table Results
                    if (line.contains(IMAGEJ_TABLE_RESULTS_HEADER)) {
                        sb.append(line).append("\n");
                        break;
                    }
                }
                if (line.length() == 0)
                    continue;
                if (line.charAt(0) == '#')
                    sb.append(line).append("\n");
                else
                    break;
            }
            header = sb.toString();
            version = getField("FileVersion");
            guessFormat(line);
        } catch (IOException e) {
        // ignore
        } finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    return header;
}
Also used : BufferedReader(java.io.BufferedReader) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 7 with UnicodeReader

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

the class PeakResultsReader method readMALK.

private MemoryPeakResults readMALK() {
    MemoryPeakResults results = createResults();
    if (Utils.isNullOrEmpty(name))
        results.setName(new File(filename).getName());
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(filename);
        FileChannel channel = fis.getChannel();
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int errors = 0;
        // Skip the header
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) != '#') {
                // This is the first record
                if (!addMALKResult(results, line))
                    errors = 1;
                break;
            }
        }
        int c = 0;
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) == '#')
                continue;
            if (!addMALKResult(results, line)) {
                if (++errors >= 10) {
                    break;
                }
            }
            if (++c % 512 == 0)
                showProgress(channel);
        }
    } catch (IOException e) {
    // ignore
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    // The GDSC SMLM code still adds a calibration to the MALK file when saving so we may be able to convert back
    if (getCalibration() != null) {
        if (Maths.isFinite(calibration.getNmPerPixel()) && calibration.getNmPerPixel() > 0) {
            double nmPerPixel = calibration.getNmPerPixel();
            for (PeakResult p : results.getResults()) {
                p.params[Gaussian2DFunction.X_POSITION] /= nmPerPixel;
                p.params[Gaussian2DFunction.Y_POSITION] /= nmPerPixel;
                p.origX = (int) p.params[Gaussian2DFunction.X_POSITION];
                p.origY = (int) p.params[Gaussian2DFunction.Y_POSITION];
            }
        }
    }
    return results;
}
Also used : FileChannel(java.nio.channels.FileChannel) BufferedReader(java.io.BufferedReader) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with UnicodeReader

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

the class PeakResultsReader method readNSTORM.

private MemoryPeakResults readNSTORM() {
    MemoryPeakResults results = createResults();
    results.setName(new File(filename).getName());
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(filename);
        FileChannel channel = fis.getChannel();
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int errors = 0;
        // Skip the single line header
        input.readLine();
        int c = 0;
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (!addNSTORMResult(results, line)) {
                if (++errors >= 10) {
                    break;
                }
            }
            if (++c % 512 == 0)
                showProgress(channel);
        }
    } catch (IOException e) {
    // ignore
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    // The following relationship holds when length == 1:
    // Area = Height * 2 * pi * (Width / (pixel_pitch*2) )^2
    // => Pixel_pitch = 0.5 * Width / sqrt(Area / (Height * 2 * pi))
    // Try and create a calibration
    Statistics pixelPitch = new Statistics();
    for (PeakResult p : results.getResults()) {
        if (p.getFrame() == p.getEndFrame()) {
            float width = p.params[Gaussian2DFunction.X_SD];
            float height = p.params[Gaussian2DFunction.SIGNAL];
            float area = p.origValue;
            pixelPitch.add(0.5 * width / Math.sqrt(area / (height * 2 * Math.PI)));
            if (pixelPitch.getN() > 100)
                break;
        }
    }
    if (pixelPitch.getN() > 0) {
        final double nmPerPixel = pixelPitch.getMean();
        final double widthConversion = 1.0 / (2 * nmPerPixel);
        // Create a calibration
        calibration = new Calibration();
        calibration.setNmPerPixel(nmPerPixel);
        results.setCalibration(calibration);
        // Convert data
        for (PeakResult p : results.getResults()) {
            p.params[Gaussian2DFunction.X_POSITION] /= nmPerPixel;
            p.params[Gaussian2DFunction.Y_POSITION] /= nmPerPixel;
            // Since the width is 2*pixel pitch
            p.params[Gaussian2DFunction.X_SD] *= widthConversion;
            p.params[Gaussian2DFunction.Y_SD] *= widthConversion;
        }
    }
    // Swap to the intensity stored in the origValue field.
    for (PeakResult p : results.getResults()) {
        final float origValue = p.params[Gaussian2DFunction.SIGNAL];
        p.params[Gaussian2DFunction.SIGNAL] = p.origValue;
        p.origValue = origValue;
    }
    return results;
}
Also used : FileChannel(java.nio.channels.FileChannel) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) Statistics(gdsc.core.utils.Statistics) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 9 with UnicodeReader

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

the class LoadLocalisations method loadLocalisations.

static LocalisationList loadLocalisations(String filename) {
    if (!getFields())
        return null;
    LocalisationList localisations = new LocalisationList(distanceUnit, intensityUnit, gain, pixelPitch, exposureTime);
    final boolean hasComment = !Utils.isNullOrEmpty(comment);
    int errors = 0;
    int count = 0;
    int h = Math.max(0, header);
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(filename);
        input = new BufferedReader(new UnicodeReader(fis, null));
        Pattern p = Pattern.compile(delimiter);
        String line;
        while ((line = input.readLine()) != null) {
            // Skip header
            if (h-- > 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);
            Localisation l = new Localisation();
            try {
                if (it >= 0)
                    l.t = Integer.parseInt(fields[it]);
                if (iid >= 0)
                    l.id = Integer.parseInt(fields[iid]);
                l.x = Float.parseFloat(fields[ix]);
                l.y = Float.parseFloat(fields[iy]);
                if (iz >= 0)
                    l.z = Float.parseFloat(fields[iz]);
                if (ii >= 0)
                    l.intensity = Float.parseFloat(fields[ii]);
                if (isx >= 0)
                    l.sy = l.sx = Integer.parseInt(fields[isx]);
                if (isy >= 0)
                    l.sy = Integer.parseInt(fields[isy]);
                if (ip >= 0)
                    l.precision = Float.parseFloat(fields[ip]);
                localisations.add(l);
            } catch (NumberFormatException e) {
                if (errors++ == 0)
                    Utils.log("%s error on record %d: %s", TITLE, count, e.getMessage());
            } catch (IndexOutOfBoundsException e) {
                if (errors++ == 0)
                    Utils.log("%s error on record %d: %s", TITLE, count, e.getMessage());
            }
        }
    } catch (IOException e) {
        Utils.log("%s IO error: %s", TITLE, e.getMessage());
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
        if (errors != 0)
            Utils.log("%s has %d / %d error lines", TITLE, errors, count);
    }
    return localisations;
}
Also used : Pattern(java.util.regex.Pattern) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader)

Example 10 with UnicodeReader

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

the class FilterAnalysis method readFilterSets.

@SuppressWarnings("unchecked")
private List<FilterSet> readFilterSets() {
    GlobalSettings gs = SettingsManager.loadSettings();
    FilterSettings filterSettings = gs.getFilterSettings();
    String[] path = Utils.decodePath(filterSettings.filterSetFilename);
    OpenDialog chooser = new OpenDialog("Filter_File", path[0], path[1]);
    if (chooser.getFileName() != null) {
        IJ.showStatus("Reading filters ...");
        filterSettings.filterSetFilename = chooser.getDirectory() + chooser.getFileName();
        BufferedReader input = null;
        try {
            FileInputStream fis = new FileInputStream(filterSettings.filterSetFilename);
            input = new BufferedReader(new UnicodeReader(fis, null));
            Object o = XStreamWrapper.getInstance().fromXML(input);
            if (o != null && o instanceof List<?>) {
                SettingsManager.saveSettings(gs);
                return (List<FilterSet>) o;
            }
            IJ.log("No filter sets defined in the specified file: " + filterSettings.filterSetFilename);
        } catch (Exception e) {
            IJ.log("Unable to load the filter sets from file: " + e.getMessage());
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                // Ignore
                }
            }
            IJ.showStatus("");
        }
    }
    return null;
}
Also used : GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) OpenDialog(ij.io.OpenDialog) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) FilterSettings(gdsc.smlm.ij.settings.FilterSettings)

Aggregations

UnicodeReader (gdsc.core.utils.UnicodeReader)16 BufferedReader (java.io.BufferedReader)16 IOException (java.io.IOException)16 FileInputStream (java.io.FileInputStream)14 File (java.io.File)6 FileChannel (java.nio.channels.FileChannel)5 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)4 Pattern (java.util.regex.Pattern)4 GenericDialog (ij.gui.GenericDialog)3 InputMismatchException (java.util.InputMismatchException)3 LinkedList (java.util.LinkedList)3 NoSuchElementException (java.util.NoSuchElementException)3 Scanner (java.util.Scanner)3 FilterSettings (gdsc.smlm.ij.settings.FilterSettings)2 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)2 Point (java.awt.Point)2 List (java.util.List)2 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 Statistics (gdsc.core.utils.Statistics)1