awacke1 commited on
Commit
ab34103
Β·
1 Parent(s): 1d136fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -47
app.py CHANGED
@@ -4,60 +4,84 @@ import random
4
  import plotly.graph_objects as go
5
  import plotly.express as px
6
 
7
- def health_game():
8
- # Define the states and conditions of interest
9
- states = ["Minnesota", "Florida", "California"]
10
- top_n = 10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Define the list dictionary of top 10 health conditions descending by cost, with emojis, treatment recommendation and potential savings
13
- health_conditions = [
14
- {"condition": "πŸ’” Heart disease", "emoji": "πŸ’—", "spending": 214.3, "treatment": "Regular checkups with a cardiologist", "savings": "$1000"},
15
- {"condition": "πŸ€• Trauma-related disorders", "emoji": "πŸš‘", "spending": 198.6, "treatment": "Counseling and physical therapy", "savings": "$500"},
16
- {"condition": "πŸ¦€ Cancer", "emoji": "πŸŽ—οΈ", "spending": 171.0, "treatment": "Early detection and treatment", "savings": "$2000"},
17
- {"condition": "🧠 Mental disorders", "emoji": "🧘", "spending": 150.8, "treatment": "Therapy and medication", "savings": "$1500"},
18
- {"condition": "🦴 Osteoarthritis and joint disorders", "emoji": "πŸ₯", "spending": 142.4, "treatment": "Low-impact exercise and physical therapy", "savings": "$800"},
19
- {"condition": "πŸ’‰ Diabetes", "emoji": "🩸", "spending": 107.4, "treatment": "Regular checkups and medication", "savings": "$1200"},
20
- {"condition": "🫁 Chronic obstructive pulmonary disease and asthma", "emoji": "πŸ«€", "spending": 91.0, "treatment": "Inhalers and breathing exercises", "savings": "$600"},
21
- {"condition": "🩺 Hypertension", "emoji": "πŸ’‰", "spending": 83.9, "treatment": "Lifestyle changes and medication", "savings": "$900"},
22
- {"condition": "πŸ”¬ Hyperlipidemia", "emoji": "πŸ”¬", "spending": 83.9, "treatment": "Lifestyle changes and medication", "savings": "$700"},
23
- {"condition": "🦴 Back problems", "emoji": "🧍", "spending": 67.0, "treatment": "Physical therapy and exercise", "savings": "$400"}
24
- ]
25
-
26
- # Create a DataFrame from the list dictionary
27
- df_top_conditions = pd.DataFrame(health_conditions)
28
-
29
- # Calculate the total spending
30
- total_spending = round(df_top_conditions["spending"].sum(), 1)
31
-
32
- # Define the roll function
33
- def roll():
34
- rolls = [random.randint(1, 10) for _ in range(1000)]
35
- frequencies = [rolls.count(i) for i in range(1, 11)]
36
- return frequencies
37
-
38
- # Define the sunburst chart
39
- fig_sunburst = go.Figure(go.Sunburst(
40
- labels=df_top_conditions["emoji"] + " " + df_top_conditions["condition"],
41
- parents=[""] * top_n,
42
- values=df_top_conditions["spending"],
43
- maxdepth=2
44
  ))
45
 
46
- # Customize the layout of the sunburst chart
47
-
48
- fig_sunburst.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # Display the sunburst chart and variants per condition in the Streamlit app
51
  st.plotly_chart(fig_sunburst)
52
- condition_idx = st.selectbox("Select your current health condition", df_top_conditions.index)
53
- row = df_top_conditions.loc[condition_idx]
54
 
55
- st.write(f"Based on the severity of your {row['condition']}, we recommend {row['treatment']} for early treatment. This could save you up to {row['savings']} in healthcare costs.")
 
 
 
 
 
 
 
 
 
 
56
 
57
- frequencies = roll()
58
- fig_bar = px.bar(x=[f"Variant {i}" for i in range(1, 11)], y=frequencies[:10], labels={'x': 'Variant', 'y': 'Frequency'})
59
- fig_bar.update_layout(title=f"Variants of {row['condition']} ({row['emoji']})")
60
  st.plotly_chart(fig_bar)
61
 
 
 
62
 
63
- health_game()
 
4
  import plotly.graph_objects as go
5
  import plotly.express as px
6
 
