Search in sources :

Example 51 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testDenseWindow.

@Test
public void testDenseWindow() {
    for (String text : new String[] { "from 2 hours ago to 30 minutes ago", "starting from 2 hours until 30 minutes", "starting from 2 hours ago until 30 minutes ago", "starting from 30 minutes ago until 2 hours ago", "from 30 minutes ago to 2 hours ago " }) {
        Window w = WindowProcessor.process(text);
        /*
    A dense window starting 2 hour ago and continuing until 30 minutes ago
     */
        Date now = new Date();
        List<Range<Long>> intervals = w.toIntervals(now.getTime());
        Assert.assertEquals(1, intervals.size());
        assertEquals(now.getTime() - TimeUnit.HOURS.toMillis(2), intervals.get(0).getMinimum());
        assertEquals(now.getTime() - TimeUnit.MINUTES.toMillis(30), intervals.get(0).getMaximum());
    }
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.Test)

Example 52 with Range

use of org.apache.commons.lang3.Range in project webanno by webanno.

the class TypeSystemAnalysis method isSlotFeature.

private boolean isSlotFeature(TypeSystem aTS, Feature aFeature) {
    // Slot features are multi-valued
    if (!FSUtil.isMultiValuedFeature(aTS, aFeature)) {
        return false;
    }
    // The component type is the link type - it must be present
    Type linkType = aFeature.getRange().getComponentType();
    if (linkType == null) {
        return false;
    }
    // The range of the slot feature is its link type which must inherit from TOP
    if (!aTS.getTopType().equals(aTS.getParent(linkType))) {
        return false;
    }
    // The link feature must have exactly two features (link-with-role)
    if (linkType.getFeatures().size() != 2) {
        return false;
    }
    Optional<Feature> roleFeature = linkType.getFeatures().stream().filter(f -> f.getRange().getName().equals(CAS.TYPE_NAME_STRING)).findFirst();
    if (!roleFeature.isPresent()) {
        return false;
    }
    Optional<Feature> linkFeature = linkType.getFeatures().stream().filter(f -> !f.getRange().isPrimitive()).findFirst();
    if (!linkFeature.isPresent()) {
        return false;
    }
    // Hm, ok, so this looks like a slot feature.
    return true;
}
Also used : FEAT_REL_SOURCE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_SOURCE) FEAT_REL_TARGET(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.FEAT_REL_TARGET) TypeDescription(org.apache.uima.resource.metadata.TypeDescription) LoggerFactory(org.slf4j.LoggerFactory) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) HashMap(java.util.HashMap) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) FSUtil(org.apache.uima.fit.util.FSUtil) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) HashSet(java.util.HashSet) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) Arrays.asList(java.util.Arrays.asList) ListValuedMap(org.apache.commons.collections4.ListValuedMap) Map(java.util.Map) TypeSystem(org.apache.uima.cas.TypeSystem) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Logger(org.slf4j.Logger) MultiValueMode(de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) Set(java.util.Set) RELATION_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.RELATION_TYPE) SPAN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE) Collectors(java.util.stream.Collectors) CasCreationUtils(org.apache.uima.util.CasCreationUtils) List(java.util.List) FeatureDescription(org.apache.uima.resource.metadata.FeatureDescription) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) StringUtils.removeEnd(org.apache.commons.lang3.StringUtils.removeEnd) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) CHAIN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CHAIN_TYPE) Optional(java.util.Optional) Type(org.apache.uima.cas.Type) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 53 with Range

use of org.apache.commons.lang3.Range in project sirix by sirixdb.

the class AbstractSunburstGUI method style.

/**
 * Style menu.
 */
protected void style() {
    final Group ctrl = mControlP5.addGroup("menu", 15, 25, 35);
    ctrl.setColorLabel(mParent.color(255));
    ctrl.setColorBackground(mParent.color(100));
    ctrl.close();
    mParent.colorMode(PConstants.RGB, 255, 255, 255);
    final int backgroundColor = 0x99ffffff;
    int i = 0;
    for (final Slider slider : mSliders) {
        slider.setGroup(ctrl);
        slider.setId(i);
        final Label label = slider.getCaptionLabel();
        label.toUpperCase(true);
        label.setColor(mParent.color(0));
        label.setColorBackground(backgroundColor);
        final ControllerStyle style = label.getStyle();
        style.padding(4, 0, 1, 3);
        style.marginTop = -4;
        style.marginLeft = 0;
        style.marginRight = -14;
        slider.plugTo(mControl);
        i++;
    }
    i = 0;
    for (final Range range : mRanges) {
        range.setGroup(ctrl);
        range.setId(i);
        final Label label = range.getCaptionLabel();
        label.toUpperCase(true);
        label.setColor(mParent.color(0));
        label.setColorBackground(backgroundColor);
        final ControllerStyle style = label.getStyle();
        style.padding(4, 0, 1, 3);
        style.marginTop = -4;
        range.plugTo(mControl);
        i++;
    }
    i = 0;
    for (final Toggle toggle : mToggles) {
        toggle.setGroup(ctrl);
        toggle.setId(i);
        final Label label = toggle.getCaptionLabel();
        label.setColor(mParent.color(0));
        label.setColorBackground(backgroundColor);
        final ControllerStyle style = label.getStyle();
        style.padding(4, 3, 1, 3);
        style.marginTop = -19;
        style.marginLeft = 18;
        style.marginRight = 5;
        toggle.plugTo(mControl);
        i++;
    }
    mParent.colorMode(PConstants.HSB, 360, 100, 100);
    mParent.textLeading(14);
    mParent.textAlign(PConstants.LEFT, PConstants.TOP);
    mParent.cursor(PConstants.CROSS);
}
Also used : Group(controlP5.Group) ControllerStyle(controlP5.ControllerStyle) Slider(controlP5.Slider) Toggle(controlP5.Toggle) Label(controlP5.Label) Range(controlP5.Range)

Example 54 with Range

use of org.apache.commons.lang3.Range in project drill by axbaretto.

the class LocalFunctionRegistry method registerOperatorsWithInference.

private void registerOperatorsWithInference(DrillOperatorTable operatorTable, Map<String, Collection<DrillFuncHolder>> registeredFunctions) {
    final Map<String, DrillSqlOperator.DrillSqlOperatorBuilder> map = Maps.newHashMap();
    final Map<String, DrillSqlAggOperator.DrillSqlAggOperatorBuilder> mapAgg = Maps.newHashMap();
    for (Entry<String, Collection<DrillFuncHolder>> function : registeredFunctions.entrySet()) {
        final ArrayListMultimap<Pair<Integer, Integer>, DrillFuncHolder> functions = ArrayListMultimap.create();
        final ArrayListMultimap<Integer, DrillFuncHolder> aggregateFunctions = ArrayListMultimap.create();
        final String name = function.getKey().toUpperCase();
        boolean isDeterministic = true;
        boolean isNiladic = false;
        for (DrillFuncHolder func : function.getValue()) {
            final int paramCount = func.getParamCount();
            if (func.isAggregating()) {
                aggregateFunctions.put(paramCount, func);
            } else {
                final Pair<Integer, Integer> argNumberRange;
                if (registeredFuncNameToArgRange.containsKey(name)) {
                    argNumberRange = registeredFuncNameToArgRange.get(name);
                } else {
                    argNumberRange = Pair.of(func.getParamCount(), func.getParamCount());
                }
                functions.put(argNumberRange, func);
            }
            if (!func.isDeterministic()) {
                isDeterministic = false;
            }
            if (func.isNiladic()) {
                isNiladic = true;
            }
        }
        for (Entry<Pair<Integer, Integer>, Collection<DrillFuncHolder>> entry : functions.asMap().entrySet()) {
            final Pair<Integer, Integer> range = entry.getKey();
            final int max = range.getRight();
            final int min = range.getLeft();
            if (!map.containsKey(name)) {
                map.put(name, new DrillSqlOperator.DrillSqlOperatorBuilder().setName(name));
            }
            final DrillSqlOperator.DrillSqlOperatorBuilder drillSqlOperatorBuilder = map.get(name);
            drillSqlOperatorBuilder.addFunctions(entry.getValue()).setArgumentCount(min, max).setDeterministic(isDeterministic).setNiladic(isNiladic);
        }
        for (Entry<Integer, Collection<DrillFuncHolder>> entry : aggregateFunctions.asMap().entrySet()) {
            if (!mapAgg.containsKey(name)) {
                mapAgg.put(name, new DrillSqlAggOperator.DrillSqlAggOperatorBuilder().setName(name));
            }
            final DrillSqlAggOperator.DrillSqlAggOperatorBuilder drillSqlAggOperatorBuilder = mapAgg.get(name);
            drillSqlAggOperatorBuilder.addFunctions(entry.getValue()).setArgumentCount(entry.getKey(), entry.getKey());
        }
    }
    for (final Entry<String, DrillSqlOperator.DrillSqlOperatorBuilder> entry : map.entrySet()) {
        operatorTable.addOperatorWithInference(entry.getKey(), entry.getValue().build());
    }
    for (final Entry<String, DrillSqlAggOperator.DrillSqlAggOperatorBuilder> entry : mapAgg.entrySet()) {
        operatorTable.addOperatorWithInference(entry.getKey(), entry.getValue().build());
    }
}
Also used : DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) DrillSqlAggOperator(org.apache.drill.exec.planner.sql.DrillSqlAggOperator) DrillSqlOperator(org.apache.drill.exec.planner.sql.DrillSqlOperator) Collection(java.util.Collection) Pair(org.apache.commons.lang3.tuple.Pair)

Example 55 with Range

use of org.apache.commons.lang3.Range in project narchy by automenta.

the class Optimize method run.

public Result<X> run(int maxIterations, int repeats, FloatFunction<Supplier<X>> eval) {
    assert (repeats >= 1);
    final int dim = tweaks.size();
    double[] mid = new double[dim];
    // double[] sigma = new double[n];
    double[] min = new double[dim];
    double[] max = new double[dim];
    double[] inc = new double[dim];
    // double[] range = new double[dim];
    X example = subject.get();
    int i = 0;
    for (Tweak w : tweaks) {
        TweakFloat s = (TweakFloat) w;
        // initial guess: get from sample, otherwise midpoint of min/max range
        Object guess = s.get(example);
        mid[i] = guess != null ? ((float) guess) : ((s.getMax() + s.getMin()) / 2f);
        min[i] = (s.getMin());
        max[i] = (s.getMax());
        inc[i] = s.getInc();
        // range[i] = max[i] - min[i];
        // sigma[i] = Math.abs(max[i] - min[i]) * 0.75f; //(s.getInc());
        i++;
    }
    FasterList<DoubleObjectPair<double[]>> experiments = new FasterList<>(maxIterations);
    final double[] maxScore = { Double.NEGATIVE_INFINITY };
    ObjectiveFunction func = new ObjectiveFunction(point -> {
        double score;
        try {
            double sum = 0;
            for (int r = 0; r < repeats; r++) {
                Supplier<X> x = () -> subject(point);
                sum += eval.floatValueOf(x);
            }
            score = sum / repeats;
        } catch (Exception e) {
            logger.error("{} {} {}", this, point, e);
            score = Float.NEGATIVE_INFINITY;
        }
        if (trace)
            csv.out(ArrayUtils.add(point, (int) 0, score));
        maxScore[0] = Math.max(maxScore[0], score);
        // System.out.println(
        // n4(score) + " / " + n4(maxScore[0]) + "\t" + n4(point)
        // );
        experiments.add(pair(score, point));
        experimentIteration(point, score);
        return score;
    });
    if (trace)
        csv = new CSVOutput(System.out, Stream.concat(Stream.of("score"), tweaks.stream().map(t -> t.id)).toArray(String[]::new));
    experimentStart();
    try {
        solve(dim, func, mid, min, max, inc, maxIterations);
    } catch (Throwable t) {
        logger.info("solve {} {}", func, t);
    }
    return new Result<>(experiments, tweaks);
}
Also used : MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) Logger(org.slf4j.Logger) SortedSet(java.util.SortedSet) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) MathArrays(org.apache.commons.math3.util.MathArrays) FasterList(jcog.list.FasterList) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) Supplier(java.util.function.Supplier) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) List(java.util.List) Stream(java.util.stream.Stream) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) GoalType(org.apache.commons.math3.optim.nonlinear.scalar.GoalType) Map(java.util.Map) PrimitiveTuples.pair(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples.pair) CSVOutput(jcog.meter.event.CSVOutput) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) Pair(org.eclipse.collections.api.tuple.Pair) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Joiner(com.google.common.base.Joiner) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) FasterList(jcog.list.FasterList) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) DoubleObjectPair(org.eclipse.collections.api.tuple.primitive.DoubleObjectPair) CSVOutput(jcog.meter.event.CSVOutput)

Aggregations

List (java.util.List)26 Map (java.util.Map)21 HashMap (java.util.HashMap)20 ArrayList (java.util.ArrayList)19 Collectors (java.util.stream.Collectors)19 StringUtils (org.apache.commons.lang3.StringUtils)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 Set (java.util.Set)14 Pair (org.apache.commons.lang3.tuple.Pair)14 Test (org.junit.Test)14 IOException (java.io.IOException)12 Optional (java.util.Optional)11 Range (org.apache.commons.lang3.Range)11 Date (java.util.Date)10 HashSet (java.util.HashSet)9 ExecutorService (java.util.concurrent.ExecutorService)9 Collection (java.util.Collection)8 Stream (java.util.stream.Stream)8 Lists (com.google.common.collect.Lists)7