File size: 6,055 Bytes
823b52c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

            total_sessions = sum(engagement_counts.values())
            engagement_pct = (engagement_counts['Engaged'] / total_sessions * 100) if total_sessions > 0 else 0
            engagement_pct = round(engagement_pct)

            student_metrics[student_name] = {
                'Attendance (%)': attendance_pct,
                'Attendance #': sessions_attended,
                'Engagement (%)': engagement_pct
            }

        return pd.DataFrame.from_dict(student_metrics, orient='index').reset_index().rename(columns={'index': 'Student'})

    def compute_average_metrics(self, student_metrics_df):
        attendance_avg_stats = student_metrics_df['Attendance (%)'].mean()
        engagement_avg_stats = student_metrics_df['Engagement (%)'].mean()
        return round(attendance_avg_stats), round(engagement_avg_stats)