-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cellular_Automata_Tumor_new.py
469 lines (369 loc) · 20.2 KB
/
Cellular_Automata_Tumor_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import statistics
from matplotlib.colors import ListedColormap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import Scale, Button, ttk
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.figure import Figure
from reportlab.pdfgen import canvas
from io import BytesIO
from matplotlib.colors import Normalize
class TumorSimulatorApp:
def __init__(self, master):
self.master = master
master.title("Tumor Simulator")
# Notebook for organizing sections
self.notebook = ttk.Notebook(master)
self.notebook.pack(fill=tk.BOTH, expand=1)
# Additional variable for storing the graphs plot
self.graphs_plot = None
# First tab for tumor simulation
self.tumor_tab = ttk.Frame(self.notebook)
self.notebook.add(self.tumor_tab, text='Tumor Simulation')
# Second tab for simulation controls
self.controls_tab = ttk.Frame(self.notebook)
self.notebook.add(self.controls_tab, text='Simulation Controls')
self.tumor_size = 20
self.speed = 100 # Initial speed (milliseconds)
self.generations = int(60000 / self.speed)
self.tumor_growth_probability = 0.01 # Initial tumor growth probability
self.tumor_death_probability = 0.34 # Initial tumor cell death probability
self.living_death_probability = 0.009 # Initial living cell death probability
self.regeneration_probability = 0.1 # Initial regeneration probability
self.initial_tumor = self.initialize_tumor(self.tumor_size)
# Analytical solution variables
self.analytical_solution_time_points = []
self.analytical_solution_tumor_sizes = []
# Simulation solution variables
self.simulation_time_points = []
self.simulation_tumor_sizes = []
# Tumor simulation tab
self.fig, self.ax = plt.subplots()
self.rectangles = []
self.monte_carlo_results = []
# ...
self.monte_carlo_button = Button(self.controls_tab, text="Run Monte Carlo Simulation (5 runs)",
command=self.run_monte_carlo_simulation)
self.monte_carlo_button.pack()
for i in range(self.tumor_size):
for j in range(self.tumor_size):
rect = patches.Rectangle((i - 0.5, j - 0.5), 1, 1, edgecolor='pink', linewidth=1, facecolor='none')
self.rectangles.append(rect)
self.ax.add_patch(rect)
self.img = self.ax.imshow(self.initial_tumor, cmap='Reds', interpolation='none', vmin=0, vmax=2)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.tumor_tab)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# Simulation controls tab
self.speed_scale = Scale(self.controls_tab, from_=50, to=1000, orient=tk.HORIZONTAL, label="Speed", length=300,
resolution=50, command=self.update_speed)
self.speed_scale.set(self.speed)
self.speed_scale.pack()
self.probability_scale = Scale(self.controls_tab, from_=0, to=1, orient=tk.HORIZONTAL,
label="Tumor Growth Probability", length=300, resolution=0.01,
command=self.update_probability)
self.probability_scale.set(self.tumor_growth_probability)
self.probability_scale.pack()
self.death_probability_scale = Scale(self.controls_tab, from_=0, to=1, orient=tk.HORIZONTAL,
label="Tumor Cell Death Probability", length=300, resolution=0.01,
command=self.update_death_probability)
self.death_probability_scale.set(self.tumor_death_probability)
self.death_probability_scale.pack()
self.living_death_probability_scale = Scale(self.controls_tab, from_=0, to=1, orient=tk.HORIZONTAL,
label="Living Cell Death Probability", length=300, resolution=0.01,
command=self.update_living_death_probability)
self.living_death_probability_scale.set(self.living_death_probability)
self.living_death_probability_scale.pack()
self.regeneration_probability_scale = Scale(self.controls_tab, from_=0, to=1, orient=tk.HORIZONTAL,
label="Regeneration Probability", length=300, resolution=0.01,
command=self.update_regeneration_probability)
self.regeneration_probability_scale.set(self.regeneration_probability)
self.regeneration_probability_scale.pack()
self.start_button = Button(self.controls_tab, text="Start Simulation", command=self.start_simulation)
self.start_button.pack()
self.show_graphs_button = Button(self.controls_tab, text="Show Graphs", command=self.show_graphs)
self.show_graphs_button.pack()
self.reset_button = Button(self.controls_tab, text="Reset Simulation", command=self.reset_simulation)
self.reset_button.pack()
self.animation = None
self.tumor_counts = {'tumor_cells': [], 'dead_tumor_cells': [], 'living_cells': []}
def initialize_tumor(self, size):
tumor = np.zeros((size, size))
random_row, random_col = np.random.randint(0, size), np.random.randint(0, size)
tumor[random_row, random_col] = 2
tumor[tumor == 4] = 0
return tumor
def update_tumor(self, tumor, frame):
new_tumor = np.copy(tumor)
rows, cols = tumor.shape
test = np.copy(tumor)
for i in range(rows):
for j in range(cols):
if new_tumor[i, j] == 2:
if new_tumor[i, j] == 2 and np.random.rand() < self.tumor_death_probability:
test[i, j] = 3
# if np.random.rand() < self.tumor_growth_probability:
# direction = np.random.choice(['N', 'S', 'W', 'E', 'NE', 'NW', 'SE', 'SW'])
# if 'N' in direction and i - 1 >= 0:
# if new_tumor[i - 1, j] == 0:
# test[i - 1, j] = 2
# if 'S' in direction and i + 1 < rows:
# if new_tumor[i + 1, j] == 0:
# test[i + 1, j] = 2
# if 'W' in direction and j - 1 >= 0:
# if new_tumor[i, j - 1] == 0:
# test[i, j - 1] = 2
# if 'E' in direction and j + 1 < cols:
# if new_tumor[i, j + 1] == 0:
# test[i, j + 1] = 2
# if 'NE' in direction and i - 1 >= 0 and j + 1 < cols:
# if new_tumor[i - 1, j + 1] == 0:
# test[i - 1, j + 1] = 2
# if 'NW' in direction and i - 1 >= 0 and j - 1 >= 0:
# if new_tumor[i - 1, j - 1] == 0:
# test[i - 1, j - 1] = 2
# if 'SE' in direction and i + 1 < rows and j + 1 < cols:
# if new_tumor[i + 1, j + 1] == 0:
# test[i + 1, j + 1] = 2
# if 'SW' in direction and i + 1 < rows and j - 1 >= 0:
# if new_tumor[i + 1, j - 1] == 0:
# test[i + 1, j - 1] = 2
elif new_tumor[i, j] == 0:
neighbour=0
if i - 1 >= 0:
if new_tumor[i - 1, j] == 2:
neighbour=neighbour+1
if i + 1 < rows:
if new_tumor[i + 1, j] == 2:
neighbour=neighbour+1
if j - 1 >= 0:
if new_tumor[i, j - 1] == 2:
neighbour=neighbour+1
if j + 1 < cols:
if new_tumor[i, j + 1] == 2:
neighbour=neighbour+1
if i - 1 >= 0 and j + 1 < cols:
if new_tumor[i - 1, j + 1] == 2:
neighbour=neighbour+1
if i - 1 >= 0 and j - 1 >= 0:
if new_tumor[i - 1, j - 1] == 2:
neighbour=neighbour+1
if i + 1 < rows and j + 1 < cols:
if new_tumor[i + 1, j + 1] == 2:
neighbour=neighbour+1
if i + 1 < rows and j - 1 >= 0:
if new_tumor[i + 1, j - 1] == 2:
neighbour=neighbour+1
if neighbour != 0:
neighbour_effect = self.tumor_growth_probability + (neighbour/8)
mapped_specific_probability_tumor = (neighbour_effect - self.tumor_growth_probability) * (1-self.tumor_growth_probability) + self.tumor_growth_probability
if np.random.rand() < mapped_specific_probability_tumor:
test[i, j] = 2
elif np.random.rand() < self.living_death_probability:
test[i, j] = 4
elif new_tumor[i, j] == 4 or new_tumor[i, j] == 3:
if np.random.rand() < self.regeneration_probability:
test[i, j] = 0
return test
def animate_tumor(self, frame):
new_tumor = self.update_tumor(self.initial_tumor, frame)
changed_indices = np.where(new_tumor != self.initial_tumor)
for i, j in zip(changed_indices[0], changed_indices[1]):
if new_tumor[i, j] == 3:
self.rectangles[i * self.tumor_size + j].set_edgecolor('black')
self.rectangles[i * self.tumor_size + j].set_facecolor('black')
if new_tumor[i, j] == 4:
self.rectangles[i * self.tumor_size + j].set_edgecolor('black')
self.rectangles[i * self.tumor_size + j].set_facecolor('blue')
if new_tumor[i, j] == 0:
self.rectangles[i * self.tumor_size + j].set_edgecolor('black')
self.rectangles[i * self.tumor_size + j].set_facecolor('white')
if new_tumor[i, j] == 2:
self.rectangles[i * self.tumor_size + j].set_edgecolor('black')
self.rectangles[i * self.tumor_size + j].set_facecolor('red')
self.rectangles[i * self.tumor_size + j].set_xy((j - 0.5, i - 0.5))
tumor_count = np.count_nonzero(new_tumor == 2)
dead_tumor_count = np.count_nonzero(new_tumor == 3)
living_cells_count = np.count_nonzero(new_tumor == 0)
self.tumor_counts['tumor_cells'].append(tumor_count)
self.tumor_counts['dead_tumor_cells'].append(dead_tumor_count)
self.tumor_counts['living_cells'].append(living_cells_count)
self.initial_tumor = new_tumor
self.img.set_array(self.initial_tumor)
return self.img, *self.rectangles
def end_simulation(self):
fig, ax = plt.subplots()
frames = np.arange(1, min(self.generations + 1, len(self.tumor_counts['tumor_cells']) + 1))
ax.plot(frames, self.tumor_counts['tumor_cells'][:len(frames)], label='Tumor Cells')
ax.plot(frames, self.tumor_counts['dead_tumor_cells'][:len(frames)], label='Dead Tumor Cells')
ax.plot(frames, self.tumor_counts['living_cells'][:len(frames)], label='Living Cells')
ax.set_xlabel('Generation')
ax.set_ylabel('Cell Count')
ax.legend()
plt.show()
def start_simulation(self):
if self.animation is not None:
self.animation.event_source.stop()
self.animation = animation.FuncAnimation(self.fig, self.animate_tumor, frames=self.generations,
interval=self.speed, blit=True)
def reset_simulation(self):
if self.animation is not None:
self.animation.event_source.stop()
self.initial_tumor = self.initialize_tumor(self.tumor_size)
self.img.set_array(self.initial_tumor)
for i, rect in enumerate(self.rectangles):
row, col = divmod(i, self.tumor_size)
rect.set_xy((col - 0.5, row - 0.5))
rect.set_edgecolor('pink')
rect.set_facecolor('none')
self.tumor_counts = {'tumor_cells': [], 'dead_tumor_cells': [], 'living_cells': []}
self.canvas.draw()
def show_graphs(self):
if self.graphs_plot is not None:
self.graphs_plot.destroy() # Close the existing plot if any
fig, ax = plt.subplots()
frames = np.arange(1, len(self.tumor_counts['tumor_cells']) + 1)
ax.plot(frames, self.tumor_counts['tumor_cells'], label='Tumor Cells')
ax.plot(frames, self.tumor_counts['dead_tumor_cells'], label='Dead Tumor Cells')
ax.plot(frames, self.tumor_counts['living_cells'], label='Living Cells')
ax.set_xlabel('Generation')
ax.set_ylabel('Cell Count')
ax.legend()
# Create a new window for the graphs plot
self.graphs_plot = tk.Toplevel(self.master)
self.graphs_plot.wm_title("Simulation Graphs")
self.graphs_plot.protocol("WM_DELETE_WINDOW", self.close_graphs_plot)
canvas = FigureCanvasTkAgg(fig, master=self.graphs_plot)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def close_graphs_plot(self):
if self.graphs_plot is not None:
self.graphs_plot.destroy()
self.graphs_plot = None
def update_speed(self, val):
self.speed = int(val)
if self.animation is not None:
self.animation.event_source.interval = self.speed
def update_probability(self, val):
self.tumor_growth_probability = float(val)
def update_death_probability(self, val):
self.tumor_death_probability = float(val)
def update_living_death_probability(self, val):
self.living_death_probability = float(val)
def update_regeneration_probability(self, val):
self.regeneration_probability = float(val)
def compute_analytical_solution(self):
# Reset previous analytical solution data
self.analytical_solution_time_points = []
self.analytical_solution_tumor_sizes = []
# Initial conditions
tn = 0
Pn = 1 # Initial tumor size
# Time step for Euler's method
delta_t = 1
# Number of time steps (adjust as needed)
num_steps = 100
for _ in range(num_steps):
# Update time and tumor size using Euler's method
tn = tn + delta_t
Pn = Pn + self.tumor_growth_probability * Pn * delta_t - self.tumor_death_probability * Pn * delta_t
# Store results for plotting
self.analytical_solution_time_points.append(tn)
self.analytical_solution_tumor_sizes.append(Pn)
# Plot the analytical solution
plt.figure()
plt.plot(self.analytical_solution_time_points, self.analytical_solution_tumor_sizes, label='Analytical Solution')
plt.xlabel('Time')
plt.ylabel('Tumor Size')
plt.title('Analytical Solution using Euler\'s Method')
plt.legend()
plt.show()
def run_monte_carlo_simulation(self):
pdf_buffer = BytesIO()
pdf_pages = PdfPages(pdf_buffer)
results = []
for run_index in range(10):
tumor_counts = {'tumor_cells': [], 'dead_tumor_cells': [], 'living_cells': []}
initial_tumor = self.initialize_tumor(self.tumor_size)
for frame in range(self.generations):
new_tumor = self.update_tumor(initial_tumor, frame)
tumor_count = np.count_nonzero(new_tumor == 2)
dead_tumor_count = np.count_nonzero(new_tumor == 3)
living_cells_count = np.count_nonzero(new_tumor == 0)
tumor_counts['tumor_cells'].append(tumor_count)
tumor_counts['dead_tumor_cells'].append(dead_tumor_count)
tumor_counts['living_cells'].append(living_cells_count)
initial_tumor = new_tumor
results.append(tumor_counts)
# Generate visualizations for each run
self.generate_visualizations(run_index, initial_tumor, tumor_counts, pdf_pages)
# Generate summary page
self.generate_summary_page(results, pdf_pages)
# Close the PdfPages instance before saving the buffer to a file
pdf_pages.close()
# Save the PDF buffer to a file or display as needed
with open('monte_carlo_results.pdf', 'wb') as pdf_file:
pdf_file.write(pdf_buffer.getvalue())
pdf_buffer.close()
def generate_visualizations(self, run_index, final_grid, tumor_counts, pdf_pages):
# Generate visualizations and add to the PDF
plt.figure()
# Create a custom colormap for visualization
custom_cmap = ListedColormap(['white','white','red','black','blue'])
plt.imshow(final_grid, cmap=custom_cmap, interpolation='none')
plt.title(f'Final Grid - Run {run_index + 1}')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
pdf_pages.savefig()
plt.close()
plt.figure()
plt.plot(tumor_counts['tumor_cells'], label='Tumor Cells')
plt.plot(tumor_counts['dead_tumor_cells'], label='Dead Tumor Cells')
plt.plot(tumor_counts['living_cells'], label='Living Cells')
plt.xlabel('Generation')
plt.ylabel('Cell Count')
plt.title(f'Run {run_index + 1} - Cell Counts Over Generations')
plt.legend()
pdf_pages.savefig()
plt.close()
def generate_summary_page(self, results, pdf_pages):
# Extract the tumor cell counts at the end of each run
end_tumor_counts = [result['tumor_cells'][-1] for result in results]
# Calculate mean and standard deviation
mean_end_tumor_count = statistics.mean(end_tumor_counts)
std_dev_end_tumor_count = statistics.stdev(end_tumor_counts)
# Create a summary figure
plt.figure()
plt.bar(range(len(end_tumor_counts)), end_tumor_counts, color='lightblue', label='Tumor Cells at End of Run')
plt.axhline(mean_end_tumor_count, color='red', linestyle='dashed', linewidth=2, label='Mean')
plt.xlabel('Run Index')
plt.ylabel('Cell Count')
plt.title('Summary - Tumor Cells at End of Each Run')
plt.legend()
# Save the summary figure to the PDF
pdf_pages.savefig()
plt.close()
# Display mean and standard deviation
print(f"Mean of Tumor Cells at End: {mean_end_tumor_count}")
print(f"Standard Deviation of Tumor Cells at End: {std_dev_end_tumor_count}")
# Add numerical results to the next page
self.add_numerical_results_page(mean_end_tumor_count, std_dev_end_tumor_count, pdf_pages)
def add_numerical_results_page(self, mean_value, std_dev_value, pdf_pages):
# Create a new page for numerical results
plt.figure()
plt.text(0.5, 0.5, f"Mean of Tumor Cells at End: {mean_value}\nStandard Deviation of Tumor Cells at End: {std_dev_value}",
ha='center', va='center', fontsize=12)
plt.axis('off')
plt.title('Numerical Results')
# Save the numerical results page to the PDF
pdf_pages.savefig()
plt.close()
if __name__ == "__main__":
root = tk.Tk()
app = TumorSimulatorApp(root)
root.mainloop()