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-11320: SOLR-11439: Add harmonicFit Stream Evaluator
Date Tue, 17 Oct 2017 15:53:19 GMT
SOLR-11439: Add harmonicFit 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/b9c8aa70
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/b9c8aa70
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/b9c8aa70

Branch: refs/heads/jira/solr-11320
Commit: b9c8aa7059fd7bc94d5bcfb0dab6f0370e211531
Parents: e92bde1
Author: Joel Bernstein <jbernste@apache.org>
Authored: Wed Oct 4 15:51:19 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Sun Oct 8 21:03:47 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../solrj/io/eval/HarmonicFitEvaluator.java     | 85 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 20 +++++
 3 files changed, 106 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b9c8aa70/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 69017ac..860ead6 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -310,6 +310,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("betaDistribution", BetaDistributionEvaluator.class)
         .withFunctionName("polyfit", PolyFitEvaluator.class)
         .withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class)
+        .withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b9c8aa70/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
new file mode 100644
index 0000000..09ed8df
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java
@@ -0,0 +1,85 @@
+/*
+ * 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.function.HarmonicOscillator;
+import org.apache.commons.math3.fitting.HarmonicCurveFitter;
+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 HarmonicFitEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public HarmonicFitEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... objects) throws IOException{
+
+    if(objects.length > 3) {
+      throw new IOException("harmonicFit 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 -> ((BigDecimal) 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 -> ((BigDecimal) value).doubleValue()).toArray();
+        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
+
+
+    }
+
+    HarmonicCurveFitter curveFitter = HarmonicCurveFitter.create();
+
+    WeightedObservedPoints points = new WeightedObservedPoints();
+    for(int i=0; i<x.length; i++) {
+      points.add(x[i], y[i]);
+    }
+
+    double[] coef = curveFitter.fit(points.toList());
+    HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);
+
+    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/b9c8aa70/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 36333eb..c51309f 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
@@ -6579,6 +6579,26 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertTrue(out.get(7).intValue() == 1);
   }
 
+  @Test
+  public void testHarmonicFit() throws Exception {
+    String cexpr = "let(a=sin(sequence(100, 1, 6)), b=harmonicFit(a), s=ebeSubtract(a, b))";
+
+    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("s");
+    assertTrue(out.size() == 100);
+    for(Number n : out) {
+      assertEquals(n.doubleValue(), 0.0, .01);
+    }
+  }
+
 
   @Test
   public void testAnova() throws Exception {


Mime
View raw message