You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by le...@apache.org on 2024/07/21 15:47:51 UTC
(iotdb) 07/12: Add
This is an automated email from the ASF dual-hosted git repository.
leirui pushed a commit to branch research/LTS-visualization
in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 33562646f9c53e92ec51358a21ad4fb15e046e84
Author: Lei Rui <10...@qq.com>
AuthorDate: Mon Jul 15 00:54:32 2024 +0800
Add
---
.../iotdb/db/query/simpiece/MySample_fsw_full.java | 198 ++++++++++++++++++++
.../simpiece/MySample_shrinkingcone_full.java | 195 ++++++++++++++++++++
.../db/query/simpiece/MySample_simpiece_full.java | 199 +++++++++++++++++++++
3 files changed, 592 insertions(+)
diff --git a/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_fsw_full.java b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_fsw_full.java
new file mode 100644
index 00000000000..e4e581488dd
--- /dev/null
+++ b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_fsw_full.java
@@ -0,0 +1,198 @@
+/*
+ * 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.
+ */
+
+// Sim-Piece code forked from https://github.com/xkitsios/Sim-Piece.git
+
+package org.apache.iotdb.db.query.simpiece;
+
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.List;
+
+public class MySample_fsw_full {
+
+ public static void main(String[] args) {
+ String fileDir = "D:\\desktop\\NISTPV\\";
+ String[] datasetNameList =
+ new String[] {
+ "NISTPV-Ground-2015-WindSpeed_ms",
+ "NISTPV-Ground-2015-Qloss_Ah",
+ "NISTPV-Ground-2015-Pyra1_Wm2",
+ "NISTPV-Ground-2015-RTD_C_3"
+ };
+ int[] noutList = new int[] {320, 360, 400, 440, 480, 520, 560, 600, 640};
+
+ double[][] epsilonArray = {
+ {
+ 9.618271350860596,
+ 9.101282596588135,
+ 8.991526126861572,
+ 8.62817907333374,
+ 8.374999523162842,
+ 8.202636241912842,
+ 7.999906063079834,
+ 7.947059154510498,
+ 7.858933925628662
+ },
+ {
+ 9.999275207519531E-4,
+ 9.989738464355469E-4,
+ 9.980201721191406E-4,
+ 9.970664978027344E-4,
+ 9.961128234863281E-4,
+ 9.951591491699219E-4,
+ 9.942054748535156E-4,
+ 9.932518005371094E-4,
+ 9.922981262207031E-4
+ },
+ {
+ 440.0235962867737,
+ 432.0247492790222,
+ 423.55674982070923,
+ 414.65001153945923,
+ 405.77113008499146,
+ 401.21820974349976,
+ 396.7935194969177,
+ 393.2065939903259,
+ 392.30953645706177
+ },
+ {
+ 9.295397281646729,
+ 8.096678256988525,
+ 7.536666393280029,
+ 6.961357593536377,
+ 6.473535060882568,
+ 6.064479351043701,
+ 5.743286609649658,
+ 5.5219340324401855,
+ 5.185042858123779
+ }
+ };
+
+ // double[][] epsilonArray = new double[datasetNameList.length][];
+ // for (int i = 0; i < datasetNameList.length; i++) {
+ // epsilonArray[i] = new double[noutList.length];
+ // }
+
+ for (int y = 0; y < datasetNameList.length; y++) {
+ String datasetName = datasetNameList[y];
+ int start = 0;
+ int end = 1000_0000;
+ int N = end - start;
+ boolean hasHeader = false;
+ try (FileInputStream inputStream = new FileInputStream(fileDir + datasetName + ".csv")) {
+ String delimiter = ",";
+ TimeSeries ts =
+ TimeSeriesReader.getMyTimeSeries(
+ inputStream, delimiter, false, N, start, hasHeader, false);
+ for (int x = 0; x < noutList.length; x++) {
+ int nout = noutList[x];
+ // double epsilon = getFSWParam(nout, ts, 1e-6);
+ // epsilonArray[y][x] = epsilon;
+
+ double epsilon = epsilonArray[y][x];
+
+ List<Point> reducedPoints = FSW.reducePoints(ts.data, epsilon);
+ System.out.println(
+ datasetName
+ + ": n="
+ + N
+ + ",m="
+ + nout
+ + ",epsilon="
+ + epsilon
+ + ",actual m="
+ + reducedPoints.size());
+ try (PrintWriter writer =
+ new PrintWriter(
+ new FileWriter(
+ datasetName
+ + "-"
+ + N
+ + "-"
+ + nout
+ + "-"
+ + reducedPoints.size()
+ + "-fsw.csv"))) {
+ for (Point p : reducedPoints) {
+ writer.println(p.getTimestamp() + "," + p.getValue());
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ for (int i = 0; i < epsilonArray.length; i++) { // 遍历行
+ for (int j = 0; j < epsilonArray[i].length; j++) { // 遍历列
+ System.out.print(epsilonArray[i][j] + ",");
+ }
+ System.out.println();
+ }
+ }
+
+ public static double getFSWParam(int nout, TimeSeries ts, double accuracy) throws IOException {
+ double epsilon = 1;
+ boolean directLess = false;
+ boolean directMore = false;
+ while (true) {
+ List<Point> reducedPoints = FSW.reducePoints(ts.data, epsilon);
+ if (reducedPoints.size() > nout) {
+ if (directMore) {
+ break;
+ }
+ if (!directLess) {
+ directLess = true;
+ }
+ epsilon *= 2;
+ } else {
+ if (directLess) {
+ break;
+ }
+ if (!directMore) {
+ directMore = true;
+ }
+ epsilon /= 2;
+ }
+ }
+ double left = 0;
+ double right = 0;
+ if (directLess) {
+ left = epsilon / 2;
+ right = epsilon;
+ }
+ if (directMore) {
+ left = epsilon;
+ right = epsilon * 2;
+ }
+ while (Math.abs(right - left) > accuracy) {
+ double mid = (left + right) / 2;
+ List<Point> reducedPoints = FSW.reducePoints(ts.data, mid);
+ if (reducedPoints.size() > nout) {
+ left = mid;
+ } else {
+ right = mid;
+ }
+ }
+ return (left + right) / 2;
+ }
+}
diff --git a/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_shrinkingcone_full.java b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_shrinkingcone_full.java
new file mode 100644
index 00000000000..d0c7aeef63d
--- /dev/null
+++ b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_shrinkingcone_full.java
@@ -0,0 +1,195 @@
+/*
+ * 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.
+ */
+
+// Sim-Piece code forked from https://github.com/xkitsios/Sim-Piece.git
+
+package org.apache.iotdb.db.query.simpiece;
+
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.List;
+
+public class MySample_shrinkingcone_full {
+
+ public static void main(String[] args) {
+ String fileDir = "D:\\desktop\\NISTPV\\";
+ String[] datasetNameList =
+ new String[] {
+ "NISTPV-Ground-2015-WindSpeed_ms",
+ "NISTPV-Ground-2015-Qloss_Ah",
+ "NISTPV-Ground-2015-Pyra1_Wm2",
+ "NISTPV-Ground-2015-RTD_C_3"
+ };
+ int[] noutList = new int[] {320, 360, 400, 440, 480, 520, 560, 600, 640};
+
+ // double[] epsilonList = new double[]{0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
+ // 0.001,
+ // 0.0009999999999999999};
+
+ double[][] epsilonArray = {
+ {
+ 14.388325214385986,
+ 13.768231868743896,
+ 13.343345165252686,
+ 13.091249942779541,
+ 12.69767427444458,
+ 12.391706943511963,
+ 12.167190074920654,
+ 11.972727298736572,
+ 11.756273746490479
+ },
+ {0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.0009999999999999999},
+ {
+ 680.2196469306946,
+ 665.2842917442322,
+ 650.2610821723938,
+ 636.7945942878723,
+ 620.9016032218933,
+ 604.7394433021545,
+ 592.9867577552795,
+ 578.6471419334412,
+ 570.5106825828552
+ },
+ {
+ 11.259880542755127,
+ 10.37297010421753,
+ 9.402754306793213,
+ 8.867334842681885,
+ 8.384315967559814,
+ 7.915340900421143,
+ 7.39516019821167,
+ 6.892223834991455,
+ 6.5178914070129395
+ }
+ };
+
+ // double[][] epsilonArray = new double[datasetNameList.length][];
+ // for (int i = 0; i < datasetNameList.length; i++) {
+ // epsilonArray[i] = new double[noutList.length];
+ // }
+
+ for (int y = 0; y < datasetNameList.length; y++) {
+ String datasetName = datasetNameList[y];
+ int start = 0;
+ int end = 1000_0000;
+ int N = end - start;
+ boolean hasHeader = false;
+ try (FileInputStream inputStream = new FileInputStream(fileDir + datasetName + ".csv")) {
+ String delimiter = ",";
+ TimeSeries ts =
+ TimeSeriesReader.getMyTimeSeries(
+ inputStream, delimiter, false, N, start, hasHeader, false);
+ for (int x = 0; x < noutList.length; x++) {
+ int nout = noutList[x];
+
+ // double epsilon = getSCParam(nout, ts, 1e-6);
+ // epsilonArray[y][x] = epsilon;
+
+ double epsilon = epsilonArray[y][x];
+
+ // double epsilon = epsilonList[x]; // 单独给Qloss手调
+
+ List<Point> reducedPoints = ShrinkingCone.reducePoints(ts.data, epsilon);
+ System.out.println(
+ datasetName
+ + ": n="
+ + N
+ + ",m="
+ + nout
+ + ",epsilon="
+ + epsilon
+ + ",actual m="
+ + reducedPoints.size());
+ try (PrintWriter writer =
+ new PrintWriter(
+ new FileWriter(
+ datasetName
+ + "-"
+ + N
+ + "-"
+ + nout
+ + "-"
+ + reducedPoints.size()
+ + "-sc.csv"))) {
+ for (Point p : reducedPoints) {
+ writer.println(p.getTimestamp() + "," + p.getValue());
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ for (int i = 0; i < epsilonArray.length; i++) { // 遍历行
+ for (int j = 0; j < epsilonArray[i].length; j++) { // 遍历列
+ System.out.print(epsilonArray[i][j] + ",");
+ }
+ System.out.println();
+ }
+ }
+
+ public static double getSCParam(int nout, TimeSeries ts, double accuracy) throws IOException {
+ double epsilon = 1;
+ boolean directLess = false;
+ boolean directMore = false;
+ while (true) {
+ List<Point> reducedPoints = ShrinkingCone.reducePoints(ts.data, epsilon);
+ if (reducedPoints.size() > nout) {
+ if (directMore) {
+ break;
+ }
+ if (!directLess) {
+ directLess = true;
+ }
+ epsilon *= 2;
+ } else {
+ if (directLess) {
+ break;
+ }
+ if (!directMore) {
+ directMore = true;
+ }
+ epsilon /= 2;
+ }
+ }
+ double left = 0;
+ double right = 0;
+ if (directLess) {
+ left = epsilon / 2;
+ right = epsilon;
+ }
+ if (directMore) {
+ left = epsilon;
+ right = epsilon * 2;
+ }
+ while (Math.abs(right - left) > accuracy) {
+ double mid = (left + right) / 2;
+ List<Point> reducedPoints = ShrinkingCone.reducePoints(ts.data, mid);
+ if (reducedPoints.size() > nout) {
+ left = mid;
+ } else {
+ right = mid;
+ }
+ }
+ return (left + right) / 2;
+ }
+}
diff --git a/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_simpiece_full.java b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_simpiece_full.java
new file mode 100644
index 00000000000..e7a2cd19c43
--- /dev/null
+++ b/server/src/main/java/org/apache/iotdb/db/query/simpiece/MySample_simpiece_full.java
@@ -0,0 +1,199 @@
+/*
+ * 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.
+ */
+
+// Sim-Piece code forked from https://github.com/xkitsios/Sim-Piece.git
+
+package org.apache.iotdb.db.query.simpiece;
+
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Comparator;
+import java.util.List;
+
+public class MySample_simpiece_full {
+
+ public static void main(String[] args) {
+ String fileDir = "D:\\desktop\\NISTPV\\";
+ String[] datasetNameList =
+ new String[] {
+ "NISTPV-Ground-2015-WindSpeed_ms",
+ "NISTPV-Ground-2015-Qloss_Ah",
+ "NISTPV-Ground-2015-Pyra1_Wm2",
+ "NISTPV-Ground-2015-RTD_C_3"
+ };
+ int[] noutList = new int[] {320, 360, 400, 440, 480, 520, 560, 600, 640};
+
+ double[][] epsilonArray = {
+ {
+ 10.470000203704831,
+ 9.973635465240477,
+ 9.571347576141356,
+ 9.339938166046142,
+ 9.208912633514405,
+ 9.135163892364506,
+ 8.744444478607178,
+ 8.61042620162964,
+ 8.422101323699948
+ },
+ {
+ 9.987499999999999E-4,
+ 9.974999999999997E-4,
+ 9.784999999999998E-4,
+ 9.4425E-4,
+ 9.255E-4,
+ 8.8375E-4,
+ 8.4775E-4,
+ 7.865E-4,
+ 7.3825E-4
+ },
+ {
+ 502.0505219552564,
+ 493.3274919509581,
+ 485.91779255051347,
+ 479.26180967157245,
+ 463.5216509033579,
+ 446.88677012006065,
+ 441.2913785749938,
+ 435.45897277584766,
+ 426.0481592410648
+ },
+ {
+ 13.678959740142822,
+ 12.275238493499756,
+ 11.224719192962645,
+ 10.475999729766848,
+ 9.741776887359617,
+ 9.125823136444094,
+ 8.625197060394289,
+ 8.105121862335203,
+ 7.743280215911865
+ }
+ };
+
+ // double[][] epsilonArray = new double[datasetNameList.length][];
+ // for (int i = 0; i < datasetNameList.length; i++) {
+ // epsilonArray[i] = new double[noutList.length];
+ // }
+
+ for (int y = 0; y < datasetNameList.length; y++) {
+ String datasetName = datasetNameList[y];
+ int start = 0;
+ int end = 1000_0000;
+ int N = end - start;
+ // apply Sim-Piece on the input file, outputting nout points saved in csvFile
+ boolean hasHeader = false;
+ try (FileInputStream inputStream = new FileInputStream(fileDir + datasetName + ".csv")) {
+ String delimiter = ",";
+ TimeSeries ts =
+ TimeSeriesReader.getMyTimeSeries(
+ inputStream, delimiter, false, N, start, hasHeader, false);
+ for (int x = 0; x < noutList.length; x++) {
+ int nout = noutList[x];
+
+ // double epsilon = getSimPieceParam(nout, ts, 1e-6);
+ // epsilonArray[y][x] = epsilon;
+
+ double epsilon = epsilonArray[y][x];
+
+ SimPiece simPiece = new SimPiece(ts.data, epsilon);
+ System.out.println(
+ datasetName
+ + ": n="
+ + N
+ + ",m="
+ + nout
+ + ",epsilon="
+ + epsilon
+ + ",actual m="
+ + simPiece.segments.size() * 2);
+ List<SimPieceSegment> segments = simPiece.segments;
+ segments.sort(Comparator.comparingLong(SimPieceSegment::getInitTimestamp));
+ try (PrintWriter writer =
+ new PrintWriter(
+ new FileWriter(
+ datasetName
+ + "-"
+ + N
+ + "-"
+ + nout
+ + "-"
+ + segments.size() * 2
+ + "-simpiece.csv"))) {
+ for (int i = 0; i < segments.size() - 1; i++) {
+ // start point of this segment
+ writer.println(segments.get(i).getInitTimestamp() + "," + segments.get(i).getB());
+ // end point of this segment
+ double v =
+ (segments.get(i + 1).getInitTimestamp() - segments.get(i).getInitTimestamp())
+ * segments.get(i).getA()
+ + segments.get(i).getB();
+ writer.println(segments.get(i + 1).getInitTimestamp() + "," + v);
+ }
+ // the two end points of the last segment
+ writer.println(
+ segments.get(segments.size() - 1).getInitTimestamp()
+ + ","
+ + segments.get(segments.size() - 1).getB());
+ double v =
+ (simPiece.lastTimeStamp - segments.get(segments.size() - 1).getInitTimestamp())
+ * segments.get(segments.size() - 1).getA()
+ + segments.get(segments.size() - 1).getB();
+ writer.println(simPiece.lastTimeStamp + "," + v);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ for (int i = 0; i < epsilonArray.length; i++) { // 遍历行
+ for (int j = 0; j < epsilonArray[i].length; j++) { // 遍历列
+ System.out.print(epsilonArray[i][j] + ",");
+ }
+ System.out.println();
+ }
+ }
+
+ public static double getSimPieceParam(int nout, TimeSeries ts, double accuracy)
+ throws IOException {
+ double epsilon = ts.range * 0.001;
+ while (true) {
+ SimPiece simPiece = new SimPiece(ts.data, epsilon);
+ if (simPiece.segments.size() * 2 > nout) { // note *2 for disjoint
+ epsilon *= 2;
+ } else {
+ break;
+ }
+ }
+ double left = epsilon / 2;
+ double right = epsilon;
+ while (Math.abs(right - left) > accuracy) {
+ double mid = (left + right) / 2;
+ SimPiece simPiece = new SimPiece(ts.data, mid);
+ if (simPiece.segments.size() * 2 > nout) { // note *2 for disjoint
+ left = mid;
+ } else {
+ right = mid;
+ }
+ }
+ return (left + right) / 2;
+ }
+}