Search in sources :

Example 11 with MatrixWriter

use of org.apache.sysml.runtime.io.MatrixWriter in project systemml by apache.

the class JMLCInputStreamReadTest method runJMLCInputStreamReadTest.

private void runJMLCInputStreamReadTest(DataType dt, boolean sparse, String format, boolean metaData) throws IOException {
    TestConfiguration config = getTestConfiguration(TEST_NAME);
    loadTestConfiguration(config);
    // generate inputs
    OutputInfo oinfo = format.equals("csv") ? OutputInfo.CSVOutputInfo : OutputInfo.TextCellOutputInfo;
    double[][] data = TestUtils.round(getRandomMatrix(rows, cols, 0.51, 7.49, sparse ? sparsity2 : sparsity1, 7));
    Connection conn = new Connection();
    try {
        if (dt == DataType.MATRIX) {
            // write input matrix
            MatrixBlock mb = DataConverter.convertToMatrixBlock(data);
            MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(oinfo);
            writer.writeMatrixToHDFS(mb, output("X"), rows, cols, -1, -1, -1);
            // read matrix from input stream
            FileInputStream fis = new FileInputStream(output("X"));
            double[][] data2 = conn.convertToDoubleMatrix(fis, rows, cols, format);
            fis.close();
            // compare matrix result
            TestUtils.compareMatrices(data, data2, rows, cols, 0);
        } else if (dt == DataType.FRAME) {
            // write input frame
            String[][] fdata = FrameTransformTest.createFrameData(data, "V");
            // test quoted tokens w/ inner quotes
            fdata[3][1] = "\"ab\"\"cdef\"";
            if (format.equals("csv"))
                // test delimiter and space tokens
                fdata[7][2] = "\"a,bc def\"";
            FrameBlock fb = DataConverter.convertToFrameBlock(fdata);
            if (metaData) {
                fb.setColumnNames(IntStream.range(0, cols).mapToObj(i -> "CC" + i).collect(Collectors.toList()).toArray(new String[0]));
            }
            FrameWriter writer = FrameWriterFactory.createFrameWriter(oinfo);
            writer.writeFrameToHDFS(fb, output("X"), rows, cols);
            // read frame from input stream
            FileInputStream fis = new FileInputStream(output("X"));
            String[][] fdata2 = conn.convertToStringFrame(fis, rows, cols, format);
            fis.close();
            // compare frame result
            TestUtils.compareFrames(fdata, fdata2, rows, cols);
        } else {
            throw new IOException("Unsupported data type: " + dt.name());
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        MapReduceTool.deleteFileIfExistOnHDFS(output("X"));
        IOUtilFunctions.closeSilently(conn);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) Connection(org.apache.sysml.api.jmlc.Connection) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FrameWriter(org.apache.sysml.runtime.io.FrameWriter) IOException(java.io.IOException) OutputInfo(org.apache.sysml.runtime.matrix.data.OutputInfo) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) MatrixWriter(org.apache.sysml.runtime.io.MatrixWriter)

Example 12 with MatrixWriter

use of org.apache.sysml.runtime.io.MatrixWriter in project systemml by apache.

the class FrameMatrixCastingTest method writeMatrixOrFrameInput.

private static void writeMatrixOrFrameInput(String fname, double[][] A, int rows, int cols, DataType dt, ValueType vt) throws IOException {
    int blksize = ConfigurationManager.getBlocksize();
    // write input data
    if (dt == DataType.FRAME) {
        FrameBlock fb = DataConverter.convertToFrameBlock(DataConverter.convertToMatrixBlock(A), vt);
        FrameWriter writer = FrameWriterFactory.createFrameWriter(OutputInfo.BinaryBlockOutputInfo);
        writer.writeFrameToHDFS(fb, fname, rows, cols);
    } else {
        MatrixBlock mb = DataConverter.convertToMatrixBlock(A);
        MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(OutputInfo.BinaryBlockOutputInfo);
        writer.writeMatrixToHDFS(mb, fname, (long) rows, (long) cols, blksize, blksize, -1);
    }
    // write meta data
    MatrixCharacteristics mc = new MatrixCharacteristics(rows, cols, blksize, blksize);
    MapReduceTool.writeMetaDataFile(fname + ".mtd", vt, null, dt, mc, OutputInfo.BinaryBlockOutputInfo);
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) FrameWriter(org.apache.sysml.runtime.io.FrameWriter) MatrixWriter(org.apache.sysml.runtime.io.MatrixWriter) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Aggregations

MatrixWriter (org.apache.sysml.runtime.io.MatrixWriter)12 IOException (java.io.IOException)10 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)8 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)7 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 FrameWriter (org.apache.sysml.runtime.io.FrameWriter)6 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)6 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)4 FrameReader (org.apache.sysml.runtime.io.FrameReader)3 FileInputStream (java.io.FileInputStream)2 Path (org.apache.hadoop.fs.Path)2 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)2 Connection (org.apache.sysml.api.jmlc.Connection)2 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)2 MatrixReader (org.apache.sysml.runtime.io.MatrixReader)2 JobReturn (org.apache.sysml.runtime.matrix.JobReturn)2 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)2 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)2 CSVFileFormatProperties (org.apache.sysml.runtime.matrix.data.CSVFileFormatProperties)1 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)1