generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from databricks-industry-solutions/feature-simp…
…lify-pandas-and-dlt Feature simplify pandas and dlt
- Loading branch information
Showing
6 changed files
with
12,039 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,34 @@ | ||
# Databricks notebook source | ||
import pyspark.pandas as ps | ||
import pyspark.sql.utils | ||
import pandas as pd | ||
import re | ||
import dlt | ||
import os | ||
from pyspark.sql.functions import * | ||
ps.set_option('compute.ops_on_diff_frames', True) | ||
# | ||
# load handwritten notes from csv | ||
# | ||
def load_data(path="/data/12k_handwritten_clinical_notes.csv"): | ||
return (spark.read.format("csv") | ||
.option("header",True) | ||
.load("file:///" + os.getcwd() + path) | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
@dlt.table | ||
def load_data(): | ||
data = pd.read_csv( | ||
"//Volumes/ang_nara_catalog/rad_llm/clinical_data/12k_handwritten_clinical_notes.csv" | ||
) | ||
data = data.drop(['Unnamed: 0'], axis=1) | ||
return spark.createDataFrame(data) | ||
|
||
|
||
# COMMAND ---------- | ||
|
||
@dlt.table | ||
def remove_label_counts_less_than_50(): | ||
df = dlt.read('load_data') | ||
df.write.format("delta").mode("overwrite").option("overwriteSchema",True).saveAsTable("ang_nara_catalog.rad_llm.delta_rad") | ||
df = spark.sql(""" | ||
SELECT t.input, t.radiology_labels | ||
# | ||
# Only use where there exists 50 or more labels | ||
# | ||
def filtered_table(): | ||
df = load_data() | ||
df.createOrReplaceTempView("radiology_data") | ||
return spark.sql(""" | ||
SELECT t.input, t.radiology_labels | ||
FROM ( | ||
SELECT t.*, COUNT(*) OVER (PARTITION BY radiology_labels) AS cnt | ||
FROM ang_nara_catalog.rad_llm.delta_rad t | ||
FROM radiology_data t | ||
) t | ||
WHERE cnt > 50 | ||
""") | ||
return df | ||
""").withColumn("instruction", lit('predict radiology label for the clinical notes')) | ||
|
||
# COMMAND ---------- | ||
|
||
@dlt.table | ||
def filtered_table(): | ||
df = dlt.read('remove_label_counts_less_than_50') | ||
df = df.withColumn("instruction", lit('predict radiology label for the clinical notes')) | ||
df.write.format("delta").mode("overwrite").option("overwriteSchema",True).saveAsTable("ang_nara_catalog.rad_llm.delta_rad_filtered") | ||
return df | ||
data = filtered_table() | ||
#TODO save to a volume | ||
data.show() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.