2021-12-30 23:58:38 +01:00
|
|
|
##############################################################################
|
|
|
|
# ScoDoc
|
2022-01-01 14:51:28 +01:00
|
|
|
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
|
2021-12-30 23:58:38 +01:00
|
|
|
# See LICENSE
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""Résultats semestres classiques (non APC)
|
|
|
|
"""
|
2022-02-07 16:32:04 +01:00
|
|
|
|
2021-12-30 23:58:38 +01:00
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
2022-02-07 16:32:04 +01:00
|
|
|
from sqlalchemy.sql import text
|
|
|
|
|
|
|
|
from flask import g, url_for
|
2022-01-16 23:47:52 +01:00
|
|
|
|
2022-02-07 16:32:04 +01:00
|
|
|
from app import db
|
|
|
|
from app import log
|
2022-02-06 18:29:22 +01:00
|
|
|
from app.comp import moy_mod, moy_ue, inscr_mod
|
2022-01-07 15:08:45 +01:00
|
|
|
from app.comp.res_common import NotesTableCompat
|
2022-01-25 10:45:13 +01:00
|
|
|
from app.comp.bonus_spo import BonusSport
|
|
|
|
from app.models import ScoDocSiteConfig
|
2022-02-07 16:32:04 +01:00
|
|
|
from app.models.etudiants import Identite
|
2021-12-30 23:58:38 +01:00
|
|
|
from app.models.formsemestre import FormSemestre
|
2022-02-07 16:32:04 +01:00
|
|
|
from app.models.ues import UniteEns
|
2022-01-25 10:45:13 +01:00
|
|
|
from app.scodoc.sco_codes_parcours import UE_SPORT
|
2022-02-07 16:32:04 +01:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2022-01-25 10:45:13 +01:00
|
|
|
from app.scodoc.sco_utils import ModuleType
|
2021-12-30 23:58:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ResultatsSemestreClassic(NotesTableCompat):
|
|
|
|
"""Résultats du semestre (formation classique): organisation des calculs."""
|
|
|
|
|
|
|
|
_cached_attrs = NotesTableCompat._cached_attrs + (
|
|
|
|
"modimpl_coefs",
|
|
|
|
"modimpl_idx",
|
|
|
|
"sem_matrix",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, formsemestre):
|
|
|
|
super().__init__(formsemestre)
|
|
|
|
|
|
|
|
if not self.load_cached():
|
|
|
|
self.compute()
|
|
|
|
self.store()
|
|
|
|
# recalculé (aussi rapide que de les cacher)
|
|
|
|
self.moy_min = self.etud_moy_gen.min()
|
|
|
|
self.moy_max = self.etud_moy_gen.max()
|
|
|
|
self.moy_moy = self.etud_moy_gen.mean()
|
|
|
|
|
|
|
|
def compute(self):
|
|
|
|
"Charge les notes et inscriptions et calcule les moyennes d'UE et gen."
|
|
|
|
self.sem_matrix, self.modimpls_results = notes_sem_load_matrix(
|
|
|
|
self.formsemestre
|
|
|
|
)
|
|
|
|
self.modimpl_inscr_df = inscr_mod.df_load_modimpl_inscr(self.formsemestre)
|
|
|
|
self.modimpl_coefs = np.array(
|
2022-01-25 10:45:13 +01:00
|
|
|
[m.module.coefficient for m in self.formsemestre.modimpls_sorted]
|
2021-12-30 23:58:38 +01:00
|
|
|
)
|
2022-01-25 10:45:13 +01:00
|
|
|
self.modimpl_idx = {
|
|
|
|
m.id: i for i, m in enumerate(self.formsemestre.modimpls_sorted)
|
|
|
|
}
|
2021-12-30 23:58:38 +01:00
|
|
|
"l'idx de la colonne du mod modimpl.id est modimpl_idx[modimpl.id]"
|
|
|
|
|
2022-01-25 10:45:13 +01:00
|
|
|
modimpl_standards_mask = np.array(
|
|
|
|
[
|
|
|
|
(m.module.module_type == ModuleType.STANDARD)
|
|
|
|
and (m.module.ue.type != UE_SPORT)
|
|
|
|
for m in self.formsemestre.modimpls_sorted
|
|
|
|
]
|
|
|
|
)
|
2022-01-16 23:47:52 +01:00
|
|
|
(
|
|
|
|
self.etud_moy_gen,
|
|
|
|
self.etud_moy_ue,
|
|
|
|
self.etud_coef_ue_df,
|
|
|
|
) = moy_ue.compute_ue_moys_classic(
|
2021-12-30 23:58:38 +01:00
|
|
|
self.formsemestre,
|
|
|
|
self.sem_matrix,
|
|
|
|
self.ues,
|
|
|
|
self.modimpl_inscr_df,
|
|
|
|
self.modimpl_coefs,
|
2022-01-25 10:45:13 +01:00
|
|
|
modimpl_standards_mask,
|
2021-12-30 23:58:38 +01:00
|
|
|
)
|
2022-02-01 11:37:05 +01:00
|
|
|
# --- Modules de MALUS sur les UEs et la moyenne générale
|
|
|
|
self.malus = moy_ue.compute_malus(
|
|
|
|
self.formsemestre, self.sem_matrix, self.ues, self.modimpl_inscr_df
|
|
|
|
)
|
|
|
|
self.etud_moy_ue -= self.malus
|
|
|
|
# ajuste la moyenne générale (à l'aide des coefs d'UE)
|
|
|
|
self.etud_moy_gen -= (self.etud_coef_ue_df * self.malus).sum(
|
|
|
|
axis=1
|
|
|
|
) / self.etud_coef_ue_df.sum(axis=1)
|
|
|
|
|
2022-01-25 10:45:13 +01:00
|
|
|
# --- Bonus Sport & Culture
|
|
|
|
bonus_class = ScoDocSiteConfig.get_bonus_sport_class()
|
|
|
|
if bonus_class is not None:
|
|
|
|
bonus: BonusSport = bonus_class(
|
|
|
|
self.formsemestre,
|
|
|
|
self.sem_matrix,
|
|
|
|
self.ues,
|
|
|
|
self.modimpl_inscr_df,
|
|
|
|
self.modimpl_coefs,
|
2022-01-26 20:38:27 +01:00
|
|
|
self.etud_moy_gen,
|
|
|
|
self.etud_moy_ue,
|
2022-01-25 10:45:13 +01:00
|
|
|
)
|
|
|
|
self.bonus_ues = bonus.get_bonus_ues()
|
|
|
|
if self.bonus_ues is not None:
|
|
|
|
self.etud_moy_ue += self.bonus_ues # somme les dataframes
|
2022-01-26 22:59:53 +01:00
|
|
|
self.etud_moy_ue.clip(lower=0.0, upper=20.0, inplace=True)
|
2022-01-25 10:45:13 +01:00
|
|
|
bonus_mg = bonus.get_bonus_moy_gen()
|
|
|
|
if bonus_mg is not None:
|
|
|
|
self.etud_moy_gen += bonus_mg
|
2022-01-26 22:59:53 +01:00
|
|
|
self.etud_moy_gen.clip(lower=0.0, upper=20.0, inplace=True)
|
2022-01-25 10:45:13 +01:00
|
|
|
self.bonus = (
|
|
|
|
bonus_mg # compat nt, utilisé pour l'afficher sur les bulletins
|
|
|
|
)
|
2022-02-07 16:32:04 +01:00
|
|
|
# --- UE capitalisées
|
|
|
|
self.apply_capitalisation()
|
|
|
|
|
2022-01-25 10:45:13 +01:00
|
|
|
# --- Classements:
|
2022-02-06 18:29:22 +01:00
|
|
|
self.compute_rangs()
|
2021-12-30 23:58:38 +01:00
|
|
|
|
|
|
|
def get_etud_mod_moy(self, moduleimpl_id: int, etudid: int) -> float:
|
|
|
|
"""La moyenne de l'étudiant dans le moduleimpl
|
|
|
|
Result: valeur float (peut être NaN) ou chaîne "NI" (non inscrit ou DEM)
|
|
|
|
"""
|
|
|
|
return self.modimpls_results[moduleimpl_id].etuds_moy_module.get(etudid, "NI")
|
|
|
|
|
2022-01-07 10:37:48 +01:00
|
|
|
def get_mod_stats(self, moduleimpl_id: int) -> dict:
|
|
|
|
"""Stats sur les notes obtenues dans un modimpl"""
|
|
|
|
notes_series: pd.Series = self.modimpls_results[moduleimpl_id].etuds_moy_module
|
|
|
|
nb_notes = len(notes_series)
|
|
|
|
if not nb_notes:
|
|
|
|
super().get_mod_stats(moduleimpl_id)
|
|
|
|
return {
|
|
|
|
# Series: Statistical methods from ndarray have been overridden to automatically
|
|
|
|
# exclude missing data (currently represented as NaN)
|
|
|
|
"moy": notes_series.mean(), # donc sans prendre en compte les NaN
|
|
|
|
"max": notes_series.max(),
|
|
|
|
"min": notes_series.min(),
|
|
|
|
"nb_notes": nb_notes,
|
|
|
|
"nb_missing": sum(notes_series.isna()),
|
|
|
|
"nb_valid_evals": sum(
|
|
|
|
self.modimpls_results[moduleimpl_id].evaluations_completes
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:32:04 +01:00
|
|
|
def compute_etud_ue_coef(self, etudid: int, ue: UniteEns) -> float:
|
|
|
|
"""Détermine le coefficient de l'UE pour cet étudiant.
|
|
|
|
N'est utilisé que pour l'injection des UE capitalisées dans la
|
|
|
|
moyenne générale.
|
|
|
|
Coef = somme des coefs des modules de l'UE auxquels il est inscrit
|
|
|
|
"""
|
|
|
|
c = comp_etud_sum_coef_modules_ue(self.formsemestre.id, etudid, ue["ue_id"])
|
|
|
|
if c is not None: # inscrit à au moins un module de cette UE
|
|
|
|
return c
|
|
|
|
# arfff: aucun moyen de déterminer le coefficient de façon sûre
|
|
|
|
log(
|
|
|
|
"* oups: calcul coef UE impossible\nformsemestre_id='%s'\netudid='%s'\nue=%s"
|
|
|
|
% (self.formsemestre.id, etudid, ue)
|
|
|
|
)
|
|
|
|
etud: Identite = Identite.query.get(etudid)
|
|
|
|
raise ScoValueError(
|
|
|
|
"""<div class="scovalueerror"><p>Coefficient de l'UE capitalisée %s impossible à déterminer
|
|
|
|
pour l'étudiant <a href="%s" class="discretelink">%s</a></p>
|
|
|
|
<p>Il faut <a href="%s">saisir le coefficient de cette UE avant de continuer</a></p>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
% (
|
|
|
|
ue.acronyme,
|
|
|
|
url_for("scolar.ficheEtud", scodoc_dept=g.scodoc_dept, etudid=etudid),
|
|
|
|
etud.nom_disp(),
|
|
|
|
url_for(
|
|
|
|
"notes.formsemestre_edit_uecoefs",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
formsemestre_id=self.formsemestre.id,
|
|
|
|
err_ue_id=ue["ue_id"],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return 0.0 # ?
|
|
|
|
|
2021-12-30 23:58:38 +01:00
|
|
|
|
2022-01-25 10:45:13 +01:00
|
|
|
def notes_sem_load_matrix(formsemestre: FormSemestre) -> tuple[np.ndarray, dict]:
|
2021-12-30 23:58:38 +01:00
|
|
|
"""Calcule la matrice des notes du semestre
|
2022-01-25 10:45:13 +01:00
|
|
|
(charge toutes les notes, calcule les moyennes des modules
|
2021-12-30 23:58:38 +01:00
|
|
|
et assemble la matrice)
|
|
|
|
Resultat:
|
|
|
|
sem_matrix : 2d-array (etuds x modimpls)
|
|
|
|
modimpls_results dict { modimpl.id : ModuleImplResultsClassic }
|
|
|
|
"""
|
|
|
|
modimpls_results = {}
|
|
|
|
modimpls_notes = []
|
2022-01-25 10:45:13 +01:00
|
|
|
for modimpl in formsemestre.modimpls_sorted:
|
2021-12-30 23:58:38 +01:00
|
|
|
mod_results = moy_mod.ModuleImplResultsClassic(modimpl)
|
|
|
|
etuds_moy_module = mod_results.compute_module_moy()
|
|
|
|
modimpls_results[modimpl.id] = mod_results
|
|
|
|
modimpls_notes.append(etuds_moy_module)
|
|
|
|
return (
|
|
|
|
notes_sem_assemble_matrix(modimpls_notes),
|
|
|
|
modimpls_results,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def notes_sem_assemble_matrix(modimpls_notes: list[pd.Series]) -> np.ndarray:
|
|
|
|
"""Réuni les notes moyennes des modules du semestre en une matrice
|
|
|
|
|
|
|
|
modimpls_notes : liste des moyennes de module
|
|
|
|
(Series rendus par compute_module_moy, index: etud)
|
|
|
|
Resultat: ndarray (etud x module)
|
|
|
|
"""
|
|
|
|
modimpls_notes_arr = [s.values for s in modimpls_notes]
|
|
|
|
modimpls_notes = np.stack(modimpls_notes_arr)
|
|
|
|
# passe de (mod x etud) à (etud x mod)
|
|
|
|
return modimpls_notes.T
|
2022-02-07 16:32:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def comp_etud_sum_coef_modules_ue(formsemestre_id, etudid, ue_id):
|
|
|
|
"""Somme des coefficients des modules de l'UE dans lesquels cet étudiant est inscrit
|
|
|
|
ou None s'il n'y a aucun module.
|
|
|
|
"""
|
|
|
|
# comme l'ancien notes_table.comp_etud_sum_coef_modules_ue
|
|
|
|
# mais en raw sqlalchemy et la somme en SQL
|
|
|
|
sql = text(
|
|
|
|
"""
|
|
|
|
SELECT sum(mod.coefficient)
|
|
|
|
FROM notes_modules mod, notes_moduleimpl mi, notes_moduleimpl_inscription ins
|
|
|
|
WHERE mod.id = mi.module_id
|
|
|
|
and ins.etudid = :etudid
|
|
|
|
and ins.moduleimpl_id = mi.id
|
|
|
|
and mi.formsemestre_id = :formsemestre_id
|
|
|
|
and mod.ue_id = :ue_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cursor = db.session.execute(
|
|
|
|
sql, {"etudid": etudid, "formsemestre_id": formsemestre_id, "ue_id": ue_id}
|
|
|
|
)
|
|
|
|
r = cursor.fetchone()
|
|
|
|
if r is None:
|
|
|
|
return None
|
|
|
|
return r[0]
|