7
+ def generate_health_conditions():
8
+ return [
9
+ {"condition": "πŸ’” Heart disease", "emoji": "πŸ’—", "spending": {"Minnesota": 100, "Florida": 150, "California": 200, "New York": 120, "Texas": 180}, "treatment": "Regular checkups with a cardiologist", "savings": "$1000"},
10
+ {"condition": "πŸ€• Trauma-related disorders", "emoji": "πŸš‘", "spending": {"Minnesota": 90, "Florida": 110, "California": 150, "New York": 100, "Texas": 130}, "treatment": "Counseling and physical therapy", "savings": "$500"},
11
+ {"condition": "πŸ¦€ Cancer", "emoji": "πŸŽ—οΈ", "spending": {"Minnesota": 80, "Florida": 120, "California": 180, "New York": 100, "Texas": 150}, "treatment": "Early detection and treatment", "savings": "$2000"},
12
+ {"condition": "🧠 Mental disorders", "emoji": "🧘", "spending": {"Minnesota": 70, "Florida": 100, "California": 140, "New York": 90, "Texas": 120}, "treatment": "Therapy and medication", "savings": "$1500"},
13
+ {"condition": "🦴 Osteoarthritis and joint disorders", "emoji": "πŸ₯", "spending": {"Minnesota": 60, "Florida": 90, "California": 120, "New York": 80, "Texas": 100}, "treatment": "Low-impact exercise and physical therapy", "savings": "$800"},
14
+ {"condition": "πŸ’‰ Diabetes", "emoji": "🩸", "spending": {"Minnesota": 50, "Florida": 80, "California": 100, "New York": 60, "Texas": 90}, "treatment": "Regular checkups and medication", "savings": "$1200"},
15
+ {"condition": "🫁 Chronic obstructive pulmonary disease and asthma", "emoji": "πŸ«€", "spending": {"Minnesota": 40, "Florida": 70, "California": 90, "New York": 50, "Texas": 80}, "treatment": "Inhalers and breathing exercises", "savings": "$600"},
16
+ {"condition": "🩺 Hypertension", "emoji": "πŸ’‰", "spending": {"Minnesota": 30, "Florida": 60, "California": 80, "New York": 40, "Texas": 70}, "treatment": "Lifestyle changes and medication", "savings": "$900"},
17
+ {"condition": "πŸ”¬ Hyperlipidemia", "emoji": "πŸ”¬", "spending": {"Minnesota": 20, "Florida": 50, "California": 70, "New York": 30, "Texas": 60}, "treatment": "Lifestyle changes and medication", "savings": "$700"},
18
+ {"condition": "🦴 Back problems", "emoji": "🧍", "spending": {"Minnesota": 10, "Florida": 40,
19
+
20
+ def calculate_total_spending(health_conditions):
21
+ total_spending = 0
22
+ for condition in health_conditions:
23
+ for state, spending in condition["spending"].items():
24
+ total_spending += spending
25
+ return round(total_spending, 1)
26
 
27
+ def generate_sunburst_chart(health_conditions, total_spending):
28
+ fig_sunburst = go.Figure(go.Treemap(
29
+ labels=[f"{condition['emoji']} {condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()],
30
+ parents=[f"{condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()],
31
+ values=[spending for condition in health_conditions for spending in condition['spending'].values()],
32
+ branchvalues="total",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  ))
34
 
35
+ fig_sunburst.update_layout(
36
+ title=f"Top Health Conditions in Different States by Spending (Total: ${total_spending}B)",
37
+ margin=dict(l=0, r=0, t=50, b=0),
38
+ )
39
+ return fig_sunburst
40
+
41
+ def roll(state, condition):
42
+ frequencies = [random.randint(1, 10) for _ in range(1000)]
43
+ spending = condition["spending"][state]
44
+ frequencies = [round(frequency * spending / 100, 1) for frequency in frequencies]
45
+ return frequencies
46
+
47
+ def generate_bar_chart(state, condition, frequencies):
48
+ fig_bar = px.bar(
49
+ x=[f"Variant {i}" for i in range(1, 11)],
50
+ y=frequencies[:10],
51
+ labels={'x': 'Variant', 'y': 'Cost'},
52
+ title=f"Variants of {condition['condition']} ({condition['emoji']}) in {state}",
53
+ )
54
+ return fig_bar
55
+
56
+ def generate_recommendation(condition):
57
+ return f"Based on the severity of your {condition['condition']}, we recommend {condition['treatment']} for early treatment. This could save you up to {condition['savings']} in healthcare costs."
58
+
59
+ def main():
60
+ states = ["Minnesota", "Florida", "California", "New York", "Texas"]
61
+ top_n = 10
62
+
63
+ health_conditions = generate_health_conditions()
64
+
65
+ total_spending = calculate_total_spending(health_conditions)
66
+
67
+ fig_sunburst = generate_sunburst_chart(health_conditions, total_spending)
68
 
 
69
  st.plotly_chart(fig_sunburst)
 
 
70
 
71
+ condition_idx = st.selectbox("Select your current health condition", range(top_n))
72
+
73
+ condition = health_conditions[condition_idx]
74
+
75
+ st.write(generate_recommendation(condition))
76
+
77
+ state = st.selectbox("Select your state", states)
78
+
79
+ frequencies = roll(state, condition)
80
+
81
+ fig_bar = generate_bar_chart(state, condition, frequencies)
82
 
 
 
 
83
  st.plotly_chart(fig_bar)
84
 
85
+ if __name__ == "__main__":
86
+ main()
87