import torch


def rank_segments(e, feature_segments):
    """
    rank segments of e
    :param e: explanation e
    :param feature_segments: unranked feature segments (list of (sensor, t_start, t_end))
    :returns: sorted list of the feature segments with the mean score as well
    """
    scores = [torch.mean(e[d, t_start:t_end]).detach().cpu().numpy() for d, t_start, t_end in feature_segments]
    ranked_feature_segments = sorted(zip(scores, feature_segments), reverse=True)  # sort by scores in descending order
    return ranked_feature_segments