From 2acdcc64b06d860bc609edca07813870956f505e Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sat, 1 Apr 2017 15:32:19 +0300 Subject: [PATCH] Remove try:... for plots to free memory --- notes_tools/reports/filters.py | 2 + .../reports/templates/tpl_reports_term.tex | 7 +- notes_tools/tools/evaluation.py | 123 +++++++----------- notes_tools/tools/term.py | 55 ++++---- 4 files changed, 78 insertions(+), 109 deletions(-) diff --git a/notes_tools/reports/filters.py b/notes_tools/reports/filters.py index d5ee9c5..81087ff 100644 --- a/notes_tools/reports/filters.py +++ b/notes_tools/reports/filters.py @@ -4,6 +4,7 @@ from uuid import uuid4 from path import Path import logging +import matplotlib.pyplot as plt logger = logging.getLogger(__name__) def includegraphics(fig_ax, document_path="./", fig_path="fig/", @@ -34,6 +35,7 @@ def includegraphics(fig_ax, document_path="./", fig_path="fig/", fig.tight_layout() fig.savefig(path_to_file/filename) logger.info("Graphique {} sauvé à {}".format(filename, path_to_file)) + plt.close(fig) return "\includegraphics[scale={sc}]{{{f}}}".format(sc = scale, f=Path(fig_path)/filename) diff --git a/notes_tools/reports/templates/tpl_reports_term.tex b/notes_tools/reports/templates/tpl_reports_term.tex index ad52383..de90f9f 100644 --- a/notes_tools/reports/templates/tpl_reports_term.tex +++ b/notes_tools/reports/templates/tpl_reports_term.tex @@ -50,9 +50,12 @@ Compétences \vfill -\Var{e.pies_on_competence | includegraphics(document_path=directory, scale=0.3)} -\Var{e.pies_on_domaine | includegraphics(document_path=directory, scale=0.3)} +\Var{e.heatmap_on_competence | includegraphics(document_path=directory, scale=0.6)} + +\Var{e.heatmap_on_domain | includegraphics(document_path=directory, scale=0.6)} +%# \Var{e.pies_on_competence | includegraphics(document_path=directory, scale=0.3)} +%# \Var{e.pies_on_domaine | includegraphics(document_path=directory, scale=0.3)} %#\Var{conn_df} diff --git a/notes_tools/tools/evaluation.py b/notes_tools/tools/evaluation.py index 568ff0a..76640a4 100644 --- a/notes_tools/tools/evaluation.py +++ b/notes_tools/tools/evaluation.py @@ -102,68 +102,48 @@ class Student(object): @property def pies_on_competence(self): """ Pies chart on competences """ - try: - self._pies_on_competence - except AttributeError: - self._pies_on_competence = pie_pivot_table(self.quest_df, - index = "Level", - columns = "Competence", - values = "Eleve", - aggfunc = len, - fill_value = 0, - ) - return self._pies_on_competence + return pie_pivot_table(self.quest_df, + index = "Level", + columns = "Competence", + values = "Eleve", + aggfunc = len, + fill_value = 0, + ) @property def pies_on_domaine(self): """ Pies chart on domaines """ - try: - self._pies_on_domaine - except AttributeError: - self._pies_on_domaine = pie_pivot_table(self.quest_df, - index = "Level", - columns = "Domaine", - values = "Eleve", - aggfunc = len, - fill_value = 0, - ) - return self._pies_on_domaine + return pie_pivot_table(self.quest_df, + index = "Level", + columns = "Domaine", + values = "Eleve", + aggfunc = len, + fill_value = 0, + ) @property def radar_on_competence(self): """ Radar plot on competence """ - try: - self._radar_on_competence - except AttributeError: - self._radar_on_competence = radar_on(self.quest_df, + return radar_on(self.quest_df, "Competence") - return self._radar_on_competence @property def radar_on_domaine(self): """ Radar plot on domaine """ - try: - self._radar_on_domaine - except AttributeError: - self._radar_on_domaine = radar_on(self.quest_df, + return radar_on(self.quest_df, "Domaine") - return self._radar_on_domaine @property def heatmap_on_domain(self): """ Heatmap over evals on domains """ - try: - self._heatmap_on_domain - except AttributeError: - comp = pd.pivot_table(self.quest_df, - index = "Competence", - columns = ["Exercice", "Question"], - values = ["Normalized"], - aggfunc = np.mean, - ) - comp.columns = [f"{i['Exercice']} {i['Question']}" for _,i in self.quest_df[["Exercice", "Question"]].drop_duplicates().iterrows()] - self._heatmap_on_domain = sns.heatmap(comp) - return self._heatmap_on_domain + comp = pd.pivot_table(self.quest_df, + index = "Competence", + columns = ["Exercice", "Question"], + values = ["Normalized"], + aggfunc = np.mean, + ) + comp.columns = [f"{i['Exercice']} {i['Question']}" for _,i in self.quest_df[["Exercice", "Question"]].drop_duplicates().iterrows()] + return sns.heatmap(comp) class Classe(object): @@ -201,45 +181,38 @@ class Classe(object): @property def hist_boxplot(self): """ Marks histogram and associed box plot """ - try: - self._hist_boxplot - except AttributeError: - self._hist_boxplot = hist_boxplot(self.eval_df) - return self._hist_boxplot + return hist_boxplot(self.eval_df) @property def level_heatmap(self): """ Heapmap on acheivement level """ - try: - self._level_heatmap - except AttributeError: - pv = pd.pivot_table(self.quest_df, - index = "Eleve", - columns = ["Exercice", "Question", "Commentaire"], - values = ["Normalized"], - aggfunc = "mean", - ) + pv = pd.pivot_table(self.quest_df, + index = "Eleve", + columns = ["Exercice", "Question", "Commentaire"], + values = ["Normalized"], + aggfunc = "mean", + ) - def lines_4_heatmap(c): - lines = [] - ini = '' - for k,v in enumerate(c.labels[1][::-1]): - if v != ini: - lines.append(k) - ini = v - return lines[1:] - exercice_sep = lines_4_heatmap(pv.columns) + def lines_4_heatmap(c): + lines = [] + ini = '' + for k,v in enumerate(c.labels[1][::-1]): + if v != ini: + lines.append(k) + ini = v + return lines[1:] + exercice_sep = lines_4_heatmap(pv.columns) - pv.columns = [f"{i[3]:.15} {i[1]} {i[2]}" for i in pv.columns.get_values()] - - self._level_heatmap = sns.heatmap(pv.T) + pv.columns = [f"{i[3]:.15} {i[1]} {i[2]}" for i in pv.columns.get_values()] + + level_heatmap = sns.heatmap(pv.T) - self._level_heatmap.hlines(exercice_sep, - *self._level_heatmap.get_xlim(), - colors = "orange", - ) + level_heatmap.hlines(exercice_sep, + *self._level_heatmap.get_xlim(), + colors = "orange", + ) + return level_heatmap - return self._level_heatmap @property def pies_eff_pts_on_competence(self): diff --git a/notes_tools/tools/term.py b/notes_tools/tools/term.py index 378a017..cafa6ae 100644 --- a/notes_tools/tools/term.py +++ b/notes_tools/tools/term.py @@ -53,69 +53,60 @@ class Student(object): @property def pies_on_competence(self): """ Pies chart on competences """ - try: - self._pies_on_competence - except AttributeError: - self._pies_on_competence = pie_pivot_table(self.quest_df, + return pie_pivot_table(self.quest_df, index = "Level", columns = "Competence", values = "Eleve", aggfunc = len, fill_value = 0, ) - return self._pies_on_competence @property def pies_on_domaine(self): """ Pies chart on domaines """ - try: - self._pies_on_domaine - except AttributeError: - self._pies_on_domaine = pie_pivot_table(self.quest_df, + return pie_pivot_table(self.quest_df, index = "Level", columns = "Domaine", values = "Eleve", aggfunc = len, fill_value = 0, ) - return self._pies_on_domaine @property def radar_on_competence(self): """ Radar plot on competence """ - try: - self._radar_on_competence - except AttributeError: - self._radar_on_competence = radar_on(self.quest_df, + return radar_on(self.quest_df, "Competence") - return self._radar_on_competence @property def radar_on_domaine(self): """ Radar plot on domaine """ - try: - self._radar_on_domaine - except AttributeError: - self._radar_on_domaine = radar_on(self.quest_df, + return radar_on(self.quest_df, "Domaine") - return self._radar_on_domaine @property def heatmap_on_domain(self): """ Heatmap over evals on domains """ - try: - self._heatmap_on_domain - except AttributeError: - comp = pd.pivot_table(self.quest_df, - index = "Competence", - columns = ["Date","Nom"], - values = ["Normalized"], - aggfunc = np.mean, - ) - comp.columns = [i[1].strftime("%Y-%m-%d") + "\n" + i[2] for i in comp.columns] - self._heatmap_on_domain = sns.heatmap(comp) - return self._heatmap_on_domain + comp = pd.pivot_table(self.quest_df, + index = "Domaine", + columns = ["Date","Nom"], + values = ["Normalized"], + aggfunc = np.mean, + ) + comp.columns = [i[1].strftime("%Y-%m-%d") + "\n" + i[2] for i in comp.columns] + return sns.heatmap(comp) + @property + def heatmap_on_competence(self): + """ Heatmap over evals on competences """ + comp = pd.pivot_table(self.quest_df, + index = "Competence", + columns = ["Date","Nom"], + values = ["Normalized"], + aggfunc = np.mean, + ) + comp.columns = [i[1].strftime("%Y-%m-%d") + "\n" + i[2] for i in comp.columns] + return sns.heatmap(comp) def parallel_on_evals(self, classe_evals): """ Parallel coordinate plot of the class with student line highlight """