def maximal_balanced_subset(self, by_duration=False, label_list_ids=None):
        """
        Create a subset of the corpus as big as possible, so that the labels are balanced approximately.
        The label with the shortest duration (or with the fewest utterance if by_duration=False) is taken as reference.
        All other labels are selected so they match the shortest one as far as possible.

        Args:
            by_duration (bool): If True the size measure is the duration of all utterances in a subset/corpus.
            label_list_ids (list): List of label-list ids. If none is given, all label-lists are considered
                                   for balancing. Otherwise only the ones that are in the list are considered.

        Returns:
            Subview: The subview representing the subset.
        """
        all_label_values = self.corpus.all_label_values(label_list_ids=label_list_ids)

        if by_duration:
            utterance_durations = {utt_idx: utt.duration for utt_idx, utt in self.corpus.utterances.items()}
            total_duration_per_label = self.corpus.label_durations(label_list_ids=label_list_ids)
            rarest_label_duration = sorted(total_duration_per_label.values())[0]
            target_duration = len(all_label_values) * rarest_label_duration

            label_durations_per_utterance = {}
            for utt_idx, utt in self.corpus.utterances.items():
                label_durations_per_utterance[utt_idx] = utt.label_total_duration(label_list_ids)

            subset_utterance_ids = utils.select_balanced_subset(label_durations_per_utterance,
                                                                target_duration,
                                                                list(all_label_values),
                                                                select_count_values=utterance_durations,
                                                                seed=self.rand.random())

        else:
            total_count_per_label = self.corpus.label_count(label_list_ids=label_list_ids)
            lowest_label_count = sorted(total_count_per_label.values())[0]
            target_label_count = lowest_label_count * len(all_label_values)
            utterance_with_label_counts = collections.defaultdict(dict)

            for utterance_idx, utterance in self.corpus.utterances.items():
                utterance_with_label_counts[utterance_idx] = utterance.label_count(label_list_ids=label_list_ids)

            subset_utterance_ids = utils.select_balanced_subset(utterance_with_label_counts,
                                                                target_label_count,
                                                                list(all_label_values),
                                                                seed=self.rand.random())

        filter = subview.MatchingUtteranceIdxFilter(utterance_idxs=set(subset_utterance_ids))
        return subview.Subview(self.corpus, filter_criteria=[filter])