from typing import Sequence
from typing import Mapping


def minimize_xgboost_model(trees: Sequence[Mapping]) -> Sequence[Mapping]:
    """Strip xgboost extra stats from model

    xgboost can dump additional statistics with the model, but these trip up
    the ltr plugin parser. Strip the models down to only what is necessary.
    """
    def visit(node):
        if 'leaf' in node:
            keep = ['nodeid', 'leaf']
        else:
            keep = ['nodeid', 'split', 'split_condition', 'yes', 'no', 'missing', 'depth']
        out = {key: node[key] for key in keep}
        if 'children' in node:
            out['children'] = [visit(child) for child in node['children']]
        return out

    return [visit(tree) for tree in trees]