lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject [21/50] lucene-solr:jira/solr-11458-2: SOLR-11680: Add normalizeSum Stream Evaluator
Date Mon, 04 Dec 2017 17:49:05 GMT
SOLR-11680: Add normalizeSum 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/8072b46b
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/8072b46b
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/8072b46b

Branch: refs/heads/jira/solr-11458-2
Commit: 8072b46b63c70f2bbcd22ae053038e25750c67a0
Parents: 7a12de4
Author: Joel Bernstein <jbernste@apache.org>
Authored: Mon Nov 27 13:27:59 2017 -0500
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Mon Nov 27 13:38:43 2017 -0500

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  1 +
 .../solrj/io/eval/NormalizeSumEvaluator.java    | 84 ++++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 50 +++++++++++-
 3 files changed, 132 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/8072b46b/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 75f2de1..a0a83c1 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -278,6 +278,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("sumColumns", SumColumnsEvaluator.class)
         .withFunctionName("diff", TimeDifferencingEvaluator.class)
         .withFunctionName("corrPValues", CorrelationSignificanceEvaluator.class)
+        .withFunctionName("normalizeSum", NormalizeSumEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/8072b46b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NormalizeSumEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NormalizeSumEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NormalizeSumEvaluator.java
new file mode 100644
index 0000000..d300f59
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NormalizeSumEvaluator.java
@@ -0,0 +1,84 @@
+/*
+ * 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.Locale;
+
+import org.apache.commons.math3.util.MathArrays;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import java.util.List;
+import java.util.ArrayList;
+
+public class NormalizeSumEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public NormalizeSumEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException{
+    super(expression, factory);
+
+    if(2 < containedEvaluators.size()){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting
at most two parameters but found %d",expression,containedEvaluators.size()));
+    }
+  }
+
+  @Override
+  public Object doWork(Object... values) throws IOException{
+
+    Object value = values[0];
+
+    double sumTo = 1.0;
+
+    if(values.length == 2) {
+      Number n = (Number)values[1];
+      sumTo = n.doubleValue();
+    }
+
+    if(null == value){
+      return null;
+    } else if(value instanceof Matrix) {
+      Matrix matrix = (Matrix) value;
+
+      double[][] data = matrix.getData();
+      double[][] unitData = new double[data.length][];
+      for(int i=0; i<data.length; i++) {
+        double[] row = data[i];
+        double[] unitRow = MathArrays.normalizeArray(row, sumTo);
+        unitData[i] = unitRow;
+      }
+
+      return new Matrix(unitData);
+    } else if(value instanceof List) {
+      List<Number> vals = (List<Number>)value;
+      double[] doubles = new double[vals.size()];
+      for(int i=0; i<doubles.length; i++) {
+        doubles[i] = vals.get(i).doubleValue();
+      }
+
+      List<Number> unitList = new ArrayList(doubles.length);
+      double[] unitArray = MathArrays.normalizeArray(doubles, sumTo);
+      for(double d : unitArray) {
+        unitList.add(d);
+      }
+
+      return unitList;
+    } else {
+      throw new IOException("The unit function expects either a numeric array or matrix as
a parameter");
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/8072b46b/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 bfee198..8f3c3f5 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
@@ -6326,11 +6326,55 @@ public class StreamExpressionTest extends SolrCloudTestCase {
 
     List<Number> array3 = (List<Number>)tuples.get(0).get("b");
     assertEquals(array3.size(), 3);
-    assertEquals(array2.get(0).doubleValue(), 0.4558423058385518, 0.0);
-    assertEquals(array2.get(1).doubleValue(), 0.5698028822981898, 0.0);
-    assertEquals(array2.get(2).doubleValue(), 0.6837634587578276, 0.0);
+    assertEquals(array3.get(0).doubleValue(), 0.4558423058385518, 0.0);
+    assertEquals(array3.get(1).doubleValue(), 0.5698028822981898, 0.0);
+    assertEquals(array3.get(2).doubleValue(), 0.6837634587578276, 0.0);
   }
 
+
+  @Test
+  public void testNormalizeSum() throws Exception {
+    String cexpr = "let(echo=true, " +
+                       "a=normalizeSum(matrix(array(1,2,3), array(4,5,6))), " +
+                       "b=normalizeSum(array(1,2,3))," +
+                       "c=normalizeSum(array(1,2,3), 100))";
+    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<List<Number>> out = (List<List<Number>>)tuples.get(0).get("a");
+    assertEquals(out.size(), 2);
+    List<Number> array1 = out.get(0);
+    assertEquals(array1.size(), 3);
+    assertEquals(array1.get(0).doubleValue(), 0.16666666666666666, 0.0001);
+    assertEquals(array1.get(1).doubleValue(), 0.3333333333333333, 0.00001);
+    assertEquals(array1.get(2).doubleValue(), 0.5, 0.0001);
+
+    List<Number> array2 = out.get(1);
+    assertEquals(array2.size(), 3);
+    assertEquals(array2.get(0).doubleValue(), 0.26666666666666666, 0.0001);
+    assertEquals(array2.get(1).doubleValue(), 0.3333333333333333, 0.0001);
+    assertEquals(array2.get(2).doubleValue(), 0.4, 0.0001);
+
+    List<Number> array3 = (List<Number>)tuples.get(0).get("b");
+    assertEquals(array3.size(), 3);
+    assertEquals(array3.get(0).doubleValue(), 0.16666666666666666, 0.0001);
+    assertEquals(array3.get(1).doubleValue(), 0.3333333333333333, 0.0001);
+    assertEquals(array3.get(2).doubleValue(), 0.5, 0.0001);
+
+    List<Number> array4 = (List<Number>)tuples.get(0).get("c");
+    assertEquals(array4.size(), 3);
+    assertEquals(array4.get(0).doubleValue(), 16.666666666666666, 0.0001);
+    assertEquals(array4.get(1).doubleValue(), 33.33333333333333, 0.00001);
+    assertEquals(array4.get(2).doubleValue(), 50, 0.0001);
+  }
+
+
   @Test
   public void testStandardize() throws Exception {
     String cexpr = "let(echo=true, a=standardize(matrix(array(1,2,3), array(4,5,6))), b=standardize(array(4,5,6)))";


Mime
View raw message