Ashar086 commited on
Commit
f9988d0
·
verified ·
1 Parent(s): 422e260

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -90
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  import time
3
- import random
4
  import plotly.graph_objects as go
5
 
6
  # Set page layout to wide mode
@@ -20,114 +19,145 @@ outcomes = [
20
 
21
  # Different possible outcomes of each treatment
22
  possibilities = {
23
- 1: ["Well", "Slight Improvement", "No Response"],
24
- 2: ["Significant Improvement", "Mild Side Effects"],
25
- 3: ["Effective Treatment", "Inconclusive Results"],
26
- 4: ["Improvement", "Significant Side Effects"],
27
- 5: ["Worsened Side Effects", "Manageable Side Effects"],
28
- 6: ["Successful Adjustment", "Further Adjustment Needed"],
29
- 7: ["Good Response", "Limited Response"],
30
- 8: ["Complete Recovery", "Partial Improvement"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
32
 
33
- # Store patients' records (persistent across runs)
34
- patients_records = {
35
- "Well": 0,
36
- "Slight Improvement": 0,
37
- "No Response": 0,
38
- "Significant Improvement": 0,
39
- "Mild Side Effects": 0,
40
- "Effective Treatment": 0,
41
- "Inconclusive Results": 0,
42
- "Improvement": 0,
43
- "Significant Side Effects": 0,
44
- "Worsened Side Effects": 0,
45
- "Manageable Side Effects": 0,
46
- "Successful Adjustment": 0,
47
- "Further Adjustment Needed": 0,
48
- "Good Response": 0,
49
- "Limited Response": 0,
50
- "Complete Recovery": 0,
51
- "Partial Improvement": 0
52
- }
53
-
54
- # Generate random patient names
55
- def generate_patient_name():
56
- first_names = ["John", "Jane", "Alex", "Sara", "Tom", "Emily", "Michael", "Lucy"]
57
- last_names = ["Smith", "Doe", "Brown", "Wilson", "Taylor", "Lee"]
58
- return f"{random.choice(first_names)} {random.choice(last_names)}"
59
-
60
- # Run simulation and update patient record based on random outcomes
61
- def simulate_patient_outcome():
62
- for stage in range(1, 9):
63
- patient_name = generate_patient_name()
64
- outcome_list = possibilities[stage]
65
- outcome = random.choice(outcome_list)
66
- patients_records[outcome] += 1 # Increment count for that outcome
67
-
68
- # Create a tree diagram using Plotly with patient information
69
- def create_tree_diagram():
70
- labels = ["Patients"]
71
  parents = [""]
72
- values = [sum(patients_records.values())]
73
- colors = ['lightgrey']
74
 
75
- for category, patient_count in patients_records.items():
76
- labels.append(f"{category} ({patient_count} patients)")
77
- parents.append("Patients")
78
- values.append(patient_count)
79
- colors.append('lightgrey')
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  fig = go.Figure(go.Treemap(
82
  labels=labels,
83
  parents=parents,
84
  values=values,
85
  marker=dict(colors=colors),
86
- textinfo="label+value",
87
- hoverinfo="label+value"
88
  ))
89
 
90
  fig.update_layout(
91
  margin=dict(t=10, l=10, r=10, b=10),
92
  width=600, height=400,
93
- uniformtext=dict(minsize=12, mode='show'),
94
- transition_duration=500
95
  )
96
 
97
  return fig
98
 
99
  # Streamlit app layout
100
- st.title("Precision Medicine AI Agents - Treatment Decision Tree Simulation")
 
 
 
 
 
101
 
102
  # Add a start animation button
103
- run_simulation = st.button("Run Simulation")
104
-
105
- if run_simulation:
106
- simulate_patient_outcome() # Simulate new patient outcomes
107
-
108
- # Display the patient outcomes incrementally
109
- for i, (outcome, outcome_type) in enumerate(outcomes, 1):
110
- with st.container():
111
- col1, col2 = st.columns([1, 2])
112
-
113
- with col1:
114
- if outcome_type == "success":
115
- st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#d4edda; color:#155724;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
116
- elif outcome_type == "info":
117
- st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#cce5ff; color:#004085;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
118
- elif outcome_type == "warning":
119
- st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#fff3cd; color:#856404;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
120
- elif outcome_type == "error":
121
- st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#f8d7da; color:#721c24;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
122
-
123
- st.markdown("### Current Patients:")
124
- stage_possibilities = possibilities[i]
125
- for possibility in stage_possibilities:
126
- patient_count = patients_records.get(possibility, 0)
127
- st.markdown(f"**{possibility}:** {patient_count} patients")
128
-
129
- with col2:
130
- fig = create_tree_diagram() # Update chart with patient records
131
- st.plotly_chart(fig, use_container_width=True)
132
-
133
- time.sleep(1) # Shorter delay for smoother transitions
 
 
 
 
 
 
1
  import streamlit as st
2
  import time
 
3
  import plotly.graph_objects as go
4
 
5
  # Set page layout to wide mode
 
19
 
20
  # Different possible outcomes of each treatment
21
  possibilities = {
22
+ 1: [
23
+ ("Possibility 1: Patient responds well to Treatment 1", "success"),
24
+ ("Possibility 2: Slight improvement, but inconclusive results", "info"),
25
+ ("Possibility 3: No response, reevaluation needed", "error")
26
+ ],
27
+ 2: [
28
+ ("Possibility 1: Significant improvement", "success"),
29
+ ("Possibility 2: Mild side effects", "warning")
30
+ ],
31
+ 3: [
32
+ ("Possibility 1: Treatment is effective", "success"),
33
+ ("Possibility 2: Inconclusive lab results", "info")
34
+ ],
35
+ 4: [
36
+ ("Possibility 1: Improvement with Drug B", "success"),
37
+ ("Possibility 2: Significant side effects", "error")
38
+ ],
39
+ 5: [
40
+ ("Possibility 1: Side effects worsen, modify dosage", "warning"),
41
+ ("Possibility 2: Manageable side effects", "info")
42
+ ],
43
+ 6: [
44
+ ("Possibility 1: Dosage adjustment successful", "success"),
45
+ ("Possibility 2: Further modification needed", "warning")
46
+ ],
47
+ 7: [
48
+ ("Possibility 1: Patient responds well to modified treatment", "success"),
49
+ ("Possibility 2: Limited response, consider alternatives", "warning")
50
+ ],
51
+ 8: [
52
+ ("Possibility 1: Complete recovery", "success"),
53
+ ("Possibility 2: Partial improvement, continue monitoring", "info")
54
+ ]
55
  }
56
 
57
+ # Initialize session state for patient counts if not already present
58
+ if 'well_count' not in st.session_state:
59
+ st.session_state['well_count'] = 0
60
+ if 'slight_improvement_count' not in st.session_state:
61
+ st.session_state['slight_improvement_count'] = 0
62
+ if 'no_response_count' not in st.session_state:
63
+ st.session_state['no_response_count'] = 0
64
+
65
+ # Function to simulate patient response
66
+ def simulate_patient_response():
67
+ # Simulating possible outcomes - can be adjusted for more complexity
68
+ # Example random patient responses (you can adjust this logic)
69
+ import random
70
+ outcome = random.choice(["well", "slight improvement", "no response"])
71
+ if outcome == "well":
72
+ st.session_state['well_count'] += 1
73
+ elif outcome == "slight improvement":
74
+ st.session_state['slight_improvement_count'] += 1
75
+ elif outcome == "no response":
76
+ st.session_state['no_response_count'] += 1
77
+
78
+ # Create a tree diagram using Plotly (unchanged from original)
79
+ def create_tree_diagram(stage):
80
+ labels = [f"Stage {stage}: {outcomes[stage - 1][0]}"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  parents = [""]
82
+ values = [1] # Root node value
83
+ colors = ['lightgrey'] # Root node color
84
 
85
+ stage_possibilities = possibilities.get(stage, [("No specific possibilities defined", "info")])
86
+
87
+ for possibility, outcome_type in stage_possibilities:
88
+ labels.append(possibility)
89
+ parents.append(f"Stage {stage}: {outcomes[stage - 1][0]}")
90
+ values.append(1) # Equal weight for all possibilities
91
+ if outcome_type == "success":
92
+ colors.append('#d4edda')
93
+ elif outcome_type == "info":
94
+ colors.append('#cce5ff')
95
+ elif outcome_type == "warning":
96
+ colors.append('#fff3cd')
97
+ elif outcome_type == "error":
98
+ colors.append('#f8d7da')
99
+ else:
100
+ colors.append('lightgrey')
101
 
102
  fig = go.Figure(go.Treemap(
103
  labels=labels,
104
  parents=parents,
105
  values=values,
106
  marker=dict(colors=colors),
107
+ textinfo="label",
108
+ textfont=dict(size=14),
109
  ))
110
 
111
  fig.update_layout(
112
  margin=dict(t=10, l=10, r=10, b=10),
113
  width=600, height=400,
114
+ uniformtext=dict(minsize=12, mode='show')
 
115
  )
116
 
117
  return fig
118
 
119
  # Streamlit app layout
120
+ st.title("Precision Medicine AI Agents - Treatment Decision Tree")
121
+
122
+ # Display the current patient counts
123
+ st.write(f"### Well: {st.session_state['well_count']}")
124
+ st.write(f"### Slight Improvement: {st.session_state['slight_improvement_count']}")
125
+ st.write(f"### No Response: {st.session_state['no_response_count']}")
126
 
127
  # Add a start animation button
128
+ start_button = st.button("Start Animation")
129
+
130
+ if start_button:
131
+ simulate_patient_response() # Simulate patient response when button is pressed
132
+
133
+ for i, (outcome, outcome_type) in enumerate(outcomes, 1):
134
+ with st.container():
135
+ col1, col2 = st.columns([1, 2])
136
+
137
+ with col1:
138
+ if outcome_type == "success":
139
+ st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#d4edda; color:#155724;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
140
+ elif outcome_type == "info":
141
+ st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#cce5ff; color:#004085;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
142
+ elif outcome_type == "warning":
143
+ st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#fff3cd; color:#856404;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
144
+ elif outcome_type == "error":
145
+ st.markdown(f"<div style='padding:10px; border-radius:5px; background-color:#f8d7da; color:#721c24;'><strong>Stage {i}:</strong> {outcome}</div>", unsafe_allow_html=True)
146
+
147
+ st.markdown("### Possibilities:")
148
+ stage_possibilities = possibilities.get(i, [("No specific possibilities defined", "info")])
149
+ for possibility, possibility_type in stage_possibilities:
150
+ if possibility_type == "success":
151
+ st.markdown(f"<div style='padding:5px; background-color:#d4edda; color:#155724;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
152
+ elif possibility_type == "info":
153
+ st.markdown(f"<div style='padding:5px; background-color:#cce5ff; color:#004085;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
154
+ elif possibility_type == "warning":
155
+ st.markdown(f"<div style='padding:5px; background-color:#fff3cd; color:#856404;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
156
+ elif possibility_type == "error":
157
+ st.markdown(f"<div style='padding:5px; background-color:#f8d7da; color:#721c24;'><strong>{possibility}</strong></div>", unsafe_allow_html=True)
158
+
159
+ with col2:
160
+ fig = create_tree_diagram(i)
161
+ st.plotly_chart(fig, use_container_width=True)
162
+
163
+ time.sleep(5)