Feat: filter none numeric scores

This commit is contained in:
Bertrand Benjamin 2021-04-19 14:30:37 +02:00
parent 7e6b24aaea
commit 2e86b3a0a2
2 changed files with 63 additions and 4 deletions

View File

@ -10,7 +10,7 @@ def score_to_mark(x, score_max, rounding=lambda x: round(x, 2)):
if the item is leveled then the score is multiply by the score_rate if the item is leveled then the score is multiply by the score_rate
otherwise it copies the score otherwise it copies the score
:param x: dictionnary with "is_leveled", "score" and "score_rate" keys :param x: dictionnary with "is_leveled", "score" (need to be number) and "score_rate" keys
:param score_max: :param score_max:
:param rounding: rounding mark function :param rounding: rounding mark function
:return: the mark :return: the mark
@ -125,6 +125,10 @@ def score_to_level(x, level_max=3):
return int(ceil(x["score"] / x["score_rate"] * level_max)) return int(ceil(x["score"] / x["score_rate"] * level_max))
def score_to_numeric_score(x, score_config):
pass
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -2,6 +2,7 @@
# encoding: utf-8 # encoding: utf-8
from .on_score_column import score_to_mark, score_to_level from .on_score_column import score_to_mark, score_to_level
import pandas as pd
def compute_marks(df, score_max, rounding=lambda x: round(x, 2)): def compute_marks(df, score_max, rounding=lambda x: round(x, 2)):
@ -9,7 +10,7 @@ def compute_marks(df, score_max, rounding=lambda x: round(x, 2)):
apply score_to_mark to each row apply score_to_mark to each row
:param df: DataFrame with "score", "is_leveled" and "score_rate" columns. :param df: DataFrame with "score" (need to be number), "is_leveled" and "score_rate" columns.
>>> import pandas as pd >>> import pandas as pd
>>> d = {"Eleve":["E1"]*6 + ["E2"]*6, >>> d = {"Eleve":["E1"]*6 + ["E2"]*6,
@ -113,12 +114,12 @@ def compute_normalized(df, rounding=lambda x: round(x, 2)):
>>> d = {"Eleve":["E1"]*6 + ["E2"]*6, >>> d = {"Eleve":["E1"]*6 + ["E2"]*6,
... "score_rate":[1]*2+[2]*2+[2]*2 + [1]*2+[2]*2+[2]*2, ... "score_rate":[1]*2+[2]*2+[2]*2 + [1]*2+[2]*2+[2]*2,
... "is_leveled":[0]*4+[1]*2 + [0]*4+[1]*2, ... "is_leveled":[0]*4+[1]*2 + [0]*4+[1]*2,
... "score":[1, 0.33, 2, 1.5, 1, 3, 0.666, 1, 1.5, 1, 2, 3], ... "score":[0, 0.33, 2, 1.5, 1, 3, 0.666, 1, 1.5, 1, 2, 3],
... } ... }
>>> df = pd.DataFrame(d) >>> df = pd.DataFrame(d)
>>> df["mark"] = compute_marks(df, 3) >>> df["mark"] = compute_marks(df, 3)
>>> compute_normalized(df) >>> compute_normalized(df)
0 1.00 0 0.00
1 0.33 1 0.33
2 1.00 2 1.00
3 0.75 3 0.75
@ -135,6 +136,60 @@ def compute_normalized(df, rounding=lambda x: round(x, 2)):
return rounding(df["mark"] / df["score_rate"]) return rounding(df["mark"] / df["score_rate"])
def filter_none_score(df, score_config):
"""Filter rows where scores have None numeric values
:example:
>>> import pandas as pd
>>> d = {"Eleve":["E1"]*7,
... "score_rate": [1]*7,
... "is_leveled":[0]+[1]*6,
... "score":[0.33, "", ".", "a", 1, 2, 3],
... }
>>> score_config = {
... 'BAD': {'value': 0, 'numeric_value': 0},
... 'FEW': {'value': 1, 'numeric_value': 1},
... 'NEARLY': {'value': 2, 'numeric_value': 2},
... 'GOOD': {'value': 3, 'numeric_value': 3},
... 'NOTFILLED': {'value': '', 'numeric_value': 'None'},
... 'NOANSWER': {'value': '.', 'numeric_value': 0},
... 'ABS': {'value': 'a', 'numeric_value': 'None'}
... }
>>> df = pd.DataFrame(d)
>>> filter_none_score(df, score_config)
Eleve score_rate is_leveled score
0 E1 1 0 0.33
2 E1 1 1 .
4 E1 1 1 1
5 E1 1 1 2
6 E1 1 1 3
"""
not_leveled_df = df[df["is_leveled"] != 1]
leveled_df = df[df["is_leveled"] == 1]
not_none_values = [
v["value"]
for v in score_config.values()
if str(v["numeric_value"]).lower() != "none"
]
filtered_leveled_df = leveled_df[leveled_df["score"].isin(not_none_values)]
return pd.concat([not_leveled_df, filtered_leveled_df])
def score_to_numeric_score(df, score_config):
"""Transform a score to the corresponding numeric value
>>> d = {"Eleve":["E1"]*6 + ["E2"]*6,
... "score_rate":[1]*2+[2]*2+[2]*2 + [1]*2+[2]*2+[2]*2,
... "is_leveled":[0]*4+[1]*2 + [0]*4+[1]*2,
... "score":[0, 0.33, 2, 1.5, 1, 3, 0.666, 1, 1.5, 1, 2, 3],
... }
"""
pass
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4: