prakharg24 commited on
Commit
8ed1e68
·
verified ·
1 Parent(s): e549f99

Update my_pages/ica.py

Browse files
Files changed (1) hide show
  1. my_pages/ica.py +34 -33
my_pages/ica.py CHANGED
@@ -11,7 +11,7 @@ def render():
11
  add_instruction_text(
12
  """
13
  Explore the intention-convention-arbitrariness (ICA) framework.<br>
14
- Use the sliders to adjust the three dimensions.
15
  """
16
  )
17
 
@@ -23,15 +23,14 @@ def render():
23
  "Arbitrary": 0.34
24
  }
25
 
26
- # Keep track of previous weights
27
- if "prev_weights" not in st.session_state:
28
- st.session_state.prev_weights = st.session_state.weights.copy()
29
-
30
  w = st.session_state.weights
31
- prev_w = st.session_state.prev_weights
32
 
33
- # --- Three sliders ---
34
  col1, col2, col3 = st.columns(3)
 
 
 
 
35
  with col1:
36
  i_new = st.slider("Intentional", 0.0, 1.0, w["Intentional"], 0.01)
37
  with col2:
@@ -39,38 +38,40 @@ def render():
39
  with col3:
40
  a_new = st.slider("Arbitrary", 0.0, 1.0, w["Arbitrary"], 0.01)
41
 
42
- # --- Adjust other sliders proportionally ---
43
- # Detect which slider changed
44
  if i_new != prev_w["Intentional"]:
 
45
  diff = i_new - prev_w["Intentional"]
46
- total_other = w["Conventional"] + w["Arbitrary"]
47
- if total_other > 0:
48
- w["Conventional"] -= diff * (w["Conventional"] / total_other)
49
- w["Arbitrary"] -= diff * (w["Arbitrary"] / total_other)
50
- w["Intentional"] = i_new
51
-
52
  elif c_new != prev_w["Conventional"]:
 
53
  diff = c_new - prev_w["Conventional"]
54
- total_other = w["Intentional"] + w["Arbitrary"]
55
- if total_other > 0:
56
- w["Intentional"] -= diff * (w["Intentional"] / total_other)
57
- w["Arbitrary"] -= diff * (w["Arbitrary"] / total_other)
58
- w["Conventional"] = c_new
59
-
60
  elif a_new != prev_w["Arbitrary"]:
 
61
  diff = a_new - prev_w["Arbitrary"]
62
- total_other = w["Intentional"] + w["Conventional"]
63
- if total_other > 0:
64
- w["Intentional"] -= diff * (w["Intentional"] / total_other)
65
- w["Conventional"] -= diff * (w["Conventional"] / total_other)
66
- w["Arbitrary"] = a_new
67
-
68
- # Clamp small floating point errors
69
- for k in w:
70
- w[k] = max(0.0, min(1.0, round(w[k], 4)))
71
-
72
- # Update prev_weights for next run
73
- st.session_state.prev_weights = w.copy()
 
 
 
 
 
 
 
 
 
74
 
75
  # --- Triangle vertices ---
76
  vertices = np.array([
 
11
  add_instruction_text(
12
  """
13
  Explore the intention-convention-arbitrariness (ICA) framework.<br>
14
+ Use the sliders to adjust the three dimensions. The sum always remains 1.
15
  """
16
  )
17
 
 
23
  "Arbitrary": 0.34
24
  }
25
 
 
 
 
 
26
  w = st.session_state.weights
 
27
 
28
+ # --- Slider inputs ---
29
  col1, col2, col3 = st.columns(3)
30
+
31
+ # Save previous weights for difference computation
32
+ prev_w = w.copy()
33
+
34
  with col1:
35
  i_new = st.slider("Intentional", 0.0, 1.0, w["Intentional"], 0.01)
36
  with col2:
 
38
  with col3:
39
  a_new = st.slider("Arbitrary", 0.0, 1.0, w["Arbitrary"], 0.01)
40
 
41
+ # --- Detect slider changes and redistribute ---
42
+ changed = None
43
  if i_new != prev_w["Intentional"]:
44
+ changed = "Intentional"
45
  diff = i_new - prev_w["Intentional"]
46
+ others = ["Conventional", "Arbitrary"]
 
 
 
 
 
47
  elif c_new != prev_w["Conventional"]:
48
+ changed = "Conventional"
49
  diff = c_new - prev_w["Conventional"]
50
+ others = ["Intentional", "Arbitrary"]
 
 
 
 
 
51
  elif a_new != prev_w["Arbitrary"]:
52
+ changed = "Arbitrary"
53
  diff = a_new - prev_w["Arbitrary"]
54
+ others = ["Intentional", "Conventional"]
55
+
56
+ if changed:
57
+ # Handle slider dragged to 1
58
+ if locals()[changed.lower() + "_new"] >= 1.0:
59
+ for k in others:
60
+ w[k] = 0.0
61
+ w[changed] = 1.0
62
+ else:
63
+ total_other = w[others[0]] + w[others[1]]
64
+ if total_other > 0:
65
+ w[others[0]] -= diff * (w[others[0]] / total_other)
66
+ w[others[1]] -= diff * (w[others[1]] / total_other)
67
+ w[changed] = locals()[changed.lower() + "_new"]
68
+
69
+ # Clamp small floating point errors
70
+ for k in w:
71
+ w[k] = max(0.0, min(1.0, round(w[k], 4)))
72
+
73
+ # --- Update weights in session state immediately ---
74
+ st.session_state.weights = w
75
 
76
  # --- Triangle vertices ---
77
  vertices = np.array([