Skip to content

Commit

Permalink
Merge pull request #35 from ImageMarkup/isic-123-check-rcm-lesions
Browse files Browse the repository at this point in the history
Enforce that RCM cases belong to at most one lesion
  • Loading branch information
danlamanna authored May 15, 2024
2 parents 1991db5 + 81342fe commit 68afb51
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions isic_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ def check_rcm_at_most_one_macroscopic_image(self) -> MetadataBatch:

return self

@model_validator(mode="after")
def check_rcm_case_at_most_one_lesion(self) -> MetadataBatch:
rcm_case_to_lesions: dict[str, set[str]] = defaultdict(set)

for item in self.items:
if item.rcm_case_id and item.lesion_id:
rcm_case_to_lesions[item.rcm_case_id].add(item.lesion_id)

bad_rcm_cases = [
rcm_case for rcm_case in rcm_case_to_lesions if len(rcm_case_to_lesions[rcm_case]) > 1
]
if bad_rcm_cases:
raise PydanticCustomError(
"one_rcm_case_multiple_lesions",
"One or more RCM cases belong to multiple lesions.",
{"examples": bad_rcm_cases[:5]},
)

return self


class MetadataRow(BaseModel):
model_config = ConfigDict(
Expand Down
25 changes: 25 additions & 0 deletions tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,28 @@ def test_rcm_case_has_at_most_one_macroscopic_image():
MetadataRow(image_type="RCM: macroscopic", rcm_case_id="bar"),
]
)


def test_rcm_cases_belong_to_same_lesion():
with pytest.raises(ValidationError) as excinfo:
MetadataBatch(
items=[
MetadataRow(
rcm_case_id="foo", lesion_id="foolesion", _ignore_rcm_model_checks=True
),
MetadataRow(
rcm_case_id="foo", lesion_id="barlesion", _ignore_rcm_model_checks=True
),
]
)
assert len(excinfo.value.errors()) == 1
assert "belong to multiple lesions" in excinfo.value.errors()[0]["msg"]


def test_blank_rcm_cases_dont_belong_to_same_lesion():
MetadataBatch(
items=[
MetadataRow(rcm_case_id="", lesion_id="foolesion"),
MetadataRow(rcm_case_id="", lesion_id="barlesion"),
]
)

0 comments on commit 68afb51

Please sign in to comment.