Update appbackup.py
Browse files- appbackup.py +17 -6
appbackup.py
CHANGED
@@ -32,8 +32,14 @@ def generate_predictions(medication_data, site, bmd, mu, sigma):
|
|
32 |
|
33 |
for _, row in site_data.iterrows():
|
34 |
drug = row['Medication']
|
35 |
-
predictions = {
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
37 |
for year in row.index[1:-1]: # Skip 'Medication' and 'Site' columns
|
38 |
if not pd.isna(row[year]):
|
39 |
percentage_increase = row[year]
|
@@ -41,12 +47,15 @@ def generate_predictions(medication_data, site, bmd, mu, sigma):
|
|
41 |
predicted_tscore = calculate_tscore(predicted_bmd, mu, sigma)
|
42 |
|
43 |
predictions['Year'].append(year.replace(" Year", "")) # Simplify year label
|
|
|
44 |
predictions['Predicted BMD'].append(round(predicted_bmd, 3))
|
45 |
predictions['Predicted T-score'].append(round(predicted_tscore, 1))
|
|
|
46 |
|
47 |
all_results.append({'Drug': drug, 'Predictions': predictions})
|
48 |
return all_results
|
49 |
|
|
|
50 |
# Display results as table and plots
|
51 |
def display_results(predictions, site):
|
52 |
st.subheader(f"Predictions for {site}")
|
@@ -59,13 +68,13 @@ def display_results(predictions, site):
|
|
59 |
st.write(f"### {drug}")
|
60 |
st.dataframe(pd.DataFrame(predictions))
|
61 |
|
62 |
-
# Plot BMD and T-score
|
63 |
bmd_plot = go.Scatter(
|
64 |
-
x=predictions['Year'], y=predictions['Predicted BMD'], mode='lines+markers',
|
65 |
name='Predicted BMD', line=dict(color='blue')
|
66 |
)
|
67 |
tscore_plot = go.Scatter(
|
68 |
-
x=predictions['Year'], y=predictions['Predicted T-score'], mode='lines+markers',
|
69 |
name='Predicted T-score', line=dict(color='green')
|
70 |
)
|
71 |
|
@@ -73,11 +82,13 @@ def display_results(predictions, site):
|
|
73 |
col1, col2 = st.columns(2)
|
74 |
with col1:
|
75 |
st.plotly_chart(go.Figure(data=[bmd_plot], layout=go.Layout(
|
76 |
-
title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)"
|
|
|
77 |
)))
|
78 |
with col2:
|
79 |
st.plotly_chart(go.Figure(data=[tscore_plot], layout=go.Layout(
|
80 |
-
title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score"
|
|
|
81 |
)))
|
82 |
|
83 |
# Streamlit UI
|
|
|
32 |
|
33 |
for _, row in site_data.iterrows():
|
34 |
drug = row['Medication']
|
35 |
+
predictions = {
|
36 |
+
'Year': ['0'],
|
37 |
+
'Year Index': [0], # Numeric x-axis for plotting
|
38 |
+
'Predicted BMD': [round(bmd, 3)],
|
39 |
+
'Predicted T-score': [round(calculate_tscore(bmd, mu, sigma), 1)]
|
40 |
+
}
|
41 |
|
42 |
+
year_index = 1
|
43 |
for year in row.index[1:-1]: # Skip 'Medication' and 'Site' columns
|
44 |
if not pd.isna(row[year]):
|
45 |
percentage_increase = row[year]
|
|
|
47 |
predicted_tscore = calculate_tscore(predicted_bmd, mu, sigma)
|
48 |
|
49 |
predictions['Year'].append(year.replace(" Year", "")) # Simplify year label
|
50 |
+
predictions['Year Index'].append(year_index) # Numeric x-axis
|
51 |
predictions['Predicted BMD'].append(round(predicted_bmd, 3))
|
52 |
predictions['Predicted T-score'].append(round(predicted_tscore, 1))
|
53 |
+
year_index += 1
|
54 |
|
55 |
all_results.append({'Drug': drug, 'Predictions': predictions})
|
56 |
return all_results
|
57 |
|
58 |
+
|
59 |
# Display results as table and plots
|
60 |
def display_results(predictions, site):
|
61 |
st.subheader(f"Predictions for {site}")
|
|
|
68 |
st.write(f"### {drug}")
|
69 |
st.dataframe(pd.DataFrame(predictions))
|
70 |
|
71 |
+
# Plot BMD and T-score using Year Index
|
72 |
bmd_plot = go.Scatter(
|
73 |
+
x=predictions['Year Index'], y=predictions['Predicted BMD'], mode='lines+markers',
|
74 |
name='Predicted BMD', line=dict(color='blue')
|
75 |
)
|
76 |
tscore_plot = go.Scatter(
|
77 |
+
x=predictions['Year Index'], y=predictions['Predicted T-score'], mode='lines+markers',
|
78 |
name='Predicted T-score', line=dict(color='green')
|
79 |
)
|
80 |
|
|
|
82 |
col1, col2 = st.columns(2)
|
83 |
with col1:
|
84 |
st.plotly_chart(go.Figure(data=[bmd_plot], layout=go.Layout(
|
85 |
+
title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)",
|
86 |
+
xaxis=dict(tickmode='array', tickvals=predictions['Year Index'], ticktext=predictions['Year'])
|
87 |
)))
|
88 |
with col2:
|
89 |
st.plotly_chart(go.Figure(data=[tscore_plot], layout=go.Layout(
|
90 |
+
title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score",
|
91 |
+
xaxis=dict(tickmode='array', tickvals=predictions['Year Index'], ticktext=predictions['Year'])
|
92 |
)))
|
93 |
|
94 |
# Streamlit UI
|