Search in sources :

Example 1 with Matrix

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));
        }
    }
}
Also used : Matrix(us.hebi.matlab.mat.types.Matrix) MatFile(us.hebi.matlab.mat.types.MatFile) MatFile(us.hebi.matlab.mat.types.MatFile) File(java.io.File) Source(us.hebi.matlab.mat.types.Source) Test(org.junit.jupiter.api.Test)

Example 2 with Matrix

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);
}
Also used : Matrix(us.hebi.matlab.mat.types.Matrix)

Example 3 with Matrix

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);
    }
}
Also used : MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) Cell(us.hebi.matlab.mat.types.Cell) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatFile(us.hebi.matlab.mat.types.MatFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matrix(us.hebi.matlab.mat.types.Matrix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) IOException(java.io.IOException) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) Mat5(us.hebi.matlab.mat.format.Mat5) PlugIn(ij.plugin.PlugIn) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) MatFile(us.hebi.matlab.mat.types.MatFile) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) IOException(java.io.IOException) Cell(us.hebi.matlab.mat.types.Cell)

Example 4 with Matrix

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);
    }
}
Also used : MemoryResultsList(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.MemoryResultsList) Cell(us.hebi.matlab.mat.types.Cell) UnitConverterUtils(uk.ac.sussex.gdsc.smlm.data.config.UnitConverterUtils) IdFramePeakResultComparator(uk.ac.sussex.gdsc.smlm.results.sort.IdFramePeakResultComparator) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MatFile(us.hebi.matlab.mat.types.MatFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matrix(us.hebi.matlab.mat.types.Matrix) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) MultiDialog(uk.ac.sussex.gdsc.core.ij.gui.MultiDialog) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) PeakResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.PeakResultProcedure) SettingsManager(uk.ac.sussex.gdsc.smlm.ij.settings.SettingsManager) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) IOException(java.io.IOException) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) Logger(java.util.logging.Logger) SamplerUtils(uk.ac.sussex.gdsc.core.utils.rng.SamplerUtils) AttributePeakResult(uk.ac.sussex.gdsc.smlm.results.AttributePeakResult) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) Plot(ij.gui.Plot) TIntHashSet(gnu.trove.set.hash.TIntHashSet) TimeUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.TimeUnit) XyzrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyzrResultProcedure) List(java.util.List) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Paths(java.nio.file.Paths) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) IJ(ij.IJ) SimpleArrayUtils(uk.ac.sussex.gdsc.core.utils.SimpleArrayUtils) Mat5(us.hebi.matlab.mat.format.Mat5) PlugIn(ij.plugin.PlugIn) NormalizedGaussianSampler(org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler) TypeConverter(uk.ac.sussex.gdsc.core.data.utils.TypeConverter) LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) UniformRandomProviders(uk.ac.sussex.gdsc.core.utils.rng.UniformRandomProviders) FrameCounter(uk.ac.sussex.gdsc.smlm.results.count.FrameCounter) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) Matrix(us.hebi.matlab.mat.types.Matrix) MatFile(us.hebi.matlab.mat.types.MatFile) IOException(java.io.IOException)

Example 5 with Matrix

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));
        }
    }
}
Also used : Matrix(us.hebi.matlab.mat.types.Matrix) MatFile(us.hebi.matlab.mat.types.MatFile) Cell(us.hebi.matlab.mat.types.Cell) MatFile(us.hebi.matlab.mat.types.MatFile) File(java.io.File) Source(us.hebi.matlab.mat.types.Source) Test(org.junit.jupiter.api.Test)

Aggregations

Matrix (us.hebi.matlab.mat.types.Matrix)5 MatFile (us.hebi.matlab.mat.types.MatFile)4 TIntHashSet (gnu.trove.set.hash.TIntHashSet)2 IJ (ij.IJ)2 Plot (ij.gui.Plot)2 PlugIn (ij.plugin.PlugIn)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)2 NormalizedGaussianSampler (org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler)2 Test (org.junit.jupiter.api.Test)2 TypeConverter (uk.ac.sussex.gdsc.core.data.utils.TypeConverter)2