def random_split(
    dataset: "data.AnomalibDataset",
    split_ratio: float | Sequence[float],
    label_aware: bool = False,
    seed: int | None = None,
) -> list["data.AnomalibDataset"]:
    """Randomly split a dataset into multiple subsets.

    Args:
        dataset: Source dataset to split
        split_ratio: Split ratios that must sum to 1. If single float ``x`` is
            provided, splits into ``[1-x, x]``
        label_aware: If ``True``, maintains class label distributions in splits
        seed: Random seed for reproducibility

    Returns:
        List of dataset splits based on provided ratios

    Example:
        >>> splits = random_split(dataset, [0.7, 0.3], seed=42)
        >>> len(splits)
        2
        >>> # Label-aware splitting
        >>> splits = random_split(dataset, 0.2, label_aware=True)
        >>> len(splits)
        2
    """
    if isinstance(split_ratio, float):
        split_ratio = [1 - split_ratio, split_ratio]

    if not (math.isclose(sum(split_ratio), 1) and sum(split_ratio) <= 1):
        msg = f"Split ratios must sum to 1, found {sum(split_ratio)}"
        raise ValueError(msg)

    if not all(0 < ratio < 1 for ratio in split_ratio):
        msg = f"All split ratios must be between 0 and 1, found {split_ratio}"
        raise ValueError(msg)

    # create list of source data
    if label_aware and "label_index" in dataset.samples:
        indices_per_label = [group.index for _, group in dataset.samples.groupby("label_index")]
        per_label_datasets = [dataset.subsample(indices) for indices in indices_per_label]
    else:
        per_label_datasets = [dataset]

    # outer list: per-label unique, inner list: random subsets with the given ratio
    subsets: list[list[data.AnomalibDataset]] = []
    # split each (label-aware) subset of source data
    for label_dataset in per_label_datasets:
        # get subset lengths
        subset_lengths = [math.floor(len(label_dataset.samples) * ratio) for ratio in split_ratio]
        for i in range(len(label_dataset.samples) - sum(subset_lengths)):
            subset_idx = i % sum(subset_lengths)
            subset_lengths[subset_idx] += 1
        if 0 in subset_lengths:
            msg = """Zero subset length encountered during splitting. This means one of your subsets
            might be empty or devoid of either normal or anomalous images."""
            logger.warning(msg)

        # perform random subsampling
        random_state = torch.Generator().manual_seed(seed) if seed else None
        indices = torch.randperm(len(label_dataset.samples), generator=random_state)
        subsets.append(
            [label_dataset.subsample(subset_indices) for subset_indices in torch.split(indices, subset_lengths)],
        )

    # invert outer/inner lists
    # outer list: subsets with the given ratio, inner list: per-label unique
    subsets = list(map(list, zip(*subsets, strict=True)))
    return [concatenate_datasets(subset) for subset in subsets]
Randomly split a dataset into multiple subsets.

    Args:
        dataset: Source dataset to split
        split_ratio: Split ratios that must sum to 1. If single float ``x`` is
            provided, splits into ``[1-x, x]``
        label_aware: If ``True``, maintains class label distributions in splits
        seed: Random seed for reproducibility

    Returns:
        List of dataset splits based on provided ratios

    Example:
        >>> splits = random_split(dataset, [0.7, 0.3], seed=42)
        >>> len(splits)
        2
        >>> # Label-aware splitting
        >>> splits = random_split(dataset, 0.2, label_aware=True)
        >>> len(splits)
        2
    
src/anomalib/data/utils/split.py