Skip to content

Commit

Permalink
Merge pull request #38 from ImageMarkup/isic-171-use-decimal-fields
Browse files Browse the repository at this point in the history
Use decimal fields where possible
  • Loading branch information
danlamanna authored May 20, 2024
2 parents aa6fac8 + f5d9a21 commit 842da5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
9 changes: 6 additions & 3 deletions isic_metadata/metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from collections import defaultdict
from decimal import Decimal
from typing import Annotated, Any, Literal

from annotated_types import Ge # noqa: TCH002
Expand Down Expand Up @@ -47,7 +48,7 @@
"enum": "Unsupported value for {loc}: '{value}'.",
"int_parsing": "Unable to parse value as an integer.",
"bool_parsing": "Unable to parse value as a boolean.",
"float_parsing": "Unable to parse value as a number.",
"decimal_parsing": "Unable to parse value as a number.",
"greater_than_equal": "Number must be greater than or equal to {ge}.",
}

Expand Down Expand Up @@ -208,15 +209,17 @@ class MetadataRow(BaseModel):
personal_hx_mm: bool | None = None
family_hx_mm: bool | None = None
clin_size_long_diam_mm: (
Annotated[float, BeforeValidator(ClinSizeLongDiamMm.parse_measurement_str)] | None
Annotated[Decimal, BeforeValidator(ClinSizeLongDiamMm.parse_measurement_str)] | None
) = None
fitzpatrick_skin_type: FitzpatrickSkinType | None = None
melanocytic: bool | None = None
concomitant_biopsy: bool | None = None

mel_class: MelClassEnum | None = None
mel_mitotic_index: MelMitoticIndexEnum | None = None
mel_thick_mm: Annotated[float, BeforeValidator(MelThickMm.parse_measurement_str)] | None = None
mel_thick_mm: Annotated[Decimal, BeforeValidator(MelThickMm.parse_measurement_str)] | None = (
None
)
mel_type: MelTypeEnum | None = None
mel_ulcer: bool | None = None

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ ignore = [
# Excessive
"RET503", # implict return
"PLR2004", # magic value used in comparison

"TCH003", # type checking blocks

]

[tool.ruff.lint.per-file-ignores]
Expand Down
26 changes: 15 additions & 11 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from typing import Any

from hypothesis import given
Expand All @@ -15,8 +16,8 @@
[
("age", "54", 54, {}),
("melanocytic", "True", True, {}),
("clin_size_long_diam_mm", "4mm", 4.0, {}),
("mel_thick_mm", ".33mm", 0.33, {"diagnosis": "melanoma"}),
("clin_size_long_diam_mm", "4mm", Decimal("4.0"), {}),
("mel_thick_mm", ".33mm", Decimal("0.33"), {"diagnosis": "melanoma"}),
("mel_ulcer", "false", False, {"diagnosis": "melanoma"}),
("family_hx_mm", "False", False, {}),
("personal_hx_mm", "0", False, {}),
Expand Down Expand Up @@ -82,11 +83,11 @@ def test_nevus_diagnosis():
@pytest.mark.parametrize(
("raw", "parsed"),
[
("4.5 mm", 4.5),
("14.2 mm", 14.2),
("4.5mm", 4.5),
("1mm", 1.0),
("3.25", 3.25),
("4.5 mm", Decimal("4.5")),
("14.2 mm", Decimal("14.2")),
("4.5mm", Decimal("4.5")),
("1mm", Decimal("1.0")),
("3.25", Decimal("3.25")),
],
)
def test_mel_thick_mm(raw: str, parsed: float):
Expand All @@ -102,13 +103,16 @@ def test_mel_thick_mm_invalid():


@given(
clin_size=st.one_of(st.floats(min_value=0, exclude_min=True), st.integers(min_value=1)).map(
lambda x: f"{x} mm"
)
clin_size=st.one_of(
# keep max values bounded so they don't generate larger than representable decimals.
# disallow infinity to avoid the string 'inf'.
st.floats(min_value=0, max_value=1_000_000, exclude_min=True, allow_infinity=False),
st.integers(min_value=1, max_value=1_000),
).map(lambda x: f"{x} mm")
)
def test_clin_size_long_diam_mm_always_rounded(clin_size: str):
metadata = MetadataRow.model_validate({"clin_size_long_diam_mm": clin_size})
assert isinstance(metadata.clin_size_long_diam_mm, float)
assert isinstance(metadata.clin_size_long_diam_mm, Decimal)
assert metadata.clin_size_long_diam_mm == round(metadata.clin_size_long_diam_mm, ndigits=1)


Expand Down

0 comments on commit 842da5c

Please sign in to comment.