Skip to content

Commit

Permalink
Merge pull request #1704 from opensafely-core/bot/update-external-stu…
Browse files Browse the repository at this point in the history
…dies

Update `external_studies` test code
  • Loading branch information
iaindillingham authored Nov 7, 2023
2 parents 772f062 + cfceecf commit 9e3bb6c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class matched_hx_comparator(PatientFrame):
dataset.end_deregist = matched_hx_comparator.end_deregist
dataset.end_lc_cure = matched_hx_comparator.end_lc_cure
dataset.end_date = minimum_of(dataset.end_death, dataset.end_deregist, dataset.end_lc_cure, study_end_date)
dataset.total_hx_drug_visit = total_hx_drug_visit # historical prescription visits

# Add current prescription visits
total_drug_visit(dataset, dataset.index_date, end_date=dataset.end_date)

# Outcome visit
# Historical GP visits: 2019-3-1 to 2020-3-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class matched_hx_cases(PatientFrame):
dataset.end_deregist = matched_hx_cases.end_deregist
dataset.end_lc_cure = matched_hx_cases.end_lc_cure
dataset.end_date = minimum_of(dataset.end_death, dataset.end_deregist, dataset.end_lc_cure, study_end_date)
dataset.total_hx_drug_visit = total_hx_drug_visit # historical prescription visits

# Add current prescription visits
total_drug_visit(dataset, dataset.index_date, end_date=dataset.end_date)


# Outcome visit
# Historical GP visits: 2019-3-1 to 2020-3-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ class matched_cases(PatientFrame):
dataset.exposure = matched_cases.exposure

# Add visits for any prescriptions:
total_drug_visit(dataset, dataset.index_date, num_months=1, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=2, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=3, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=4, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=5, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=6, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=7, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=8, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=9, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=10, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=11, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=12, end_date=dataset.end_date)

total_drug_visit(dataset, dataset.index_date, end_date=dataset.end_date)

# Add drug prescription frequencies by BNF chapters
# drugs: bnf ch1 : gi drugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,6 @@ class matched_cases(PatientFrame):
outpatient_visit(dataset, from_date=dataset.index_date, num_months=11, end_date=dataset.end_date)
outpatient_visit(dataset, from_date=dataset.index_date, num_months=12, end_date=dataset.end_date)

# Outpatient visit for long COVID
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=1, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=2, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=3, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=4, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=5, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=6, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=7, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=8, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=9, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=10, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=11, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=12, end_date=dataset.end_date)


# Hospital admission costs
cost_apc_fn(dataset, from_date=dataset.index_date, num_months=1, end_date=dataset.end_date)
cost_apc_fn(dataset, from_date=dataset.index_date, num_months=2, end_date=dataset.end_date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,7 @@ class matched_matches(PatientFrame):
dataset.exposure = matched_matches.exposure

# Add visits for any prescriptions:
total_drug_visit(dataset, dataset.index_date, num_months=1, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=2, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=3, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=4, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=5, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=6, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=7, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=8, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=9, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=10, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=11, end_date=dataset.end_date)
total_drug_visit(dataset, dataset.index_date, num_months=12, end_date=dataset.end_date)

total_drug_visit(dataset, dataset.index_date, end_date=dataset.end_date)

# Add drug prescription frequencies by BNF chapters
# drugs: bnf ch1 : gi drugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,6 @@ class matched_matches(PatientFrame):
outpatient_visit(dataset, from_date=dataset.index_date, num_months=11, end_date=dataset.end_date)
outpatient_visit(dataset, from_date=dataset.index_date, num_months=12, end_date=dataset.end_date)

# Outpatient visit for long COVID
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=1, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=2, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=3, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=4, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=5, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=6, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=7, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=8, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=9, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=10, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=11, end_date=dataset.end_date)
outpatient_lc_dx_visit(dataset, from_date=dataset.index_date, num_months=12, end_date=dataset.end_date)


