Search in sources :

Example 21 with MLResults

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

the class MLContextTest method testOutputBinaryBlocksPYDML.

@Test
public void testOutputBinaryBlocksPYDML() {
    System.out.println("MLContextTest - output binary blocks PYDML");
    String s = "M = full('1 2 3 4', rows=2, cols=2);";
    MLResults results = ml.execute(pydml(s).out("M"));
    Matrix m = results.getMatrix("M");
    JavaPairRDD<MatrixIndexes, MatrixBlock> binaryBlocks = m.toBinaryBlocks();
    MatrixMetadata mm = m.getMatrixMetadata();
    MatrixCharacteristics mc = mm.asMatrixCharacteristics();
    JavaRDD<String> javaRDDStringIJV = RDDConverterUtils.binaryBlockToTextCell(binaryBlocks, mc);
    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 : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) Matrix(org.apache.sysml.api.mlcontext.Matrix) MLResults(org.apache.sysml.api.mlcontext.MLResults) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) MatrixMetadata(org.apache.sysml.api.mlcontext.MatrixMetadata) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) Test(org.junit.Test)

Example 22 with MLResults

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

the class MLContextTest method testOutputListStringCSVSparseDML.

@Test
public void testOutputListStringCSVSparseDML() {
    System.out.println("MLContextTest - output List String CSV Sparse DML");
    String s = "M = matrix(0, rows=10, cols=10); M[1,1]=1; M[1,2]=2; M[2,1]=3; M[2,2]=4; print(toString(M));";
    Script script = dml(s).out("M");
    MLResults results = ml.execute(script);
    MatrixObject mo = results.getMatrixObject("M");
    List<String> lines = MLContextConversionUtil.matrixObjectToListStringCSV(mo);
    Assert.assertEquals("1.0,2.0", lines.get(0));
    Assert.assertEquals("3.0,4.0", lines.get(1));
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) MLResults(org.apache.sysml.api.mlcontext.MLResults) Test(org.junit.Test)

Example 23 with MLResults

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

the class MLContextTest method testOutputDataFramePYDMLDoublesNoIDColumn.

@Test
public void testOutputDataFramePYDMLDoublesNoIDColumn() {
    System.out.println("MLContextTest - output DataFrame PYDML, doubles no ID column");
    String s = "M = full('1 2 3 4', rows=2, cols=2)";
    Script script = pydml(s).out("M");
    MLResults results = ml.execute(script);
    Dataset<Row> dataFrame = results.getDataFrameDoubleNoIDColumn("M");
    List<Row> list = dataFrame.collectAsList();
    Row row1 = list.get(0);
    Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
    Assert.assertEquals(2.0, row1.getDouble(1), 0.0);
    Row row2 = list.get(1);
    Assert.assertEquals(3.0, row2.getDouble(0), 0.0);
    Assert.assertEquals(4.0, row2.getDouble(1), 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) MLResults(org.apache.sysml.api.mlcontext.MLResults) Row(org.apache.spark.sql.Row) Test(org.junit.Test)

Example 24 with MLResults

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

the class MLContextFrameTest method testInputFrameAndMatrixOutputMatrixAndFrame.

@Test
public void testInputFrameAndMatrixOutputMatrixAndFrame() {
    System.out.println("MLContextFrameTest - input frame and matrix, output matrix and frame");
    Row[] rowsA = { RowFactory.create("Doc1", "Feat1", 10), RowFactory.create("Doc1", "Feat2", 20), RowFactory.create("Doc2", "Feat1", 31) };
    JavaRDD<Row> javaRddRowA = sc.parallelize(Arrays.asList(rowsA));
    List<StructField> fieldsA = new ArrayList<StructField>();
    fieldsA.add(DataTypes.createStructField("myID", DataTypes.StringType, true));
    fieldsA.add(DataTypes.createStructField("FeatureName", DataTypes.StringType, true));
    fieldsA.add(DataTypes.createStructField("FeatureValue", DataTypes.IntegerType, true));
    StructType schemaA = DataTypes.createStructType(fieldsA);
    Dataset<Row> dataFrameA = spark.createDataFrame(javaRddRowA, schemaA);
    String dmlString = "[tA, tAM] = transformencode (target = A, spec = \"{ids: false ,recode: [ myID, FeatureName ]}\");";
    Script script = dml(dmlString).in("A", dataFrameA, new FrameMetadata(FrameFormat.CSV, dataFrameA.count(), (long) dataFrameA.columns().length)).out("tA").out("tAM");
    MLResults results = ml.execute(script);
    double[][] matrixtA = results.getMatrixAs2DDoubleArray("tA");
    Assert.assertEquals(10.0, matrixtA[0][2], 0.0);
    Assert.assertEquals(20.0, matrixtA[1][2], 0.0);
    Assert.assertEquals(31.0, matrixtA[2][2], 0.0);
    Dataset<Row> dataFrame_tA = results.getMatrix("tA").toDF();
    System.out.println("Number of matrix tA rows = " + dataFrame_tA.count());
    dataFrame_tA.printSchema();
    dataFrame_tA.show();
    Dataset<Row> dataFrame_tAM = results.getFrame("tAM").toDF();
    System.out.println("Number of frame tAM rows = " + dataFrame_tAM.count());
    dataFrame_tAM.printSchema();
    dataFrame_tAM.show();
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) MLResults(org.apache.sysml.api.mlcontext.MLResults) ArrayList(java.util.ArrayList) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) CommaSeparatedValueStringToDoubleArrayRow(org.apache.sysml.test.integration.mlcontext.MLContextTest.CommaSeparatedValueStringToDoubleArrayRow) FrameMetadata(org.apache.sysml.api.mlcontext.FrameMetadata) Test(org.junit.Test)

