Search in sources :

Example 6 with OverCountWindowStreamOp

use of com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp in project Alink by alibaba.

the class LSTNetTrainBatchOpTest method testStreamMultiVar.

@Test
public void testStreamMultiVar() throws Exception {
    BatchOperator.setParallelism(1);
    final int numCols = 10;
    final String timeColName = "ts";
    final String vecColName = "vec";
    final String selectClause = "TO_TIMESTAMP(" + timeColName + ") as " + timeColName + ", " + vecColName;
    BatchOperator<?> source = new RandomTableSourceBatchOp().setNumRows(1000L).setNumCols(numCols);
    String[] selectedColNames = source.getColNames();
    AppendIdBatchOp appendIdBatchOp = new AppendIdBatchOp().setIdCol(timeColName).linkFrom(source);
    ColumnsToVectorBatchOp columnsToVectorBatchOp = new ColumnsToVectorBatchOp().setSelectedCols(selectedColNames).setVectorCol(vecColName).linkFrom(appendIdBatchOp);
    BatchOperator<?> timeBatchOp = new SelectBatchOp().setClause(selectClause).linkFrom(columnsToVectorBatchOp);
    LSTNetTrainBatchOp trainOp = new LSTNetTrainBatchOp().setVectorCol(vecColName).setTimeCol(timeColName).setWindow(24 * 7).setHorizon(12).setNumEpochs(1).linkFrom(timeBatchOp);
    StreamOperator<?> sourceStreamOp = new RandomTableSourceStreamOp().setNumCols(numCols).setMaxRows(1000L);
    ColumnsToVectorStreamOp columnsToVectorStreamOp = new ColumnsToVectorStreamOp().setSelectedCols(selectedColNames).setVectorCol(vecColName).linkFrom(sourceStreamOp);
    AppendIdStreamOp appendIdStreamOp = new AppendIdStreamOp().setIdCol(timeColName).linkFrom(columnsToVectorStreamOp);
    StreamOperator<?> timestampStreamOp = new SelectStreamOp().setClause(selectClause).linkFrom(appendIdStreamOp);
    OverCountWindowStreamOp overCountWindowStreamOp = new OverCountWindowStreamOp().setClause("MTABLE_AGG_PRECEDING(" + timeColName + ", " + vecColName + ") as col_agg").setTimeCol(timeColName).setPrecedingRows(24 * 7).linkFrom(timestampStreamOp);
    LSTNetPredictStreamOp predictStreamOp = new LSTNetPredictStreamOp(trainOp).setValueCol("col_agg").setPredictionCol("pred").setReservedCols(timeColName).linkFrom(overCountWindowStreamOp);
    FilePath tmpAkFile = new FilePath(new Path(folder.getRoot().getPath(), "lstnet_test_stream_multi_var_result.ak"));
    predictStreamOp.link(new AkSinkStreamOp().setOverwriteSink(true).setFilePath(tmpAkFile));
    StreamOperator.execute();
}
Also used : FilePath(com.alibaba.alink.common.io.filesystem.FilePath) Path(org.apache.flink.core.fs.Path) FilePath(com.alibaba.alink.common.io.filesystem.FilePath) AkSinkStreamOp(com.alibaba.alink.operator.stream.sink.AkSinkStreamOp) AppendIdStreamOp(com.alibaba.alink.operator.stream.dataproc.AppendIdStreamOp) SelectBatchOp(com.alibaba.alink.operator.batch.sql.SelectBatchOp) RandomTableSourceBatchOp(com.alibaba.alink.operator.batch.source.RandomTableSourceBatchOp) ColumnsToVectorStreamOp(com.alibaba.alink.operator.stream.dataproc.format.ColumnsToVectorStreamOp) AppendIdBatchOp(com.alibaba.alink.operator.batch.dataproc.AppendIdBatchOp) RandomTableSourceStreamOp(com.alibaba.alink.operator.stream.source.RandomTableSourceStreamOp) SelectStreamOp(com.alibaba.alink.operator.stream.sql.SelectStreamOp) OverCountWindowStreamOp(com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp) ColumnsToVectorBatchOp(com.alibaba.alink.operator.batch.dataproc.format.ColumnsToVectorBatchOp) LSTNetPredictStreamOp(com.alibaba.alink.operator.stream.timeseries.LSTNetPredictStreamOp) Test(org.junit.Test)

Example 7 with OverCountWindowStreamOp

use of com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp in project Alink by alibaba.

the class ProphetPredictStreamOpTest method testModel.

@Test
public void testModel() throws Exception {
    Row[] rowsData = new Row[] { Row.of("1", new Timestamp(117, 11, 1, 0, 0, 0, 0), 9.59076113897809), Row.of("1", new Timestamp(117, 11, 2, 0, 0, 0, 0), 8.51959031601596), Row.of("2", new Timestamp(117, 11, 3, 0, 0, 0, 0), 9.59076113897809), Row.of("1", new Timestamp(117, 11, 4, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 5, 0, 0, 0, 0), 8.51959031601596), Row.of("1", new Timestamp(117, 11, 6, 0, 0, 0, 0), 8.07246736935477), Row.of("2", new Timestamp(117, 11, 7, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 8, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 9, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 10, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 11, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 12, 0, 0, 0, 0), 8.18367658262066), Row.of("2", new Timestamp(117, 11, 13, 0, 0, 0, 0), 8.18367658262066) };
    String[] colNames = new String[] { "id", "ds1", "y1" };
    // train batch model.
    MemSourceBatchOp source = new MemSourceBatchOp(Arrays.asList(rowsData), colNames);
    ProphetTrainBatchOp model = new ProphetTrainBatchOp().setTimeCol("ds1").setValueCol("y1");
    // .setPythonCmdPath("/Library/Frameworks/Python.framework/Versions/3.7/bin/python3");
    source.link(model);
    MemSourceStreamOp streamSource = new MemSourceStreamOp(Arrays.asList(rowsData), colNames);
    OverCountWindowStreamOp over = new OverCountWindowStreamOp().setTimeCol("ds1").setPrecedingRows(4).setClause("mtable_agg_preceding(ds1,y1) as tensor");
    ProphetPredictStreamOp streamPred = new ProphetPredictStreamOp(model).setValueCol("tensor").setPredictNum(1).setPredictionCol("pred").setPredictionDetailCol("pred_detail");
    LookupVectorInTimeSeriesStreamOp valueOp = new LookupVectorInTimeSeriesStreamOp().setTimeSeriesCol("pred").setTimeCol("ds1").setReservedCols("ds1", "tensor", "pred").setOutputCol("y_hat");
    streamSource.link(over).link(streamPred).link(valueOp).print();
    StreamOperator.execute();
}
Also used : MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) OverCountWindowStreamOp(com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp) Row(org.apache.flink.types.Row) ProphetTrainBatchOp(com.alibaba.alink.operator.batch.timeseries.ProphetTrainBatchOp) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 8 with OverCountWindowStreamOp

use of com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp in project Alink by alibaba.

the class ArimaStreamOpTest method test.

@Test
public void test() throws Exception {
    List<Row> mTableData = Arrays.asList(Row.of(1, new Timestamp(1), 10.0), Row.of(1, new Timestamp(2), 11.0), Row.of(1, new Timestamp(3), 12.0), Row.of(1, new Timestamp(4), 13.0), Row.of(1, new Timestamp(5), 14.0), Row.of(1, new Timestamp(6), 15.0), Row.of(1, new Timestamp(7), 16.0), Row.of(1, new Timestamp(8), 17.0), Row.of(1, new Timestamp(9), 18.0), Row.of(1, new Timestamp(10), 19.0));
    MemSourceStreamOp source = new MemSourceStreamOp(mTableData, new String[] { "id", "ts", "val" });
    CollectSinkStreamOp resultOp = source.link(new OverCountWindowStreamOp().setPartitionCols("id").setTimeCol("ts").setPrecedingRows(5).setClause("mtable_agg_preceding(ts, val) as data")).link(new ArimaStreamOp().setValueCol("data").setOrder(new int[] { 1, 0, 1 }).setPredictNum(12).setPredictionCol("predict")).link(new LookupValueInTimeSeriesStreamOp().setTimeCol("ts").setTimeSeriesCol("predict").setOutputCol("out")).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> sResult = resultOp.getAndRemoveValues();
    sResult.sort(new RowComparator(1));
    Assert.assertEquals(10, sResult.size());
    Assert.assertEquals(13.9992, (Double) sResult.get(4).getField(5), 10e-5);
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) OverCountWindowStreamOp(com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) RowComparator(com.alibaba.alink.operator.common.dataproc.SortUtils.RowComparator) Row(org.apache.flink.types.Row) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 9 with OverCountWindowStreamOp

use of com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp in project Alink by alibaba.

the class AutoGarchStreamOpTest method test.

@Test
public void test() throws Exception {
    List<Row> mTableData = Arrays.asList(Row.of(1, new Timestamp(1), 10.0), Row.of(1, new Timestamp(2), 11.0), Row.of(1, new Timestamp(3), 12.0), Row.of(1, new Timestamp(4), 13.0), Row.of(1, new Timestamp(5), 14.0), Row.of(1, new Timestamp(6), 15.0), Row.of(1, new Timestamp(7), 16.0), Row.of(1, new Timestamp(8), 17.0), Row.of(1, new Timestamp(9), 18.0), Row.of(1, new Timestamp(10), 19.0));
    MemSourceStreamOp source = new MemSourceStreamOp(mTableData, new String[] { "id", "ts", "val" });
    CollectSinkStreamOp resultOp = source.link(new OverCountWindowStreamOp().setPartitionCols("id").setTimeCol("ts").setPrecedingRows(5).setClause("mtable_agg_preceding(ts, val) as data")).link(new AutoGarchStreamOp().setValueCol("data").setIcType("AIC").setPredictNum(10).setMaxOrder(4).setIfGARCH11(true).setMinusMean(false).setPredictionCol("predict")).link(new LookupValueInTimeSeriesStreamOp().setTimeCol("ts").setTimeSeriesCol("predict").setOutputCol("out")).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> sResult = resultOp.getAndRemoveValues();
    sResult.sort(new RowComparator(1));
    Assert.assertEquals(10, sResult.size());
    Assert.assertEquals(12.0893, (Double) sResult.get(4).getField(5), 10e-5);
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) OverCountWindowStreamOp(com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) RowComparator(com.alibaba.alink.operator.common.dataproc.SortUtils.RowComparator) Row(org.apache.flink.types.Row) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 10 with OverCountWindowStreamOp

