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-11414: Add gammaDistribution Stream Evaluator
Date Thu, 28 Sep 2017 18:24:04 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 790533da1 -> 3f94f2ed4


SOLR-11414: Add gammaDistribution 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/3f94f2ed
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/3f94f2ed
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/3f94f2ed

Branch: refs/heads/master
Commit: 3f94f2ed4cd3e0126a588952946855337c940959
Parents: 790533d
Author: Joel Bernstein <jbernste@apache.org>
Authored: Thu Sep 28 14:08:38 2017 -0400
Committer: Joel Bernstein <jbernste@apache.org>
Committed: Thu Sep 28 14:08:38 2017 -0400

----------------------------------------------------------------------
 .../org/apache/solr/handler/StreamHandler.java  |  2 +-
 .../io/eval/GammaDistributionEvaluator.java     | 48 ++++++++++++++++++
 .../solrj/io/stream/StreamExpressionTest.java   | 51 ++++++++++++++++++++
 3 files changed, 100 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3f94f2ed/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 559ccea..127c44a 100644
--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java
@@ -306,7 +306,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
         .withFunctionName("mode", ModeEvaluator.class)
         .withFunctionName("logNormalDistribution", LogNormalDistributionEvaluator.class)
         .withFunctionName("zipFDistribution", ZipFDistributionEvaluator.class)
-
+        .withFunctionName("gammaDistribution", GammaDistributionEvaluator.class)
         // Boolean Stream Evaluators
 
         .withFunctionName("and", AndEvaluator.class)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3f94f2ed/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GammaDistributionEvaluator.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GammaDistributionEvaluator.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GammaDistributionEvaluator.java
new file mode 100644
index 0000000..52edc6e
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/GammaDistributionEvaluator.java
@@ -0,0 +1,48 @@
+/*
+ * 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.distribution.GammaDistribution;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+
+public class GammaDistributionEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker
{
+
+  private static final long serialVersionUID = 1;
+
+  public GammaDistributionEvaluator(StreamExpression expression, StreamFactory factory) throws
IOException {
+    super(expression, factory);
+  }
+
+  @Override
+  public Object doWork(Object first, Object second) throws IOException{
+    if(null == first){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found
for the first value",toExpression(constructingFactory)));
+    }
+    if(null == second){
+      throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found
for the second value",toExpression(constructingFactory)));
+    }
+
+    Number shape = (Number)first;
+    Number scale = (Number)second;
+
+    return new GammaDistribution(shape.doubleValue(), scale.doubleValue());
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3f94f2ed/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 1326740..6799371 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
@@ -6398,6 +6398,8 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     }
   }
 
+
+
   @Test
   public void testEnumeratedDistribution() throws Exception {
     String cexpr = "let(a=uniformIntegerDistribution(1, 10)," +
@@ -6625,6 +6627,55 @@ public class StreamExpressionTest extends SolrCloudTestCase {
     assertTrue(f.doubleValue() < g.doubleValue());
   }
 
+
+  @Test
+  public void testGammaDistribution() throws Exception {
+    String cexpr = "let(echo=true, " +
+        "a=describe(sample(gammaDistribution(1, 10),10000)), " +
+        "b=describe(sample(gammaDistribution(3, 10),10000)), " +
+        "c=describe(sample(gammaDistribution(5, 10),10000))," +
+        "d=describe(sample(gammaDistribution(7, 10),10000))," +
+        "e=mean(sample(gammaDistribution(1, 10),10000))," +
+        "f=mean(sample(gammaDistribution(1, 20),10000))," +
+        "g=mean(sample(gammaDistribution(1, 30),10000)))";
+
+    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);
+    Map a = (Map)tuples.get(0).get("a");
+    Map b = (Map)tuples.get(0).get("b");
+    Map c = (Map)tuples.get(0).get("c");
+    Map d = (Map)tuples.get(0).get("d");
+
+    Number sa = (Number)a.get("skewness");
+    Number sb = (Number)b.get("skewness");
+    Number sc = (Number)c.get("skewness");
+    Number sd = (Number)d.get("skewness");
+
+    //Test shape change
+    assertTrue(sa.doubleValue() > sb.doubleValue());
+    assertTrue(sb.doubleValue() > sc.doubleValue());
+    assertTrue(sc.doubleValue() > sd.doubleValue());
+
+    //Test scale change
+
+    Number e = (Number)tuples.get(0).get("e");
+    Number f = (Number)tuples.get(0).get("f");
+    Number g = (Number)tuples.get(0).get("g");
+
+    assertTrue(e.doubleValue() < f.doubleValue());
+    assertTrue(f.doubleValue() < g.doubleValue());
+  }
+
+
+
+
   @Test
   public void testLogNormalDistribution() throws Exception {
     String cexpr = "let(echo=true, " +


Mime
View raw message