Update my_pages/ica.py
Browse files- my_pages/ica.py +59 -5
my_pages/ica.py
CHANGED
|
@@ -1,8 +1,62 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
def render():
|
| 5 |
-
st.title("
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pages/ica.py
|
| 2 |
import streamlit as st
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
def render():
|
| 7 |
+
st.title("Intentional - Conventional - Arbitrary Triangle")
|
| 8 |
+
|
| 9 |
+
# Vertices of the triangle
|
| 10 |
+
vertices = {
|
| 11 |
+
"Intentional": (0.0, 0.0),
|
| 12 |
+
"Conventional": (1.0, 0.0),
|
| 13 |
+
"Arbitrary": (0.5, np.sqrt(3)/2)
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Explanations
|
| 17 |
+
explanations = {
|
| 18 |
+
"Intentional": "An intentional choice is deliberate and goal-oriented.",
|
| 19 |
+
"Conventional": "A conventional choice follows established norms.",
|
| 20 |
+
"Arbitrary": "Random seeds — a purely arbitrary choice."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
if "clicked_vertex" not in st.session_state:
|
| 24 |
+
st.session_state.clicked_vertex = None
|
| 25 |
+
|
| 26 |
+
# Function to handle clicks
|
| 27 |
+
def onclick(event):
|
| 28 |
+
if event.xdata is None or event.ydata is None:
|
| 29 |
+
return # Clicked outside plot
|
| 30 |
+
|
| 31 |
+
click_point = np.array([event.xdata, event.ydata])
|
| 32 |
+
closest_vertex = min(
|
| 33 |
+
vertices.keys(),
|
| 34 |
+
key=lambda v: np.linalg.norm(click_point - np.array(vertices[v]))
|
| 35 |
+
)
|
| 36 |
+
st.session_state.clicked_vertex = closest_vertex
|
| 37 |
+
st.experimental_rerun()
|
| 38 |
+
|
| 39 |
+
# Draw triangle
|
| 40 |
+
fig, ax = plt.subplots()
|
| 41 |
+
triangle_coords = np.array(list(vertices.values()) + [vertices["Intentional"]])
|
| 42 |
+
ax.plot(triangle_coords[:, 0], triangle_coords[:, 1], 'k-', lw=2)
|
| 43 |
+
|
| 44 |
+
# Label vertices
|
| 45 |
+
for label, (x, y) in vertices.items():
|
| 46 |
+
ax.text(x, y + 0.05, label, ha='center', fontsize=12, fontweight='bold')
|
| 47 |
+
ax.plot(x, y, 'o', markersize=10)
|
| 48 |
+
|
| 49 |
+
ax.set_aspect('equal')
|
| 50 |
+
ax.axis('off')
|
| 51 |
+
|
| 52 |
+
# Connect click event
|
| 53 |
+
cid = fig.canvas.mpl_connect("button_press_event", onclick)
|
| 54 |
+
|
| 55 |
+
st.pyplot(fig)
|
| 56 |
+
|
| 57 |
+
# Show explanation if a vertex was clicked
|
| 58 |
+
if st.session_state.clicked_vertex:
|
| 59 |
+
st.markdown(
|
| 60 |
+
f"### You clicked near **{st.session_state.clicked_vertex}**\n"
|
| 61 |
+
f"{explanations[st.session_state.clicked_vertex]}"
|
| 62 |
+
)
|