prakharg24 commited on
Commit
917cd02
·
verified ·
1 Parent(s): d414164

Update my_pages/ica.py

Browse files
Files changed (1) hide show
  1. my_pages/ica.py +64 -56
my_pages/ica.py CHANGED
@@ -3,74 +3,82 @@ import matplotlib.pyplot as plt
3
  import numpy as np
4
 
5
  def render():
6
- st.title("Intentional / Conventional / Arbitrary Triangle")
7
 
8
- # Initialize session state for sliders
9
- if "intentional" not in st.session_state:
10
- st.session_state.intentional = 0.33
11
- st.session_state.conventional = 0.33
12
- st.session_state.arbitrary = 0.34
 
13
 
14
- # Sliders
15
- intentional = st.slider("Intentional", 0.0, 1.0, st.session_state.intentional, 0.01)
16
- conventional = st.slider("Conventional", 0.0, 1.0, st.session_state.conventional, 0.01)
17
- arbitrary = st.slider("Arbitrary", 0.0, 1.0, st.session_state.arbitrary, 0.01)
 
 
 
 
 
 
 
 
 
18
 
19
- # Normalize values so sum is always 1
20
- total = intentional + conventional + arbitrary
21
- if total == 0:
22
- total = 1
23
- intentional /= total
24
- conventional /= total
25
- arbitrary /= total
26
 
27
- # Store back in session state
28
- st.session_state.intentional = intentional
29
- st.session_state.conventional = conventional
30
- st.session_state.arbitrary = arbitrary
31
 
32
- # Plot triangle
33
- fig, ax = plt.subplots(figsize=(5, 5))
34
 
35
- # Triangle vertices (Intentional, Conventional, Arbitrary)
36
- vertices = np.array([[0.5, np.sqrt(3)/2], [0, 0], [1, 0]])
37
- labels = ["Intentional", "Conventional", "Arbitrary"]
38
 
39
- # Draw triangle edges
40
- ax.plot([vertices[0][0], vertices[1][0]], [vertices[0][1], vertices[1][1]], 'k-')
41
- ax.plot([vertices[1][0], vertices[2][0]], [vertices[1][1], vertices[2][1]], 'k-')
42
- ax.plot([vertices[2][0], vertices[0][0]], [vertices[2][1], vertices[0][1]], 'k-')
 
43
 
44
- # Add vertex labels
45
- for i, label in enumerate(labels):
46
- ax.text(vertices[i][0], vertices[i][1] + 0.05, label, ha='center', fontsize=12, fontweight='bold')
 
 
 
47
 
48
- # Convert (intentional, conventional, arbitrary) to XY coords
49
  point = (
50
- conventional + 0.5 * intentional,
51
- (np.sqrt(3)/2) * intentional
 
52
  )
53
 
54
- # Plot point
55
- ax.scatter(point[0], point[1], color='red', s=100, zorder=5)
 
 
 
 
 
 
 
 
56
 
57
- ax.set_aspect('equal')
58
- ax.axis('off')
59
  st.pyplot(fig)
60
 
61
- # Show dominant property description
62
- dominant = max(
63
- ("Intentional", intentional),
64
- ("Conventional", conventional),
65
- ("Arbitrary", arbitrary),
66
- key=lambda x: x[1]
67
- )[0]
68
-
69
- descriptions = {
70
- "Intentional": "Example: Carefully designed rules.",
71
- "Conventional": "Example: Following industry standards.",
72
- "Arbitrary": "Example: Random seeds."
73
- }
74
-
75
- st.subheader(f"Dominant property: {dominant}")
76
- st.write(descriptions[dominant])
 
3
  import numpy as np
4
 
5
  def render():
6
+ st.title("ICA Triangle")
7
 
8
+ if "weights" not in st.session_state:
9
+ st.session_state.weights = {
10
+ "Intentional": 0.33,
11
+ "Conventional": 0.33,
12
+ "Arbitrary": 0.34
13
+ }
14
 
15
+ # Which one to adjust
16
+ control_choice = st.radio(
17
+ "Select dimension to adjust",
18
+ ["Intentional", "Conventional", "Arbitrary"],
19
+ horizontal=True
20
+ )
21
+
22
+ # Current values
23
+ w = st.session_state.weights
24
+ current_value = w[control_choice]
25
+
26
+ # Slider for selected one
27
+ new_value = st.slider(control_choice, 0.0, 1.0, current_value, 0.01)
28
 
29
+ # Adjust others proportionally
30
+ diff = new_value - current_value
31
+ others = [k for k in w.keys() if k != control_choice]
32
+ total_other = w[others[0]] + w[others[1]]
 
 
 
33
 
34
+ if total_other > 0:
35
+ w[others[0]] -= diff * (w[others[0]] / total_other)
36
+ w[others[1]] -= diff * (w[others[1]] / total_other)
 
37
 
38
+ w[control_choice] = new_value
 
39
 
40
+ # Clamp small floating point errors
41
+ for k in w:
42
+ w[k] = max(0.0, min(1.0, round(w[k], 4)))
43
 
44
+ # Normalize back to sum=1
45
+ total = sum(w.values())
46
+ if total != 0:
47
+ for k in w:
48
+ w[k] = round(w[k] / total, 4)
49
 
50
+ # Triangle vertices
51
+ vertices = np.array([
52
+ [0.5, np.sqrt(3)/2], # Intentional
53
+ [0, 0], # Conventional
54
+ [1, 0] # Arbitrary
55
+ ])
56
 
57
+ # Point from barycentric coords
58
  point = (
59
+ w["Intentional"] * vertices[0] +
60
+ w["Conventional"] * vertices[1] +
61
+ w["Arbitrary"] * vertices[2]
62
  )
63
 
64
+ # Plot
65
+ fig, ax = plt.subplots()
66
+ ax.plot(*np.append(vertices, [vertices[0]], axis=0).T, 'k-')
67
+ ax.scatter(vertices[:,0], vertices[:,1], c=["blue", "green", "red"], s=100)
68
+ ax.text(*vertices[0], "Intentional", ha="center", va="bottom")
69
+ ax.text(*vertices[1], "Conventional", ha="right", va="top")
70
+ ax.text(*vertices[2], "Arbitrary", ha="left", va="top")
71
+ ax.scatter(point[0], point[1], c="orange", s=200, zorder=5)
72
+ ax.set_aspect("equal")
73
+ ax.axis("off")
74
 
 
 
75
  st.pyplot(fig)
76
 
77
+ # Display interpretation
78
+ closest = max(w.items(), key=lambda x: x[1])[0]
79
+ if closest == "Arbitrary":
80
+ st.info("Example: Random seeds — a truly arbitrary decision.")
81
+ elif closest == "Intentional":
82
+ st.info("Example: Ethical constraints — a fully intentional choice.")
83
+ elif closest == "Conventional":
84
+ st.info("Example: Industry standard preprocessing — a conventional decision.")