-
Notifications
You must be signed in to change notification settings - Fork 24
feat/custom-leaderboards-command #3926
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49eee26
feat/custom-leaderboards-command
lsabor 47b0890
bug fixes
lsabor 2fe00e1
change command to task, add job, refactor scoring/utils functions for…
lsabor 009609b
erge branch 'main' of github.com:Metaculus/metaculus into feat/custom…
lsabor fd592a8
add graceful failure and fix bug from unintentional overriding variable
lsabor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import logging | ||
| from datetime import datetime | ||
| from collections import defaultdict | ||
|
|
||
| from django.db.models import QuerySet | ||
| import dramatiq | ||
|
|
||
| from posts.models import Post | ||
| from scoring.constants import LeaderboardScoreTypes, ScoreTypes | ||
| from scoring.models import Leaderboard, Score | ||
| from scoring.score_math import evaluate_question | ||
| from projects.models import Project | ||
| from questions.models import Question | ||
| from questions.constants import UnsuccessfulResolutionType | ||
| from scoring.utils import generate_entries_from_scores, process_entries_for_leaderboard | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def calculate_minimum_time_scores( | ||
| questions: QuerySet[Question], | ||
| minimum_time: datetime, | ||
| score_type: ScoreTypes = ScoreTypes.PEER, | ||
| ) -> list[Score]: | ||
|
|
||
| scores: list[Score] = [] | ||
|
|
||
| c = questions.count() | ||
| i = 0 | ||
| for question in questions: | ||
| i += 1 | ||
| logger.info(f"Processing question {i}/{c} (ID: {question.id})") | ||
| if question.open_time >= minimum_time: | ||
| scores.extend(question.scores.filter(score_type=score_type)) | ||
| continue | ||
| question.open_time = minimum_time | ||
| # simulate scores as if question open_time was minimum_time | ||
| new_scores = evaluate_question( | ||
| question=question, | ||
| resolution=question.resolution, | ||
| score_types=[score_type], | ||
| ) | ||
| scores.extend(new_scores) | ||
|
|
||
| return scores | ||
|
|
||
|
|
||
| def calculate_spot_times_scores( | ||
| questions: QuerySet[Question], | ||
| spot_times: list[datetime], | ||
| score_type: ScoreTypes = ScoreTypes.SPOT_PEER, | ||
| ) -> list[Score]: | ||
|
|
||
| scores: list[Score] = [] | ||
|
|
||
| c = questions.count() | ||
| i = 0 | ||
| for question in questions: | ||
| i += 1 | ||
| logger.info(f"Processing question {i}/{c} (ID: {question.id})") | ||
| question_scores: list[Score] = [] | ||
| for spot_time in spot_times: | ||
| # simulate scores as if question spot_scoring_time was spot_time | ||
| new_scores = evaluate_question( | ||
| question=question, | ||
| resolution=question.resolution, | ||
| score_types=[score_type], | ||
| spot_forecast_time=spot_time, | ||
| ) | ||
| question_scores.extend(new_scores) | ||
| user_score_map = defaultdict(list) | ||
| for score in question_scores: | ||
| user_score_map[(score.user_id, score.aggregation_method)].append( | ||
| score.score | ||
| ) | ||
| for (user_id, aggregation_method), user_scores in user_score_map.items(): | ||
| scores.append( | ||
| Score( | ||
| user_id=user_id, | ||
| aggregation_method=aggregation_method, | ||
| score=sum(user_scores) / len(spot_times), | ||
| score_type=score_type, | ||
| question=question, | ||
| coverage=len(user_scores) / len(spot_times), | ||
| ) | ||
| ) | ||
|
|
||
| return scores | ||
|
|
||
|
|
||
| @dramatiq.actor | ||
| def update_custom_leaderboard( | ||
| project_id: int, | ||
| minimum_time: datetime | None = None, | ||
| spot_times: list[datetime] | None = None, | ||
| score_type: ScoreTypes = ScoreTypes.PEER, | ||
| ) -> None: | ||
| project = Project.objects.filter(id=project_id).first() | ||
| if not project: | ||
| logger.error(f"Project with id {project_id} does not exist.") | ||
| return | ||
| if (not minimum_time and not spot_times) or (minimum_time and spot_times): | ||
lsabor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logger.error("minimum_time or spot_times must be provided, but not both.") | ||
| return | ||
|
|
||
| # setup | ||
| name = ( | ||
| f"Set open_time for {project.name} at {minimum_time}" | ||
| if minimum_time | ||
| else (f"Spot time for {project.name} at {len(spot_times)} spot times") | ||
| ) | ||
| leaderboard, _ = Leaderboard.objects.get_or_create( | ||
| prize_pool=0, | ||
| name=name, | ||
| project=project, | ||
| score_type=LeaderboardScoreTypes.MANUAL, | ||
| ) | ||
| questions = ( | ||
| leaderboard.get_questions() | ||
| .filter( | ||
| related_posts__post__curation_status=Post.CurationStatus.APPROVED, | ||
| resolution__isnull=False, | ||
| ) | ||
| .exclude(resolution__in=UnsuccessfulResolutionType) | ||
| ) | ||
| # detect if any questions actually resolved since last evaluation | ||
| existing_entries = leaderboard.entries.all() | ||
| if existing_entries.exists(): | ||
| last_evaluation_time = max( | ||
| entry.calculated_on for entry in existing_entries if entry.calculated_on | ||
| ) | ||
| questions = questions.filter(resolution_set_time__gt=last_evaluation_time) | ||
| if not questions.exists(): | ||
| logger.info( | ||
| "No questions resolved since last evaluation " | ||
| f"at {last_evaluation_time}, skipping leaderboard update." | ||
| ) | ||
| return | ||
|
|
||
| if minimum_time: | ||
| scores = calculate_minimum_time_scores(questions, minimum_time, score_type) | ||
|
|
||
| if spot_times: | ||
| if score_type == ScoreTypes.PEER: | ||
| score_type = ScoreTypes.SPOT_PEER | ||
| if score_type == ScoreTypes.BASELINE: | ||
| score_type = ScoreTypes.SPOT_BASELINE | ||
| scores = calculate_spot_times_scores(questions, spot_times, score_type) | ||
|
|
||
| # temporarily change leaderboard type for entry creation | ||
| if score_type in [ScoreTypes.PEER, ScoreTypes.SPOT_PEER]: | ||
| leaderboard.score_type = LeaderboardScoreTypes.PEER_TOURNAMENT | ||
| elif score_type in [ScoreTypes.BASELINE, ScoreTypes.SPOT_BASELINE]: | ||
| leaderboard.score_type = LeaderboardScoreTypes.SPOT_BASELINE_TOURNAMENT | ||
| else: | ||
| leaderboard.score_type = score_type | ||
| new_entries = generate_entries_from_scores(scores, questions, leaderboard) | ||
| leaderboard.score_type = LeaderboardScoreTypes.MANUAL | ||
|
|
||
| process_entries_for_leaderboard( | ||
| new_entries, project, leaderboard, force_finalize=False | ||
| ) | ||
|
|
||
| logger.info(f"Updated leaderboard: {leaderboard.name} with id {leaderboard.id}") | ||
| return | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only things happening in this file is factoring out bits from a few bulky functions so they can be used by the new custom leaderboard update task. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add specific items here
be sure to add failsafes