Update app.py
Browse files
app.py
CHANGED
@@ -30,100 +30,76 @@ def adjust_constants(bmd_patient, tscore_patient, c_avg, c_sd):
|
|
30 |
def calculate_bmd_increase(baseline_bmd, percentage_increase):
|
31 |
return baseline_bmd * (1 + percentage_increase)
|
32 |
|
33 |
-
# Step 4: Create a table showing BMD prediction and T-score conversion for each year
|
34 |
-
def
|
35 |
years = ['1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
|
36 |
-
|
37 |
|
38 |
-
|
|
|
|
|
39 |
for year in years:
|
40 |
if not pd.isna(df_bmd_data.loc[df_bmd_data['Drug'] == drug, year].values[0]):
|
41 |
percent_increase = df_bmd_data.loc[df_bmd_data['Drug'] == drug, year].values[0]
|
42 |
bmd_new = calculate_bmd_increase(bmd_patient, percent_increase) # Use baseline BMD
|
43 |
tscore_new = calculate_tscore_from_bmd(bmd_new, c_avg_new, c_sd_new) # Calculate predicted T-score
|
44 |
-
predictions.append((
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
tscore_values = [baseline_tscore] + list(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
yaxis2=dict(title='T-score', overlaying='y', side='right', showgrid=False),
|
81 |
-
legend=dict(x=0.1, y=1.1, orientation='h')
|
82 |
-
)
|
83 |
-
|
84 |
-
# Create BMD and T-score figures
|
85 |
-
fig_bmd = go.Figure(data=traces_bmd, layout=layout_bmd)
|
86 |
-
fig_tscore = go.Figure(data=traces_tscore, layout=layout_tscore)
|
87 |
-
|
88 |
-
# Use Streamlit columns to place two plots side by side
|
89 |
-
col1, col2 = st.columns(2)
|
90 |
-
with col1:
|
91 |
-
st.plotly_chart(fig_bmd)
|
92 |
-
with col2:
|
93 |
-
st.plotly_chart(fig_tscore)
|
94 |
|
95 |
# Step 6: Calculate T-score from adjusted BMD
|
96 |
def calculate_tscore_from_bmd(bmd_patient, c_avg, c_sd):
|
97 |
return (bmd_patient - c_avg) / c_sd
|
98 |
|
99 |
# Main function to load data, run the application, and plot results with T-score labels
|
100 |
-
def
|
101 |
# Step 1: Load and clean BMD data from the Excel sheet
|
102 |
df_bmd_data = load_clean_bmd_data(file_path)
|
103 |
|
104 |
# Step 2: Adjust constants based on the patient's data
|
105 |
c_avg_new, c_sd_new = adjust_constants(bmd_patient, tscore_patient, C_avg_lunar, C_sd_lunar)
|
106 |
|
107 |
-
# Step 4: Create
|
108 |
-
|
109 |
|
110 |
-
# Display baseline BMD and T-score
|
111 |
st.write(f"Baseline: BMD = {bmd_patient:.3f}, T-score = {tscore_patient:.2f}")
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
# Step 5: Plot the BMD and T-score graphs side by side
|
117 |
-
plot_bmd_and_tscore_separate(prediction_table, bmd_patient, tscore_patient)
|
118 |
-
|
119 |
-
# Step 6: Check if goal is achieved and show the predicted BMD and T-score for each year
|
120 |
-
for i, row in prediction_table.iterrows():
|
121 |
-
st.write(f"Year {row['Year']}: BMD = {row['Predicted BMD']:.3f}, T-score = {row['Predicted T-score']:.2f}")
|
122 |
-
|
123 |
-
# Check if the goal of BMD >= -2.4 is achieved
|
124 |
-
if row['Predicted T-score'] >= -2.4:
|
125 |
-
st.success(f"Goal achieved at year {row['Year']}")
|
126 |
-
break
|
127 |
|
128 |
# Streamlit UI
|
129 |
def main():
|
@@ -139,10 +115,8 @@ def main():
|
|
139 |
'Romosozumab + Ibandronate', 'Romosozumab + Zoledronate', 'Alendronate',
|
140 |
'Risedronate', 'Ibandronate oral', 'Ibandronate IV (3mg)', 'Zoledronate']
|
141 |
|
142 |
-
# Add option to select
|
143 |
-
selected_drugs = st.multiselect("Select drugs to compare", drug_options
|
144 |
-
if "All Medications" in selected_drugs:
|
145 |
-
selected_drugs = drug_options # If "All Medications" is selected, include all drugs
|
146 |
|
147 |
# Set C_avg and C_sd for Lunar device (example values)
|
148 |
C_avg_lunar = 0.95 # Example: Average BMD for Total Hip from Excel (Lunar)
|
@@ -157,7 +131,7 @@ def main():
|
|
157 |
else:
|
158 |
# Run prediction and plot results
|
159 |
if st.button("Predict"):
|
160 |
-
|
161 |
|
162 |
if __name__ == "__main__":
|
163 |
-
main()
|
|
|
30 |
def calculate_bmd_increase(baseline_bmd, percentage_increase):
|
31 |
return baseline_bmd * (1 + percentage_increase)
|
32 |
|
33 |
+
# Step 4: Create a table showing BMD prediction and T-score conversion for each year for each drug
|
34 |
+
def create_bmd_and_tscore_prediction_tables(df_bmd_data, selected_drugs, bmd_patient, c_avg_new, c_sd_new):
|
35 |
years = ['1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
|
36 |
+
drug_tables = {}
|
37 |
|
38 |
+
# Loop through each selected drug and generate the prediction table
|
39 |
+
for drug in selected_drugs:
|
40 |
+
predictions = []
|
41 |
for year in years:
|
42 |
if not pd.isna(df_bmd_data.loc[df_bmd_data['Drug'] == drug, year].values[0]):
|
43 |
percent_increase = df_bmd_data.loc[df_bmd_data['Drug'] == drug, year].values[0]
|
44 |
bmd_new = calculate_bmd_increase(bmd_patient, percent_increase) # Use baseline BMD
|
45 |
tscore_new = calculate_tscore_from_bmd(bmd_new, c_avg_new, c_sd_new) # Calculate predicted T-score
|
46 |
+
predictions.append((year, bmd_new, tscore_new))
|
47 |
+
|
48 |
+
# Create DataFrame for each drug
|
49 |
+
drug_table = pd.DataFrame(predictions, columns=['Year', 'Predicted BMD', 'Predicted T-score'])
|
50 |
+
drug_tables[drug] = drug_table
|
51 |
+
|
52 |
+
return drug_tables
|
53 |
+
|
54 |
+
# Step 5: Plot BMD and T-score as separate graphs side by side for each drug
|
55 |
+
def display_prediction_tables_and_plots(prediction_tables, baseline_bmd, baseline_tscore):
|
56 |
+
# Loop through each drug's prediction table
|
57 |
+
for drug, table in prediction_tables.items():
|
58 |
+
st.write(f"### {drug} Results")
|
59 |
+
|
60 |
+
# Display the prediction table for the current drug
|
61 |
+
st.dataframe(table)
|
62 |
+
|
63 |
+
# Create and display separate plots for each drug
|
64 |
+
years = ['0'] + list(table['Year'])
|
65 |
+
bmd_values = [baseline_bmd] + list(table['Predicted BMD'])
|
66 |
+
tscore_values = [baseline_tscore] + list(table['Predicted T-score'])
|
67 |
+
|
68 |
+
# Create BMD plot
|
69 |
+
trace_bmd = go.Scatter(x=years, y=bmd_values, mode='lines+markers', name=f'{drug} (BMD)')
|
70 |
+
fig_bmd = go.Figure(data=[trace_bmd], layout=go.Layout(title=f'{drug} - Predicted BMD over Time', xaxis=dict(title='Years'), yaxis=dict(title='BMD (g/cm²)')))
|
71 |
+
|
72 |
+
# Create T-score plot
|
73 |
+
trace_tscore = go.Scatter(x=years, y=tscore_values, mode='lines+markers', name=f'{drug} (T-score)', yaxis='y2')
|
74 |
+
fig_tscore = go.Figure(data=[trace_tscore], layout=go.Layout(title=f'{drug} - Predicted T-score over Time', xaxis=dict(title='Years'), yaxis2=dict(title='T-score', overlaying='y', side='right')))
|
75 |
+
|
76 |
+
# Display the plots
|
77 |
+
col1, col2 = st.columns(2)
|
78 |
+
with col1:
|
79 |
+
st.plotly_chart(fig_bmd)
|
80 |
+
with col2:
|
81 |
+
st.plotly_chart(fig_tscore)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# Step 6: Calculate T-score from adjusted BMD
|
84 |
def calculate_tscore_from_bmd(bmd_patient, c_avg, c_sd):
|
85 |
return (bmd_patient - c_avg) / c_sd
|
86 |
|
87 |
# Main function to load data, run the application, and plot results with T-score labels
|
88 |
+
def main_with_separate_tables(file_path, bmd_patient, tscore_patient, C_avg_lunar, C_sd_lunar, selected_drugs):
|
89 |
# Step 1: Load and clean BMD data from the Excel sheet
|
90 |
df_bmd_data = load_clean_bmd_data(file_path)
|
91 |
|
92 |
# Step 2: Adjust constants based on the patient's data
|
93 |
c_avg_new, c_sd_new = adjust_constants(bmd_patient, tscore_patient, C_avg_lunar, C_sd_lunar)
|
94 |
|
95 |
+
# Step 4: Create separate prediction tables for each selected drug
|
96 |
+
prediction_tables = create_bmd_and_tscore_prediction_tables(df_bmd_data, selected_drugs, bmd_patient, c_avg_new, c_sd_new)
|
97 |
|
98 |
+
# Display baseline BMD and T-score
|
99 |
st.write(f"Baseline: BMD = {bmd_patient:.3f}, T-score = {tscore_patient:.2f}")
|
100 |
|
101 |
+
# Step 5: Display prediction tables and plots for each drug
|
102 |
+
display_prediction_tables_and_plots(prediction_tables, bmd_patient, tscore_patient)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# Streamlit UI
|
105 |
def main():
|
|
|
115 |
'Romosozumab + Ibandronate', 'Romosozumab + Zoledronate', 'Alendronate',
|
116 |
'Risedronate', 'Ibandronate oral', 'Ibandronate IV (3mg)', 'Zoledronate']
|
117 |
|
118 |
+
# Add option to select multiple drugs
|
119 |
+
selected_drugs = st.multiselect("Select drugs to compare", drug_options)
|
|
|
|
|
120 |
|
121 |
# Set C_avg and C_sd for Lunar device (example values)
|
122 |
C_avg_lunar = 0.95 # Example: Average BMD for Total Hip from Excel (Lunar)
|
|
|
131 |
else:
|
132 |
# Run prediction and plot results
|
133 |
if st.button("Predict"):
|
134 |
+
main_with_separate_tables(file_path, bmd_patient, tscore_patient, C_avg_lunar, C_sd_lunar, selected_drugs)
|
135 |
|
136 |
if __name__ == "__main__":
|
137 |
+
main()
|