def merge_samples(datasets, selector):
    """
    Merge samples within a category.
    ex: merge samples stored as multiple files (channels) to one.
    :param datasets: dict of datasets
    :param selector: Selector function returning a key and a channel
    :return:
    """
    for ds, dsv in datasets.items():
        for cat, samples in dsv.items():
            merged = {}
            for sample in samples:
                key, channel = selector(sample)
                merged.setdefault(key, {})[channel] = sample
            dsv[cat] = list(merged.values())
    return datasets