Example 25 with MLResults

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

the class MLContextFrameTest method testTransform.

@Test
public void testTransform() {
    System.out.println("MLContextFrameTest - transform");
    Row[] rowsA = { RowFactory.create("\"`@(\"(!&", 2, "20news-bydate-train/comp.os.ms-windows.misc/9979"), RowFactory.create("\"`@(\"\"(!&\"", 3, "20news-bydate-train/comp.os.ms-windows.misc/9979") };
    JavaRDD<Row> javaRddRowA = sc.parallelize(Arrays.asList(rowsA));
    List<StructField> fieldsA = new ArrayList<StructField>();
    fieldsA.add(DataTypes.createStructField("featureName", DataTypes.StringType, true));
    fieldsA.add(DataTypes.createStructField("featureValue", DataTypes.IntegerType, true));
    fieldsA.add(DataTypes.createStructField("id", DataTypes.StringType, true));
    StructType schemaA = DataTypes.createStructType(fieldsA);
    Dataset<Row> dataFrameA = spark.createDataFrame(javaRddRowA, schemaA);
    String dmlString = "[tA, tAM] = transformencode (target = A, spec = \"{ids: false ,recode: [ featureName, id ]}\");";
    Script script = dml(dmlString).in("A", dataFrameA, new FrameMetadata(FrameFormat.CSV, dataFrameA.count(), (long) dataFrameA.columns().length)).out("tA").out("tAM");
    ml.setExplain(true);
    ml.setExplainLevel(ExplainLevel.RECOMPILE_HOPS);
    MLResults results = ml.execute(script);
    double[][] matrixtA = results.getMatrixAs2DDoubleArray("tA");
    Assert.assertEquals(1.0, matrixtA[0][2], 0.0);
    Dataset<Row> dataFrame_tA = results.getMatrix("tA").toDF();
    System.out.println("Number of matrix tA rows = " + dataFrame_tA.count());
    dataFrame_tA.printSchema();
    dataFrame_tA.show();
    Dataset<Row> dataFrame_tAM = results.getFrame("tAM").toDF();
    System.out.println("Number of frame tAM rows = " + dataFrame_tAM.count());
    dataFrame_tAM.printSchema();
    dataFrame_tAM.show();
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) MLResults(org.apache.sysml.api.mlcontext.MLResults) ArrayList(java.util.ArrayList) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) CommaSeparatedValueStringToDoubleArrayRow(org.apache.sysml.test.integration.mlcontext.MLContextTest.CommaSeparatedValueStringToDoubleArrayRow) FrameMetadata(org.apache.sysml.api.mlcontext.FrameMetadata) Test(org.junit.Test)

Aggregations

MLResults (org.apache.sysml.api.mlcontext.MLResults)51 Script (org.apache.sysml.api.mlcontext.Script)47 Test (org.junit.Test)44 Row (org.apache.spark.sql.Row)18 ArrayList (java.util.ArrayList)11 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)9 StructType (org.apache.spark.sql.types.StructType)7 MatrixMetadata (org.apache.sysml.api.mlcontext.MatrixMetadata)7 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)7 StructField (org.apache.spark.sql.types.StructField)6 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)6 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)6 FrameMetadata (org.apache.sysml.api.mlcontext.FrameMetadata)5 Matrix (org.apache.sysml.api.mlcontext.Matrix)5 List (java.util.List)4 CommaSeparatedValueStringToDoubleArrayRow (org.apache.sysml.test.integration.mlcontext.MLContextTest.CommaSeparatedValueStringToDoubleArrayRow)4 IOException (java.io.IOException)3 Seq (scala.collection.Seq)3 HashMap (java.util.HashMap)2 JavaRDD (org.apache.spark.api.java.JavaRDD)2