Search in sources :

Example 71 with Script

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

the class MLContextTest method testRDDSumCSVPYDML.

@Test
public void testRDDSumCSVPYDML() {
    System.out.println("MLContextTest - RDD<String> CSV sum PYDML");
    List<String> list = new ArrayList<String>();
    list.add("1,1,1");
    list.add("2,2,2");
    list.add("3,3,3");
    JavaRDD<String> javaRDD = sc.parallelize(list);
    RDD<String> rdd = JavaRDD.toRDD(javaRDD);
    Script script = pydml("print('sum: ' + sum(M))").in("M", rdd);
    setExpectedStdOut("sum: 18.0");
    ml.execute(script);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 72 with Script

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

the class MLContextTest method testOutputDataFrameOfVectorsDML.

@Test
public void testOutputDataFrameOfVectorsDML() {
    System.out.println("MLContextTest - output DataFrame of vectors DML");
    String s = "m=matrix('1 2 3 4',rows=2,cols=2);";
    Script script = dml(s).out("m");
    MLResults results = ml.execute(script);
    Dataset<Row> df = results.getDataFrame("m", true);
    Dataset<Row> sortedDF = df.sort(RDDConverterUtils.DF_ID_COLUMN);
    // verify column types
    StructType schema = sortedDF.schema();
    StructField[] fields = schema.fields();
    StructField idColumn = fields[0];
    StructField vectorColumn = fields[1];
    Assert.assertTrue(idColumn.dataType() instanceof DoubleType);
    Assert.assertTrue(vectorColumn.dataType() instanceof VectorUDT);
    List<Row> list = sortedDF.collectAsList();
    Row row1 = list.get(0);
    Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
    Vector v1 = (DenseVector) row1.get(1);
    double[] arr1 = v1.toArray();
    Assert.assertArrayEquals(new double[] { 1.0, 2.0 }, arr1, 0.0);
    Row row2 = list.get(1);
    Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
    Vector v2 = (DenseVector) row2.get(1);
    double[] arr2 = v2.toArray();
    Assert.assertArrayEquals(new double[] { 3.0, 4.0 }, arr2, 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) VectorUDT(org.apache.spark.ml.linalg.VectorUDT) StructType(org.apache.spark.sql.types.StructType) MLResults(org.apache.sysml.api.mlcontext.MLResults) StructField(org.apache.spark.sql.types.StructField) DoubleType(org.apache.spark.sql.types.DoubleType) Row(org.apache.spark.sql.Row) Vector(org.apache.spark.ml.linalg.Vector) DenseVector(org.apache.spark.ml.linalg.DenseVector) DenseVector(org.apache.spark.ml.linalg.DenseVector) Test(org.junit.Test)

Example 73 with Script

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

the class MLContextTest method testAddScalarIntegerInputsDML.

@Test
public void testAddScalarIntegerInputsDML() {
    System.out.println("MLContextTest - add scalar integer inputs DML");
    String s = "total = in1 + in2; print('total: ' + total);";
    Script script = dml(s).in("in1", 1).in("in2", 2);
    setExpectedStdOut("total: 3");
    ml.execute(script);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) Test(org.junit.Test)

Example 74 with Script

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

the class MLContextTest method testDisplayBooleanDML.

@Test
public void testDisplayBooleanDML() {
    System.out.println("MLContextTest - display boolean DML");
    String s = "print(b);";
    Script script = dml(s).in("b", true);
    setExpectedStdOut("TRUE");
    ml.execute(script);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) Test(org.junit.Test)

Example 75 with Script

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

the class MLContextTest method testDataFrameSumDMLDoublesWithIDColumnSortCheck.

@Test
public void testDataFrameSumDMLDoublesWithIDColumnSortCheck() {
    System.out.println("MLContextTest - DataFrame sum DML, doubles with ID column sort check");
    List<String> list = new ArrayList<String>();
    list.add("3,7,8,9");
    list.add("1,1,2,3");
    list.add("2,4,5,6");
    JavaRDD<String> javaRddString = sc.parallelize(list);
    JavaRDD<Row> javaRddRow = javaRddString.map(new CommaSeparatedValueStringToDoubleArrayRow());
    List<StructField> fields = new ArrayList<StructField>();
    fields.add(DataTypes.createStructField(RDDConverterUtils.DF_ID_COLUMN, DataTypes.DoubleType, true));
    fields.add(DataTypes.createStructField("C1", DataTypes.DoubleType, true));
    fields.add(DataTypes.createStructField("C2", DataTypes.DoubleType, true));
    fields.add(DataTypes.createStructField("C3", DataTypes.DoubleType, true));
    StructType schema = DataTypes.createStructType(fields);
    Dataset<Row> dataFrame = spark.createDataFrame(javaRddRow, schema);
    MatrixMetadata mm = new MatrixMetadata(MatrixFormat.DF_DOUBLES_WITH_INDEX);
    Script script = dml("print('M[1,1]: ' + as.scalar(M[1,1]));").in("M", dataFrame, mm);
    setExpectedStdOut("M[1,1]: 1.0");
    ml.execute(script);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) StructType(org.apache.spark.sql.types.StructType) ArrayList(java.util.ArrayList) StructField(org.apache.spark.sql.types.StructField) Row(org.apache.spark.sql.Row) MatrixMetadata(org.apache.sysml.api.mlcontext.MatrixMetadata) Test(org.junit.Test)

Aggregations

Script (org.apache.sysml.api.mlcontext.Script)206 Test (org.junit.Test)188 ArrayList (java.util.ArrayList)64 Row (org.apache.spark.sql.Row)53 MLResults (org.apache.sysml.api.mlcontext.MLResults)47 StructType (org.apache.spark.sql.types.StructType)36 StructField (org.apache.spark.sql.types.StructField)35 MatrixMetadata (org.apache.sysml.api.mlcontext.MatrixMetadata)35 DenseVector (org.apache.spark.ml.linalg.DenseVector)14 Vector (org.apache.spark.ml.linalg.Vector)14 VectorUDT (org.apache.spark.ml.linalg.VectorUDT)14 Matrix (org.apache.sysml.api.mlcontext.Matrix)13 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)13 MLContext (org.apache.sysml.api.mlcontext.MLContext)9 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)9 Tuple2 (scala.Tuple2)9 List (java.util.List)8 URL (java.net.URL)7 DMLScript (org.apache.sysml.api.DMLScript)7 Seq (scala.collection.Seq)7