You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2023/01/18 05:39:58 UTC
[iotdb] branch master updated: Add distance-based outlier detection funtion (#8892)
This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 7d2a79d779 Add distance-based outlier detection funtion (#8892)
7d2a79d779 is described below
commit 7d2a79d7791455fb4d7fa68e1043bfec3dffb163
Author: iotdb-lsmod <12...@users.noreply.github.com>
AuthorDate: Wed Jan 18 13:39:51 2023 +0800
Add distance-based outlier detection funtion (#8892)
---
.../Operators-Functions/Anomaly-Detection.md | 71 +++++++++++++++-
.../Operators-Functions/Anomaly-Detection.md | 73 +++++++++++++++-
.../apache/iotdb/libudf/it/anomaly/AnomalyIT.java | 66 +++++++++++++++
.../apache/iotdb/library/anomaly/UDTFOutlier.java | 99 ++++++++++++++++++++++
4 files changed, 307 insertions(+), 2 deletions(-)
diff --git a/docs/UserGuide/Operators-Functions/Anomaly-Detection.md b/docs/UserGuide/Operators-Functions/Anomaly-Detection.md
index c060a7f686..f5cf5358b6 100644
--- a/docs/UserGuide/Operators-Functions/Anomaly-Detection.md
+++ b/docs/UserGuide/Operators-Functions/Anomaly-Detection.md
@@ -522,4 +522,73 @@ Output series:
|1970-01-01T08:01:12.000+08:00| 950.0|
|1970-01-01T08:01:13.000+08:00| 1059.0|
+-----------------------------+------------+
-```
\ No newline at end of file
+```
+
+## Outlier
+
+### Usage
+
+This function is used to detect distance-based outliers. For each point in the current window, if the number of its neighbors within the distance of neighbor distance threshold is less than the neighbor count threshold, the point in detected as an outlier.
+
+**Name:** OUTLIER
+
+**Input Series:** Only support a single input series. The type is INT32 / INT64 / FLOAT / DOUBLE.
+
++ `r`:the neighbor distance threshold.
++ `k`:the neighbor count threshold.
++ `w`:the window size.
++ `s`:the slide size.
+
+**Output Series:** Output a single series. The type is the same as the input.
+
+### Examples
+
+#### Assigning Parameters of Queries
+
+Input series:
+
+```
++-----------------------------+------------+
+| Time|root.test.s1|
++-----------------------------+------------+
+|2020-01-04T23:59:55.000+08:00| 56.0|
+|2020-01-04T23:59:56.000+08:00| 55.1|
+|2020-01-04T23:59:57.000+08:00| 54.2|
+|2020-01-04T23:59:58.000+08:00| 56.3|
+|2020-01-04T23:59:59.000+08:00| 59.0|
+|2020-01-05T00:00:00.000+08:00| 60.0|
+|2020-01-05T00:00:01.000+08:00| 60.5|
+|2020-01-05T00:00:02.000+08:00| 64.5|
+|2020-01-05T00:00:03.000+08:00| 69.0|
+|2020-01-05T00:00:04.000+08:00| 64.2|
+|2020-01-05T00:00:05.000+08:00| 62.3|
+|2020-01-05T00:00:06.000+08:00| 58.0|
+|2020-01-05T00:00:07.000+08:00| 58.9|
+|2020-01-05T00:00:08.000+08:00| 52.0|
+|2020-01-05T00:00:09.000+08:00| 62.3|
+|2020-01-05T00:00:10.000+08:00| 61.0|
+|2020-01-05T00:00:11.000+08:00| 64.2|
+|2020-01-05T00:00:12.000+08:00| 61.8|
+|2020-01-05T00:00:13.000+08:00| 64.0|
+|2020-01-05T00:00:14.000+08:00| 63.0|
++-----------------------------+------------+
+```
+
+SQL for query:
+
+```sql
+select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test
+```
+
+Output series:
+
+```
++-----------------------------+--------------------------------------------------------+
+| Time|outlier(root.test.s1,"r"="5.0","k"="4","w"="10","s"="5")|
++-----------------------------+--------------------------------------------------------+
+|2020-01-05T00:00:03.000+08:00| 69.0|
++-----------------------------+--------------------------------------------------------+
+|2020-01-05T00:00:08.000+08:00| 52.0|
++-----------------------------+--------------------------------------------------------+
+```
+
diff --git a/docs/zh/UserGuide/Operators-Functions/Anomaly-Detection.md b/docs/zh/UserGuide/Operators-Functions/Anomaly-Detection.md
index ff56f06098..0559e34a48 100644
--- a/docs/zh/UserGuide/Operators-Functions/Anomaly-Detection.md
+++ b/docs/zh/UserGuide/Operators-Functions/Anomaly-Detection.md
@@ -528,4 +528,75 @@ select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test
|1970-01-01T08:01:12.000+08:00| 950.0|
|1970-01-01T08:01:13.000+08:00| 1059.0|
+-----------------------------+------------+
-```
\ No newline at end of file
+```
+
+## Outlier
+
+### 函数简介
+
+本函数用于检测基于距离的异常点。在当前窗口中,如果一个点距离阈值范围内的邻居数量(包括它自己)少于密度阈值,则该点是异常点。
+
+**函数名:** OUTLIER
+
+**输入序列:** 仅支持单个输入序列,类型为 INT32 / INT64 / FLOAT / DOUBLE。
+
+**参数:**
+
++ `r`:基于距离异常检测中的距离阈值。
++ `k`:基于距离异常检测中的密度阈值。
++ `w`:用于指定滑动窗口的大小。
++ `s`:用于指定滑动窗口的步长。
+
+**输出序列**:输出单个序列,类型与输入序列相同。
+
+### 使用示例
+
+#### 指定查询参数
+
+输入序列:
+
+```
++-----------------------------+------------+
+| Time|root.test.s1|
++-----------------------------+------------+
+|2020-01-04T23:59:55.000+08:00| 56.0|
+|2020-01-04T23:59:56.000+08:00| 55.1|
+|2020-01-04T23:59:57.000+08:00| 54.2|
+|2020-01-04T23:59:58.000+08:00| 56.3|
+|2020-01-04T23:59:59.000+08:00| 59.0|
+|2020-01-05T00:00:00.000+08:00| 60.0|
+|2020-01-05T00:00:01.000+08:00| 60.5|
+|2020-01-05T00:00:02.000+08:00| 64.5|
+|2020-01-05T00:00:03.000+08:00| 69.0|
+|2020-01-05T00:00:04.000+08:00| 64.2|
+|2020-01-05T00:00:05.000+08:00| 62.3|
+|2020-01-05T00:00:06.000+08:00| 58.0|
+|2020-01-05T00:00:07.000+08:00| 58.9|
+|2020-01-05T00:00:08.000+08:00| 52.0|
+|2020-01-05T00:00:09.000+08:00| 62.3|
+|2020-01-05T00:00:10.000+08:00| 61.0|
+|2020-01-05T00:00:11.000+08:00| 64.2|
+|2020-01-05T00:00:12.000+08:00| 61.8|
+|2020-01-05T00:00:13.000+08:00| 64.0|
+|2020-01-05T00:00:14.000+08:00| 63.0|
++-----------------------------+------------+
+```
+
+用于查询的 SQL 语句:
+
+```sql
+select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test
+```
+
+输出序列:
+
+```
++-----------------------------+--------------------------------------------------------+
+| Time|outlier(root.test.s1,"r"="5.0","k"="4","w"="10","s"="5")|
++-----------------------------+--------------------------------------------------------+
+|2020-01-05T00:00:03.000+08:00| 69.0|
++-----------------------------+--------------------------------------------------------+
+|2020-01-05T00:00:08.000+08:00| 52.0|
++-----------------------------+--------------------------------------------------------+
+```
+
diff --git a/integration-test/src/test/java/org/apache/iotdb/libudf/it/anomaly/AnomalyIT.java b/integration-test/src/test/java/org/apache/iotdb/libudf/it/anomaly/AnomalyIT.java
index b7e8e588f5..41f740a5ad 100644
--- a/integration-test/src/test/java/org/apache/iotdb/libudf/it/anomaly/AnomalyIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/libudf/it/anomaly/AnomalyIT.java
@@ -55,6 +55,11 @@ public class AnomalyIT {
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
statement.addBatch("create database root.vehicle");
+ statement.addBatch(
+ "create timeseries root.vehicle.d0.s0 with "
+ + "datatype=float, "
+ + "encoding=plain, "
+ + "compression=uncompressed");
statement.addBatch(
"create timeseries root.vehicle.d1.s1 with "
+ "datatype=int32, "
@@ -175,6 +180,7 @@ public class AnomalyIT {
+ "datatype=double, "
+ "encoding=plain, "
+ "compression=uncompressed");
+
statement.executeBatch();
} catch (SQLException throwable) {
fail(throwable.getMessage());
@@ -184,6 +190,46 @@ public class AnomalyIT {
private static void generateData() {
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 100, 56.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 200, 55.1));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 300, 54.2));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 400, 56.3));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 500, 60.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 600, 60.5));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 700, 64.5));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 800, 69.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 900, 64.2));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1000, 62.3));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1100, 58.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1200, 58.9));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1300, 52.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1400, 62.3));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1500, 61.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1600, 64.2));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1700, 61.8));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1800, 64.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 1900, 63.0));
+ statement.addBatch(
+ String.format("insert into root.vehicle.d0(timestamp,s0) values(%d,%f)", 2000, 59.0));
statement.addBatch(
String.format(
"insert into root.vehicle.d1(timestamp,s1,s2,s3,s4) values(%d,%d,%d,%d,%d)",
@@ -513,6 +559,8 @@ public class AnomalyIT {
statement.execute("create function range as 'org.apache.iotdb.library.anomaly.UDTFRange'");
statement.execute(
"create function TwoSidedFilter as 'org.apache.iotdb.library.anomaly.UDTFTwoSidedFilter'");
+ statement.execute(
+ "create function outlier as 'org.apache.iotdb.library.anomaly.UDTFOutlier'");
} catch (SQLException throwable) {
fail(throwable.getMessage());
}
@@ -1502,4 +1550,22 @@ public class AnomalyIT {
fail(throwable.getMessage());
}
}
+
+ @Test
+ public void testOutlier1() {
+ String sqlStr =
+ "select outlier(d0.s0, \"r\"=\"5\", \"k\"=\"4\", \"w\"=\"10\", \"s\"=\"5\") from root.vehicle";
+ try (Connection connection = EnvFactory.getEnv().getConnection();
+ Statement statement = connection.createStatement()) {
+ ResultSet resultSet = statement.executeQuery(sqlStr);
+ resultSet.next();
+ double result1 = resultSet.getDouble(2);
+ resultSet.next();
+ double result2 = resultSet.getDouble(2);
+ Assert.assertEquals(69.0, result1, 0.01);
+ Assert.assertEquals(52.0, result2, 0.01);
+ } catch (SQLException throwable) {
+ fail(throwable.getMessage());
+ }
+ }
}
diff --git a/library-udf/src/main/java/org/apache/iotdb/library/anomaly/UDTFOutlier.java b/library-udf/src/main/java/org/apache/iotdb/library/anomaly/UDTFOutlier.java
new file mode 100644
index 0000000000..a3c3c13a7e
--- /dev/null
+++ b/library-udf/src/main/java/org/apache/iotdb/library/anomaly/UDTFOutlier.java
@@ -0,0 +1,99 @@
+/*
+ * 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.iotdb.library.anomaly;
+
+import org.apache.iotdb.library.util.Util;
+import org.apache.iotdb.udf.api.UDTF;
+import org.apache.iotdb.udf.api.access.Row;
+import org.apache.iotdb.udf.api.collector.PointCollector;
+import org.apache.iotdb.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.udf.api.type.Type;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/*
+This function is used to detect distance-based anomalies.
+*/
+public class UDTFOutlier implements UDTF {
+ private int k;
+ private double r;
+ private int w;
+ private int s;
+ private int i;
+ private ArrayList<Long> currentTimeWindow = new ArrayList<>();
+ private ArrayList<Double> currentValueWindow = new ArrayList<>();
+ private Map<Long, Double> outliers = new HashMap<>();
+
+ @Override
+ public void beforeStart(UDFParameters udfParameters, UDTFConfigurations udtfConfigurations)
+ throws Exception {
+ udtfConfigurations
+ .setAccessStrategy(new RowByRowAccessStrategy())
+ .setOutputDataType(udfParameters.getDataType(0));
+ this.k = udfParameters.getIntOrDefault("k", 3);
+ this.r = udfParameters.getDoubleOrDefault("r", 5);
+ this.w = udfParameters.getIntOrDefault("w", 1000);
+ this.s = udfParameters.getIntOrDefault("s", 500);
+
+ this.i = 0;
+
+ udtfConfigurations.setAccessStrategy(new RowByRowAccessStrategy());
+ udtfConfigurations.setOutputDataType(Type.DOUBLE);
+ }
+
+ @Override
+ public void transform(Row row, PointCollector collector) throws Exception {
+ if (!row.isNull(0)) {
+ if (i >= w && (i - w) % s == 0) detect();
+
+ if (i >= w) {
+ currentValueWindow.remove(0);
+ currentTimeWindow.remove(0);
+ }
+ currentTimeWindow.add(row.getTime());
+ currentValueWindow.add(Util.getValueAsDouble(row));
+ i += 1;
+ }
+ }
+
+ @Override
+ public void terminate(PointCollector collector) throws Exception {
+ for (Long time :
+ outliers.keySet().stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList())) {
+ collector.putDouble(time, outliers.get(time));
+ }
+ }
+
+ private void detect() {
+ for (int j = 0; j < w; j++) {
+ int cnt = 0;
+ for (int l = 0; l < w; l++)
+ if (Math.abs(currentValueWindow.get(j) - currentValueWindow.get(l)) <= this.r) cnt++;
+ if (cnt < this.k && !outliers.keySet().contains(currentTimeWindow.get(j)))
+ outliers.put(currentTimeWindow.get(j), currentValueWindow.get(j));
+ }
+ }
+}