<commit_before>from datetime import datetime
from lxml import html

from .base import BaseHandler
from rubberband.constants import IPET_EVALUATIONS, FORMAT_DATETIME_SHORT, NONE_DISPLAY
from rubberband.models import TestSet

from ipet import Experiment, TestRun
from ipet.evaluation import IPETEvaluation
import pandas as pd

import json
import string


class EvaluationView(BaseHandler):

    def get(self, eval_id):
        # get evalfile
        evalfile = IPET_EVALUATIONS[int(eval_id)]

        # read testrunids
        testrunids = self.get_argument("testruns").split(",")

        # read defaultgroup
        default = self.get_argument("default", testrunids[0])
        default_rbid = None

        # default and implicit style is ipetevaluation. if given latex, generate a table in the
        # style of the release report
        style = self.get_argument("style", None)

        # get testruns
        testruns = []
        for i in testrunids:
            t = TestSet.get(id=i)
            if t.meta.id == default:
                default_rbid = t.id
            testruns.append(t)

        try:
            testruns.sort(key=lambda x: x.git_commit_timestamp)
        except:
            pass

        # evaluate with ipet
        ex, results, repres = setup_experiment(testruns)
        ev = IPETEvaluation.fromXMLFile(evalfile["path"])
        ev.set_defaultgroup(default_rbid)
        longtable, aggtable = ev.evaluate(ex)

        # None style is default
        if style is None:
            # postprocessing
            longtable.insert(0, "id", range(1, len(longtable) + 1))
            html_long, style_long = table_to_html(longtable, ev, add_class="ipet-long-table")
            html_agg, style_agg = table_to_html(aggtable, ev, add_class="ipet-aggregated-table")

            html_long = process_ipet_table(html_long, repres) + \
                html.tostring(style_long).decode("utf-8")
            html_agg = process_ipet_table(html_agg, repres) + \
                html.tostring(style_agg).decode("utf-8")

            # render to strings
            html_tables = self.render_string("results/evaluation.html",
                    ipet_long_table=html_long,
                    ipet_aggregated_table=html_agg).decode("utf-8")

            results_table = self.render_string("results_table.html",
                    results=results, representation=repres, radios=True,
                    checked=default,
                    tablename="ipet-legend-table").decode("utf-8")

            # send evaluated data
            mydict = {"ipet-legend-table": results_table,
                    "ipet-eval-result": html_tables}
            self.write(json.dumps(mydict))
        elif style == "latex":
            # generate a table that can be used in the release-report
            df = aggtable
            # care for the columns
            df = df.reset_index()
            poss = ['RubberbandId', 'GitHash', 'Settings', 'LPSolver']
            for i in poss:
                if i in df.columns:
                    colindex = i

            cols = [c for c in df.columns if (c in ['Group', colindex, '_time_'] or
                c.startswith("N_") or c.startswith("T_")) and not c.endswith(")p")]
            # collect keys for later replacement
            repl = {}
            repl["_time_"] = "timeout"
            for i in cols:
                if i.startswith("N_"):
                    repl[i] = "nodes"
                if i.startswith("T_"):
                    repl[i] = "time"

            cols1 = [c for c in cols if c.endswith("Q") or c in ['Group', colindex]]
            cols2 = [c for c in cols if not c.endswith("Q") or c in ['Group', colindex]]
            df_rel = df[cols1]
            df_abs = df[cols2]
            df_count = df["_count_"]

            # groups in rows
            rows = ['clean', '[0,tilim]', '[1,tilim]', '[10,tilim]', '[100,tilim]', '[1000,tilim]',
                    'diff-timeouts', 'MIPLIB2010 (87)', 'Cor@l (349)', 'continuous', 'integer']

            df_rel = df_rel.pivot_table(index=['Group'], columns=[colindex]).swaplevel(
                    axis=1).sort_index(axis=1, level=0, sort_remaining=True, ascending=False)
            df_abs = df_abs.pivot_table(index=['Group'], columns=[colindex]).swaplevel(
                    axis=1).sort_index(axis=1, level=0, sort_remaining=True, ascending=False)
            df_count = df.pivot_table(values=['_count_'], index=['Group'],
                    columns=[colindex]).swaplevel(axis=1)

            df = df_abs
            df.insert(loc=0, column=(None, "instances"), value=df_count[df_count.columns[0]])<commit_msg>fix replacement in latex table generation<commit_after>from datetime import datetime
from lxml import html

