Spaces:
Sleeping
Sleeping
4kasha
commited on
Commit
•
a3ad19b
1
Parent(s):
302eb57
fix
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
-
import time
|
5 |
from scipy.ndimage import label
|
6 |
|
7 |
-
|
8 |
def create_initial_plot(grid_size):
|
9 |
grid = np.zeros((grid_size, grid_size))
|
10 |
fig = plt.figure(figsize=(15, 6))
|
@@ -12,62 +10,68 @@ def create_initial_plot(grid_size):
|
|
12 |
graph_ax = fig.add_subplot(122)
|
13 |
|
14 |
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
15 |
-
grid_ax.set_title(
|
16 |
-
f'Site Occupation Probability p = 0.00\n'
|
17 |
-
f'Largest Cluster Ratio = 0.000'
|
18 |
-
)
|
19 |
|
20 |
graph_ax.plot([0], [0], '-b', label='Largest Cluster Size')
|
21 |
graph_ax.set_xlabel('Occupation Probability p')
|
22 |
-
graph_ax.set_ylabel('
|
23 |
graph_ax.set_title('Phase Transition in 2D Percolation')
|
24 |
graph_ax.grid(True)
|
25 |
-
graph_ax.legend()
|
26 |
graph_ax.set_xlim(0, 1)
|
27 |
graph_ax.set_ylim(0, 1)
|
28 |
|
29 |
# plt.tight_layout()
|
30 |
return fig
|
31 |
|
32 |
-
def
|
33 |
labeled_array, num_features = label(grid)
|
34 |
if num_features == 0:
|
35 |
return np.zeros_like(grid), 0
|
36 |
|
37 |
sizes = [np.sum(labeled_array == i) for i in range(1, num_features + 1)]
|
38 |
-
if not sizes:
|
39 |
-
return np.zeros_like(grid), 0
|
40 |
|
41 |
max_cluster_index = np.argmax(sizes) + 1
|
42 |
max_cluster_mask = labeled_array == max_cluster_index
|
43 |
max_cluster_size = sizes[max_cluster_index - 1]
|
44 |
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
48 |
p_step = 0.02
|
49 |
fine_step = 0.01
|
50 |
|
51 |
-
p1 = np.arange(0, 0.56, p_step)
|
52 |
p2 = np.arange(0.56, 0.60 + fine_step, fine_step)
|
53 |
-
p3 = np.arange(0.60 + p_step,
|
54 |
steps = np.concatenate((p1, p2, p3))
|
55 |
|
56 |
p_values = []
|
57 |
-
|
58 |
|
59 |
fig = plt.figure(figsize=(15, 6))
|
60 |
grid_ax = fig.add_subplot(121)
|
61 |
graph_ax = fig.add_subplot(122)
|
62 |
|
63 |
-
np.random.seed(
|
64 |
for p in steps:
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
|
69 |
p_values.append(p)
|
70 |
-
|
71 |
|
72 |
grid_ax.clear()
|
73 |
graph_ax.clear()
|
@@ -75,17 +79,13 @@ def run_percolation_simulation(grid_size):
|
|
75 |
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
76 |
grid_ax.imshow(largest_cluster_mask, cmap='Blues', alpha=0.7)
|
77 |
|
78 |
-
grid_ax.set_title(
|
79 |
-
f'Site Occupation Probability p = {p:.2f}\n'
|
80 |
-
f'Largest Cluster Ratio = {largest_cluster_ratio:.3f}'
|
81 |
-
)
|
82 |
|
83 |
-
graph_ax.plot(p_values,
|
84 |
graph_ax.set_xlabel('Occupation Probability p')
|
85 |
-
graph_ax.set_ylabel('
|
86 |
graph_ax.set_title('Phase Transition in 2D Percolation')
|
87 |
graph_ax.grid(True)
|
88 |
-
graph_ax.legend()
|
89 |
graph_ax.set_xlim(0, 1)
|
90 |
graph_ax.set_ylim(0, 1)
|
91 |
|
@@ -93,8 +93,6 @@ def run_percolation_simulation(grid_size):
|
|
93 |
|
94 |
yield fig
|
95 |
|
96 |
-
time.sleep(0.25)
|
97 |
-
|
98 |
with gr.Blocks() as demo:
|
99 |
gr.Markdown("# 2D Site Percolation Simulation")
|
100 |
gr.Markdown("""
|
@@ -103,6 +101,7 @@ with gr.Blocks() as demo:
|
|
103 |
|
104 |
- Gray dots: Occupied sites
|
105 |
- Blue region: Largest connected cluster
|
|
|
106 |
""")
|
107 |
|
108 |
with gr.Row():
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
|
|
4 |
from scipy.ndimage import label
|
5 |
|
|
|
6 |
def create_initial_plot(grid_size):
|
7 |
grid = np.zeros((grid_size, grid_size))
|
8 |
fig = plt.figure(figsize=(15, 6))
|
|
|
10 |
graph_ax = fig.add_subplot(122)
|
11 |
|
12 |
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
13 |
+
grid_ax.set_title(f'Site Occupation Probability p = 0.00')
|
|
|
|
|
|
|
14 |
|
15 |
graph_ax.plot([0], [0], '-b', label='Largest Cluster Size')
|
16 |
graph_ax.set_xlabel('Occupation Probability p')
|
17 |
+
graph_ax.set_ylabel('Spanning Cluster Size Ratio')
|
18 |
graph_ax.set_title('Phase Transition in 2D Percolation')
|
19 |
graph_ax.grid(True)
|
|
|
20 |
graph_ax.set_xlim(0, 1)
|
21 |
graph_ax.set_ylim(0, 1)
|
22 |
|
23 |
# plt.tight_layout()
|
24 |
return fig
|
25 |
|
26 |
+
def get_largest_cluster_spanning_size(grid):
|
27 |
labeled_array, num_features = label(grid)
|
28 |
if num_features == 0:
|
29 |
return np.zeros_like(grid), 0
|
30 |
|
31 |
sizes = [np.sum(labeled_array == i) for i in range(1, num_features + 1)]
|
|
|
|
|
32 |
|
33 |
max_cluster_index = np.argmax(sizes) + 1
|
34 |
max_cluster_mask = labeled_array == max_cluster_index
|
35 |
max_cluster_size = sizes[max_cluster_index - 1]
|
36 |
|
37 |
+
perc_x = np.intersect1d(labeled_array[0,:],labeled_array[-1,:])
|
38 |
+
perc_x = perc_x[np.where(perc_x>0)]
|
39 |
+
# perc_y = np.intersect1d(labeled_array[0,:],labeled_array[-1,:])
|
40 |
+
# perc_y = perc_y[np.where(perc_y>0)]
|
41 |
|
42 |
+
if len(perc_x)>0:# or len(perc_y)>0: # spanning cond.
|
43 |
+
return max_cluster_mask, max_cluster_size
|
44 |
+
else:
|
45 |
+
return max_cluster_mask, 0
|
46 |
+
|
47 |
+
def run_percolation_simulation(grid_size, num_samples=10):
|
48 |
p_step = 0.02
|
49 |
fine_step = 0.01
|
50 |
|
51 |
+
p1 = np.arange(0, 0.56, p_step*2)
|
52 |
p2 = np.arange(0.56, 0.60 + fine_step, fine_step)
|
53 |
+
p3 = np.arange(0.60 + p_step, 0.98 + p_step, p_step)
|
54 |
steps = np.concatenate((p1, p2, p3))
|
55 |
|
56 |
p_values = []
|
57 |
+
spanning_clusters = []
|
58 |
|
59 |
fig = plt.figure(figsize=(15, 6))
|
60 |
grid_ax = fig.add_subplot(121)
|
61 |
graph_ax = fig.add_subplot(122)
|
62 |
|
63 |
+
np.random.seed(42)
|
64 |
for p in steps:
|
65 |
+
total_spanning_cluster_size = 0
|
66 |
+
for _ in range(num_samples):
|
67 |
+
grid = np.random.random((grid_size, grid_size)) < p
|
68 |
+
largest_cluster_mask, spanning_cluster_size = get_largest_cluster_spanning_size(grid)
|
69 |
+
total_spanning_cluster_size += spanning_cluster_size
|
70 |
+
|
71 |
+
spanning_cluster_ratio = total_spanning_cluster_size / (num_samples * grid_size * grid_size)
|
72 |
|
73 |
p_values.append(p)
|
74 |
+
spanning_clusters.append(spanning_cluster_ratio)
|
75 |
|
76 |
grid_ax.clear()
|
77 |
graph_ax.clear()
|
|
|
79 |
grid_ax.imshow(grid, cmap='Greys', alpha=0.3)
|
80 |
grid_ax.imshow(largest_cluster_mask, cmap='Blues', alpha=0.7)
|
81 |
|
82 |
+
grid_ax.set_title(f'Site Occupation Probability p = {p:.2f}')
|
|
|
|
|
|
|
83 |
|
84 |
+
graph_ax.plot(p_values, spanning_clusters, '-b', label='Largest Cluster Size')
|
85 |
graph_ax.set_xlabel('Occupation Probability p')
|
86 |
+
graph_ax.set_ylabel('Spanning Cluster Size Ratio')
|
87 |
graph_ax.set_title('Phase Transition in 2D Percolation')
|
88 |
graph_ax.grid(True)
|
|
|
89 |
graph_ax.set_xlim(0, 1)
|
90 |
graph_ax.set_ylim(0, 1)
|
91 |
|
|
|
93 |
|
94 |
yield fig
|
95 |
|
|
|
|
|
96 |
with gr.Blocks() as demo:
|
97 |
gr.Markdown("# 2D Site Percolation Simulation")
|
98 |
gr.Markdown("""
|
|
|
101 |
|
102 |
- Gray dots: Occupied sites
|
103 |
- Blue region: Largest connected cluster
|
104 |
+
- Spanning Cluster: A cluster is spanning from one side to another
|
105 |
""")
|
106 |
|
107 |
with gr.Row():
|