Skip to content

Commit 8c7b33e

Browse files
authored
Merge pull request #21 from ImageMarkup/isic-167-skip-emptyish-values
Skip emptyish values in processing metadata
2 parents 64c7ff8 + 0418e7a commit 8c7b33e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

isic_metadata/metadata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import functools
55
from typing import Any, Callable, Optional, Union
66

7+
import numpy as np
78
from pydantic import (
89
BaseModel,
910
BeforeValidator,
@@ -169,6 +170,16 @@ def strip(cls, v):
169170
v = v.strip()
170171
return v
171172

173+
@model_validator(mode="before")
174+
@classmethod
175+
def strip_none_and_nan_values(cls, values: dict[str, Any]) -> dict[str, Any]:
176+
for field_name, value in list(values.items()):
177+
if value is None or value is np.nan:
178+
del values[field_name]
179+
elif isinstance(value, str) and not value.strip():
180+
del values[field_name]
181+
return values
182+
172183
@field_validator(
173184
"anatom_site_general",
174185
"benign_malignant",

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
"Programming Language :: Python",
3434
],
3535
python_requires=">=3.9",
36-
install_requires=["pandas", "pydantic>=2"],
36+
install_requires=["pandas", "numpy", "pydantic>=2"],
3737
packages=find_packages(),
3838
)

tests/test_fields.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any
22

33
from hypothesis import given, strategies as st
4+
import numpy as np
45
from pydantic import ValidationError
56
import pytest
67

@@ -13,6 +14,13 @@ def test_unstructured_fields():
1314
assert metadata.unstructured["hello"] == "world"
1415

1516

17+
@pytest.mark.parametrize(("emptyish_value"), ["", " ", "\t", np.nan, None])
18+
def test_empty_fields_are_omitted(emptyish_value):
19+
metadata = MetadataRow(diagnosis="melanoma", mel_type=emptyish_value)
20+
assert metadata.diagnosis == "melanoma"
21+
assert metadata.mel_thick_mm is None
22+
23+
1624
def test_melanoma_fields():
1725
with pytest.raises(ValidationError) as excinfo:
1826
# mel_class can only be set if diagnosis is melanoma

0 commit comments

Comments
 (0)