Alex Chan commited on
Commit
d4d0942
Β·
1 Parent(s): b513264

Update Streamlit UI

Browse files

Includes:
- Expander with markdown explanation on how to use the tool.
- Create additional column for tunable hyperparameter.
- Toggle-behaviour to show the train panel or upload panel

Files changed (2) hide show
  1. frontend/app.py +47 -26
  2. frontend/how_to.md +49 -0
frontend/app.py CHANGED
@@ -16,8 +16,8 @@ import streamlit.components.v1 as components
16
 
17
  import api_client
18
 
19
- st.set_page_config(page_title="Interactive TM Visualization", layout="wide")
20
- st.title("Interactive Tsetlin Machine Feature Visualization")
21
 
22
  st.markdown("""
23
  <style>
@@ -42,37 +42,58 @@ except api_client.BackendError as e:
42
  _err(f"Backend unavailable: {e}")
43
  st.stop()
44
 
 
 
 
 
45
 
46
  # ──────────────────────────
47
  # Training Panel (top)
48
  # ──────────────────────────
49
- st.subheader("🧠 Train a Tsetlin Machine")
50
- st.caption("Train a model on a binarised dataset. Training and visualisation run "
51
- "on the private backend; this page only displays the results.")
52
-
53
- c1, c2, c3 = st.columns([1, 1, 1])
54
- with c1:
55
- tm_type = st.selectbox("TM type", tm_types, index=0)
56
- with c2:
57
- dataset_key = st.selectbox("Binarised dataset", dataset_keys, index=0)
58
- with c3:
59
- with st.expander("Training parameters", expanded=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  n_trials = st.number_input("Optuna trials", min_value=1, max_value=100, value=10, step=1)
 
61
  epochs = st.number_input("Final training epochs", min_value=1, max_value=100, value=15, step=1)
62
 
63
- if st.button("πŸš€ Train & Visualise", type="primary"):
64
- with st.spinner(f"Training {tm_type} on {dataset_key}…"):
65
- try:
66
- meta = api_client.train(tm_type, dataset_key, int(n_trials), int(epochs))
67
- except api_client.BackendError as e:
68
- _err(str(e))
69
- st.stop()
70
- st.session_state.meta = meta
71
- st.session_state.sample_idx = 0
72
- st.rerun()
73
 
74
- # Optional: upload your own files instead
75
- with st.expander("πŸ“‚ Or upload your own files", expanded=False):
76
  u1, u2 = st.columns(2)
77
  with u1:
78
  up_model = st.file_uploader("Upload TM Model JSON", type="json")
@@ -81,7 +102,7 @@ with st.expander("πŸ“‚ Or upload your own files", expanded=False):
81
  if st.button("Load uploaded files", disabled=not (up_model and up_csv)):
82
  try:
83
  meta = api_client.upload(up_model.getvalue(), up_model.name,
84
- up_csv.getvalue(), up_csv.name)
85
  except api_client.BackendError as e:
86
  _err(str(e))
87
  st.stop()
 
16
 
17
  import api_client
18
 
19
+ st.set_page_config(page_title="Interactive TMAtlas Visualiser Tool", layout="wide")
20
+ st.title("Interactive TMAtlas Feature Visualiser")
21
 
22
  st.markdown("""
23
  <style>
 
42
  _err(f"Backend unavailable: {e}")
43
  st.stop()
44
 
45
+ # Usage guide
46
+ with st.expander("πŸ“– How to Use This Tool", expanded=False):
47
+ with open("how_to.md", "r", encoding = "utf-8") as f:
48
+ st.markdown(f.read())
49
 
50
  # ──────────────────────────
51
  # Training Panel (top)
52
  # ──────────────────────────
53
+
54
+ choiceA = "Train a new Tsetlin Machine"
55
+ choiceB = "Upload a trained Tsetlin Machine"
56
+
57
+ st.subheader("Select to either train a new Tsetlin Machine, or uploaded a trained model")
58
+ train_or_upload_key = st.selectbox(
59
+ "Make your choice here:",
60
+ [choiceA, choiceB],
61
+ index = 0, # Default assumes the user wants to train a new model
62
+ help = "Select to either train a new Tsetlin Machine, or upload a trained one, for Co-occurrence visualisation.",
63
+ )
64
+ choiceA_enabled = (train_or_upload_key == choiceA)
65
+
66
+ up_model = None
67
+ up_csv = None
68
+
69
+ if choiceA_enabled:
70
+ st.subheader("🧠 Train a Tsetlin Machine")
71
+ st.caption("Train a model on a binarised dataset. Training and visualisation run "
72
+ "on the private backend. This page only displays the results.")
73
+
74
+ c1, c2, c3, c4 = st.columns([1, 1, 1, 1])
75
+ with c1:
76
+ tm_type = st.selectbox("TM type", tm_types, index=0)
77
+ with c2:
78
+ dataset_key = st.selectbox("Binarised dataset", dataset_keys, index=0)
79
+ with c3:
80
  n_trials = st.number_input("Optuna trials", min_value=1, max_value=100, value=10, step=1)
81
+ with c4:
82
  epochs = st.number_input("Final training epochs", min_value=1, max_value=100, value=15, step=1)
83
 
84
+ if st.button("πŸš€ Train & Visualise", type="primary"):
85
+ with st.spinner(f"Training {tm_type} on {dataset_key}…"):
86
+ try:
87
+ meta = api_client.train(tm_type, dataset_key, int(n_trials), int(epochs))
88
+ except api_client.BackendError as e:
89
+ _err(str(e))
90
+ st.stop()
91
+ st.session_state.meta = meta
92
+ st.session_state.sample_idx = 0
93
+ st.rerun()
94
 
95
+ else:
96
+ st.subheader("πŸ“‚ Your trained Tsetlin Machine and corresponding CSV")
97
  u1, u2 = st.columns(2)
98
  with u1:
99
  up_model = st.file_uploader("Upload TM Model JSON", type="json")
 
102
  if st.button("Load uploaded files", disabled=not (up_model and up_csv)):
103
  try:
104
  meta = api_client.upload(up_model.getvalue(), up_model.name,
105
+ up_csv.getvalue(), up_csv.name)
106
  except api_client.BackendError as e:
107
  _err(str(e))
108
  st.stop()
frontend/how_to.md ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ **1. Upload Files:**
2
+ * **Model JSON**: Contains TM model configuration (features, clauses, weights)
3
+ * **Samples CSV**: Contains input data with feature columns matching model features
4
+
5
+ **2. Navigate Samples:**
6
+ * Use the dropdown or Previous/Next buttons to browse samples
7
+ * Sample info shows actual/predicted values and pre-computed activations
8
+
9
+ **3. Visualization Controls:**
10
+ * **Weight threshold**: Filter clauses by importance
11
+ * **Show single-feature nodes**: Toggle square nodes for single-feature clauses
12
+
13
+ **4. Network Visualization:**
14
+ * **Round nodes (Features)**:
15
+ - **Color indicates value relative to the feature's range defined in the model:**
16
+ - πŸ”΅ **Blue**: Low values (near minimum of range)
17
+ - 🟒 **Green**: Middle values (around center of range)
18
+ - 🟑 **Yellow/Orange**: High values (near maximum of range)
19
+ - ⚫ **Black**: Out-of-range values (below minimum or above maximum)
20
+ - The range comes from the model JSON's `features[].range` array, NOT from the data
21
+ - Example: If Temperature range is [10, 35] in the model:
22
+ - Value 12 β†’ Blue (low)
23
+ - Value 22.5 β†’ Green (middle)
24
+ - Value 33 β†’ Yellow (high)
25
+ - Value 40 β†’ Black (out of range!)
26
+ * **Square nodes**: Single-feature clauses (blue = positive, red = negative weight)
27
+ * **Edges**: Multi-feature connections (thickness = importance, color = net weight)
28
+
29
+ **5. Clause Scatter Plot (NEW):**
30
+ * Select two features to visualize their clause relationships
31
+ * **Shows only active clauses for the current sample**
32
+ * **X and Y axes**: Show threshold values from clause conditions
33
+ * **Circle size**: Proportional to clause weight (bigger = more impact)
34
+ * **Circle color**: Blue = positive weight, Red = negative weight
35
+ * **Options**: Filter by weight threshold
36
+
37
+ **6. Export Data:**
38
+ * Click "Export Graph Data" to download visualization as JSON
39
+ * Includes all nodes, edges, statistics, and calculations
40
+
41
+ **7. Analytics:**
42
+ * **Top Impactful Relationships**: Most important feature pairs
43
+ * **Feature Activation Summary**: Which features appear most in active clauses
44
+ * **Output Calculations**: Detailed breakdown of model predictions
45
+
46
+ **8. Understanding Out-of-Range Values:**
47
+ * Black nodes indicate the model is seeing data outside its expected range
48
+ * This might explain unexpected predictions or behavior
49
+ * The model was trained/designed for specific ranges shown in the model JSON