lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbern...@apache.org
Subject lucene-solr:branch_7x: SOLR-10680: Add minMaxScale Stream Evaluator
Date Thu, 02 Nov 2017 17:57:18 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x f48bfaea4 -> 2136fc5ec


SOLR-10680: Add minMaxScale 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/2136fc5e
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/2136fc5e
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/2136fc5e

Branch: refs/heads/branch_7x
Commit: 2136fc5ecdb90b24391306a8126bda9e545e8dcf
Parents: f48bfae
Author: Joel Bernstein <jbernste@apache.org>
Authored: Thu Nov 2 13:43:44 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Thu Nov 2 13:53:27 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |   1 +
 .../solrj/io/eval/MinMaxScaleEvaluator.java     | 115 +++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   |  62 ++++++++++
 3 files changed, 178 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2136fc5e/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 f6b9686..2e12a9b 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -273,6 +273,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("unit", UnitEvaluator.class)
         .withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)
         .withFunctionName("precision", PrecisionEvaluator.class)
+        .withFunctionName("minMaxScale", MinMaxScaleEvaluator.class)
 
         // Boolean Stream Evaluators
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2136fc5e/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MinMaxScaleEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MinMaxScaleEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MinMaxScaleEvaluator.java
new file mode 100644
index 0000000..60c6377
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MinMaxScaleEvaluator.java
@@ -0,0 +1,115 @@
+/*
+ * 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.List;
+
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class MinMaxScaleEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker
{
+  protected static final long serialVersionUID = 1L;
+
+  public MinMaxScaleEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException{
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object... values) throws IOException {
+
+    if(null == values){
+      return null;
+    }
+
+    double min = 0;
+    double max = 1;
+
+    if(values.length == 3) {
+      min = ((Number)values[1]).doubleValue();
+      max = ((Number)values[2]).doubleValue();
+    }
+
+    if(values[0] instanceof Matrix) {
+      Matrix matrix = (Matrix)values[0];
+      double[][] data = matrix.getData();
+      double[][] scaled = new double[data.length][];
+      for(int i=0; i<scaled.length; i++) {
+        double[] row = data[i];
+        scaled[i] = scale(row, min, max);
+      }
+
+      return new Matrix(scaled);
+
+    } else if(values[0] instanceof List) {
+      List<Number> vec = (List)values[0];
+      double[] data = new double[vec.size()];
+
+      for(int i=0; i<vec.size(); i++) {
+        data[i] = vec.get(i).doubleValue();
+      }
+
+      data = scale(data, min, max);
+      List<Number> scaled = new ArrayList(data.length);
+      for(double d : data) {
+        scaled.add(d);
+      }
+
+      return scaled;
+    } else {
+      throw new IOException();
+    }
+  }
+
+  private double[] scale(double[] values, double min, double max) {
+
+    double localMin = Double.MAX_VALUE;
+    double localMax = Double.MIN_VALUE;
+    for (double d : values) {
+      if (d > localMax) {
+        localMax = d;
+      }
+
+      if (d < localMin) {
+        localMin = d;
+      }
+    }
+
+    //First scale between 0 and 1
+
+    double[] scaled = new double[values.length];
+
+    for (int i = 0; i < scaled.length; i++) {
+      double x = values[i];
+      double s = (x - localMin) / (localMax - localMin);
+      scaled[i] = s;
+    }
+
+    if (min != 0 || max != 1) {
+      //Next scale between specific min/max
+      double scale = max - min;
+
+      for (int i = 0; i < scaled.length; i++) {
+        double d = scaled[i];
+        scaled[i] = (scale * d) + min;
+      }
+    }
+
+    return scaled;
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/2136fc5e/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 5fe8608..176b030 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
@@ -7113,6 +7113,68 @@ public class StreamExpressionTest extends SolrCloudTestCase {
   }
 
   @Test
+  public void testMinMaxScale() throws Exception {
+    String cexpr = "let(echo=true, a=minMaxScale(matrix(array(1,2,3,4,5), array(10,20,30,40,50))),
" +
+                                  "b=minMaxScale(matrix(array(1,2,3,4,5), array(10,20,30,40,50)),
0, 100)," +
+                                  "c=minMaxScale(array(1,2,3,4,5))," +
+                                  "d=minMaxScale(array(1,2,3,4,5), 0, 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>> matrix = (List<List<Number>>)tuples.get(0).get("a");
+    List<Number> row1 = matrix.get(0);
+    assertEquals(row1.get(0).doubleValue(), 0,0);
+    assertEquals(row1.get(1).doubleValue(), .25,0);
+    assertEquals(row1.get(2).doubleValue(), .5,0);
+    assertEquals(row1.get(3).doubleValue(), .75, 0);
+    assertEquals(row1.get(4).doubleValue(), 1, 0);
+
+    List<Number> row2 = matrix.get(1);
+    assertEquals(row2.get(0).doubleValue(), 0,0);
+    assertEquals(row2.get(1).doubleValue(), .25,0);
+    assertEquals(row2.get(2).doubleValue(), .5,0);
+    assertEquals(row2.get(3).doubleValue(), .75,0);
+    assertEquals(row2.get(4).doubleValue(), 1,0);
+
+    matrix = (List<List<Number>>)tuples.get(0).get("b");
+    row1 = matrix.get(0);
+    assertEquals(row1.get(0).doubleValue(), 0,0);
+    assertEquals(row1.get(1).doubleValue(), 25,0);
+    assertEquals(row1.get(2).doubleValue(), 50,0);
+    assertEquals(row1.get(3).doubleValue(), 75,0);
+    assertEquals(row1.get(4).doubleValue(), 100,0);
+
+    row2 = matrix.get(1);
+    assertEquals(row2.get(0).doubleValue(), 0,0);
+    assertEquals(row2.get(1).doubleValue(), 25,0);
+    assertEquals(row2.get(2).doubleValue(), 50,0);
+    assertEquals(row2.get(3).doubleValue(), 75,0);
+    assertEquals(row2.get(4).doubleValue(), 100,0);
+
+    List<Number> row3= (List<Number>)tuples.get(0).get("c");
+    assertEquals(row3.get(0).doubleValue(), 0,0);
+    assertEquals(row3.get(1).doubleValue(), .25,0);
+    assertEquals(row3.get(2).doubleValue(), .5,0);
+    assertEquals(row3.get(3).doubleValue(), .75,0);
+    assertEquals(row3.get(4).doubleValue(), 1,0);
+
+    List<Number> row4= (List<Number>)tuples.get(0).get("d");
+    assertEquals(row4.get(0).doubleValue(), 0,0);
+    assertEquals(row4.get(1).doubleValue(), 25,0);
+    assertEquals(row4.get(2).doubleValue(), 50,0);
+    assertEquals(row4.get(3).doubleValue(), 75,0);
+    assertEquals(row4.get(4).doubleValue(), 100,0);
+  }
+
+
+  @Test
   public void testMean() throws Exception {
     String cexpr = "mean(array(1,2,3,4,5))";
     ModifiableSolrParams paramsLoc = new ModifiableSolrParams();


Mime
View raw message