<commit_before># -*- coding: utf-8 -*-
"""
    edacc.views.analysis
    --------------------

    Defines request handler functions for the analysis pages.

    :copyright: (c) 2010 by <NAME>.
    :license: MIT, see LICENSE for details.
"""

import math
import numpy
import StringIO
import csv

from sqlalchemy.orm import joinedload

from flask import Module
from flask import render_template as render
from flask import abort, request, jsonify, Response
from werkzeug import Headers, secure_filename

from edacc import models, forms, ranking, statistics
from edacc.views.helpers import require_phase, require_login
from edacc.constants import RANKING, ANALYSIS1, ANALYSIS2
from edacc.views import plot
from edacc.forms import EmptyQuery

analysis = Module(__name__)

#def render(*args, **kwargs):
#    from tidylib import tidy_document
#    res = render_template(*args, **kwargs)
#    doc, errs = tidy_document(res)
#    return doc


@analysis.route('/<database>/experiment/<int:experiment_id>/ranking/')
@require_phase(phases=RANKING)
@require_login
def solver_ranking(database, experiment_id):
    """ Display a page with the ranking of the solvers of
        the experiment.
    """
    db = models.get_database(database) or abort(404)
    experiment = db.session.query(db.Experiment).get(experiment_id) or abort(404)

    form = forms.RankingForm(request.args)
    form.i.query = sorted(experiment.get_instances(db), key=lambda i: i.name) or EmptyQuery()

    if form.i.data:
        #ranked_solvers = ranking.avg_point_biserial_correlation_ranking(db, experiment, form.i.data)
        ranked_solvers = ranking.number_of_solved_instances_ranking(db, experiment, form.i.data)
        ranking_data = ranking.get_ranking_data(db, experiment, ranked_solvers, form.i.data,
                                                form.penalized_average_runtime.data, form.calculate_average_dev.data)
            
        if 'csv' in request.args:
            head = ['#', 'Solver', '# of successful runs', '% of all runs', '% of VBS runs',
                                 'cumulated CPU time', 'avg. CPU time per successful run']
    
            if form.calculate_average_dev.data: head.append('avg. deviation of successful runs')
            if form.penalized_average_runtime.data: head.append('penalized avg. runtime')
            csv_response = StringIO.StringIO()
            csv_writer = csv.writer(csv_response)
            csv_writer.writerow(head)

            for rnk, row in enumerate(ranking_data):
                write_row = [rnk, row[0], row[1], round(row[2] * 100, 2), round(row[3] * 100, 2)] + map(lambda x: round(x, 2), row[4:6])
                if form.calculate_average_dev.data: write_row.append(round(row[6], 2))
                if form.penalized_average_runtime.data: write_row.append(round(row[7], 2))
                csv_writer.writerow(write_row)

            csv_response.seek(0)
            headers = Headers()
            headers.add('Content-Type', 'text/csv')
            headers.add('Content-Disposition', 'attachment',
                        filename=secure_filename(experiment.name + "_ranking.csv"))
            return Response(response=csv_response.read(), headers=headers)
        elif 'latex' in request.args:
            head = ['\\#', 'Solver', '\\# of successful runs', '\\% of all runs', '\\% of VBS runs',
                                 'cumulated CPU time', 'avg. CPU time per successful run']
    
            if form.calculate_average_dev.data: head.append('avg. deviation of successful runs')
            if form.penalized_average_runtime.data: head.append('penalized avg. runtime')
            table = "\\begin{tabular}{" + ('|'.join(['c'] * len(head))) + "}\n"
            table += ' & '.join(head) + "\\\\ \\hline\n"
            for rnk, row in enumerate(ranking_data):
                table += ' & '.join(map(str, [rnk, row[0], row[1], round(row[2] * 100, 2), round(row[3] * 100, 2)] + map(lambda x: round(x, 2), row[4:6])))
                if form.calculate_average_dev.data: table += " & " + str(round(row[6], 2))
                if form.penalized_average_runtime.data: table += " & " + str(round(row[7], 2))
                table += "\\\\ \\hline\n"
            table += "\\end{tabular}"
            
            headers = Headers()
            headers.add('Content-Type', 'text/plain')
            return Response(response=table, headers=headers)
            

        return render('/analysis/ranking.html', database=database, db=db,
                      experiment=experiment, ranked_solvers=ranked_solvers,
                      data=ranking_data, form=form, instance_properties=db.get_instance_properties())

    return render('/analysis/ranking.html', database=database, db=db,
              experiment=experiment, form=form, instance_properties=db.get_instance_properties())


@analysis.route('/<database>/experiment/<int:experiment_id>/cactus/')
@require_phase(phases=ANALYSIS1)
@require_login
def cactus_plot(database, experiment_id):
    """ Displays a page where the user can select a set of instances and a
        result property and obtains a cactus plot of the number of instances
        solved within a given amount of the result property.

        For example: Number of instances the program can solve when given 100 seconds
        of CPU time.
    """
    db = models.get_database(database) or abort(404)
    experiment = db.session.query(db.Experiment).get(experiment_id) or abort(404)

    form = forms.CactusPlotForm(request.args)
    form.i.query = sorted(experiment.get_instances(db), key=lambda i: i.name) or EmptyQuery()
    result_properties = db.get_plotable_result_properties()
    result_properties = zip([p.idProperty for p in result_properties], [p.name for p in result_properties])<commit_msg>cache ranking page<commit_after># -*- coding: utf-8 -*-
"""
    edacc.views.analysis
    --------------------

    Defines request handler functions for the analysis pages.

    :copyright: (c) 2010 by <NAME>.
    :license: MIT, see LICENSE for details.
"""

import math
import numpy
import StringIO
import csv

from sqlalchemy.orm import joinedload
from sqlalchemy import or_

from flask import Module
from flask import render_template as render
from flask import abort, request, jsonify, Response
from werkzeug import Headers, secure_filename

from edacc import models, forms, ranking, statistics
from edacc.web import cache
from edacc.views.helpers import require_phase, require_login
from edacc.constants import RANKING, ANALYSIS1, ANALYSIS2
from edacc.views import plot
from edacc.forms import EmptyQuery

analysis = Module(__name__)

#def render(*args, **kwargs):
#    from tidylib import tidy_document
#    res = render_template(*args, **kwargs)
#    doc, errs = tidy_document(res)
#    return doc


