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