File size: 16,715 Bytes
c60d2d1 823b52c 94ba3a4 823b52c c60d2d1 823b52c c60d2d1 823b52c 91d4500 823b52c c60d2d1 823b52c c60d2d1 823b52c c60d2d1 823b52c c60d2d1 823b52c 20a37c7 52b9783 20a37c7 52b9783 20a37c7 52b9783 20a37c7 52b9783 20a37c7 52b9783 20a37c7 52b9783 20a37c7 52b9783 20a37c7 35c3293 20a37c7 35c3293 20a37c7 35c3293 20a37c7 52b9783 20a37c7 b7e5770 c60d2d1 35c3293 |
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 |
# import pandas as pd
# import os
# import re
# from huggingface_hub import InferenceClient
# class DataProcessor:
# INTERVENTION_COLUMN = 'Did the intervention happen today?'
# ENGAGED_STR = 'Engaged (Respect, Responsibility, Effort)'
# PARTIALLY_ENGAGED_STR = 'Partially Engaged (about 50%)'
# NOT_ENGAGED_STR = 'Not Engaged (less than 50%)'
# def __init__(self):
# self.hf_api_key = os.getenv('HF_API_KEY')
# if not self.hf_api_key:
# raise ValueError("HF_API_KEY not set in environment variables")
# self.client = InferenceClient(api_key=self.hf_api_key)
# def read_excel(self, uploaded_file):
# return pd.read_excel(uploaded_file)
# def format_session_data(self, df):
# df['Date of Session'] = self.safe_convert_to_datetime(df['Date of Session'], '%m/%d/%Y')
# df['Timestamp'] = self.safe_convert_to_datetime(df['Timestamp'], '%I:%M %p')
# df['Session Start Time'] = self.safe_convert_to_time(df['Session Start Time'], '%I:%M %p')
# df['Session End Time'] = self.safe_convert_to_time(df['Session End Time'], '%I:%M %p')
# df = df[['Date of Session', 'Timestamp'] + [col for col in df.columns if col not in ['Date of Session', 'Timestamp']]]
# return df
# def safe_convert_to_time(self, series, format_str='%I:%M %p'):
# try:
# converted = pd.to_datetime(series, format='%H:%M:%S', errors='coerce')
# if format_str:
# return converted.dt.strftime(format_str)
# return converted
# except Exception as e:
# print(f"Error converting series to time: {e}")
# return series
# def safe_convert_to_datetime(self, series, format_str=None):
# try:
# converted = pd.to_datetime(series, errors='coerce')
# if format_str:
# return converted.dt.strftime(format_str)
# return converted
# except Exception as e:
# print(f"Error converting series to datetime: {e}")
# return series
# def replace_student_names_with_initials(self, df):
# updated_columns = []
# for col in df.columns:
# if col.startswith('Student Attendance'):
# match = re.match(r'Student Attendance \[(.+?)\]', col)
# if match:
# name = match.group(1)
# name_parts = name.split()
# if len(name_parts) == 1:
# initials = name_parts[0][0]
# else:
# initials = ''.join([part[0] for part in name_parts])
# updated_columns.append(f'Student Attendance [{initials}]')
# else:
# updated_columns.append(col)
# else:
# updated_columns.append(col)
# df.columns = updated_columns
# return df
# def compute_intervention_statistics(self, df):
# total_days = len(df)
# sessions_held = df[self.INTERVENTION_COLUMN].str.strip().str.lower().eq('yes').sum()
# sessions_not_held = df[self.INTERVENTION_COLUMN].str.strip().str.lower().eq('no').sum()
# intervention_frequency = (sessions_held / total_days) * 100 if total_days > 0 else 0
# intervention_frequency = round(intervention_frequency, 0)
# stats = {
# 'Intervention Frequency (%)': [intervention_frequency],
# 'Intervention Sessions Held': [sessions_held],
# 'Intervention Sessions Not Held': [sessions_not_held],
# 'Total Number of Days Available': [total_days]
# }
# return pd.DataFrame(stats)
# def compute_student_metrics(self, df):
# intervention_df = df[df[self.INTERVENTION_COLUMN].str.strip().str.lower() == 'yes']
# intervention_sessions_held = len(intervention_df)
# student_columns = [col for col in df.columns if col.startswith('Student Attendance')]
# student_metrics = {}
# for col in student_columns:
# student_name = col.replace('Student Attendance [', '').replace(']', '').strip()
# student_data = intervention_df[[col]].copy()
# student_data[col] = student_data[col].fillna('Absent')
# attendance_values = student_data[col].apply(lambda x: 1 if x in [
# self.ENGAGED_STR,
# self.PARTIALLY_ENGAGED_STR,
# self.NOT_ENGAGED_STR
# ] else 0)
# sessions_attended = attendance_values.sum()
# attendance_pct = (sessions_attended / intervention_sessions_held) * 100 if intervention_sessions_held > 0 else 0
# attendance_pct = round(attendance_pct)
# engagement_counts = {
# 'Engaged': 0,
# 'Partially Engaged': 0,
# 'Not Engaged': 0,
# 'Absent': 0
# }
# for x in student_data[col]:
# if x == self.ENGAGED_STR:
# engagement_counts['Engaged'] += 1
# elif x == self.PARTIALLY_ENGAGED_STR:
# engagement_counts['Partially Engaged'] += 1
# elif x == self.NOT_ENGAGED_STR:
# engagement_counts['Not Engaged'] += 1
# else:
# engagement_counts['Absent'] += 1 # Count as Absent if not engaged
# # Calculate percentages for engagement states
# total_sessions = sum(engagement_counts.values())
# # Engagement (%)
# engagement_pct = (engagement_counts['Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
# engagement_pct = round(engagement_pct)
# engaged_pct = (engagement_counts['Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
# engaged_pct = round(engaged_pct)
# partially_engaged_pct = (engagement_counts['Partially Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
# partially_engaged_pct = round(partially_engaged_pct)
# not_engaged_pct = (engagement_counts['Not Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
# not_engaged_pct = round(not_engaged_pct)
# absent_pct = (engagement_counts['Absent'] / total_sessions * 100) if total_sessions > 0 else 0
# absent_pct = round(absent_pct)
# # Store metrics in the required order
# student_metrics[student_name] = {
# 'Attendance (%)': attendance_pct,
# 'Attendance #': sessions_attended, # Raw number of sessions attended
# 'Engagement (%)': engagement_pct,
# 'Engaged (%)': engaged_pct,
# 'Partially Engaged (%)': partially_engaged_pct,
# 'Not Engaged (%)': not_engaged_pct,
# 'Absent (%)': absent_pct
# }
# # Create a DataFrame from student_metrics
# student_metrics_df = pd.DataFrame.from_dict(student_metrics, orient='index').reset_index()
# student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
# return student_metrics_df
# def compute_average_metrics(self, student_metrics_df):
# # Calculate the attendance and engagement average percentages across students
# attendance_avg_stats = student_metrics_df['Attendance (%)'].mean() # Calculate the average attendance percentage
# engagement_avg_stats = student_metrics_df['Engagement (%)'].mean() # Calculate the average engagement percentage
# # Round the averages to make them whole numbers
# attendance_avg_stats = round(attendance_avg_stats)
# engagement_avg_stats = round(engagement_avg_stats)
# return attendance_avg_stats, engagement_avg_stats
import pandas as pd
import os
import re
from huggingface_hub import InferenceClient
# from graphviz import Digraph
class DataProcessor:
INTERVENTION_COLUMN = 'Did the intervention happen today?'
ENGAGED_STR = 'Engaged (Respect, Responsibility, Effort)'
PARTIALLY_ENGAGED_STR = 'Partially Engaged (about 50%)'
NOT_ENGAGED_STR = 'Not Engaged (less than 50%)'
def __init__(self, student_metrics_df=None):
self.hf_api_key = os.getenv('HF_API_KEY')
if not self.hf_api_key:
raise ValueError("HF_API_KEY not set in environment variables")
self.client = InferenceClient(api_key=self.hf_api_key)
self.student_metrics_df = student_metrics_df
def read_excel(self, uploaded_file):
return pd.read_excel(uploaded_file)
def format_session_data(self, df):
df['Date of Session'] = self.safe_convert_to_datetime(df['Date of Session'], '%m/%d/%Y')
df['Timestamp'] = self.safe_convert_to_datetime(df['Timestamp'], '%I:%M %p')
df['Session Start Time'] = self.safe_convert_to_time(df['Session Start Time'], '%I:%M %p')
df['Session End Time'] = self.safe_convert_to_time(df['Session End Time'], '%I:%M %p')
return df
def safe_convert_to_time(self, series, format_str='%I:%M %p'):
try:
converted = pd.to_datetime(series, format='%H:%M:%S', errors='coerce')
if format_str:
return converted.dt.strftime(format_str)
return converted
except Exception as e:
print(f"Error converting series to time: {e}")
return series
def safe_convert_to_datetime(self, series, format_str=None):
try:
converted = pd.to_datetime(series, errors='coerce')
if format_str:
return converted.dt.strftime(format_str)
return converted
except Exception as e:
print(f"Error converting series to datetime: {e}")
return series
def replace_student_names_with_initials(self, df):
updated_columns = []
for col in df.columns:
if col.startswith('Student Attendance'):
match = re.match(r'Student Attendance \[(.+?)\]', col)
if match:
name = match.group(1)
initials = ''.join([part[0] for part in name.split()])
updated_columns.append(f'Student Attendance [{initials}]')
else:
updated_columns.append(col)
else:
updated_columns.append(col)
df.columns = updated_columns
return df
def compute_intervention_statistics(self, df):
total_days = len(df)
sessions_held = df[self.INTERVENTION_COLUMN].str.strip().str.lower().eq('yes').sum()
intervention_frequency = (sessions_held / total_days) * 100 if total_days > 0 else 0
return pd.DataFrame({
'Intervention Frequency (%)': [round(intervention_frequency, 0)],
'Intervention Sessions Held': [sessions_held],
'Intervention Sessions Not Held': [total_days - sessions_held],
'Total Number of Days Available': [total_days]
})
def compute_student_metrics(self, df):
intervention_df = df[df[self.INTERVENTION_COLUMN].str.strip().str.lower() == 'yes']
intervention_sessions_held = len(intervention_df)
student_columns = [col for col in df.columns if col.startswith('Student Attendance')]
student_metrics = {}
for col in student_columns:
student_name = col.replace('Student Attendance [', '').replace(']', '').strip()
student_data = intervention_df[[col]].copy()
student_data[col] = student_data[col].fillna('Absent')
attendance_values = student_data[col].apply(lambda x: 1 if x in [
self.ENGAGED_STR,
self.PARTIALLY_ENGAGED_STR,
self.NOT_ENGAGED_STR
] else 0)
sessions_attended = attendance_values.sum()
attendance_pct = (sessions_attended / intervention_sessions_held) * 100 if intervention_sessions_held > 0 else 0
attendance_pct = round(attendance_pct)
engagement_counts = {
'Engaged': 0,
'Partially Engaged': 0,
'Not Engaged': 0,
'Absent': 0
}
for x in student_data[col]:
if x == self.ENGAGED_STR:
engagement_counts['Engaged'] += 1
elif x == self.PARTIALLY_ENGAGED_STR:
engagement_counts['Partially Engaged'] += 1
elif x == self.NOT_ENGAGED_STR:
engagement_counts['Not Engaged'] += 1
else:
engagement_counts['Absent'] += 1 # Count as Absent if not engaged
# Calculate percentages for engagement states
total_sessions = sum(engagement_counts.values())
# Engagement (%)
engagement_pct = (engagement_counts['Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
engagement_pct = round(engagement_pct)
engaged_pct = (engagement_counts['Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
engaged_pct = round(engaged_pct)
partially_engaged_pct = (engagement_counts['Partially Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
partially_engaged_pct = round(partially_engaged_pct)
not_engaged_pct = (engagement_counts['Not Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
not_engaged_pct = round(not_engaged_pct)
absent_pct = (engagement_counts['Absent'] / total_sessions * 100) if total_sessions > 0 else 0
absent_pct = round(absent_pct)
# Determine if the student attended ≥ 90% of sessions
attended_90 = "Yes" if attendance_pct >= 90 else "No"
# Determine if the student was engaged ≥ 80% of the time
engaged_80 = "Yes" if engaged_pct >= 80 else "No"
# Store metrics in the required order
student_metrics[student_name] = {
'Attended ≥ 90%': attended_90,
'Engagement ≥ 80%': engaged_80,
'Attendance (%)': attendance_pct,
# 'Attendance #': sessions_attended,
'Engagement (%)': engagement_pct,
'Engaged (%)': engaged_pct,
'Partially Engaged (%)': partially_engaged_pct,
'Not Engaged (%)': not_engaged_pct,
'Absent (%)': absent_pct
}
# Create a DataFrame from student_metrics
student_metrics_df = pd.DataFrame.from_dict(student_metrics, orient='index').reset_index()
student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
return student_metrics_df
def compute_average_metrics(self, student_metrics_df):
# Calculate the attendance and engagement average percentages across students
attendance_avg_stats = student_metrics_df['Attendance (%)'].mean() # Calculate the average attendance percentage
engagement_avg_stats = student_metrics_df['Engagement (%)'].mean() # Calculate the average engagement percentage
# Round the averages to make them whole numbers
attendance_avg_stats = round(attendance_avg_stats)
engagement_avg_stats = round(engagement_avg_stats)
return attendance_avg_stats, engagement_avg_stats
def evaluate_student(self, row, attendance_threshold=90, engagement_threshold=80):
if row["Attended ≥ 90%"] == "No":
return "Address Attendance"
elif row["Engagement ≥ 80%"] == "No":
return "Address Engagement"
return "Consider addressing logistical barriers, improving fidelity, and/or collecting progress monitoring data"
# def build_tree_diagram(self, row):
# dot = Digraph()
# dot.node("Q1", "Has the student attended ≥ 90% of interventions?")
# dot.node("Q2", "Has the student been engaged ≥ 80% of intervention time?")
# dot.node("A1", "Address Attendance", shape="box")
# dot.node("A2", "Address Engagement", shape="box")
# dot.node("A3", "Consider addressing logistical barriers", shape="box")
# if row["Attended ≥ 90%"] == "No":
# dot.edge("Q1", "A1", label="No")
# else:
# dot.edge("Q1", "Q2", label="Yes")
# dot.edge("Q2", "A2" if row["Engagement ≥ 80%"] == "No" else "A3", label="Yes")
# return dot
|