# Hospital admission costs
cost_apc_fn(dataset, from_date=dataset.index_date, num_months=1, end_date=dataset.end_date)
cost_apc_fn(dataset, from_date=dataset.index_date, num_months=2, end_date=dataset.end_date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import date
from databuilder.ehrql import Dataset, days, years, case, when
from databuilder.tables.beta.tpp import (
patients, addresses, appointments,
patients, addresses, appointments, medications,
practice_registrations, clinical_events,
sgss_covid_all_tests, ons_deaths, hospital_admissions,
)
Expand All @@ -16,6 +16,7 @@
from variables import hospitalisation_diagnosis_matches

hx_study_start_date = date(2019, 3, 1)
hx_study_end_date = date(2020, 3, 1)

# Function for extracting clinical factors
def clinical_ctv3_matches(gpevent, codelist):
Expand Down Expand Up @@ -107,6 +108,12 @@ def clinical_ctv3_matches(gpevent, codelist):
# # temporary immunosuppression
temporary_immune_suppress = clinical_ctv3_matches(clinical_events, codelists.temporary_immune_suppress_code)

# Add historical prescription visit counts:
total_hx_drug_visit = medications \
.where((medications.date >= hx_study_start_date) &
(medications.date <= hx_study_end_date)) \
.date.count_distinct_for_patient()

# The following codes will be removed later when the importing CSV file function is ready.
# Use these codes to test this is working.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def add_hx_gp_visits(dataset, num_months):
def add_hos_visits(dataset, from_date, num_months, end_date):
# Number of Hospitalisation within `num_months` of `from_date`
num_visits = hospital_admissions \
.where((hospital_admissions.admission_date >= from_date + days((num_months-1)*30)) &
(hospital_admissions.admission_date < from_date + days(num_months*30)) &
(hospital_admissions.admission_date <= end_date)) \
.where((hospital_admissions.discharge_date >= (hospital_admissions.admission_date + days(1))) &
(hospital_admissions.admission_date >= from_date + days((num_months-1)*30)) &
(hospital_admissions.admission_date < from_date + days(num_months*30)) &
(hospital_admissions.admission_date <= end_date)) \
.admission_date.count_distinct_for_patient()
setattr(dataset, f"hos_visit_m{num_months}", num_visits)

Expand All @@ -68,7 +69,7 @@ def add_hx_hos_visits(dataset, num_months):
# Number of Hospitalisation within `num_months` of `from_date`
num_visits = hospital_admissions \
.where((hospital_admissions.admission_date >= hx_study_start_date + days((num_months-1)*30)) &
(hospital_admissions.discharge_date < hx_study_start_date + days(num_months*30))) \
(hospital_admissions.discharge_date < hx_study_start_date + days(num_months*30))) \
.admission_date.count_distinct_for_patient()
setattr(dataset, f"hx_hos_visit_m{num_months}", num_visits)

Expand All @@ -78,8 +79,8 @@ def add_ae_visits(dataset, from_date, num_months, end_date):
# Number of A&E visits within `num_months` of `from_date`
num_visits = emergency_care_attendances \
.where((emergency_care_attendances.arrival_date >= from_date + days((num_months-1)*30)) &
(emergency_care_attendances.arrival_date < from_date + days(num_months*30)) &
(emergency_care_attendances.arrival_date <= end_date)) \
(emergency_care_attendances.arrival_date < from_date + days(num_months*30)) &
(emergency_care_attendances.arrival_date <= end_date)) \
.arrival_date.count_distinct_for_patient()
setattr(dataset, f"ae_visit_m{num_months}", num_visits)

