Search in sources :

Example 1 with BinaryBlockMatrix

use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.

the class MLContextOutputBlocksizeTest method runMLContextOutputBlocksizeTest.

private void runMLContextOutputBlocksizeTest(String format) {
    try {
        double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 76543);
        MatrixBlock mbA = DataConverter.convertToMatrixBlock(A);
        int blksz = ConfigurationManager.getBlocksize();
        MatrixCharacteristics mc = new MatrixCharacteristics(rows, cols, blksz, blksz, mbA.getNonZeros());
        //create input dataset
        JavaPairRDD<MatrixIndexes, MatrixBlock> in = SparkExecutionContext.toMatrixJavaPairRDD(sc, mbA, blksz, blksz);
        BinaryBlockMatrix bbmatrix = new BinaryBlockMatrix(in, mc);
        ml.setExplain(true);
        ml.setExplainLevel(ExplainLevel.HOPS);
        //execute script
        String s = "if( sum(X) > 0 )" + "   X = X/2;" + "R = X;" + "write(R, \"/tmp\", format=\"" + format + "\");";
        Script script = dml(s).in("X", bbmatrix).out("R");
        MLResults results = ml.execute(script);
        //compare output matrix characteristics
        MatrixCharacteristics mcOut = results.getMatrix("R").getMatrixMetadata().asMatrixCharacteristics();
        Assert.assertEquals(blksz, mcOut.getRowsPerBlock());
        Assert.assertEquals(blksz, mcOut.getColsPerBlock());
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) BinaryBlockMatrix(org.apache.sysml.api.mlcontext.BinaryBlockMatrix) MLResults(org.apache.sysml.api.mlcontext.MLResults) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 2 with BinaryBlockMatrix

use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.

the class MLContextTest method testOutputBinaryBlockMatrixPYDML.

@Test
public void testOutputBinaryBlockMatrixPYDML() {
    System.out.println("MLContextTest - output BinaryBlockMatrix PYDML");
    String s = "M = full('1 2 3 4', rows=2, cols=2);";
    BinaryBlockMatrix binaryBlockMatrix = ml.execute(pydml(s).out("M")).getBinaryBlockMatrix("M");
    JavaRDD<String> javaRDDStringIJV = MLContextConversionUtil.binaryBlockMatrixToJavaRDDStringIJV(binaryBlockMatrix);
    List<String> lines = javaRDDStringIJV.collect();
    Assert.assertEquals("1 1 1.0", lines.get(0));
    Assert.assertEquals("1 2 2.0", lines.get(1));
    Assert.assertEquals("2 1 3.0", lines.get(2));
    Assert.assertEquals("2 2 4.0", lines.get(3));
}
Also used : BinaryBlockMatrix(org.apache.sysml.api.mlcontext.BinaryBlockMatrix) Test(org.junit.Test)

Example 3 with BinaryBlockMatrix

use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.

the class MLContextTest method testInputBinaryBlockMatrixPYDML.

@Test
public void testInputBinaryBlockMatrixPYDML() {
    System.out.println("MLContextTest - input BinaryBlockMatrix PYDML");
    List<String> list = new ArrayList<String>();
    list.add("10,20,30");
    list.add("40,50,60");
    list.add("70,80,90");
    JavaRDD<String> javaRddString = sc.parallelize(list);
    JavaRDD<Row> javaRddRow = javaRddString.map(new CommaSeparatedValueStringToRow());
    List<StructField> fields = new ArrayList<StructField>();
    fields.add(DataTypes.createStructField("C1", DataTypes.StringType, true));
    fields.add(DataTypes.createStructField("C2", DataTypes.StringType, true));
    fields.add(DataTypes.createStructField("C3", DataTypes.StringType, true));
    StructType schema = DataTypes.createStructType(fields);
    Dataset<Row> dataFrame = spark.createDataFrame(javaRddRow, schema);
    BinaryBlockMatrix binaryBlockMatrix = new BinaryBlockMatrix(dataFrame);
    Script script = pydml("avg = avg(M)").in("M", binaryBlockMatrix).out("avg");
    double avg = ml.execute(script).getDouble("avg");
    Assert.assertEquals(50.0, avg, 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) ArrayList(java.util.ArrayList) BinaryBlockMatrix(org.apache.sysml.api.mlcontext.BinaryBlockMatrix) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) Test(org.junit.Test)

Example 4 with BinaryBlockMatrix

use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.

the class MLContextTest method testInputBinaryBlockMatrixDML.

@Test
public void testInputBinaryBlockMatrixDML() {
    System.out.println("MLContextTest - input BinaryBlockMatrix DML");
    List<String> list = new ArrayList<String>();
    list.add("10,20,30");
    list.add("40,50,60");
    list.add("70,80,90");
    JavaRDD<String> javaRddString = sc.parallelize(list);
    JavaRDD<Row> javaRddRow = javaRddString.map(new CommaSeparatedValueStringToRow());
    List<StructField> fields = new ArrayList<StructField>();
    fields.add(DataTypes.createStructField("C1", DataTypes.StringType, true));
    fields.add(DataTypes.createStructField("C2", DataTypes.StringType, true));
    fields.add(DataTypes.createStructField("C3", DataTypes.StringType, true));
    StructType schema = DataTypes.createStructType(fields);
    Dataset<Row> dataFrame = spark.createDataFrame(javaRddRow, schema);
    BinaryBlockMatrix binaryBlockMatrix = new BinaryBlockMatrix(dataFrame);
    Script script = dml("avg = avg(M);").in("M", binaryBlockMatrix).out("avg");
    double avg = ml.execute(script).getDouble("avg");
    Assert.assertEquals(50.0, avg, 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) ArrayList(java.util.ArrayList) BinaryBlockMatrix(org.apache.sysml.api.mlcontext.BinaryBlockMatrix) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) Test(org.junit.Test)

Example 5 with BinaryBlockMatrix

use of org.apache.sysml.api.mlcontext.BinaryBlockMatrix in project incubator-systemml by apache.

the class MLContextTest method testOutputBinaryBlockMatrixDML.

@Test
public void testOutputBinaryBlockMatrixDML() {
    System.out.println("MLContextTest - output BinaryBlockMatrix DML");
    String s = "M = matrix('1 2 3 4', rows=2, cols=2);";
    BinaryBlockMatrix binaryBlockMatrix = ml.execute(dml(s).out("M")).getBinaryBlockMatrix("M");
    JavaRDD<String> javaRDDStringIJV = MLContextConversionUtil.binaryBlockMatrixToJavaRDDStringIJV(binaryBlockMatrix);
    List<String> lines = javaRDDStringIJV.collect();
    Assert.assertEquals("1 1 1.0", lines.get(0));
    Assert.assertEquals("1 2 2.0", lines.get(1));
    Assert.assertEquals("2 1 3.0", lines.get(2));
    Assert.assertEquals("2 2 4.0", lines.get(3));
}
Also used : BinaryBlockMatrix(org.apache.sysml.api.mlcontext.BinaryBlockMatrix) Test(org.junit.Test)

Aggregations

BinaryBlockMatrix (org.apache.sysml.api.mlcontext.BinaryBlockMatrix)5 Test (org.junit.Test)4 Script (org.apache.sysml.api.mlcontext.Script)3 ArrayList (java.util.ArrayList)2 Row (org.apache.spark.sql.Row)2 StructField (org.apache.spark.sql.types.StructField)2 StructType (org.apache.spark.sql.types.StructType)2 MLResults (org.apache.sysml.api.mlcontext.MLResults)1 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)1 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)1 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)1