prakharg24 commited on
Commit
b1c232b
·
verified ·
1 Parent(s): 64eeded

Update my_pages/ica.py

Browse files
Files changed (1) hide show
  1. my_pages/ica.py +41 -47
my_pages/ica.py CHANGED
@@ -5,83 +5,77 @@ import numpy as np
5
  def render():
6
  st.title("ICA Triangle")
7
 
8
- # Initialize state
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
- st.write("Adjust one slider at a time. The others will update automatically so the total = 1.")
15
-
16
- # Choose which one to control
17
  control_choice = st.radio(
18
- "Which dimension do you want to adjust?",
19
  ["Intentional", "Conventional", "Arbitrary"],
20
  horizontal=True
21
  )
22
 
23
- # Get current values
24
- i = st.session_state.intentional
25
- c = st.session_state.conventional
26
- a = st.session_state.arbitrary
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- if control_choice == "Intentional":
29
- i = st.slider("Intentional", 0.0, 1.0, i, 0.01)
30
- remaining = 1.0 - i
31
- c = min(c, remaining)
32
- a = remaining - c
33
- elif control_choice == "Conventional":
34
- c = st.slider("Conventional", 0.0, 1.0, c, 0.01)
35
- remaining = 1.0 - c
36
- i = min(i, remaining)
37
- a = remaining - i
38
- elif control_choice == "Arbitrary":
39
- a = st.slider("Arbitrary", 0.0, 1.0, a, 0.01)
40
- remaining = 1.0 - a
41
- i = min(i, remaining)
42
- c = remaining - i
43
 
44
- # Save updated values
45
- st.session_state.intentional = round(i, 4)
46
- st.session_state.conventional = round(c, 4)
47
- st.session_state.arbitrary = round(a, 4)
48
 
49
- # Triangle vertices (equilateral)
 
 
 
 
 
 
50
  vertices = np.array([
51
  [0.5, np.sqrt(3)/2], # Intentional
52
  [0, 0], # Conventional
53
  [1, 0] # Arbitrary
54
  ])
55
 
56
- # Compute point position
57
  point = (
58
- st.session_state.intentional * vertices[0] +
59
- st.session_state.conventional * vertices[1] +
60
- st.session_state.arbitrary * vertices[2]
61
  )
62
 
63
  # Plot
64
  fig, ax = plt.subplots()
65
- ax.plot(*np.append(vertices, [vertices[0]], axis=0).T, 'k-') # Outline
66
  ax.scatter(vertices[:,0], vertices[:,1], c=["blue", "green", "red"], s=100)
67
  ax.text(*vertices[0], "Intentional", ha="center", va="bottom")
68
  ax.text(*vertices[1], "Conventional", ha="right", va="top")
69
  ax.text(*vertices[2], "Arbitrary", ha="left", va="top")
70
-
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
- # Explanation
78
- closest = max(
79
- [("Intentional", st.session_state.intentional),
80
- ("Conventional", st.session_state.conventional),
81
- ("Arbitrary", st.session_state.arbitrary)],
82
- key=lambda x: x[1]
83
- )[0]
84
-
85
  if closest == "Arbitrary":
86
  st.info("Example: Random seeds — a truly arbitrary decision.")
87
  elif closest == "Intentional":
 
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":