def generate_output_filename(
    input_path: str | Path,
    output_path: str | Path,
    dataset_name: str | None = None,
    category: str | None = None,
    mkdir: bool = True,
) -> Path:
    """Generate an output filename based on the input path.

    This function generates an output path that preserves the directory structure after the
    dataset root, using an improved algorithm that works with any folder structure.

    Args:
        input_path (str | Path): Path to the input file.
        output_path (str | Path): Base output directory path.
        dataset_name (str | None, optional): Name of the dataset to find in the input path.
            If provided, the path structure after this dataset directory is preserved.
            If not provided or not found, uses intelligent heuristics.
            Defaults to ``None``.
        category (str | None, optional): Category name to find in the input path after
            dataset name. If provided, preserves structure after this category.
            Defaults to ``None``.
        mkdir (bool, optional): Whether to create the output directory structure.
            Defaults to ``True``.

    Returns:
        Path: Generated output file path preserving relevant directory structure.

    Examples:
        Basic usage with MVTec-style dataset:

        >>> input_path = "/data/MVTecAD/bottle/test/broken_large/000.png"
        >>> generate_output_filename(input_path, "./results", "MVTecAD", "bottle")
        PosixPath('results/test/broken_large/000.png')

        Without category preserves more structure:

        >>> generate_output_filename(input_path, "./results", "MVTecAD")
        PosixPath('results/bottle/test/broken_large/000.png')

        Works with folder datasets:

        >>> path = "/datasets/MyDataset/normal/image001.png"
        >>> generate_output_filename(path, "./output", "MyDataset")
        PosixPath('output/normal/image001.png')

        Handles custom structures gracefully:

        >>> path = "/custom/path/MyData/category/split/image.png"
        >>> generate_output_filename(path, "./out", "MyData")
        PosixPath('out/category/split/image.png')

        Auto-detection when dataset_name not provided:

        >>> path = "/any/folder/structure/normal/image.png"
        >>> generate_output_filename(path, "./out")
        PosixPath('out/normal/image.png')

        Case-insensitive matching:

        >>> path = "/data/mvtecad/BOTTLE/test/000.png"
        >>> generate_output_filename(path, "./results", "MVTecAD", "bottle")
        PosixPath('results/test/000.png')

        Relative paths:

        >>> path = "data/MVTecAD/bottle/test/000.png"
        >>> generate_output_filename(path, "./results", "MVTecAD")
        PosixPath('results/bottle/test/000.png')

    Note:
        - Uses intelligent path analysis to work with any folder structure
        - Preserves directory structure after the dataset root
        - If ``mkdir=True``, creates output directory structure if it doesn't exist
        - Original filename is always preserved in output path
    """
    input_path = Path(input_path)
    output_path = Path(output_path)

    # Find the base path to exclude
    exclude_base = None

    # Try to find dataset_name in path
    if dataset_name:
        for i, part in enumerate(input_path.parts):
            if part.lower() == dataset_name.lower():
                exclude_base = Path(*input_path.parts[: i + 1])
                break

    # Try to find category after dataset
    if exclude_base and category:
        try:
            remaining = input_path.relative_to(exclude_base)
            for j, part in enumerate(remaining.parts):
                if part.lower() == category.lower():
                    exclude_base = exclude_base / Path(*remaining.parts[: j + 1])
                    break
        except ValueError:
            pass  # relative_to failed, keep original exclude_base

    # Use relative_to to get the remaining path structure
    if exclude_base:
        try:
            relative_path = input_path.relative_to(exclude_base)
            preserved_dirs = relative_path.parts[:-1]  # All dirs except filename
        except ValueError:
            # Fallback: keep last directory
            preserved_dirs = (input_path.parent.name,)
    else:
        # No dataset found, keep last directory
        preserved_dirs = (input_path.parent.name,)

    # Build final path
    final_output_path = output_path / Path(*preserved_dirs) if preserved_dirs else output_path

    if mkdir:
        final_output_path.mkdir(parents=True, exist_ok=True)

    return final_output_path / input_path.name
Generate an output filename based on the input path.

    This function generates an output path that preserves the directory structure after the
    dataset root, using an improved algorithm that works with any folder structure.

    Args:
        input_path (str | Path): Path to the input file.
        output_path (str | Path): Base output directory path.
        dataset_name (str | None, optional): Name of the dataset to find in the input path.
            If provided, the path structure after this dataset directory is preserved.
            If not provided or not found, uses intelligent heuristics.
            Defaults to ``None``.
        category (str | None, optional): Category name to find in the input path after
            dataset name. If provided, preserves structure after this category.
            Defaults to ``None``.
        mkdir (bool, optional): Whether to create the output directory structure.
            Defaults to ``True``.

    Returns:
        Path: Generated output file path preserving relevant directory structure.

    Examples:
        Basic usage with MVTec-style dataset:

        >>> input_path = "/data/MVTecAD/bottle/test/broken_large/000.png"
        >>> generate_output_filename(input_path, "./results", "MVTecAD", "bottle")
        PosixPath('results/test/broken_large/000.png')

        Without category preserves more structure:

        >>> generate_output_filename(input_path, "./results", "MVTecAD")
        PosixPath('results/bottle/test/broken_large/000.png')

        Works with folder datasets:

        >>> path = "/datasets/MyDataset/normal/image001.png"
        >>> generate_output_filename(path, "./output", "MyDataset")
        PosixPath('output/normal/image001.png')

        Handles custom structures gracefully:

        >>> path = "/custom/path/MyData/category/split/image.png"
        >>> generate_output_filename(path, "./out", "MyData")
        PosixPath('out/category/split/image.png')

        Auto-detection when dataset_name not provided:

        >>> path = "/any/folder/structure/normal/image.png"
        >>> generate_output_filename(path, "./out")
        PosixPath('out/normal/image.png')

        Case-insensitive matching:

        >>> path = "/data/mvtecad/BOTTLE/test/000.png"
        >>> generate_output_filename(path, "./results", "MVTecAD", "bottle")
        PosixPath('results/test/000.png')

        Relative paths:

        >>> path = "data/MVTecAD/bottle/test/000.png"
        >>> generate_output_filename(path, "./results", "MVTecAD")
        PosixPath('results/bottle/test/000.png')

    Note:
        - Uses intelligent path analysis to work with any folder structure
        - Preserves directory structure after the dataset root
        - If ``mkdir=True``, creates output directory structure if it doesn't exist
        - Original filename is always preserved in output path
    
src/anomalib/utils/path.py