def get_groups(data, group_min_size, max_groups):
    """ Picks the top groups out of a dataset

    1. Remove groups with too few data points
    2. Pick the top groups
    3. Sort groups lexicographically
    """
    group2count = {}
    for group in data:
        group2count[group] = group2count.get(group, 0) + 1
    groups = [group for group, count in group2count.items() if count >=
        group_min_size]
    if max_groups >= 0:
        groups = sorted(groups, key=group2count.get, reverse=True)[:max_groups]
    return sorted(groups, key=lambda g: (g is None, g))
