-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Update 3 UDFs: Percentile, Quantile and Cluster #17375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suyx1999
wants to merge
10
commits into
apache:master
Choose a base branch
from
suyx1999:fix-udf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
978f8ff
Fix two UDFs Percentile and Quantile
suyx1999 c079ad1
Add unit test dependency in pom.xml
suyx1999 ee55d77
Remove Additional Tests
suyx1999 9b40088
Update UDTFCluster
suyx1999 81d9efb
Update DLearnIT and register-UDF
suyx1999 5f3b75e
Update library-udf/src/main/java/org/apache/iotdb/library/dlearn/util…
suyx1999 3a1ca8c
Update library-udf/src/main/java/org/apache/iotdb/library/dlearn/UDTF…
suyx1999 4584745
Update format
suyx1999 c1ddbe3
Fix Bugs in Quantile
suyx1999 1b1d69a
Fix errors in register-UDF.bat
suyx1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
library-udf/src/main/java/org/apache/iotdb/library/dlearn/UDTFCluster.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| /* | ||
| * 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.dlearn; | ||
|
|
||
| import org.apache.iotdb.library.dlearn.util.cluster.KMeans; | ||
| import org.apache.iotdb.library.dlearn.util.cluster.KShape; | ||
| import org.apache.iotdb.library.dlearn.util.cluster.MedoidShape; | ||
| 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.UDFParameterValidator; | ||
| import org.apache.iotdb.udf.api.customizer.parameter.UDFParameters; | ||
| import org.apache.iotdb.udf.api.customizer.strategy.RowByRowAccessStrategy; | ||
| import org.apache.iotdb.udf.api.exception.UDFException; | ||
| import org.apache.iotdb.udf.api.type.Type; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Clusters a time series by partitioning it into non-overlapping subsequences of length l. | ||
| * Parameters: l, k, method (default kmeans), norm, maxiter, output; medoidshape also uses | ||
| * sample_rate (greedy sampling ratio; use 1 when the window count is small). Requires at least k | ||
| * windows. | ||
| */ | ||
| public class UDTFCluster implements UDTF { | ||
|
|
||
| private static final String METHOD_KMEANS = "kmeans"; | ||
| private static final String METHOD_KSHAPE = "kshape"; | ||
| private static final String METHOD_MEDOIDSHAPE = "medoidshape"; | ||
|
|
||
| private static final String OUTPUT_LABEL = "label"; | ||
| private static final String OUTPUT_CENTROID = "centroid"; | ||
|
|
||
| private static final int DEFAULT_MAX_ITER = 200; | ||
| private static final double DEFAULT_SAMPLE_RATE = 0.3; | ||
| private static final String DEFAULT_METHOD = METHOD_KMEANS; | ||
|
|
||
| private int l; | ||
| private int k; | ||
| private String method; | ||
| private boolean norm; | ||
| private int maxIter; | ||
| private String output; | ||
| private double sampleRate; | ||
|
|
||
| private final List<Long> timestamps = new ArrayList<>(); | ||
| private final List<Double> values = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void validate(UDFParameterValidator validator) throws Exception { | ||
| validator | ||
| .validateInputSeriesNumber(1) | ||
| .validateInputSeriesDataType(0, Type.INT32, Type.INT64, Type.FLOAT, Type.DOUBLE) | ||
| .validate( | ||
| x -> (int) x > 0, | ||
| "Parameter l must be a positive integer.", | ||
| validator.getParameters().getInt("l")) | ||
| .validate( | ||
| x -> (int) x >= 2, | ||
| "Parameter k must be at least 2.", | ||
| validator.getParameters().getInt("k")) | ||
| .validate( | ||
| x -> { | ||
| String m = ((String) x).toLowerCase(); | ||
| return METHOD_KMEANS.equals(m) | ||
| || METHOD_KSHAPE.equals(m) | ||
| || METHOD_MEDOIDSHAPE.equals(m); | ||
| }, | ||
| "Parameter method must be one of: kmeans, kshape, medoidshape.", | ||
| validator.getParameters().getStringOrDefault("method", DEFAULT_METHOD)) | ||
| .validate( | ||
| x -> (int) x >= 1, | ||
| "Parameter maxiter must be a positive integer.", | ||
| validator.getParameters().getIntOrDefault("maxiter", DEFAULT_MAX_ITER)) | ||
| .validate( | ||
| x -> { | ||
| String o = ((String) x).toLowerCase(); | ||
| return OUTPUT_LABEL.equals(o) || OUTPUT_CENTROID.equals(o); | ||
| }, | ||
| "Parameter output must be label or centroid.", | ||
| validator.getParameters().getStringOrDefault("output", OUTPUT_LABEL)) | ||
| .validate( | ||
| x -> { | ||
| double d = ((Number) x).doubleValue(); | ||
| return d > 0 && d <= 1.0; | ||
| }, | ||
| "Parameter sample_rate must be in (0, 1].", | ||
| validator.getParameters().getDoubleOrDefault("sample_rate", DEFAULT_SAMPLE_RATE)); | ||
| } | ||
|
|
||
| @Override | ||
| public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations) | ||
| throws Exception { | ||
| this.output = parameters.getStringOrDefault("output", OUTPUT_LABEL).toLowerCase(); | ||
| if (OUTPUT_CENTROID.equals(output)) { | ||
| configurations.setAccessStrategy(new RowByRowAccessStrategy()).setOutputDataType(Type.DOUBLE); | ||
| } else { | ||
| configurations.setAccessStrategy(new RowByRowAccessStrategy()).setOutputDataType(Type.INT32); | ||
| } | ||
| this.l = parameters.getInt("l"); | ||
| this.k = parameters.getInt("k"); | ||
| this.method = parameters.getStringOrDefault("method", DEFAULT_METHOD).toLowerCase(); | ||
| this.norm = parameters.getBooleanOrDefault("norm", true); | ||
| this.maxIter = parameters.getIntOrDefault("maxiter", DEFAULT_MAX_ITER); | ||
| this.sampleRate = parameters.getDoubleOrDefault("sample_rate", DEFAULT_SAMPLE_RATE); | ||
| timestamps.clear(); | ||
| values.clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(Row row, PointCollector collector) throws Exception { | ||
| if (!row.isNull(0)) { | ||
| timestamps.add(row.getTime()); | ||
| values.add(Util.getValueAsDouble(row)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void terminate(PointCollector collector) throws Exception { | ||
| int n = values.size(); | ||
| if (n < l) { | ||
| throw new UDFException( | ||
| "Time series length must be at least l; got " + n + " points, l=" + l + "."); | ||
| } | ||
| int numWindows = n / l; | ||
| if (numWindows < k) { | ||
| throw new UDFException( | ||
| "Not enough non-overlapping windows: got " | ||
| + numWindows | ||
| + " windows, need at least k=" | ||
| + k | ||
| + "."); | ||
| } | ||
|
|
||
| double[][] windows = new double[numWindows][l]; | ||
| long[] windowStartTime = new long[numWindows]; | ||
| for (int w = 0; w < numWindows; w++) { | ||
| windowStartTime[w] = timestamps.get(w * l); | ||
| for (int j = 0; j < l; j++) { | ||
| windows[w][j] = values.get(w * l + j); | ||
| } | ||
| } | ||
|
|
||
| if (OUTPUT_LABEL.equals(output)) { | ||
| int[] labels; | ||
| if (METHOD_KMEANS.equals(method)) { | ||
| KMeans km = new KMeans(); | ||
| km.fit(windows, k, norm, maxIter); | ||
| labels = km.getLabels(); | ||
| } else if (METHOD_KSHAPE.equals(method)) { | ||
| KShape ks = new KShape(); | ||
| ks.fit(windows, k, norm, maxIter); | ||
| labels = ks.getLabels(); | ||
| } else if (METHOD_MEDOIDSHAPE.equals(method)) { | ||
| MedoidShape ms = new MedoidShape(); | ||
| ms.setSampleRate(sampleRate); | ||
| ms.fit(windows, k, norm, maxIter); | ||
| labels = ms.getLabels(); | ||
| } else { | ||
| throw new UDFException("Unsupported method: " + method); | ||
| } | ||
| for (int w = 0; w < numWindows; w++) { | ||
| collector.putInt(windowStartTime[w], labels[w]); | ||
| } | ||
| } else { | ||
| double[][] centroids; | ||
| if (METHOD_KMEANS.equals(method)) { | ||
| KMeans km = new KMeans(); | ||
| km.fit(windows, k, norm, maxIter); | ||
| centroids = km.getCentroids(); | ||
| } else if (METHOD_KSHAPE.equals(method)) { | ||
| KShape ks = new KShape(); | ||
| ks.fit(windows, k, norm, maxIter); | ||
| centroids = ks.getCentroids(); | ||
| } else if (METHOD_MEDOIDSHAPE.equals(method)) { | ||
| MedoidShape ms = new MedoidShape(); | ||
| ms.setSampleRate(sampleRate); | ||
| ms.fit(windows, k, norm, maxIter); | ||
| centroids = ms.getCentroids(); | ||
| } else { | ||
| throw new UDFException("Unsupported method: " + method); | ||
| } | ||
| emitConcatenatedCentroids(collector, centroids); | ||
| } | ||
| } | ||
|
|
||
| private static void emitConcatenatedCentroids(PointCollector collector, double[][] centroids) | ||
| throws Exception { | ||
| long t = 0L; | ||
| for (double[] row : centroids) { | ||
| for (double v : row) { | ||
| collector.putDouble(t++, v); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.