zhipeng93 commented on code in PR #196: URL: https://github.com/apache/flink-ml/pull/196#discussion_r1066577618
########## flink-ml-lib/src/test/java/org/apache/flink/ml/feature/OnlineStandardScalerTest.java: ########## @@ -0,0 +1,396 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.ml.feature; + +import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner; +import org.apache.flink.api.common.eventtime.WatermarkStrategy; +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.api.common.time.Time; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.common.window.CountTumblingWindows; +import org.apache.flink.ml.common.window.EventTimeTumblingWindows; +import org.apache.flink.ml.common.window.GlobalWindows; +import org.apache.flink.ml.feature.standardscaler.OnlineStandardScaler; +import org.apache.flink.ml.feature.standardscaler.OnlineStandardScalerModel; +import org.apache.flink.ml.feature.standardscaler.StandardScalerModelData; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.ml.linalg.typeinfo.DenseVectorTypeInfo; +import org.apache.flink.ml.util.ReadWriteUtils; +import org.apache.flink.ml.util.TestUtils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.test.util.AbstractTestBase; +import org.apache.flink.types.Row; + +import org.apache.commons.collections.IteratorUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** Tests {@link OnlineStandardScaler} and {@link OnlineStandardScalerModel}. */ +public class OnlineStandardScalerTest extends AbstractTestBase { + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + private StreamExecutionEnvironment env; + private StreamTableEnvironment tEnv; + private final List<Row> inputData = + Arrays.asList( + Row.of(0L, Vectors.dense(-2.5, 9, 1)), + Row.of(1000L, Vectors.dense(1.4, -5, 1)), + Row.of(2000L, Vectors.dense(2, -1, -2)), + Row.of(6000L, Vectors.dense(0.7, 3, 1)), + Row.of(7000L, Vectors.dense(0, 1, 1)), + Row.of(8000L, Vectors.dense(0.5, 0, -2)), + Row.of(9000L, Vectors.dense(0.4, 1, 1)), + Row.of(10000L, Vectors.dense(0.3, 2, 1)), + Row.of(11000L, Vectors.dense(0.5, 1, -2))); + + private final List<StandardScalerModelData> expectedModelData = + Arrays.asList( + new StandardScalerModelData( + Vectors.dense(0.3, 1, 0), + Vectors.dense(2.4433583, 7.2111026, 1.7320508), + 0L, + 2999L), + new StandardScalerModelData( + Vectors.dense(0.35, 1.1666667, 0), + Vectors.dense(1.5630099, 4.6654760, 1.5491933), + 1L, + 8999L), + new StandardScalerModelData( + Vectors.dense(0.3666667, 1.2222222, 0), + Vectors.dense(1.2369316, 3.7006005, 1.5), + 2L, + 11999L)); + + private static final double TOLERANCE = 1e-7; + + private Table inputTable; + + private Table inputTableWithEventTime; + + @Before + public void before() { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.getConfig().enableObjectReuse(); + env.setParallelism(4); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + + DataStream<Row> inputStream = env.fromCollection(inputData); + inputTable = + tEnv.fromDataStream( + inputStream, + Schema.newBuilder() + .column("f0", DataTypes.BIGINT()) + .column("f1", DataTypes.RAW(DenseVectorTypeInfo.INSTANCE)) + .build()) + .as("id", "input"); + + DataStream<Row> inputStreamWithEventTime = + inputStream.assignTimestampsAndWatermarks( + WatermarkStrategy.<Row>forMonotonousTimestamps() + .withTimestampAssigner( + (SerializableTimestampAssigner<Row>) + (element, recordTimestamp) -> + element.getFieldAs(0))); + inputTableWithEventTime = + tEnv.fromDataStream( + inputStreamWithEventTime, + Schema.newBuilder() + .column("f0", DataTypes.BIGINT()) + .column("f1", DataTypes.RAW(DenseVectorTypeInfo.INSTANCE)) + .columnByMetadata("rowtime", "TIMESTAMP_LTZ(3)") + .watermark("rowtime", "SOURCE_WATERMARK()") + .build()) + .as("id", "input"); + } + + @Test + public void testParam() { + OnlineStandardScaler standardScaler = new OnlineStandardScaler(); + + assertEquals("input", standardScaler.getInputCol()); + assertEquals(false, standardScaler.getWithMean()); + assertEquals(true, standardScaler.getWithStd()); + assertEquals("output", standardScaler.getOutputCol()); + assertNull(standardScaler.getModelVersionCol()); + assertEquals(GlobalWindows.getInstance(), standardScaler.getWindows()); + assertEquals(0L, standardScaler.getMaxAllowedModelDelayMs()); + + standardScaler + .setInputCol("test_input") + .setWithMean(true) + .setWithStd(false) + .setOutputCol("test_output") + .setModelVersionCol("model_version_col") + .setWindows(EventTimeTumblingWindows.of(Time.milliseconds(3000))) + .setMaxAllowedModelDelayMs(3000L); + + assertEquals("test_input", standardScaler.getInputCol()); + assertEquals(true, standardScaler.getWithMean()); + assertEquals(false, standardScaler.getWithStd()); + assertEquals("test_output", standardScaler.getOutputCol()); + assertEquals("model_version_col", standardScaler.getModelVersionCol()); + assertEquals( + EventTimeTumblingWindows.of(Time.milliseconds(3000)), standardScaler.getWindows()); + assertEquals(3000L, standardScaler.getMaxAllowedModelDelayMs()); + } + + @Test + public void testOutputSchema() { + Table renamedTable = inputTable.as("test_id", "test_input"); + OnlineStandardScaler standardScaler = + new OnlineStandardScaler() + .setInputCol("test_input") + .setOutputCol("test_output") + .setModelVersionCol("model_version_col"); + Table output = standardScaler.fit(renamedTable).transform(renamedTable)[0]; + + assertEquals( + Arrays.asList("test_id", "test_input", "test_output", "model_version_col"), + output.getResolvedSchema().getColumnNames()); + } + + @Test + public void testFitAndPredictWithEventTimeWindow() throws Exception { + OnlineStandardScaler standardScaler = new OnlineStandardScaler(); + Table output; + int windowSizeMs = 3000; + + // Tests event time window with maxAllowedModelDelayMs as 0. + standardScaler + .setWindows(EventTimeTumblingWindows.of(Time.milliseconds(windowSizeMs))) + .setModelVersionCol("modelVersionCol"); + output = standardScaler.fit(inputTableWithEventTime).transform(inputTableWithEventTime)[0]; + verifyUsedModelVersion( Review Comment: The prediction result is undetermined because we may use new model to predict old data. So we can only check the model version here. The prediction logic is covered by other unit tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
