Skip to content

Commit

Permalink
Merge branch 'anipose-loader' of https://github.com/neuroinformatics-…
Browse files Browse the repository at this point in the history
…unit/movement into anipose-loader
  • Loading branch information
vigji committed Dec 9, 2024
2 parents dd21bc1 + b6e24b6 commit 6a22f33
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions movement/validators/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def _file_contains_expected_levels(self, attribute, value):
".csv header rows do not match the known format for "
"DeepLabCut pose estimation output files.",
)



@define
class ValidAniposeCSV:
"""Class for validating Anipose-style .csv files.
Expand Down Expand Up @@ -248,43 +249,66 @@ def _file_contains_expected_headers(self, attribute, value):
These are to be found among the top 4 rows of the file.
"""
expected_header_suffixes = ["_x", "_y", "_z", "_score", "_error", "_ncams"]
expected_headers = ["fnum", "center_0", "center_1", "center_2",
"M_00", "M_01", "M_02", "M_10", "M_11",
"M_12", "M_20", "M_21", "M_22"]

expected_header_suffixes = [
"_x",
"_y",
"_z",
"_score",
"_error",
"_ncams",
]
expected_headers = [
"fnum",
"center_0",
"center_1",
"center_2",
"M_00",
"M_01",
"M_02",
"M_10",
"M_11",
"M_12",
"M_20",
"M_21",
"M_22",
]

# Read the first line of the CSV to get the headers
with open(value) as f:
headers = f.readline().strip().split(",")

# Check that all expected headers are present
if not all(h in headers for h in expected_headers):
raise log_error(
ValueError,
"CSV file is missing some expected headers."
f"Expected: {expected_headers}."
f"Expected: {expected_headers}.",
)

# For all other headers, check they have expected suffixes and base names
other_headers = [h for h in headers if h not in expected_headers]
for header in other_headers:
# Check suffix
if not any(header.endswith(suffix) for suffix in expected_header_suffixes):
if not any(
header.endswith(suffix) for suffix in expected_header_suffixes
):
raise log_error(
ValueError,
f"Header {header} does not have an expected suffix."
f"Header {header} does not have an expected suffix.",
)
# Get base name by removing suffix
base = header.rsplit("_", 1)[0]
# Check base name has all expected suffixes
if not all(f"{base}{suffix}" in headers for suffix in expected_header_suffixes):
if not all(
f"{base}{suffix}" in headers
for suffix in expected_header_suffixes
):
raise log_error(
ValueError,
ValueError,
f"Base header {base} is missing some expected suffixes."
f"Expected: {expected_header_suffixes};"
f"Got: {headers}."
f"Got: {headers}.",
)



@define
Expand Down

0 comments on commit 6a22f33

Please sign in to comment.