From 99449c82c9c0d315d74758727d913408a380b143 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 18 Dec 2016 17:11:24 +0300 Subject: [PATCH] Fix context and log issues in Grind --- numass-main/build.gradle | 2 - .../groovy/inr/numass/LaunchGrindShell.groovy | 19 ++++++--- .../inr/numass/NumassWorkspaceSpec.groovy | 41 ------------------- .../scripts/workspace/TestWorkspace.groovy | 2 +- .../main/java/inr/numass/NumassPlugin.java | 2 +- .../inr/numass/actions/PrepareDataAction.java | 10 ++--- .../numass/models/NamedSpectrumCaching.java | 18 ++++---- .../models/sterile/NumassTransmission.java | 2 +- .../inr/numass/utils/ExpressionUtils.java | 15 +++++-- .../java/inr/numass/utils/TritiumUtils.java | 6 +-- .../inr/numass/utils/UnderflowCorrection.java | 2 +- .../workspace/NumassTableFilterTask.java | 19 +++++++-- .../numass/actions/PrepareDataActionTest.java | 8 ++-- 13 files changed, 66 insertions(+), 80 deletions(-) delete mode 100644 numass-main/src/main/groovy/inr/numass/NumassWorkspaceSpec.groovy diff --git a/numass-main/build.gradle b/numass-main/build.gradle index 6d73b5dd..e9234091 100644 --- a/numass-main/build.gradle +++ b/numass-main/build.gradle @@ -11,8 +11,6 @@ dependencies { compile group: 'commons-cli', name: 'commons-cli', version: '1.+' compile group: 'commons-io', name: 'commons-io', version: '2.+' compile project(':dataforge-stat:dataforge-minuit') - compile project(':dataforge-fx') - compile project(':dataforge-plots:plots-jfc') compile project(':numass-storage') compile project(':dataforge-grind:grind-terminal') } diff --git a/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy b/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy index 81531f6d..9b94d86a 100644 --- a/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy +++ b/numass-main/src/main/groovy/inr/numass/LaunchGrindShell.groovy @@ -3,6 +3,7 @@ package inr.numass import hep.dataforge.context.Global import hep.dataforge.grind.GrindWorkspaceBuilder import hep.dataforge.grind.terminal.GrindTerminal +import inr.numass.workspace.* /** * Created by darksnake on 29-Aug-16. @@ -19,19 +20,25 @@ println "Loading config file from $cfgPath" //Global.instance().pluginManager().loadPlugin("inr.numass:numass") println "Starting Grind shell" -if(cfgPath) { +if (cfgPath) { try { GrindTerminal.dumb().launch { - GrindWorkspaceBuilder numass = new GrindWorkspaceBuilder() - .withSpec(NumassWorkspaceSpec) - .from(new File(cfgPath)) - shell.bind("numass", numass) + GrindWorkspaceBuilder numass = new GrindWorkspaceBuilder(it.shell.context).read(new File(cfgPath)).startup { + it.loadTask(NumassPrepareTask) + it.loadTask(NumassTableFilterTask) + it.loadTask(NumassFitScanTask) + it.loadTask(NumassSubstractEmptySourceTask) + it.loadTask(NumassFitScanSummaryTask) + it.loadTask(NumassFitTask) + it.loadTask(NumassFitSummaryTask) + } + it.shell.bind("numass", numass) } } catch (Exception ex) { ex.printStackTrace(); } finally { Global.terminate(); } -} else{ +} else { println "No configuration path. Provide path via --config option" } \ No newline at end of file diff --git a/numass-main/src/main/groovy/inr/numass/NumassWorkspaceSpec.groovy b/numass-main/src/main/groovy/inr/numass/NumassWorkspaceSpec.groovy deleted file mode 100644 index 5a21ff8a..00000000 --- a/numass-main/src/main/groovy/inr/numass/NumassWorkspaceSpec.groovy +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2015 Alexander Nozik. - * - * Licensed 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 inr.numass - -import groovy.transform.CompileStatic -import hep.dataforge.grind.WorkspaceSpec -import inr.numass.workspace.* - -/** - * Created by darksnake on 16-Aug-16. - */ -@CompileStatic -class NumassWorkspaceSpec extends WorkspaceSpec { - - NumassWorkspaceSpec() { - //load tasks - super.task(NumassPrepareTask) - super.task(NumassTableFilterTask) - super.task(NumassFitScanTask) - super.task(NumassSubstractEmptySourceTask) - super.task(NumassFitScanSummaryTask) - super.task(NumassFitTask) - super.task(NumassFitSummaryTask) - } - - -} diff --git a/numass-main/src/main/groovy/inr/numass/scripts/workspace/TestWorkspace.groovy b/numass-main/src/main/groovy/inr/numass/scripts/workspace/TestWorkspace.groovy index e46f2899..8bdcf074 100644 --- a/numass-main/src/main/groovy/inr/numass/scripts/workspace/TestWorkspace.groovy +++ b/numass-main/src/main/groovy/inr/numass/scripts/workspace/TestWorkspace.groovy @@ -7,7 +7,7 @@ import hep.dataforge.grind.GrindWorkspaceBuilder */ -new GrindWorkspaceBuilder().from { +new GrindWorkspaceBuilder().read { new File("D:\\Work\\Numass\\sterile2016\\workspace.groovy") }.runTask("numass.prepare", "fill_2").computeAll() diff --git a/numass-main/src/main/java/inr/numass/NumassPlugin.java b/numass-main/src/main/java/inr/numass/NumassPlugin.java index fd374967..61b48804 100644 --- a/numass-main/src/main/java/inr/numass/NumassPlugin.java +++ b/numass-main/src/main/java/inr/numass/NumassPlugin.java @@ -44,7 +44,7 @@ import org.apache.commons.math3.util.FastMath; * @author Alexander Nozik */ @PluginDef(group = "inr.numass", name = "numass", - dependsOn = {"hep.dataforge:actions", "hep.dataforge:math", "hep.dataforge:MINUIT", "hep.dataforge:plots-jfc"}, + dependsOn = {"hep.dataforge:actions", "hep.dataforge:math", "hep.dataforge:MINUIT"}, description = "Numass data analysis tools") public class NumassPlugin extends BasicPlugin { diff --git a/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java b/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java index b3654dad..74b0822a 100644 --- a/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java +++ b/numass-main/src/main/java/inr/numass/actions/PrepareDataAction.java @@ -37,7 +37,7 @@ import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; -import static inr.numass.utils.TritiumUtils.evaluateExpression; +import static inr.numass.utils.TritiumUtils.pointExpression; /** * @author Darksnake @@ -80,7 +80,7 @@ public class PrepareDataAction extends OneToOneAction { if (meta.hasValue("correction")) { final String correction = meta.getString("correction"); - corrections.add((point) -> evaluateExpression(point, correction)); + corrections.add((point) -> pointExpression(correction, point)); } List dataList = new ArrayList<>(); @@ -153,7 +153,7 @@ public class PrepareDataAction extends OneToOneAction { return new Correction() { @Override public double corr(NMPoint point) { - return evaluateExpression(point, expr); + return pointExpression(expr, point); } @Override @@ -161,7 +161,7 @@ public class PrepareDataAction extends OneToOneAction { if (errExpr.isEmpty()) { return 0; } else { - return evaluateExpression(point, errExpr); + return pointExpression(errExpr, point); } } }; @@ -202,7 +202,7 @@ public class PrepareDataAction extends OneToOneAction { private final Function deadTimeFunction; public DeadTimeCorrection(String expr) { - deadTimeFunction = point -> evaluateExpression(point, expr); + deadTimeFunction = point -> pointExpression(expr, point); } @Override diff --git a/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java b/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java index 128ee84a..6ef315ba 100644 --- a/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java +++ b/numass-main/src/main/java/inr/numass/models/NamedSpectrumCaching.java @@ -15,21 +15,23 @@ */ package inr.numass.models; -import hep.dataforge.stat.parametric.AbstractParametricFunction; -import static hep.dataforge.stat.parametric.FunctionUtils.getSpectrumDerivativeFunction; -import static hep.dataforge.stat.parametric.FunctionUtils.getSpectrumFunction; -import hep.dataforge.stat.parametric.ParametricFunction; import hep.dataforge.maths.MathUtils; import hep.dataforge.maths.NamedVector; import hep.dataforge.names.AbstractNamedSet; +import hep.dataforge.stat.parametric.AbstractParametricFunction; +import hep.dataforge.stat.parametric.ParametricFunction; import hep.dataforge.values.NamedValueSet; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.interpolation.SplineInterpolator; import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import static hep.dataforge.stat.parametric.FunctionUtils.getSpectrumDerivativeFunction; +import static hep.dataforge.stat.parametric.FunctionUtils.getSpectrumFunction; + /** * * @author Darksnake @@ -52,7 +54,7 @@ public class NamedSpectrumCaching extends AbstractParametricFunction { this.a = a; this.b = b; this.source = spectrum; - spectrumDerivCache = new HashMap<>(source.getDimension()); + spectrumDerivCache = new HashMap<>(source.size()); // spectrumDerivCache = new CacheElement[source.getDimension()]; } diff --git a/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java b/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java index 8647a34d..41f42c11 100644 --- a/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java +++ b/numass-main/src/main/java/inr/numass/models/sterile/NumassTransmission.java @@ -41,7 +41,7 @@ public class NumassTransmission extends AbstractParametricBiFunction { Map binding = new HashMap<>(); binding.put("Ei", Ei); binding.put("Ef", Ef); - return ExpressionUtils.evaluate(trapFuncStr, binding); + return ExpressionUtils.function(trapFuncStr, binding); }; } } else { diff --git a/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java index 8324a503..9f1eb62c 100644 --- a/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java +++ b/numass-main/src/main/java/inr/numass/utils/ExpressionUtils.java @@ -8,7 +8,7 @@ package inr.numass.utils; import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.lang.Script; -import hep.dataforge.utils.Utils; +import hep.dataforge.utils.Misc; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; @@ -18,7 +18,7 @@ import java.util.Map; * @author Alexander Nozik */ public class ExpressionUtils { - private static final Map cache = Utils.getLRUCache(100); + private static final Map cache = Misc.getLRUCache(100); private static final GroovyShell shell; static { @@ -38,7 +38,7 @@ public class ExpressionUtils { } - public static double evaluate(String expression, Map binding) { + public static double function(String expression, Map binding) { synchronized (cache) { Binding b = new Binding(binding); Script script = getScript(expression); @@ -46,4 +46,13 @@ public class ExpressionUtils { return ((Number) script.run()).doubleValue(); } } + + public static boolean condition(String expression, Map binding){ + synchronized (cache) { + Binding b = new Binding(binding); + Script script = getScript(expression); + script.setBinding(b); + return (boolean) script.run(); + } + } } diff --git a/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java b/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java index 340c2d02..30b00940 100644 --- a/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java +++ b/numass-main/src/main/java/inr/numass/utils/TritiumUtils.java @@ -129,16 +129,16 @@ public class TritiumUtils { /** * Evaluate groovy expression using numass point as parameter * - * @param point * @param expression + * @param point * @return */ - public static double evaluateExpression(NMPoint point, String expression) { + public static double pointExpression(String expression, NMPoint point) { Map exprParams = new HashMap<>(); exprParams.put("T", point.getLength()); exprParams.put("U", point.getUread()); exprParams.put("cr", ((double) point.getEventsCount()) / point.getLength()); exprParams.put("point", point); - return ExpressionUtils.evaluate(expression, exprParams); + return ExpressionUtils.function(expression, exprParams); } } diff --git a/numass-main/src/main/java/inr/numass/utils/UnderflowCorrection.java b/numass-main/src/main/java/inr/numass/utils/UnderflowCorrection.java index 9a84b1e5..16729eef 100644 --- a/numass-main/src/main/java/inr/numass/utils/UnderflowCorrection.java +++ b/numass-main/src/main/java/inr/numass/utils/UnderflowCorrection.java @@ -30,7 +30,7 @@ public class UnderflowCorrection { public double get(Logable log, Meta meta, NMPoint point) { if (point.getUset() >= meta.getDouble("underflow.threshold", 17000)) { if (meta.hasValue("underflow.function")) { - return TritiumUtils.evaluateExpression(point, meta.getString("underflow.function")); + return TritiumUtils.pointExpression(meta.getString("underflow.function"), point); } else { return 1; } diff --git a/numass-main/src/main/java/inr/numass/workspace/NumassTableFilterTask.java b/numass-main/src/main/java/inr/numass/workspace/NumassTableFilterTask.java index d83d224b..ec39b9c8 100644 --- a/numass-main/src/main/java/inr/numass/workspace/NumassTableFilterTask.java +++ b/numass-main/src/main/java/inr/numass/workspace/NumassTableFilterTask.java @@ -7,10 +7,14 @@ import hep.dataforge.data.DataNode; import hep.dataforge.description.TypedActionDef; import hep.dataforge.meta.Laminate; import hep.dataforge.meta.MetaBuilder; +import hep.dataforge.tables.DataPoint; import hep.dataforge.tables.Table; import hep.dataforge.tables.TableTransform; import hep.dataforge.workspace.SingleActionTask; import hep.dataforge.workspace.TaskModel; +import inr.numass.utils.ExpressionUtils; + +import java.util.function.Predicate; /** * Created by darksnake on 13-Aug-16. @@ -47,10 +51,17 @@ public class NumassTableFilterTask extends SingleActionTask { private class FilterTableAction extends OneToOneAction { @Override protected Table execute(Context context, String name, Table input, Laminate inputMeta) { - double uLo = inputMeta.getDouble("filter.from", 0); - double uHi = inputMeta.getDouble("filter.to", Double.POSITIVE_INFINITY); - getLogger(inputMeta).debug("Filtering finished"); - return TableTransform.filter(input, "Uset", uLo, uHi); + if (inputMeta.hasValue("filter.from") || inputMeta.hasValue("filter.to")) { + double uLo = inputMeta.getDouble("filter.from", 0); + double uHi = inputMeta.getDouble("filter.to", Double.POSITIVE_INFINITY); + getLogger(inputMeta).debug("Filtering finished"); + return TableTransform.filter(input, "Uset", uLo, uHi); + } else if (inputMeta.hasValue("filter.condition")) { + Predicate predicate = (dp) -> ExpressionUtils.condition(inputMeta.getString("filter.condition"), dp.asMap()); + return TableTransform.filter(input, predicate); + } else { + throw new RuntimeException("No filtering condition specified"); + } } } } diff --git a/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java b/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java index 7b83d3bb..8c9237d4 100644 --- a/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java +++ b/numass-main/src/test/java/inr/numass/actions/PrepareDataActionTest.java @@ -6,11 +6,11 @@ package inr.numass.actions; import inr.numass.utils.ExpressionUtils; +import org.junit.Assert; +import org.junit.Test; + import java.util.HashMap; import java.util.Map; -import org.junit.Assert; - -import org.junit.Test; /** * @@ -25,7 +25,7 @@ public class PrepareDataActionTest { public void testExpression() { Map exprParams = new HashMap<>(); exprParams.put("U", 18000d); - double correctionFactor = ExpressionUtils.evaluate("1 + 13.265 * exp(- U / 2343.4)", exprParams); + double correctionFactor = ExpressionUtils.function("1 + 13.265 * exp(- U / 2343.4)", exprParams); Assert.assertEquals("Testing expression calculation", 1.006125, correctionFactor, 1e-5); }