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/08/11 18:19:42 UTC
lucene-solr:master: SOLR-12634: Add gaussfit Stream Evaluator
Repository: lucene-solr
Updated Branches:
refs/heads/master 928b92caa -> 17eb8cd14
SOLR-12634: Add gaussfit Stream Evaluator
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/17eb8cd1
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/17eb8cd1
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/17eb8cd1
Branch: refs/heads/master
Commit: 17eb8cd14d27d2680fe7c4b3871f3eb883542d34
Parents: 928b92c
Author: Joel Bernstein <jb...@apache.org>
Authored: Sat Aug 11 14:17:18 2018 -0400
Committer: Joel Bernstein <jb...@apache.org>
Committed: Sat Aug 11 14:17:44 2018 -0400
----------------------------------------------------------------------
.../org/apache/solr/client/solrj/io/Lang.java | 1 +
.../client/solrj/io/eval/GaussFitEvaluator.java | 91 ++++++++++++++++++++
.../apache/solr/client/solrj/io/TestLang.java | 2 +-
.../solrj/io/stream/MathExpressionTest.java | 46 ++++++++++
4 files changed, 139 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17eb8cd1/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 6f170c4..cedf062 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
@@ -247,6 +247,7 @@ public class Lang {
.withFunctionName("getValue", GetValueEvaluator.class)
.withFunctionName("setValue", SetValueEvaluator.class)
.withFunctionName("knnRegress", KnnRegressionEvaluator.class)
+ .withFunctionName("gaussfit", GaussFitEvaluator.class)
// Boolean Stream Evaluators
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17eb8cd1/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GaussFitEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GaussFitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GaussFitEvaluator.java
new file mode 100644
index 0000000..a26c8d7
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GaussFitEvaluator.java
@@ -0,0 +1,91 @@
+/*
+ * 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.Gaussian;
+import org.apache.commons.math3.fitting.GaussianCurveFitter;
+import org.apache.commons.math3.fitting.WeightedObservedPoints;
+import org.apache.commons.math3.fitting.WeightedObservedPoint;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GaussFitEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+ protected static final long serialVersionUID = 1L;
+
+ public GaussFitEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+ super(expression, factory);
+ }
+
+ @Override
+ public Object doWork(Object... objects) throws IOException{
+
+ if(objects.length >= 3) {
+ throw new IOException("gaussfit function takes a maximum of 2 arguments.");
+ }
+
+ Object first = objects[0];
+
+ double[] x = null;
+ double[] y = null;
+
+ if(objects.length == 1) {
+ //Only the y values passed
+
+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+ x = new double[y.length];
+ for(int i=0; i<y.length; i++) {
+ x[i] = i;
+ }
+
+ } else if(objects.length == 2) {
+ // x and y passed
+ Object second = objects[1];
+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+
+
+ }
+
+ GaussianCurveFitter curveFitter = GaussianCurveFitter.create();
+
+ WeightedObservedPoints points = new WeightedObservedPoints();
+ for(int i=0; i<x.length; i++) {
+ points.add(x[i], y[i]);
+ }
+
+ List<WeightedObservedPoint> pointList = points.toList();
+
+ double[] guess = new GaussianCurveFitter.ParameterGuesser(pointList).guess();
+ curveFitter = curveFitter.withStartPoint(guess);
+
+ double[] coef = curveFitter.fit(pointList);
+ Gaussian gaussian = new Gaussian(coef[0], coef[1], coef[2]);
+ List list = new ArrayList();
+ for(double xvalue : x) {
+ double yvalue= gaussian.value(xvalue);
+ list.add(yvalue);
+ }
+
+ return new VectorFunction(gaussian, list);
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17eb8cd1/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 df56844..1bbf032 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
@@ -69,7 +69,7 @@ public class TestLang extends LuceneTestCase {
TemporalEvaluatorDayOfQuarter.FUNCTION_NAME, "abs", "add", "div", "mult", "sub", "log", "pow",
"mod", "ceil", "floor", "sin", "asin", "sinh", "cos", "acos", "cosh", "tan", "atan", "tanh", "round", "sqrt",
"cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan",
- "earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress"};
+ "earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress", "gaussfit"};
@Test
public void testLang() {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/17eb8cd1/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 a9be57e..a27ad2d 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
@@ -3466,6 +3466,52 @@ public class MathExpressionTest extends SolrCloudTestCase {
}
@Test
+ public void testGaussfit() throws Exception {
+ String cexpr = "let(echo=true, " +
+ "x=array(79.56,81.32,82.82,84.64,86.18,87.89,89.53,91.14,92.8,94.43,96.08,97.72,99.37,101,102.66,104.3,105.94,107.59,109.23,110.87,112.52,114.13,115.82,117.44,119.27), " +
+ "y=array(3, 3, 26, 54, 139, 344, 685, 1289, 2337, 3593, 4781, 5964, 6538, 6357, 5705, 4548, 3280, 2058, 1191, 649, 285, 112, 34, 18, 7)," +
+ "g=gaussfit(x,y))";
+
+ 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> predictions = (List<Number>)tuples.get(0).get("g");
+ assertEquals(predictions.size(), 25);
+ assertEquals(predictions.get(0).doubleValue(), 1.5217511259930976, 0);
+ assertEquals(predictions.get(1).doubleValue(), 6.043059526517849, 0);
+ assertEquals(predictions.get(2).doubleValue(), 17.74876254851105, 0);
+ assertEquals(predictions.get(3).doubleValue(), 58.12355990996735, 0);
+ assertEquals(predictions.get(4).doubleValue(), 142.98079858358975, 0);
+ assertEquals(predictions.get(5).doubleValue(), 347.5571069372449, 0);
+ assertEquals(predictions.get(6).doubleValue(), 729.8016076579886, 0);
+ assertEquals(predictions.get(7).doubleValue(), 1361.3981561397804, 0);
+ assertEquals(predictions.get(8).doubleValue(), 2322.566306687647, 0);
+ assertEquals(predictions.get(9).doubleValue(), 3524.6949840829216, 0);
+ assertEquals(predictions.get(10).doubleValue(), 4824.273031596218, 0);
+ assertEquals(predictions.get(11).doubleValue(), 5915.519574509397, 0);
+ assertEquals(predictions.get(12).doubleValue(), 6514.552728035438, 0);
+ assertEquals(predictions.get(13).doubleValue(), 6438.3295998729845, 0);
+ assertEquals(predictions.get(14).doubleValue(), 5702.59200814961, 0);
+ assertEquals(predictions.get(15).doubleValue(), 4538.7945530007, 0);
+ assertEquals(predictions.get(16).doubleValue(), 3243.606591784876, 0);
+ assertEquals(predictions.get(17).doubleValue(), 2074.9937785806937, 0);
+ assertEquals(predictions.get(18).doubleValue(), 1194.697766441063, 0);
+ assertEquals(predictions.get(19).doubleValue(), 617.6162726398896, 0);
+ assertEquals(predictions.get(20).doubleValue(), 285.248193084953, 0);
+ assertEquals(predictions.get(21).doubleValue(), 120.84133189889134, 0);
+ assertEquals(predictions.get(22).doubleValue(), 43.87052382491055, 0);
+ assertEquals(predictions.get(23).doubleValue(), 14.918461016939522, 0);
+ assertEquals(predictions.get(24).doubleValue(), 3.887269101204326, 0);
+ }
+
+
+ @Test
public void testPlot() throws Exception {
String cexpr = "let(a=array(3,2,3), plot(type=scatter, x=a, y=array(5,6,3)))";
ModifiableSolrParams paramsLoc = new ModifiableSolrParams();