import logging


def log_examples(reader, indices, tasks, subset_name, group_by=None):
    """ Log the number of examples in each class or combination

    Specify the names of the labels to group by as a sequence with group_by.
    If group_by is None, group by all labels (default).
    """
    logger = logging.getLogger()
    logger.info("Examples for " + subset_name + ':')
    logger.info("  Total number: {}".format(len(indices)))
    labels = list(tasks)
    if group_by is None:
        group_by = labels
    num_class_examples = reader.num_examples(group_by=group_by,
                                             example_indices=indices)
    logger.info("  Breakdown by " + ', '.join(labels) + ':')
    for cls, num in num_class_examples.items():
        names = []
        for label, idx in zip(labels, cls):
            # Value if regression label, else class name if class label
            if tasks[label].get('class_names') is not None:
                name = tasks[label]['class_names'][idx]
            else:
                name = str(idx)
            names.append(name)
        logger.info("    " + ', '.join(names) + ": {}".format(num))
    logger.info('')
    return num_class_examples