SOLR-12828: Add oscillate Stream Evaluator to support sine wave analysis

This commit is contained in:
Joel Bernstein 2018-10-03 12:24:48 -04:00
parent d8e40796e2
commit 751bf7db20
8 changed files with 242 additions and 5 deletions

View File

@ -174,6 +174,7 @@ public class Lang {
.withFunctionName("betaDistribution", BetaDistributionEvaluator.class)
.withFunctionName("polyfit", PolyFitEvaluator.class)
.withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
.withFunctionName("harmfit", HarmonicFitEvaluator.class)
.withFunctionName("loess", LoessEvaluator.class)
.withFunctionName("matrix", MatrixEvaluator.class)
.withFunctionName("transpose", TransposeEvaluator.class)
@ -261,7 +262,10 @@ public class Lang {
.withFunctionName("getBaryCenter", GetBaryCenterEvaluator.class)
.withFunctionName("getArea", GetAreaEvaluator.class)
.withFunctionName("getBoundarySize", GetBoundarySizeEvaluator.class)
.withFunctionName("oscillate", OscillateEvaluator.class)
.withFunctionName("getAmplitude", GetAmplitudeEvaluator.class)
.withFunctionName("getPhase", GetPhaseEvaluator.class)
.withFunctionName("getAngularFrequency", GetAngularFrequencyEvaluator.class)
// Boolean Stream Evaluators
.withFunctionName("and", AndEvaluator.class)

View File

@ -0,0 +1,42 @@
/*
* 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.Locale;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class GetAmplitudeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
private static final long serialVersionUID = 1;
public GetAmplitudeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object value) throws IOException {
if(!(value instanceof VectorFunction)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
} else {
VectorFunction vectorFunction = (VectorFunction)value;
return vectorFunction.getFromContext("amplitude");
}
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.Locale;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class GetAngularFrequencyEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
private static final long serialVersionUID = 1;
public GetAngularFrequencyEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object value) throws IOException {
if(!(value instanceof VectorFunction)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
} else {
VectorFunction vectorFunction = (VectorFunction)value;
return vectorFunction.getFromContext("angularFrequency");
}
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.Locale;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class GetPhaseEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
private static final long serialVersionUID = 1;
public GetPhaseEvaluator(StreamExpression expression, StreamFactory factory) throws IOException {
super(expression, factory);
}
@Override
public Object doWork(Object value) throws IOException {
if(!(value instanceof VectorFunction)){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a Vector Function",toExpression(constructingFactory), value.getClass().getSimpleName()));
} else {
VectorFunction vectorFunction = (VectorFunction)value;
return vectorFunction.getFromContext("phase");
}
}
}

View File

@ -70,6 +70,9 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements M
points.add(x[i], y[i]);
}
double[] guess = new HarmonicCurveFitter.ParameterGuesser(points.toList()).guess();
curveFitter = curveFitter.withStartPoint(guess);
double[] coef = curveFitter.fit(points.toList());
HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);
@ -79,6 +82,12 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements M
list.add(yvalue);
}
return list;
VectorFunction vectorFunction = new VectorFunction(pf, list);
vectorFunction.addToContext("amplitude", coef[0]);
vectorFunction.addToContext("angularFrequency", coef[1]);
vectorFunction.addToContext("phase", coef[2]);
return vectorFunction;
}
}

View File

@ -0,0 +1,57 @@
/*
* 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 java.util.ArrayList;
import org.apache.commons.math3.analysis.function.HarmonicOscillator;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class OscillateEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {
protected static final long serialVersionUID = 1L;
public OscillateEvaluator(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 oscillate function takes 3 arguments.");
}
double amp = ((Number)objects[0]).doubleValue();
double om = ((Number)objects[1]).doubleValue();
double phase = ((Number)objects[2]).doubleValue();
HarmonicOscillator pf = new HarmonicOscillator(amp, om, phase);
List list = new ArrayList();
for(int i=0; i<128; i++) {
double yvalue= pf.value(i);
list.add(yvalue);
}
return new VectorFunction(pf, list);
}
}

View File

@ -52,7 +52,7 @@ public class TestLang extends LuceneTestCase {
"poissonDistribution", "enumeratedDistribution", "probability", "sumDifference", "meanDifference",
"primes", "factorial", "movingMedian", "binomialCoefficient", "expMovingAvg", "monteCarlo", "constantDistribution",
"weibullDistribution", "mean", "mode", "logNormalDistribution", "zipFDistribution", "gammaDistribution",
"betaDistribution", "polyfit", "harmonicFit", "loess", "matrix", "transpose", "unitize",
"betaDistribution", "polyfit", "harmonicFit", "harmfit", "loess", "matrix", "transpose", "unitize",
"triangularDistribution", "precision", "minMaxScale", "markovChain", "grandSum",
"scalarAdd", "scalarSubtract", "scalarMultiply", "scalarDivide", "sumRows",
"sumColumns", "diff", "corrPValues", "normalizeSum", "geometricDistribution", "olsRegress",
@ -71,7 +71,8 @@ public class TestLang extends LuceneTestCase {
"cbrt", "coalesce", "uuid", "if", "convert", "valueAt", "memset", "fft", "ifft", "euclidean","manhattan",
"earthMovers", "canberra", "chebyshev", "ones", "zeros", "setValue", "getValue", "knnRegress", "gaussfit",
"outliers", "stream", "getCache", "putCache", "listCache", "removeCache", "zscores", "latlonVectors",
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize"};
"convexHull", "getVertices", "getBaryCenter", "getArea", "getBoundarySize","oscillate",
"getAmplitude", "getPhase", "getAngularFrequency"};
@Test
public void testLang() {

View File

@ -342,7 +342,6 @@ public class MathExpressionTest extends SolrCloudTestCase {
List<Tuple> tuples = getTuples(solrStream);
assertTrue(tuples.size() == 1);
List<List<Number>>locVectors = (List<List<Number>>)tuples.get(0).get("b");
System.out.println(locVectors);
int v=1;
for(List<Number> row : locVectors) {
double lat = row.get(0).doubleValue();
@ -2359,6 +2358,46 @@ public class MathExpressionTest extends SolrCloudTestCase {
assertTrue(out.get(5).intValue() == 72);
}
@Test
public void testOscillate() throws Exception {
String cexpr = "let(echo=true," +
" a=oscillate(10, .3, 2.9)," +
" b=describe(a)," +
" c=getValue(b, min)," +
" d=getValue(b, max)," +
" e=harmfit(a)," +
" f=getAmplitude(e)," +
" g=getAngularFrequency(e)," +
" h=getPhase(e))";
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> wave = (List<Number>)tuples.get(0).get("a");
assertEquals(wave.size(), 128);
Map desc = (Map)tuples.get(0).get("b");
Number min = (Number)tuples.get(0).get("c");
Number max = (Number)tuples.get(0).get("d");
assertEquals(min.doubleValue(), -9.9, .1);
assertEquals(max.doubleValue(), 9.9, .1);
List<Number> wave1 = (List<Number>)tuples.get(0).get("e");
assertEquals(wave1.size(), 128);
Number amp = (Number)tuples.get(0).get("f");
Number freq = (Number)tuples.get(0).get("g");
Number pha = (Number)tuples.get(0).get("h");
assertEquals(amp.doubleValue(), 10, .1);
assertEquals(freq.doubleValue(), .3, .1);
assertEquals(pha.doubleValue(), 2.9, .1);
}
@Test
public void testEbeAdd() throws Exception {
String cexpr = "let(echo=true," +
@ -2410,6 +2449,7 @@ public class MathExpressionTest extends SolrCloudTestCase {
}
@Test
public void testSetAndGetValue() throws Exception {
String cexpr = "let(echo=true," +