GitHub Copilot commited on
Commit
aeb7110
·
1 Parent(s): f6e608d

Feature: Replace Sankey with Factorization Tree (Sunburst) visualization

Browse files
Files changed (1) hide show
  1. app.py +71 -28
app.py CHANGED
@@ -55,34 +55,77 @@ def get_gpf(n):
55
  n //= i
56
  return n
57
 
58
- def visualize_potentiality_flow():
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  """
60
- Tab 1: Directed Graph (Sankey) showing Digit Constraints.
 
61
  """
62
- labels = ["Integer Stream"] + [f"Ends in {i}" for i in range(10)] + ["Composite Sink", "Prime Potential (P_n)"]
63
- sources, targets, values, colors = [], [], [], []
64
-
65
- # Layer 1: Stream -> Digits
66
- for i in range(10):
67
- sources.append(0); targets.append(i+1); values.append(10); colors.append("#444")
68
-
69
- # Layer 2: Digits -> Destination
70
- prime_lanes = [1, 3, 7, 9]
71
- for i in range(10):
72
- sources.append(i+1)
73
- if i in prime_lanes:
74
- targets.append(12) # Prime Potential
75
- colors.append("#00ffea") # Cyan
76
- else:
77
- targets.append(11) # Sink
78
- colors.append("#ff0055") # Red
79
- values.append(10)
80
-
81
- fig = go.Figure(data=[go.Sankey(
82
- node=dict(pad=15, thickness=20, line=dict(color="black", width=0.5), label=labels, color=["white"]+["#333"]*10+["#ff0055", "#00ffea"]),
83
- link=dict(source=sources, target=targets, value=values, color=colors)
84
- )])
85
- fig.update_layout(title="Prime Potentiality Flow (Mod 10 Constraints)", template="plotly_dark", height=600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="Potentiality Flow")
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(visualize_potentiality_flow, None, plot_flow)
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)"):