mirror of https://github.com/apache/lucene.git
SOLR-11923: Add bicubicSpline Stream Evaluator
This commit is contained in:
parent
e08eac421a
commit
50c17b92bd
|
@ -317,6 +317,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
|
|||
.withFunctionName("l1norm", L1NormEvaluator.class)
|
||||
.withFunctionName("linfnorm", LInfNormEvaluator.class)
|
||||
.withFunctionName("matrixMult", MatrixMultiplyEvaluator.class)
|
||||
.withFunctionName("bicubicSpline", BicubicSplineEvaluator.class)
|
||||
|
||||
// Boolean Stream Evaluators
|
||||
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* 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.List;
|
||||
|
||||
import org.apache.commons.math3.analysis.BivariateFunction;
|
||||
import org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
|
||||
|
||||
public class BicubicSplineEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public BicubicSplineEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
|
||||
super(expression, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doWork(Object... objects) throws IOException {
|
||||
|
||||
if(objects.length != 3) {
|
||||
throw new IOException("The bicubicSpline function requires three paremeters,"+objects.length+" found.");
|
||||
}
|
||||
|
||||
Object first = objects[0];
|
||||
Object second = objects[1];
|
||||
Object third = objects[2];
|
||||
|
||||
double[] x = null;
|
||||
double[] y = null;
|
||||
double[][] grid = null;
|
||||
|
||||
if(first instanceof List && second instanceof List && third instanceof Matrix) {
|
||||
List<Number> xlist = (List<Number>) first;
|
||||
x = new double[xlist.size()];
|
||||
|
||||
for(int i=0; i<x.length; i++) {
|
||||
x[i]=xlist.get(i).doubleValue();
|
||||
}
|
||||
|
||||
List<Number> ylist = (List<Number>) second;
|
||||
y = new double[ylist.size()];
|
||||
|
||||
for(int i=0; i<y.length; i++) {
|
||||
y[i] = ylist.get(i).doubleValue();
|
||||
}
|
||||
|
||||
Matrix matrix = (Matrix)third;
|
||||
grid = matrix.getData();
|
||||
|
||||
PiecewiseBicubicSplineInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
|
||||
BivariateFunction bivariateFunction = interpolator.interpolate(x, y, grid);
|
||||
return bivariateFunction;
|
||||
} else {
|
||||
throw new IOException("The bicubicSpline function expects two numeric arrays and a matrix as parameters.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,11 +22,12 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.math3.analysis.BivariateFunction;
|
||||
import org.apache.commons.math3.analysis.UnivariateFunction;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
|
||||
|
||||
public class PredictEvaluator extends RecursiveObjectEvaluator implements TwoValueWorker {
|
||||
public class PredictEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public PredictEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
|
||||
|
@ -34,15 +35,15 @@ public class PredictEvaluator extends RecursiveObjectEvaluator implements TwoVal
|
|||
}
|
||||
|
||||
@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)));
|
||||
public Object doWork(Object ... objects) throws IOException {
|
||||
if(objects.length != 2 && objects.length != 3) {
|
||||
throw new IOException("The predict function expects 2 or 3 parameters.");
|
||||
}
|
||||
|
||||
if (!(first instanceof VectorFunction) && !(first instanceof RegressionEvaluator.RegressionTuple) && !(first instanceof OLSRegressionEvaluator.MultipleRegressionTuple)) {
|
||||
Object first = objects[0];
|
||||
Object second = objects[1];
|
||||
|
||||
if (!(first instanceof BivariateFunction) && !(first instanceof VectorFunction) && !(first instanceof RegressionEvaluator.RegressionTuple) && !(first instanceof OLSRegressionEvaluator.MultipleRegressionTuple)) {
|
||||
throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a RegressionTuple", toExpression(constructingFactory), first.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
|
@ -91,8 +92,37 @@ public class PredictEvaluator extends RecursiveObjectEvaluator implements TwoVal
|
|||
} else {
|
||||
return ((List<?>) second).stream().map(value -> univariateFunction.value(((Number) value).doubleValue())).collect(Collectors.toList());
|
||||
}
|
||||
} else if(first instanceof BivariateFunction) {
|
||||
BivariateFunction bivariateFunction = (BivariateFunction) first;
|
||||
if (objects.length == 3) {
|
||||
Object third = objects[2];
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
if (second instanceof Number && third instanceof Number) {
|
||||
x = ((Number) second).doubleValue();
|
||||
y = ((Number) third).doubleValue();
|
||||
return bivariateFunction.value(x, y);
|
||||
} else {
|
||||
throw new IOException("BivariateFunction requires two numberic parameters.");
|
||||
}
|
||||
} else if (objects.length == 2) {
|
||||
if (second instanceof Matrix) {
|
||||
Matrix m = (Matrix) second;
|
||||
double[][] data = m.getData();
|
||||
if (data[0].length == 2) {
|
||||
List<Number> out = new ArrayList();
|
||||
for (double[] row : data) {
|
||||
out.add(bivariateFunction.value(row[0], row[1]));
|
||||
}
|
||||
return out;
|
||||
} else {
|
||||
throw new IOException("Bivariate Function expects a matrix with two columns");
|
||||
}
|
||||
} else {
|
||||
throw new IOException("Bivariate Function requires a matrix parameter.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8176,6 +8176,45 @@ public class StreamExpressionTest extends SolrCloudTestCase {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBicubicSpline() throws Exception {
|
||||
String cexpr = "let(echo=true," +
|
||||
" a=array(300, 400, 500, 600, 700), " +
|
||||
" b=array(340, 410, 495, 590, 640)," +
|
||||
" c=array(600, 395, 550, 510, 705),"+
|
||||
" d=array(500, 420, 510, 601, 690),"+
|
||||
" e=array(420, 411, 511, 611, 711),"+
|
||||
" f=matrix(a, b, c, d, e),"+
|
||||
" x=array(1,2,3,4,5),"+
|
||||
" y=array(100, 200, 300, 400, 500),"+
|
||||
" bspline=bicubicSpline(x, y, f), " +
|
||||
" p1=predict(bspline, 1.5, 250)," +
|
||||
" p2=predict(bspline, 3.5, 350)," +
|
||||
" p3=predict(bspline, 4.5, 450)," +
|
||||
" p4=predict(bspline,matrix(array(1.5, 250), array(3.5, 350), array(4.5, 450))))";
|
||||
|
||||
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);
|
||||
Number p1 = (Number)tuples.get(0).get("p1");
|
||||
assertEquals(p1.doubleValue(), 449.7837701612903, 0.0);
|
||||
Number p2 = (Number)tuples.get(0).get("p2");
|
||||
assertEquals(p2.doubleValue(), 536.8916383774491, 0.0);
|
||||
Number p3 = (Number)tuples.get(0).get("p3");
|
||||
assertEquals(p3.doubleValue(), 659.921875, 0.0);
|
||||
List<Number> p4 = (List<Number>)tuples.get(0).get("p4");
|
||||
assertEquals(p4.get(0).doubleValue(), 449.7837701612903, 0.0);
|
||||
assertEquals(p4.get(1).doubleValue(), 536.8916383774491, 0.0);
|
||||
assertEquals(p4.get(2).doubleValue(), 659.921875, 0.0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAkima() throws Exception {
|
||||
String cexpr = "let(echo=true," +
|
||||
|
|
Loading…
Reference in New Issue