Search in sources :

Example 1 with FloatType

use of net.imglib2.type.numeric.real.FloatType in project vcell by virtualcell.

the class VCellResultService method importCsv.

public Dataset importCsv(File directory) throws FileNotFoundException {
    File[] files = directory.listFiles();
    // TODO: Better handling
    if (files == null)
        return null;
    ArrayList<ArrayList<Float>> timeSeries = new ArrayList<>(files.length);
    Scanner scanner;
    int dataSize = 0;
    for (File file : files) {
        scanner = new Scanner(file);
        scanner.useDelimiter("[,\n]");
        while (scanner.hasNext() && !scanner.hasNextDouble()) {
            scanner.next();
        }
        if (!scanner.hasNextDouble()) {
            scanner.close();
            return null;
        }
        ArrayList<Float> data = new ArrayList<>();
        while (scanner.hasNextDouble()) {
            data.add(scanner.nextFloat());
        }
        scanner.close();
        timeSeries.add(data);
        dataSize = data.size();
    }
    int[] dimensions = { dataSize, timeSeries.size() };
    Img<FloatType> img = new ArrayImgFactory<FloatType>().create(dimensions, new FloatType());
    Cursor<FloatType> cursor = img.localizingCursor();
    while (cursor.hasNext()) {
        cursor.next();
        int xPos = cursor.getIntPosition(0);
        int tPos = cursor.getIntPosition(1);
        Float val = timeSeries.get(tPos).get(xPos);
        cursor.get().set(val);
    }
    Dataset dataset = datasetService.create(img);
    // Drop single dimensions
    @SuppressWarnings("unchecked") ImgPlus<FloatType> imgPlus = (ImgPlus<FloatType>) dataset.getImgPlus();
    FinalInterval interval = Intervals.createMinMax(0, 0, imgPlus.dimension(0) - 1, imgPlus.dimension(1) - 1);
    ImgPlus<FloatType> cropped = ops.transform().crop(imgPlus, interval, true);
    dataset.setImgPlus(cropped);
    System.out.println(dataset.numDimensions());
    dataset.setName(directory.getName());
    return dataset;
}
Also used : Scanner(java.util.Scanner) ImgPlus(net.imagej.ImgPlus) Dataset(net.imagej.Dataset) ArrayList(java.util.ArrayList) FloatType(net.imglib2.type.numeric.real.FloatType) FinalInterval(net.imglib2.FinalInterval) File(java.io.File)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1 Dataset (net.imagej.Dataset)1 ImgPlus (net.imagej.ImgPlus)1 FinalInterval (net.imglib2.FinalInterval)1 FloatType (net.imglib2.type.numeric.real.FloatType)1