Advertisement
Guest User

Untitled

a guest
Apr 21st, 2024
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | Source Code | 0 0
  1. from typing import Tuple, List
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4.  
  5. columns = ['map name', 'TvZ', 'ZvP', 'PvT']
  6. data = [
  7.     ('Alcyone', (149, 139), (157, 120), (162, 197)),
  8.     ('Amphion', (12, 9), (10, 6), (7, 13)),
  9.     ('Crimson Court', (20, 8), (4, 11), (22, 25)),
  10.     ('Dynasty', (15, 3), (9, 14), (21, 17)),
  11.     ('Ghost River', (11, 10), (12, 17), (26, 22)),
  12.     ('Goldenaura', (88, 95), (80, 107), (135, 143)),
  13.     ('Oceanborn', (126, 103), (205, 184), (251, 267)),
  14.     ('Post Youth', (9, 11), (11, 13), (8, 21)),
  15.     ('Site Delta', (113, 137), (142, 139), (138, 144)),
  16.     ('Hard Lead', (99, 101), (159, 159), (112, 173)),
  17.     ('Hecate', (68, 55), (102, 114), (170, 178)),
  18.     ('Radhuset', (33, 37), (32, 40), (62, 51)),
  19.     ('Solaris', (63, 92), (85, 84), (104, 93)),
  20.     ('Equilibrium', (50, 60), (68, 40), (70, 53))
  21. ]
  22.  
  23. s1_maps = ('Alcyone', 'Equilibrium', 'Goldenaura', 'Hard Lead', 'Hecate', 'Oceanborn', 'Radhuset', 'Site Delta', 'Solaris')
  24. s2_maps = ('Alcyone', 'Amphion', 'Crimson Court', 'Dynasty', 'Ghost River', 'Goldenaura', 'Oceanborn', 'Post Youth', 'Site Delta')
  25. new_maps = ('Amphion', 'Crimson Court', 'Dynasty', 'Ghost River', 'Post Youth')
  26.  
  27. def print_winrates(sample: Tuple) -> None:
  28.     name, TvZ, ZvP, PvT = sample
  29.     TvZ_w = round(TvZ[0] / sum(TvZ) * 100, 1)
  30.     ZvP_w = round(ZvP[0] / sum(ZvP) * 100, 1)
  31.     PvT_w = round(PvT[0] / sum(PvT) * 100, 1)
  32.     print(f'Winrate for {name}: TvZ: {TvZ_w} ZvP: {ZvP_w}, PvT: {PvT_w}')
  33.  
  34. def merge_samples(samples: List[Tuple], name: str) -> Tuple:
  35.     TvZ, ZvT = 0, 0
  36.     ZvP, PvZ = 0, 0
  37.     TvP, PvT = 0, 0
  38.     for _, (s_TvZ, s_ZvT), (s_ZvP, s_PvZ), (s_PvT, s_TvP) in samples:
  39.         TvZ += s_TvZ
  40.         ZvT += s_ZvT
  41.         ZvP += s_ZvP
  42.         PvZ += s_PvZ
  43.         PvT += s_PvT
  44.         TvP += s_TvP
  45.     return (name, (TvZ, ZvT), (ZvP, PvZ), (PvT, TvP))
  46.  
  47. def filter_samples(samples: List[Tuple], targets: Tuple[str]) -> List[Tuple]:
  48.     return list(filter(lambda sample: sample[0] in targets, samples))
  49.  
  50. def matchup_winrates(samples: List[Tuple], matchup: str) -> List[Tuple]:
  51.     matchup, side = get_matchup_indices(matchup)
  52.     res = []
  53.     for sample in samples:
  54.         name = sample[0]
  55.         sample_winrate = round(sample[matchup][side] / sum(sample[matchup]) * 100, 1)
  56.         res.append((name, sample_winrate))
  57.     return sorted(res, key=lambda sample: sample[1])
  58.  
  59. def race_winrates(samples: List[Tuple], race: str):
  60.     mu1, side1, mu2, side2 = get_race_indices(race)
  61.     res = []
  62.     for sample in samples:
  63.         name = sample[0]
  64.         mu1_wins = sample[mu1][side1]
  65.         mu1_total = sum(sample[mu1])
  66.         mu2_wins = sample[mu2][side2]
  67.         mu2_total = sum(sample[mu2])
  68.         sample_winrate = round((mu1_wins + mu2_wins) / (mu1_total + mu2_total) * 100, 1)
  69.         res.append((name, sample_winrate))
  70.     # return res
  71.     return sorted(res, key=lambda w: w[1])
  72.  
  73.  
  74. def get_matchup_indices(matchup: str) -> Tuple[int, int]:
  75.     if matchup == 'TvZ' or matchup == 'ZvT':
  76.         return 1, int(matchup == 'ZvT')
  77.     elif matchup == 'ZvP' or matchup == 'PvZ':
  78.         return 2, int(matchup == 'PvZ')
  79.     else:
  80.         return 3, int(matchup == 'TvP')
  81.  
  82. def get_race_indices(race: str) -> Tuple[int, int, int, int]:
  83.     if race == 'Z':
  84.         return *get_matchup_indices('ZvT'), *get_matchup_indices('ZvP')
  85.     elif race == 'T':
  86.         return *get_matchup_indices('TvZ'), *get_matchup_indices('TvP')
  87.     else:
  88.         return *get_matchup_indices('PvT'), *get_matchup_indices('PvZ')
  89.  
  90. def plot_matchup_winrates(winrates: List[Tuple[str, float]], matchup: str):
  91.     fig, ax = plt.subplots()
  92.     samples, rates = zip(*winrates)
  93.     p = ax.bar(samples, rates)
  94.     ax.bar_label(p, label_type='center')
  95.     ax.set_title(f'{matchup} winrates')
  96.     ax.set_ylabel('winrate')
  97.  
  98.     plt.show()
  99.  
  100. # Matchup winrates per map pools
  101. all_maps_winrates = merge_samples(data, 'all_maps')
  102. s1_winrates = merge_samples(filter_samples(data, s1_maps), 's1_maps')
  103. s2_winrates = merge_samples(filter_samples(data, s2_maps), 's2_maps')
  104. new_maps_winrates = merge_samples(filter_samples(data, new_maps), 'new_maps')
  105. aggregated_data = [all_maps_winrates, s1_winrates, s2_winrates, new_maps_winrates]
  106. for sample in aggregated_data:
  107.     print_winrates(sample)
  108.  
  109. # Race winrates
  110. ## Zerg
  111. ### Map pools
  112. zerg_map_pools = race_winrates(aggregated_data, 'Z')
  113. plot_matchup_winrates(zerg_map_pools, 'Map pools Zerg')
  114.  
  115. ## Protoss
  116. protoss_map_pool = race_winrates(aggregated_data, 'P')
  117. plot_matchup_winrates(protoss_map_pool, 'Map pools Protoss')
  118.  
  119. ## Terran
  120. terran_map_pool = race_winrates(aggregated_data, 'T')
  121. plot_matchup_winrates(terran_map_pool, 'Map pools Terran')
  122.  
  123. ### S2 results
  124. ## Zerg
  125. zerg_s2 = race_winrates(filter_samples(data, s2_maps), 'Z')
  126. plot_matchup_winrates(zerg_s2, 'Zerg S2')
  127.  
  128. ## Protoss
  129. protoss_s2 = race_winrates(filter_samples(data, s2_maps), 'P')
  130. plot_matchup_winrates(protoss_s2, 'Protoss S2')
  131.  
  132. ## Terran
  133. terran_s2 = race_winrates(filter_samples(data, s2_maps), 'T')
  134. plot_matchup_winrates(terran_s2, 'Terran S2')
  135.  
  136. # Matchup winrates
  137. ## Zerg
  138. ZvT_s2 = matchup_winrates(filter_samples(data, s2_maps), 'ZvT')
  139. TvP_s2 = matchup_winrates(filter_samples(data, s2_maps), 'TvP')
  140. ZvP_s2 = matchup_winrates(filter_samples(data, s2_maps), 'ZvP')
  141. plot_matchup_winrates(ZvT_s2, 'ZvT S2')
  142. plot_matchup_winrates(TvP_s2, 'TvP S2')
  143. plot_matchup_winrates(ZvP_s2, 'ZvP S2')
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement