-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflame_gui.py
More file actions
414 lines (339 loc) · 21.1 KB
/
Copy pathflame_gui.py
File metadata and controls
414 lines (339 loc) · 21.1 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import tkinter as tk
from tkinter import filedialog, scrolledtext, ttk
import threading
import queue
import sys
import os
import webbrowser
from flame import Flame, DEFAULT_PARAMS, SimilarityVisualizer
from argparse import Namespace
class FlameGUI(tk.Tk):
"""
A Tkinter-based graphical user interface for the FLAME analysis tool.
Designed for users who prefer an intuitive UI over terminal commands.
"""
def __init__(self):
"""Initializes the main application window and structures layout tabs."""
super().__init__()
self.title("FLAME - Formulaic Language Analysis in Medieval Expressions")
self.geometry("950x900")
self.minsize(850, 750)
self.params = {}
for key, val in DEFAULT_PARAMS.items():
if isinstance(val, bool):
self.params[key] = tk.BooleanVar(value=val)
else:
self.params[key] = tk.StringVar(value=str(val))
# REACTIVE LOGIC: Trace changes on similarity_threshold to auto-update method dropdown
self.params['similarity_threshold'].trace_add("write", self.on_threshold_change)
self.create_widgets()
self.log_queue = queue.Queue()
self.after(100, self.process_log_queue)
def on_threshold_change(self, *args):
"""Automatically switches threshold method based on auto vs numerical input."""
val = self.params['similarity_threshold'].get().strip().lower()
if val == 'auto':
self.params['auto_threshold_method'].set('otsu')
else:
self.params['auto_threshold_method'].set('percentile')
def create_widgets(self):
"""Creates and arranges all widgets inside tabbed logical containers."""
main_frame = ttk.Frame(self, padding="15")
main_frame.pack(fill=tk.BOTH, expand=True)
# Tab container layout control
self.notebook = ttk.Notebook(main_frame)
self.notebook.pack(fill=tk.X, side=tk.TOP, pady=5)
tab_core = ttk.Frame(self.notebook, padding="10")
tab_nlp = ttk.Frame(self.notebook, padding="10")
tab_reports = ttk.Frame(self.notebook, padding="10")
self.notebook.add(tab_core, text="1. Data & Core Setup")
self.notebook.add(tab_nlp, text="2. Normalization & Philology")
self.notebook.add(tab_reports, text="3. Auto-Tune & Outputs")
# ==================== TAB 1: CORE SETUP ====================
paths_frame = ttk.LabelFrame(tab_core, text="Corpus Directory Selection", padding="10")
paths_frame.pack(fill=tk.X, pady=5)
paths_frame.columnconfigure(1, weight=1)
self.create_path_entry(paths_frame, "input_path", "Primary Corpus Path:", 0)
self.create_path_entry(paths_frame, "input_path2", "Secondary Corpus Path (Optional):", 1)
core_params_frame = ttk.LabelFrame(tab_core, text="Core Windowing & Matching Settings", padding="10")
core_params_frame.pack(fill=tk.X, pady=5)
self.create_param_entry(core_params_frame, "ngram", "N-gram window size:", 0, 0)
self.create_param_entry(core_params_frame, "n_out", "N-out positions (Gaps):", 0, 2)
self.create_param_entry(core_params_frame, "min_text_length", "Min. Text Length (chars):", 1, 0)
self.create_param_entry(core_params_frame, "file_suffix", "Target File Suffix:", 1, 2)
self.create_param_entry(core_params_frame, "keep_texts", "Max Documents to Load:", 2, 0)
threshold_frame = ttk.LabelFrame(tab_core, text="Global Threshold Selection", padding="10")
threshold_frame.pack(fill=tk.X, pady=5)
self.create_param_entry(threshold_frame, "similarity_threshold", "Similarity Threshold ('auto' or 0-1):", 0, 0)
# DROPDOWN IMPLEMENTATION: Replacing entry with a reactive readonly Combobox
ttk.Label(threshold_frame, text="Auto Threshold Method:").grid(row=0, column=2, sticky=tk.W, padx=5, pady=4)
method_dropdown = ttk.Combobox(
threshold_frame,
textvariable=self.params['auto_threshold_method'],
values=['otsu', 'percentile'],
state='readonly',
width=12
)
method_dropdown.grid(row=0, column=3, sticky=tk.W, padx=5, pady=4)
# ==================== TAB 2: NLP & PHILOLOGY ====================
char_frame = ttk.LabelFrame(tab_nlp, text="Character Normalization Layers", padding="10")
char_frame.pack(fill=tk.X, pady=5)
char_frame.columnconfigure(1, weight=1)
ttk.Label(char_frame, text="Target Norm Alphabet:").grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)
ttk.Entry(char_frame, textvariable=self.params['char_norm_alphabet'], width=50).grid(row=0, column=1, columnspan=3, sticky=tk.EW, padx=5, pady=5)
self.create_param_entry(char_frame, "char_norm_strategy", "Norm Strategy:", 1, 0)
self.create_param_entry(char_frame, "char_norm_min_freq", "Min. Norm Frequency:", 1, 2)
phonetic_frame = ttk.LabelFrame(tab_nlp, text="Phonetic Reduction Layer", padding="10")
phonetic_frame.pack(fill=tk.X, pady=5)
phonetic_frame.columnconfigure(1, weight=1)
self.chk_phonetic = ttk.Checkbutton(
phonetic_frame,
text="Enable Phonetic Reduction",
variable=self.params['phonetic_reduction_enabled'],
command=self.toggle_phonetic_entries
)
self.chk_phonetic.grid(row=0, column=0, columnspan=4, sticky=tk.W, padx=5, pady=5)
ttk.Label(phonetic_frame, text="Reduced Alphabet:").grid(
row=1, column=0, sticky=tk.W, padx=5, pady=5)
self.phonetic_alphabet_entry = ttk.Entry(
phonetic_frame, textvariable=self.params['phonetic_reduction_alphabet'], width=50)
self.phonetic_alphabet_entry.grid(
row=1, column=1, columnspan=3, sticky=tk.EW, padx=5, pady=5)
ttk.Label(phonetic_frame, text="Mapping Rules (src>dst, comma-sep):").grid(
row=2, column=0, sticky=tk.W, padx=5, pady=5)
self.phonetic_rules_entry = ttk.Entry(
phonetic_frame, textvariable=self.params['phonetic_reduction_rules'], width=50)
self.phonetic_rules_entry.grid(
row=2, column=1, columnspan=3, sticky=tk.EW, padx=5, pady=5)
ttk.Label(
phonetic_frame,
text="Note: Characters not in the alphabet or rules become spaces.",
font=("TkDefaultFont", 9, "italic"),
foreground="gray"
).grid(row=3, column=0, columnspan=4, sticky=tk.W, padx=5, pady=2)
ttk.Separator(phonetic_frame, orient='horizontal').grid(
row=4, column=0, columnspan=4, sticky=tk.EW, padx=5, pady=5)
self.chk_bigram = ttk.Checkbutton(
phonetic_frame,
text="Enable Bigram Normalization",
variable=self.params['bigram_normalization_enabled'],
command=self.toggle_bigram_entries
)
self.chk_bigram.grid(row=5, column=0, columnspan=4, sticky=tk.W, padx=5, pady=5)
ttk.Label(phonetic_frame, text="Bigram Rules (src>dst, comma-sep):").grid(
row=6, column=0, sticky=tk.W, padx=5, pady=5)
self.bigram_rules_entry = ttk.Entry(
phonetic_frame, textvariable=self.params['bigram_normalization_rules'], width=50)
self.bigram_rules_entry.grid(
row=6, column=1, columnspan=3, sticky=tk.EW, padx=5, pady=5)
ttk.Label(
phonetic_frame,
text="Note: Applies one-to-many string replacements (e.g., ss>s, ie>i) before phonetic reduction.",
font=("TkDefaultFont", 9, "italic"),
foreground="gray"
).grid(row=7, column=0, columnspan=4, sticky=tk.W, padx=5, pady=2)
bpe_frame = ttk.LabelFrame(tab_nlp, text="Subword Tokenizer (BPE) Heuristics", padding="10")
bpe_frame.pack(fill=tk.X, pady=5)
self.create_param_entry(bpe_frame, "vocab_size", "Vocab Size ('auto' or integer):", 0, 0)
self.create_param_entry(bpe_frame, "vocab_min_word_freq", "Min. Word Freq for Affixes:", 0, 2)
self.create_param_entry(bpe_frame, "vocab_coverage", "Target Morphological Coverage (0-1):", 1, 0)
alignment_frame = ttk.LabelFrame(tab_nlp, text="Philological Variation & Bridge Words Evaluation", padding="10")
alignment_frame.pack(fill=tk.X, pady=5)
self.create_param_entry(alignment_frame, "fuzz_threshold", "Bridge Word Fuzzy Sensitivity (0-1):", 0, 0)
self.create_param_entry(alignment_frame, "max_gap_words", "Max Bridge Word Length Gap:", 0, 2)
# ==================== TAB 3: AUTO-TUNE & REPORTS ====================
autotune_frame = ttk.LabelFrame(tab_reports, text="Trial Digging (Autonomous Hyperparameter Auto-Tune)", padding="10")
autotune_frame.pack(fill=tk.X, pady=5)
chk_tune = ttk.Checkbutton(autotune_frame, text="Enable Self-Supervised Auto-Tune (Finds best N-gram & Gap setup)", variable=self.params['auto_tune'])
chk_tune.grid(row=0, column=0, columnspan=2, sticky=tk.W, padx=5, pady=5)
self.create_param_entry(autotune_frame, "auto_tune_sample_size", "Auto-Tune Training Sample Size:", 1, 0)
ttk.Label(autotune_frame, text="Note: Auto-tuning executes a temporary synthetic noise sweep to maximize signal separation.", font=("TkDefaultFont", 9, "italic")).grid(row=2, column=0, columnspan=4, sticky=tk.W, padx=5, pady=2)
reports_frame = ttk.LabelFrame(tab_reports, text="Report & Output File Generation", padding="10")
reports_frame.pack(fill=tk.X, pady=5)
self.chk_no_reports = ttk.Checkbutton(reports_frame, text="Disable All Document Reports", variable=self.params['no_reports'], command=self.toggle_report_checkboxes)
self.chk_no_reports.grid(row=0, column=0, columnspan=2, sticky=tk.W, padx=5, pady=5)
self.chk_html = ttk.Checkbutton(reports_frame, text="Generate Interactive Side-by-Side HTML", variable=self.params['gen_comparison_html'])
self.chk_html.grid(row=1, column=0, sticky=tk.W, padx=20, pady=2)
self.chk_summary = ttk.Checkbutton(reports_frame, text="Generate Spreadsheet Summary TSV", variable=self.params['gen_summary_tsv'])
self.chk_summary.grid(row=1, column=1, sticky=tk.W, padx=5, pady=2)
self.chk_linguistic = ttk.Checkbutton(reports_frame, text="Generate Granular Linguistic Variants TSV", variable=self.params['gen_linguistic_tsv'])
self.chk_linguistic.grid(row=2, column=0, sticky=tk.W, padx=20, pady=2)
self.chk_heatmap = ttk.Checkbutton(reports_frame, text="Generate Dynamic Cluster Heatmap HTML", variable=self.params['gen_heatmap'])
self.chk_heatmap.grid(row=2, column=1, sticky=tk.W, padx=5, pady=2)
self.toggle_report_checkboxes()
self.toggle_phonetic_entries()
self.toggle_bigram_entries()
# ==================== RUN & LOG ENGINE (BOTTOM) ====================
self.run_button = ttk.Button(main_frame, text="Run FLAME Pipeline", command=self.start_analysis_thread)
self.run_button.pack(pady=15)
results_frame = ttk.LabelFrame(main_frame, text="Direct Action: Open Analysis Results", padding="10")
results_frame.pack(fill=tk.X, pady=5)
for i in range(4): results_frame.columnconfigure(i, weight=1)
self.html_button = ttk.Button(results_frame, text="Open Aligned HTML", state=tk.DISABLED, command=lambda: self.open_result_file('text_comparisons_01.html'))
self.html_button.grid(row=0, column=0, padx=5, pady=5, sticky=tk.EW)
self.heatmap_button = ttk.Button(results_frame, text="Open Heatmap Chart", state=tk.DISABLED, command=lambda: self.open_result_file('similarity_heatmap.html'))
self.heatmap_button.grid(row=0, column=1, padx=5, pady=5, sticky=tk.EW)
self.summary_button = ttk.Button(results_frame, text="Open Summary TSV", state=tk.DISABLED, command=lambda: self.open_result_file('similarity_summary.tsv'))
self.summary_button.grid(row=0, column=2, padx=5, pady=5, sticky=tk.EW)
self.linguistic_button = ttk.Button(results_frame, text="Open Linguistic Variants TSV", state=tk.DISABLED, command=lambda: self.open_result_file('linguistic_variations.tsv'))
self.linguistic_button.grid(row=0, column=3, padx=5, pady=5, sticky=tk.EW)
log_frame = ttk.LabelFrame(main_frame, text="Pipeline Execution Standard Output Log", padding="10")
log_frame.pack(fill=tk.BOTH, expand=True, side=tk.BOTTOM)
self.log_text = scrolledtext.ScrolledText(log_frame, wrap=tk.WORD, height=12, bg="#1e1e1e", fg="#ffffff", insertbackground="white", font=("Courier", 9))
self.log_text.pack(fill=tk.BOTH, expand=True)
self.log_text.configure(state='disabled')
def toggle_report_checkboxes(self):
"""Disables individual report checkboxes if 'Disable All' is checked."""
is_disabled = self.params['no_reports'].get()
new_state = tk.DISABLED if is_disabled else tk.NORMAL
self.chk_html.config(state=new_state)
self.chk_summary.config(state=new_state)
self.chk_linguistic.config(state=new_state)
self.chk_heatmap.config(state=new_state)
def toggle_phonetic_entries(self):
"""Enables/disables phonetic reduction entries based on checkbox state."""
state = tk.NORMAL if self.params['phonetic_reduction_enabled'].get() else tk.DISABLED
self.phonetic_alphabet_entry.config(state=state)
self.phonetic_rules_entry.config(state=state)
def toggle_bigram_entries(self):
"""Enables/disables bigram normalization entries based on checkbox state."""
state = tk.NORMAL if self.params['bigram_normalization_enabled'].get() else tk.DISABLED
self.bigram_rules_entry.config(state=state)
def create_path_entry(self, parent, param_name, label_text, row):
"""Creates a standard path field workspace wrapper configuration."""
ttk.Label(parent, text=label_text).grid(row=row, column=0, sticky=tk.W, padx=5, pady=5)
entry = ttk.Entry(parent, textvariable=self.params[param_name], width=55)
entry.grid(row=row, column=1, sticky=tk.EW, padx=5, pady=5)
browse_button = ttk.Button(parent, text="Browse Folder...", command=lambda: self.browse_directory(param_name))
browse_button.grid(row=row, column=2, sticky=tk.W, padx=5, pady=5)
def create_param_entry(self, parent, param_name, label_text, row, col):
"""Utility wrapper to render clean execution properties inside grids."""
ttk.Label(parent, text=label_text).grid(row=row, column=col, sticky=tk.W, padx=5, pady=4)
entry = ttk.Entry(parent, textvariable=self.params[param_name], width=12)
entry.grid(row=row, column=col+1, sticky=tk.W, padx=5, pady=4)
def browse_directory(self, param_name):
directory = filedialog.askdirectory(title="Select Corpus Target Directory Folder")
if directory:
self.params[param_name].set(directory)
def open_result_file(self, filename):
if os.path.exists(filename):
webbrowser.open(os.path.realpath(filename))
else:
self.log_queue.put(f"\nTarget execution file asset not ready or found: {filename}\n")
def start_analysis_thread(self):
"""Spawns processing workers in background layers safely."""
self.run_button.config(state="disabled", text="Pipeline Analysis Running...")
self.html_button.config(state=tk.DISABLED)
self.heatmap_button.config(state=tk.DISABLED)
self.summary_button.config(state=tk.DISABLED)
self.linguistic_button.config(state=tk.DISABLED)
self.log_text.configure(state='normal')
self.log_text.delete(1.0, tk.END)
self.log_text.configure(state='disabled')
analysis_thread = threading.Thread(target=self.run_analysis, daemon=True)
analysis_thread.start()
def run_analysis(self):
"""Executes full operational processing sequence mapping values safely."""
analysis_successful = False
try:
args_for_flame = {}
for key, var in self.params.items():
val = var.get()
if isinstance(var, tk.BooleanVar):
args_for_flame[key] = val
elif key in ['keep_texts', 'ngram', 'n_out', 'min_text_length',
'char_norm_min_freq', 'vocab_min_word_freq',
'max_gap_words', 'auto_tune_sample_size']:
args_for_flame[key] = int(val)
elif key in ['vocab_coverage', 'fuzz_threshold']:
args_for_flame[key] = float(val)
elif key == 'similarity_threshold' and str(val).lower() != 'auto':
args_for_flame[key] = float(val)
else:
args_for_flame[key] = val
args_object = Namespace(**args_for_flame)
analyzer = Flame(args=args_object)
sys.stdout = self
sys.stderr = self
analyzer.load_corpus()
if not analyzer.corpus:
raise RuntimeError("Execution halted because no valid target documents loaded.")
if analyzer.args.auto_tune:
analyzer.auto_tune_parameters()
if (analyzer.args.ngram - analyzer.args.n_out) < 1:
raise ValueError(f"N-gram size ({analyzer.args.ngram}) minus n-out ({analyzer.args.n_out}) must be at least 1.")
analyzer.compute_similarity_matrix()
if analyzer.dist_mat is None:
raise RuntimeError("Execution processing failure: global matrix could not be resolved.")
if analyzer.args.no_reports:
print("\n--- Report generation skipped as per GUI configuration toggle. ---")
else:
print("\n--- Generating Scholar Report Assets ---")
if str(analyzer.args.similarity_threshold).lower() == 'auto':
final_threshold = analyzer._determine_auto_threshold(method=analyzer.args.auto_threshold_method)
else:
final_threshold = float(analyzer.args.similarity_threshold)
if analyzer.args.gen_heatmap:
if analyzer.dist_mat.shape[0] < 2000 and analyzer.dist_mat.shape[1] < 2000:
SimilarityVisualizer.plot_similarity_heatmap(analyzer)
else:
print(f"Skipping heatmap generation for large matrix bounds ({analyzer.dist_mat.shape[0]}x{analyzer.dist_mat.shape[1]}).")
else:
print("Skipping heatmap generation as per configuration.")
if analyzer.args.gen_comparison_html:
SimilarityVisualizer.generate_comparison_html(analyzer, similarity_threshold=final_threshold)
else:
print("Skipping interactive HTML generation as per configuration.")
if analyzer.args.gen_summary_tsv:
SimilarityVisualizer.generate_similarity_summary_tsv(analyzer, similarity_threshold=final_threshold)
else:
print("Skipping summary TSV generation as per configuration.")
if analyzer.args.gen_linguistic_tsv:
SimilarityVisualizer.generate_linguistic_summary_tsv(analyzer, similarity_threshold=final_threshold)
else:
print("Skipping linguistic TSV variants generation as per configuration.")
print("\n--- PIPELINE EXECUTION COMPLETELY FINISHED WITH SUCCESS ---")
analysis_successful = True
except Exception as e:
self.log_queue.put(f"\n--- AN EXCEPTION ERROR PIPELINE RUN ENCOUNTERED ---\n{type(e).__name__}: {e}\n")
finally:
def final_gui_update():
self.run_button.config(state="normal", text="Run FLAME Pipeline")
if analysis_successful:
self.update_results_buttons()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
self.after(0, final_gui_update)
def update_results_buttons(self):
"""Enables individual reporting buttons conditionally based on outputs matching criteria."""
if self.params['no_reports'].get():
return
if self.params['gen_comparison_html'].get() and os.path.exists('text_comparisons_01.html'):
self.html_button.config(state=tk.NORMAL)
if self.params['gen_heatmap'].get() and os.path.exists('similarity_heatmap.html'):
self.heatmap_button.config(state=tk.NORMAL)
if self.params['gen_summary_tsv'].get() and os.path.exists('similarity_summary.tsv'):
self.summary_button.config(state=tk.NORMAL)
if self.params['gen_linguistic_tsv'].get() and os.path.exists('linguistic_variations.tsv'):
self.linguistic_button.config(state=tk.NORMAL)
def write(self, text):
self.log_queue.put(text)
def flush(self):
pass
def process_log_queue(self):
try:
while True:
text = self.log_queue.get_nowait()
self.log(text)
except queue.Empty:
self.after(100, self.process_log_queue)
def log(self, text):
self.log_text.configure(state='normal')
self.log_text.insert(tk.END, text)
self.log_text.see(tk.END)
self.log_text.configure(state='disabled')
if __name__ == "__main__":
app = FlameGUI()
style = ttk.Style()
style.theme_use('clam')
app.mainloop()