use of us.hebi.matlab.mat.types.Matrix in project GDSC-SMLM by aherbert.
the class TraceExporterTest method canReadWriteMatFile.
@SuppressWarnings("resource")
@Test
void canReadWriteMatFile() throws IOException {
// Write a double matrix
final int rows = 4;
final int cols = 5;
final Matrix out = Mat5.newMatrix(rows, cols);
// row, col
final int row = 3;
final int col = 4;
out.setDouble(row, col, 5.0);
Assertions.assertEquals(5.0, out.getDouble(row, col));
// From AbstractArray.getColumnMajorIndex(row, col)
// Column major index: row + rows * col
Assertions.assertEquals(5.0, out.getDouble(row + rows * col), "column major index");
// Fill using index
for (int i = 0, size = rows * cols; i < size; i++) {
out.setDouble(i, i + 1);
}
final File file = File.createTempFile("double", ".mat");
file.deleteOnExit();
final String name = "tracks";
final MatFile matFile = Mat5.newMatFile().addArray(name, out);
Mat5.writeToFile(matFile, file);
try (Source source = Sources.openFile(file)) {
final MatFile mat = Mat5.newReader(source).readMat();
final Matrix in = mat.getMatrix(name);
Assertions.assertEquals(rows, in.getNumRows());
Assertions.assertEquals(cols, in.getNumCols());
for (int i = 0, size = rows * cols; i < size; i++) {
Assertions.assertEquals(i + 1, in.getDouble(i));
}
}
}
use of us.hebi.matlab.mat.types.Matrix in project GDSC-SMLM by aherbert.
the class TraceExporter method addTrack.
/**
* Adds the track to the cell at the given row index.
*
* @param cell the output cell
* @param index the index
* @param list the list
* @param is3d true if the data is 3D, otherwise write 2D data
*/
@SuppressWarnings("resource")
private static void addTrack(Cell cell, int index, LocalList<double[]> list, boolean is3d) {
if (list.isEmpty()) {
return;
}
// Create the matrix
final int rows = list.size();
final Matrix m = Mat5.newMatrix(rows, is3d ? 4 : 3);
// Set up column offsets
final int col1 = rows * 1;
final int col2 = rows * 2;
final int col3 = rows * 3;
for (int i = 0; i < rows; i++) {
final double[] xyz = list.unsafeGet(i);
m.setDouble(i, xyz[0]);
m.setDouble(i + col1, xyz[1]);
if (is3d) {
m.setDouble(i + col2, xyz[2]);
m.setDouble(i + col3, xyz[3]);
} else {
m.setDouble(i + col2, xyz[3]);
}
}
cell.set(index, m);
}
use of us.hebi.matlab.mat.types.Matrix in project GDSC-SMLM by aherbert.
the class TraceExporter method exportVbSpt.
@SuppressWarnings("resource")
private void exportVbSpt(MemoryPeakResults results) {
// vbSPT file format:
// https://sourceforge.net/projects/vbspt/
// Matlab matrix file (.mat) containing at least one variable that is a cell
// array where each element, representing a trajectory, is a matrix
// where the rows define the coordinates in one, two or three dimensions
// in subsequent timesteps. The number of dimensions to be used for the
// analysis will be set by the runinputfile.
// The units are arbitrary but vbSPT starting estimates must be in the same
// units. Either nm or μm are recommended.
// 3 columns for n rows of localisations
// 1. x coordinate (μm)
// 2. y coordinate (μm)
// 3. z coordinate (μm)
//
// Note: An extra column is added containing the frame. This allows results to
// be uniquely identified using frame,x,y,z
// Count the IDs. Each new result ID will increment the count.
final FrameCounter idCounter = new FrameCounter(results.getFirst().getId() - 1);
results.forEach((PeakResultProcedure) result -> {
if (idCounter.advance(result.getId())) {
idCounter.increment();
}
});
// Create the cell array as 1xN
final Cell out = Mat5.newCell(1, idCounter.getCount());
// This will reset the counter to zero and ensure the current frame does not match
// in the event of a single track
idCounter.advanceAndReset(idCounter.currentFrame() + 1);
final boolean is3d = results.is3D();
// Write the tracks
final LocalList<double[]> list = new LocalList<>();
results.forEach(DistanceUnit.UM, (XyzrResultProcedure) (x, y, z, result) -> {
if (idCounter.advance(result.getId())) {
addTrack(out, idCounter.getCount() - 1, list, is3d);
idCounter.increment();
list.clear();
}
list.add(new double[] { x, y, z, result.getFrame() });
});
addTrack(out, idCounter.getCount() - 1, list, is3d);
try (MatFile matFile = Mat5.newMatFile()) {
matFile.addArray("tracks", out);
Mat5.writeToFile(matFile, Paths.get(settings.directory, results.getName() + ".mat").toFile());
} catch (final IOException ex) {
handleException(ex);
}
}
use of us.hebi.matlab.mat.types.Matrix in project GDSC-SMLM by aherbert.
the class TraceExporter method exportAnaDda.
@SuppressWarnings("resource")
private void exportAnaDda(MemoryPeakResults results) {
// anaDDA list of tracked localisations file format:
// https://github.com/HohlbeinLab/anaDDA
// Matlab matrix file.
// 5 columns for n rows of localisations
// 1. x coordinate (μm)
// 2. y coordinate (μm)
// 3. frame number
// 4. track id
// 5. frame time (s)
// Count the number of localisations including start/end frames
final Counter row = new Counter();
results.forEach((PeakResultProcedure) result -> {
row.increment(getLength(result));
});
// Create the matrix
final int rows = row.getCount();
final Matrix out = Mat5.newMatrix(rows, 5);
// Set up column offsets
final int col1 = rows * 1;
final int col2 = rows * 2;
final int col3 = rows * 3;
final int col4 = rows * 4;
// Frame time in seconds. This is not the frame time point converted to seconds
// but the exposure duration of the frame.
final double frameTime = results.getCalibrationReader().getExposureTime() / 1000;
row.reset();
results.forEach(DistanceUnit.UM, (XyrResultProcedure) (x, y, result) -> {
if (result.hasEndFrame()) {
for (int t = result.getFrame(); t <= result.getEndFrame(); t++) {
final int index = row.getAndIncrement();
out.setDouble(index, x);
out.setDouble(index + col1, y);
out.setDouble(index + col2, t);
out.setDouble(index + col3, result.getId());
out.setDouble(index + col4, frameTime);
}
} else {
// Column major index: row + rows * col
final int index = row.getAndIncrement();
out.setDouble(index, x);
out.setDouble(index + col1, y);
out.setDouble(index + col2, result.getFrame());
out.setDouble(index + col3, result.getId());
out.setDouble(index + col4, frameTime);
}
});
try (MatFile matFile = Mat5.newMatFile()) {
matFile.addArray("tracks", out);
Mat5.writeToFile(matFile, Paths.get(settings.directory, results.getName() + ".mat").toFile());
} catch (final IOException ex) {
handleException(ex);
}
}
use of us.hebi.matlab.mat.types.Matrix in project GDSC-SMLM by aherbert.
the class TraceExporterTest method canReadWriteMatCellFile.
@SuppressWarnings("resource")
@Test
void canReadWriteMatCellFile() throws IOException {
// Create a cell: cell(1,2)
final int crows = 1;
final int ccols = 2;
final Cell cell = Mat5.newCell(crows, ccols);
// Write a matrix to two cells
final int rows1 = 2;
final int cols1 = 3;
final Matrix m1 = Mat5.newMatrix(rows1, cols1);
for (int i = 0, size = rows1 * cols1; i < size; i++) {
m1.setDouble(i, i + 1);
}
final int rows2 = 4;
final int cols2 = 3;
final Matrix m2 = Mat5.newMatrix(rows2, cols2);
for (int i = 0, size = rows2 * cols2; i < size; i++) {
m2.setDouble(i, i + 10);
}
// zero-indexed not 1-indexed as per matlab
cell.set(0, 0, m1);
cell.set(0, 1, m2);
final File file = File.createTempFile("double", ".mat");
file.deleteOnExit();
final String name = "tracks";
final MatFile matFile = Mat5.newMatFile().addArray(name, cell);
Mat5.writeToFile(matFile, file);
try (Source source = Sources.openFile(file)) {
final MatFile mat = Mat5.newReader(source).readMat();
final Cell in = mat.getCell(name);
Assertions.assertEquals(crows, in.getNumRows());
Assertions.assertEquals(ccols, in.getNumCols());
final Matrix m1b = in.getMatrix(0, 0);
for (int i = 0, size = rows1 * cols1; i < size; i++) {
Assertions.assertEquals(i + 1, m1b.getDouble(i));
}
final Matrix m2b = in.getMatrix(0, 1);
for (int i = 0, size = rows2 * cols2; i < size; i++) {
Assertions.assertEquals(i + 10, m2b.getDouble(i));
}
}
}
Aggregations