Update app.py
Browse files
app.py
CHANGED
@@ -296,83 +296,74 @@ def toggle_simulation():
|
|
296 |
|
297 |
start_stop_button = st.button("Start/Stop Simulation", on_click=toggle_simulation)
|
298 |
|
299 |
-
def
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
time.sleep(update_interval)
|
319 |
|
320 |
def update_chart():
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
cell_data, population_history, colors = st.session_state.env.get_visualization_data()
|
325 |
-
|
326 |
-
# Update cell distribution
|
327 |
-
for i, (cell_type, data) in enumerate(cell_data.items()):
|
328 |
-
st.session_state.fig.data[i].x = data["x"]
|
329 |
-
st.session_state.fig.data[i].y = data["y"]
|
330 |
-
st.session_state.fig.data[i].marker.size = data["size"]
|
331 |
-
|
332 |
-
# Update total population
|
333 |
-
total_population = [sum(counts) for counts in zip(*population_history.values())]
|
334 |
-
st.session_state.fig.data[len(cell_data)].y = total_population
|
335 |
-
|
336 |
-
# Update population by cell type
|
337 |
-
for i, (cell_type, counts) in enumerate(population_history.items()):
|
338 |
-
st.session_state.fig.data[len(cell_data) + 1 + i].y = counts
|
339 |
-
|
340 |
-
# Update organelle distribution
|
341 |
-
organelle_counts = {"nucleus": 0, "mitochondria": 0, "chloroplast": 0, "endoplasmic_reticulum": 0, "golgi_apparatus": 0}
|
342 |
-
for cell in st.session_state.env.cells:
|
343 |
-
for organelle in cell.organelles:
|
344 |
-
organelle_counts[organelle] += 1
|
345 |
-
st.session_state.fig.data[-1].y = list(organelle_counts.values())
|
346 |
-
|
347 |
-
# Update live statistics
|
348 |
-
total_cells = len(st.session_state.env.cells)
|
349 |
-
total_cells_text.text(f"Total Cells: {format_number(total_cells)}")
|
350 |
-
|
351 |
-
cell_type_counts = {cell_type: len([c for c in st.session_state.env.cells if c.cell_type == cell_type]) for cell_type in st.session_state.env.population_history.keys()}
|
352 |
-
cell_type_breakdown.text("Cell Type Breakdown:\n" + "\n".join([f"{cell_type}: {format_number(count)}" for cell_type, count in cell_type_counts.items()]))
|
353 |
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
|
|
359 |
|
360 |
-
|
|
|
|
|
361 |
|
362 |
-
# Update
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
#
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
while True:
|
378 |
-
|
|
|
|
|
|
296 |
|
297 |
start_stop_button = st.button("Start/Stop Simulation", on_click=toggle_simulation)
|
298 |
|
299 |
+
def run_simulation_step():
|
300 |
+
if st.session_state.running and st.session_state.env is not None:
|
301 |
+
for _ in range(4): # Update 4 times per frame to increase simulation speed
|
302 |
+
initial_cell_count = len(st.session_state.env.cells)
|
303 |
+
st.session_state.env.update()
|
304 |
+
final_cell_count = len(st.session_state.env.cells)
|
305 |
+
|
306 |
+
# Check for merges
|
307 |
+
if final_cell_count < initial_cell_count:
|
308 |
+
merges = initial_cell_count - final_cell_count
|
309 |
+
st.session_state.total_merges += merges
|
310 |
+
event_log.appendleft(f"Time {st.session_state.env.time}: {merges} cell merge(s) occurred!")
|
311 |
+
|
312 |
+
# Check for new cell types
|
313 |
+
for cell in st.session_state.env.cells:
|
314 |
+
if cell.cell_type not in st.session_state.env.population_history:
|
315 |
+
event_log.appendleft(f"Time {st.session_state.env.time}: New cell type '{cell.cell_type}' emerged!")
|
316 |
+
|
317 |
+
update_chart()
|
|
|
318 |
|
319 |
def update_chart():
|
320 |
+
if st.session_state.running and st.session_state.fig is not None:
|
321 |
+
with st.session_state.fig.batch_update():
|
322 |
+
cell_data, population_history, colors = st.session_state.env.get_visualization_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
+
# Update cell distribution
|
325 |
+
for i, (cell_type, data) in enumerate(cell_data.items()):
|
326 |
+
st.session_state.fig.data[i].x = data["x"]
|
327 |
+
st.session_state.fig.data[i].y = data["y"]
|
328 |
+
st.session_state.fig.data[i].marker.size = data["size"]
|
329 |
|
330 |
+
# Update total population
|
331 |
+
total_population = [sum(counts) for counts in zip(*population_history.values())]
|
332 |
+
st.session_state.fig.data[len(cell_data)].y = total_population
|
333 |
|
334 |
+
# Update population by cell type
|
335 |
+
for i, (cell_type, counts) in enumerate(population_history.items()):
|
336 |
+
st.session_state.fig.data[len(cell_data) + 1 + i].y = counts
|
337 |
|
338 |
+
# Update organelle distribution
|
339 |
+
organelle_counts = {"nucleus": 0, "mitochondria": 0, "chloroplast": 0, "endoplasmic_reticulum": 0, "golgi_apparatus": 0}
|
340 |
+
for cell in st.session_state.env.cells:
|
341 |
+
for organelle in cell.organelles:
|
342 |
+
organelle_counts[organelle] += 1
|
343 |
+
st.session_state.fig.data[-1].y = list(organelle_counts.values())
|
344 |
+
|
345 |
+
# Update live statistics
|
346 |
+
total_cells = len(st.session_state.env.cells)
|
347 |
+
total_cells_text.text(f"Total Cells: {format_number(total_cells)}")
|
348 |
+
|
349 |
+
cell_type_counts = {cell_type: len([c for c in st.session_state.env.cells if c.cell_type == cell_type]) for cell_type in st.session_state.env.population_history.keys()}
|
350 |
+
cell_type_breakdown.text("Cell Type Breakdown:\n" + "\n".join([f"{cell_type}: {format_number(count)}" for cell_type, count in cell_type_counts.items()]))
|
351 |
+
|
352 |
+
dominant_type = max(cell_type_counts, key=cell_type_counts.get)
|
353 |
+
dominant_type_text.text(f"Dominant Type: {dominant_type}")
|
354 |
+
|
355 |
+
avg_energy = sum(cell.energy for cell in st.session_state.env.cells) / total_cells if total_cells > 0 else 0
|
356 |
+
avg_energy_text.text(f"Avg Energy: {avg_energy:.2f}")
|
357 |
+
|
358 |
+
total_merges_text.text(f"Total Merges: {st.session_state.total_merges}")
|
359 |
+
|
360 |
+
# Update event log
|
361 |
+
event_log_text.text("Recent Events:\n" + "\n".join(event_log))
|
362 |
+
|
363 |
+
chart_placeholder.plotly_chart(st.session_state.fig, use_container_width=True)
|
364 |
+
|
365 |
+
# Main loop
|
366 |
while True:
|
367 |
+
run_simulation_step()
|
368 |
+
time.sleep(update_interval)
|
369 |
+
st.experimental_rerun()
|