Spaces:
Runtime error
Runtime error
GitHub Copilot
commited on
Commit
·
aeb7110
1
Parent(s):
f6e608d
Feature: Replace Sankey with Factorization Tree (Sunburst) visualization
Browse files
app.py
CHANGED
|
@@ -55,34 +55,77 @@ def get_gpf(n):
|
|
| 55 |
n //= i
|
| 56 |
return n
|
| 57 |
|
| 58 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
return fig
|
| 87 |
|
| 88 |
def visualize_prime_network(max_integer, show_links):
|
|
@@ -237,12 +280,12 @@ with gr.Blocks(theme=gr.themes.Monochrome(), title="LOGOS SPCW Protocol") as dem
|
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
with gr.Column():
|
| 240 |
-
plot_flow = gr.Plot(label="
|
| 241 |
with gr.Column():
|
| 242 |
plot_counts = gr.Plot(label="Composite Density")
|
| 243 |
|
| 244 |
btn_viz.click(visualize_prime_network, [max_int, show_links], plot_radial)
|
| 245 |
-
btn_viz.click(
|
| 246 |
btn_viz.click(visualize_gpf_counts, [max_int], plot_counts)
|
| 247 |
|
| 248 |
with gr.Tab("The Machine Shop (DSP Bridge)"):
|
|
|
|
| 55 |
n //= i
|
| 56 |
return n
|
| 57 |
|
| 58 |
+
def get_prime_factors(n):
|
| 59 |
+
"""Returns list of prime factors."""
|
| 60 |
+
factors = []
|
| 61 |
+
d = 2
|
| 62 |
+
while d * d <= n:
|
| 63 |
+
while n % d == 0:
|
| 64 |
+
factors.append(d)
|
| 65 |
+
n //= d
|
| 66 |
+
d += 1
|
| 67 |
+
if n > 1:
|
| 68 |
+
factors.append(n)
|
| 69 |
+
return factors
|
| 70 |
+
|
| 71 |
+
def visualize_factorization_tree(max_n=60):
|
| 72 |
"""
|
| 73 |
+
Factorization Tree: Primes are roots, composites branch from their smallest prime factor.
|
| 74 |
+
Uses Plotly Sunburst for hierarchical visualization.
|
| 75 |
"""
|
| 76 |
+
ids = ["Root"]
|
| 77 |
+
labels = ["ℤ"]
|
| 78 |
+
parents = [""]
|
| 79 |
+
values = [0]
|
| 80 |
+
colors = ["#1a1a2e"]
|
| 81 |
+
|
| 82 |
+
# Collect primes first
|
| 83 |
+
primes = [n for n in range(2, max_n + 1) if sympy.isprime(n)]
|
| 84 |
+
|
| 85 |
+
# Add primes as children of root
|
| 86 |
+
for p in primes:
|
| 87 |
+
ids.append(f"p_{p}")
|
| 88 |
+
labels.append(f"P({p})")
|
| 89 |
+
parents.append("Root")
|
| 90 |
+
values.append(1)
|
| 91 |
+
colors.append("#00ffea") # Cyan for primes
|
| 92 |
+
|
| 93 |
+
# Add composites as children of their smallest prime factor
|
| 94 |
+
for n in range(4, max_n + 1):
|
| 95 |
+
if not sympy.isprime(n):
|
| 96 |
+
factors = get_prime_factors(n)
|
| 97 |
+
smallest_prime = factors[0]
|
| 98 |
+
|
| 99 |
+
ids.append(f"c_{n}")
|
| 100 |
+
labels.append(f"{n}")
|
| 101 |
+
parents.append(f"p_{smallest_prime}")
|
| 102 |
+
values.append(1)
|
| 103 |
+
|
| 104 |
+
# Color by abundance (sum of divisors)
|
| 105 |
+
divisor_sum = sum(d for d in range(1, n) if n % d == 0)
|
| 106 |
+
if divisor_sum > n:
|
| 107 |
+
colors.append("#ff6b35") # Orange for Abundant
|
| 108 |
+
elif divisor_sum < n:
|
| 109 |
+
colors.append("#ff0055") # Red for Deficient
|
| 110 |
+
else:
|
| 111 |
+
colors.append("#9d4edd") # Purple for Perfect
|
| 112 |
+
|
| 113 |
+
fig = go.Figure(go.Sunburst(
|
| 114 |
+
ids=ids,
|
| 115 |
+
labels=labels,
|
| 116 |
+
parents=parents,
|
| 117 |
+
values=values,
|
| 118 |
+
branchvalues="total",
|
| 119 |
+
marker=dict(colors=colors, line=dict(color="#000", width=1)),
|
| 120 |
+
hovertemplate="<b>%{label}</b><br>Parent: %{parent}<extra></extra>"
|
| 121 |
+
))
|
| 122 |
+
|
| 123 |
+
fig.update_layout(
|
| 124 |
+
title="Prime Factorization Tree (Composites inherit from smallest prime factor)",
|
| 125 |
+
template="plotly_dark",
|
| 126 |
+
height=600,
|
| 127 |
+
margin=dict(t=50, l=10, r=10, b=10)
|
| 128 |
+
)
|
| 129 |
return fig
|
| 130 |
|
| 131 |
def visualize_prime_network(max_integer, show_links):
|
|
|
|
| 280 |
|
| 281 |
with gr.Row():
|
| 282 |
with gr.Column():
|
| 283 |
+
plot_flow = gr.Plot(label="Factorization Tree")
|
| 284 |
with gr.Column():
|
| 285 |
plot_counts = gr.Plot(label="Composite Density")
|
| 286 |
|
| 287 |
btn_viz.click(visualize_prime_network, [max_int, show_links], plot_radial)
|
| 288 |
+
btn_viz.click(visualize_factorization_tree, None, plot_flow)
|
| 289 |
btn_viz.click(visualize_gpf_counts, [max_int], plot_counts)
|
| 290 |
|
| 291 |
with gr.Tab("The Machine Shop (DSP Bridge)"):
|