You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jb...@apache.org on 2018/10/03 16:29:48 UTC
lucene-solr:branch_7x: SOLR-12828: Add oscillate Stream Evaluator to
support sine wave analysis
Repository: lucene-solr
Updated Branches:
refs/heads/branch_7x ece429122 -> fe7121862
SOLR-12828: Add oscillate Stream Evaluator to support sine wave analysis
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/fe712186
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/fe712186
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/fe712186
Branch: refs/heads/branch_7x
Commit: fe71218621989cd916bd3ce969a2ba11a85578f2
Parents: ece4291
Author: Joel Bernstein <jb...@apache.org>
Authored: Wed Oct 3 12:24:48 2018 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Wed Oct 3 12:27:00 2018 -0400
----------------------------------------------------------------------
.../org/apache/solr/client/solrj/io/Lang.java | 6 ++-
.../solrj/io/eval/GetAmplitudeEvaluator.java | 42 +++++++++++++++
.../io/eval/GetAngularFrequencyEvaluator.java | 42 +++++++++++++++
.../client/solrj/io/eval/GetPhaseEvaluator.java | 42 +++++++++++++++
.../solrj/io/eval/HarmonicFitEvaluator.java | 11 +++-
.../solrj/io/eval/OscillateEvaluator.java | 57 ++++++++++++++++++++
.../apache/solr/client/solrj/io/TestLang.java | 5 +-
.../solrj/io/stream/MathExpressionTest.java | 42 ++++++++++++++-
8 files changed, 242 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
index 17f9d57..02968b2 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/Lang.java
@@ -174,6 +174,7 @@ public class Lang {
.withFunctionName("betaDistribution", BetaDistributionEvaluator.class)
.withFunctionName("polyfit", PolyFitEvaluator.class)
.withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
+ .withFunctionName("harmfit", HarmonicFitEvaluator.class)
.withFunctionName("loess", LoessEvaluator.class)
.withFunctionName("matrix", MatrixEvaluator.class)
.withFunctionName("transpose", TransposeEvaluator.class)
@@ -261,7 +262,10 @@ public class Lang {
.withFunctionName("getBaryCenter", GetBaryCenterEvaluator.class)
.withFunctionName("getArea", GetAreaEvaluator.class)
.withFunctionName("getBoundarySize", GetBoundarySizeEvaluator.class)
-
+ .withFunctionName("oscillate", OscillateEvaluator.class)
+ .withFunctionName("getAmplitude", GetAmplitudeEvaluator.class)
+ .withFunctionName("getPhase", GetPhaseEvaluator.class)
+ .withFunctionName("getAngularFrequency", GetAngularFrequencyEvaluator.class)
// Boolean Stream Evaluators
.withFunctionName("and", AndEvaluator.class)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java
new file mode 100644
index 0000000..131029b
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAmplitudeEvaluator.java
@@ -0,0 +1,42 @@
+/*
+ * 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.solr.client.solrj.io.eval;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetAmplitudeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+ private static final long serialVersionUID = 1;
+
+ public GetAmplitudeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object value) throws IOException {
+ if(!(value instanceof VectorFunction)){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
+ } else {
+ VectorFunction vectorFunction = (VectorFunction)value;
+ return vectorFunction.getFromContext("amplitude");
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java
new file mode 100644
index 0000000..001e95e
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetAngularFrequencyEvaluator.java
@@ -0,0 +1,42 @@
+/*
+ * 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.solr.client.solrj.io.eval;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetAngularFrequencyEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+ private static final long serialVersionUID = 1;
+
+ public GetAngularFrequencyEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object value) throws IOException {
+ if(!(value instanceof VectorFunction)){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
+ } else {
+ VectorFunction vectorFunction = (VectorFunction)value;
+ return vectorFunction.getFromContext("angularFrequency");
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java
new file mode 100644
index 0000000..546ef20
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GetPhaseEvaluator.java
@@ -0,0 +1,42 @@
+/*
+ * 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.solr.client.solrj.io.eval;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GetPhaseEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
+ private static final long serialVersionUID = 1;
+
+ public GetPhaseEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object value) throws IOException {
+ if(!(value instanceof VectorFunction)){
+ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
+ } else {
+ VectorFunction vectorFunction = (VectorFunction)value;
+ return vectorFunction.getFromContext("phase");
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java
index 04c0b0c..4e7ac6e 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java
@@ -70,6 +70,9 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements M
points.add(x[i], y[i]);
}
+ double[] guess = new HarmonicCurveFitter.ParameterGuesser(points.toList()).guess();
+ curveFitter = curveFitter.withStartPoint(guess);
+
double[] coef = curveFitter.fit(points.toList());
HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);
@@ -79,6 +82,12 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements M
list.add(yvalue);
}
- return list;
+ VectorFunction vectorFunction = new VectorFunction(pf, list);
+ vectorFunction.addToContext("amplitude", coef[0]);
+ vectorFunction.addToContext("angularFrequency", coef[1]);
+ vectorFunction.addToContext("phase", coef[2]);
+
+ return vectorFunction;
+
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.java
new file mode 100644
index 0000000..6fdd3c5
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OscillateEvaluator.java
@@ -0,0 +1,57 @@
+/*
+ * 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.solr.client.solrj.io.eval;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.math3.analysis.function.HarmonicOscillator;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class OscillateEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public OscillateEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object... objects) throws IOException{
+
+ if(objects.length != 3) {
+ throw new IOException("The oscillate function takes 3 arguments.");
+ }
+
+ double amp = ((Number)objects[0]).doubleValue();
+ double om = ((Number)objects[1]).doubleValue();
+ double phase = ((Number)objects[2]).doubleValue();
+
+
+ HarmonicOscillator pf = new HarmonicOscillator(amp, om, phase);
+
+ List list = new ArrayList();
+ for(int i=0; i<128; i++) {
+ double yvalue= pf.value(i);
+ list.add(yvalue);
+ }
+
+ return new VectorFunction(pf, list);
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
index bc15e02..169fead 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/TestLang.java
@@ -52,7 +52,7 @@ public class TestLang extends LuceneTestCase {
"poissonDistribution", "enumeratedDistribution", "probability", "sumDifference", "meanDifference",
"primes", "factorial", "movingMedian", "binomialCoefficient", "expMovingAvg", "monteCarlo", "constantDistribution",
"weibullDistribution", "mean", "mode", "logNormalDistribution", "zipFDistribution", "gammaDistribution",
- "betaDistribution", "polyfit", "harmonicFit", "loess", "matrix", "transpose", "unitize",
+ "betaDistribution", "polyfit", "harmonicFit", "harmfit", "loess", "matrix", "transpose", "unitize",
"triangularDistribution", "precision", "minMaxScale", "markovChain", "grandSum",
"scalarAdd", "scalarSubtract", "scalarMultiply", "scalarDivide", "sumRows",
"sumColumns", "diff", "corrPValues", "normalizeSum", "geometricDistribution", "olsRegress",
@@ -71,7 +71,8 @@ public class TestLang extends LuceneTestCase {
"cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan",
"earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress", "gaussfit",
"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",
- "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize"};
+ "convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
+ "getAmplitude", "getPhase", "getAngularFrequency"};
@Test
public void testLang() {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fe712186/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
index 67b0e08..73aa0c9 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/MathExpressionTest.java
@@ -342,7 +342,6 @@ public class MathExpressionTest extends SolrCloudTestCase {
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
List<List<Number>>locVectors = (List<List<Number>>)tuples.get(0).get("b");
- System.out.println(locVectors);
int v=1;
for(List<Number> row : locVectors) {
double lat = row.get(0).doubleValue();
@@ -2360,6 +2359,46 @@ public class MathExpressionTest extends SolrCloudTestCase {
}
@Test
+ public void testOscillate() throws Exception {
+ String cexpr = "let(echo=true," +
+ " a=oscillate(10, .3, 2.9)," +
+ " b=describe(a)," +
+ " c=getValue(b, min)," +
+ " d=getValue(b, max)," +
+ " e=harmfit(a)," +
+ " f=getAmplitude(e)," +
+ " g=getAngularFrequency(e)," +
+ " h=getPhase(e))";
+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
+ paramsLoc.set("expr", cexpr);
+ paramsLoc.set("qt", "/stream");
+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;
+ TupleStream solrStream = new SolrStream(url, paramsLoc);
+ StreamContext context = new StreamContext();
+ solrStream.setStreamContext(context);
+ List<Tuple> tuples = getTuples(solrStream);
+ assertTrue(tuples.size() == 1);
+ List<Number> wave = (List<Number>)tuples.get(0).get("a");
+ assertEquals(wave.size(), 128);
+ Map desc = (Map)tuples.get(0).get("b");
+ Number min = (Number)tuples.get(0).get("c");
+ Number max = (Number)tuples.get(0).get("d");
+ assertEquals(min.doubleValue(), -9.9, .1);
+ assertEquals(max.doubleValue(), 9.9, .1);
+
+ List<Number> wave1 = (List<Number>)tuples.get(0).get("e");
+ assertEquals(wave1.size(), 128);
+
+ Number amp = (Number)tuples.get(0).get("f");
+ Number freq = (Number)tuples.get(0).get("g");
+ Number pha = (Number)tuples.get(0).get("h");
+
+ assertEquals(amp.doubleValue(), 10, .1);
+ assertEquals(freq.doubleValue(), .3, .1);
+ assertEquals(pha.doubleValue(), 2.9, .1);
+ }
+
+ @Test
public void testEbeAdd() throws Exception {
String cexpr = "let(echo=true," +
" a=array(2, 4, 6, 8, 10, 12)," +
@@ -2410,6 +2449,7 @@ public class MathExpressionTest extends SolrCloudTestCase {
}
+
@Test
public void testSetAndGetValue() throws Exception {
String cexpr = "let(echo=true," +