Skip to content

Commit f30f065

Browse files
committed
Added filter for subjects invited since age extension using attribute and domain check
1 parent 9c9c1c8 commit f30f065

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class InvitedSinceAgeExtension:
2+
"""
3+
Maps subject invitation criteria based on age extension presence.
4+
"""
5+
6+
YES = "yes"
7+
NO = "no"
8+
9+
_valid_values = {YES, NO}
10+
11+
@classmethod
12+
def from_description(cls, description: str) -> str:
13+
key = description.strip().lower()
14+
if key not in cls._valid_values:
15+
raise ValueError(
16+
f"Invalid invited-since-age-extension flag: '{description}'"
17+
)
18+
return key

utils/oracle/mock_selection_builder.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@
99

1010

1111
# Add helper class stubs below
12-
class HasDateOfDeathRemoval:
12+
class InvitedSinceAgeExtension:
13+
"""
14+
Maps input describing whether subject was invited since age extension.
15+
"""
16+
1317
YES = "yes"
1418
NO = "no"
1519

16-
_mapping = {
17-
"yes": YES,
18-
"no": NO,
19-
}
20+
_valid_values = {YES, NO}
2021

2122
@classmethod
2223
def from_description(cls, description: str) -> str:
2324
key = description.strip().lower()
24-
if key not in cls._mapping:
25+
if key not in cls._valid_values:
2526
raise ValueError(
26-
f"Unknown input for HasDateOfDeathRemoval: '{description}'"
27+
f"Unknown invited-since-age-extension flag: '{description}'"
2728
)
28-
return cls._mapping[key]
29+
return key
2930

3031

3132
class MockSelectionBuilder:
@@ -94,19 +95,22 @@ def _add_join_to_surveillance_review(self):
9495
# Replace this with the one you want to test,
9596
# then use utils/oracle/test_subject_criteria_dev.py to run your scenarios
9697

97-
def _add_criteria_has_date_of_death_removal(self) -> None:
98+
def _add_criteria_invited_since_age_extension(self) -> None:
9899
"""
99-
Filters subjects based on presence or absence of a date-of-death removal record.
100+
Filters subjects based on whether they were invited since age extension began.
100101
"""
101102
try:
102-
value = HasDateOfDeathRemoval.from_description(self.criteria_value)
103+
self._add_join_to_latest_episode()
104+
value = InvitedSinceAgeExtension.from_description(self.criteria_value)
103105
clause = "EXISTS" if value == "yes" else "NOT EXISTS"
104106

105107
self.sql_where.append(
106-
f"AND {clause} (SELECT 'dodr' FROM report_additional_data_t dodr "
107-
"WHERE dodr.rad_type_id = 15901 "
108-
"AND dodr.entity_id = c.contact_id)"
108+
f"AND {clause} (SELECT 'sagex' FROM screening_subject_attribute_t sagex "
109+
"INNER JOIN valid_values vvagex ON vvagex.valid_value_id = sagex.attribute_id "
110+
"AND vvagex.domain = 'FOBT_AGEX_LOWER_AGE' "
111+
"WHERE sagex.screening_subject_id = ep.screening_subject_id "
112+
"AND sagex.start_date < ep.episode_start_date)"
109113
)
110-
111114
except Exception:
112115
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
116+

utils/oracle/subject_selection_query_builder.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646
from classes.surveillance_review_case_type import SurveillanceReviewCaseType
4747
from classes.has_date_of_death_removal import HasDateOfDeathRemoval
48+
from classes.invited_since_age_extension import InvitedSinceAgeExtension
4849

4950

5051
class SubjectSelectionQueryBuilder:
@@ -2062,6 +2063,25 @@ def _add_criteria_has_date_of_death_removal(self) -> None:
20622063
except Exception:
20632064
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
20642065

2066+
def _add_criteria_invited_since_age_extension(self) -> None:
2067+
"""
2068+
Filters subjects based on whether they were invited since age extension began.
2069+
"""
2070+
try:
2071+
self._add_join_to_latest_episode()
2072+
value = InvitedSinceAgeExtension.from_description(self.criteria_value)
2073+
clause = "EXISTS" if value == "yes" else "NOT EXISTS"
2074+
2075+
self.sql_where.append(
2076+
f"AND {clause} (SELECT 'sagex' FROM screening_subject_attribute_t sagex "
2077+
"INNER JOIN valid_values vvagex ON vvagex.valid_value_id = sagex.attribute_id "
2078+
"AND vvagex.domain = 'FOBT_AGEX_LOWER_AGE' "
2079+
"WHERE sagex.screening_subject_id = ep.screening_subject_id "
2080+
"AND sagex.start_date < ep.episode_start_date)"
2081+
)
2082+
except Exception:
2083+
raise SelectionBuilderException(self.criteria_key_name, self.criteria_value)
2084+
20652085
# ------------------------------------------------------------------------
20662086
# 🧬 CADS Clinical Dataset Filters
20672087
# ------------------------------------------------------------------------

utils/oracle/test_subject_criteria_dev.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ def make_builder(key, value, index=0, comparator="="):
4141
return b
4242

4343

44-
# === Test: HAS_HAD_A_DATE_OF_DEATH_REMOVAL (yes) ===
45-
b = make_builder(SubjectSelectionCriteriaKey.HAS_HAD_A_DATE_OF_DEATH_REMOVAL, "yes")
46-
b._add_criteria_has_date_of_death_removal()
47-
print("=== HAS_HAD_A_DATE_OF_DEATH_REMOVAL (yes) ===")
44+
# === Test: INVITED_SINCE_AGE_EXTENSION (yes) ===
45+
b = make_builder(SubjectSelectionCriteriaKey.INVITED_SINCE_AGE_EXTENSION, "yes")
46+
b._add_criteria_invited_since_age_extension()
47+
print("=== INVITED_SINCE_AGE_EXTENSION (yes) ===")
4848
print(b.dump_sql(), end="\n\n")
4949

50-
# === Test: HAS_HAD_A_DATE_OF_DEATH_REMOVAL (no) ===
51-
b = make_builder(SubjectSelectionCriteriaKey.HAS_HAD_A_DATE_OF_DEATH_REMOVAL, "no")
52-
b._add_criteria_has_date_of_death_removal()
53-
print("=== HAS_HAD_A_DATE_OF_DEATH_REMOVAL (no) ===")
50+
# === Test: INVITED_SINCE_AGE_EXTENSION (no) ===
51+
b = make_builder(SubjectSelectionCriteriaKey.INVITED_SINCE_AGE_EXTENSION, "no")
52+
b._add_criteria_invited_since_age_extension()
53+
print("=== INVITED_SINCE_AGE_EXTENSION (no) ===")
5454
print(b.dump_sql(), end="\n\n")

0 commit comments

Comments
 (0)