Skip to content

Add the labeled-data header and row-index vocabulary - #13

Draft
C-Achard wants to merge 4 commits into
mainfrom
cy/io-labeled-data-vocabulary
Draft

C-Achard wants to merge 4 commits into
mainfrom
cy/io-labeled-data-vocabulary

Conversation

@C-Achard

@C-Achard C-Achard commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Keypoints vocabulary, classification of DeepLabCut's wide labeled-data header and row index.

  • KeypointIdentifier (scorer, individual, bodypart) ; KeypointIdentifiers
  • KeypointDataFields for the keypoint data ("x", "y") or ("x", "y", "likelihood")
  • Classification keeps raw data; unrecognised headers keep raw columns, duplicated or reordered columns keep their positions, and the header is never expanded from a MultiIndex's levels.
  • Supports path parts (MultiIndex) and flat path strings (older files).

@C-Achard C-Achard added io: labeled-data CollectedData and machinelabels files consumer: deeplabcut Needed by or affects DeepLabCut consumer: napari Needed by or affects napari-deeplabcut backwards compatibility Reading files written by older DeepLabCut labels Nov 24, 2026
@C-Achard C-Achard self-assigned this Nov 24, 2026
@C-Achard
C-Achard added this pull request to stack #18 November 24, 2026 16:24
@C-Achard
C-Achard marked this pull request as draft November 24, 2026 16:25
@C-Achard
C-Achard force-pushed the cy/io-labeled-data-vocabulary branch 2 times, most recently from a6f8c64 to ca7a722 Compare November 30, 2026 08:14
@C-Achard C-Achard added this to the I/O - Video, Labeled data milestone Nov 30, 2026
@C-Achard
C-Achard force-pushed the cy/io-labeled-data-vocabulary branch 3 times, most recently from f365af1 to adbf7f5 Compare November 30, 2026 15:10
@C-Achard
C-Achard removed this pull request from stack #18 November 30, 2026 15:16
@C-Achard
C-Achard added this pull request to stack #42 November 30, 2026 15:16
@C-Achard
C-Achard removed this pull request from stack #42 December 1, 2026 10:22
@C-Achard
C-Achard added this pull request to stack #44 December 1, 2026 10:23
@C-Achard
C-Achard requested a review from deruyter92 December 2, 2026 08:16
@C-Achard
C-Achard force-pushed the cy/io-labeled-data-vocabulary branch from adbf7f5 to e42e109 Compare December 2, 2026 09:32
@C-Achard
C-Achard force-pushed the cy/io-labeled-data-vocabulary branch from e42e109 to 9a9ebe0 Compare December 3, 2026 12:18
@C-Achard
C-Achard removed this pull request from stack #44 December 3, 2026 12:19
@C-Achard
C-Achard changed the base branch from cy/io-video to main December 3, 2026 12:19
@C-Achard
C-Achard added this pull request to stack #45 December 3, 2026 12:24
Rename LabeledDataHeader.columns to unrecognised_columns to clarify its purpose. Update classification, documentation, and the corresponding test.
@C-Achard

C-Achard commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@deruyter92 This is now only the DLC basic labeled data concepts. This and #15 could be the first to merge and be used in other repos

Comment on lines +45 to +78
@dataclass(frozen=True)
class KeypointIdentifier:
"""Keypoint properties.

Identifies a keypoint; does not describe the data (see ``KeypointDataFields``).

Attributes:
scorer: As stored.
individual: As stored; ``None`` when the source names no individual.
bodypart: As stored.
"""

scorer: str
individual: str | None
bodypart: str

@property
def scope(self) -> BodypartScope:
"""``UNIQUE`` exactly when the individual is DeepLabCut's unique-bodyparts bucket."""

return BodypartScope.UNIQUE if self.individual == UNIQUE_BODYPARTS_INDIVIDUAL else BodypartScope.INDIVIDUAL


