Daniel Holdbrook commited on
Commit
efd9eee
Β·
1 Parent(s): 646d2e8

dash updates

Browse files
Files changed (1) hide show
  1. app.py +151 -126
app.py CHANGED
@@ -39,6 +39,8 @@ step_counter = solara.reactive(0)
39
  running = solara.reactive(False)
40
  scatter_figure = solara.reactive(go.Figure())
41
  bar_figure = solara.reactive(go.Figure())
 
 
42
  # Reactive parameter bindings
43
  kon_a = solara.reactive(KON_A)
44
  koff_a = solara.reactive(KOFF_A)
@@ -56,28 +58,26 @@ enable_binding_b = solara.reactive(True)
56
  def render_plotly_snapshot(current_model, step_label):
57
  rec_a_x, rec_a_y = [], []
58
  rec_b_x, rec_b_y = [], []
59
- agents = getattr(current_model, "agents", [])
60
- # Compute receptor count here
61
- total_receptors = sum(
62
- 1 for agent in agents if getattr(agent, "rec_type", None) in ("A", "B")
63
- )
64
-
65
  ab0_x, ab0_y = [], []
66
  ab1_x, ab1_y = [], []
67
  ab2_x, ab2_y = [], []
 
68
  n0, n1, n2 = 0, 0, 0
 
 
 
69
 
70
  for agent in agents:
71
  if not hasattr(agent, "pos"):
72
  continue
73
 
74
  x, y = agent.pos
75
- agent_type = getattr(agent, "rec_type", None)
76
 
77
- if agent_type == "A":
78
  rec_a_x.append(x)
79
  rec_a_y.append(y)
80
- elif agent_type == "B":
81
  rec_b_x.append(x)
82
  rec_b_y.append(y)
83
  else:
@@ -90,149 +90,146 @@ def render_plotly_snapshot(current_model, step_label):
90
  ab1_x.append(x)
91
  ab1_y.append(y)
92
  n1 += 1
 
 
 
 
 
 
93
  elif n_bound == 2:
94
  ab2_x.append(x)
95
  ab2_y.append(y)
96
  n2 += 1
97
 
98
- scatter_fig = go.Figure()
99
- scatter_fig.add_trace(go.Scatter(
100
- x=rec_a_x, y=rec_a_y,
101
- mode='markers',
102
- name="Receptor A",
103
- marker=dict(symbol="hourglass", size=9, color="purple"),
104
- showlegend=True
105
- ))
106
- scatter_fig.add_trace(go.Scatter(
107
- x=rec_b_x, y=rec_b_y,
108
- mode='markers',
109
- name="Receptor B",
110
- marker=dict(symbol="diamond-tall", size=9, color="orange"),
111
- showlegend=True
112
  ))
113
- scatter_fig.add_trace(go.Scatter(
114
- x=ab0_x if ab0_x else [None],
115
- y=ab0_y if ab0_y else [None],
116
- mode='markers',
117
- name="Unbound Antibodies",
118
- marker=dict(symbol="y-up", size=12, line = dict(width=4, color="DarkSlateGrey")),
119
- showlegend=True
120
  ))
121
- scatter_fig.add_trace(go.Scatter(
122
- x=ab1_x if ab1_x else [None],
123
- y=ab1_y if ab1_y else [None],
124
- mode='markers',
125
- name="Monovalent Antibodies",
126
- marker=dict(symbol="y-up", size=14, line = dict(width=5, color="cyan")),
127
- showlegend=True
128
- ))
129
- scatter_fig.add_trace(go.Scatter(
130
- x=ab2_x if ab2_x else [None],
131
- y=ab2_y if ab2_y else [None],
132
- mode='markers',
133
- name="Bivalent Antibodies",
134
- marker=dict(symbol="y-up", size=18, line = dict(width=6, color="Green")),
135
- showlegend=True
136
  ))
137
 
138
- n0, n1, n2 = 0, 0, 0
139
- for agent in agents:
140
- if hasattr(agent, "n_bound"):
141
- if agent.n_bound == 0:
142
- n0 += 1
143
- elif agent.n_bound == 1:
144
- n1 += 1
145
- elif agent.n_bound == 2:
146
- n2 += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- bar_fig = go.Figure()
149
- bar_fig.add_trace(go.Bar(
150
- x=["Singly Bound", "Doubly Bound"],
151
- y=[n1, n2]
152
- ))
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
- # Calculate effective concentration of free antibodies
155
  area = current_model.space_size ** 2 # Β΅mΒ²
156
- depth_um = 0.06 # 60 nm in Β΅m
157
- volume_liters = area * depth_um * 1e-15 # Β΅mΒ³ to L
158
  avogadro_number = 6.022e23
159
  concentration_nM = (n0 / (avogadro_number * volume_liters) * 1e9) if volume_liters > 0 else 0
160
 
161
- # Calculate effective time
162
- effective_time_sec = step_label * getattr(current_model, "dt", 1.0)
163
-
164
  scatter_fig.update_layout(
165
- title=(
166
- f"Step {step_label} | "
167
- f"Free Abs: {n0} | "
168
- f"[Ab] β‰ˆ {concentration_nM:.2f} nM | "
169
- f"Time β‰ˆ {effective_time_sec:.2f} s"
170
- ),
171
- xaxis=dict(
172
- title="X (Β΅m)",
173
- range=[0, current_model.space_size],
174
- showgrid=False,
175
- showline=True, # Show x-axis line
176
- mirror=True, # Mirror it on the top
177
- linecolor='black',
178
- linewidth=2
179
- ),
180
- yaxis=dict(
181
- title="Y (Β΅m)",
182
- range=[-0.5, current_model.space_size],
183
- showgrid=False,
184
- showline=True, # Show y-axis line
185
- mirror=True, # Mirror it on the right
186
- linecolor='black',
187
- linewidth=2
188
- ),
189
  width=700,
190
  height=700,
191
  plot_bgcolor="gainsboro",
192
  paper_bgcolor="whitesmoke",
193
- legend=dict(
194
- title="Agent Types",
195
- x=1,
196
- y=1,
197
- xanchor="right",
198
- yanchor="top",
199
- bgcolor="rgba(255,255,255,0.85)" # white with 30% opacity
200
- )
201
  )
202
 
 
203
  bar_fig = go.Figure()
204
- bar_fig.add_trace(go.Bar(
205
- x=["Singly Bound", "Doubly Bound"],
206
- y=[n1 if n1 else 0, n2 if n2 else 0],
207
- marker_color=["cyan", "green"]
208
- ))
209
-
210
  bar_fig.update_layout(
211
  title=f"Antibody Bound States @ Step {step_label}",
212
- xaxis=dict(
213
- title="Binding State",
214
- showgrid=False,
215
- showline=True,
216
- mirror=True,
217
- linecolor='black',
218
- linewidth=2
219
- ),
220
- yaxis=dict(
221
- title="Count (antibodies)",
222
- range=[0, total_receptors],
223
- showgrid=False,
224
- showline=True,
225
- mirror=True,
226
- linecolor='black',
227
- linewidth=2
228
- ),
229
  width=500,
230
  height=500,
231
  plot_bgcolor="gainsboro",
232
  paper_bgcolor="whitesmoke"
233
  )
234
 
235
- return scatter_fig, bar_fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
 
237
  def calculate_c_bulk(n_antibodies_value, space_size=SPACE_SIZE, depth_um=0.06):
238
  volume_liters = space_size**2 * depth_um * 1e-15 # Β΅mΒ³ to L
@@ -256,19 +253,41 @@ def apply_system_settings():
256
  c_bulk=computed_c_bulk.value
257
  )
258
  step_counter.value = 0
259
- scatter, bar = render_plotly_snapshot(model.value, step_counter.value)
260
  scatter_figure.value = scatter
261
  bar_figure.value = bar
 
262
 
263
 
264
 
265
  # ─── Reset Logic ─────────────────────────────────────────────────────
266
  def reset_model():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  model.value = HybridAvidityModel()
268
  step_counter.value = 0
269
- scatter, bar = render_plotly_snapshot(model.value, step_counter.value)
 
 
 
 
270
  scatter_figure.value = scatter
271
  bar_figure.value = bar
 
272
 
273
  # ─── Simulation Step Logic ─────────────────────────────────────────────────────
274
  def step_model_actions():
@@ -280,9 +299,10 @@ def step_model_actions():
280
  model.value.step()
281
 
282
  step_counter.value += step_skip.value
283
- scatter_fig_new, bar_fig_new = render_plotly_snapshot(model.value, step_counter.value)
284
  scatter_figure.value = scatter_fig_new
285
  bar_figure.value = bar_fig_new
 
286
 
287
 
288
  # ─── Solara Page Component ─────────────────────────────────────────────────────
@@ -290,9 +310,10 @@ def step_model_actions():
290
  def Page():
291
  def initial_render():
292
  if hasattr(model.value, 'agents') and model.value.agents:
293
- scatter, bar = render_plotly_snapshot(model.value, step_counter.value)
294
  scatter_figure.value = scatter
295
  bar_figure.value = bar
 
296
  else:
297
  empty_scatter = go.Figure()
298
  empty_scatter.update_layout(
@@ -407,5 +428,9 @@ def Page():
407
  with solara.Row(gap="20px", justify="left", style="margin-top: 20px;"):
408
  with solara.Card(elevation=2):
409
  solara.FigurePlotly(scatter_figure.value)
 
410
  with solara.Card(elevation=2):
411
- solara.FigurePlotly(bar_figure.value)
 
 
 
 
39
  running = solara.reactive(False)
40
  scatter_figure = solara.reactive(go.Figure())
41
  bar_figure = solara.reactive(go.Figure())
42
+ line_figure = solara.reactive(go.Figure())
43
+ binding_history = solara.reactive([])
44
  # Reactive parameter bindings
45
  kon_a = solara.reactive(KON_A)
46
  koff_a = solara.reactive(KOFF_A)
 
58
  def render_plotly_snapshot(current_model, step_label):
59
  rec_a_x, rec_a_y = [], []
60
  rec_b_x, rec_b_y = [], []
 
 
 
 
 
 
61
  ab0_x, ab0_y = [], []
62
  ab1_x, ab1_y = [], []
63
  ab2_x, ab2_y = [], []
64
+
65
  n0, n1, n2 = 0, 0, 0
66
+ n1_a, n1_b = 0, 0 # NEW: singly bound to A vs B
67
+ agents = getattr(current_model, "agents", [])
68
+ total_receptors = sum(1 for agent in agents if getattr(agent, "rec_type", None) in ("A", "B"))
69
 
70
  for agent in agents:
71
  if not hasattr(agent, "pos"):
72
  continue
73
 
74
  x, y = agent.pos
75
+ rec_type = getattr(agent, "rec_type", None)
76
 
77
+ if rec_type == "A":
78
  rec_a_x.append(x)
79
  rec_a_y.append(y)
80
+ elif rec_type == "B":
81
  rec_b_x.append(x)
82
  rec_b_y.append(y)
83
  else:
 
90
  ab1_x.append(x)
91
  ab1_y.append(y)
92
  n1 += 1
93
+ # Identify which arm is bound
94
+ arm_state = getattr(agent, "arm_state", {})
95
+ if arm_state.get("A") is not None:
96
+ n1_a += 1
97
+ elif arm_state.get("B") is not None:
98
+ n1_b += 1
99
  elif n_bound == 2:
100
  ab2_x.append(x)
101
  ab2_y.append(y)
102
  n2 += 1
103
 
104
+ # ─── Line chart data update ─────────────────────────────────────────────
105
+ current_time = step_label * current_model.dt
106
+ binding_history.value.append((current_time, n1, n2, n1_a, n1_b))
107
+ times = [t for t, *_ in binding_history.value]
108
+ singly = [s for _, s, *_ in binding_history.value]
109
+ doubly = [d for _, _, d, *_ in binding_history.value]
110
+ singly_a = [a for *_, a, _ in binding_history.value]
111
+ singly_b = [b for *_, _, b in binding_history.value]
112
+
113
+ line_fig = go.Figure()
114
+ line_fig.add_trace(go.Scatter(
115
+ x=times, y=singly_a, mode='lines+markers', name='Singly Bound A',
116
+ line=dict(color='purple')
 
117
  ))
118
+ line_fig.add_trace(go.Scatter(
119
+ x=times, y=singly_b, mode='lines+markers', name='Singly Bound B',
120
+ line=dict(color='orange')
 
 
 
 
121
  ))
122
+ line_fig.add_trace(go.Scatter(
123
+ x=times, y=doubly, mode='lines+markers', name='Doubly Bound',
124
+ line=dict(color='green')
 
 
 
 
 
 
 
 
 
 
 
 
125
  ))
126
 
127
+ line_fig.update_layout(
128
+ title="Receptor Binding Over Time",
129
+ xaxis=dict(
130
+ title="Time (s)",
131
+ range=[0, current_time], # Start at 0, end at current time
132
+ zeroline=True,
133
+ showline=True,
134
+ linecolor='black'
135
+ ),
136
+ yaxis=dict(
137
+ title="Number of Bound Antibodies",
138
+ range=[0, max(max(singly + [0]), max(doubly + [0]), 1)], # Start at 0, auto upper bound
139
+ zeroline=True,
140
+ showline=True,
141
+ linecolor='black'
142
+ ),
143
+ width=500,
144
+ height=500,
145
+ plot_bgcolor="gainsboro",
146
+ paper_bgcolor="whitesmoke",
147
+ legend=dict(x=0.01, y=0.99)
148
+ )
149
 
150
+ line_figure.value = line_fig
151
+
152
+ # ─── Scatter plot ───────────────────────────────────────────────────────
153
+ scatter_fig = go.Figure()
154
+ scatter_fig.add_trace(go.Scatter(x=rec_a_x, y=rec_a_y, mode='markers', name="Receptor A",
155
+ marker=dict(symbol="hourglass", size=9, color="purple")))
156
+ scatter_fig.add_trace(go.Scatter(x=rec_b_x, y=rec_b_y, mode='markers', name="Receptor B",
157
+ marker=dict(symbol="diamond-tall", size=9, color="orange")))
158
+ scatter_fig.add_trace(go.Scatter(x=ab0_x or [None], y=ab0_y or [None], mode='markers',
159
+ name="Unbound Antibodies",
160
+ marker=dict(symbol="y-up", size=12, line=dict(width=4, color="DarkSlateGrey"))))
161
+ scatter_fig.add_trace(go.Scatter(x=ab1_x or [None], y=ab1_y or [None], mode='markers',
162
+ name="Monovalent Antibodies",
163
+ marker=dict(symbol="y-up", size=14, line=dict(width=5, color="cyan"))))
164
+ scatter_fig.add_trace(go.Scatter(x=ab2_x or [None], y=ab2_y or [None], mode='markers',
165
+ name="Bivalent Antibodies",
166
+ marker=dict(symbol="y-up", size=18, line=dict(width=6, color="Green"))))
167
 
 
168
  area = current_model.space_size ** 2 # Β΅mΒ²
169
+ depth_um = 0.06 # Β΅m
170
+ volume_liters = area * depth_um * 1e-15
171
  avogadro_number = 6.022e23
172
  concentration_nM = (n0 / (avogadro_number * volume_liters) * 1e9) if volume_liters > 0 else 0
173
 
 
 
 
174
  scatter_fig.update_layout(
175
+ title=(f"Step {step_label} | Free Abs: {n0} | [Ab] β‰ˆ {concentration_nM:.2f} nM | Time β‰ˆ {current_time:.2f} s"),
176
+ xaxis=dict(title="X (Β΅m)", range=[0, current_model.space_size], showgrid=False, showline=True, mirror=True,
177
+ linecolor='black', linewidth=2),
178
+ yaxis=dict(title="Y (Β΅m)", range=[-0.5, current_model.space_size], showgrid=False, showline=True, mirror=True,
179
+ linecolor='black', linewidth=2),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  width=700,
181
  height=700,
182
  plot_bgcolor="gainsboro",
183
  paper_bgcolor="whitesmoke",
184
+ legend=dict(title="Agent Types", x=1, y=1, xanchor="right", yanchor="top", bgcolor="rgba(255,255,255,0.85)")
 
 
 
 
 
 
 
185
  )
186
 
187
+ # ─── Bar plot ────────────────────────────────────────────────────────────
188
  bar_fig = go.Figure()
189
+ bar_fig.add_trace(go.Bar(x=["Singly Bound", "Doubly Bound"],
190
+ y=[n1, n2],
191
+ marker_color=["cyan", "green"]))
 
 
 
192
  bar_fig.update_layout(
193
  title=f"Antibody Bound States @ Step {step_label}",
194
+ xaxis=dict(title="Binding State", showgrid=False, showline=True, mirror=True,
195
+ linecolor='black', linewidth=2),
196
+ yaxis=dict(title="Count (antibodies)", range=[0, total_receptors], showgrid=False, showline=True,
197
+ mirror=True, linecolor='black', linewidth=2),
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  width=500,
199
  height=500,
200
  plot_bgcolor="gainsboro",
201
  paper_bgcolor="whitesmoke"
202
  )
203
 
204
+ return scatter_fig, bar_fig, line_fig
205
+
206
+ def update_line_figure(n1, n2):
207
+ current_time = step_counter.value * model.value.dt
208
+ binding_history.value.append((current_time, n1, n2))
209
+
210
+ # Extract data for plotting
211
+ times = [t for t, _, _ in binding_history.value]
212
+ singly = [s for _, s, _ in binding_history.value]
213
+ doubly = [d for _, _, d in binding_history.value]
214
+
215
+ # Create the line figure
216
+ fig = go.Figure()
217
+ fig.add_trace(go.Scatter(x=times, y=singly, mode='lines+markers', name='Singly Bound'))
218
+ fig.add_trace(go.Scatter(x=times, y=doubly, mode='lines+markers', name='Doubly Bound'))
219
+
220
+ fig.update_layout(
221
+ title="Receptor Binding Over Time",
222
+ xaxis_title="Time (s)",
223
+ yaxis_title="Number of Bound Antibodies",
224
+ width=700,
225
+ height=400,
226
+ plot_bgcolor="gainsboro",
227
+ paper_bgcolor="whitesmoke",
228
+ legend=dict(x=0.01, y=0.99)
229
+ )
230
+
231
+ line_figure.value = fig
232
+
233
 
234
  def calculate_c_bulk(n_antibodies_value, space_size=SPACE_SIZE, depth_um=0.06):
235
  volume_liters = space_size**2 * depth_um * 1e-15 # Β΅mΒ³ to L
 
253
  c_bulk=computed_c_bulk.value
254
  )
255
  step_counter.value = 0
256
+ scatter, bar, line = render_plotly_snapshot(model.value, step_counter.value)
257
  scatter_figure.value = scatter
258
  bar_figure.value = bar
259
+ line_figure.value = line
260
 
261
 
262
 
263
  # ─── Reset Logic ─────────────────────────────────────────────────────
264
  def reset_model():
265
+ # Reset all user-editable settings to defaults
266
+ n_receptor_a.value = N_RECEPTOR_A
267
+ n_receptor_b.value = N_RECEPTOR_B
268
+ n_antibodies.value = N_ANTIBODIES
269
+ kon_a.value = KON_A
270
+ koff_a.value = KOFF_A
271
+ kon_b.value = KON_B
272
+ koff_b.value = KOFF_B
273
+ enable_binding_a.value = True
274
+ enable_binding_b.value = True
275
+ step_skip.value = 1
276
+
277
+ # Recalculate c_bulk based on default values
278
+ computed_c_bulk.value = calculate_c_bulk(N_ANTIBODIES)
279
+
280
+ # Reset simulation state
281
  model.value = HybridAvidityModel()
282
  step_counter.value = 0
283
+ binding_history.value = []
284
+ line_figure.value = go.Figure()
285
+
286
+ # Render default plots
287
+ scatter, bar, line = render_plotly_snapshot(model.value, step_counter.value)
288
  scatter_figure.value = scatter
289
  bar_figure.value = bar
290
+ line_figure.value = line
291
 
292
  # ─── Simulation Step Logic ─────────────────────────────────────────────────────
293
  def step_model_actions():
 
299
  model.value.step()
300
 
301
  step_counter.value += step_skip.value
302
+ scatter_fig_new, bar_fig_new, line_fig_new = render_plotly_snapshot(model.value, step_counter.value)
303
  scatter_figure.value = scatter_fig_new
304
  bar_figure.value = bar_fig_new
305
+ line_figure.value = line_fig_new
306
 
307
 
308
  # ─── Solara Page Component ─────────────────────────────────────────────────────
 
310
  def Page():
311
  def initial_render():
312
  if hasattr(model.value, 'agents') and model.value.agents:
313
+ scatter, bar, line_fig_new = render_plotly_snapshot(model.value, step_counter.value)
314
  scatter_figure.value = scatter
315
  bar_figure.value = bar
316
+ line_figure.value = line_fig_new
317
  else:
318
  empty_scatter = go.Figure()
319
  empty_scatter.update_layout(
 
428
  with solara.Row(gap="20px", justify="left", style="margin-top: 20px;"):
429
  with solara.Card(elevation=2):
430
  solara.FigurePlotly(scatter_figure.value)
431
+
432
  with solara.Card(elevation=2):
433
+ with solara.Column():
434
+ solara.FigurePlotly(bar_figure.value)
435
+ solara.FigurePlotly(line_figure.value)
436
+