Search in sources :

Example 1 with Cell

use of us.hebi.matlab.mat.types.Cell 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 2 with Cell

use of us.hebi.matlab.mat.types.Cell 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

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