def sorted_pareto_optimal_rules()

in util.py [0:0]


def sorted_pareto_optimal_rules(rules, df: pd.DataFrame, target):
    # filters out rules that are not on the Pareto frontier
    def retain_pareto_optimal(rules: List, stats: ConfusionMatrix):
        retained = {}
        filtered = set()
        for r1 in rules.keys():
            keep = True
            for r2 in rules.keys():
                if r1 == r2:
                    continue
                if r2 in filtered:
                    continue
                # there is some rule that is better than r1,
                # so we don't need to keep r1
                if r2.is_better(r1, stats):
                    keep = False
                    filtered.add(r1)
                    break
            if keep:
                retained[r1] = rules[r1]
        return retained

    stats = ConfusionMatrix(df, target)
    pareto_optimal = retain_pareto_optimal(rules, stats)
    sorted_rules = sorted(pareto_optimal.items(), key=lambda x: x[1], reverse=True)
    # leave out score
    return [rule for rule, _ in sorted_rules]