ScoDoc-Lille/app/comp/moy_ue.py

60 lines
2.3 KiB
Python

# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
"""Fonctions de calcul des moyennes d'UE
"""
import numpy as np
import pandas as pd
from app import db
from app import models
def df_load_ue_coefs(formation_id: int, semestre_idx: int) -> pd.DataFrame:
"""Load coefs of all modules in formation and returns a DataFrame
rows = UEs, columns = modules, value = coef.
On considère toutes les UE et modules du semestre.
Unspecified coefs (not defined in db) are set to zero.
Si semestre_idx None, prend toutes les UE de la formation.
"""
ues = models.UniteEns.query.filter_by(formation_id=formation_id)
modules = models.Module.query.filter_by(formation_id=formation_id)
if semestre_idx is not None:
ues = ues.filter_by(semestre_idx=semestre_idx)
modules = modules.filter_by(semestre_id=semestre_idx)
ue_ids = [ue.id for ue in ues]
module_ids = [module.id for module in modules]
df = pd.DataFrame(columns=module_ids, index=ue_ids, dtype=float)
for mod_coef in (
db.session.query(models.ModuleUECoef)
.filter(models.UniteEns.formation_id == formation_id)
.filter(models.ModuleUECoef.ue_id == models.UniteEns.id)
):
df[mod_coef.module_id][mod_coef.ue_id] = mod_coef.coef
df.fillna(value=0, inplace=True)
return df, ues, modules