Skip to content

Commit

Permalink
Remove untracked from names
Browse files Browse the repository at this point in the history
  • Loading branch information
sfmig committed Dec 16, 2024
1 parent 3b3af6a commit 17711ff
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 47 deletions.
17 changes: 7 additions & 10 deletions ethology/annotations/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import pandas as pd
from movement.validators.files import ValidFile

from ethology.annotations.json_schemas import (
COCO_UNTRACKED_SCHEMA,
VIA_UNTRACKED_SCHEMA,
)
from ethology.annotations.json_schemas import COCO_SCHEMA, VIA_SCHEMA
from ethology.annotations.validators import (
ValidCOCOUntrackedJSON,
ValidCOCOJSON,
ValidJSON,
ValidVIAUntrackedJSON,
ValidVIAJSON,
)

STANDARD_DF_COLUMNS = [
Expand Down Expand Up @@ -49,8 +46,8 @@ def df_from_via_json_file(file_path: Path) -> pd.DataFrame:
file = ValidFile(
file_path, expected_permission="r", expected_suffix=[".json"]
)
json_file = ValidJSON(path=file.path, schema=VIA_UNTRACKED_SCHEMA)
via_untracked_file = ValidVIAUntrackedJSON(json_file.path)
json_file = ValidJSON(path=file.path, schema=VIA_SCHEMA)
via_untracked_file = ValidVIAJSON(json_file.path)

# Read as standard dataframe
return _df_from_validated_via_json_file(via_untracked_file.path)
Expand All @@ -76,8 +73,8 @@ def df_from_coco_json_file(file_path: Path) -> pd.DataFrame:
file = ValidFile(
file_path, expected_permission="r", expected_suffix=[".json"]
)
json_file = ValidJSON(path=file.path, schema=COCO_UNTRACKED_SCHEMA)
coco_untracked_file = ValidCOCOUntrackedJSON(json_file.path)
json_file = ValidJSON(path=file.path, schema=COCO_SCHEMA)
coco_untracked_file = ValidCOCOJSON(json_file.path)

# Read as standard dataframe
return _df_from_validated_coco_json_file(coco_untracked_file.path)
Expand Down
4 changes: 2 additions & 2 deletions ethology/annotations/json_schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""JSON schemas for VIA and COCO annotations."""

VIA_UNTRACKED_SCHEMA = {
VIA_SCHEMA = {
"type": "object",
"properties": {
# settings for browser UI
Expand Down Expand Up @@ -66,7 +66,7 @@
},
}

COCO_UNTRACKED_SCHEMA = {
COCO_SCHEMA = {
"type": "object",
"properties": {
"info": {"type": "object"},
Expand Down
4 changes: 2 additions & 2 deletions ethology/annotations/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _file_matches_JSON_schema(self, attribute, value):


@define
class ValidVIAUntrackedJSON:
class ValidVIAJSON:
"""Class for validating VIA JSON files for untracked data.
Checks the VIA JSON file for untracked data contains the required keys.
Expand Down Expand Up @@ -135,7 +135,7 @@ def _file_contains_required_keys(self, attribute, value):


@define
class ValidCOCOUntrackedJSON:
class ValidCOCOJSON:
"""Class for validating COCO JSON files for untracked data.
The validator ensures that the file matches the expected schema.
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def pooch_registry() -> dict:
@pytest.fixture()
def get_paths_test_data():
"""Define a factory fixture to get the paths of the data files
under a specific zip.
under a specific subdirectory in the GIN repository.
The name of the zip file is intended to match a testing module. For
The name of the subdirectories is intended to match a testing module. For
example, to get the paths to the test files for the annotations
tests module, we would call `get_paths_test_data(pooch_registry,
module, we would call `get_paths_test_data(pooch_registry,
"test_annotations")` in a test. This assumes in the GIN repository
there is a zip file named `test_annotations.zip` under the `test_data`
directory containing the relevant test files.
there is a subdirectory named `test_annotations` under the `test_data`
directory with the relevant test files.
"""

def _get_paths_test_data(pooch_registry, subdir_name: str) -> dict:
Expand Down
56 changes: 28 additions & 28 deletions tests/test_unit/test_annotations/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import jsonschema
import pytest

from ethology.annotations.json_schemas import (
COCO_UNTRACKED_SCHEMA,
VIA_UNTRACKED_SCHEMA,
)
from ethology.annotations.json_schemas import COCO_SCHEMA, VIA_SCHEMA
from ethology.annotations.validators import (
ValidCOCOUntrackedJSON,
ValidCOCOJSON,
ValidJSON,
ValidVIAUntrackedJSON,
ValidVIAJSON,
_check_keys,
)

Expand Down Expand Up @@ -179,9 +176,9 @@ def _coco_json_file_with_missing_keys(
"input_file_standard, input_schema",
[
("VIA", None),
("VIA", VIA_UNTRACKED_SCHEMA),
("VIA", VIA_SCHEMA),
("COCO", None),
("COCO", COCO_UNTRACKED_SCHEMA),
("COCO", COCO_SCHEMA),
],
)
@pytest.mark.parametrize(
Expand All @@ -194,7 +191,7 @@ def test_valid_json(
input_schema,
annotations_test_data,
):
"""Test the ValidJSON validator with valid files."""
"""Test the ValidJSON validator with valid inputs."""
filepath = annotations_test_data[
f"{input_file_standard}_{input_json_file_suffix}"
]
Expand Down Expand Up @@ -223,13 +220,13 @@ def test_valid_json(
),
(
"via_json_file_with_schema_error",
VIA_UNTRACKED_SCHEMA,
VIA_SCHEMA,
pytest.raises(jsonschema.exceptions.ValidationError),
"49.5 is not of type 'integer'\n\n",
),
(
"coco_json_file_with_schema_error",
COCO_UNTRACKED_SCHEMA,
COCO_SCHEMA,
pytest.raises(jsonschema.exceptions.ValidationError),
"[{'area': 432, 'bbox': [1278, 556, 16, 27], 'category_id': 1, "
"'id': 8917, 'image_id': 199, 'iscrowd': 0}] is not of type "
Expand Down Expand Up @@ -263,16 +260,17 @@ def test_valid_json_errors(
"VIA_JSON_sample_2.json",
],
)
def test_valid_via_untracked_json(annotations_test_data, input_json_file):
def test_valid_via_json(annotations_test_data, input_json_file):
"""Test the ValidVIAJSON validator with valid inputs."""
filepath = annotations_test_data[input_json_file]
with does_not_raise():
ValidVIAUntrackedJSON(
ValidVIAJSON(
path=filepath,
)


@pytest.mark.parametrize(
"valid_json_file",
"valid_via_json_file",
[
"VIA_JSON_sample_1.json",
"VIA_JSON_sample_2.json",
Expand Down Expand Up @@ -316,32 +314,33 @@ def test_valid_via_untracked_json(annotations_test_data, input_json_file):
),
],
)
def test_valid_via_untracked_json_missing_keys(
valid_json_file,
def test_valid_via_json_missing_keys(
valid_via_json_file,
missing_keys,
via_json_file_with_missing_keys,
expected_exception,
log_message,
):
# create invalid json file with missing keys
"""Test the ValidVIAJSON when input has missing keys."""
# create invalid VIA json file with missing keys
invalid_json_file, edited_image_dicts = via_json_file_with_missing_keys(
valid_json_file, missing_keys
valid_via_json_file, missing_keys
)

# get key of affected image in _via_img_metadata
# get key of affected images in _via_img_metadata
img_key_str = edited_image_dicts.get(list(missing_keys.keys())[0], None)

# run validation
with expected_exception as excinfo:
ValidVIAUntrackedJSON(
ValidVIAJSON(
path=invalid_json_file,
)

assert str(excinfo.value) == log_message.format(img_key_str)


@pytest.mark.parametrize(
"valid_json_file",
"valid_coco_json_file",
[
"COCO_JSON_sample_1.json",
"COCO_JSON_sample_2.json",
Expand Down Expand Up @@ -383,24 +382,25 @@ def test_valid_via_untracked_json_missing_keys(
),
],
)
def test_valid_coco_untracked_json(
valid_json_file,
def test_valid_coco_json_missing_keys(
valid_coco_json_file,
missing_keys,
coco_json_file_with_missing_keys,
expected_exception,
log_message,
):
"""Test the ValidCOCOJSON when input has missing keys."""
# create invalid json file with missing keys
invalid_json_file, edited_image_dicts = coco_json_file_with_missing_keys(
valid_json_file, missing_keys
valid_coco_json_file, missing_keys
)

# get key of affected image in _via_img_metadata
img_dict = edited_image_dicts.get(list(missing_keys.keys())[0], None)

# run validation
with expected_exception as excinfo:
ValidCOCOUntrackedJSON(
ValidCOCOJSON(
path=invalid_json_file,
)

Expand All @@ -415,7 +415,7 @@ def test_valid_coco_untracked_json(
{"images": "", "annotations": "", "categories": ""},
"",
does_not_raise(),
),
), # zero missing keys
(
["images", "annotations", "categories"],
{"annotations": "", "categories": ""},
Expand All @@ -431,9 +431,9 @@ def test_valid_coco_untracked_json(
(
["images", "annotations", "categories"],
{"annotations": "", "categories": ""},
"TEST",
"FOO",
pytest.raises(ValueError),
), # one missing key
), # one missing key with additional message
],
)
def test_check_keys(
Expand Down

0 comments on commit 17711ff

Please sign in to comment.