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

Update my_pages/ica.py

Browse files
Files changed (1) hide show
  1. my_pages/ica.py +31 -33
my_pages/ica.py CHANGED
@@ -23,14 +23,15 @@ def render():
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,40 +39,37 @@ def render():
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([
 
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
  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
+ st.session_state.prev_weights = w.copy()
52
+ st.experimental_rerun()
53
+
54
  elif c_new != prev_w["Conventional"]:
 
55
  diff = c_new - prev_w["Conventional"]
56
+ total_other = w["Intentional"] + w["Arbitrary"]
57
+ if total_other > 0:
58
+ w["Intentional"] -= diff * (w["Intentional"] / total_other)
59
+ w["Arbitrary"] -= diff * (w["Arbitrary"] / total_other)
60
+ w["Conventional"] = c_new
61
+ st.session_state.prev_weights = w.copy()
62
+ st.experimental_rerun()
63
+
64
  elif a_new != prev_w["Arbitrary"]:
 
65
  diff = a_new - prev_w["Arbitrary"]
66
+ total_other = w["Intentional"] + w["Conventional"]
67
+ if total_other > 0:
68
+ w["Intentional"] -= diff * (w["Intentional"] / total_other)
69
+ w["Conventional"] -= diff * (w["Conventional"] / total_other)
70
+ w["Arbitrary"] = a_new
71
+ st.session_state.prev_weights = w.copy()
72
+ st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # --- Triangle vertices ---
75
  vertices = np.array([