-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_outpatients.py
153 lines (128 loc) · 3.91 KB
/
generate_outpatients.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Databricks notebook source
# MAGIC %md
# MAGIC
# MAGIC # Generate nhp outpatients
# MAGIC
# COMMAND ----------
from itertools import chain
from databricks.connect import DatabricksSession
from pyspark.sql import functions as F
from pyspark.sql.types import * # pylint: disable-all
from nhp_datasets.icbs import icb_mapping, main_icbs
from nhp_datasets.providers import get_provider_successors_mapping, providers
spark = DatabricksSession.builder.getOrCreate()
provider_successors_mapping = get_provider_successors_mapping()
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Has Procedure
# COMMAND ----------
df = spark.read.table("hes.silver.opa")
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Calculate provider column
# COMMAND ----------
df = df.withColumn(
"provider",
F.when(F.col("sitetret") == "RW602", "R0A")
.when(F.col("sitetret") == "RM318", "R0A")
.otherwise(provider_successors_mapping[F.col("procode3")]),
).filter(F.col("provider").isin(providers))
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Calculate icb column
# COMMAND ----------
df = df.withColumn("icb", icb_mapping[F.col("ccg_residence")])
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ## Create OPA Data
# MAGIC
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC ### Extract data
# MAGIC
# COMMAND ----------
hes_opa_processed = (
df.filter(F.col("sex").isin(["1", "2"]))
.filter(F.col("atentype").isin(["1", "2", "21", "22"]))
.join(main_icbs, "provider", "left")
.withColumn(
"age",
F.when(F.col("apptage") >= 7000, 0)
.when(F.col("apptage") > 90, 90)
.otherwise(F.col("apptage")),
)
.filter(F.col("age") <= 120)
.withColumn(
"is_main_icb", F.when(F.col("icb") == F.col("main_icb"), True).otherwise(False)
)
.drop("main_icb")
.withColumn("is_surgical_specialty", F.col("tretspef").rlike("^1(?!=(80|9[012]))"))
.withColumn("is_adult", (F.col("apptage") >= 18) & (F.col("apptage") <= 7000))
.withColumn(
"is_gp_ref", (F.col("refsourc") == "03") & F.col("firstatt").isin(["1", "3"])
)
.withColumn(
"is_cons_cons_ref",
(F.col("refsourc") == "05")
& F.col("firstatt").isin(["1", "3"])
& F.col("sushrg").rlike("^WF0[12]B$"),
)
.withColumn("is_first", F.col("atentype").isin(["1", "21"]))
.withColumn("is_tele_appointment", F.col("atentype").isin(["21", "22"]).cast("int"))
.withColumn("is_face_to_face_appointment", 1 - F.col("is_tele_appointment"))
.withColumn(
"has_procedures",
~F.col("sushrg").rlike("^(WF|U)") & (F.col("is_face_to_face_appointment") == 1),
)
.groupBy(
F.col("fyear"),
F.col("provider"),
F.col("age"),
F.col("sex").cast("int"),
F.col("tretspef"),
F.col("sitetret"),
F.col("has_procedures"),
F.col("is_main_icb"),
F.col("is_surgical_specialty"),
F.col("is_adult"),
F.col("is_gp_ref"),
F.col("is_cons_cons_ref"),
F.col("is_first"),
)
.agg(
F.sum("is_face_to_face_appointment").alias("attendances"),
F.sum("is_tele_appointment").alias("tele_attendances"),
)
.withColumn(
"type",
F.concat(
F.when(F.col("is_adult"), "adult").otherwise("child"),
F.lit("_"),
F.when(F.col("is_surgical_specialty"), "surgical").otherwise(
"non-surgical"
),
),
)
.withColumn(
"group",
F.when(F.col("has_procedures"), "procedure")
.when(F.col("is_first"), "first")
.otherwise("followup"),
)
.withColumn(
"hsagrp", F.concat(F.lit("op_"), F.col("type"), F.lit("_"), F.col("group"))
)
.repartition("fyear", "provider")
)
# COMMAND ----------
(
hes_opa_processed.withColumn("index", F.expr("uuid()"))
.write.partitionBy("fyear", "provider")
.mode("overwrite")
.saveAsTable("su_data.nhp.opa")
)