use of com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp in project Alink by alibaba.

the class LookupVectorInTimeSeriesStreamOpTest method test.

@Test
public void test() throws Exception {
    List<Row> mTableData = Arrays.asList(Row.of(new Timestamp(1), "10.0 21.0"), Row.of(new Timestamp(2), "11.0 22.0"), Row.of(new Timestamp(3), "12.0 23.0"), Row.of(new Timestamp(4), "13.0 24.0"), Row.of(new Timestamp(5), "14.0 25.0"), Row.of(new Timestamp(6), "15.0 26.0"), Row.of(new Timestamp(7), "16.0 27.0"), Row.of(new Timestamp(8), "17.0 28.0"), Row.of(new Timestamp(9), "18.0 29.0"), Row.of(new Timestamp(10), "19.0 30.0"));
    MemSourceStreamOp source = new MemSourceStreamOp(mTableData, new String[] { "ts", "val" });
    CollectSinkStreamOp resultOp = source.link(new OverCountWindowStreamOp().setTimeCol("ts").setPrecedingRows(5).setClause("mtable_agg(ts, val) as data")).link(new ShiftStreamOp().setValueCol("data").setShiftNum(7).setPredictNum(12).setPredictionCol("predict")).link(new LookupVectorInTimeSeriesStreamOp().setTimeCol("ts").setTimeSeriesCol("predict").setOutputCol("out").setReservedCols("ts")).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> expectRows = Arrays.asList(Row.of(new Timestamp(1), null), Row.of(new Timestamp(2), VectorUtil.getVector("10.0 21.0")), Row.of(new Timestamp(3), VectorUtil.getVector("10.0 21.0")), Row.of(new Timestamp(4), VectorUtil.getVector("10.0 21.0")), Row.of(new Timestamp(5), VectorUtil.getVector("10.0 21.0")), Row.of(new Timestamp(6), VectorUtil.getVector("10.0 21.0")), Row.of(new Timestamp(7), VectorUtil.getVector("11.0 22.0")), Row.of(new Timestamp(8), VectorUtil.getVector("12.0 23.0")), Row.of(new Timestamp(9), VectorUtil.getVector("13.0 24.0")), Row.of(new Timestamp(10), VectorUtil.getVector("14.0 25.0")));
    assertListRowEqual(expectRows, resultOp.getAndRemoveValues(), 0);
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) OverCountWindowStreamOp(com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) Row(org.apache.flink.types.Row) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Aggregations

OverCountWindowStreamOp (com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp)12 Test (org.junit.Test)12 MemSourceStreamOp (com.alibaba.alink.operator.stream.source.MemSourceStreamOp)10 Row (org.apache.flink.types.Row)10 Timestamp (java.sql.Timestamp)8 CollectSinkStreamOp (com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp)6 RowComparator (com.alibaba.alink.operator.common.dataproc.SortUtils.RowComparator)5 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)3 LSTNetPredictStreamOp (com.alibaba.alink.operator.stream.timeseries.LSTNetPredictStreamOp)3 FilePath (com.alibaba.alink.common.io.filesystem.FilePath)2 AppendIdBatchOp (com.alibaba.alink.operator.batch.dataproc.AppendIdBatchOp)2 RandomTableSourceBatchOp (com.alibaba.alink.operator.batch.source.RandomTableSourceBatchOp)2 SelectBatchOp (com.alibaba.alink.operator.batch.sql.SelectBatchOp)2 AppendIdStreamOp (com.alibaba.alink.operator.stream.dataproc.AppendIdStreamOp)2 AkSinkStreamOp (com.alibaba.alink.operator.stream.sink.AkSinkStreamOp)2 RandomTableSourceStreamOp (com.alibaba.alink.operator.stream.source.RandomTableSourceStreamOp)2 SelectStreamOp (com.alibaba.alink.operator.stream.sql.SelectStreamOp)2 Path (org.apache.flink.core.fs.Path)2 ColumnsToVectorBatchOp (com.alibaba.alink.operator.batch.dataproc.format.ColumnsToVectorBatchOp)1 ProphetTrainBatchOp (com.alibaba.alink.operator.batch.timeseries.ProphetTrainBatchOp)1