Search in sources :

Example 31 with Row

use of org.apache.spark.sql.Row in project incubator-systemml by apache.

the class MLContextTest method testOutputDataFrameFromMatrixDML.

@Test
public void testOutputDataFrameFromMatrixDML() {
    System.out.println("MLContextTest - output DataFrame from matrix DML");
    String s = "M = matrix('1 2 3 4', rows=2, cols=2);";
    Script script = dml(s).out("M");
    Dataset<Row> df = ml.execute(script).getMatrix("M").toDF();
    Dataset<Row> sortedDF = df.sort(RDDConverterUtils.DF_ID_COLUMN);
    List<Row> list = sortedDF.collectAsList();
    Row row1 = list.get(0);
    Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
    Assert.assertEquals(1.0, row1.getDouble(1), 0.0);
    Assert.assertEquals(2.0, row1.getDouble(2), 0.0);
    Row row2 = list.get(1);
    Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
    Assert.assertEquals(3.0, row2.getDouble(1), 0.0);
    Assert.assertEquals(4.0, row2.getDouble(2), 0.0);
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) Row(org.apache.spark.sql.Row) Test(org.junit.Test)

Example 32 with Row

use of org.apache.spark.sql.Row in project incubator-systemml by apache.

the class MLContextTest method testOutputDataFramePYDML.

@Test
public void testOutputDataFramePYDML() {
    System.out.println("MLContextTest - output DataFrame PYDML");
    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.getDataFrame("M");
    List<Row> list = dataFrame.collectAsList();
    Row row1 = list.get(0);
    Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
    Assert.assertEquals(1.0, row1.getDouble(1), 0.0);
    Assert.assertEquals(2.0, row1.getDouble(2), 0.0);
    Row row2 = list.get(1);
    Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
    Assert.assertEquals(3.0, row2.getDouble(1), 0.0);
    Assert.assertEquals(4.0, row2.getDouble(2), 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 33 with Row

use of org.apache.spark.sql.Row 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 34 with Row

use of org.apache.spark.sql.Row 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)

Example 35 with Row

use of org.apache.spark.sql.Row in project incubator-systemml by apache.

the class MLContextTest method testOutputDataFrameDMLDoublesWithIDColumn.

@Test
public void testOutputDataFrameDMLDoublesWithIDColumn() {
    System.out.println("MLContextTest - output DataFrame DML, doubles with ID column");
    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> dataFrame = results.getDataFrameDoubleWithIDColumn("M");
    List<Row> list = dataFrame.collectAsList();
    Row row1 = list.get(0);
    Assert.assertEquals(1.0, row1.getDouble(0), 0.0);
    Assert.assertEquals(1.0, row1.getDouble(1), 0.0);
    Assert.assertEquals(2.0, row1.getDouble(2), 0.0);
    Row row2 = list.get(1);
    Assert.assertEquals(2.0, row2.getDouble(0), 0.0);
    Assert.assertEquals(3.0, row2.getDouble(1), 0.0);
    Assert.assertEquals(4.0, row2.getDouble(2), 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)

Aggregations

Row (org.apache.spark.sql.Row)129 Test (org.junit.Test)60 Script (org.apache.sysml.api.mlcontext.Script)53 StructType (org.apache.spark.sql.types.StructType)50 ArrayList (java.util.ArrayList)48 StructField (org.apache.spark.sql.types.StructField)46 SparkSession (org.apache.spark.sql.SparkSession)43 VectorUDT (org.apache.spark.ml.linalg.VectorUDT)19 MatrixMetadata (org.apache.sysml.api.mlcontext.MatrixMetadata)19 MLResults (org.apache.sysml.api.mlcontext.MLResults)18 DenseVector (org.apache.spark.ml.linalg.DenseVector)16 Vector (org.apache.spark.ml.linalg.Vector)16 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)15 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)12 SQLContext (org.apache.spark.sql.SQLContext)12 User (uk.gov.gchq.gaffer.user.User)12 HashSet (java.util.HashSet)10 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)9 Tuple2 (scala.Tuple2)9 GetDataFrameOfElements (uk.gov.gchq.gaffer.spark.operation.dataframe.GetDataFrameOfElements)9