Update app.py
Browse files
app.py
CHANGED
@@ -35,44 +35,55 @@ def create_bmd_and_tscore_prediction_table(df_bmd_data, drug_selected, bmd_patie
|
|
35 |
years = ['1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
|
36 |
predictions = []
|
37 |
|
38 |
-
for
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
-
return pd.DataFrame(predictions, columns=['Year', 'Predicted BMD', 'Predicted T-score'])
|
46 |
|
47 |
# Step 5: Plot BMD and T-score as separate graphs side by side
|
48 |
def plot_bmd_and_tscore_separate(prediction_table, baseline_bmd, baseline_tscore):
|
49 |
-
years = ['0'] + list(prediction_table['Year'])
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
layout_bmd = go.Layout(
|
61 |
title="Predicted BMD over Time",
|
62 |
xaxis=dict(title='Years'),
|
63 |
-
yaxis=dict(title='BMD (g/cm²)', showgrid=False)
|
|
|
64 |
)
|
65 |
|
66 |
-
#
|
67 |
layout_tscore = go.Layout(
|
68 |
title="Predicted T-score over Time",
|
69 |
xaxis=dict(title='Years'),
|
70 |
-
|
|
|
71 |
)
|
72 |
|
73 |
-
# Create figures
|
74 |
-
fig_bmd = go.Figure(data=
|
75 |
-
fig_tscore = go.Figure(data=
|
76 |
|
77 |
# Use Streamlit columns to place two plots side by side
|
78 |
col1, col2 = st.columns(2)
|
@@ -109,8 +120,8 @@ def main_with_plot_tscore_labels(file_path, bmd_patient, tscore_patient, C_avg_l
|
|
109 |
for i, row in prediction_table.iterrows():
|
110 |
st.write(f"Year {row['Year']}: BMD = {row['Predicted BMD']:.3f}, T-score = {row['Predicted T-score']:.2f}")
|
111 |
|
112 |
-
# Check if the goal of BMD >= -2.
|
113 |
-
if row['Predicted T-score'] >= -2.
|
114 |
st.success(f"Goal achieved at year {row['Year']}")
|
115 |
break
|
116 |
|
@@ -122,12 +133,16 @@ def main():
|
|
122 |
bmd_patient = st.number_input("Initial BMD", min_value=0.0, max_value=2.0, value=0.635, step=0.001)
|
123 |
tscore_patient = st.number_input("Initial T-score", min_value=-5.0, max_value=2.0, value=-2.5, step=0.01)
|
124 |
|
125 |
-
#
|
126 |
drug_options = ['Teriparatide', 'Teriparatide + Denosumab', 'Denosumab', 'Denosumab + Teriparatide',
|
127 |
'Romosozumab', 'Romosozumab + Denosumab', 'Romosozumab + Alendronate',
|
128 |
'Romosozumab + Ibandronate', 'Romosozumab + Zoledronate', 'Alendronate',
|
129 |
'Risedronate', 'Ibandronate oral', 'Ibandronate IV (3mg)', 'Zoledronate']
|
130 |
-
|
|
|
|
|
|
|
|
|
131 |
|
132 |
# Set C_avg and C_sd for Lunar device (example values)
|
133 |
C_avg_lunar = 0.95 # Example: Average BMD for Total Hip from Excel (Lunar)
|
@@ -136,9 +151,13 @@ def main():
|
|
136 |
# Example file path
|
137 |
file_path = "BMD constant calculator.xlsx"
|
138 |
|
139 |
-
#
|
140 |
-
if
|
141 |
-
|
|
|
|
|
|
|
|
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
main()
|
|
|
35 |
years = ['1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
|
36 |
predictions = []
|
37 |
|
38 |
+
for drug in drug_selected:
|
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((drug, year, bmd_new, tscore_new))
|
45 |
|
46 |
+
return pd.DataFrame(predictions, columns=['Drug', 'Year', 'Predicted BMD', 'Predicted T-score'])
|
47 |
|
48 |
# Step 5: Plot BMD and T-score as separate graphs side by side
|
49 |
def plot_bmd_and_tscore_separate(prediction_table, baseline_bmd, baseline_tscore):
|
50 |
+
years = ['0'] + list(prediction_table['Year'].unique())
|
51 |
+
|
52 |
+
# Plot for BMD
|
53 |
+
traces_bmd = []
|
54 |
+
for drug in prediction_table['Drug'].unique():
|
55 |
+
df_drug = prediction_table[prediction_table['Drug'] == drug]
|
56 |
+
bmd_values = [baseline_bmd] + list(df_drug['Predicted BMD'])
|
57 |
+
trace_bmd = go.Scatter(x=years, y=bmd_values, mode='lines+markers', name=drug + ' (BMD)')
|
58 |
+
traces_bmd.append(trace_bmd)
|
59 |
+
|
60 |
+
# Plot for T-score
|
61 |
+
traces_tscore = []
|
62 |
+
for drug in prediction_table['Drug'].unique():
|
63 |
+
df_drug = prediction_table[prediction_table['Drug'] == drug]
|
64 |
+
tscore_values = [baseline_tscore] + list(df_drug['Predicted T-score'])
|
65 |
+
trace_tscore = go.Scatter(x=years, y=tscore_values, mode='lines+markers', name=drug + ' (T-score)', yaxis='y2')
|
66 |
+
traces_tscore.append(trace_tscore)
|
67 |
+
|
68 |
+
# Create BMD layout
|
69 |
layout_bmd = go.Layout(
|
70 |
title="Predicted BMD over Time",
|
71 |
xaxis=dict(title='Years'),
|
72 |
+
yaxis=dict(title='BMD (g/cm²)', showgrid=False),
|
73 |
+
legend=dict(x=0.1, y=1.1, orientation='h')
|
74 |
)
|
75 |
|
76 |
+
# Create T-score layout
|
77 |
layout_tscore = go.Layout(
|
78 |
title="Predicted T-score over Time",
|
79 |
xaxis=dict(title='Years'),
|
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)
|
|
|
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 |
|
|
|
133 |
bmd_patient = st.number_input("Initial BMD", min_value=0.0, max_value=2.0, value=0.635, step=0.001)
|
134 |
tscore_patient = st.number_input("Initial T-score", min_value=-5.0, max_value=2.0, value=-2.5, step=0.01)
|
135 |
|
136 |
+
# Drug options
|
137 |
drug_options = ['Teriparatide', 'Teriparatide + Denosumab', 'Denosumab', 'Denosumab + Teriparatide',
|
138 |
'Romosozumab', 'Romosozumab + Denosumab', 'Romosozumab + Alendronate',
|
139 |
'Romosozumab + Ibandronate', 'Romosozumab + Zoledronate', 'Alendronate',
|
140 |
'Risedronate', 'Ibandronate oral', 'Ibandronate IV (3mg)', 'Zoledronate']
|
141 |
+
|
142 |
+
# Add option to select all medications
|
143 |
+
selected_drugs = st.multiselect("Select drugs to compare", drug_options, default=None)
|
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)
|
|
|
151 |
# Example file path
|
152 |
file_path = "BMD constant calculator.xlsx"
|
153 |
|
154 |
+
# ตรวจสอบว่ามีการเลือกยาแล้วหรือไม่
|
155 |
+
if len(selected_drugs) == 0:
|
156 |
+
st.warning("Please select at least one drug to compare.")
|
157 |
+
else:
|
158 |
+
# Run prediction and plot results
|
159 |
+
if st.button("Predict"):
|
160 |
+
main_with_plot_tscore_labels(file_path, bmd_patient, tscore_patient, C_avg_lunar, C_sd_lunar, selected_drugs)
|
161 |
|
162 |
if __name__ == "__main__":
|
163 |
main()
|