@analysis.route('/<database>/experiment/<int:experiment_id>/ranking/')
@require_phase(phases=RANKING)
@require_login
def solver_ranking(database, experiment_id):
    """ Display a page with the ranking of the solvers of
        the experiment.
    """
    db = models.get_database(database) or abort(404)
    experiment = db.session.query(db.Experiment).get(experiment_id) or abort(404)

    form = forms.RankingForm(request.args)
    form.i.query = sorted(experiment.get_instances(db), key=lambda i: i.name) or EmptyQuery()

    if form.i.data:
        CACHE_TIME = 14*24*60*60
        @cache.memoize(timeout=CACHE_TIME)
        def cached_ranking(database, experiment_id, num_finished_jobs, form_i_data, form_par, form_avg_dev, csv=False, latex=False):
            #ranked_solvers = ranking.avg_point_biserial_correlation_ranking(db, experiment, form.i.data)
            ranked_solvers = ranking.number_of_solved_instances_ranking(db, experiment, form.i.data)
            ranking_data = ranking.get_ranking_data(db, experiment, ranked_solvers, form.i.data,
                                                    form.penalized_average_runtime.data, form.calculate_average_dev.data)

            if csv:
                head = ['#', 'Solver', '# of successful runs', '% of all runs', '% of VBS runs',
                                     'cumulated CPU time', 'avg. CPU time per successful run']

                if form.calculate_average_dev.data: head.append('avg. deviation of successful runs')
                if form.penalized_average_runtime.data: head.append('penalized avg. runtime')
                csv_response = StringIO.StringIO()
                csv_writer = csv.writer(csv_response)
                csv_writer.writerow(head)

                for rnk, row in enumerate(ranking_data):
                    write_row = [rnk, row[0], row[1], round(row[2] * 100, 2), round(row[3] * 100, 2)] + map(lambda x: round(x, 2), row[4:6])
                    if form.calculate_average_dev.data: write_row.append(round(row[6], 2))
                    if form.penalized_average_runtime.data: write_row.append(round(row[7], 2))
                    csv_writer.writerow(write_row)

                csv_response.seek(0)
                headers = Headers()
                headers.add('Content-Type', 'text/csv')
                headers.add('Content-Disposition', 'attachment',
                            filename=secure_filename(experiment.name + "_ranking.csv"))
                return Response(response=csv_response.read(), headers=headers)
            elif latex:
                head = ['\\#', 'Solver', '\\# of successful runs', '\\% of all runs', '\\% of VBS runs',
                                     'cumulated CPU time', 'avg. CPU time per successful run']

                if form.calculate_average_dev.data: head.append('avg. deviation of successful runs')
                if form.penalized_average_runtime.data: head.append('penalized avg. runtime')
                table = "\\begin{tabular}{" + ('|'.join(['c'] * len(head))) + "}\n"
                table += ' & '.join(head) + "\\\\ \\hline\n"
                for rnk, row in enumerate(ranking_data):
                    table += ' & '.join(map(str, [rnk, row[0], row[1], round(row[2] * 100, 2), round(row[3] * 100, 2)] + map(lambda x: round(x, 2), row[4:6])))
                    if form.calculate_average_dev.data: table += " & " + str(round(row[6], 2))
                    if form.penalized_average_runtime.data: table += " & " + str(round(row[7], 2))
                    table += "\\\\ \\hline\n"
                table += "\\end{tabular}"

                headers = Headers()
                headers.add('Content-Type', 'text/plain')
                return Response(response=table, headers=headers)


            return render('/analysis/ranking.html', database=database, db=db,
                          experiment=experiment, ranked_solvers=ranked_solvers,
                          data=ranking_data, form=form, instance_properties=db.get_instance_properties())

        num_jobs_finished = db.session.query(db.ExperimentResult) \
            .filter_by(experiment=experiment).filter(or_(db.ExperimentResult.status>=1, db.ExperimentResult.status<-1)).count()
        return cached_ranking(database, experiment_id, num_jobs_finished, [i.idInstance for i in form.i.data],
                              form.penalized_average_runtime.data, form.calculate_average_dev.data,
                              'csv' in request.args, 'latex' in request.args)

    return render('/analysis/ranking.html', database=database, db=db,
              experiment=experiment, form=form, instance_properties=db.get_instance_properties())


@analysis.route('/<database>/experiment/<int:experiment_id>/cactus/')
@require_phase(phases=ANALYSIS1)
@require_login
def cactus_plot(database, experiment_id):
    """ Displays a page where the user can select a set of instances and a
        result property and obtains a cactus plot of the number of instances
        solved within a given amount of the result property.

        For example: Number of instances the program can solve when given 100 seconds
        of CPU time.
    """
    db = models.get_database(database) or abort(404)
    experiment = db.session.query(db.Experiment).get(experiment_id) or abort(404)

    form = forms.CactusPlotForm(request.args)
    form.i.query = sorted(experiment.get_instances(db), key=lambda i: i.name) or EmptyQuery()
    result_properties = db.get_plotable_result_properties()
    result_properties = zip([p.idProperty for p in result_properties], [p.name for p in result_properties])
{'fraction_non_alphanumeric': 0.12049358819259617, 'fraction_numerical': 0.010807323171223486, 'mean_word_length': 2.7248422949834787, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4002401', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3564, 'n_words': 884}