Ejemplo n.º 1
0
    def total_target_evaled(self, target_best_of_five, target_avgset):
        import total

        sumer = st.Sumator()
        sets_sumer = st.Sumator()
        for tour, match, aset in self.tour_match_aset:
            if aset is not None:
                avgset_coef = target_avgset / aset
            else:
                year = max(2005, tour.date.year)
                aset = total.generic_avgset(
                    tour.sex,
                    tour.surface,
                    tour.level == "chal",
                    match.rnd.qualification(),
                    min(2017, year),
                )
                if aset is None:
                    log.error("{} H2H {}-{} fail avgset at t-date{}".format(
                        tour.sex, self.fst_player, self.snd_player, tour.date))
                    avgset_coef = 1.0
                else:
                    avgset_coef = target_avgset / aset
            total_value = match.score.normalized_total(treat_bo5_as_bo3=False)
            sets_value = match.score.sets_count()
            best_of_five = match.score.best_of_five()
            if (best_of_five, target_best_of_five) == (False, True):
                total_value = total.normbo3_to_normbo5(total_value)
                sets_value = total.setsbo3_to_setsbo5(sets_value)
            elif (best_of_five, target_best_of_five) == (True, False):
                total_value = total.normbo5_to_normbo3(total_value)
                sets_value = total.setsbo5_to_setsbo3(sets_value)
            sumer.hit(total_value * avgset_coef)
            sets_sumer.hit(sets_value * avgset_coef)
        return sumer.average(), sets_sumer.average()
Ejemplo n.º 2
0
 def avgset(self, isqual):
     """return SizedValue"""
     res_sumator = st.Sumator()
     for rnd in list(self.matches_from_rnd.keys()):
         is_best_of_five = self.best_of_five(rnd)
         if rnd.qualification() is isqual:
             for match in self.matches_from_rnd[rnd]:
                 if match.score is None or match.paired():
                     continue
                 setnum = match.score.sets_count(full=True)
                 if setnum == 0:
                     continue
                 stotals = [
                     min(13, match.score[i][0] + match.score[i][1])
                     for i in range(setnum)
                 ]
                 # skips if winner is very strong dominated:
                 if max(stotals) <= 7:
                     continue  # skip if dominated to zero (or to one) in each set
                 if not isqual:
                     if not is_best_of_five and stotals in ([8, 6], [6, 8]):
                         continue  # skip if dominated 6-2, 6-0
                     if is_best_of_five and stotals in (
                         [8, 6, 6],
                         [6, 8, 6],
                         [6, 6, 8],
                     ):
                         continue  # skip if dominated 6-2, 6-0, 6-0
                 for set_total in stotals:
                     res_sumator.hit(set_total)
     if not res_sumator:
         return rl.SizedValue()
     return rl.SizedValue(value=res_sumator.average(), size=res_sumator.size)
Ejemplo n.º 3
0
def get_dif_bonus(sex, pid1, pid2, min_date=None, max_date=None):
    result1, result2 = st.Sumator(), st.Sumator()
    for date, match_results in results_dict[sex].items():
        if min_date is not None and date < min_date:
            continue
        if max_date is not None and date > max_date:
            break
        for match_res in match_results:
            if match_res.first_id == pid1:
                result1.hit(match_res.games_dif)  # win value
            elif match_res.second_id == pid1:
                result1.hit(-match_res.games_dif)  # loss value

            if match_res.first_id == pid2:
                result2.hit(match_res.games_dif)  # win value
            elif match_res.second_id == pid2:
                result2.hit(-match_res.games_dif)  # loss value
    svalue1 = rl.SizedValue.create_from_sumator(result1)
    svalue2 = rl.SizedValue.create_from_sumator(result2)
    return sized_value_to_float(svalue2) - sized_value_to_float(svalue1)
