lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject [06/50] [abbrv] lucene-solr:jira/solr-11285: SOLR-11436: Add polyfit and polyfitDerivative Stream Evaluators
Date Thu, 12 Oct 2017 11:50:44 GMT
SOLR-11436: Add polyfit and polyfitDerivative Stream Evaluators


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

Branch: refs/heads/jira/solr-11285
Commit: 1782dd9ca901eced0adbe9932cdbc50ad5792663
Parents: b10eb11
Author: Joel Bernstein <jbernste@apache.org>
Authored: Wed Oct 4 13:56:46 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Wed Oct 4 14:02:13 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |   2 +
 .../io/eval/PolyFitDerivativeEvaluator.java     | 104 +++++++++++++++++++
 .../client/solrj/io/eval/PolyFitEvaluator.java  | 102 ++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   |  38 +++++++
 4 files changed, 246 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1782dd9c/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 16a31fb..69017ac 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -308,6 +308,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("zipFDistribution", ZipFDistributionEvaluator.class)
         .withFunctionName("gammaDistribution", GammaDistributionEvaluator.class)
         .withFunctionName("betaDistribution", BetaDistributionEvaluator.class)
+        .withFunctionName("polyfit", PolyFitEvaluator.class)
+        .withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1782dd9c/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java
new file mode 100644
index 0000000..1aacb9e
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java
@@ -0,0 +1,104 @@
+/*
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math3.fitting.PolynomialCurveFitter;
+
+import org.apache.commons.math3.fitting.WeightedObservedPoints;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class PolyFitDerivativeEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public PolyFitDerivativeEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    if(objects.length > 3) {
+      throw new IOException("polyfitDerivative function takes a maximum of 3 arguments.");
+    }
+
+    Object first = objects[0];
+
+    double[] x = null;
+    double[] y = null;
+    int degree = 3;
+
+    if(objects.length == 1) {
+      //Only the y values passed
+
+      y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      x = new double[y.length];
+      for(int i=0; i<y.length; i++) {
+        x[i] = i;
+      }
+
+    } else if(objects.length == 3) {
+      // x, y and degree passed
+
+      Object second = objects[1];
+      x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      degree = ((Number)objects[2]).intValue();
+    } else if(objects.length == 2) {
+      if(objects[1] instanceof List) {
+        // x and y passed
+        Object second = objects[1];
+        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      } else {
+        // y and degree passed
+        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+        x = new double[y.length];
+        for(int i=0; i<y.length; i++) {
+          x[i] = i;
+        }
+
+        degree = ((Number)objects[1]).intValue();
+      }
+    }
+
+    PolynomialCurveFitter curveFitter = PolynomialCurveFitter.create(degree);
+    WeightedObservedPoints points = new WeightedObservedPoints();
+    for(int i=0; i<x.length; i++) {
+      points.add(x[i], y[i]);
+    }
+
+    double[] coef = curveFitter.fit(points.toList());
+    PolynomialFunction pf = new PolynomialFunction(coef);
+    UnivariateFunction univariateFunction = pf.derivative();
+
+    List list = new ArrayList();
+    for(double xvalue : x) {
+      double yvalue= univariateFunction.value(xvalue);
+      list.add(yvalue);
+    }
+
+    return list;
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1782dd9c/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java
new file mode 100644
index 0000000..2c0bf2a
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java
@@ -0,0 +1,102 @@
+/*
+ * 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.math.BigDecimal;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math3.fitting.PolynomialCurveFitter;
+
+import org.apache.commons.math3.fitting.WeightedObservedPoints;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class PolyFitEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public PolyFitEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    if(objects.length > 3) {
+      throw new IOException("polyfit function takes a maximum of 3 arguments.");
+    }
+
+    Object first = objects[0];
+
+    double[] x = null;
+    double[] y = null;
+    int degree = 3;
+
+    if(objects.length == 1) {
+      //Only the y values passed
+
+      y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      x = new double[y.length];
+      for(int i=0; i<y.length; i++) {
+        x[i] = i;
+      }
+
+    } else if(objects.length == 3) {
+      // x, y and degree passed
+
+      Object second = objects[1];
+      x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      degree = ((Number)objects[2]).intValue();
+    } else if(objects.length == 2) {
+      if(objects[1] instanceof List) {
+        // x and y passed
+        Object second = objects[1];
+        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+      } else {
+        // y and degree passed
+        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+        x = new double[y.length];
+        for(int i=0; i<y.length; i++) {
+          x[i] = i;
+        }
+
+        degree = ((Number)objects[1]).intValue();
+      }
+    }
+
+    PolynomialCurveFitter curveFitter = PolynomialCurveFitter.create(degree);
+    WeightedObservedPoints points = new WeightedObservedPoints();
+    for(int i=0; i<x.length; i++) {
+      points.add(x[i], y[i]);
+    }
+
+    double[] coef = curveFitter.fit(points.toList());
+    PolynomialFunction pf = new PolynomialFunction(coef);
+
+    List list = new ArrayList();
+    for(double xvalue : x) {
+      double yvalue= pf.value(xvalue);
+      list.add(yvalue);
+    }
+
+    return list;
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1782dd9c/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 9e4c6c3..c73adf6 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
@@ -6541,6 +6541,44 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertTrue(out.get(5).intValue() == -6);
   }
 
+  @Test
+  public void testPolyfit() throws Exception {
+    String cexpr = "let(echo=true," +
+                   "    a=array(0,1,2,3,4,5,6,7)," +
+                   "    fit=polyfit(a, 1)," +
+                   "    deriv=polyfitDerivative(a, 1))";
+    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);
+    assertTrue(out.get(0).intValue() == 0);
+    assertTrue(out.get(1).intValue() == 1);
+    assertTrue(out.get(2).intValue() == 2);
+    assertTrue(out.get(3).intValue() == 3);
+    assertTrue(out.get(4).intValue() == 4);
+    assertTrue(out.get(5).intValue() == 5);
+    assertTrue(out.get(6).intValue() == 6);
+    assertTrue(out.get(7).intValue() == 7);
+
+    out = (List<Number>)tuples.get(0).get("deriv");
+    assertTrue(out.size() == 8);
+    assertTrue(out.get(0).intValue() == 1);
+    assertTrue(out.get(1).intValue() == 1);
+    assertTrue(out.get(2).intValue() == 1);
+    assertTrue(out.get(3).intValue() == 1);
+    assertTrue(out.get(4).intValue() == 1);
+    assertTrue(out.get(5).intValue() == 1);
+    assertTrue(out.get(6).intValue() == 1);
+    assertTrue(out.get(7).intValue() == 1);
+  }
+
 
   @Test
   public void testAnova() throws Exception {


Mime
View raw message