def round_week_shift_by_struct(tour, rnd):
    """
    -1 если раунд играется на прошлой неделе по отношению к основной неделе турнира
     0 если раунд играется на основной неделе турнира
     1 если раунд играется на следующей неделе по отношению к основной неделе турнира
    """

    def qualifying_shift_by_struct(tour):
        if (tour.level == "main" and tour.surface == "Carpet") or (
            tour.level == "main" and tour.surface == "Clay" and tour.sex == "wta"
        ):
            return 0
        else:
            return -1

    if tour.grand_slam():
        if rnd.qualification() or rnd.pre_qualification():
            return -1
        elif rnd >= tennis.Round("Fourth"):
            return 1
    elif tour.has_round(tennis.Round("Fourth")):  # example: Indian Wells, Miami tours
        if (tour.sex == "atp" and rnd >= tennis.Round("Third")) or (
            tour.sex == "wta" and rnd >= tennis.Round("Fourth")
        ):
            return 1
    else:
        if rnd == tennis.Round("Qualifying"):
            return qualifying_shift_by_struct(tour)
        if rnd.qualification() or rnd.pre_qualification():
            return -1
    return 0  # most often case
Example #2
0
    def test_soft_main_init(self):
        import tennis

        keys = Keys.soft_main_from_raw(level=tennis.Level("main"),
                                       rnd=tennis.Round("Second"))
        self.assertEqual(keys["level"], "main")

        keys = Keys.soft_main_from_raw(level=tennis.Level("main"),
                                       rnd=tennis.Round("q-First"))
        self.assertEqual(keys["level"], "qual")

        keys = Keys.soft_main_from_raw(level=tennis.Level("masters"),
                                       rnd=tennis.Round("Second"))
        self.assertEqual(keys["level"], "main")

        keys = Keys.soft_main_from_raw(level=tennis.Level("masters"),
                                       rnd=tennis.Round("q-First"))
        self.assertEqual(keys["level"], "main")

        keys = Keys.soft_main_from_raw(level=tennis.Level("gs"),
                                       rnd=tennis.Round("Second"))
        self.assertEqual(keys["level"], "main")

        keys = Keys.soft_main_from_raw(level=tennis.Level("gs"),
                                       rnd=tennis.Round("q-First"))
        self.assertEqual(keys["level"], "qual")

        keys = Keys.soft_main_from_raw(level=tennis.Level("chal"),
                                       rnd=tennis.Round("Second"))
        self.assertEqual(keys["level"], "chal")

        keys = Keys.soft_main_from_raw(level=tennis.Level("chal"),
                                       rnd=tennis.Round("q-First"))
        self.assertEqual(keys["level"], "chal")

        keys = Keys.soft_main_from_raw(level=tennis.Level("future"),
                                       rnd=tennis.Round("Second"))
        self.assertEqual(keys["level"], "future")

        keys = Keys.soft_main_from_raw(level=tennis.Level("future"),
                                       rnd=tennis.Round("q-First"))
        self.assertEqual(keys["level"], "future")
Example #3
0
    def last_res_advantage_side(self):
        if not self.fst_last_res or not self.snd_last_res:
            return None
        if (self.level in ("masters", "gs") and self.rnd >= tennis.Round("Fourth")) or (
            self.level == "main" and self.rnd >= tennis.Round("1/2")
        ):
            return None  # players are deep in tournament
        fst_bonuses, snd_bonuses = 0, 0

        fst_poor = self.fst_last_res.poor_practice()
        snd_poor = self.snd_last_res.poor_practice()
        if fst_poor and not snd_poor and self.snd_last_res.min_load_vs_poor_practice():
            snd_bonuses += 1
        elif (
            not fst_poor and snd_poor and self.fst_last_res.min_load_vs_poor_practice()
        ):
            fst_bonuses += 1

        if self.rnd in ("q-First", "q-Second"):
            fst_prev_empty = self.fst_last_res.prev_weeks_empty(weeks_num=2)
            snd_prev_empty = self.snd_last_res.prev_weeks_empty(weeks_num=2)
            if not fst_prev_empty and snd_prev_empty:
                fst_bonuses += 1
            if fst_prev_empty and not snd_prev_empty:
                snd_bonuses += 1
        elif self.rnd == "First":
            fst_prev_empty = self.fst_last_res.last_weeks_empty(weeks_num=3)
            snd_prev_empty = self.snd_last_res.last_weeks_empty(weeks_num=3)
            if not fst_prev_empty and snd_prev_empty:
                fst_bonuses += 1
            if fst_prev_empty and not snd_prev_empty:
                snd_bonuses += 1

        if fst_bonuses > 0 and snd_bonuses == 0:
            return co.LEFT
        elif snd_bonuses > 0 and fst_bonuses == 0:
            return co.RIGHT
 def find_and_union(tours):
     slam_indicies = [
         i
         for i in range(len(tours))
         if tours[i].grand_slam() or tours[i].name in ("Perth", "Olympics")
     ]
     slam_size = len(slam_indicies)
     if slam_size <= 1:
         return False
     for i in range(slam_size - 1):
         fst_idx = slam_indicies[i]
         for j in range(i + 1, slam_size):
             snd_idx = slam_indicies[j]
             if (
                 tours[fst_idx].date != tours[snd_idx].date
                 or tours[fst_idx].name != tours[snd_idx].name
             ):
                 continue
             if (
                 tours[fst_idx].has_round(tennis.Round("First"), paired=False)
                 or tours[fst_idx].has_round(tennis.Round("Robin"), paired=False)
             ) and (
                 not tours[snd_idx].has_round(tennis.Round("First"), paired=False)
                 and not tours[snd_idx].has_round(
                     tennis.Round("Robin"), paired=False
                 )
             ):
                 main_idx, mix_idx = fst_idx, snd_idx
             elif (
                 tours[snd_idx].has_round(tennis.Round("First"), paired=False)
                 or tours[snd_idx].has_round(tennis.Round("Robin"), paired=False)
             ) and (
                 not tours[fst_idx].has_round(tennis.Round("First"), paired=False)
                 and not tours[fst_idx].has_round(
                     tennis.Round("Robin"), paired=False
                 )
             ):
                 main_idx, mix_idx = snd_idx, fst_idx
             else:
                 continue
             for rnd, matches in list(tours[mix_idx].matches_from_rnd.items()):
                 tours[main_idx].matches_from_rnd[rnd] += copy.deepcopy(matches)
             del tours[mix_idx]
             return True
     return False
    def test_best_of_five(self):
        isbo5 = best_of_five(
            date=datetime.date(2020, 9, 3),
            sex="atp",
            tour_name=tour_name.TourName("U.S. Open"),
            level="gs",
            rnd=tennis.Round("Third"),
        )
        self.assertTrue(isbo5)

        isbo5 = best_of_five(
            date=datetime.date(2018, 9, 3),
            sex="atp",
            tour_name=tour_name.TourName("Davis Cup"),
            level="teamworld",
            rnd=tennis.Round("Robin"),
        )
        self.assertTrue(isbo5)

        isbo5 = best_of_five(
            date=datetime.date(2020, 9, 3),
            sex="atp",
            tour_name=tour_name.TourName("ATP Cup"),
            level="teamworld",
            rnd=tennis.Round("Robin"),
        )
        self.assertFalse(isbo5)

        isbo5 = best_of_five(
            date=datetime.date(2020, 9, 3),
            sex="atp",
            tour_name=tour_name.TourName("Davis Cup"),
            level="teamworld",
            rnd=tennis.Round("Robin"),
        )
        self.assertFalse(isbo5)

        isbo5 = best_of_five(
            date=datetime.date(2020, 9, 3),
            sex="atp",
            tour_name="Davis Cup",
            level="teamworld",
            rnd=tennis.Round("Robin"),
        )
        self.assertFalse(isbo5)

        isbo5 = best_of_five(
            date=datetime.date(2018, 9, 3),
            sex="atp",
            tour_name="Davis Cup",
            level="team",
            rnd=tennis.Round("Robin"),
        )
        self.assertFalse(isbo5)
