File size: 9,111 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
255
256
257
258
259
# pylint: disable=missing-docstring
"""Basic tests for the PCPT prostate cancer risk model.

Web calculator available at: https://riskcalc.org/PCPTRC/
"""

import pytest

from sentinel.risk_models import PCPTRiskModel
from sentinel.user_input import (
    Anthropometrics,
    CancerType,
    ClinicalTests,
    Demographics,
    DREResult,
    DRETest,
    Ethnicity,
    FamilyMemberCancer,
    FamilyRelation,
    FamilySide,
    Lifestyle,
    PCA3Test,
    PercentFreePSATest,
    PersonalMedicalHistory,
    PSATest,
    RelationshipDegree,
    Sex,
    SmokingHistory,
    SmokingStatus,
    T2ERGTest,
    UserInput,
)

# Ground-truth regression fixtures collected from the official PCPT web calculator.
GROUND_TRUTH_CASES = [
    {
        "name": "low_risk",
        "input": UserInput(
            demographics=Demographics(
                age_years=70,
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(
                prior_negative_prostate_biopsy=False,
            ),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=10.0),
                dre=DRETest(result=DREResult.NORMAL),
            ),
        ),
        "expected_high_grade": 15.0,
        "expected_low_grade": 23.0,
        "expected_no_cancer": 62.0,
    },
    {
        "name": "medium_high_risk",
        "input": UserInput(
            demographics=Demographics(
                age_years=80,
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(
                prior_negative_prostate_biopsy=False,
            ),
            family_history=[
                FamilyMemberCancer(
                    relation=FamilyRelation.FATHER,
                    side=FamilySide.PATERNAL,
                    degree=RelationshipDegree.FIRST,
                    cancer_type=CancerType.PROSTATE,
                    age_at_diagnosis=70,
                )
            ],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=25.0),
            ),
        ),
        "expected_high_grade": 42.0,
        "expected_low_grade": 26.0,
        "expected_no_cancer": 32.0,
    },
    {
        "name": "high_risk",
        "input": UserInput(
            demographics=Demographics(
                age_years=65,
                sex=Sex.MALE,
                ethnicity=Ethnicity.BLACK,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(
                prior_negative_prostate_biopsy=False,
            ),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=36.0),
                dre=DRETest(result=DREResult.ABNORMAL),
            ),
        ),
        "expected_high_grade": 66.0,
        "expected_low_grade": 13.0,
        "expected_no_cancer": 21.0,
    },
]


class TestPCPTRiskModel:
    def setup_method(self) -> None:
        self.model = PCPTRiskModel()

    def test_metadata(self) -> None:
        assert self.model.name == "pcpt"
        assert self.model.cancer_type() == "prostate"
        assert "PCPT" in self.model.description()
        assert "percent" in self.model.interpretation().lower()
        assert len(self.model.references()) > 0

    def test_absolute_risk_basic(self) -> None:
        user = UserInput(
            demographics=Demographics(
                age_years=60,
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(
                prior_negative_prostate_biopsy=False,
            ),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=2.5),
                dre=DRETest(result=DREResult.NORMAL),
            ),
        )
        risks = self.model.absolute_risk(user)
        assert risks["no_cancer"] > 0
        assert risks["low_grade"] > 0
        assert risks["high_grade"] > 0
        total = risks["no_cancer"] + risks["low_grade"] + risks["high_grade"]
        assert 99.5 <= total <= 100.5

    @pytest.mark.parametrize("case", GROUND_TRUTH_CASES, ids=lambda case: case["name"])
    def test_ground_truth_cases(self, case) -> None:
        risks = self.model.absolute_risk(case["input"])
        assert risks["high_grade"] == pytest.approx(
            case["expected_high_grade"], abs=2.0
        )
        assert risks["low_grade"] == pytest.approx(case["expected_low_grade"], abs=2.0)
        assert risks["no_cancer"] == pytest.approx(case["expected_no_cancer"], abs=2.0)

    def test_compute_score_with_male_user_input(self) -> None:
        user = UserInput(
            demographics=Demographics(
                age_years=60,
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(
                prior_negative_prostate_biopsy=False,
            ),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=2.5),
                percent_free_psa=PercentFreePSATest(value_percent=18.0),
                dre=DRETest(result=DREResult.NORMAL),
                pca3=PCA3Test(score=25.0),
                t2erg=T2ERGTest(score=10.0),
            ),
        )

        score = self.model.compute_score(user)
        assert "No Cancer" in score
        assert "Low Grade" in score
        assert "High Grade" in score

    def test_compute_score_rejects_female_user(self) -> None:
        user = UserInput(
            demographics=Demographics(
                age_years=60,
                sex=Sex.FEMALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=2.5),
            ),
        )

        score = self.model.compute_score(user)
        assert score == "N/A: PCPT applies to male patients only."

    def test_validation_errors(self) -> None:
        """Test validation errors for missing required fields."""
        user = UserInput(
            demographics=Demographics(
                age_years=60,
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            family_history=[],
            clinical_tests=ClinicalTests(),  # Missing PSA
        )

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

    def test_age_out_of_range(self) -> None:
        """Test age outside validated range raises ValueError."""
        user = UserInput(
            demographics=Demographics(
                age_years=50,  # Below minimum
                sex=Sex.MALE,
                ethnicity=Ethnicity.WHITE,
                anthropometrics=Anthropometrics(height_cm=175.0, weight_kg=80.0),
            ),
            lifestyle=Lifestyle(
                smoking=SmokingHistory(status=SmokingStatus.NEVER, pack_years=0.0),
            ),
            personal_medical_history=PersonalMedicalHistory(),
            family_history=[],
            clinical_tests=ClinicalTests(
                psa=PSATest(value_ng_ml=2.5),
            ),
        )

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