forked from hodgesmr/mastodon_digest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thresholds.py
43 lines (29 loc) · 1.1 KB
/
thresholds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import annotations
from enum import Enum
from typing import TYPE_CHECKING
from scipy import stats
if TYPE_CHECKING:
from models import ScoredPost
from scorers import Scorer
class Threshold(Enum):
LAX = 90
NORMAL = 95
STRICT = 98
def get_name(self):
return self.name.lower()
def posts_meeting_criteria(
self, posts: list[ScoredPost], scorer: Scorer
) -> list[ScoredPost]:
"""Returns a list of ScoredPosts that meet this Threshold with the given Scorer"""
all_post_scores = [p.get_score(scorer) for p in posts]
min_score = stats.scoreatpercentile(all_post_scores, per=self.value)
threshold_posts = [
post for post, score in zip(posts, all_post_scores) if score >= min_score
]
return threshold_posts
def get_thresholds():
"""Returns a dictionary mapping lowercase threshold names to values"""
return {i.get_name(): i.value for i in Threshold}
def get_threshold_from_name(name: str) -> Threshold:
"""Returns Threshold for a given named string"""
return Threshold[name.upper()]