def tours_generator(
    sex,
    todaymode=False,
    min_date=None,
    max_date=None,
    time_reverse=False,
    with_paired=False,
    with_mix=False,
    rnd_detailing=False,
    with_bets=False,
    with_stat=False,
    with_ratings=False,
    with_pers_det=False,
):
    from matchstat import get

    assert not (not with_paired and with_mix), "inconsistent with_paired with_mix"
    sql_builder = SqlBuilder(
        sex,
        todaymode,
        min_date=min_date,
        max_date=max_date,
        time_reverse=time_reverse,
        with_paired=with_paired,
        with_mix=with_mix,
    )
    cur_tour = None
    for row in sql_builder.rows():
        tour = Tournament(
            row.tour_id,
            row.tour_name,
            sex=sex,
            surface=tennis.Surface(row.surf_txt),
            rank=row.tour_rank,
            date=row.tour_dt.date() if row.tour_dt else None,
            money=row.tour_money,
            cou=row.tour_cou,
        )
        rnd = tennis.Round(row.rnd_txt)
        scr = sc.Score(row.score_txt) if row.score_txt else None
        fst_player = tennis.Player(
            ident=row.fst_plr_id,
            name=row.fst_plr_name,
            cou=row.fst_plr_cou,
            birth_date=row.fst_plr_dt.date() if row.fst_plr_dt else None,
        )
        snd_player = tennis.Player(
            ident=row.snd_plr_id,
            name=row.snd_plr_name,
            cou=row.snd_plr_cou,
            birth_date=row.snd_plr_dt.date() if row.snd_plr_dt else None,
        )
        m = tennis.Match(
            fst_player,
            snd_player,
            scr,
            rnd,
            row.match_dt.date() if row.match_dt else None,
        )
        if not m.paired():
            if with_pers_det:
                m.read_pers_det(sex)
            if with_ratings:
                m.read_ratings(sex, tour.date)
            if with_bets:
                m.fill_offer(sex, tour.ident, tour.date, alter_bettor=False)
            if with_stat:
                m.stat = get(
                    sex,
                    tour.ident,
                    rnd,
                    fst_player.ident,
                    snd_player.ident,
                    tt.get_year_weeknum(tour.date),
                )

        if cur_tour is not None and cur_tour.ident != tour.ident:
            if rnd_detailing:
                cur_tour.rounds_detailing()
            yield cur_tour
            cur_tour = tour
        elif cur_tour is None:
            cur_tour = tour
        cur_tour.matches_from_rnd[rnd].append(m)

    if cur_tour is not None:
        if rnd_detailing:
            cur_tour.rounds_detailing()
        yield cur_tour
 def rounds_detailing(self):
     for matches in list(self.matches_from_rnd.values()):
         for m in matches:
             m.first_draw_status = ""
             m.second_draw_status = ""
     qual_r1st_plr_ids = [
         m.first_player.ident
         for m in self.matches_from_rnd[tennis.Round("q-First")]
         if not m.paired() and not m.second_player.unknown()
     ] + [
         m.second_player.ident
         for m in self.matches_from_rnd[tennis.Round("q-First")]
         if not m.paired() and not m.first_player.unknown()
     ]
     qual_r2nd_plr_ids = [
         m.first_player.ident
         for m in self.matches_from_rnd[tennis.Round("q-Second")]
         if not m.paired() and not m.second_player.unknown()
     ] + [
         m.second_player.ident
         for m in self.matches_from_rnd[tennis.Round("q-Second")]
         if not m.paired() and not m.first_player.unknown()
     ]
     qual_final_plr_ids = [
         m.first_player.ident
         for m in self.matches_from_rnd[tennis.Round("Qualifying")]
         if not m.paired() and not m.second_player.unknown()
     ] + [
         m.second_player.ident
         for m in self.matches_from_rnd[tennis.Round("Qualifying")]
         if not m.paired() and not m.first_player.unknown()
     ]
     qual_plr_ids = (
         set(qual_r1st_plr_ids) | set(qual_r2nd_plr_ids) | set(qual_final_plr_ids)
     )
     if qual_plr_ids:
         self.qual_lost = False
     else:
         qual_plr_ids = self.__qual_idents_from_dbseed()
     md_r1st_plr_ids = [
         m.first_player.ident
         for m in self.matches_from_rnd[tennis.Round("First")]
         if not m.paired() and not m.second_player.unknown()
     ] + [
         m.second_player.ident
         for m in self.matches_from_rnd[tennis.Round("First")]
         if not m.paired() and not m.first_player.unknown()
     ]
     md_r2nd_plr_ids = [
         m.first_player.ident
         for m in self.matches_from_rnd[tennis.Round("Second")]
         if not m.paired() and not m.second_player.unknown()
     ] + [
         m.second_player.ident
         for m in self.matches_from_rnd[tennis.Round("Second")]
         if not m.paired() and not m.first_player.unknown()
     ]
     bye_plr_ids = set(md_r2nd_plr_ids) - set(md_r1st_plr_ids) - set(qual_plr_ids)
     if not qual_plr_ids and not bye_plr_ids:
         return
     for rnd in sorted(self.matches_from_rnd.keys()):
         if rnd.qualification() or rnd.pre_qualification() or rnd.robin():
             continue
         for match in self.matches_from_rnd[rnd]:
             self.__round_detailing_impl(
                 match,
                 qual_plr_ids,
                 bye_plr_ids,
                 len(md_r1st_plr_ids),
                 len(md_r2nd_plr_ids),
             )
Example #8
0
    def load_pre_live_data(self):
        def simple_feature(name):
            if name in dct:
                self.features.append(feature.RigidFeature(name=name, value=dct[name]))

        def simple_sv_feature(name):
            if name in dct:
                sval_args = dct[name]
                if sval_args is not None:
                    sval = rl.SizedValue(*sval_args)
                    self.features.append(feature.RigidFeature(name=name, value=sval))

        def plr_sv_feature(name):
            fst_name = f"fst_{name}"
            snd_name = f"snd_{name}"
            if fst_name in dct and snd_name in dct:
                fst_val = rl.SizedValue(*dct[fst_name])
                snd_val = rl.SizedValue(*dct[snd_name])
                self.features.append(
                    feature.Feature(name=fst_name, value=fst_val, flip_value=snd_val)
                )
                self.features.append(
                    feature.Feature(name=snd_name, value=snd_val, flip_value=fst_val)
                )

        def plr_feature(name):
            fst_name = f"fst_{name}"
            snd_name = f"snd_{name}"
            if fst_name in dct and snd_name in dct:
                fst_val = dct[fst_name]
                snd_val = dct[snd_name]
                self.features.append(
                    feature.Feature(name=fst_name, value=fst_val, flip_value=snd_val)
                )
                self.features.append(
                    feature.Feature(name=snd_name, value=snd_val, flip_value=fst_val)
                )

        def player_load(prefix: str, player: tennis.Player):
            player.ident = dct.get(prefix + "_player_id")
            player.name = dct.get(prefix + "_player_name")
            player.rating = ratings.Rating(dct.get(prefix + "_player_rating"))
            ymd = dct.get(prefix + "_player_bdate")
            if ymd is not None:
                player.birth_date = datetime.date(ymd[0], ymd[1], ymd[2])
            setattr(
                self,
                "hist_" + prefix + "_srv_win",
                rl.SizedValue(*dct[prefix + "_hist_srv_win"]),
            )
            setattr(
                self,
                "hist_" + prefix + "_rcv_win",
                rl.SizedValue(*dct[prefix + "_hist_rcv_win"]),
            )

        dct = pre_live_dir.get_data(self.pre_live_name())
        if self.first_player is None:
            self.first_player = tennis.Player()
        if self.second_player is None:
            self.second_player = tennis.Player()
        player_load("fst", self.first_player)
        player_load("snd", self.second_player)
        if "rnd" in dct:
            rnd_txt = dct["rnd"]
            if rnd_txt:
                rnd_txt_parts = rnd_txt.split(";")
                self.rnd = tennis.Round(rnd_txt_parts[0])
        self.decset_ratio_dif = dct.get("decset_ratio_dif")
        self.decset_bonus_dif = dct.get("decset_bonus_dif")
        self.h2h_direct = dct.get("h2h_direct")

        self.features = []
        simple_feature("recently_winner_id")
Example #9
0
 def rnd(self):
     return tennis.Round(self.round_var.get())