lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbern...@apache.org
Subject lucene-solr:branch_7x: SOLR-11430: Add lerp and akima Stream Evaluators to support linear and akima spline interpolation
Date Tue, 02 Jan 2018 15:40:27 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 1ee11ec62 -> 4618f9a91


SOLR-11430: Add lerp and akima Stream Evaluators to support linear and akima spline interpolation


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/4618f9a9
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/4618f9a9
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/4618f9a9

Branch: refs/heads/branch_7x
Commit: 4618f9a919cef150f66aa3db19f4376ddc348baf
Parents: 1ee11ec
Author: Joel Bernstein <jbernste@apache.org>
Authored: Tue Jan 2 10:23:23 2018 -0500
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Tue Jan 2 10:35:25 2018 -0500

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  2 +
 .../client/solrj/io/eval/AkimaEvaluator.java    | 71 ++++++++++++++++
 .../client/solrj/io/eval/LerpEvaluator.java     | 71 ++++++++++++++++
 .../client/solrj/io/eval/MeanEvaluator.java     |  1 -
 .../client/solrj/io/eval/SequenceEvaluator.java |  2 +-
 .../solrj/io/stream/StreamExpressionTest.java   | 85 +++++++++++++++++++-
 6 files changed, 229 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
index 73dea3c..0a880c6 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -288,6 +288,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("density", DensityEvaluator.class)
         .withFunctionName("mannWhitney", MannWhitneyUEvaluator.class)
         .withFunctionName("sumSq", SumSqEvaluator.class)