Ejemplo n.º 4
0
def player_games_dif_average(sex,
                             ident,
                             min_date=None,
                             max_date=None,
                             as_float=False):
    """:returns SizedValue if as_float is False"""
    sumer = st.Sumator()
    for date, match_results in results_dict[sex].items():
        if min_date is not None and date < min_date:
            continue
        if max_date is not None and date > max_date:
            break
        for match_res in match_results:
            if match_res.first_id == ident:
                sumer.hit(match_res.games_dif)  # win value
            elif match_res.second_id == ident:
                sumer.hit(-match_res.games_dif)  # loss value
    svalue = rl.SizedValue.create_from_sumator(sumer)
    return sized_value_to_float(svalue) if as_float else svalue
Ejemplo n.º 5
0
 def __init__(
     self,
     sex,
     max_tours_count=4,
     max_matches_count=254,
     max_years_count=3,
     max_diff_years=4,
     min_year=2000,
 ):
     self.sex = sex
     # condition for inner collected stuf:
     self.max_tours_count = max_tours_count
     self.max_matches_count = max_matches_count
     self.max_years_count = max_years_count
     self.max_diff_years = max_diff_years
     self.min_year = min_year
     self.as_years_nt_nm_from_surf_lev_cou_tname = defaultdict(
         lambda: [st.Sumator(), set(), 0, 0]
     )
Ejemplo n.º 6
0
    def get_features(self, ywn_hist_lst, match, tour=None, rnd=None):
        """return list of features"""

        def add_features(name, fst_sumator, snd_sumator):
            if (
                fst_sumator.size >= self.min_stat_size
                and snd_sumator.size >= self.min_stat_size
            ):
                feature.add_pair(
                    features,
                    name,
                    fst_value=fst_sumator.average(),
                    snd_value=snd_sumator.average(),
                )
            else:
                feature.add_pair(features, name)

        def player_stat(mstat, stat_name, side):
            method = getattr(mstat, stat_name)
            fst_snd = method()
            if fst_snd is None:
                return None
            fst_obj, snd_obj = fst_snd
            return fst_obj if side.is_left() else snd_obj

        def visit_stat_match(mch, coef, stat_name, fst_sum, snd_sum):
            if (
                mch.stat is None
                or mch.paired()
                or mch.score.sets_count(full=True) < 2
                or not mch.stat.is_total_points_won()
            ):
                return
            if match.first_player.ident in (
                mch.first_player.ident,
                mch.second_player.ident,
            ):
                side = co.side(match.first_player.ident == mch.first_player.ident)
                fst_obj = player_stat(mch.stat, stat_name, side)
                if (
                    fst_obj
                    and fst_obj.value < 1.01
                    and fst_sum.size < self.stop_stat_size
                ):
                    fst_sum += fst_obj.value * coef
            if match.second_player.ident in (
                mch.first_player.ident,
                mch.second_player.ident,
            ):
                side = co.side(match.second_player.ident == mch.first_player.ident)
                snd_obj = player_stat(mch.stat, stat_name, side)
                if (
                    snd_obj
                    and snd_obj.value < 1.01
                    and snd_sum.size < self.stop_stat_size
                ):
                    snd_sum += snd_obj.value * coef

        features = []
        for stat_name in self.stat_names:
            fst_sum, snd_sum = st.Sumator(), st.Sumator()
            for tr in weeked_tours.tours(self.sex, ywn_hist_lst[0]):  # cur match week
                coef = matchstat.generic_surface_trans_coef(
                    self.sex, stat_name, str(tr.surface), str(tour.surface)
                )
                for mch, rn in tr.match_rnd_list():
                    if mch.date is not None and match.date is not None:
                        isvisitable = mch.date < match.date
                    else:
                        isvisitable = rn < rnd
                    if isvisitable:
                        visit_stat_match(mch, coef, stat_name, fst_sum, snd_sum)

            for ywn in ywn_hist_lst[1:]:  # strong before current match weeks
                if (
                    fst_sum.size >= self.stop_stat_size
                    and snd_sum.size >= self.stop_stat_size
                ):
                    break
                for tr in weeked_tours.tours(self.sex, ywn):
                    coef = matchstat.generic_surface_trans_coef(
                        self.sex, stat_name, str(tr.surface), str(tour.surface)
                    )
                    for mch in tr.matches(reverse=True):
                        visit_stat_match(mch, coef, stat_name, fst_sum, snd_sum)

            add_features(stat_name, fst_sum, snd_sum)
        return features