lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbern...@apache.org
Subject lucene-solr:master: SOLR-11046: Add residuals Stream Evaluator
Date Wed, 12 Jul 2017 14:41:03 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master f22dc3771 -> d35da77b7


SOLR-11046: Add residuals 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/d35da77b
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/d35da77b
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/d35da77b

Branch: refs/heads/master
Commit: d35da77b72af3a6ed2132e7d59712713c2ca4a3a
Parents: f22dc37
Author: Joel Bernstein <jbernste@apache.org>
Authored: Wed Jul 12 10:23:56 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Wed Jul 12 10:28:52 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  2 +-
 .../solrj/io/eval/ResidualsEvaluator.java       | 82 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 24 ++++++
 3 files changed, 107 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d35da77b/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 3901c61..ea2a931 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -217,8 +217,8 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("scale", ScaleEvaluator.class)
         .withFunctionName("sequence", SequenceEvaluator.class)
         .withFunctionName("addAll", AddAllEvaluator.class)
+        .withFunctionName("residuals", ResidualsEvaluator.class)
 
-        
         // Boolean Stream Evaluators
         .withFunctionName("and", AndEvaluator.class)
         .withFunctionName("eor", ExclusiveOrEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d35da77b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java
new file mode 100644
index 0000000..9a9c869
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java
@@ -0,0 +1,82 @@
+/*
+ * 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.ArrayList;
+import java.util.Locale;
+import java.util.List;
+
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation.ExpressionType;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParameter;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class ResidualsEvaluator extends ComplexEvaluator implements Expressible {
+
+  private static final long serialVersionUID = 1;
+
+  public ResidualsEvaluator(StreamExpression expression,
+                            StreamFactory factory) throws IOException {
+    super(expression, factory);
+
+    if(3 != subEvaluators.size()){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting
three values (regression result and two numeric arrays) but found %d",expression,subEvaluators.size()));
+    }
+  }
+
+  public List<Number> evaluate(Tuple tuple) throws IOException {
+
+    StreamEvaluator r = subEvaluators.get(0);
+    StreamEvaluator a = subEvaluators.get(1);
+    StreamEvaluator b = subEvaluators.get(2);
+
+    RegressionEvaluator.RegressionTuple rt= (RegressionEvaluator.RegressionTuple)r.evaluate(tuple);
+    List<Number> listA = (List<Number>)a.evaluate(tuple);
+    List<Number> listB = (List<Number>)b.evaluate(tuple);
+    List<Number> residuals = new ArrayList();
+
+    for(int i=0; i<listA.size(); i++) {
+      double valueA = listA.get(i).doubleValue();
+      double prediction = rt.predict(valueA);
+      double valueB = listB.get(i).doubleValue();
+      double residual = valueB - prediction;
+      residuals.add(residual);
+    }
+
+    return residuals;
+  }
+
+  @Override
+  public StreamExpressionParameter toExpression(StreamFactory factory) throws IOException
{
+    StreamExpression expression = new StreamExpression(factory.getFunctionName(getClass()));
+    return expression;
+  }
+
+  @Override
+  public Explanation toExplanation(StreamFactory factory) throws IOException {
+    return new Explanation(nodeId.toString())
+        .withExpressionType(ExpressionType.EVALUATOR)
+        .withFunctionName(factory.getFunctionName(getClass()))
+        .withImplementingClass(getClass().getName())
+        .withExpression(toExpression(factory).toString());
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d35da77b/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 8524f4d..ff105f6 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
@@ -6021,6 +6021,30 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertTrue(out.get(8).intValue() == 9);
   }
 
+
+  @Test
+  public void testResiduals() throws Exception {
+    String cexpr = "let(a=array(1,2,3,4,5,6), b=array(2,4,6,8,10,12), c=regress(a,b), tuple(res=residuals(c,a,a)))";
+    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("res");
+    assertTrue(out.size() == 6);
+    assertTrue(out.get(0).intValue() == -1);
+    assertTrue(out.get(1).intValue() == -2);
+    assertTrue(out.get(2).intValue() == -3);
+    assertTrue(out.get(3).intValue() == -4);
+    assertTrue(out.get(4).intValue() == -5);
+    assertTrue(out.get(5).intValue() == -6);
+  }
+
+
   @Test
   public void testAnova() throws Exception {
     String cexpr = "anova(array(1,2,3,5,4,6), array(5,2,3,5,4,6), array(1,2,7,5,4,6))";


Mime
View raw message