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;
}
Aggregations