degree_analysis.py 6.79 KB
Newer Older
João Denis Rodrigues's avatar
João Denis Rodrigues committed
1
2
3
import pandas as pd
import numpy as np
import math
4
5
from utils.situations import Situation, EvasionForm

João Denis Rodrigues's avatar
João Denis Rodrigues committed
6
7

def average_graduation(df):
8
9
10
    total_student = df['MATR_ALUNO'].drop_duplicates().shape[0]
    total_graduate = df[df.FORMA_EVASAO == EvasionForm.EF_FORMATURA].shape[0]

João Denis Rodrigues's avatar
João Denis Rodrigues committed
11
12
    return total_graduate / total_student

13

João Denis Rodrigues's avatar
João Denis Rodrigues committed
14
def general_failure(df):
15
    affect_ira = df[df.SITUACAO.isin(Situation.SITUATION_AFFECT_IRA)]
João Denis Rodrigues's avatar
João Denis Rodrigues committed
16
    failures = affect_ira[affect_ira.SITUACAO.isin(Situation.SITUATION_FAIL)]
17

João Denis Rodrigues's avatar
João Denis Rodrigues committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    average = failures.shape[0] / affect_ira.shape[0]

    student_courses = affect_ira.groupby(['MATR_ALUNO'], as_index=False)\
                                .aggregate({'SITUACAO': 'count'})
    student_failures = failures.groupby(['MATR_ALUNO'], as_index=False)\
                               .aggregate({'SITUACAO': 'count'})

    merged = pd.merge(student_courses, student_failures, on=['MATR_ALUNO'])
    merged.columns = ['MART_ALUNO', 'FEITAS', 'REPROVADO']
    variance = merged['REPROVADO'].div(merged['FEITAS']).sub(average)\
                                      .pow(2).sum() / merged.shape[0]
    standard_deviation = math.sqrt(variance)
    return (average, standard_deviation)

32

Legton's avatar
Legton committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def current_students_failure(df):
    fixed = df.loc[(df.FORMA_EVASAO == EvasionForm.EF_ATIVO)]
    affect_ira = fixed[fixed.SITUACAO.isin(Situation.SITUATION_AFFECT_IRA)]
    failures = affect_ira[affect_ira.SITUACAO.isin(Situation.SITUATION_FAIL)]

    average = failures.shape[0] / affect_ira.shape[0]

    student_courses = affect_ira.groupby(['MATR_ALUNO'], as_index=False)\
                                .aggregate({'SITUACAO': 'count'})
    student_failures = failures.groupby(['MATR_ALUNO'], as_index=False)\
                               .aggregate({'SITUACAO': 'count'})

    merged = pd.merge(student_courses, student_failures, on=['MATR_ALUNO'])
    merged.columns = ['MART_ALUNO', 'FEITAS', 'REPROVADO']
    variance = merged['REPROVADO'].div(merged['FEITAS']).sub(average)\
                                      .pow(2).sum() / merged.shape[0]
    standard_deviation = math.sqrt(variance)
    return (average, standard_deviation)

João Denis Rodrigues's avatar
João Denis Rodrigues committed
52
def general_ira(df):
53
    fixed = df[df.SITUACAO.isin(Situation.SITUATION_AFFECT_IRA)]
João Denis Rodrigues's avatar
João Denis Rodrigues committed
54
55
    fixed = fixed[fixed.MEDIA_FINAL <= 100]
    return (fixed.MEDIA_FINAL.mean(), fixed.MEDIA_FINAL.std())
Legton's avatar
Legton committed
56

Legton's avatar
Legton committed
57
def current_ira(df):
58
59
60
61
62
    ano_grade = int(df.loc[df['NUM_VERSAO'].idxmax()]['NUM_VERSAO'])
    fixed = df.loc[(df['NUM_VERSAO'] == ano_grade)]
    fixed = fixed[fixed.SITUACAO.isin(Situation.SITUATION_AFFECT_IRA)]
    fixed = fixed[fixed.MEDIA_FINAL <= 100]
    return (fixed.MEDIA_FINAL.mean(), fixed.MEDIA_FINAL.std())
Legton's avatar
Legton committed
63

64
def current_students_ira(df):
Legton's avatar
Legton committed
65
66
67
68
    fixed = df.loc[(df.FORMA_EVASAO == EvasionForm.EF_ATIVO)]
    fixed = fixed[fixed.SITUACAO.isin(Situation.SITUATION_AFFECT_IRA)]
    fixed = fixed[fixed.MEDIA_FINAL <= 100]
    return (fixed.MEDIA_FINAL.mean(), fixed.MEDIA_FINAL.std())
Legton's avatar
Legton committed
69

Legton's avatar
Legton committed
70
def general_evasion_rate(df):
Legton's avatar
Legton committed
71
72
73
    students = df['MATR_ALUNO'].drop_duplicates()
    total_student = students.shape[0]
    total_evasion = students.loc[(df.FORMA_EVASAO != EvasionForm.EF_ATIVO) & (df.FORMA_EVASAO != EvasionForm.EF_FORMATURA) & (df.FORMA_EVASAO != EvasionForm.EF_REINTEGRACAO)].shape[0]
