Tracer { public static void saveAsCsv(Vector vec, String fmt, String filePath) throws IOException { String s = mkString(vec, fmt); Files.write(Paths.get(filePath), s.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); }  static void showAscii(Vector vec, IgniteLogger log, String fmt); static void showAscii(Vector vec, IgniteLogger log); static void showAscii(Vector vec, String fmt); static String asAscii(Vector vec, String fmt, boolean showMeta); static void showAscii(Matrix mtx); static void showAscii(Matrix mtx, String fmt); static String asAscii(Matrix mtx, String fmt); static void showAscii(Matrix mtx, IgniteLogger log, String fmt); static void showAscii(Vector vec); static void saveAsCsv(Vector vec, String fmt, String filePath); static void saveAsCsv(Matrix mtx, String fmt, String filePath); static void showHtml(Matrix mtx); static void showHtml(Matrix mtx, boolean useAsciiFallback); static void showHtml(Matrix mtx, ColorMapper cm); static void showHtml(Matrix mtx, ColorMapper cm, boolean useAsciiFallback); static void showHtml(Vector vec); static void showHtml(Vector vec, boolean useAsciiFallback); static void showRegressionDatasetInHtml(String name, DataStreamGenerator generator, int limit,
        int xIdx); static void showRegressionDatasetInHtml(DataStreamGenerator generator, int limit,
        int xIdx); static void showClassificationDatasetHtml(DataStreamGenerator generator, int limit,
        int xIdx, int yIdx, boolean isLabeled); static void showClassificationDatasetHtml(String name, DataStreamGenerator generator, int limit,
        int xIdx, int yIdx, boolean isLabeled); static void showHtml(Vector vec, ColorMapper cm); static void showHtml(Vector vec, ColorMapper cm, boolean useAsciiFallback);  }
@Test public void testWriteVectorToCSVFile() throws IOException { DenseVector vector = new DenseVector(MathTestConstants.STORAGE_SIZE); for (int i = 0; i < vector.size(); i++) vector.set(i, Math.random()); Path file = createTempFile("vector", ".csv"); Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString()); System.out.println("Vector exported: " + file.getFileName()); List<String> strings = Files.readAllLines(file); Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2); String[] csvVals = reduce.orElse("").split(","); for (int i = 0; i < vector.size(); i++) { Double csvVal = Double.valueOf(csvVals[i]); assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA); } Files.deleteIfExists(file); }
@Test public void testWriteMatrixToCSVFile() throws IOException { DenseMatrix matrix = new DenseMatrix(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE); for (int i = 0; i < matrix.rowSize(); i++) for (int j = 0; j < matrix.columnSize(); j++) matrix.set(i, j, Math.random()); Path file = createTempFile("matrix", ".csv"); Tracer.saveAsCsv(matrix, DEFAULT_FORMAT, file.toString()); System.out.println("Matrix exported: " + file.getFileName()); List<String> strings = Files.readAllLines(file); Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2); String[] csvVals = reduce.orElse("").split(","); for (int i = 0; i < matrix.rowSize(); i++) for (int j = 0; j < matrix.columnSize(); j++) { Double csvVal = Double.valueOf(csvVals[i * matrix.rowSize() + j]); assertEquals("Unexpected value.", csvVal, matrix.get(i, j), DEFAULT_DELTA); } Files.deleteIfExists(file); }