Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion dlccore/io/labeled_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
# Licensed under GNU Lesser General Public License v3.0
#

"""Labeled-data files: ``model`` holds what is true in any format, and each table format is a subpackage.
"""Labeled-data files. The public API is ``__all__``.

``__all__`` holds:
- File, model and views
- Arrays view
- Identity types
- Reader and writer
``model`` holds concepts common to all formats.
``_wide``, DeepLabCut's stored table, and ``_checks`` are internal to dlccore; validation reports condition as findings.

Imports no format eagerly, so no format's dependencies load until a file is read or written, or that format is
imported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@

import pandas as pd

from dlccore.io.labeled_data.long.model import FRAME_COLUMNS
from dlccore.io.labeled_data.model import LIKELIHOOD_COORD, POSITION_COORDS
from dlccore.io.labeled_data.wide.header import KeypointColumns, LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data.wide.row_index import (
from dlccore.io.labeled_data._wide.header import KeypointColumns, LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data._wide.row_index import (
LabeledDataRowIndex,
RowIndexShape,
frame_part_as_text,
split_frame_path,
)
from dlccore.io.labeled_data.long.model import FRAME_COLUMNS
from dlccore.io.labeled_data.model import LIKELIHOOD_COORD, POSITION_COORDS

if TYPE_CHECKING:
from dlccore.io.labeled_data.wide.h5 import H5KeySelection
from dlccore.io.labeled_data._wide.h5 import H5KeySelection

_COORDINATES = frozenset({*POSITION_COORDS, LIKELIHOOD_COORD})

Expand All @@ -47,7 +47,7 @@ def unread_h5_keys(selection: H5KeySelection | None) -> tuple[str, ...]:
The unread keys, in the file's order. Empty unless the file holds several keys.
"""

from dlccore.io.labeled_data.wide.h5 import H5KeySelectionOutcome
from dlccore.io.labeled_data._wide.h5 import H5KeySelectionOutcome

if selection is None or selection.outcome is not H5KeySelectionOutcome.MULTI_KEY:
return ()
Expand All @@ -64,7 +64,7 @@ def legacy_h5_key(selection: H5KeySelection | None) -> str | None:
The key, or ``None`` if the file is not H5, holds another key, or holds several keys.
"""

from dlccore.io.labeled_data.wide.h5 import H5KeySelectionOutcome
from dlccore.io.labeled_data._wide.h5 import H5KeySelectionOutcome

return _single_key(selection, H5KeySelectionOutcome.SINGLE_LEGACY)

Expand All @@ -79,7 +79,7 @@ def unknown_h5_key(selection: H5KeySelection | None) -> str | None:
The key, or ``None`` if the file is not H5, holds a known key, or holds several keys.
"""

from dlccore.io.labeled_data.wide.h5 import H5KeySelectionOutcome
from dlccore.io.labeled_data._wide.h5 import H5KeySelectionOutcome

return _single_key(selection, H5KeySelectionOutcome.SINGLE_UNKNOWN)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import pandas as pd

from dlccore.io.errors import UnreadableFileError
from dlccore.io.labeled_data.wide.header import HEADER_DEPTHS, LabeledDataHeaderShape, header_shape
from dlccore.io.labeled_data.wide.table import LabeledDataRead
from dlccore.io.labeled_data._wide.header import HEADER_DEPTHS, LabeledDataHeaderShape, header_shape
from dlccore.io.labeled_data._wide.table import LabeledDataRead


def _header_rows_and_index_columns(rows: list[list[str]]) -> tuple[int, int]:
Expand Down Expand Up @@ -96,7 +96,7 @@ def write_labeled_data_csv(df: pd.DataFrame, path: Path) -> None:
"""Write a wide table as DeepLabCut's human-readable CSV; the H5 stays authoritative.

Values are written as pandas formats them, so a float can differ in its last digits after a round trip, and
the frames ``checks.file.frames_lost_to_csv`` returns are written but read back as something else.
the frames ``_checks.file.frames_lost_to_csv`` returns are written but read back as something else.
"""

df.to_csv(path)
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import pandas as pd

from dlccore.io.labeled_data._wide.header import MULTI_ANIMAL_LEVELS, SINGLE_ANIMAL_LEVELS
from dlccore.io.labeled_data.long.model import FRAME_COLUMNS, LabeledData, identity_columns
from dlccore.io.labeled_data.model import LIKELIHOOD_COORD, POSITION_COORDS
from dlccore.io.labeled_data.wide.header import MULTI_ANIMAL_LEVELS, SINGLE_ANIMAL_LEVELS


def long_to_wide(labels: LabeledData) -> pd.DataFrame:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pandas as pd

from dlccore.io.errors import UnreadableFileError
from dlccore.io.labeled_data.wide.table import LabeledDataRead
from dlccore.io.labeled_data._wide.table import LabeledDataRead
from dlccore.io.stamp import WRITER_KEY, WRITER_VERSION_KEY, WriterStamp

# Observed from a payload pandas cannot rebuild: TypeError (unknown storer), NoSuchNodeError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

from dlccore.io.errors import FileChangedError
from dlccore.io.file_stat import FileStat
from dlccore.io.labeled_data._wide.header import LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data._wide.row_index import LabeledDataRowIndex, RowIndexShape, frame_part_as_text
from dlccore.io.labeled_data._wide.table import LabeledDataRead, classify_labeled_data_read
from dlccore.io.labeled_data.model import FramePath, Keypoint
from dlccore.io.labeled_data.wide.header import LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data.wide.row_index import LabeledDataRowIndex, RowIndexShape, frame_part_as_text
from dlccore.io.labeled_data.wide.table import LabeledDataRead, classify_labeled_data_read
from dlccore.io.stamp import WriterStamp
from dlccore.io.suffixes import (
CSV_SUFFIX,
Expand All @@ -39,8 +39,8 @@
)

if TYPE_CHECKING:
from dlccore.io.labeled_data._wide.h5 import H5KeySelection
from dlccore.io.labeled_data.long.model import LabeledData
from dlccore.io.labeled_data.wide.h5 import H5KeySelection


@dataclass(frozen=True, eq=False)
Expand Down Expand Up @@ -97,13 +97,13 @@ def wide_suffix(path: Path) -> str | None:


def _read_h5(path: Path) -> LabeledDataRead:
from dlccore.io.labeled_data.wide.h5 import read_labeled_data_h5
from dlccore.io.labeled_data._wide.h5 import read_labeled_data_h5

return read_labeled_data_h5(path)


def _read_csv(path: Path) -> LabeledDataRead:
from dlccore.io.labeled_data.wide.csv import read_labeled_data_csv
from dlccore.io.labeled_data._wide.csv import read_labeled_data_csv

return read_labeled_data_csv(path)

Expand Down Expand Up @@ -175,10 +175,10 @@ def write_wide_labeled_data(labels: LabeledData, path: Path, *, replaces: FileSt
UserWarning: Once both files are in place, for each frame the CSV twin reads back without.
"""

from dlccore.io.labeled_data.checks.file import frames_lost_to_csv, unread_h5_keys
from dlccore.io.labeled_data.checks.model import keypoints_with_empty_individual, likelihood_in_human_labels
from dlccore.io.labeled_data.wide.csv import write_labeled_data_csv
from dlccore.io.labeled_data.wide.h5 import select_h5_key, write_labeled_data_h5
from dlccore.io.labeled_data._checks.file import frames_lost_to_csv, unread_h5_keys
from dlccore.io.labeled_data._checks.model import keypoints_with_empty_individual, likelihood_in_human_labels
from dlccore.io.labeled_data._wide.csv import write_labeled_data_csv
from dlccore.io.labeled_data._wide.h5 import select_h5_key, write_labeled_data_h5

if suffix_in(path, frozenset({H5_SUFFIX})) is None:
raise ValueError(f"{path}: the labeled-data writer writes an .h5 file and its .csv twin; got {path.suffix!r}.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

import pandas as pd

from dlccore.io.labeled_data.wide.header import LabeledDataHeader, classify_labeled_data_header
from dlccore.io.labeled_data.wide.row_index import LabeledDataRowIndex, classify_labeled_data_row_index
from dlccore.io.labeled_data._wide.header import LabeledDataHeader, classify_labeled_data_header
from dlccore.io.labeled_data._wide.row_index import LabeledDataRowIndex, classify_labeled_data_row_index

if TYPE_CHECKING:
from dlccore.io.labeled_data.wide.h5 import H5KeySelection
from dlccore.io.labeled_data._wide.h5 import H5KeySelection
from dlccore.io.stamp import WriterStamp


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import numpy as np
import pandas as pd

from dlccore.io.labeled_data.checks.file import (
from dlccore.io.labeled_data._checks.file import (
frames_of_other_depth,
frames_with_unnamed_parts,
has_no_frames,
Expand All @@ -32,15 +32,15 @@
non_numeric_columns,
row_index_is_unrecognised,
)
from dlccore.io.labeled_data._wide.header import LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data._wide.row_index import LabeledDataRowIndex, frame_part_as_text
from dlccore.io.labeled_data._wide.table import classify_wide_df
from dlccore.io.labeled_data.long.model import (
FRAME_COLUMNS,
LabeledData,
identity_values,
)
from dlccore.io.labeled_data.model import LIKELIHOOD_COORD, POSITION_COORDS
from dlccore.io.labeled_data.wide.header import LabeledDataHeader, LabeledDataHeaderShape
from dlccore.io.labeled_data.wide.row_index import LabeledDataRowIndex, frame_part_as_text
from dlccore.io.labeled_data.wide.table import classify_wide_df


def wide_to_long(
Expand Down
14 changes: 7 additions & 7 deletions dlccore/io/labeled_data/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
if TYPE_CHECKING:
import pandas as pd

from dlccore.io.labeled_data._wide.h5 import H5KeySelection
from dlccore.io.labeled_data._wide.header import LabeledDataHeader
from dlccore.io.labeled_data._wide.row_index import LabeledDataRowIndex
from dlccore.io.labeled_data._wide.storage import WideLabeledDataStorage
from dlccore.io.labeled_data.long.model import LabeledData
from dlccore.io.labeled_data.wide.h5 import H5KeySelection
from dlccore.io.labeled_data.wide.header import LabeledDataHeader
from dlccore.io.labeled_data.wide.row_index import LabeledDataRowIndex
from dlccore.io.labeled_data.wide.storage import WideLabeledDataStorage
from dlccore.io.stamp import WriterStamp


Expand Down Expand Up @@ -98,7 +98,7 @@ def labeled_data(self) -> LabeledData:
ValueError: Naming the file and why it has no model, e.g. a frame of other than three parts.
"""

from dlccore.io.labeled_data.wide.to_long import wide_to_long
from dlccore.io.labeled_data._wide.to_long import wide_to_long

try:
return wide_to_long(self._stored.dataframe, self._stored.header, self._stored.row_index)
Expand Down Expand Up @@ -126,7 +126,7 @@ def read_labeled_data(path: Path) -> LabeledDataFile:
ValueError: If no reader handles the suffix.
"""

from dlccore.io.labeled_data.wide.storage import read_wide_labeled_data
from dlccore.io.labeled_data._wide.storage import read_wide_labeled_data

try:
stat = FileStat.of(path)
Expand Down Expand Up @@ -166,6 +166,6 @@ def write_labeled_data(labels: LabeledData, path: Path, *, replaces: FileStat |
without values, which pandas and DeepLabCut read as the row index's names. The H5 keeps it.
"""

from dlccore.io.labeled_data.wide.storage import write_wide_labeled_data
from dlccore.io.labeled_data._wide.storage import write_wide_labeled_data

return write_wide_labeled_data(labels, path, replaces=replaces)
4 changes: 2 additions & 2 deletions dlccore/io/labeled_data/long/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def from_wide_df(cls, df: pd.DataFrame) -> LabeledData:
ValueError: If ``df`` cannot be converted; see ``wide.to_long.wide_to_long`` for the cases.
"""

from dlccore.io.labeled_data.wide.to_long import wide_df_to_long
from dlccore.io.labeled_data._wide.to_long import wide_df_to_long

return wide_df_to_long(df)

Expand All @@ -100,7 +100,7 @@ def to_arrays(self) -> LabeledDataArrays:
return long_to_arrays(self)

def to_wide_df(self) -> pd.DataFrame:
from dlccore.io.labeled_data.wide.from_long import long_to_wide
from dlccore.io.labeled_data._wide.from_long import long_to_wide

return long_to_wide(self)

Expand Down
4 changes: 2 additions & 2 deletions tests/io/fixtures/labeled_data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Written with a local napari-deeplabcut checkout at the producer commit below.
- `deeplabcut.utils.auxfun_multianimal.convert_single2multiplelegacyAM(config, userfeedback=False, target="multi")`, from a hand-built 3-level single-animal file whose multi-animal bodyparts are named `<individual>_<bodypart>`. Nothing in DeepLabCut calls this function; the header shape it writes is the one labeling writes;
- `deeplabcut.generate_training_dataset.trainingsetmanipulation.dropduplicatesinannotatinfiles(config)`, from a hand-built single-animal file with a flat string row index and one row twice.
- **Only these paths**: DeepLabCut's labeling and `convert2_maDLC` write the same header shapes by other code; neither is exercised here.
- **Bytes are not reproducible**: HDF5 stamps every object with its write time, so a rerun writes a different `.h5` holding an equal table. The 2026-10-10 and 2026-10-12 runs differ in 72 bytes across 24 objects, all timestamps. Each `.csv` reads back equal to its `.h5` twin (`tests/io/labeled_data/wide/test_csv.py`).
- **Bytes are not reproducible**: HDF5 stamps every object with its write time, so a rerun writes a different `.h5` holding an equal table. The 2026-10-10 and 2026-10-12 runs differ in 72 bytes across 24 objects, all timestamps. Each `.csv` reads back equal to its `.h5` twin (`tests/io/labeled_data/_wide/test_csv.py`).

| File | Animals | Source | Row index | Notes |
|---|---|---|---|---|
Expand All @@ -43,4 +43,4 @@ Rerun 2026-10-16 with deeplabcut 3.0.2, pandas 2.3.3, PyTables 3.11.1 and numpy

**Both projects read keyless**; `pd.read_hdf(path)` with no key succeeds for any single-key file. So a project labeled with both tools in that period legitimately holds both keys, one per video folder.

A `keypoints` file is therefore real labeled data produced by the GUI DeepLabCut ships for labelling, not a corrupt or exotic file. Fixtures covering it exist so that stays true. The registry entry asserting it is `LEGACY` is pinned by `tests/io/labeled_data/wide/test_h5.py`.
A `keypoints` file is therefore real labeled data produced by the GUI DeepLabCut ships for labelling, not a corrupt or exotic file. Fixtures covering it exist so that stays true. The registry entry asserting it is `LEGACY` is pinned by `tests/io/labeled_data/_wide/test_h5.py`.
14 changes: 7 additions & 7 deletions tests/io/labeled_data/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import pytest

from dlccore.io.labeled_data import Keypoint, LabeledData, read_labeled_data
from dlccore.io.labeled_data.checks.file import (
from dlccore.io.labeled_data._checks.file import (
frame_absorbed_into_index_names,
frames_of_other_depth,
frames_with_unnamed_parts,
Expand All @@ -28,14 +28,14 @@
row_index_is_unrecognised,
unread_h5_keys,
)
from dlccore.io.labeled_data.checks.model import (
from dlccore.io.labeled_data._checks.model import (
keypoints_with_empty_individual,
likelihood_in_human_labels,
missing_keypoints,
)
from dlccore.io.labeled_data.wide.h5 import H5KeySelectionOutcome, select_h5_key
from dlccore.io.labeled_data.wide.header import LabeledDataHeader, classify_labeled_data_header
from dlccore.io.labeled_data.wide.row_index import LabeledDataRowIndex, classify_labeled_data_row_index
from dlccore.io.labeled_data._wide.h5 import H5KeySelectionOutcome, select_h5_key
from dlccore.io.labeled_data._wide.header import LabeledDataHeader, classify_labeled_data_header
from dlccore.io.labeled_data._wide.row_index import LabeledDataRowIndex, classify_labeled_data_row_index

FIXTURES_DIR = Path(__file__).parents[1] / "fixtures" / "labeled_data"
SINGLE_ANIMAL_LEVELS = ["scorer", "bodyparts", "coords"]
Expand Down Expand Up @@ -253,8 +253,8 @@ def test_index_names_that_spell_no_three_part_frame_are_not_found(names: tuple[o


def test_checks_are_split_into_file_and_model_facts() -> None:
importlib.import_module("dlccore.io.labeled_data.checks.file")
importlib.import_module("dlccore.io.labeled_data.checks.model")
importlib.import_module("dlccore.io.labeled_data._checks.file")
importlib.import_module("dlccore.io.labeled_data._checks.model")


def test_a_keypoint_whose_individual_is_named_empty_is_found() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/io/labeled_data/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import dlccore.io.labeled_data as labeled_data
from dlccore.io.errors import UnreadableFileError
from dlccore.io.labeled_data import LabeledData, LabeledDataFile, read_labeled_data, write_labeled_data
from dlccore.io.labeled_data.wide.storage import WideLabeledDataStorage, read_wide_labeled_data
from dlccore.io.labeled_data._wide.storage import WideLabeledDataStorage, read_wide_labeled_data

FIXTURES_DIR = Path(__file__).parents[1] / "fixtures" / "labeled_data"
FIXTURES = sorted(path.name for path in FIXTURES_DIR.glob("*") if path.suffix in {".h5", ".csv"})
Expand Down
6 changes: 3 additions & 3 deletions tests/io/labeled_data/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from dlccore.io import FileChangedError, FileStat
from dlccore.io.labeled_data import LabeledData, read_labeled_data, write_labeled_data
from dlccore.io.labeled_data.checks.file import frames_lost_to_csv
from dlccore.io.labeled_data.wide.storage import csv_twin
from dlccore.io.labeled_data._checks.file import frames_lost_to_csv
from dlccore.io.labeled_data._wide.storage import csv_twin
from dlccore.io.stamp import WRITER_KEY, WRITER_VERSION_KEY, WriterStamp

FIXTURES_DIR = Path(__file__).parents[1] / "fixtures" / "labeled_data"
Expand Down Expand Up @@ -374,7 +374,7 @@ def test_files_beside_the_target_survive_a_write(tmp_path: Path) -> None:


def test_reading_records_the_stat_taken_before_the_read(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from dlccore.io.labeled_data.wide import storage
from dlccore.io.labeled_data._wide import storage

path = tmp_path / "CollectedData_Sc.h5"
path.write_bytes((FIXTURES_DIR / "single_animal_human.h5").read_bytes())
Expand Down
8 changes: 4 additions & 4 deletions tests/io/labeled_data/wide/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import pytest

from dlccore.io.errors import UnreadableFileError
from dlccore.io.labeled_data.wide.csv import read_labeled_data_csv
from dlccore.io.labeled_data.wide.h5 import read_labeled_data_h5
from dlccore.io.labeled_data.wide.header import LabeledDataHeaderShape
from dlccore.io.labeled_data.wide.table import classify_labeled_data_read
from dlccore.io.labeled_data._wide.csv import read_labeled_data_csv
from dlccore.io.labeled_data._wide.h5 import read_labeled_data_h5
from dlccore.io.labeled_data._wide.header import LabeledDataHeaderShape
from dlccore.io.labeled_data._wide.table import classify_labeled_data_read

FIXTURES_DIR = Path(__file__).parents[2] / "fixtures" / "labeled_data"
CSV_FIXTURES = sorted(path.name for path in FIXTURES_DIR.glob("*.csv"))
Expand Down
6 changes: 3 additions & 3 deletions tests/io/labeled_data/wide/test_from_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import pandas as pd
import pytest

from dlccore.io.labeled_data._wide.from_long import long_to_wide
from dlccore.io.labeled_data._wide.storage import read_wide_labeled_data
from dlccore.io.labeled_data._wide.to_long import wide_df_to_long, wide_to_long
from dlccore.io.labeled_data.long.model import LabeledData
from dlccore.io.labeled_data.wide.from_long import long_to_wide
from dlccore.io.labeled_data.wide.storage import read_wide_labeled_data
from dlccore.io.labeled_data.wide.to_long import wide_df_to_long, wide_to_long

FIXTURES_DIR = Path(__file__).parents[2] / "fixtures" / "labeled_data"
FIXTURES = sorted(path.name for path in FIXTURES_DIR.glob("*") if path.suffix in {".h5", ".csv"})
Expand Down
2 changes: 1 addition & 1 deletion tests/io/labeled_data/wide/test_h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tables

from dlccore.io.errors import UnreadableFileError
from dlccore.io.labeled_data.wide.h5 import (
from dlccore.io.labeled_data._wide.h5 import (
H5_KEY_REGISTRY,
H5KeySelection,
H5KeySelectionOutcome,
Expand Down
Loading
Loading