File size: 11,428 Bytes
da0dac8 | 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 | """
utils/data_loader.py
--------------------
Data ingestion utilities for teacher feedback datasets.
Supports:
- CSV upload
- Excel (.xlsx) upload
- Manual text entry (single feedback)
- Sample dataset generation for demo purposes
Expected CSV/Excel columns (flexible mapping supported):
- feedback_text : the raw feedback string (required)
- teacher_name : teacher identifier (optional)
- date : submission date (optional)
- subject : course / subject taught (optional)
- rating : numeric rating (1β5) (optional)
"""
from __future__ import annotations
import io
import logging
import random
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
import pandas as pd
import numpy as np
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Column aliases β maps common column name variants to canonical names
# ---------------------------------------------------------------------------
COLUMN_ALIASES: Dict[str, List[str]] = {
"feedback_text": [
"feedback", "comment", "comments", "review", "reviews",
"text", "feedback_text", "student_feedback", "response",
"review_text", "review text", "feedback text", "comments_text",
"comments text", "comment_text", "comment text", "response_text",
"response text", "reviews_text", "reviews text", "student_comment",
"student_comments", "student_review", "student_reviews", "evaluation",
"evaluations",
],
"teacher_name": [
"teacher", "teacher_name", "instructor", "professor",
"faculty", "lecturer", "name", "teacher name", "instructor name",
"professor name", "faculty name", "lecturer name", "staff name",
"staff", "educator", "educator name",
],
"date": [
"date", "submission_date", "created_at", "timestamp", "submitted_on",
"submission date", "submitted date", "date submitted", "created date",
"time", "datetime",
],
"subject": [
"subject", "course", "class", "module", "department", "subject name",
"course name", "class name", "module name", "course code", "course_code",
],
"rating": [
"rating", "score", "stars", "grade_given", "marks", "overall_rating",
"overall rating", "teacher rating", "teacher_rating", "rating score",
"score rating", "stars rating",
],
}
def _normalize_columns(df: pd.DataFrame) -> pd.DataFrame:
"""Rename columns to canonical names using alias mapping."""
rename_map: Dict[str, str] = {}
lower_cols = {c.lower().strip(): c for c in df.columns}
for canonical, aliases in COLUMN_ALIASES.items():
for alias in aliases:
if alias in lower_cols and canonical not in df.columns:
rename_map[lower_cols[alias]] = canonical
break
return df.rename(columns=rename_map)
def load_csv(file_obj) -> pd.DataFrame:
"""
Load feedback data from a CSV file object (e.g., Streamlit UploadedFile).
Returns
-------
pd.DataFrame with normalized column names.
"""
try:
df = pd.read_csv(file_obj, encoding="utf-8")
except UnicodeDecodeError:
file_obj.seek(0)
df = pd.read_csv(file_obj, encoding="latin-1")
df = _normalize_columns(df)
df = _clean_dataframe(df)
logger.info(f"Loaded CSV: {len(df)} rows, columns={list(df.columns)}")
return df
def load_excel(file_obj) -> pd.DataFrame:
"""
Load feedback data from an Excel file object.
Returns
-------
pd.DataFrame with normalized column names.
"""
df = pd.read_excel(file_obj, engine="openpyxl")
df = _normalize_columns(df)
df = _clean_dataframe(df)
logger.info(f"Loaded Excel: {len(df)} rows, columns={list(df.columns)}")
return df
def _clean_dataframe(df: pd.DataFrame) -> pd.DataFrame:
"""Drop empty rows and ensure feedback_text column exists."""
df = df.dropna(how="all")
if "feedback_text" not in df.columns:
# Attempt to use first string column as feedback text
str_cols = df.select_dtypes(include="object").columns.tolist()
if str_cols:
df = df.rename(columns={str_cols[0]: "feedback_text"})
logger.warning(
f"No 'feedback_text' column found. Using '{str_cols[0]}' as feedback."
)
else:
raise ValueError(
"Dataset must contain a text column for feedback. "
"Expected column name: 'feedback', 'comment', 'review', or 'text'."
)
df["feedback_text"] = df["feedback_text"].astype(str).str.strip()
df = df[df["feedback_text"].str.len() > 5]
# Parse dates if present
if "date" in df.columns:
df["date"] = pd.to_datetime(df["date"], errors="coerce")
# Normalize rating to 0β100 if present
if "rating" in df.columns:
df["rating"] = pd.to_numeric(df["rating"], errors="coerce")
max_r = df["rating"].max()
if max_r and max_r <= 10:
df["rating_normalized"] = (df["rating"] / max_r * 100).round(1)
elif max_r and max_r <= 5:
df["rating_normalized"] = (df["rating"] / 5 * 100).round(1)
else:
df["rating_normalized"] = df["rating"]
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Sample dataset generator
# ---------------------------------------------------------------------------
SAMPLE_TEACHERS = [
"Dr. Aisha Khan",
"Prof. Ravi Sharma",
"Ms. Priya Nair",
"Mr. James Okafor",
"Dr. Emily Chen",
]
SAMPLE_SUBJECTS = [
"Mathematics", "Physics", "Computer Science",
"English Literature", "Chemistry",
]
POSITIVE_FEEDBACK = [
"An exceptional teacher who explains concepts with remarkable clarity and patience.",
"Very engaging and knowledgeable. Makes even difficult topics easy to understand.",
"Always available for doubt-clearing sessions. Truly passionate about teaching.",
"The best teacher I've had. Innovative teaching methods and great communication skills.",
"Provides excellent real-world examples and keeps the class very interactive.",
"Highly professional and punctual. Course content is well-structured and relevant.",
"Great at making students feel comfortable to ask questions. Very supportive.",
"Thoroughly prepares lessons and gives constructive feedback on assignments.",
"Dynamic and enthusiastic. Classes are never boring, always something new to learn.",
"Subject knowledge is outstanding. Explains the 'why' behind every concept.",
]
NEUTRAL_FEEDBACK = [
"The teacher covers the syllabus adequately. Could use more practical examples.",
"Teaching is satisfactory. Some topics could be explained in more depth.",
"Classes are on time and content is standard. Nothing exceptional but no issues.",
"Good knowledge of the subject but needs to improve student interaction.",
"Assignments are fair. The teaching pace is sometimes too fast.",
"Average experience. The teacher is available but not very proactive.",
"Lectures are informative but could be more engaging for students.",
"The course content is relevant though the delivery can be monotonous at times.",
]
NEGATIVE_FEEDBACK = [
"Very difficult to understand due to fast speaking pace and unclear explanations.",
"Often arrives late to class which is very disrespectful of students' time.",
"The feedback on assignments is vague and not helpful for improvement.",
"Does not engage with students. Lectures are one-sided and boring.",
"Limited knowledge beyond the textbook. Cannot answer deeper questions.",
"Very poor classroom management. Class is frequently chaotic and unproductive.",
"Course content feels outdated and not aligned with current industry standards.",
"The teacher is unapproachable and dismissive when students ask for help.",
]
def generate_sample_dataset(
n_rows: int = 150,
n_teachers: int = 5,
seed: int = 42,
) -> pd.DataFrame:
"""
Generate a realistic sample dataset for demonstration purposes.
Parameters
----------
n_rows : int
Number of feedback entries to generate.
n_teachers : int
Number of teachers (capped at len(SAMPLE_TEACHERS)).
seed : int
Random seed for reproducibility.
Returns
-------
pd.DataFrame
"""
random.seed(seed)
np.random.seed(seed)
teachers = SAMPLE_TEACHERS[:min(n_teachers, len(SAMPLE_TEACHERS))]
subjects = SAMPLE_SUBJECTS[:min(n_teachers, len(SAMPLE_SUBJECTS))]
teacher_subject_map = dict(zip(teachers, subjects))
# Weighted feedback distribution: 50% pos, 30% neutral, 20% neg
all_feedback = (
POSITIVE_FEEDBACK * 5 + NEUTRAL_FEEDBACK * 3 + NEGATIVE_FEEDBACK * 2
)
start_date = datetime(2024, 1, 1)
end_date = datetime(2024, 12, 31)
date_range = (end_date - start_date).days
rows = []
for _ in range(n_rows):
teacher = random.choice(teachers)
feedback = random.choice(all_feedback)
rating_base = (
random.uniform(3.5, 5.0) if feedback in POSITIVE_FEEDBACK
else random.uniform(2.5, 3.5) if feedback in NEUTRAL_FEEDBACK
else random.uniform(1.0, 2.5)
)
date = start_date + timedelta(days=random.randint(0, date_range))
rows.append({
"feedback_text": feedback,
"teacher_name": teacher,
"subject": teacher_subject_map[teacher],
"rating": round(rating_base, 1),
"date": date.strftime("%Y-%m-%d"),
})
df = pd.DataFrame(rows)
return _clean_dataframe(df)
def validate_dataset(df: pd.DataFrame) -> Dict[str, Any]:
"""
Validate a loaded dataset and return diagnostic information.
Returns
-------
dict
- ``is_valid`` : bool
- ``n_rows`` : int
- ``has_teacher_col`` : bool
- ``has_date_col`` : bool
- ``has_rating_col`` : bool
- ``missing_pct`` : float (% of empty feedback cells)
- ``warnings`` : list[str]
"""
warnings: List[str] = []
is_valid = True
if "feedback_text" not in df.columns:
is_valid = False
warnings.append("Missing required column: feedback_text")
missing_pct = 0.0
if "feedback_text" in df.columns:
missing = df["feedback_text"].isna().sum() + (df["feedback_text"] == "").sum()
missing_pct = round(missing / len(df) * 100, 2) if len(df) > 0 else 0.0
if missing_pct > 20:
warnings.append(f"{missing_pct}% of feedback entries are empty or missing.")
has_teacher = "teacher_name" in df.columns
has_date = "date" in df.columns
has_rating = "rating" in df.columns
if not has_teacher:
warnings.append("No 'teacher_name' column found β analysis will be aggregated.")
if not has_date:
warnings.append("No 'date' column found β trend analysis will be unavailable.")
return {
"is_valid": is_valid,
"n_rows": len(df),
"has_teacher_col": has_teacher,
"has_date_col": has_date,
"has_rating_col": has_rating,
"missing_pct": missing_pct,
"warnings": warnings,
}
|