mirror of https://github.com/apache/lucene.git
SOLR-131476: Add movingMAD Stream Evaluator
This commit is contained in:
parent
01dfe7bf4b
commit
732281c4cc
|
@ -281,6 +281,7 @@ public class Lang {
|
|||
.withFunctionName("rtrim", RightShiftEvaluator.class)
|
||||
.withFunctionName("repeat", RepeatEvaluator.class)
|
||||
.withFunctionName("natural", NaturalEvaluator.class)
|
||||
.withFunctionName("movingMAD", MovingMADEvaluator.class)
|
||||
|
||||
// Boolean Stream Evaluators
|
||||
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* 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.Locale;
|
||||
|
||||
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
|
||||
|
||||
public class MovingMADEvaluator extends RecursiveNumericEvaluator implements TwoValueWorker {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public MovingMADEvaluator(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)));
|
||||
}
|
||||
if(!(first instanceof List<?>)){
|
||||
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a List",toExpression(constructingFactory), first.getClass().getSimpleName()));
|
||||
}
|
||||
if(!(second instanceof Number)){
|
||||
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
List<?> values = (List<?>)first;
|
||||
int window = ((Number)second).intValue();
|
||||
|
||||
List<Number> moving = new ArrayList<>();
|
||||
DescriptiveStatistics slider = new DescriptiveStatistics(window);
|
||||
for(Object value : values){
|
||||
slider.addValue(((Number)value).doubleValue());
|
||||
if(slider.getN() >= window){
|
||||
double[] doubles = slider.getValues();
|
||||
double mean = slider.getMean();
|
||||
double total = 0;
|
||||
for(double d : doubles) {
|
||||
total+=Math.abs(d-mean);
|
||||
}
|
||||
moving.add(total/window);
|
||||
}
|
||||
}
|
||||
|
||||
return moving;
|
||||
}
|
||||
|
||||
}
|
|
@ -74,7 +74,7 @@ public class TestLang extends LuceneTestCase {
|
|||
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
|
||||
"getAmplitude", "getPhase", "getAngularFrequency", "enclosingDisk", "getCenter", "getRadius",
|
||||
"getSupportPoints", "pairSort", "log10", "plist", "recip", "pivot", "ltrim", "rtrim", "export",
|
||||
"zplot", "natural", "repeat"};
|
||||
"zplot", "natural", "repeat", "movingMAD"};
|
||||
|
||||
@Test
|
||||
public void testLang() {
|
||||
|
|
|
@ -4313,6 +4313,26 @@ public class MathExpressionTest extends SolrCloudTestCase {
|
|||
assertEquals((double) out.get(3), 5.5, .0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMovingMAD() throws Exception {
|
||||
String cexpr = "movingMAD(array(1,2,3,4,5,6,9), 4)";
|
||||
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("return-value");
|
||||
assertTrue(out.size()==4);
|
||||
assertEquals((double) out.get(0).doubleValue(), 1, .0);
|
||||
assertEquals((double) out.get(1).doubleValue(), 1, .0);
|
||||
assertEquals((double) out.get(2).doubleValue(), 1, .0);
|
||||
assertEquals((double) out.get(3).doubleValue(), 1.5, .0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMannWhitney() throws Exception {
|
||||
String cexpr = "mannWhitney(array(0.15,0.10,0.11,0.24,0.08,0.08,0.10,0.10,0.10,0.12,0.04,0.07), " +
|
||||
|
|
Loading…
Reference in New Issue