prakharg24 commited on
Commit
2e9959e
·
verified ·
1 Parent(s): b1c232b

Update my_pages/ica.py

Browse files
Files changed (1) hide show
  1. my_pages/ica.py +53 -65
my_pages/ica.py CHANGED
@@ -3,82 +3,70 @@ import matplotlib.pyplot as plt
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.")
 
 
 
 
 
 
 
 
 
3
  import numpy as np
4
 
5
  def render():
6
+ st.title("Intentional / Conventional / Arbitrary Triangle")
7
 
8
+ # Initialize state for slider values
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 for each dimension
15
+ new_intentional = st.slider("Intentional", 0.0, 1.0, st.session_state.intentional, 0.01)
16
+ new_conventional = st.slider("Conventional", 0.0, 1.0, st.session_state.conventional, 0.01)
17
+ new_arbitrary = st.slider("Arbitrary", 0.0, 1.0, st.session_state.arbitrary, 0.01)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Update button
20
+ if st.button("Update Point"):
21
+ total = new_intentional + new_conventional + new_arbitrary
22
+ if total == 0:
23
+ total = 1 # avoid div by zero
24
+ st.session_state.intentional = new_intentional / total
25
+ st.session_state.conventional = new_conventional / total
26
+ st.session_state.arbitrary = new_arbitrary / total
27
 
28
+ # Plot triangle
29
+ fig, ax = plt.subplots(figsize=(5, 5))
30
 
31
+ # Triangle vertices
32
+ vertices = np.array([[0.5, np.sqrt(3)/2], [0, 0], [1, 0]]) # Intentional, Conventional, Arbitrary
33
+ labels = ["Intentional", "Conventional", "Arbitrary"]
34
 
35
+ # Draw triangle
36
+ ax.plot([vertices[0][0], vertices[1][0]], [vertices[0][1], vertices[1][1]], 'k-')
37
+ ax.plot([vertices[1][0], vertices[2][0]], [vertices[1][1], vertices[2][1]], 'k-')
38
+ ax.plot([vertices[2][0], vertices[0][0]], [vertices[2][1], vertices[0][1]], 'k-')
 
39
 
40
+ # Add labels
41
+ for i, label in enumerate(labels):
42
+ ax.text(vertices[i][0], vertices[i][1] + 0.05, label, ha='center', fontsize=12, fontweight='bold')
 
 
 
43
 
44
+ # Convert (intentional, conventional, arbitrary) to XY coords
45
  point = (
46
+ st.session_state.conventional + 0.5 * st.session_state.intentional,
47
+ (np.sqrt(3)/2) * st.session_state.intentional
 
48
  )
49
 
50
+ # Plot point
51
+ ax.scatter(point[0], point[1], color='red', s=100, zorder=5)
 
 
 
 
 
 
 
 
52
 
53
+ ax.set_aspect('equal')
54
+ ax.axis('off')
55
  st.pyplot(fig)
56
 
57
+ # Display property based on dominant dimension
58
+ dominant = max(
59
+ ("Intentional", st.session_state.intentional),
60
+ ("Conventional", st.session_state.conventional),
61
+ ("Arbitrary", st.session_state.arbitrary),
62
+ key=lambda x: x[1]
63
+ )[0]
64
+
65
+ descriptions = {
66
+ "Intentional": "Example: Carefully designed rules.",
67
+ "Conventional": "Example: Following industry standards.",
68
+ "Arbitrary": "Example: Random seeds."
69
+ }
70
+
71
+ st.subheader(f"Dominant property: {dominant}")
72
+ st.write(descriptions[dominant])