Search in sources :

Example 16 with UnicodeReader

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

the class PCPALMClusters method loadHistogram.

/**
	 * Load the histogram from the file. Assumes the histogram is [int, float] format and creates a contiguous histogram
	 * from zero
	 * 
	 * @param filename
	 * @return
	 */
private HistogramData loadHistogram(String filename) {
    BufferedReader input = null;
    try {
        int f = 0;
        double a = 0;
        String u = "";
        FileInputStream fis = new FileInputStream(filename);
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int count = 0;
        ArrayList<float[]> data = new ArrayList<float[]>();
        // Read the header and store the calibration if present
        while ((line = input.readLine()) != null) {
            count++;
            if (line.length() == 0)
                continue;
            if (Character.isDigit(line.charAt(0)))
                // This is the first record
                break;
            String[] fields = line.split("[\t, ]+");
            if (fields[0].equalsIgnoreCase("frames"))
                f = Integer.parseInt(fields[1]);
            if (fields[0].equalsIgnoreCase("area"))
                a = Double.parseDouble(fields[1]);
            if (fields[0].equalsIgnoreCase("units"))
                u = fields[1];
        }
        final Pattern pattern = Pattern.compile("[\t, ]+");
        while (line != null) {
            if (line.length() == 0)
                continue;
            if (!Character.isDigit(line.charAt(0)))
                continue;
            // Extract the first 2 fields
            Scanner scanner = new Scanner(line);
            scanner.useLocale(Locale.US);
            scanner.useDelimiter(pattern);
            try {
                int molecules = scanner.nextInt();
                float frequency = scanner.nextFloat();
                // Check for duplicates
                for (float[] d : data) {
                    if (d[0] == molecules) {
                        error("Duplicate molecules field on line " + count);
                        return null;
                    }
                }
                data.add(new float[] { molecules, frequency });
            } catch (InputMismatchException e) {
                error("Incorrect fields on line " + count);
                return null;
            } catch (NoSuchElementException e) {
                error("Incorrect fields on line " + count);
                return null;
            } finally {
                scanner.close();
            }
            // Get the next line
            line = input.readLine();
            count++;
        }
        if (data.isEmpty()) {
            error("No data in file " + filename);
            return null;
        }
        // Create a contiguous histogram from zero
        int maxN = 0;
        for (float[] d : data) {
            if (maxN < d[0])
                maxN = (int) d[0];
        }
        float[][] hist = new float[2][maxN + 1];
        for (int n = 0; n <= maxN; n++) {
            hist[0][n] = n;
            for (float[] d : data) {
                if (n == d[0])
                    hist[1][n] = d[1];
            }
        }
        HistogramData histogramData = new HistogramData(hist, f, a, u);
        histogramData.filename = filename;
        return histogramData;
    } catch (IOException e) {
        IJ.error(TITLE, "Unable to read from file " + filename);
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Scanner(java.util.Scanner) ArrayList(java.util.ArrayList) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) InputMismatchException(java.util.InputMismatchException) ClusterPoint(gdsc.core.clustering.ClusterPoint) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) NoSuchElementException(java.util.NoSuchElementException)

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