<commit_before>from collections import defaultdict
import math
import numpy as np
import time

from consts.event_type import EventType
from database.event_query import TeamYearEventsQuery
from helpers.event_helper import EventHelper
from helpers.match_helper import MatchHelper
from helpers.matchstats_helper import MatchstatsHelper
from models.event_details import EventDetails
from models.match import Match


class ContributionCalculator(object):
    def __init__(self, event, matches, stat, default_mean, default_var):
        """
        stat: 'score' or a specific breakdown like 'auto_points' or 'boulders'
        """
        self._event = event
        self._matches = matches
        self._stat = stat
        self._default_mean = default_mean
        self._default_var = default_var

        self._team_list, self._team_id_map = self._build_team_mapping()

        # Setup matrices
        m = len(self._matches)
        t = len(self._team_list)
        self._Ao = np.zeros((2*m, t))  # Match teams
        self._Mmean = np.zeros((2*m, 1))  # Means
        self._Mvar = np.zeros((2*m, 1))  # Variances

        # Past event stats for initialization
        self._past_stats_mean, self._past_stats_var = self._get_past_stats(self._event, self._team_list)

        # For finding event averages for initialization
        self._mean_sums = []
        self._var_sums = []

        # These aren't used to persist state, just allocating space
        self._Oe = np.zeros((t, 1))  # Prior estimates
        self._diags = np.ndarray(t)  # Prior estimates variances

        # Things to return
        self._means = {}
        self._vars = {}

    def _build_team_mapping(self):
        """
        Returns (team_list, team_id_map)
        team_list: A list of team_str such as 'frc254' or 'frc254B'
        team_id_map: A dict of key: team_str, value: row index in x_matrix that corresponds to the team
        """
        # Build team list
        team_list = set()
        for match in self._matches:
            for alliance_color in ['red', 'blue']:
                for team in match.alliances[alliance_color]['teams']:
                    team_list.add(team)

        team_list = list(team_list)
        team_id_map = {}
        for i, team in enumerate(team_list):
            team_id_map[team] = i

        return team_list, team_id_map

    def _get_past_stats(self, cur_event, team_list):
        past_stats_mean = defaultdict(list)  # team key > values
        past_stats_var = defaultdict(list)  # team key > values

        no_priors_team_list = team_list

        for year_diff in xrange(2):
            team_events_futures = []
            for team in no_priors_team_list:
                team_events_futures.append((team, TeamYearEventsQuery(team, cur_event.year - year_diff).fetch_async()))

            no_priors_team_list = []
            for team, events_future in team_events_futures:
                events = events_future.get_result()
                EventHelper.sort_events(events)<commit_msg>Don't use previous year data to seed predictions<commit_after>from collections import defaultdict
import math
import numpy as np
import time

from consts.event_type import EventType
from database.event_query import TeamYearEventsQuery
from helpers.event_helper import EventHelper
from helpers.match_helper import MatchHelper
from helpers.matchstats_helper import MatchstatsHelper
from models.event_details import EventDetails
from models.match import Match


class ContributionCalculator(object):
    def __init__(self, event, matches, stat, default_mean, default_var):
        """
        stat: 'score' or a specific breakdown like 'auto_points' or 'boulders'
        """
        self._event = event
        self._matches = matches
        self._stat = stat
        self._default_mean = default_mean
        self._default_var = default_var

        self._team_list, self._team_id_map = self._build_team_mapping()

        # Setup matrices
        m = len(self._matches)
        t = len(self._team_list)
        self._Ao = np.zeros((2*m, t))  # Match teams
        self._Mmean = np.zeros((2*m, 1))  # Means
        self._Mvar = np.zeros((2*m, 1))  # Variances

        # Past event stats for initialization
        self._past_stats_mean, self._past_stats_var = self._get_past_stats(self._event, self._team_list)

        # For finding event averages for initialization
        self._mean_sums = []
        self._var_sums = []

        # These aren't used to persist state, just allocating space
        self._Oe = np.zeros((t, 1))  # Prior estimates
        self._diags = np.ndarray(t)  # Prior estimates variances

        # Things to return
        self._means = {}
        self._vars = {}

    def _build_team_mapping(self):
        """
        Returns (team_list, team_id_map)
        team_list: A list of team_str such as 'frc254' or 'frc254B'
        team_id_map: A dict of key: team_str, value: row index in x_matrix that corresponds to the team
        """
        # Build team list
        team_list = set()
        for match in self._matches:
            for alliance_color in ['red', 'blue']:
                for team in match.alliances[alliance_color]['teams']:
                    team_list.add(team)

        team_list = list(team_list)
        team_id_map = {}
        for i, team in enumerate(team_list):
            team_id_map[team] = i

        return team_list, team_id_map

    def _get_past_stats(self, cur_event, team_list):
        past_stats_mean = defaultdict(list)  # team key > values
        past_stats_var = defaultdict(list)  # team key > values

        no_priors_team_list = team_list

        for year_diff in xrange(1):
            team_events_futures = []
            for team in no_priors_team_list:
                team_events_futures.append((team, TeamYearEventsQuery(team, cur_event.year - year_diff).fetch_async()))

            no_priors_team_list = []
            for team, events_future in team_events_futures:
                events = events_future.get_result()
                EventHelper.sort_events(events)
{'fraction_non_alphanumeric': 0.08117898896756133, 'fraction_numerical': 0.004281244854273012, 'mean_word_length': 2.519119351100811, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3193381', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1759, 'n_words': 472}