ywcb00 commented on code in PR #2629:
URL: https://github.com/apache/systemds/pull/2629#discussion_r4444873529


##########
src/test/scripts/functions/builtin/exponentialSmoothing.R:
##########
@@ -0,0 +1,95 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+args <- commandArgs(trailingOnly = TRUE)
+
+if (length(args) != 2L) {
+  stop("Expected arguments: <double|triple> <output_path>")
+}
+
+mode <- args[1]
+output_path <- args[2]
+
+ts_data <- c(
+  112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118,
+  115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140,
+  145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166,
+  171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194,
+  196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201
+)

Review Comment:
   Try to pass these data from the java test class instead of hard-coding it 
inside the R script.



##########
src/main/java/org/apache/sysds/runtime/util/EMAUtils.java:
##########
@@ -72,7 +72,7 @@ public double coef() {
 public class EMAUtils {
 
        public static FrameBlock exponentialMovingAverageImputation(FrameBlock 
block, int search_iterations, String mode,
-               int freq, Double alpha, Double beta, Double gamma) {
+                                                                               
                                                int freq, Double alpha, Double 
beta, Double gamma) {

Review Comment:
   Please deactivate this formatting feature in your auto-formatter/IDE.
   According to our code style, the indent of this line was correct before.



##########
src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.sysds.runtime.util;
+
+import org.apache.sysds.runtime.matrix.data.MatrixValue;
+import org.apache.sysds.test.AutomatedTestBase;
+import org.apache.sysds.test.TestConfiguration;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+
+import static org.apache.sysds.test.TestUtils.readRMatrixFromFS;
+import static  org.apache.sysds.runtime.util.EMAUtils.*;
+
+public class EMAUtilsTest extends AutomatedTestBase {
+    private static final String TEST_NAME = "exponentialSmoothing";
+    private static final String TEST_DIR = "functions/builtin/";
+    private static final String TEST_CLASS_DIR = TEST_DIR + 
EMAUtilsTest.class.getSimpleName() + "/";
+
+    private static final double REFERENCE_EPS = 1e-4;
+    private static final Double[] TS_DATA = {
+            112., 118., 132., 129., 121., 135., 148., 148., 136., 119., 104., 
118.,
+            115., 126., 141., 135., 125., 149., 170., 170., 158., 133., 114., 
140.,
+            145., 150., 178., 163., 172., 178., 199., 199., 184., 162., 146., 
166.,
+            171., 180., 193., 181., 183., 218., 230., 242., 209., 191., 172., 
194.,
+            196., 196., 236., 235., 229., 243., 264., 272., 237., 211., 180., 
201.
+    };
+    @Override
+    public void setUp() {
+        addTestConfiguration(TEST_NAME,
+                new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] 
{ "R_double", "R_triple", "R_triple_odd" }));
+    }
+
+    @Test
+    public void testDoubleExponentialSmoothing() {
+        fullRScriptName = SCRIPT_DIR + TEST_DIR + TEST_NAME + ".R";
+        String resultPath = output("R_double");
+
+        rCmd = getRCmd("double", resultPath);
+        runRScript(true);
+
+        HashMap<MatrixValue.CellIndex, Double> rOutput = 
readRMatrixFromFS(resultPath);
+        Assert.assertFalse("R output should contain values", 
rOutput.isEmpty());
+
+        int numRows = (int) rOutput.keySet().stream().mapToLong(idx -> 
idx.row).max().orElse(0);
+        double[] rFittedValues = new double[numRows];
+        for (int i = 1; i <= numRows; i++) {
+            rFittedValues[i - 1] = rOutput.get(new MatrixValue.CellIndex(i, 
1));
+        }
+
+
+        Container javaFittedValues = double_exponential_smoothing(TS_DATA, 
0.3, 0.1);

Review Comment:
   Could we change this test to perform exponential smoothing by calling a DML 
script rather than calling the Java method directly. Examples for such tests 
can be found in `BuiltinPowerTransformTest.java` and `BuiltinEMATest.java`.
   
   Also, what is the difference between this test class and the 
`BuiltinEMATest.java`. Can we merge these two test classes?



##########
src/test/java/org/apache/sysds/runtime/util/EMAUtilsTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.sysds.runtime.util;
+
+import org.apache.sysds.runtime.matrix.data.MatrixValue;
+import org.apache.sysds.test.AutomatedTestBase;
+import org.apache.sysds.test.TestConfiguration;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+
+import static org.apache.sysds.test.TestUtils.readRMatrixFromFS;
+import static  org.apache.sysds.runtime.util.EMAUtils.*;

Review Comment:
   Try to avoid star imports (i.e., `import _.*;`). Import `EMAUtils` instead 
and and call the respective methods with the class prefix.



##########
src/test/scripts/functions/builtin/exponentialSmoothing.R:
##########
@@ -0,0 +1,95 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+args <- commandArgs(trailingOnly = TRUE)
+
+if (length(args) != 2L) {
+  stop("Expected arguments: <double|triple> <output_path>")
+}
+
+mode <- args[1]
+output_path <- args[2]
+
+ts_data <- c(
+  112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118,
+  115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140,
+  145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166,
+  171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194,
+  196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201
+)
+
+if (mode == "double") {
+  hw_model <- stats::HoltWinters(
+    ts_data,
+    alpha=0.3,
+    beta = 0.1,
+    gamma = FALSE)
+  fit_vals <- as.numeric(fitted(hw_model)[, "xhat"])
+
+  res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1)
+  res_matrix[, 1] <- fit_vals
+
+} else if (mode == "triple") {
+   ts_obj <- ts(ts_data, frequency = 12)
+
+   hw_model <- stats::HoltWinters(
+     ts_obj,
+     alpha = 0.3,
+     beta = 0.1,
+     gamma = 0.2,
+     seasonal = "additive"
+   )
+
+   fit_vals <- as.numeric(fitted(hw_model)[, "xhat"])
+
+   res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1)
+   res_matrix[, 1] <- fit_vals
+
+} else if (mode == "odd_triple") {
+     ts_obj <- ts(ts_data, frequency = 7)
+
+     hw_model <- stats::HoltWinters(
+          ts_obj,
+          alpha = 0.3,
+          beta = 0.1,
+          gamma = 0.2,
+          seasonal = "additive"
+        )
+
+     fit_vals <- as.numeric(fitted(hw_model)[, "xhat"])
+
+     res_matrix <- matrix(0, nrow = length(fit_vals), ncol = 1)
+     res_matrix[, 1] <- fit_vals
+
+  } else {
+  stop(paste("Unknown smoothing mode:", mode))
+}
+
+dir.create(
+  dirname(output_path),
+  recursive = TRUE,
+  showWarnings = FALSE
+)
+
+Matrix::writeMM(
+  Matrix::Matrix(res_matrix, sparse = TRUE),
+  file = output_path
+)

Review Comment:
   Always include a newline at the end of a file, please.



-- 
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]

Reply via email to