mirror of https://github.com/apache/lucene.git
SOLR-11429: Add loess Stream Evaluator to support Local Regression interpolation
This commit is contained in:
parent
f9c066e36c
commit
a6e1237451
|
@ -267,6 +267,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
|
|||
.withFunctionName("polyfit", PolyFitEvaluator.class)
|
||||
.withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class)
|
||||
.withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
|
||||
.withFunctionName("loess", LoessEvaluator.class)
|
||||
|
||||
// Boolean Stream Evaluators
|
||||
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* 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.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.math3.analysis.interpolation.LoessInterpolator;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
|
||||
|
||||
public class LoessEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
private double bandwidth = .25;
|
||||
private int robustIterations = 2;
|
||||
|
||||
public LoessEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
|
||||
super(expression, factory);
|
||||
|
||||
List<StreamExpressionNamedParameter> namedParams = factory.getNamedOperands(expression);
|
||||
|
||||
for(StreamExpressionNamedParameter namedParam : namedParams){
|
||||
if(namedParam.getName().equals("bandwidth")){
|
||||
this.bandwidth = Double.parseDouble(namedParam.getParameter().toString().trim());
|
||||
} else if(namedParam.getName().equals("robustIterations")) {
|
||||
this.robustIterations = Integer.parseInt(namedParam.getParameter().toString().trim());
|
||||
} else {
|
||||
throw new IOException("Unexpected named parameter:"+namedParam.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doWork(Object... objects) throws IOException{
|
||||
|
||||
Object first = objects[0];
|
||||
|
||||
double[] x = null;
|
||||
double[] y = null;
|
||||
|
||||
if(objects.length == 1) {
|
||||
//Only the y values passed
|
||||
y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
|
||||
x = new double[y.length];
|
||||
for(int i=0; i<y.length; i++) {
|
||||
x[i] = i;
|
||||
}
|
||||
} else if(objects.length == 2) {
|
||||
Object second = objects[1];
|
||||
x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
|
||||
y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
|
||||
}
|
||||
|
||||
LoessInterpolator interpolator = new LoessInterpolator(bandwidth, robustIterations);
|
||||
double[] smooth = interpolator.smooth(x, y);
|
||||
|
||||
List list = new ArrayList();
|
||||
for(double yvalue : smooth) {
|
||||
list.add(yvalue);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -171,7 +171,7 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorker
|
|||
|
||||
Set<String> namedParameters = factory.getNamedOperands(expression).stream().map(param -> param.getName()).collect(Collectors.toSet());
|
||||
long ignorableCount = ignoredNamedParameters.stream().filter(name -> namedParameters.contains(name)).count();
|
||||
|
||||
/*
|
||||
if(0 != expression.getParameters().size() - containedEvaluators.size() - ignorableCount){
|
||||
if(namedParameters.isEmpty()){
|
||||
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - unknown operands found - expecting only StreamEvaluators or field names", expression));
|
||||
|
@ -180,6 +180,7 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorker
|
|||
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - unknown operands found - expecting only StreamEvaluators, field names, or named parameters [%s]", expression, namedParameters.stream().collect(Collectors.joining(","))));
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6635,6 +6635,35 @@ public class StreamExpressionTest extends SolrCloudTestCase {
|
|||
assertTrue(out.get(7).intValue() == 1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLoess() throws Exception {
|
||||
String cexpr = "let(echo=true," +
|
||||
" a=array(0,1,2,3,4,5,6,7)," +
|
||||
" fit=loess(a))";
|
||||
|
||||
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("fit");
|
||||
assertTrue(out.size() == 8);
|
||||
assertTrue(out.get(0).intValue() == 0);
|
||||
assertTrue(out.get(1).intValue() == 1);
|
||||
assertTrue(out.get(2).intValue() == 2);
|
||||
assertTrue(out.get(3).intValue() == 3);
|
||||
assertTrue(out.get(4).intValue() == 4);
|
||||
assertTrue(out.get(5).intValue() == 5);
|
||||
assertTrue(out.get(6).intValue() == 6);
|
||||
assertTrue(out.get(7).intValue() == 7);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHarmonicFit() throws Exception {
|
||||
String cexpr = "let(a=sin(sequence(100, 1, 6)), b=harmonicFit(a), s=ebeSubtract(a, b))";
|
||||
|
|
Loading…
Reference in New Issue