Improve filtering and add "other"
This commit is contained in:
parent
b5d05722d1
commit
451d9ba77e
1 changed files with 17 additions and 3 deletions
|
@ -12,6 +12,7 @@ from conf import CONFIG
|
||||||
|
|
||||||
|
|
||||||
class QuoteQuota(commands.Cog):
|
class QuoteQuota(commands.Cog):
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.quotes_channel_id = CONFIG["quote_a"].get("channel_id")
|
self.quotes_channel_id = CONFIG["quote_a"].get("channel_id")
|
||||||
|
@ -25,8 +26,8 @@ class QuoteQuota(commands.Cog):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate_pie_chart(
|
def generate_pie_chart(
|
||||||
usernames: Iterable[str],
|
usernames: list[str],
|
||||||
counts: Iterable[int],
|
counts: list[int],
|
||||||
) -> discord.File:
|
) -> discord.File:
|
||||||
"""
|
"""
|
||||||
Converts the given username and count tuples into a nice pretty pie chart.
|
Converts the given username and count tuples into a nice pretty pie chart.
|
||||||
|
@ -39,6 +40,19 @@ class QuoteQuota(commands.Cog):
|
||||||
def pct(v: int):
|
def pct(v: int):
|
||||||
return f"{v:.1f}% ({(v / 100) * sum(counts):0f})"
|
return f"{v:.1f}% ({(v / 100) * sum(counts):0f})"
|
||||||
|
|
||||||
|
other = []
|
||||||
|
# Any authors with less than 5% of the total count will be grouped into "other"
|
||||||
|
for i, author in enumerate(usernames.copy()):
|
||||||
|
if (c := counts[i]) / sum(counts) < 0.05:
|
||||||
|
other.append(c)
|
||||||
|
counts[i] = -1
|
||||||
|
usernames.remove(author)
|
||||||
|
if other:
|
||||||
|
usernames.append("Other")
|
||||||
|
counts.append(sum(other))
|
||||||
|
# And now filter out any -1% counts
|
||||||
|
counts = [c for c in counts if c != -1]
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
fig, ax = plt.subplots()
|
||||||
ax.pie(
|
ax.pie(
|
||||||
counts,
|
counts,
|
||||||
|
@ -46,7 +60,7 @@ class QuoteQuota(commands.Cog):
|
||||||
autopct=pct,
|
autopct=pct,
|
||||||
)
|
)
|
||||||
fio = io.BytesIO()
|
fio = io.BytesIO()
|
||||||
fig.savefig(fio, format='jpg', bbox_inches='tight')
|
fig.savefig(fio, format='jpg')
|
||||||
fio.seek(0)
|
fio.seek(0)
|
||||||
return discord.File(fio, filename="pie.jpeg")
|
return discord.File(fio, filename="pie.jpeg")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue