Create a MIDD from conjunctions of intervals

@param intervals
@return
@throws MIDDParsingException
@throws MIDDException
325573
public AbstractNode createFromConjunctionClauses(Map<String, AttributeInfo> intervals) throws MIDDParsingException, MIDDException {

        if (intervals == null || intervals.size() == 0) {
            return ExternalNode.newInstance(); // return true-value external node
        }

        // Create edges from intervals
        Map<Integer, AbstractEdge<?>> edges = new HashMap<Integer, AbstractEdge<?>>();
        for (String attrId : intervals.keySet()) {
            int varId = attrMapper.getVariableId(attrId);
            Interval<?> interval = intervals.get(attrId).getInterval();

            Class<?> type = interval.getType();
            AbstractEdge<?> e = EdgeUtils.createEdge(interval);
            edges.put(varId, e);
        }

        List<Integer> lstVarIds = new ArrayList<Integer>(edges.keySet());
        Collections.sort(lstVarIds);

        // create a MIDD path from list of edges, start from lowest var-id
        InternalNode<?> root = null;
        InternalNode<?> currentNode = null;
        AbstractEdge<?> currentEdge = null;

        Iterator<Integer> lstIt = lstVarIds.iterator();
        while (lstIt.hasNext()) {
            Integer varId = lstIt.next();
            AbstractEdge<?> e = edges.get(varId);

            // default is NotApplicable, unless the "MustBePresent" is set to "true"
            String attrId = attrMapper.getAttributeId(varId);
            boolean isAttrMustBePresent = intervals.get(attrId).isMustBePresent;

            InternalNodeState nodeState = new InternalNodeState(isAttrMustBePresent ? DecisionType.Indeterminate : DecisionType.NotApplicable);

            InternalNode<?> node = NodeUtils.createInternalNode(varId, nodeState, e.getType());
            if (root == null) {
                root = node; // root points to the start of the MIDD path
                currentNode = node;
                currentEdge = e;
            } else {
                currentNode.addChild(currentEdge, node);
                currentNode = node;
                currentEdge = e;
            }
        }
        // the tail points to the true clause
        currentNode.addChild(currentEdge, ExternalNode.newInstance()); // add a true-value external node
        return root;
    }
965469
def union(self, other):
        
        result = IntervalSet()
        for el in self:
            result.add(el)
        for el in other:
            result.add(el)
        return result
697030
public static ClockInterval from(ChronoInterval<PlainTime> interval) {

        if (interval instanceof ClockInterval) {
            return ClockInterval.class.cast(interval);
        } else {
            return new ClockInterval(interval.getStart(), interval.getEnd());
        }

    }
835307
public static <T extends Comparable<T>> Partition<T> intersect(Partition<T> p1, Partition<T> p2) throws MIDDException {

        if (p1.size() == 0 || p2.size() == 0) {
            return new Partition<T>();
        }

        List<Interval<T>> tempIntervals = combine(p1, p2);

        Interval<T> newInterval = null;
        Interval<T> superOfNewIntervalP1 = null;
        Interval<T> superOfNewIntervalP2 = null;

        // combine adjacent intervals which belong to the same type
        List<Interval<T>> outputPartition = new ArrayList<Interval<T>>();

        for (int i = 0; i < tempIntervals.size(); i++) {
            Interval<T> tempInterval = tempIntervals.get(i);
            Interval<T> superOfTempIntervalP1 = p1.getSuperInterval(tempInterval);
            Interval<T> superOfTempIntervalP2 = p2.getSuperInterval(tempInterval);

            // if it belongs to at most one partition, ignore!
            if (superOfTempIntervalP1 == null || superOfTempIntervalP2 == null) {
                continue;
            }

            if (newInterval == null) {
                newInterval = tempInterval;
                superOfNewIntervalP1 = superOfTempIntervalP1;
                superOfNewIntervalP2 = superOfTempIntervalP2;
                continue;
            }

            if (superOfNewIntervalP1 == superOfTempIntervalP1 && superOfNewIntervalP2 == superOfTempIntervalP2) {
                newInterval.includeBound(tempInterval);
            } else {
                outputPartition.add(newInterval);
                newInterval = tempInterval;
                superOfNewIntervalP1 = superOfTempIntervalP1;
                superOfNewIntervalP2 = superOfTempIntervalP2;
            }

        }
        if (newInterval != null) {
            outputPartition.add(newInterval);
        }
        return new Partition<T>(outputPartition);
    }
573396
public Interval withStartDate(LocalDate date) {
        requireNonNull(date);
        return new Interval(date, startTime, endDate, endTime, zoneId);
    }
701918
public Result<Void> deleteDataPoints(Series series, Interval interval) {
    checkNotNull(series);
    checkNotNull(interval);

    URI uri = null;
    try {
      URIBuilder builder = new URIBuilder(String.format("/%s/series/key/%s/data/", API_VERSION, urlencode(series.getKey())));
      addIntervalToURI(builder, interval);
      uri = builder.build();
    } catch (URISyntaxException e) {
      String message = String.format("Could not build URI with inputs: key: %s, interval: %s", series.getKey(), interval);
      throw new IllegalArgumentException(message, e);
    }

    HttpRequest request = buildRequest(uri.toString(), HttpMethod.DELETE);
    Result<Void> result = execute(request, Void.class);
    return result;
  }
285172
protected static Map<Double, Double> doQuantization(double max,
                                                        double min,
                                                        double[] values)
    {
        double range = max - min;
        int noIntervals = 20;
        double intervalSize = range / noIntervals;
        int[] intervals = new int[noIntervals];
        for (double value : values)
        {
            int interval = Math.min(noIntervals - 1,
                                    (int) Math.floor((value - min) / intervalSize));
            assert interval >= 0 && interval < noIntervals : "Invalid interval: " + interval;
            ++intervals[interval];
        }
        Map<Double, Double> discretisedValues = new HashMap<Double, Double>();
        for (int i = 0; i < intervals.length; i++)
        {
            // Correct the value to take into account the size of the interval.
            double value = (1 / intervalSize) * (double) intervals[i];
            discretisedValues.put(min + ((i + 0.5) * intervalSize), value);
        }
        return discretisedValues;
    }
1139867
public static Interval minus(final Interval interval, final Period period) {
    return new Interval(interval.getStart().minus(period),
        interval.getEnd().minus(period));
  }
286319
public Interval withDates(LocalDate startDate, LocalDate endDate) {
        requireNonNull(startDate);
        requireNonNull(endDate);
        return new Interval(startDate, this.startTime, endDate, this.endTime, this.zoneId);
    }
1268445
public S interval(Long interval) {
        builder.interval(String.valueOf(interval));
        return self;
    }
1275745
public static List<Interval> combine(List<Interval> intervals) {
        List<Interval> combined = new ArrayList<Interval>();
        Collections.sort(intervals);
        int len = intervals.size();
        for (int i = 0; i < len; i++) {
            combined.add(intervals.get(i));

            int j;
            for (j = i + 1; j < len; j++) {
                if (intervals.get(i).stop >= intervals.get(j).start) {
                    intervals.get(i).stop = intervals.get(j).stop;
                } else {
                    break;
                }
            }
            i = j - 1;
        }

        return combined;
    }
797923
public static function create(array $expressions) : Intervals
    {
        /** @var \Interval\Parser\IntervalsParser $parser */
        $parser = self::loadDi()->get(Di::PARSER_INTERVALS);
        return $parser->parse($expressions);
    }
625616
public List<O> createdInPeriod(Date start, Date end, int numObjects) {
        return findByDate("creationDate", start, end, numObjects, true);
    }
646964
def _merge_intervals(self, min_depth):
        
        def add_interval(ret, start, stop):
            if min_depth is not None:
                shift = 2 * (29 - min_depth)
                mask = (int(1) << shift) - 1

                if stop - start < mask:
                    ret.append((start, stop))
                else:
                    ofs = start & mask
                    st = start
                    if ofs > 0:
                        st = (start - ofs) + (mask + 1)
                        ret.append((start, st))

                    while st + mask + 1 < stop:
                        ret.append((st, st + mask + 1))
                        st = st + mask + 1

                    ret.append((st, stop))
            else:
                ret.append((start, stop))

        ret = []
        start = stop = None
        # Use numpy sort method
        self._intervals.sort(axis=0)
        for itv in self._intervals:
            if start is None:
                start, stop = itv
                continue

            # gap between intervals
            if itv[0] > stop:
                add_interval(ret, start, stop)
                start, stop = itv
            else:
                # merge intervals
                if itv[1] > stop:
                    stop = itv[1]

        if start is not None and stop is not None:
            add_interval(ret, start, stop)

        self._intervals = np.asarray(ret)
987828
def merge_intervals(intervals):
    
    if intervals is None:
        return None
    intervals.sort(key=lambda i: i[0])
    out = [intervals.pop(0)]
    for i in intervals:
        if out[-1][-1] >= i[0]:
            out[-1][-1] = max(out[-1][-1], i[-1])
        else:
            out.append(i)
    return out
573413
def from_interval(self, startnote, shorthand, up=True):
        """"""
        return self.from_interval_shorthand(startnote, shorthand, up)
1067480
public IntervalCollection<T> intersect(IntervalCollection<T> other) {

        if (this.isEmpty() || other.isEmpty()) {
            List<ChronoInterval<T>> zero = Collections.emptyList();
            return this.create(zero);
        }

        List<ChronoInterval<T>> list = new ArrayList<>();

        for (ChronoInterval<T> a : this.intervals) {
            for (ChronoInterval<T> b : other.intervals) {
                List<ChronoInterval<T>> candidates = new ArrayList<>(2);
                candidates.add(a);
                candidates.add(b);
                candidates.sort(this.getComparator());
                candidates = this.intersect(candidates);
                if (!candidates.isEmpty()) {
                    list.addAll(candidates);
                }
            }
        }

        list.sort(this.getComparator());
        return this.create(list).withBlocks();

    }