def main(args: Namespace): load_teams(args.database) leagues = [] if args.country: for country in args.country: leagues.extend([ code for code, league in league_register.items() if league.country == country.capitalize() ]) if args.league: leagues.extend(list(args.league)) if not args.country and not args.league: leagues.extend(list(league_register.keys())) for league_code in leagues: league = league_register[league_code] load_league(args.database, league) seasons = Season.seasons(league) if not seasons: messages.warning_message("No season data found") else: if args.history: seasons = seasons[-args.history:] if seasons[-1].current: this_season = seasons.pop() if args.team: teams = [] for team_name in get_multiple_teams(args): (row, ) = extract_picked_team(args.database, team_name, league) team = Team.inventory[row[0]] teams.append(team) else: teams = this_season.teams() events = [Event.get(event) for event in args.event] historical_data = {} for event in events: historical_data[event] = DataUnit(Counter(), seasons) for season in seasons: for team in season.teams(): count_events(season, team, args.venue, args.half, event, args.negate, historical_data[event]) analyse_teams(args, league, seasons, this_season, teams, historical_data) else: messages.error_message( "The current season has not yet started")
def analyse_team_and_event(seasons: List[Season], this_season: Season, team: Team, event: Callable): team_history = DataUnit(Counter(), seasons) for season in seasons: count_events(season, team, args.venue, args.half, event, args.negate, team_history) team_now = DataUnit(Counter(), [this_season], team=team) count_events(this_season, team, args.venue, args.half, event, args.negate, team_now) return team_now, team_history
def compute_chunked_data(seasons: List[Season], func: Callable, negate: bool, venue: Venue, half: Half, chunk_size: int): chunk_to_chart = OrderedDict() for season in seasons: table = LeagueTable(season, half) table_map = table.group(chunk_size) for chunk_id in range(table_map.number_of_chunks()): if chunk_id not in chunk_to_chart: chart = DataUnit(Counter(), seasons, positions=table_map.get_rows(chunk_id)) chunk_to_chart[chunk_id] = chart chart = chunk_to_chart[chunk_id] for row_id in table_map.get_rows(chunk_id): team = table[row_id].TEAM count_events(season, team, venue, half, func, negate, chart) return list(chunk_to_chart.values())
def compute_aggregated_data(seasons: List[Season], selected_team: Team, func: Callable, negate: bool, venue: Venue, half: Half): if seasons[-1].current: this_season = seasons.pop() else: this_season = None charts = [] aggregated_history = DataUnit(Counter(), seasons) charts.append(aggregated_history) for season in seasons: for team in season.teams(): count_events(season, team, venue, half, func, negate, aggregated_history) if selected_team is not None: team_history = DataUnit(Counter(), seasons, team=selected_team) charts.append(team_history) for season in seasons: count_events(season, team_history.team, venue, half, func, negate, team_history) if this_season is not None: aggregated_now = DataUnit(Counter(), [this_season]) charts.append(aggregated_now) for team in this_season.teams(): count_events(this_season, team, venue, half, func, negate, aggregated_now) if selected_team is not None: team_now = DataUnit(Counter(), [this_season], team=selected_team, highlight=True) charts.append(team_now) count_events(this_season, selected_team, venue, half, func, negate, team_now) return charts
def main(args: Namespace): load_teams(args.database) league = league_register[get_unique_league(args)] load_league(args.database, league) seasons = Season.seasons(league) if not seasons: messages.error_message("No season data found") if args.history: seasons = seasons[-args.history:] if args.team: (row, ) = extract_picked_team(args.database, args.team, league) selected_team = Team.inventory[row[0]] else: selected_team = None func = Event.get(get_unique_event(args)) if args.chunks: data = compute_chunked_data(seasons, func, args.negate, args.venue, args.half, args.chunks) nrows = len(data) if selected_team is not None: ncols = 3 else: ncols = 1 fig, axes = plt.subplots(nrows=len(data), ncols=ncols, figsize=(20, 13), squeeze=False, constrained_layout=True) x_limit, _ = find_limits(data) for i, datum in enumerate(data): ax = axes[i, 0] plot(ax, datum, x_limit, args.lines) if selected_team is not None: golden_season = seasons[-1] golden_table = LeagueTable(golden_season, args.half) golden_map = golden_table.group(args.chunks) chunk_to_seasons = OrderedDict() for chunk_id in range(golden_map.number_of_chunks()): if chunk_id not in chunk_to_seasons: chunk_to_seasons[chunk_id] = [] for season in seasons: if season != golden_season: table = LeagueTable(season, args.half) table_map = table.group(args.chunks) if selected_team in season.teams(): position = table.team_position(selected_team) chunk_id = table_map.get_chunk(position) chunk_to_seasons[chunk_id].append(season) chunk_to_datum = OrderedDict() for chunk_id, chunk_seasons in chunk_to_seasons.items(): if chunk_seasons: datum = DataUnit(Counter(), chunk_seasons, team=selected_team, positions=golden_map.get_rows(chunk_id)) chunk_to_datum[chunk_id] = datum for season in seasons: if season == golden_season: position = golden_table.team_position(selected_team) datum = DataUnit(Counter(), [golden_season], team=selected_team, positions=[position], highlight=True) golden_datum = datum else: if selected_team in season.teams(): table = LeagueTable(season, args.half) table_map = table.group(args.chunks) position = table.team_position(selected_team) chunk_id = table_map.get_chunk(position) datum = chunk_to_datum[chunk_id] count_events(season, selected_team, args.venue, args.half, func, args.negate, datum) position = golden_table.team_position(selected_team) chunk_id = golden_map.get_chunk(position) ax = axes[chunk_id, 1] plot(ax, golden_datum, x_limit, args.lines) used = {(chunk_id, 1)} for chunk_id, datum in chunk_to_datum.items(): ax = axes[chunk_id, 2] plot(ax, datum, x_limit, args.lines) used.add((chunk_id, 2)) for i in range(0, nrows): for j in range(1, 3): if (i, j) not in used: fig.delaxes(axes[i, j]) else: data = compute_aggregated_data(seasons, selected_team, func, args.negate, args.venue, args.half) x_limit, _ = find_limits(data) display = DisplayGrid(len(data), 2) fig, axes = plt.subplots(nrows=display.nrows, ncols=display.ncols, figsize=(20, 10), squeeze=False) for i, datum in enumerate(data): cell_x, cell_y = display.index(i) ax = axes[cell_x, cell_y] plot(ax, datum, x_limit, args.lines) title = construct_title(league, func, args.negate, args.venue, args.half) fig.suptitle(title, fontweight='bold', fontsize=14) plt.show(block=args.block)