Skip to content

fix: lexicographic file sort misorders numbered frames - #54

Open
andrewwhitecdw wants to merge 2 commits into
NVlabs:mainfrom
andrewwhitecdw:bugfix/convert-to-lerobot-8257d421
Open

fix: lexicographic file sort misorders numbered frames#54
andrewwhitecdw wants to merge 2 commits into
NVlabs:mainfrom
andrewwhitecdw:bugfix/convert-to-lerobot-8257d421

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

This PR addresses the following issue in scripts/convert_to_lerobot.py: lexicographic file sort misorders numbered frames.

Changes

  • scripts/convert_to_lerobot.py: lexicographic file sort misorders numbered frames.

Details

Before:

# Get the paths of files within a directory.
def get_files_in_dir(path: Path) -> list[Path]:
    return sorted([p for p in path.iterdir() if p.is_file()])

After:

import re

# Get the paths of files within a directory.
def _natural_sort_key(path: Path):
    return [int(s) if s.isdigit() else s.lower() for s in re.split(r"(\d+)", path.name)]

def get_files_in_dir(path: Path) -> list[Path]:
    return sorted([p for p in path.iterdir() if p.is_file()], key=_natural_sort_key)

andrewwhitecdw and others added 2 commits July 27, 2026 21:06
Auditor: The natural-sort key returns a list of mixed `int` and `str` tokens, which raises a `TypeError` in Python 3 whenever two filenames differ in digit/non-digit structure (e.g. '1.jpg' vs 'a.jpg', or 'frame_1.jpg' vs '0.jpg'). This makes the generic `get_files_in_dir` unsafe for heterogeneous directories. Also, `import re` is inserted mid-file instead of at the top. Use a type-safe natural key (e.g., tagged tuples) and relocate the import.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant