Sephfox commited on
Commit
2ff6f00
·
verified ·
1 Parent(s): d015dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -61
app.py CHANGED
@@ -241,28 +241,26 @@ def format_number(num):
241
  return str(num)
242
 
243
  def update_chart():
244
- global fig # Assuming fig is a global variable
245
-
246
  # Clear existing traces
247
- fig.data = []
248
 
249
  # Cell positions
250
  cell_types = [cell.cell_type for cell in st.session_state.env.cells]
251
  x_positions = [cell.x for cell in st.session_state.env.cells]
252
  y_positions = [cell.y for cell in st.session_state.env.cells]
253
 
254
- fig.add_trace(go.Scatter(x=x_positions, y=y_positions, mode='markers',
255
  marker=dict(color=[colors[ct] for ct in cell_types]),
256
  text=cell_types, hoverinfo='text'), row=1, col=1)
257
 
258
  # Population history
259
  for cell_type, counts in st.session_state.env.population_history.items():
260
- fig.add_trace(go.Scatter(y=counts, mode='lines', name=cell_type,
261
  line=dict(color=colors[cell_type])), row=1, col=2)
262
 
263
  # Population by cell type
264
  for cell_type, counts in st.session_state.env.population_history.items():
265
- fig.add_trace(go.Scatter(y=counts, mode='lines', name=cell_type,
266
  line=dict(color=colors[cell_type])), row=2, col=1)
267
 
268
  # Organelle distribution
@@ -272,66 +270,23 @@ def update_chart():
272
  for organelle in cell.organelles:
273
  organelle_counts[organelle] += 1
274
 
275
- fig.add_trace(go.Bar(x=list(organelle_counts.keys()), y=list(organelle_counts.values()),
276
  name="Organelles"), row=2, col=2)
277
 
278
  # Update axis labels and layout
279
- fig.update_xaxes(title_text="X", row=1, col=1)
280
- fig.update_yaxes(title_text="Y", row=1, col=1)
281
- fig.update_xaxes(title_text="Time", row=1, col=2)
282
- fig.update_yaxes(title_text="Population", row=1, col=2)
283
- fig.update_xaxes(title_text="Time", row=2, col=1)
284
- fig.update_yaxes(title_text="Population", row=2, col=1)
285
- fig.update_xaxes(title_text="Organelle", row=2, col=2)
286
- fig.update_yaxes(title_text="Count", row=2, col=2)
287
 
288
- fig.update_layout(height=800, width=1200, title_text="Advanced Cell Evolution Simulation")
289
 
290
  # Update the chart placeholder
291
- chart_placeholder.plotly_chart(fig)
292
-
293
- # Sidebar for controls and live statistics
294
- st.sidebar.header("Simulation Controls")
295
- initial_cells = st.sidebar.slider("Initial number of cells", 10, 500, 200)
296
- update_interval = st.sidebar.slider("Update interval (seconds)", 0.01, 1.0, 0.05)
297
-
298
- st.sidebar.header("Environmental Effects")
299
- radiation = st.sidebar.checkbox("Radiation")
300
- predation = st.sidebar.checkbox("Predation")
301
- symbiosis = st.sidebar.checkbox("Symbiosis")
302
-
303
- effects = {
304
- "radiation": radiation,
305
- "predation": predation,
306
- "symbiosis": symbiosis
307
- }
308
-
309
- # Live statistics placeholders
310
- st.sidebar.header("Live Statistics")
311
- total_cells_text = st.sidebar.empty()
312
- cell_type_breakdown = st.sidebar.empty()
313
- dominant_type_text = st.sidebar.empty()
314
- avg_energy_text = st.sidebar.empty()
315
- total_merges_text = st.sidebar.empty()
316
-
317
- # Event log
318
- st.sidebar.header("Event Log")
319
- event_log = deque(maxlen=10) # Keep the last 10 events
320
- event_log_text = st.sidebar.empty()
321
-
322
- # Create placeholders for the chart
323
- chart_placeholder = st.empty()
324
-
325
-
326
-
327
- if 'running' not in st.session_state:
328
- st.session_state.running = False
329
- if 'total_merges' not in st.session_state:
330
- st.session_state.total_merges = 0
331
- if 'env' not in st.session_state:
332
- st.session_state.env = None
333
- if 'fig' not in st.session_state:
334
- st.session_state.fig = None
335
 
336
  def start_simulation():
337
  st.session_state.running = True
@@ -340,7 +295,15 @@ def start_simulation():
340
  for _ in range(initial_cells):
341
  cell = Cell(random.uniform(0, st.session_state.env.width), random.uniform(0, st.session_state.env.height))
342
  st.session_state.env.add_cell(cell)
343
- st.session_state.fig = setup_figure(st.session_state.env)
 
 
 
 
 
 
 
 
344
 
345
  def stop_simulation():
346
  st.session_state.running = False
 
241
  return str(num)
242
 
243
  def update_chart():
 
 
244
  # Clear existing traces
245
+ st.session_state.fig.data = []
246
 
247
  # Cell positions
248
  cell_types = [cell.cell_type for cell in st.session_state.env.cells]
249
  x_positions = [cell.x for cell in st.session_state.env.cells]
250
  y_positions = [cell.y for cell in st.session_state.env.cells]
251
 
252
+ st.session_state.fig.add_trace(go.Scatter(x=x_positions, y=y_positions, mode='markers',
253
  marker=dict(color=[colors[ct] for ct in cell_types]),
254
  text=cell_types, hoverinfo='text'), row=1, col=1)
255
 
256
  # Population history
257
  for cell_type, counts in st.session_state.env.population_history.items():
258
+ st.session_state.fig.add_trace(go.Scatter(y=counts, mode='lines', name=cell_type,
259
  line=dict(color=colors[cell_type])), row=1, col=2)
260
 
261
  # Population by cell type
262
  for cell_type, counts in st.session_state.env.population_history.items():
263
+ st.session_state.fig.add_trace(go.Scatter(y=counts, mode='lines', name=cell_type,
264
  line=dict(color=colors[cell_type])), row=2, col=1)
265
 
266
  # Organelle distribution
 
270
  for organelle in cell.organelles:
271
  organelle_counts[organelle] += 1
272
 
273
+ st.session_state.fig.add_trace(go.Bar(x=list(organelle_counts.keys()), y=list(organelle_counts.values()),
274
  name="Organelles"), row=2, col=2)
275
 
276
  # Update axis labels and layout
277
+ st.session_state.fig.update_xaxes(title_text="X", row=1, col=1)
278
+ st.session_state.fig.update_yaxes(title_text="Y", row=1, col=1)
279
+ st.session_state.fig.update_xaxes(title_text="Time", row=1, col=2)
280
+ st.session_state.fig.update_yaxes(title_text="Population", row=1, col=2)
281
+ st.session_state.fig.update_xaxes(title_text="Time", row=2, col=1)
282
+ st.session_state.fig.update_yaxes(title_text="Population", row=2, col=1)
283
+ st.session_state.fig.update_xaxes(title_text="Organelle", row=2, col=2)
284
+ st.session_state.fig.update_yaxes(title_text="Count", row=2, col=2)
285
 
286
+ st.session_state.fig.update_layout(height=800, width=1200, title_text="Advanced Cell Evolution Simulation")
287
 
288
  # Update the chart placeholder
289
+ chart_placeholder.plotly_chart(st.session_state.fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  def start_simulation():
292
  st.session_state.running = True
 
295
  for _ in range(initial_cells):
296
  cell = Cell(random.uniform(0, st.session_state.env.width), random.uniform(0, st.session_state.env.height))
297
  st.session_state.env.add_cell(cell)
298
+ st.session_state.fig = go.Figure(layout=go.Layout(
299
+ height=800, width=1200,
300
+ title='Advanced Cell Evolution Simulation',
301
+ showlegend=True,
302
+ ))
303
+ # Set up subplots
304
+ st.session_state.fig = make_subplots(rows=2, cols=2,
305
+ subplot_titles=('Cell Positions', 'Population History',
306
+ 'Population by Cell Type', 'Organelle Distribution'))
307
 
308
  def stop_simulation():
309
  st.session_state.running = False