Apache Lucene open-source search software
SOLR-11430: Add lerp and akima Stream Evaluators to support linear and akima spline interpolation
Joel Bernsteina9258476842f
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex 73dea3c2471..0a880c69d6e 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 Evaluatorsdiff --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.javanew file mode 100644index 00000000000..ff68963bbd4--- /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;+ }++}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.javanew file mode 100644index 00000000000..5df9ca9289e--- /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;+ }++}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.javaindex c2e11f7077d..6e771869430 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;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.javaindex ee9dece8e11..c8e227be4fc 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.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.javaindex 2662fd23dd7..fe107924847 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 {@Testpublic 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))";@@ -7383,6 +7384,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);+ }++@Testpublic void testHarmonicFit() throws Exception {String cexpr = "let(a=sin(sequence(100, 1, 6)), b=harmonicFit(a), s=ebeSubtract(a, b))";