File size: 12,624 Bytes
e98015f |
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 |
# app.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, roc_auc_score
from tabulate import tabulate
import warnings
import traceback
import gradio as gr
import os
import git
# --- Main Class (Slightly Refactored for Interactivity) ---
# The core logic remains, but we separate data loading from analysis functions.
warnings.filterwarnings('ignore')
plt.style.use('default')
sns.set_palette("husl")
class EnhancedAIvsRealGazeAnalyzer:
def __init__(self):
self.questions = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6']
self.correct_answers = {'Pair1': 'B', 'Pair2': 'B', 'Pair3': 'B', 'Pair4': 'B', 'Pair5': 'B', 'Pair6': 'B'}
self.combined_data = None
self.response_data = None
self.numeric_cols = []
self.time_metrics = []
def load_and_process_data(self, base_path, response_file):
"""Loads all data and preprocesses it once."""
print("Loading and processing data...")
# Load response data
self.response_data = pd.read_excel(response_file) if response_file.endswith('.xlsx') else pd.read_csv(
response_file)
self.response_data.columns = self.response_data.columns.str.strip()
for pair, correct_answer in self.correct_answers.items():
if pair in self.response_data.columns:
self.response_data[f'{pair}_Correct'] = (
self.response_data[pair].astype(str).str.strip().str.upper() == correct_answer)
# Load eye-tracking data
all_data = {}
for question in self.questions:
file_path = f"{base_path}/Filtered_GenAI_Metrics_cleaned_{question}.xlsx"
if os.path.exists(file_path):
xls = pd.ExcelFile(file_path)
all_data[question] = {sheet_name: pd.read_excel(xls, sheet_name) for sheet_name in xls.sheet_names}
# Combine and merge
all_dfs = [df.copy().assign(Question=q, Metric_Type=m) for q, qd in all_data.items() for m, df in qd.items()]
if not all_dfs:
raise ValueError("No eye-tracking data files were found or loaded.")
self.combined_data = pd.concat(all_dfs, ignore_index=True)
self.combined_data.columns = self.combined_data.columns.str.strip()
# Merge with responses
et_id_col = next((c for c in self.combined_data.columns if 'participant' in c.lower()), None)
resp_id_col = next((c for c in self.response_data.columns if 'participant' in c.lower()), None)
response_long = self.response_data.melt(id_vars=[resp_id_col], value_vars=self.correct_answers.keys(),
var_name='Pair', value_name='Response')
correctness_long = self.response_data.melt(id_vars=[resp_id_col],
value_vars=[f'{p}_Correct' for p in self.correct_answers.keys()],
var_name='Pair_Correct_Col', value_name='Correct')
correctness_long['Pair'] = correctness_long['Pair_Correct_Col'].str.replace('_Correct', '')
response_long = response_long.merge(correctness_long[[resp_id_col, 'Pair', 'Correct']],
on=[resp_id_col, 'Pair'])
q_to_pair = {f'Q{i + 1}': f'Pair{i + 1}' for i in range(6)}
self.combined_data['Pair'] = self.combined_data['Question'].map(q_to_pair)
self.combined_data = self.combined_data.merge(response_long, left_on=[et_id_col, 'Pair'],
right_on=[resp_id_col, 'Pair'], how='left')
self.combined_data['Answer_Correctness'] = self.combined_data['Correct'].map(
{True: 'Correct', False: 'Incorrect'})
# Identify numeric and time columns for later use
self.numeric_cols = self.combined_data.select_dtypes(include=np.number).columns.tolist()
self.time_metrics = [c for c in self.numeric_cols if
any(k in c.lower() for k in ['time', 'duration', 'fixation'])]
print("Data loading complete.")
return self # Return self for chaining
def analyze_rq1_metric(self, metric):
"""Analyzes a single metric for RQ1."""
if metric not in self.combined_data.columns:
return None, "Metric not found."
correct = self.combined_data.loc[self.combined_data['Answer_Correctness'] == 'Correct', metric].dropna()
incorrect = self.combined_data.loc[self.combined_data['Answer_Correctness'] == 'Incorrect', metric].dropna()
# Perform t-test
t_stat, p_val = stats.ttest_ind(incorrect, correct, equal_var=False, nan_policy='omit')
# Create plot
fig, ax = plt.subplots(figsize=(8, 6))
sns.boxplot(data=self.combined_data, x='Answer_Correctness', y=metric, ax=ax, palette=['#66b3ff', '#ff9999'])
ax.set_title(f'Comparison of "{metric}" by Answer Correctness', fontsize=14)
ax.set_xlabel("Answer Correctness")
ax.set_ylabel(metric)
plt.tight_layout()
# Create summary text
summary = f"""
### Analysis for: **{metric}**
- **Mean (Correct Answers):** {correct.mean():.4f}
- **Mean (Incorrect Answers):** {incorrect.mean():.4f}
- **T-test p-value:** {p_val:.4f}
**Conclusion:**
- {'There is a **statistically significant** difference between the groups (p < 0.05).' if p_val < 0.05 else 'There is **no statistically significant** difference between the groups (p >= 0.05).'}
"""
return fig, summary
def run_prediction_model(self, test_size, n_estimators):
"""Runs the RandomForest model with given parameters for RQ2."""
leaky_features = ['Total_Correct', 'Overall_Accuracy', 'Correct']
features_to_use = [col for col in self.numeric_cols if col not in leaky_features]
features = self.combined_data[features_to_use].copy()
target = self.combined_data['Answer_Correctness'].map({'Correct': 1, 'Incorrect': 0})
valid_indices = target.notna()
features, target = features[valid_indices], target[valid_indices]
features = features.fillna(features.median()).fillna(0)
if len(target.unique()) < 2:
return "Not enough classes to train the model.", None
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=test_size, random_state=42,
stratify=target)
scaler = StandardScaler()
X_train_scaled, X_test_scaled = scaler.fit_transform(X_train), scaler.transform(X_test)
model = RandomForestClassifier(n_estimators=n_estimators, random_state=42, class_weight='balanced')
model.fit(X_train_scaled, y_train)
y_pred_proba = model.predict_proba(X_test_scaled)[:, 1]
y_pred = model.predict(X_test_scaled)
# Generate results
report = classification_report(y_test, y_pred, target_names=['Incorrect', 'Correct'], output_dict=True)
auc_score = roc_auc_score(y_test, y_pred_proba)
report_df = pd.DataFrame(report).transpose().round(3)
report_md = f"""
### Model Performance
- **AUC Score:** **{auc_score:.4f}**
- **Overall Accuracy:** {report['accuracy']:.3f}
**Classification Report:**
{report_df.to_markdown()}
"""
# Feature importance plot
feature_importance = pd.DataFrame({'Feature': features.columns, 'Importance': model.feature_importances_})
feature_importance = feature_importance.sort_values('Importance', ascending=False).head(15)
fig, ax = plt.subplots(figsize=(10, 8))
sns.barplot(data=feature_importance, x='Importance', y='Feature', ax=ax, palette='viridis')
ax.set_title(f'Top 15 Predictive Features (n_estimators={n_estimators})', fontsize=14)
plt.tight_layout()
return report_md, fig
# --- DATA SETUP (RUNS ONCE AT STARTUP) ---
def setup_and_load_data():
"""Clones the repo if not present and loads data."""
repo_url = "https://github.com/RextonRZ/GenAIEyeTrackingCleanedDataset"
repo_dir = "GenAIEyeTrackingCleanedDataset"
if not os.path.exists(repo_dir):
print(f"Cloning data repository from {repo_url}...")
git.Repo.clone_from(repo_url, repo_dir)
else:
print("Data repository already exists.")
base_path = os.path.join(repo_dir, "cleaned_dataset")
response_file = os.path.join(repo_dir, "response_sheet", "GenAI_Response_Sheet.xlsx")
analyzer = EnhancedAIvsRealGazeAnalyzer().load_and_process_data(base_path, response_file)
return analyzer
print("Starting application setup...")
analyzer = setup_and_load_data()
print("Application setup complete. Ready for interaction.")
# --- GRADIO INTERACTIVE FUNCTIONS ---
def update_rq1_visuals(metric_choice):
"""Called by Gradio when the dropdown for RQ1 changes."""
if not metric_choice:
return None, "Please select a metric from the dropdown."
plot, summary = analyzer.analyze_rq1_metric(metric_choice)
return plot, summary
def update_rq2_model(test_size, n_estimators):
"""Called by Gradio when sliders for RQ2 change."""
n_estimators = int(n_estimators) # Ensure it's an integer
report, plot = analyzer.run_prediction_model(test_size, n_estimators)
return report, plot
# --- GRADIO INTERFACE DEFINITION ---
description = """
# Interactive Dashboard: AI vs. Real Gaze Analysis
Explore the eye-tracking dataset by interacting with the controls below. The data is automatically loaded from the public GitHub repository.
"""
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(description)
with gr.Tabs():
with gr.TabItem("RQ1: Viewing Time vs. Correctness"):
gr.Markdown("### Does viewing time differ based on whether a participant's answer was correct?")
with gr.Row():
with gr.Column(scale=1):
rq1_metric_dropdown = gr.Dropdown(
choices=analyzer.time_metrics,
label="Select a Time-Based Metric to Analyze",
value=analyzer.time_metrics[0] if analyzer.time_metrics else None
)
rq1_summary_output = gr.Markdown(label="Statistical Summary")
with gr.Column(scale=2):
rq1_plot_output = gr.Plot(label="Metric Comparison")
with gr.TabItem("RQ2: Predicting Correctness from Gaze"):
gr.Markdown("### Can we build a model to predict answer correctness from gaze patterns?")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("#### Tune Model Hyperparameters")
rq2_test_size_slider = gr.Slider(
minimum=0.1, maximum=0.5, step=0.05, value=0.3, label="Test Set Size"
)
rq2_estimators_slider = gr.Slider(
minimum=10, maximum=200, step=10, value=100, label="Number of Trees (n_estimators)"
)
rq2_report_output = gr.Markdown(label="Model Performance Report")
with gr.Column(scale=2):
rq2_plot_output = gr.Plot(label="Feature Importance")
# Wire up the interactive components
rq1_metric_dropdown.change(
fn=update_rq1_visuals,
inputs=[rq1_metric_dropdown],
outputs=[rq1_plot_output, rq1_summary_output]
)
# Use .release to only update when the user lets go of the slider
rq2_test_size_slider.release(
fn=update_rq2_model,
inputs=[rq2_test_size_slider, rq2_estimators_slider],
outputs=[rq2_report_output, rq2_plot_output]
)
rq2_estimators_slider.release(
fn=update_rq2_model,
inputs=[rq2_test_size_slider, rq2_estimators_slider],
outputs=[rq2_report_output, rq2_plot_output]
)
# Load initial state
demo.load(
fn=update_rq1_visuals,
inputs=[rq1_metric_dropdown],
outputs=[rq1_plot_output, rq1_summary_output]
)
demo.load(
fn=update_rq2_model,
inputs=[rq2_test_size_slider, rq2_estimators_slider],
outputs=[rq2_report_output, rq2_plot_output]
)
if __name__ == "__main__":
demo.launch() |