Legton's avatar
Legton committed
74
75
76
77
78
79
80
81
82

    return total_evasion / total_student

def current_evasion_rate(df):
    ano_grade = int(df.loc[df['NUM_VERSAO'].idxmax()]['NUM_VERSAO'])
    students = df.loc[(df['NUM_VERSAO'] == ano_grade)]
    students = students['MATR_ALUNO'].drop_duplicates()
    total_student = students.shape[0]
    total_evasion = students.loc[(df.FORMA_EVASAO != EvasionForm.EF_ATIVO) & (df.FORMA_EVASAO != EvasionForm.EF_FORMATURA) & (df.FORMA_EVASAO != EvasionForm.EF_REINTEGRACAO)].shape[0]
Legton's avatar
Legton committed
83
84

    return total_evasion / total_student
Legton's avatar
Legton committed
85
86

def average_graduation_time(df):
Legton's avatar
Legton committed
87
    graduates = df.loc[(df.FORMA_EVASAO == EvasionForm.EF_FORMATURA)]
Legton's avatar
Legton committed
88
89
    total_graduate = graduates.shape[0]
    average_time = 0
Legton's avatar
Legton committed
90
91
    year_end = int(df['ANO'].max())
    semester_end = graduates['PERIODO'].max()
Legton's avatar
Legton committed
92
    for index, row in graduates.iterrows():
Legton's avatar
Legton committed
93
94
        if pd.notnull(row['ANO_EVASAO']):
            year_end = int(row['ANO_EVASAO'])
Legton's avatar
Legton committed
95
            try: 
Legton's avatar
Legton committed
96
                semester_end = int(row['SEMESTRE_EVASAO'])
Legton's avatar
Legton committed
97
            except ValueError:
Legton's avatar
Legton committed
98
                semester_end = graduates['PERIODO'].max()
Legton's avatar
Legton committed
99
100
        year = int(row['ANO_INGRESSO'])
        semester = int(row['SEMESTRE_INGRESSO'])
Legton's avatar
Legton committed
101
102
103
104
        difference = 2 * (year_end - year) + (semester_end - semester) + 1
        average_time += difference
    average_time /= total_graduate
    average_time /= 2
Legton's avatar
Legton committed
105

Legton's avatar
Legton committed
106
107
108
    return average_time

def total_students(df):
Legton's avatar
Legton committed
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
    return df.loc[(df.FORMA_EVASAO == EvasionForm.EF_ATIVO)].drop_duplicates('MATR_ALUNO').shape[0]

def taxa_abandono(df):
    students = df['MATR_ALUNO'].drop_duplicates()
    total_student = students.shape[0]
    total_abandono = students.loc[(df.FORMA_EVASAO == EvasionForm.EF_ABANDONO)].shape[0]

    return total_abandono / total_student

def average_ira_graph(df):
    alunos = df.drop_duplicates('MATR_ALUNO')

    dic = build_dict_ira_medio(alunos)

    return dic

def current_students_average_ira_graph(df):
    alunos_se = df.loc[(df.FORMA_EVASAO == EvasionForm.EF_ATIVO)]
    alunos_se = alunos_se.drop_duplicates('MATR_ALUNO')

    dic_se = build_dict_ira_medio(alunos_se)

    return dic_se

def graduates_average_ira_graph(df):
    alunos_for = df.loc[(df.FORMA_EVASAO == EvasionForm.EF_FORMATURA)]
    alunos_for = alunos_for.drop_duplicates('MATR_ALUNO')

    dic_for = build_dict_ira_medio(alunos_for)

    return dic_for

def period_evasion_graph(df):
    di_qtd = {}
    dic = {}
    evasions_total = 0
    year_start = int(df['ANO'].min())
    year_end = int(df['ANO'].max()) + 1
    students = df.drop_duplicates()
    for year in range(year_start, year_end):
        for semester in range(1, 3):
            evasions = students.loc[(df['ANO_EVASAO'] == str(year)) & (df['SEMESTRE_EVASAO'] == str(semester))].shape[0]
            date = str(year) + ' {}º Período'.format(semester)
            di_qtd[date] = evasions
            evasions_total += evasions
    if evasions_total:
        for di in di_qtd:
            qtd = di_qtd[di]
            dic[di] = {'qtd': qtd, 'taxa': (qtd/evasions_total)*100}

    return dic

def build_dict_ira_medio(alunos):
    dic = {"00-4.9":0, "05-9.9":0, "10-14.9":0, "15-19.9":0, "20-24.9":0, "25-29.9":0, "30-34.9":0,
           "35-39.9":0, "40-44.9":0, "45-49.9":0, "50-54.9":0, "55-59.9":0, "60-64.9":0, "65-69.9":0,
           "70-74.9":0, "75-79.9":0, "80-84.9":0, "85-89.9":0, "90-94.9": 0,"95-100":0}

    iras = []
    for index, row in alunos.iterrows():
        if(row['MEDIA_FINAL'] is not None):
            iras.append(row['MEDIA_FINAL'])

    for d in dic:
        aux = d.split('-')
        v1 = float(aux[0])
        if (v1 == 0.0):
            v1 += 0.01
        v2 = float(aux[1])
        dic[d] = sum((float(num) >= v1) and (float(num) < v2) for num in iras)

    return dic