zhipeng93 commented on code in PR #148: URL: https://github.com/apache/flink-ml/pull/148#discussion_r960328621
########## flink-ml-lib/src/test/java/org/apache/flink/ml/clustering/AgglomerativeClusteringTest.java: ########## @@ -0,0 +1,312 @@ +/* + * 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.clustering; + +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.ml.clustering.agglomerativeclustering.AgglomerativeClustering; +import org.apache.flink.ml.clustering.agglomerativeclustering.AgglomerativeClusteringParams; +import org.apache.flink.ml.common.distance.CosineDistanceMeasure; +import org.apache.flink.ml.common.distance.EuclideanDistanceMeasure; +import org.apache.flink.ml.common.distance.ManhattanDistanceMeasure; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.ml.linalg.Vectors; +import org.apache.flink.ml.util.TestUtils; +import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +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.Arrays; +import java.util.Comparator; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** Tests {@link AgglomerativeClustering}. */ +public class AgglomerativeClusteringTest extends AbstractTestBase { + + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + private StreamTableEnvironment tEnv; + private StreamExecutionEnvironment env; + private Table inputDataTable; + + private static final List<DenseVector> INPUT_DATA = + Arrays.asList( + Vectors.dense(1, 1), + Vectors.dense(1, 4), + Vectors.dense(1, 0), + Vectors.dense(4, 1.5), + Vectors.dense(4, 4), + Vectors.dense(4, 0)); + + private static final List<Row> EUCLIDEAN_AVERAGE_MERGE_INFO = + Arrays.asList( + Row.of(0, 2, 1, 2), + Row.of(3, 5, 1.5, 2), + Row.of(1, 4, 3, 2), + Row.of(6, 7, 3.1394402, 4), + Row.of(8, 9, 3.9559706, 6)); + + private static final List<Row> COSINE_AVERAGE_MERGE_INFO = + Arrays.asList( + Row.of(2, 5, 0, 2), + Row.of(0, 4, 1.1102230E-16, 2), + Row.of(3, 6, 0.0636708, 3), + Row.of(1, 7, 0.1425070, 3), + Row.of(8, 9, 0.3664484, 6)); + + private static final List<Row> MANHATTAN_AVERAGE_MERGE_INFO = + Arrays.asList( + Row.of(0, 2, 1, 2), + Row.of(3, 5, 1.5, 2), + Row.of(1, 4, 3, 2), + Row.of(6, 7, 3.75, 4), + Row.of(8, 9, 4.875, 6)); + + private static final List<Row> EUCLIDEAN_SINGLE_MERGE_INFO = + Arrays.asList( + Row.of(0, 2, 1, 2), + Row.of(3, 5, 1.5, 2), + Row.of(4, 7, 2.5, 3), + Row.of(8, 9, 3, 6), + Row.of(1, 6, 3, 3)); + + private static final List<Row> EUCLIDEAN_WARD_MERGE_INFO = + Arrays.asList( + Row.of(0, 2, 1, 2), + Row.of(3, 5, 1.5, 2), + Row.of(1, 4, 3, 2), + Row.of(6, 7, 4.2573465, 4), + Row.of(8, 9, 5.5113519, 6)); + + private static final List<Row> EUCLIDEAN_COMPLETE_MERGE_INFO = + Arrays.asList( + Row.of(0, 2, 1, 2), + Row.of(3, 5, 1.5, 2), + Row.of(1, 4, 3, 2), + Row.of(6, 7, 3.3541019, 4), + Row.of(8, 9, 5, 6)); + + private static final List<Row> EUCLIDEAN_WARD_K_AS_TWO_RESULT = + Arrays.asList( + Row.of(Vectors.dense(1, 1), 0), + Row.of(Vectors.dense(1, 4), 1), + Row.of(Vectors.dense(1, 0), 0), + Row.of(Vectors.dense(4, 1.5), 0), + Row.of(Vectors.dense(4, 4), 1), + Row.of(Vectors.dense(4, 0), 0)); + + private static final List<Row> EUCLIDEAN_WARD_THRESHOLD_AS_TWO_RESULT = + Arrays.asList( + Row.of(Vectors.dense(1, 1), 0), + Row.of(Vectors.dense(1, 4), 1), + Row.of(Vectors.dense(1, 0), 0), + Row.of(Vectors.dense(4, 1.5), 2), + Row.of(Vectors.dense(4, 4), 1), + Row.of(Vectors.dense(4, 0), 2)); + + private static final double TOLERANCE = 1e-7; + + @Before + public void before() { + Configuration config = new Configuration(); + config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true); + env = StreamExecutionEnvironment.getExecutionEnvironment(config); + env.setParallelism(3); + env.enableCheckpointing(100); + env.setRestartStrategy(RestartStrategies.noRestart()); + tEnv = StreamTableEnvironment.create(env); + inputDataTable = tEnv.fromDataStream(env.fromCollection(INPUT_DATA)).as("features"); Review Comment: Good catch. I made a mistake here --- the mergeInfo should be different in different runs and only the distances should be the same. I have added a `map` after `env.fromCollection()` to force it as a parallel stream and also re-wrote the corresponding unit test. -- 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]