from .base import BaseHandler
from rubberband.constants import IPET_EVALUATIONS, FORMAT_DATETIME_SHORT, NONE_DISPLAY
from rubberband.models import TestSet

from ipet import Experiment, TestRun
from ipet.evaluation import IPETEvaluation
import pandas as pd

import json
import string


class EvaluationView(BaseHandler):

    def get(self, eval_id):
        # get evalfile
        evalfile = IPET_EVALUATIONS[int(eval_id)]

        # read testrunids
        testrunids = self.get_argument("testruns").split(",")

        # read defaultgroup
        default = self.get_argument("default", testrunids[0])
        default_rbid = None

        # default and implicit style is ipetevaluation. if given latex, generate a table in the
        # style of the release report
        style = self.get_argument("style", None)

        # get testruns
        testruns = []
        for i in testrunids:
            t = TestSet.get(id=i)
            if t.meta.id == default:
                default_rbid = t.id
            testruns.append(t)

        try:
            testruns.sort(key=lambda x: x.git_commit_timestamp)
        except:
            pass

        # evaluate with ipet
        ex, results, repres = setup_experiment(testruns)
        ev = IPETEvaluation.fromXMLFile(evalfile["path"])
        ev.set_defaultgroup(default_rbid)
        longtable, aggtable = ev.evaluate(ex)

        # None style is default
        if style is None:
            # postprocessing
            longtable.insert(0, "id", range(1, len(longtable) + 1))
            html_long, style_long = table_to_html(longtable, ev, add_class="ipet-long-table")
            html_agg, style_agg = table_to_html(aggtable, ev, add_class="ipet-aggregated-table")

            html_long = process_ipet_table(html_long, repres) + \
                html.tostring(style_long).decode("utf-8")
            html_agg = process_ipet_table(html_agg, repres) + \
                html.tostring(style_agg).decode("utf-8")

            # render to strings
            html_tables = self.render_string("results/evaluation.html",
                    ipet_long_table=html_long,
                    ipet_aggregated_table=html_agg).decode("utf-8")

            results_table = self.render_string("results_table.html",
                    results=results, representation=repres, radios=True,
                    checked=default,
                    tablename="ipet-legend-table").decode("utf-8")

            # send evaluated data
            mydict = {"ipet-legend-table": results_table,
                    "ipet-eval-result": html_tables}
            self.write(json.dumps(mydict))
        elif style == "latex":
            # generate a table that can be used in the release-report
            df = aggtable
            # care for the columns
            df = df.reset_index()
            poss = ['RubberbandId', 'GitHash', 'Settings', 'LPSolver']
            for i in poss:
                if i in df.columns:
                    colindex = i

            cols = [c for c in df.columns if (c in ['Group', colindex, '_time_'] or
                c.startswith("N_") or c.startswith("T_")) and not c.endswith(")p")]
            # collect keys for later replacement
            repl = {}
            repl["_time_"] = "timeout"
            for i in cols:
                if i.startswith("N_"):
                    repl[" " + i + " "] = " nodes "
                if i.startswith("T_"):
                    repl[" " + i + " "] = " time "

            cols1 = [c for c in cols if c.endswith("Q") or c in ['Group', colindex]]
            cols2 = [c for c in cols if not c.endswith("Q") or c in ['Group', colindex]]
            df_rel = df[cols1]
            df_abs = df[cols2]
            df_count = df["_count_"]

            # groups in rows
            rows = ['clean', '[0,tilim]', '[1,tilim]', '[10,tilim]', '[100,tilim]', '[1000,tilim]',
                    'diff-timeouts', 'MIPLIB2010 (87)', 'Cor@l (349)', 'continuous', 'integer']

            df_rel = df_rel.pivot_table(index=['Group'], columns=[colindex]).swaplevel(
                    axis=1).sort_index(axis=1, level=0, sort_remaining=True, ascending=False)
            df_abs = df_abs.pivot_table(index=['Group'], columns=[colindex]).swaplevel(
                    axis=1).sort_index(axis=1, level=0, sort_remaining=True, ascending=False)
            df_count = df.pivot_table(values=['_count_'], index=['Group'],
                    columns=[colindex]).swaplevel(axis=1)

            df = df_abs
            df.insert(loc=0, column=(None, "instances"), value=df_count[df_count.columns[0]])
{'fraction_non_alphanumeric': 0.11444492070295756, 'fraction_numerical': 0.008786969567081011, 'mean_word_length': 2.0640183847669076, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6596242', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2747, 'n_words': 644}