Commit e619d18f authored by Dominique Merle's avatar Dominique Merle
Browse files

Implement plotting deliberation as merit profiles.

parent 3ae9dec0
......@@ -36,6 +36,9 @@ import argparse
import csv
import math
import copy
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint
from io import StringIO
from functools import cmp_to_key
......@@ -169,7 +172,7 @@ def deliberate(judgments_data,
candidates_list = list()
everyones_judgments = dict()
skip_rows = 0
header_on_row = skip_rows + 0 # Set to -1 to disable header
header_on_row = skip_rows + 0 # toggle 0 to -1 to disable header
if is_string(judgments_data):
judgments_data = load_judgments_from_string(judgments_data)
......@@ -284,6 +287,127 @@ def sort_candidates(judgments_tallies, candidates, mentions):
)
def plot_merit_profile(judgments_tallies, candidates, mentions, filename=None):
"""
:param filename: String, should match `*.png` or `*.pdf`. Paths are allowed.
"""
pprint(judgments_tallies)
pprint("candidates")
pprint(candidates)
pprint("mentions")
pprint(mentions)
bar_girth = 0.62
candidates = [c for c in reversed(candidates)] # :(|) oOoK
# opinions = [ # for each mention, how many for each candidate
# (20, 35, 30, 35, 27, 30, 35, 27), # excellent
# (25, 32, 34, 20, 25, 30, 35, 27), # etc.
# (25, 32, 34, 20, 25, 30, 35, 27),
# (25, 32, 34, 20, 25, 30, 35, 27),
# (25, 32, 34, 20, 25, 30, 35, 27),
# (20, 35, 30, 35, 27, 30, 35, 27),
# (20, 35, 30, 35, 27, 30, 35, 27),
# ]
opinions = [[judgments_tallies[c][m] for c in candidates] for m in mentions]
# candidates = ('G1', 'G2', 'G3', 'G4', 'G5')
# mentions = (
# u"excellent⋅e",
# u"très bien",
# u"bien",
# u"assez bien",
# u"passable",
# u"insuffisant⋅e",
# u"à rejeter",
# )
colors = (
(0, 0.49, 0.24, 1),
(0.01, 0.67, 0.35, 1),
(0.49, 0.76, 0.22, 1),
(0.78, 0.84, 0, 1),
(0.99, 0.7, 0, 1),
(0.93, 0.43, 0, 1),
(0.88, 0.21, 0.11, 1),
)
# candidates = (
# "Tyran Sanguinaire",
# "Chien",
# "Écologiste Décroissant",
# "Capitaliste Prédateur",
# "Imposteur Crapuleux",
# "Nationaliste Identitaire",
# "Camarade Communiste",
# "Marianne",
# )
candidates_amount = len(candidates)
# vlorp = 0.05
barhs = []
legends = []
ind = [i for i in range(candidates_amount)]
leftOffset = (0,) * candidates_amount
judgments_sum = (0,) * candidates_amount
for opinion, mention in zip(opinions, mentions):
judgments_sum = tuple(p + q for p, q in zip(opinion, judgments_sum))
judgments_count = max(judgments_sum)
vlorp = judgments_count * 0.003
for i, mention in enumerate(mentions):
#print(u"Plotting bars of mention %s…" % mention)
opinion = opinions[i]
barh = plt.barh(ind, opinion, bar_girth, left=leftOffset, color=colors[i])
barhs.append(barh)
legends.append(barh[0])
# At some point, we should either go full numpy or not at all
# The vlorp is a nasty hack to show white separators between colors
leftOffset = tuple(p + q + vlorp for p, q in zip(opinion, leftOffset))
# p1 = plt.barh(ind, menMeans, bar_girth)
# p2 = plt.barh(ind, womenMeans, bar_girth, left=menMeans)
adjusted_width = max(leftOffset)
plt.title('Merit Profiles')
plt.ylabel('Candidates')
plt.yticks(ind, candidates)
plt.xlabel('Mentions given')
plt.xticks([adjusted_width - vlorp], [judgments_count])
# plt.xticks(np.arange(0, 81, 10))
plt.legend(
legends, mentions,
ncol=len(mentions),
loc='upper center',
prop={'size': 6},
fancybox=True,
shadow=True,
bbox_to_anchor=(0.5, -0.15),
)
plt.axvline(x=adjusted_width*0.5, linestyle='--')
# ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05, fancybox=True, shadow=True, ncol=5)
# ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05, fancybox=True, shadow=True, ncol=5)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
if filename is not None:
# plt.savefig(filename)
plt.savefig(filename, dpi=200, bbox_inches='tight')
else:
plt.show()
plt.clf()
def main(args_parser, args): # move to bottom, no need for a func
log("MAJORITY JUDGMENT POLLING -- Version %s" % __version__)
......
import unittest
from limaju import deliberate
from limaju import deliberate, plot_merit_profile, load_mentions_from_string
class TestLimaju(unittest.TestCase):
......@@ -70,6 +70,17 @@ EXCELLENT, EXCELLENT, EXCELLENT, EXCELLENT
self.assertEqual(deliberation, ['B', 'D', 'A', 'C'])
def test_plotting_deliberation(self):
mentions = load_mentions_from_string(self.test_mentions)
judgments = ''
with open("examples/judgments_01.csv") as sample:
judgments = "".join(sample.readlines())
deliberation, tally = deliberate(judgments, self.test_mentions)
plot_merit_profile(tally, deliberation, mentions, filename="test_plot.png")
# self.assertEqual(deliberation, ['B', 'D', 'A', 'C'])
# def test_raise(self):
# with self.assertRaises(TypeError):
# s.split(2)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment