Skip to content
Draft
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: 10 additions & 0 deletions dlccore/project/schemas/project_layout/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#
106 changes: 106 additions & 0 deletions dlccore/project/schemas/project_layout/labeled_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

from dlccore.io.suffixes import H5_SUFFIX, HUMAN_LABELS_PREFIX, LABELED_DATA_TABLE_SUFFIXES
from dlccore.project.schemas.base.filesystem import (
DirectorySchema,
FileSchema,
NamePattern,
RepeatedNodeSchema,
)
from dlccore.project.schemas.base.resources import (
DirectoryResourceKind,
FileResourceKind,
ResourceManagement,
)

# FIXME: @C-Achard review valid file types
LABELED_FRAME = FileSchema(
schema_id="dlc.labeled_data.frame",
kind=FileResourceKind.LABELED_FRAME,
name=NamePattern(
suffixes=frozenset(
{
".png",
".jpg",
".jpeg",
".bmp",
".tif",
".tiff",
}
)
),
description="An extracted frame available for labeling.",
)
HUMAN_LABELS = FileSchema(
schema_id="dlc.labeled_data.human_labels",
kind=FileResourceKind.HUMAN_LABELS,
name=NamePattern(
prefix=HUMAN_LABELS_PREFIX,
suffixes=LABELED_DATA_TABLE_SUFFIXES,
),
description="Human keypoint labels for a labeled dataset.",
)

MACHINE_LABELS = FileSchema(
schema_id="dlc.labeled_data.machine_labels",
kind=FileResourceKind.MACHINE_LABELS,
name=NamePattern(
prefix="machinelabels-iter",
suffixes=frozenset({H5_SUFFIX}),
),
description="Machine keypoint labels for a labeled dataset, per training iteration.",
)

MACHINE_LABELS_CSV = FileSchema(
schema_id="dlc.labeled_data.machine_labels_csv",
kind=FileResourceKind.MACHINE_LABELS,
name=NamePattern(exact="machinelabels.csv"),
description="Machine keypoint labels as CSV, written beside the latest iteration's H5.",
)

NAPARI_DLC_METADATA = FileSchema(
schema_id="dlc.labeled_data.napari_metadata",
kind=FileResourceKind.NAPARI_DLC_METADATA,
name=NamePattern(
exact=".napari-deeplabcut.json",
),
required=False,
description="JSON metadata for the napari DeepLabCut plugin.",
management=ResourceManagement.KNOWN_UNMANAGED,
)


# ``name`` is deliberately unconstrained: a directory here cannot be told apart from
# DeepLabCut ``check_labels`` plot output, or from a dataset not labeled yet, by structure
# alone. Discovery declines to guess; the judgement belongs to validation.
LABELED_DATASET = DirectorySchema(
schema_id="dlc.labeled_data.dataset",
kind=DirectoryResourceKind.LABELED_DATASET,
name=NamePattern(),
description=("A labeled dataset associated with one source video or another config-defined dataset identity."),
children=(NAPARI_DLC_METADATA,),
repeated_children=(
RepeatedNodeSchema(child=LABELED_FRAME),
RepeatedNodeSchema(child=HUMAN_LABELS),
RepeatedNodeSchema(child=MACHINE_LABELS),
RepeatedNodeSchema(child=MACHINE_LABELS_CSV),
),
)


LABELED_DATA = DirectorySchema(
schema_id="dlc.labeled_data",
kind=DirectoryResourceKind.LABELED_DATA_DIRECTORY,
name=NamePattern(exact="labeled-data"),
description="Root directory containing labeled datasets.",
repeated_children=(RepeatedNodeSchema(child=LABELED_DATASET),),
)
48 changes: 48 additions & 0 deletions dlccore/project/schemas/project_layout/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

from dlccore.project.schemas.base.filesystem import (
DirectorySchema,
FileSchema,
NamePattern,
index_layout,
)
from dlccore.project.schemas.base.resources import DirectoryResourceKind, FileResourceKind
from dlccore.project.schemas.project_layout.labeled_data import LABELED_DATA
from dlccore.project.schemas.project_layout.videos import VIDEOS

PROJECT_CONFIG = FileSchema(
schema_id="dlc.project.config",
kind=FileResourceKind.PROJECT_CONFIG,
name=NamePattern(exact="config.yaml"),
required=True,
description="The authoritative DeepLabCut project configuration.",
)


PROJECT = DirectorySchema(
schema_id="dlc.project",
kind=DirectoryResourceKind.PROJECT_ROOT,
name=NamePattern(),
required=True,
description="A canonical DeepLabCut project root.",
children=(
# Config file
PROJECT_CONFIG,
## Input and labeled data
VIDEOS,
LABELED_DATA,
## Training datasets
### Extend later...
),
)

PROJECT_INDEX = index_layout(PROJECT)
37 changes: 37 additions & 0 deletions dlccore/project/schemas/project_layout/videos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

from dlccore.io.suffixes import VIDEO_SUFFIXES
from dlccore.project.schemas.base.filesystem import (
DirectorySchema,
FileSchema,
NamePattern,
RepeatedNodeSchema,
)
from dlccore.project.schemas.base.resources import DirectoryResourceKind, FileResourceKind

SOURCE_VIDEO = FileSchema(
schema_id="dlc.videos.source_video",
kind=FileResourceKind.SOURCE_VIDEO,
name=NamePattern(
suffixes=VIDEO_SUFFIXES,
),
description="A source video registered with the project.",
)


VIDEOS = DirectorySchema(
schema_id="dlc.videos",
kind=DirectoryResourceKind.VIDEOS_DIRECTORY,
name=NamePattern(exact="videos"),
description="Root directory for project source videos.",
repeated_children=(RepeatedNodeSchema(child=SOURCE_VIDEO),),
)
50 changes: 50 additions & 0 deletions tests/io/test_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

"""``dlccore.io``'s import boundaries: nothing from ``dlccore`` outside io, suffixes only the standard library, and
no format module loaded until it is imported.

``dlccore.project`` reaches io only through ``dlccore.project.content``, except for the layout, which imports the
data in ``dlccore.io.suffixes``.
"""

import ast
Expand All @@ -27,6 +30,9 @@
PACKAGE_ROOT = Path(dlccore.__file__).parent
IO = "dlccore.io"
SUFFIXES = f"{IO}.suffixes"
PROJECT = "dlccore.project"
CONTENT = f"{PROJECT}.content"
SCHEMAS = f"{PROJECT}.schemas"


def _within(name: str, package: str) -> bool:
Expand Down Expand Up @@ -88,6 +94,50 @@ def test_only_io_imports_the_private_labeled_data_modules() -> None:
assert {module: names for module, names in found.items() if names and not _within(module, IO)} == {}


def _may_import_from_io(module: str, name: str) -> bool:
"""Content may import any of io; the layout may import its suffixes, which are data only."""

return _within(module, CONTENT) or (_within(module, SCHEMAS) and _within(name, SUFFIXES))


def test_only_content_imports_io_and_the_layout_only_its_suffixes() -> None:
offending = {
module: sorted(name for name in imports if _within(name, IO) and not _may_import_from_io(module, name))
for module, imports in _package_modules()
if _within(module, PROJECT)
}

assert {module: names for module, names in offending.items() if names} == {}


def test_the_layout_may_import_no_other_part_of_io() -> None:
# Control: the exception for suffixes must not let the layout import io's readers.
assert _may_import_from_io(f"{SCHEMAS}.project_layout.videos", SUFFIXES)
assert not _may_import_from_io(f"{SCHEMAS}.project_layout.videos", f"{IO}.labeled_data")
assert not _may_import_from_io(f"{PROJECT}.validation.structure", SUFFIXES)


def test_the_scan_sees_the_layout_import_io_suffixes() -> None:
# Control: the layout does import suffixes, so the exception above is exercised.
assert SUFFIXES in dict(_package_modules())[f"{SCHEMAS}.project_layout.videos"]


def _modules_spelling(literal: str) -> set[str]:
"""Modules holding ``literal`` as a code constant; docstrings, which only mention it, are longer strings."""

return {
path.relative_to(PACKAGE_ROOT).as_posix()
for path in PACKAGE_ROOT.rglob("*.py")
for node in ast.walk(ast.parse(path.read_text(encoding="utf-8")))
if isinstance(node, ast.Constant) and node.value == literal
}


def test_the_human_labels_prefix_is_spelled_once_in_io() -> None:
# The layout imports it from io.suffixes, so the project layer and the I/O layer can never claim different files.
assert _modules_spelling("CollectedData_") == {"io/suffixes.py"}


def test_the_scan_reaches_io() -> None:
# Control: io's own __init__ imports its errors module, so an empty scan cannot pass the test above.
assert f"{IO}.errors" in dict(_package_modules())[IO]
Expand Down
32 changes: 32 additions & 0 deletions tests/project/schemas/test_layout_suffixes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# DeepLabCut Toolbox (deeplabcut.org)
# © A. & M.W. Mathis Labs
# https://github.com/DeepLabCut/dlccore
#
# Contributors:
# https://github.com/DeepLabCut/dlccore/graphs/contributors
#
# Licensed under GNU Lesser General Public License v3.0
#

from pathlib import Path

import pytest

from dlccore.io.suffixes import LABELED_DATA_TABLE_SUFFIXES, VIDEO_SUFFIXES
from dlccore.project.schemas.base.filesystem import FileSchema
from dlccore.project.schemas.project_layout.labeled_data import HUMAN_LABELS, MACHINE_LABELS
from dlccore.project.schemas.project_layout.videos import SOURCE_VIDEO


@pytest.mark.parametrize(
("schema", "suffixes"),
[(HUMAN_LABELS, LABELED_DATA_TABLE_SUFFIXES), (MACHINE_LABELS, {".h5"}), (SOURCE_VIDEO, VIDEO_SUFFIXES)],
)
def test_the_layout_claims_the_suffixes_io_reads(schema: FileSchema, suffixes: frozenset[str]) -> None:
assert schema.name.suffixes == suffixes


@pytest.mark.parametrize("name", ["session.flv", "session.MPV", "session.qt", "session.yuv"])
def test_every_video_deeplabcut_reads_is_claimed_as_a_video(name: str) -> None:
assert SOURCE_VIDEO.name.matches(Path(name))
Loading
Loading