Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] No failure when creation date is not null and other improvements to _check_derived_image_xml #957

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 39 additions & 24 deletions clinica/iotools/converters/adni_to_bids/adni_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,46 @@ def _get_text(xml_el: xml.etree.ElementTree.Element, cast=None) -> str:
return xml_el.text


def _check_derived_image_xml(derived: xml.etree.ElementTree.Element):
def _check_derived_image_xml(derived: xml.etree.ElementTree.Element) -> None:
"""Perform sanity checks on derived images."""
assert len(derived) >= 9 # multiple <provenanceDetail> nodes possible
assert (
_check_xml_and_get_text(derived[2], "imageType") == "image volume"
), "imageType"
assert _check_xml_and_get_text(derived[3], "tissue") == "All", "tissue"
assert _check_xml_and_get_text(derived[4], "hemisphere") == "Both", "hemis"
assert (
_check_xml_and_get_text(derived[5], "anatomicStructure") == "Brain"
), "anat struct"
assert (
_check_xml_and_get_text(derived[6], "registration") == "native"
), "registration not native"
# skip check relatedImage, original ID consistence + "derived from"...
# skip processing steps with names, versions, dates and so on
# 2 cases
if derived[-1].tag == "creationDate":
assert (
_check_xml_and_get_text(derived[-1], "creationDate") == "0000-00-00"
), "creaDate"
else:
assert (
_check_xml_and_get_text(derived[-2], "creationDate") == "0000-00-00"
), "creaDate"
if len(derived) < 9:
raise ValueError("derived image does not have enough field")
for idx, field, expected in zip(
[2, 3, 4, 5, 6],
["imageType", "tissue", "hemisphere", "anatomicStructure", "registration"],
["image volume", "All", "Both", "Brain", "native"],
):
_check_xml_field(derived[idx], field, expected)
idx = -1 if derived[-1].tag == "creationDate" else -2
if (
image_create_date := _check_xml_and_get_text(derived[idx], "creationDate")
) != "0000-00-00":
try:
_validate_date_iso_format(image_create_date)
except ValueError as e:
raise ValueError(
f"The creationDate for the derived image is not valid: {e}"
)


def _check_xml_field(
xml_element: xml.etree.ElementTree.Element, field: str, expected_value: str
) -> None:
"""Check that the given field of the given XML element has the expected value."""
if (found_value := _check_xml_and_get_text(xml_element, field)) != expected_value:
raise ValueError(
f"The {field} for the derived image should be '{expected_value}'. "
f"{found_value} was found instead."
)


def _validate_date_iso_format(date: str) -> None:
import datetime

try:
datetime.date.fromisoformat(date)
except ValueError:
raise ValueError(f"Incorrect data format {date}, should be YYYY-MM-DD")


def _get_derived_image_metadata(derived: xml.etree.ElementTree.Element) -> dict:
Expand Down