-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_Setup Data.py
259 lines (180 loc) · 8.35 KB
/
01_Setup Data.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
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
# Databricks notebook source
# MAGIC %md
# MAGIC #Prepare Data
# MAGIC ###### Let us start by creating some synthetic data to work with.
# MAGIC <img src="./resources/build_1.png" alt="Prepare Data" width="900" style="border:2px;"/>
# MAGIC
# MAGIC
# MAGIC
# MAGIC This notebook will create CATALOG and SCHEMA if it does not exist and create the below data tables.
# MAGIC
# MAGIC **member_enrolment**: Table containing member enrolment information like client and plan_id
# MAGIC
# MAGIC **member_accumulators**: Table containing member accumulators like deductibles and out of pocket spent
# MAGIC
# MAGIC **cpt_codes**: Table containing CPT codes and descriptions
# MAGIC
# MAGIC **procedure_cost**: Table containing negotiated cost of each procedure.
# MAGIC
# MAGIC In addition to these tables, this notebook creates a Unity Catalog Volume and store the Summary of Benefit PDF files and CPT Code CSV files in appropriate folders
# MAGIC
# MAGIC We are using synthetic data as example. In reality robust Data Ingestion Pipelines will be used to manage this data in a Lakehouse.
# MAGIC
# MAGIC #####Read More:
# MAGIC * [Databricks Volumes](https://docs.databricks.com/en/sql/language-manual/sql-ref-volumes.html)
# MAGIC * [Ingest Data into Databricks Lakehouse](https://docs.databricks.com/en/ingestion/index.html)
# MAGIC * [Data Pipelines in Databricks](https://docs.databricks.com/en/getting-started/data-pipeline-get-started.html)
# MAGIC
# COMMAND ----------
# MAGIC %run ./utils/init
# COMMAND ----------
# MAGIC %md
# MAGIC ####Create Catalog and Schema
# COMMAND ----------
spark.sql(f"CREATE CATALOG IF NOT EXISTS {catalog}")
spark.sql(f"CREATE SCHEMA IF NOT EXISTS {catalog}.{schema}")
spark.sql(f"CREATE VOLUME IF NOT EXISTS {catalog}.{schema}.{sbc_folder}")
spark.sql(f"CREATE VOLUME IF NOT EXISTS {catalog}.{schema}.{cpt_folder}")
# COMMAND ----------
# MAGIC %md
# MAGIC #### Copy Files to Volume
# MAGIC
# COMMAND ----------
#Let us first copy the SBC files
for sbc_file in sbc_files:
dbutils.fs.cp(f"file:/Workspace/{project_root_path}/resources/{sbc_file}",sbc_folder_path,True)
# COMMAND ----------
#Now lets copy the cpt codes file
#Downloaded from https://www.cdc.gov/nhsn/xls/cpt-pcm-nhsn.xlsx
dbutils.fs.cp(f"file:/Workspace/{project_root_path}/resources/{cpt_file}",cpt_folder_path,True)
# COMMAND ----------
# MAGIC %md
# MAGIC #### Create Data Tables
# MAGIC - Member Enrolment Table: Contains member details including the client id
# MAGIC - Member Accumulator Table: Contain member year to date deductible accumulator
# MAGIC - Procedure Cost Table: Contain estimated cost of all the covered procedures
# MAGIC
# MAGIC
# COMMAND ----------
import pandas as pd
from pyspark.sql.types import StructType, StructField, StringType, DateType, DoubleType, IntegerType, LongType
import datetime
# COMMAND ----------
# MAGIC %md
# MAGIC #####`member_enrolment`
# COMMAND ----------
member_table_schema = StructType([
StructField("member_id",StringType(), nullable=False),
StructField("client_id",StringType(), nullable=False),
StructField("plan_id",StringType(), nullable=False),
StructField("plan_start_date",DateType(), nullable=False),
StructField("plan_end_date",DateType(), nullable=False),
StructField("active_ind",StringType(), nullable=False),
])
member_data = [
("1234",client_names[0],"P1", datetime.date(2024,1,1), datetime.date(2024,12,31),"Y" ),
("2345",client_names[0],"P1", datetime.date(2024,1,1), datetime.date(2024,12,31),"Y" ),
("7890",client_names[1],"P2", datetime.date(2024,1,1), datetime.date(2024,12,31),"Y" ),
]
member = spark.createDataFrame(member_data, schema=member_table_schema)
spark.sql(f"DROP TABLE IF EXISTS {catalog}.{schema}.{member_table_name}")
spark.catalog.createTable(f"{catalog}.{schema}.{member_table_name}", schema=member_table_schema)
member.write.mode("append").saveAsTable(f"{catalog}.{schema}.{member_table_name}")
spark.sql(f"ALTER TABLE {catalog}.{schema}.{member_table_name} ADD CONSTRAINT {member_table_name}_pk PRIMARY KEY( member_id )")
# COMMAND ----------
# MAGIC %md
# MAGIC ###### Inspect and Verify `Data`
# COMMAND ----------
display(spark.table(f"{catalog}.{schema}.{member_table_name}"))
# COMMAND ----------
# MAGIC %md
# MAGIC #####`member_accumulators`
# COMMAND ----------
member_accumulators_schema = StructType([
StructField("member_id",StringType(), nullable=False),
StructField("oop_max",DoubleType(), nullable=False),
StructField("fam_deductible",DoubleType(), nullable=False),
StructField("mem_deductible",DoubleType(), nullable=False),
StructField("oop_agg",DoubleType(), nullable=False),
StructField("mem_ded_agg",DoubleType(), nullable=False),
StructField("fam_ded_agg",DoubleType(), nullable=False),
])
member_accumulators_data = [
('1234', 2500.00, 1500.00, 1000.00, 500.00, 500.00, 750.00),
('2345', 2500.00, 1500.00, 1000.00, 250.00, 250.00, 750.00),
('7890', 3000.00, 2500.00, 2000.00, 3000.00, 2000.00, 2000.00),
]
member_accumulators = spark.createDataFrame(member_accumulators_data, schema=member_accumulators_schema)
spark.sql(f"DROP TABLE IF EXISTS {catalog}.{schema}.{member_accumulators_table_name}")
spark.catalog.createTable(f"{catalog}.{schema}.{member_accumulators_table_name}", schema=member_accumulators_schema)
member_accumulators.write.mode("append").saveAsTable(f"{catalog}.{schema}.{member_accumulators_table_name}")
spark.sql(f"ALTER TABLE {catalog}.{schema}.{member_accumulators_table_name} ADD CONSTRAINT {member_accumulators_table_name}_pk PRIMARY KEY( member_id)")
# COMMAND ----------
# MAGIC %md
# MAGIC ###### Inspect and Verify Data
# COMMAND ----------
display(spark.table(f"{catalog}.{schema}.{member_accumulators_table_name}"))
# COMMAND ----------
# MAGIC %md
# MAGIC #####`cpt_codes`
# MAGIC
# COMMAND ----------
from pyspark.sql.functions import monotonically_increasing_id
cpt_codes_file = f"{cpt_folder_path}/{cpt_file}"
cpt_codes_file_schema = (StructType()
.add("code",StringType(),True)
.add("description",StringType(),True)
)
cpt_codes_table_schema = (StructType()
.add("id",LongType(),False)
.add("code",StringType(),True)
.add("description",StringType(),True)
)
cpt_df = (spark
.read
.option("header", "false")
.option("delimiter", "\t")
.schema(cpt_codes_file_schema)
.csv(cpt_codes_file)
.repartition(1)
.withColumn("id",monotonically_increasing_id())
.select("id","code","description")
)
spark.sql(f"DROP TABLE IF EXISTS {catalog}.{schema}.{cpt_code_table_name}")
spark.catalog.createTable(f"{catalog}.{schema}.{cpt_code_table_name}", schema=cpt_codes_table_schema)
cpt_df.write.mode("append").saveAsTable(f"{catalog}.{schema}.{cpt_code_table_name}")
spark.sql(f"ALTER TABLE {catalog}.{schema}.{cpt_code_table_name} ADD CONSTRAINT {cpt_code_table_name}_pk PRIMARY KEY( id )")
# COMMAND ----------
# MAGIC %md
# MAGIC ###### Inspect and Verify Data
# COMMAND ----------
display(cpt_df)
# COMMAND ----------
# MAGIC %md
# MAGIC #####`procedure_cost`
# MAGIC Table containing negotiated cost of each procedure.
# MAGIC For simiplicity we will assign a random cost to each procedure
# COMMAND ----------
from pyspark.sql.functions import rand,round, pow, ceil,col
procedure_cost_schema = StructType([
StructField("procedure_code",StringType(), nullable=False),
StructField("cost",DoubleType(), nullable=False)
])
spark.sql(f"DROP TABLE IF EXISTS {catalog}.{schema}.{procedure_cost_table_name}")
spark.catalog.createTable(f"{catalog}.{schema}.{procedure_cost_table_name}", schema=procedure_cost_schema)
#Read the procedure codes and assign some cost to it
#In a production scenario it could be a complex procedure to calculate the expected cost
procedure_cost = (
spark
.table(f"{catalog}.{schema}.{cpt_code_table_name}")
.withColumn("pow", ceil(rand(seed=1234) * 10) % 3 + 2 )
.withColumn("cost", round(rand(seed=2345) * pow(10, "pow") + 20 ,2) )
.select(col("code").alias("procedure_code"),"cost")
)
procedure_cost.write.mode("append").saveAsTable(f"{catalog}.{schema}.{procedure_cost_table_name}")
spark.sql(f"ALTER TABLE {catalog}.{schema}.{procedure_cost_table_name} ADD CONSTRAINT {procedure_cost_table_name}_pk PRIMARY KEY( procedure_code )")
# COMMAND ----------
# MAGIC %md
# MAGIC ###### Inspect and Verify Data
# COMMAND ----------
display(spark.table(f"{catalog}.{schema}.{procedure_cost_table_name}"))