-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_RunTextReplayCoder.py
More file actions
308 lines (253 loc) · 12.8 KB
/
1_RunTextReplayCoder.py
File metadata and controls
308 lines (253 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# AMIRA_text_replay_from_sampling_only.py | randomization is decoupled from the coding tool now.
import argparse
import pandas as pd
import tkinter as tk
from tkinter import simpledialog, messagebox
from tkinter import font as tkfont
import os, pathlib
from datetime import datetime
# ---- Column char budgets ----
STORY_COL_CHARS = 12
SPEECH_COL_CHARS = 12
W2V_COL_CHARS = 12
# Change this based on the sample file you are annotating.
SAMPLE_FILE = "init_annotations_august29.xlsx" # <-- Excel now
SHOW_W2V = True
SHOW_ATOMIC_PHRASES = True # False = hide words not overlapping the clip window
# Multi-select labels (checkboxes)
LABEL_OPTIONS = [
"Gaming",
"Struggling",
"Okay [None]",
"Noncompliant",
"Correcting Amira",
"Giving Up"
]
def safe_txt(v):
s = "" if pd.isna(v) else str(v)
return s if s.strip() != "" else "?"
def _clip(s, n):
s = safe_txt(s)
return s if len(s) <= n else s[: n-1] + "…"
def load_sampling(path):
df = pd.read_excel(path, dtype={'sample_id':str}) # preserve symbols
req = {"sample_id","activity_id","student_id","clip_index","clip_start","clip_end","clip_seconds","row_type"}
miss = req - set(df.columns)
if miss:
raise ValueError(f"Sampling file missing columns: {sorted(miss)}")
for c in ["clip_index","clip_start","clip_end","clip_seconds","phrase_index","word_index","adjusted_start","adjusted_end","correct_label"]:
if c in df.columns:
df[c] = pd.to_numeric(df[c], errors="coerce")
if "order" in df.columns:
df = df.sort_values("order", kind="stable")
headers = (df.groupby("sample_id")[["activity_id","student_id","clip_index","clip_start","clip_end","clip_seconds"]]
.first()
.reset_index())
by_clip = {sid: g for sid, g in df.groupby("sample_id")}
return headers, by_clip
class ReplayCoder:
def __init__(self, master, sample):
self.master = master
self.master.title("AMIRA Text Replay (Self-contained)")
self.master.minsize(1000, 600)
self.coder_name = simpledialog.askstring("Coder Name", "Enter your name:")
if not self.coder_name: exit()
self.coder_name = self.coder_name.strip()
if not (sample and os.path.exists(sample)):
messagebox.showerror("Missing sampling file", f"Could not find: {sample}")
exit(1)
self.headers, self.by_clip = load_sampling(sample)
self.sample_label = f"Sample: {pathlib.Path(sample).name}"
self.save_file = f"coder_{self.coder_name}_{sample}_annotations.csv"
self.answered = set()
if os.path.exists(self.save_file):
prev = pd.read_csv(self.save_file)
if "sample_id" in prev.columns:
self.answered = set(prev["sample_id"].dropna().astype(str).tolist())
self.remaining = [row for _, row in self.headers.iterrows() if str(row["sample_id"]) not in self.answered]
self.total_all = len(self.headers)
self.coded_so_far = len(self.answered)
self.current_index = 0
# UI
self.progress_label = tk.Label(master, text="", font=("Helvetica", 12))
self.progress_label.pack()
self.text_frame = tk.Frame(master); self.text_frame.pack(fill=tk.BOTH, expand=True)
self.yscroll = tk.Scrollbar(self.text_frame, orient="vertical")
self.xscroll = tk.Scrollbar(self.text_frame, orient="horizontal")
self.text_box = tk.Text(
self.text_frame, height=30, wrap="none",
xscrollcommand=self.xscroll.set, yscrollcommand=self.yscroll.set,
font=("Courier New", 10)
)
self.yscroll.config(command=self.text_box.yview)
self.xscroll.config(command=self.text_box.xview)
self.yscroll.pack(side=tk.RIGHT, fill=tk.Y)
self.xscroll.pack(side=tk.BOTTOM, fill=tk.X)
self.text_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.text_box.tag_configure("bold", font=("Courier New", 10, "bold"))
self.text_box.tag_configure("blue", foreground="#1565c0")
self.text_box.tag_configure("correct_tag", foreground="#2e7d32")
self.text_box.tag_configure("incorrect_tag", foreground="#c62828")
self.text_box.tag_configure("unknown_tag", foreground="#6a737d")
self.text_box.tag_configure("bracket_tag", foreground="#6a737d")
self.text_box.tag_configure("rawtext_tag", foreground="#6a737d") # soft gray
self.text_box.tag_configure("italic", font=("Courier New", 10, "italic"))
self._set_compact_tabs()
self.text_box.bind("<Configure>", lambda e: self._set_compact_tabs())
# --- checkbox panel ---
self.controls_frame = tk.Frame(master)
self.controls_frame.pack(pady=(4, 6))
self.label_vars = {}
for opt in LABEL_OPTIONS:
var = tk.BooleanVar(value=False)
chk = tk.Checkbutton(self.controls_frame, text=opt, variable=var, onvalue=True, offvalue=False)
chk.pack(side=tk.LEFT, padx=6)
self.label_vars[opt] = var
# Save & Next button
self.nav_frame = tk.Frame(master)
self.nav_frame.pack(pady=(0, 8))
tk.Button(self.nav_frame, text="Save & Next", command=self.save_and_next).pack(side=tk.LEFT, padx=6)
self.update_progress()
self.display_next()
def _set_compact_tabs(self):
f = tkfont.Font(font=self.text_box["font"])
pad = f.measure(" ")
col1 = f.measure(" Story Word: " + ("m" * STORY_COL_CHARS)) + pad
col2 = col1 + f.measure("Student Speech: " + ("m" * SPEECH_COL_CHARS)) + pad
if SHOW_W2V:
col3 = col2 + f.measure("W2V: " + ("m" * W2V_COL_CHARS)) + pad
else:
col3 = col2
col4 = col3 + f.measure("[000.00-000.00] ") + pad
if SHOW_W2V:
self.text_box.configure(tabs=(col1, col2, col3, col4))
else:
self.text_box.configure(tabs=(col1, col2, col4))
def update_progress(self):
self.progress_label.config(
text=f"{self.sample_label} — Coded {self.coded_so_far + self.current_index} / {self.total_all}"
)
def display_next(self):
# clear checkboxes each clip
for var in self.label_vars.values():
var.set(False)
if self.current_index >= len(self.remaining):
messagebox.showinfo("Done", "All clips coded!")
self.master.quit()
return
self.text_box.delete("1.0", tk.END)
h = self.remaining[self.current_index]
self.current_header = h
sid = str(h["sample_id"])
aid = h["activity_id"]; stu = h["student_id"]
k = int(h["clip_index"]); cs = float(h["clip_start"]); ce = float(h["clip_end"]); clen = int(h["clip_seconds"])
self.text_box.insert(tk.END, f"{self.sample_label}\n")
self.text_box.insert(tk.END, f"Session: {aid}\nStudent: {stu}\n")
self.text_box.insert(tk.END, f"Clip: {k} [{cs}-{ce} sec] (length={clen}s)\n\n")
g = self.by_clip.get(sid, pd.DataFrame())
if g.empty:
self.text_box.insert(tk.END, "(No rows for this clip; skipping.)\n")
self.current_index += 1
self.update_progress()
self.display_next()
return
words = g[g["row_type"] == "word"].copy()
pivs = g[g["row_type"] == "phrase_iv"].copy()
phrases = sorted([int(x) for x in words["phrase_index"].dropna().unique().tolist()])
def word_overlaps(row):
return (row["adjusted_start"] < ce) and (row["adjusted_end"] > cs)
for pidx in phrases:
pw = words[words["phrase_index"] == pidx].sort_values("word_index")
if not SHOW_ATOMIC_PHRASES:
pw = pw[pw.apply(word_overlaps, axis=1)]
if pw.empty:
continue
self.text_box.insert(tk.END, f"Phrase {pidx + 1}:\n")
for _, w in pw.iterrows():
story = _clip(w.get("story_word"), STORY_COL_CHARS)
kaldi = _clip(w.get("kaldi_word"), SPEECH_COL_CHARS)
w2v = _clip(w.get("w2v_word"), W2V_COL_CHARS) if SHOW_W2V else ""
line = f" Story Word: {story}\tStudent Speech: {kaldi}"
if SHOW_W2V:
line += f"\tW2V: {w2v}"
line += "\t"
self.text_box.insert(tk.END, line)
self.text_box.insert(tk.END, f"[{w['adjusted_start']:.2f}-{w['adjusted_end']:.2f}] [", ("bracket_tag",))
if pd.isna(w.get("correct_label")):
status_text, tag = "Unknown", "unknown_tag"
else:
try:
is_correct = int(w["correct_label"]) == 1
except Exception:
is_correct = False
status_text, tag = ("Correct", "correct_tag") if is_correct else ("Incorrect", "incorrect_tag")
self.text_box.insert(tk.END, status_text, (tag,))
self.text_box.insert(tk.END, "]\n", ("bracket_tag",))
types = str(w.get("word_iv_types")) if pd.notna(w.get("word_iv_types")) else ""
words_iv = str(w.get("word_iv_words")) if pd.notna(w.get("word_iv_words")) else ""
if types.strip() != "":
t_list = types.split("|")
w_list = words_iv.split("|") if words_iv.strip() != "" else ["?"] * len(t_list)
if len(w_list) < len(t_list):
w_list += ["?"] * (len(t_list) - len(w_list))
for ttype, iword in zip(t_list, w_list):
self.text_box.insert(tk.END, f" 🛑 Word Intervention: ", "bold")
self.text_box.insert(tk.END, f"{ttype}", "blue")
self.text_box.insert(tk.END, f" → \"{iword}\"\n")
# Choose a representative raw_text for the phrase (first non-empty)
raw_val = ""
if "raw_text" in pw.columns:
for v in pw["raw_text"].astype(str).tolist():
if v and v.strip() and v.strip().lower() != "nan":
raw_val = v.strip()
break
# self.text_box.insert(tk.END, f"Phrase {pidx + 1}:\n")
if raw_val:
# show raw_text under the phrase header
self.text_box.insert(tk.END, " Raw text: ", ("rawtext_tag", "italic"))
self.text_box.insert(tk.END, f"{raw_val}\n", ("rawtext_tag",))
piv_p = pivs[pivs["phrase_index"] == pidx]
for _, r in piv_p.iterrows():
itype = safe_txt(r.get("phrase_iv_type"))
if itype.lower() == "none":
continue
iword = safe_txt(r.get("phrase_iv_word"))
self.text_box.insert(tk.END, f" 🛑 Phrase Intervention: ", "bold")
self.text_box.insert(tk.END, f"{itype}", "blue")
self.text_box.insert(tk.END, f" → \"{iword}\"\n")
self.text_box.insert(tk.END, "\n")
self.update_progress()
def save_and_next(self):
# gather selections
selected = [opt for opt, var in self.label_vars.items() if var.get()]
if not selected:
messagebox.showwarning("No labels selected", "Please select at least one label before saving.")
return
labels_str = "|".join(selected) # store multiple labels in one column @I used the pipe symbol for now, so it won't break the save file (vs using commas or periods)
h = self.current_header
sid = str(h["sample_id"])
row = {
"coder": self.coder_name,
"sample_id": sid,
"batch_id": "",
"activity_id": h["activity_id"],
"student_id": h["student_id"],
"clip_index": int(h["clip_index"]),
"clip_start": float(h["clip_start"]),
"clip_end": float(h["clip_end"]),
"clip_seconds": float(h["clip_seconds"]),
"label": labels_str, # <-- multi-select saved here
"note": "",
"timestamp": datetime.now().isoformat(),
}
file_exists = os.path.isfile(self.save_file)
pd.DataFrame([row]).to_csv(self.save_file, mode="a", header=not file_exists, index=False)
self.current_index += 1
self.display_next()
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--sample", default="sample.xlsx")
args = ap.parse_args()
root = tk.Tk()
app = ReplayCoder(root, args.sample)
root.mainloop()