File size: 9,531 Bytes
8018595
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Tests for the MRAT Melanoma Risk Model.

Ground truth values collected from: https://mrisktool.cancer.gov/calculator.html.
All scenarios assume the patient is Non-Hispanic white, matching the published MRAT scope.
"""

import pytest

from sentinel.risk_models import MRATRiskModel
from sentinel.user_input import (
    Anthropometrics,
    ComplexionLevel,
    Demographics,
    DermatologicProfile,
    FemaleSmallMolesCategory,
    FemaleTanResponse,
    FrecklingIntensity,
    Lifestyle,
    MaleSmallMolesCategory,
    PersonalMedicalHistory,
    Sex,
    SmokingHistory,
    SmokingStatus,
    UserInput,
    USGeographicRegion,
)

GROUND_TRUTH_CASES = [
    {
        "name": "male_light_complexion_high_damage",
        "input": UserInput(
            demographics=Demographics(
                age_years=30,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.NORTHERN,
                complexion=ComplexionLevel.LIGHT,
                freckling=FrecklingIntensity.MILD,
                male_sunburn=True,  # True=YES, had sunburn
                male_has_two_or_more_big_moles=False,  # False=<2 moles
                male_small_moles=MaleSmallMolesCategory.LESS_THAN_SEVEN,
                solar_damage=False,  # False=NO damage
            ),
        ),
        "expected": 0.02,
    },
    {
        "name": "male_medium_complexion_average_moles",
        "input": UserInput(
            demographics=Demographics(
                age_years=45,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.CENTRAL,
                complexion=ComplexionLevel.MEDIUM,
                freckling=FrecklingIntensity.MODERATE,
                male_sunburn=False,  # False=NO sunburn
                male_has_two_or_more_big_moles=True,  # True=≥2 moles
                male_small_moles=MaleSmallMolesCategory.SEVEN_TO_SIXTEEN,
                solar_damage=True,  # True=YES damage
            ),
        ),
        "expected": 0.54,
    },
    {
        "name": "female_central_region_moderate_features",
        "input": UserInput(
            demographics=Demographics(
                age_years=50,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.SOUTHERN,
                complexion=ComplexionLevel.MEDIUM,
                freckling=FrecklingIntensity.MODERATE,
                female_tan=FemaleTanResponse.MODERATE,
                female_small_moles=FemaleSmallMolesCategory.FIVE_TO_ELEVEN,
            ),
        ),
        "expected": 0.16,
    },
    {
        "name": "female_northern_region_severe_freckling",
        "input": UserInput(
            demographics=Demographics(
                age_years=65,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.NORTHERN,
                complexion=ComplexionLevel.LIGHT,
                freckling=FrecklingIntensity.SEVERE,
                female_tan=FemaleTanResponse.NONE,
                female_small_moles=FemaleSmallMolesCategory.TWELVE_OR_MORE,
            ),
        ),
        "expected": 1.19,
    },
    {
        "name": "male_dark_complexion_extensive_moles",
        "input": UserInput(
            demographics=Demographics(
                age_years=55,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.SOUTHERN,
                complexion=ComplexionLevel.DARK,
                freckling=FrecklingIntensity.ABSENT,
                male_sunburn=False,  # False=NO sunburn
                male_has_two_or_more_big_moles=False,  # False=<2 moles
                male_small_moles=MaleSmallMolesCategory.SEVENTEEN_OR_MORE,
                solar_damage=False,  # False=NO damage
            ),
        ),
        "expected": 0.52,
    },
]


class TestMRATModel:
    """Test suite for MRATRiskModel."""

    def setup_method(self) -> None:
        """Initialise the MRAT model instance for each test."""
        self.model = MRATRiskModel()

    @pytest.mark.parametrize("case", GROUND_TRUTH_CASES, ids=lambda x: x["name"])
    def test_ground_truth_placeholders(self, case):
        """Check that absolute risk calculation returns a float for each scenario.

        Args:
            case (dict[str, MRATInput | float | str]): Test scenario definition.
        """
        result = self.model.absolute_risk(case["input"])
        assert isinstance(result, float)

    def test_compute_score_male_user(self):
        """Ensure male user profiles yield a percentage string from compute_score."""
        user = UserInput(
            demographics=Demographics(
                age_years=42,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.NORTHERN,
                complexion=ComplexionLevel.LIGHT,
                freckling=FrecklingIntensity.MILD,
                male_sunburn=True,
                male_has_two_or_more_big_moles=True,
                male_small_moles=MaleSmallMolesCategory.SEVEN_TO_SIXTEEN,
                solar_damage=False,
            ),
        )

        score = self.model.compute_score(user)
        assert score.endswith("%")

    def test_compute_score_female_user(self):
        """Ensure female user profiles yield a percentage string from compute_score."""
        user = UserInput(
            demographics=Demographics(
                age_years=37,
                sex=Sex.FEMALE,
                anthropometrics=Anthropometrics(height_cm=165.0, weight_kg=65.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.SOUTHERN,
                complexion=ComplexionLevel.MEDIUM,
                freckling=FrecklingIntensity.MODERATE,
                female_tan=FemaleTanResponse.MODERATE,
                female_small_moles=FemaleSmallMolesCategory.FIVE_TO_ELEVEN,
            ),
        )

        score = self.model.compute_score(user)
        assert score.endswith("%")

    def test_missing_dermatologic(self):
        """Verify missing dermatologic information raises ValueError."""
        user = UserInput(
            demographics=Demographics(
                age_years=30,
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=None,
        )

        with pytest.raises(ValueError, match=r"Invalid inputs for MRAT:"):
            self.model.compute_score(user)

    def test_validation_errors(self):
        """Test that model raises ValueError for invalid inputs."""
        user_input = UserInput(
            demographics=Demographics(
                age_years=15,  # Below minimum
                sex=Sex.MALE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=70.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            dermatologic=DermatologicProfile(
                region=USGeographicRegion.NORTHERN,
                complexion=ComplexionLevel.LIGHT,
                freckling=FrecklingIntensity.MILD,
            ),
        )

        with pytest.raises(ValueError, match=r"Invalid inputs for MRAT:"):
            self.model.compute_score(user_input)