def get_flattened_intervalset(self): """Get an IntervalSet containing all intervals in all the IntervalSets.""" output = [] for intervals in self.get_grouped_intervals().values(): output.extend(intervals.get_intervals()) return IntervalSet(output)
def method(self, other, *args, profile=False, **kwargs): with perf_count(name, profile): selfmap = self.get_grouped_intervals() othermap = other.get_grouped_intervals() keys = set(selfmap.keys()).union(othermap.keys()) def func(set1, set2): return getattr(IntervalSet, name)(set1, set2, *args, **kwargs) results_map = { v: func(selfmap.get(v, IntervalSet([])), othermap.get(v, IntervalSet([]))) for v in keys } return IntervalSetMapping( IntervalSetMapping._remove_empty_intervalsets(results_map))
def from_iterable(cls, iterable, key_parser, bounds_parser, payload_parser=lambda _: None, progress=False, total=None): """Constructs an IntervalSetMapping from an iterable. Args: iterable: An iterable of arbitrary elements. Each element will become an interval in the collection. key_parser: A function that takes an element in iterable and returns the key for the interval. bounds_parser: A function that takes an element in iterable and returns the bounds for the interval. payload_parser (optional): A function that takes an element in iterable and returns the payload for the interval. Defaults to producing None for all elements. progress (Bool, optional): Whether to display a progress bar using tqdm. Defaults to False. total (int, optional): Total number of elements in iterable. Only used to estimate ETA for the progress bar, and only takes effect if progress is True. Returns: A IntervalSetMapping constructed from iterable and the parsers provided. Note: Everything in iterable will be materialized in RAM. """ key_to_intervals = {} for row in (tqdm(iterable, total=total) if progress and total is not None else tqdm(iterable) if progress else iterable): interval = Interval(bounds_parser(row), payload_parser(row)) key = key_parser(row) if key in key_to_intervals: key_to_intervals[key].append(interval) else: key_to_intervals[key] = [interval] return cls({ key: IntervalSet(intervals) for key, intervals in key_to_intervals.items() })
def from_intervalset(cls, intervalset, key_fn): """Constructs an IntervalSetMapping from an IntervalSet by grouping by ``key_fn``. Args: intervalset (IntervalSet): An interval set containing all intervals to put in the mapping. key_fn: A function that takes an interval and returns the domain key. Returns: An IntervalSetMapping with the same intervals organized into domains by their key accroding to ``key_fn``. """ def reducer(acc, interval): key = key_fn(interval) if key not in acc: acc[key] = [interval] else: acc[key].append(interval) return acc grouped = intervalset.fold(reducer, {}) return cls({k: IntervalSet(v) for k, v in grouped.items()})
def _empty_set(): return IntervalSet([])