+        .withFunctionName("akima", AkimaEvaluator.class)
+        .withFunctionName("lerp", LerpEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AkimaEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AkimaEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AkimaEvaluator.java
new file mode 100644
index 0000000..ff68963
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AkimaEvaluator.java
@@ -0,0 +1,71 @@
+/*
+ * 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.interpolation.AkimaSplineInterpolator;
+import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class AkimaEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public AkimaEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    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) {
+      Object second = objects[1];
+      x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+      y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+    }
+
+    AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator();
+    PolynomialSplineFunction spline = interpolator.interpolate(x, y);
+
+    List<Number> list = new ArrayList();
+    for(double xvalue : x) {
+      list.add(spline.value(xvalue));
+    }
+
+    VectorFunction vec = new VectorFunction(spline, list);
+    vec.addToContext("x", x);
+    vec.addToContext("y", y);
+
+    return vec;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LerpEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LerpEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LerpEvaluator.java
new file mode 100644
index 0000000..5df9ca9
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LerpEvaluator.java
@@ -0,0 +1,71 @@
+/*
+ * 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.interpolation.LinearInterpolator;
+import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class LerpEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
+  protected static final long serialVersionUID = 1L;
+
+  public LerpEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    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) {
+      Object second = objects[1];
+      x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+      y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
+    }
+
+    LinearInterpolator interpolator = new LinearInterpolator();
+    PolynomialSplineFunction spline = interpolator.interpolate(x, y);
+
+    List<Number> list = new ArrayList();
+    for(double xvalue : x) {
+      list.add(spline.value(xvalue));
+    }
+
+    VectorFunction vec = new VectorFunction(spline, list);
+    vec.addToContext("x", x);
+    vec.addToContext("y", y);
+
+    return vec;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
index c2e11f7..6e77186 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanEvaluator.java
@@ -24,7 +24,6 @@ import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
 import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
 import org.apache.commons.math3.stat.StatUtils;
 
-
 public class MeanEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
   protected static final long serialVersionUID = 1L;
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java
index ee9dece..c8e227b 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SequenceEvaluator.java
@@ -1,4 +1,4 @@
-/*
+  /*
  * 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.

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4618f9a9/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
index 2662fd2..fe10792 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
@@ -7345,7 +7345,8 @@ public class StreamExpressionTest extends SolrCloudTestCase {
   @Test
   public void testSpline() throws Exception {
     String cexpr = "let(echo=true," +
-                   "    a=array(0,1,2,3,4,5,6,7), b=array(1,70,90,10,78, 100, 1, 9)," +
+                   "    a=array(0,1,2,3,4,5,6,7), " +
+                   "    b=array(1,70,90,10,78, 100, 1, 9)," +
                    "    fit=spline(a, b), " +
                    "    der=derivative(fit))";
 
@@ -7384,6 +7385,88 @@ public class StreamExpressionTest extends SolrCloudTestCase {
 
 
   @Test
+  public void testAkima() throws Exception {
+    String cexpr = "let(echo=true," +
+        "    a=array(0,1,2,3,4,5,6,7), " +
+        "    b=array(1,70,90,10,78, 100, 1, 9)," +
+        "    fit=akima(a, b), " +
+        "    der=derivative(fit))";
+
+    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> out = (List<Number>)tuples.get(0).get("fit");
+    assertTrue(out.size() == 8);
+    assertEquals(out.get(0).doubleValue(), 1.0, 0.0001);
+    assertEquals(out.get(1).doubleValue(), 70.0, 0.0001);
+    assertEquals(out.get(2).doubleValue(), 90.0, 0.0001);
+    assertEquals(out.get(3).doubleValue(), 10.0, 0.0001);
+    assertEquals(out.get(4).doubleValue(), 78.0, 0.0001);
+    assertEquals(out.get(5).doubleValue(), 100.0, 0.0001);
+    assertEquals(out.get(6).doubleValue(), 1.0, 0.0001);
+    assertEquals(out.get(7).doubleValue(), 9.0, 0.0001);
+
+    List<Number> out1 = (List<Number>)tuples.get(0).get("der");
+    assertTrue(out1.size() == 8);
+    assertEquals(out1.get(0).doubleValue(), 93.5, 0.0001);
+    assertEquals(out1.get(1).doubleValue(), 44.5, 0.0001);
+    assertEquals(out1.get(2).doubleValue(),-4.873096446700508, 0.0001);
+    assertEquals(out1.get(3).doubleValue(), 21.36986301369863, 0.0001);
+    assertEquals(out1.get(4).doubleValue(),42.69144981412639, 0.0001);
+    assertEquals(out1.get(5).doubleValue(),-14.379084967320262, 0.0001);
+    assertEquals(out1.get(6).doubleValue(), -45.5, 0.0001);
+    assertEquals(out1.get(7).doubleValue(), 61.5, 0.0001);
+  }
+
+
+  @Test
+  public void testLerp() throws Exception {
+    String cexpr = "let(echo=true," +
+        "    a=array(0,1,2,3,4,5,6,7), " +
+        "    b=array(1,70,90,10,78, 100, 1, 9)," +
+        "    fit=lerp(a, b), " +
+        "    der=derivative(fit))";
+
+    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> out = (List<Number>)tuples.get(0).get("fit");
+    assertTrue(out.size() == 8);
+    assertEquals(out.get(0).doubleValue(), 1.0, 0.0001);
+    assertEquals(out.get(1).doubleValue(), 70.0, 0.0001);
+    assertEquals(out.get(2).doubleValue(), 90.0, 0.0001);
+    assertEquals(out.get(3).doubleValue(), 10.0, 0.0001);
+    assertEquals(out.get(4).doubleValue(), 78.0, 0.0001);
+    assertEquals(out.get(5).doubleValue(), 100.0, 0.0001);
+    assertEquals(out.get(6).doubleValue(), 1.0, 0.0001);
+    assertEquals(out.get(7).doubleValue(), 9.0, 0.0001);
+
+    List<Number> out1 = (List<Number>)tuples.get(0).get("der");
+    assertTrue(out1.size() == 8);
+    assertEquals(out1.get(0).doubleValue(), 69.0, 0.0001);
+    assertEquals(out1.get(1).doubleValue(), 20.0, 0.0001);
+    assertEquals(out1.get(2).doubleValue(),-80.0, 0.0001);
+    assertEquals(out1.get(3).doubleValue(), 68, 0.0001);
+    assertEquals(out1.get(4).doubleValue(), 22.0, 0.0001);
+    assertEquals(out1.get(5).doubleValue(),-99.0, 0.0001);
+    assertEquals(out1.get(6).doubleValue(), 8.0, 0.0001);
+    assertEquals(out1.get(7).doubleValue(), 8.0, 0.0001);
+  }
+
+
+  @Test
   public void testHarmonicFit() throws Exception {
     String cexpr = "let(a=sin(sequence(100, 1, 6)), b=harmonicFit(a), s=ebeSubtract(a, b))";
 


Mime
View raw message