torch_ecg.utils.get_optimal_covering¶
- torch_ecg.utils.get_optimal_covering(total_interval: Sequence[Real] | list, to_cover: List[Real | Sequence[Real] | list], min_len: Real, split_threshold: Real, isolated_point_dist_threshold: Real = 0, traceback: bool = False, **kwargs: Any) Sequence[Sequence[Real] | list] | list | Tuple[Sequence[Sequence[Real] | list] | list, list] [source]¶
Compute an optimal covering of to_cover by intervals.
This function tries to find an optimal covering (disjoint union of intervals) that covers to_cover such that each interval in the covering is of length at least min_len, and any two intervals in the covering are at least split_threshold distance apart.
- Parameters:
total_interval (Interval) – The total interval that the covering is picked from.
to_cover (list) – A list of intervals or points to cover.
min_len (numbers.Real) – Minimun length (positive) of the intervals of the covering.
split_threshold (numbers.Real) – Minumun distance (positive) of intervals of the covering.
isolated_point_dist_threshold (numbers.Real, default 0.0) – The minimum distance (non-negative) of isolated points in to_cover to the interval boundaries of the interval containing the point in the covering. If one wants the isolated points to be centered in the interval containing the point, set isolated_point_dist_threshold to be
min_len / 2
.traceback (bool, default False) – if True, a list containing the list of indices of the intervals in the original to_cover, that each interval in the covering covers.
- Raises:
If any of the intervals in to_cover exceeds the range of total_interval, –
ValueError will be raised –
- Returns:
covering (GeneralizedInterval) – The covering that satisfies the given conditions
ret_traceback (list, optional) – Contains the list of indices of the intervals in the original to_cover, that each interval in the covering covers. If traceback is False, this will not be returned.
TODO
make positions of isolated points in the final covering as close as possible to the center of the interval that contains the point
Examples
>>> total_interval = [0, 100] >>> to_cover = [[7,33], 66, [82, 89]] >>> get_optimal_covering(total_interval, to_cover, 10, 5) [[7, 33], [56, 66], [82, 92]] >>> get_optimal_covering(total_interval, to_cover, 10, 5, traceback=True) ([[7, 33], [56, 66], [82, 92]], [[0], [1], [2]]) >>> get_optimal_covering(total_interval, to_cover, 20, 5, traceback=True) ([[7, 33], [46, 66], [80, 100]], [[0], [1], [2]]) >>> get_optimal_covering(total_interval, to_cover, 20, 13, traceback=True) ([[7, 33], [46, 66], [80, 100]], [[0], [1], [2]]) >>> get_optimal_covering(total_interval, to_cover, 20, 14, traceback=True) ([[7, 33], [66, 89]], [[0], [1, 2]]) >>> get_optimal_covering(total_interval, to_cover, 20, 13, isolated_point_dist_threshold=1) [[7, 33], [47, 67], [80, 100]] >>> get_optimal_covering(total_interval, to_cover, 20, 13, isolated_point_dist_threshold=2) [[7, 33], [64, 89]] >>> get_optimal_covering(total_interval, to_cover, 30, 3) [[3, 33], [36, 66], [70, 100]] >>> get_optimal_covering(total_interval, to_cover, 30, 4) [[3, 33], [59, 89]] >>> get_optimal_covering(total_interval, to_cover, 40, 5) [[0, 40], [60, 100]] >>> get_optimal_covering(total_interval, to_cover, 1000, 1, traceback=True) ([[0, 100]], [[0, 1, 2]])