Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics #944

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pyyaml = "==6.0.1"
psycopg = {extras = ["binary"], version = "==3.1.19"}
jinja2 = "==3.1.4"
django-cleanup = "==8.1.0"
seaborn = "==0.13.2"
matplotlib = "==3.9.0"
numpy = "==2.0.0"
django-pandas = "==0.6.7"

[dev-packages]
pre-commit = "==3.7.0"
Expand Down
793 changes: 582 additions & 211 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions bullet/problems/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db.models import UniqueConstraint
from web.fields import LanguageField

from django_pandas.managers import DataFrameManager


class Problem(models.Model):
competition = models.ForeignKey(
Expand Down Expand Up @@ -47,6 +49,8 @@ class ProblemStat(models.Model):
solved_time = models.DurationField(blank=True, null=True)
solve_duration = models.DurationField(blank=True, null=True)

objects = DataFrameManager()


class ScannerLog(models.Model):
class Result(models.IntegerChoices):
Expand Down
26 changes: 26 additions & 0 deletions bullet/problems/templates/archive/problem_statistics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "web/base.html" %}
{% load i18n %}
{% load country_url %}
{% load archive_statements %}
{% block title %}
{% trans "Problem statistics" %}
{% endblock title %}
{% block hero %}
<h1 class="text-4xl md:text-6xl font-bold mt-8 md:mt-24">
{% trans "Problem statistics" %}
</h1>
<h2 class="text-2xl md:text-4xl mt-4 mb-4 md:mb-16">{{ competition.name }}</h2>
{% endblock hero %}
{% block content %}
<main class="my-8 max-w-screen-md mx-auto px-2">
<h2 class="font-bold text-lg">
{% blocktrans with n=object.name %}Problem {{ n }}{% endblocktrans %}
</h2>
<div class="prose prose-archive">{{ problem.statement|safe }}</div>
{% for plot in plots %}
<img src="data:image/svg+xml;base64,{{ plot }}"
alt="Plot of the problem statistics"
class="max-w-screen-md" />
{% endfor %}
</main>
{% endblock content %}
22 changes: 22 additions & 0 deletions bullet/problems/templates/archive/school_statistics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "web/base.html" %}
{% load i18n %}
{% load country_url %}
{% load archive_statements %}
{% block title %}
{% trans "Problem statistics" %}
{% endblock title %}
{% block hero %}
<h1 class="text-4xl md:text-6xl font-bold mt-8 md:mt-24">
{% trans "School statistics" %}
</h1>
<h2 class="text-2xl md:text-4xl mt-4 mb-4 md:mb-16">{{ competition.name }}</h2>
{% endblock hero %}
{% block content %}
<main class="my-8 max-w-screen-md mx-auto px-2">
<h2 class="font-bold text-lg">{{ School.name }}</h2>
<div class="prose prose-archive">{{ problem.statement|safe }}</div>
<img src="data:image/svg+xml;base64,{{ plot }}"
alt="Plot of the problem statistics"
class="max-w-screen-md" />
</main>
{% endblock content %}
12 changes: 11 additions & 1 deletion bullet/problems/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from problems.views import archive, results
from problems.views import archive, results, statistics

urlpatterns = [
path(
Expand Down Expand Up @@ -49,4 +49,14 @@
archive.ProblemStatementView.as_view(),
name="archive_problems",
),
path(
"archive/<int:competition_number>/problem/<int:pk>/",
statistics.ProblemStatisticsView.as_view(),
name="problem_statistics",
),
path(
"archive/<int:competition_number>/school/<int:pk>/",
statistics.SchoolStatisticsView.as_view(),
name="school_statistics",
),
]
140 changes: 140 additions & 0 deletions bullet/problems/views/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from django.views.generic.detail import DetailView
from problems.models import Problem, ProblemStat, ProblemStatement
from education.models import School
from users.models import Team
from django.utils.translation import get_language
import matplotlib.pyplot as plt
from io import StringIO

import seaborn as sns
import seaborn.objects as so
import numpy as np
import pandas as pd
from matplotlib.dates import DateFormatter
import base64


class ProblemStatisticsView(DetailView):
model = Problem
template_name = "archive/problem_statistics.html"

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["problem"] = ProblemStatement.objects.filter(
problem=self.object, language=get_language()
).first()
ctx["stats"] = ProblemStat.objects.filter(problem=self.object)

r = (
ProblemStat.objects.filter(problem=self.object)
.exclude(solve_duration__isnull=True)
.exclude(team__language="fa")
.to_dataframe(
[
"solve_duration",
"team__language",
"team__venue__category__identifier",
]
)
.astype({"solve_duration": "timedelta64[ns]"})
)
epoch = pd.Timestamp("1970-01-01")
r["solve_duration"] = r["solve_duration"] + epoch
plots = []
for category in r["team__venue__category__identifier"].unique():
s = StringIO()
plt.figure()
sns.set(font_scale=1.5)
sns.set_style("whitegrid")
g = sns.displot(
r.query("team__venue__category__identifier == @category"),
x="solve_duration",
hue="team__language",
kind="kde",
)
plt.gcf().set_size_inches(12, 6)

g.set(
title=f"Problem solving time Distribution for {self.object}",
xlabel="Solving time",
ylabel="Number of teams",
)

g._legend.set(title="Language")
date_form = DateFormatter("%H:%M:%S")
plt.gca().xaxis.set_major_formatter(date_form)
plt.xlim(0, None)
locs, labels = plt.xticks()
plt.setp(labels, rotation=45, ha="right", rotation_mode="anchor")
plt.savefig(s, format="svg")
plt.close()
plots.append(base64.b64encode(bytes(s.getvalue(), "utf-8")).decode("utf-8"))

ctx["plots"] = plots
return ctx


class SchoolStatisticsView(DetailView):
model = School
template_name = "archive/school_statistics.html"

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["school"] = self.object

s = StringIO()
teams = Team.objects.filter(school=self.object)

r = (
ProblemStat.objects.filter(team__in=teams)
.exclude(solved_time__isnull=True)
.exclude(team__language="fa")
.to_dataframe(
[
"solved_time",
"team__in_school_symbol",
"team__venue__category__identifier",
"team__venue__category__competition",
]
)
.astype({"solved_time": "timedelta64[ns]"})
)
print(r)
epoch = pd.Timestamp("1970-01-01")
r["solved_time"] = r["solved_time"] + epoch
# r["team"] = r["team"].apply(str)
sns.set_style("whitegrid")
g = sns.displot(
r,
x="solved_time",
stat="count",
hue=r[
[
"team__in_school_symbol",
"team__venue__category__competition",
"team__venue__category__identifier",
]
]
.apply(tuple, axis=1)
.apply(lambda x: f"{x[1]} - {x[0]} {x[2]}" if x[0] else f"{x[1]} - {x[2]}"),
kind="ecdf",
)
plt.gcf().set_size_inches(12, 7)

g.set(
title=f"Problem solving time Distribution for {self.object}",
xlabel="Solving time",
ylabel="Number of teams",
)

g.legend.set(
title="Teams",
)
date_form = DateFormatter("%H:%M:%S")
plt.gca().xaxis.set_major_formatter(date_form)
plt.xlim(0, None)
locs, labels = plt.xticks()
plt.setp(labels, rotation=45, ha="right", rotation_mode="anchor")
plt.savefig(s, format="svg")
ctx["plot"] = base64.b64encode(bytes(s.getvalue(), "utf-8")).decode("utf-8")
return ctx
Loading