SOLR-10765: Add anova Stream Evaluator

This commit is contained in:
Joel Bernstein 2017-05-29 20:47:51 -04:00
parent cb97ad787a
commit 963f43f6c0
3 changed files with 95 additions and 0 deletions

View File

@ -192,6 +192,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("sequence", SequenceEvaluator.class) .withFunctionName("sequence", SequenceEvaluator.class)
.withFunctionName("array", ArrayEvaluator.class) .withFunctionName("array", ArrayEvaluator.class)
.withFunctionName("hist", HistogramEvaluator.class) .withFunctionName("hist", HistogramEvaluator.class)
.withFunctionName("anova", AnovaEvaluator.class)
// metrics // metrics
.withFunctionName("min", MinMetric.class) .withFunctionName("min", MinMetric.class)

View File

@ -0,0 +1,76 @@
/*
* 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 java.util.HashMap;
import java.util.Map;
import org.apache.commons.math3.stat.inference.OneWayAnova;
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 AnovaEvaluator extends ComplexEvaluator implements Expressible {
private static final long serialVersionUID = 1;
public AnovaEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
public Tuple evaluate(Tuple tuple) throws IOException {
List<double[]> list = new ArrayList();
for(StreamEvaluator subEvaluator : subEvaluators) {
List<Number> nums = (List<Number>)subEvaluator.evaluate(tuple);
double[] darray = new double[nums.size()];
for(int i=0; i< nums.size(); i++) {
darray[i]=nums.get(i).doubleValue();
}
list.add(darray);
}
OneWayAnova anova = new OneWayAnova();
double p = anova.anovaPValue(list);
double f = anova.anovaFValue(list);
Map m = new HashMap();
m.put("p-value", p);
m.put("f-ratio", f);
return new Tuple(m);
}
@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());
}
}

View File

@ -5821,6 +5821,24 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertTrue(out.get(5).doubleValue() == 500.23D); assertTrue(out.get(5).doubleValue() == 500.23D);
} }
@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))";
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 out = (Map)tuples.get(0).get("return-value");
assertEquals((double)out.get("p-value"), 0.788298D, .0001);
assertEquals((double)out.get("f-ratio"), 0.24169D, .0001);
}
@Test @Test
public void testScale() throws Exception { public void testScale() throws Exception {
UpdateRequest updateRequest = new UpdateRequest(); UpdateRequest updateRequest = new UpdateRequest();