@dataclass(frozen=True)
class KeypointIdentifiers(Sequence[KeypointIdentifier]):
"""The identifiers of labeled data's keypoints, in keypoint order.

Available as ``identifiers`` on:

- ``LabeledDataFile``: read from the header; equal to the model's when the model can be built.
For inspecting a file without building the model, or a file with no frames.
- ``LabeledData``: available after construction (``from_wide_df``, ``from_long_df``, ``without_frames``).
- ``LabeledDataArrays``: the model's. Column ``k`` of each array is keypoint ``k``, so the arrays can be used
without the model.

@deruyter92 deruyter92 Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this maybe mixes two things currently:

the KeypointIdentifier is a faithful and useful representation of what is currently on disk in the wide HDF format. Very useful as a private representation for validating the wide formats.

Passing on the same structure via KeypointIdentifiers on public LabeledData or LabeledDataArrays to me feels a bit awkward, however. E.g.:

  • A scorer is really a property of the data collection, not a keypoint
  • bodyparts, unique_bodyparts, and individuals have to be recovered via iteration over KeypointIdentifiers, when these sets are primary facts about the data collection, which now are stored in config.yaml as a plain list.

Why not go for a more plain:

class LabeledDataDescriptor:          
    scorer: str
    bodyparts: tuple[str, ...]
    individuals: tuple[str, ...]
    unique_bodyparts: tuple[str, ...]
    data_fields: KeypointDataFields

You can keep the KeypointIdentifier for the KeypointColumns which is helpful for round-tripping to the wide HDF (private). But for the public model I would go for a plain description of what is in the data collection.

some advantages:

  • matches DLC's own model in config.yaml
  • offers direct access to the fields e.g. scorer
  • avoids impossible states like multiple scorers in one file
  • matches the arrays directly and intuitively (frames, individuals, bodyparts, 2)
  • small and easily serializable object to pass around
  • clear boundary between private, multi-level column-based representation vs public flat representation.

@deruyter92 deruyter92 Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, open question, I am maybe overlooking some complexities which you account for. Let me know what you think

@C-Achard C-Achard Dec 4, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a summary for the next iteration:

I agree that the public identity can be a descriptor like you said, e.g.

LabeledDataDescriptor(
    scorer="me",
    bodyparts=("nose", "tail"),
    individuals=("mouse1", "mouse2"),  # () for single-animal
    unique_bodyparts=("led",),
    data_fields=KeypointDataFields(("x", "y")),
)

KeypointIdentifier then stays, but private (just for reading and writing the wide header).
It also makes comparing a file layout with the expected descriptor easier, esp. for early validation and reporting of mismatches.

Invalid descriptor -> no model

Broken data should still be readable as stored (to_raw_wide_df()) and usable/reported by validation, but then LabeledData should refuse it. Two example cases (one column per keypoint, frame rows left out):

Several scorers:

scorer alice alice bob bob
bodyparts nose tail nose tail

Ragged, mouse2 has no tail column (not the same as an unlabeled tail, which is valid):

scorer me me me
individuals mouse1 mouse1 mouse2
bodyparts nose tail nose

"single" only in the wide format

Unique bodyparts would get their own field, so "single" appears only when reading or writing the legacy wide header.

Array shape

Again, I agree on the value of a consistent shape, in the array format above others.
We could keep xy in memory as (frames, keypoints, 2), and add as views:

  • per_individual(): (frames, individuals, bodyparts, 2), with one individual for single-animal data
  • unique(): (frames, unique_bodyparts, 2), empty if none are present

Both views are free (no copy). The other way would not since unique bodyparts cannot be in the 4D grid, so they would be separate in memory and getting the flat (F, K, 2) array would then copy.

Re: pose outputs

Pose outputs (frame- or time-indexed) have the same header, one scorer per file, so the same descriptor would fit, aside from the frame axis.
It would however point to needing a more generic name, e.g. KeypointsDescriptor.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backwards compatibility Reading files written by older DeepLabCut consumer: deeplabcut Needed by or affects DeepLabCut consumer: napari Needed by or affects napari-deeplabcut io: labeled-data CollectedData and machinelabels files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants