Skip to content

Commit

Permalink
Add test for region attribute not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
sfmig committed Jun 18, 2024
1 parent 68354d0 commit eb2b836
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 66 deletions.
61 changes: 32 additions & 29 deletions movement/validators/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,43 +327,46 @@ def csv_file_contains_tracked_bboxes(self, attribute, value):

# Check each row contains a bounding box
for row in df.itertuples():

Check warning on line 329 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L329

Added line #L329 was not covered by tests
# extract region shape and regular attributes
row_region_shape_attrs = ast.literal_eval(

Check warning on line 331 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L331

Added line #L331 was not covered by tests
row.region_shape_attributes
)
row_region_attrs = ast.literal_eval(row.region_attributes)

Check warning on line 334 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L334

Added line #L334 was not covered by tests
# check annotation is a rectangle
if ast.literal_eval(row.region_shape_attributes)["name"] != "rect":
if row_region_shape_attrs["name"] != "rect":
raise log_error(

Check warning on line 337 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L336-L337

Added lines #L336 - L337 were not covered by tests
ValueError,
f"{row.filename} (row {row.Index}): "
"bounding box shape must be 'rect' (rectangular) "
"but instead got "
f"'{ast.literal_eval(row.region_shape_attributes)['name']}'.",
f"'{row_region_shape_attrs['name']}'.",
)

# # check geometric parameters for the box are defined
# if not all(
# [
# key in ast.literal_eval(row.region_shape_attributes)
# for key in ["x", "y", "width", "height"]
# ]
# ):
# raise log_error(
# ValueError,
# f"At least one bounding box shape parameter is missing. "
# "Expected 'x', 'y', 'width', 'height' to exist as "
# "'region_shape_attributes', but got "
# f"{ast.literal_eval(row.region_shape_attributes).keys()}"
# f"for file {row.filename} (row {row.Index}). ",
# )

# # check track ID is defined
# if "track" not in ast.literal_eval(row.region_attributes):
# raise log_error(
# ValueError,
# f"Bounding box in file {row.filename} "
# f"and row {row.Index} "
# f"(0-based) does not have a 'track' attribute defined. "
# "Please review the VIA tracks csv file and ensure that "
# "all bounding boxes have a 'track' field under "
# "'region_attributes'.",
# )
# check all geometric parameters for the box are defined
if not all(

Check warning on line 346 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L346

Added line #L346 was not covered by tests
[
key in row_region_shape_attrs
for key in ["x", "y", "width", "height"]
]
):
raise log_error(

Check warning on line 352 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L352

Added line #L352 was not covered by tests
ValueError,
f"{row.filename} (row {row.Index}): "
f"at least one bounding box shape parameter is missing. "
"Expected 'x', 'y', 'width', 'height' to exist as "
"'region_shape_attributes', but got "
f"'{list(row_region_shape_attrs.keys())}'.",
)

# check track ID is defined
if "track" not in row_region_attrs:
raise log_error(

Check warning on line 363 in movement/validators/files.py

View check run for this annotation

Codecov / codecov/patch

movement/validators/files.py#L362-L363

Added lines #L362 - L363 were not covered by tests
ValueError,
f"{row.filename} (row {row.Index}): "
"bounding box does not have a 'track' attribute defined "
"under 'region_attributes'. "
"Please review the VIA tracks csv file.",
)

# # check track ID is castable as an integer
# try:
Expand Down
42 changes: 42 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,48 @@ def region_shape_attribute_not_rect(
return file_path


@pytest.fixture
def region_shape_attribute_missing_x(
via_tracks_csv_with_valid_header,
):
"""Return the file path for a VIA tracks csv file with invalid shape in
region_shape_attributes.
"""
file_path = via_tracks_csv_with_valid_header
with open(file_path, "a") as f: # append to the file
f.write(
"04.09.2023-04-Right_RE_test_frame_01.png,"
"26542080,"
'"{""clip"":123}",'
"1,"
"0,"
'"{""name"":""rect"",""y"":393.280914246804,""width"":46,""height"":38}",'
'"{""track"":""71""}"'
)
return file_path


@pytest.fixture
def region_attribute_missing_track(
via_tracks_csv_with_valid_header,
):
"""Return the file path for a VIA tracks csv file with missing track
attribute in region_attributes.
"""
file_path = via_tracks_csv_with_valid_header
with open(file_path, "a") as f:
f.write(
"04.09.2023-04-Right_RE_test_frame_01.png,"
"26542080,"
'"{""clip"":123}",'
"1,"
"0,"
'"{""name"":""rect"",""x"":526.2366942646654,""y"":393.280914246804,""width"":46,""height"":38}",'
'"{""foo"":""71""}"'
)
return file_path


class Helpers:
"""Generic helper methods for ``movement`` testing modules."""

Expand Down
53 changes: 16 additions & 37 deletions tests/test_unit/test_validators/test_files_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_deeplabcut_csv_validator_with_invalid_input(
ValidDeepLabCutCSV(file_path)


# @pytest.mark.skip("Fixtures not implemented yet")
@pytest.mark.skip("Fixtures not implemented yet")
@pytest.mark.parametrize(
"invalid_input, log_message",
[
Expand Down Expand Up @@ -115,42 +115,21 @@ def test_deeplabcut_csv_validator_with_invalid_input(
"bounding box shape must be 'rect' (rectangular) "
"but instead got 'circle'.",
),
# (
# "region_shape_attributes_missing_x",
# "At least one bounding box shape parameter is missing. "
# "Expected 'x', 'y', 'width', 'height' to exist as "
# "'region_shape_attributes', but got "
# "'\{bla\:blah\}' for file bla.csv (row 0).,",
# ),
# (
# "region_shape_attributes_missing_y",
# "At least one bounding box shape parameter is missing. "
# "Expected 'x', 'y', 'width', 'height' to exist as "
# "'region_shape_attributes', but got "
# "'\{bla\:blah\}' for file bla.csv (row 0).,",
# ),
# (
# "region_shape_attributes_missing_width",
# "At least one bounding box shape parameter is missing. "
# "Expected 'x', 'y', 'width', 'height' to exist as "
# "'region_shape_attributes', but got "
# "'\{bla\:blah\}' for file bla.csv (row 0).,",
# ),
# (
# "region_shape_attributes_missing_height",
# "At least one bounding box shape parameter is missing. "
# "Expected 'x', 'y', 'width', 'height' to exist as "
# "'region_shape_attributes', but got "
# "'\{bla\:blah\}' for file bla.csv (row 0).,",
# ),
# (
# "region_attributes_missing_track",
# "Bounding box in file bla.csv and row 0 "
# "(0-based) does not have a 'track' attribute defined. "
# "Please review the VIA tracks csv file and ensure that "
# "all bounding boxes have a 'track' field under "
# "'region_attributes'.",
# ),
(
"region_shape_attribute_missing_x",
"04.09.2023-04-Right_RE_test_frame_01.png (row 0): "
"at least one bounding box shape parameter is missing. "
"Expected 'x', 'y', 'width', 'height' to exist as "
"'region_shape_attributes', but got "
"'['name', 'y', 'width', 'height']'.",
),
(
"region_attribute_missing_track",
"04.09.2023-04-Right_RE_test_frame_01.png (row 0): "
"bounding box does not have a 'track' attribute defined "
"under 'region_attributes'. "
"Please review the VIA tracks csv file.",
),
# (
# "track_id_not_castable_as_int",
# "The track ID for the bounding box in file "
Expand Down

0 comments on commit eb2b836

Please sign in to comment.