mirror of https://github.com/apache/lucene.git
SOLR-10680: Add minMaxScale Stream Evaluator
This commit is contained in:
parent
dc6119b85d
commit
e915078707
|
@ -273,6 +273,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
|
|||
.withFunctionName("unit", UnitEvaluator.class)
|
||||
.withFunctionName("triangularDistribution", TriangularDistributionEvaluator.class)
|
||||
.withFunctionName("precision", PrecisionEvaluator.class)
|
||||
.withFunctionName("minMaxScale", MinMaxScaleEvaluator.class)
|
||||
|
||||
// Boolean Stream Evaluators
|
||||
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* 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 org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
|
||||
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
|
||||
|
||||
public class MinMaxScaleEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public MinMaxScaleEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
|
||||
super(expression, factory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object doWork(Object... values) throws IOException {
|
||||
|
||||
if(null == values){
|
||||
return null;
|
||||
}
|
||||
|
||||
double min = 0;
|
||||
double max = 1;
|
||||
|
||||
if(values.length == 3) {
|
||||
min = ((Number)values[1]).doubleValue();
|
||||
max = ((Number)values[2]).doubleValue();
|
||||
}
|
||||
|
||||
if(values[0] instanceof Matrix) {
|
||||
Matrix matrix = (Matrix)values[0];
|
||||
double[][] data = matrix.getData();
|
||||
double[][] scaled = new double[data.length][];
|
||||
for(int i=0; i<scaled.length; i++) {
|
||||
double[] row = data[i];
|
||||
scaled[i] = scale(row, min, max);
|
||||
}
|
||||
|
||||
return new Matrix(scaled);
|
||||
|
||||
} else if(values[0] instanceof List) {
|
||||
List<Number> vec = (List)values[0];
|
||||
double[] data = new double[vec.size()];
|
||||
|
||||
for(int i=0; i<vec.size(); i++) {
|
||||
data[i] = vec.get(i).doubleValue();
|
||||
}
|
||||
|
||||
data = scale(data, min, max);
|
||||
List<Number> scaled = new ArrayList(data.length);
|
||||
for(double d : data) {
|
||||
scaled.add(d);
|
||||
}
|
||||
|
||||
return scaled;
|
||||
} else {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
private double[] scale(double[] values, double min, double max) {
|
||||
|
||||
double localMin = Double.MAX_VALUE;
|
||||
double localMax = Double.MIN_VALUE;
|
||||
for (double d : values) {
|
||||
if (d > localMax) {
|
||||
localMax = d;
|
||||
}
|
||||
|
||||
if (d < localMin) {
|
||||
localMin = d;
|
||||
}
|
||||
}
|
||||
|
||||
//First scale between 0 and 1
|
||||
|
||||
double[] scaled = new double[values.length];
|
||||
|
||||
for (int i = 0; i < scaled.length; i++) {
|
||||
double x = values[i];
|
||||
double s = (x - localMin) / (localMax - localMin);
|
||||
scaled[i] = s;
|
||||
}
|
||||
|
||||
if (min != 0 || max != 1) {
|
||||
//Next scale between specific min/max
|
||||
double scale = max - min;
|
||||
|
||||
for (int i = 0; i < scaled.length; i++) {
|
||||
double d = scaled[i];
|
||||
scaled[i] = (scale * d) + min;
|
||||
}
|
||||
}
|
||||
|
||||
return scaled;
|
||||
}
|
||||
}
|
|
@ -7112,6 +7112,68 @@ public class StreamExpressionTest extends SolrCloudTestCase {
|
|||
assertEquals(num, 1.4445, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinMaxScale() throws Exception {
|
||||
String cexpr = "let(echo=true, a=minMaxScale(matrix(array(1,2,3,4,5), array(10,20,30,40,50))), " +
|
||||
"b=minMaxScale(matrix(array(1,2,3,4,5), array(10,20,30,40,50)), 0, 100)," +
|
||||
"c=minMaxScale(array(1,2,3,4,5))," +
|
||||
"d=minMaxScale(array(1,2,3,4,5), 0, 100))";
|
||||
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<List<Number>> matrix = (List<List<Number>>)tuples.get(0).get("a");
|
||||
List<Number> row1 = matrix.get(0);
|
||||
assertEquals(row1.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row1.get(1).doubleValue(), .25,0);
|
||||
assertEquals(row1.get(2).doubleValue(), .5,0);
|
||||
assertEquals(row1.get(3).doubleValue(), .75, 0);
|
||||
assertEquals(row1.get(4).doubleValue(), 1, 0);
|
||||
|
||||
List<Number> row2 = matrix.get(1);
|
||||
assertEquals(row2.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row2.get(1).doubleValue(), .25,0);
|
||||
assertEquals(row2.get(2).doubleValue(), .5,0);
|
||||
assertEquals(row2.get(3).doubleValue(), .75,0);
|
||||
assertEquals(row2.get(4).doubleValue(), 1,0);
|
||||
|
||||
matrix = (List<List<Number>>)tuples.get(0).get("b");
|
||||
row1 = matrix.get(0);
|
||||
assertEquals(row1.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row1.get(1).doubleValue(), 25,0);
|
||||
assertEquals(row1.get(2).doubleValue(), 50,0);
|
||||
assertEquals(row1.get(3).doubleValue(), 75,0);
|
||||
assertEquals(row1.get(4).doubleValue(), 100,0);
|
||||
|
||||
row2 = matrix.get(1);
|
||||
assertEquals(row2.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row2.get(1).doubleValue(), 25,0);
|
||||
assertEquals(row2.get(2).doubleValue(), 50,0);
|
||||
assertEquals(row2.get(3).doubleValue(), 75,0);
|
||||
assertEquals(row2.get(4).doubleValue(), 100,0);
|
||||
|
||||
List<Number> row3= (List<Number>)tuples.get(0).get("c");
|
||||
assertEquals(row3.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row3.get(1).doubleValue(), .25,0);
|
||||
assertEquals(row3.get(2).doubleValue(), .5,0);
|
||||
assertEquals(row3.get(3).doubleValue(), .75,0);
|
||||
assertEquals(row3.get(4).doubleValue(), 1,0);
|
||||
|
||||
List<Number> row4= (List<Number>)tuples.get(0).get("d");
|
||||
assertEquals(row4.get(0).doubleValue(), 0,0);
|
||||
assertEquals(row4.get(1).doubleValue(), 25,0);
|
||||
assertEquals(row4.get(2).doubleValue(), 50,0);
|
||||
assertEquals(row4.get(3).doubleValue(), 75,0);
|
||||
assertEquals(row4.get(4).doubleValue(), 100,0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMean() throws Exception {
|
||||
String cexpr = "mean(array(1,2,3,4,5))";
|
||||
|
|
Loading…
Reference in New Issue