Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.ml.feature;
0019 
0020 import java.util.Arrays;
0021 import java.util.List;
0022 
0023 import org.jtransforms.dct.DoubleDCT_1D;
0024 
0025 import org.junit.Assert;
0026 import org.junit.Test;
0027 
0028 import org.apache.spark.SharedSparkSession;
0029 import org.apache.spark.ml.linalg.Vector;
0030 import org.apache.spark.ml.linalg.VectorUDT;
0031 import org.apache.spark.ml.linalg.Vectors;
0032 import org.apache.spark.sql.Dataset;
0033 import org.apache.spark.sql.Row;
0034 import org.apache.spark.sql.RowFactory;
0035 import org.apache.spark.sql.types.Metadata;
0036 import org.apache.spark.sql.types.StructField;
0037 import org.apache.spark.sql.types.StructType;
0038 
0039 public class JavaDCTSuite extends SharedSparkSession {
0040 
0041   @Test
0042   public void javaCompatibilityTest() {
0043     double[] input = new double[]{1D, 2D, 3D, 4D};
0044     Dataset<Row> dataset = spark.createDataFrame(
0045       Arrays.asList(RowFactory.create(Vectors.dense(input))),
0046       new StructType(new StructField[]{
0047         new StructField("vec", (new VectorUDT()), false, Metadata.empty())
0048       }));
0049 
0050     double[] expectedResult = input.clone();
0051     (new DoubleDCT_1D(input.length)).forward(expectedResult, true);
0052 
0053     DCT dct = new DCT()
0054       .setInputCol("vec")
0055       .setOutputCol("resultVec");
0056 
0057     List<Row> result = dct.transform(dataset).select("resultVec").collectAsList();
0058     Vector resultVec = result.get(0).getAs("resultVec");
0059 
0060     Assert.assertArrayEquals(expectedResult, resultVec.toArray(), 1e-6);
0061   }
0062 }