generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstudy_definition_cohort.py
131 lines (123 loc) · 4.19 KB
/
study_definition_cohort.py
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
from cohortextractor import (
StudyDefinition,
Measure,
patients,
codelist,
combine_codelists,
codelist_from_csv,
)
from codelists import *
from common_variables import demographic_variables, clinical_variables
pandemic_start = "2020-02-01"
def make_variable(code):
return {
f"snomed_{code}": (
patients.with_these_clinical_events(
codelist([code], system="snomed"),
on_or_after=pandemic_start,
returning="number_of_matches_in_period",
include_date_of_match=True,
date_format="YYYY-MM-DD",
return_expectations={
"incidence": 0.1,
"int": {"distribution": "normal", "mean": 3, "stddev": 1},
},
)
)
}
def loop_over_codes(code_list):
variables = {}
for code in code_list:
variables.update(make_variable(code))
return variables
study = StudyDefinition(
default_expectations={
"date": {"earliest": "index_date", "latest": "today"},
"rate": "uniform",
"incidence": 0.05,
"int": {"distribution": "normal", "mean": 25, "stddev": 5},
"float": {"distribution": "normal", "mean": 25, "stddev": 5},
},
index_date="2020-11-01",
population=patients.satisfying(
"registered AND (sex = 'M' OR sex = 'F')",
registered=patients.registered_as_of("index_date"),
),
# COVID infection
sgss_positive=patients.with_test_result_in_sgss(
pathogen="SARS-CoV-2",
test_result="positive",
returning="date",
date_format="YYYY-MM-DD",
find_first_match_in_period=True,
return_expectations={"incidence": 0.1, "date": {"earliest": "index_date"}},
),
primary_care_covid=patients.with_these_clinical_events(
any_primary_care_code,
returning="date",
date_format="YYYY-MM-DD",
find_first_match_in_period=True,
return_expectations={"incidence": 0.1, "date": {"earliest": "index_date"}},
),
hospital_covid=patients.admitted_to_hospital(
with_these_diagnoses=covid_codes,
returning="date_admitted",
date_format="YYYY-MM-DD",
find_first_match_in_period=True,
return_expectations={"incidence": 0.1, "date": {"earliest": "index_date"}},
),
# Outcome
long_covid=patients.with_these_clinical_events(
any_long_covid_code,
return_expectations={"incidence": 0.05},
),
first_long_covid_date=patients.with_these_clinical_events(
any_long_covid_code,
returning="date",
date_format="YYYY-MM-DD",
find_first_match_in_period=True,
return_expectations={"incidence": 0.1, "date": {"earliest": "index_date"}},
),
**loop_over_codes(any_long_covid_code),
first_long_covid_code=patients.with_these_clinical_events(
any_long_covid_code,
returning="code",
find_first_match_in_period=True,
return_expectations={
"incidence": 0.05,
"category": {
"ratios": {
"1325161000000102": 0.2,
"1325181000000106": 0.2,
"1325021000000106": 0.3,
"1325051000000101": 0.2,
"1325061000000103": 0.1,
}
},
},
),
post_viral_fatigue=patients.with_these_clinical_events(
post_viral_fatigue_codes,
on_or_after=pandemic_start,
return_expectations={"incidence": 0.05},
),
first_post_viral_fatigue_date=patients.with_these_clinical_events(
post_viral_fatigue_codes,
on_or_after=pandemic_start,
returning="date",
date_format="YYYY-MM-DD",
find_first_match_in_period=True,
return_expectations={"incidence": 0.1, "date": {"earliest": "index_date"}},
),
**loop_over_codes(post_viral_fatigue_codes),
practice_id=patients.registered_practice_as_of(
"index_date",
returning="pseudo_id",
return_expectations={
"int": {"distribution": "normal", "mean": 1000, "stddev": 100},
"incidence": 1,
},
),
**demographic_variables,
# **clinical_variables,
)