Expand Down Expand Up @@ -339,6 +340,7 @@ def cost_opa_fn(dataset, from_date, num_months, end_date):
def hos_stay_long_fn(dataset, from_date, end_date):
hos_stay_long = hospital_admissions \
.where((hospital_admissions.admission_date >= from_date) &
(hospital_admissions.discharge_date >= (hospital_admissions.admission_date + days(1))) &
((hospital_admissions.discharge_date > (hospital_admissions.admission_date + days(14)))) &
((hospital_admissions.admission_date + days(14)) <= end_date)) \
.count_for_patient()
Expand All @@ -348,6 +350,7 @@ def hos_stay_long_fn(dataset, from_date, end_date):
def hos_stay_short_fn(dataset, from_date, end_date):
hos_stay_short = hospital_admissions \
.where((hospital_admissions.admission_date >= from_date) &
(hospital_admissions.discharge_date >= (hospital_admissions.admission_date + days(1))) &
((hospital_admissions.discharge_date <= (hospital_admissions.admission_date + days(14)))) &
((hospital_admissions.discharge_date <= end_date))) \
.count_for_patient()
Expand All @@ -356,41 +359,28 @@ def hos_stay_short_fn(dataset, from_date, end_date):

# Function for adding all visit for medications.
# Count visits on the same day once:
def total_drug_visit(dataset, from_date, num_months, end_date):
def total_drug_visit(dataset, from_date, end_date):
# Same date visits for prescriptions within `num_months` of `from_date`
num_pres = medications \
.where((medications.date >= from_date + days((num_months-1)*30)) &
(medications.date < (from_date + days(num_months*30))) &
(medications.date <= end_date)) \
.where((medications.dmd_code.is_in(total_drugs_dmd))) \
.where((medications.date >= from_date) &
(medications.date <= end_date)) \
.date.count_distinct_for_patient()
setattr(dataset, f"all_drug_visits_{num_months}", num_pres)
setattr(dataset, "total_drug_visit", num_pres)

# Temp: test generate data
dataset = Dataset()
dataset.define_population(age >= 18)
# month1 = lc_dx.date + days(0*30)
# month2 = lc_dx.date + days(1*30)
# month3 = lc_dx.date + days(2*30)
# month4 = lc_dx.date + days(3*30)
# month5 = lc_dx.date + days(4*30)
# month6 = lc_dx.date + days(5*30)
# month7 = lc_dx.date + days(6*30)
# month8 = lc_dx.date + days(7*30)
# month9 = lc_dx.date + days(8*30)
# month10 = lc_dx.date + days(9*30)
# month11 = lc_dx.date + days(10*30)
# month12 = lc_dx.date + days(11*30)

# hx_outpatient_visit(dataset, lc_dx.date, num_months=2)

# total_drug_visit(dataset, from_date = lc_dx.date, end_date=study_end_date, num_months=6)
# total_drug_visit(dataset, from_date = lc_dx.date, end_date=study_end_date)
# drug_12ent_number(dataset, lc_dx.date, num_months=2)
# add_visits(dataset, lc_dx.date, num_months=1)
# add_visits(dataset, lc_dx.date, num_months=2)
# add_hos_visits(dataset, lc_dx.date, num_months=3)
# add_hos_visits(dataset, lc_dx.date, num_months=4)
# add_ae_visits(dataset, lc_dx.date, num_months=5)
# add_ae_visits(dataset, lc_dx.date, num_months=6)
# add_visits(dataset, lc_dx.date, num_months=1,end_date=study_end_date)
# add_visits(dataset, lc_dx.date, num_months=2,end_date=study_end_date)
# add_hos_visits(dataset, lc_dx.date, num_months=3,end_date=study_end_date)
# add_hos_visits(dataset, lc_dx.date, num_months=4, end_date=study_end_date)

# add_ae_visits(dataset, lc_dx.date, num_months=6, end_date=study_end_date)

# outpatient_visit(dataset, from_date=lc_dx.date, num_months=1, end_date=study_end_date)
# outpatient_lc_dx_visit(dataset, from_date=lc_dx.date, num_months=4, end_date=study_end_date)
Expand Down

0 comments on commit 9e3bb6c

Please sign in to comment.