Example #1
0
def test_game_modelV2():
    log = Log('test.log')
    model = 'v2'
    homedata = {}
    awaydata = {}
    g = Game()
    homedata['Points'] = 1
    homedata['GP'] = 1
    awaydata['Points'] = 1
    awaydata['GP'] = 1
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 58.0 / 117.0
    assert threshold['draw'] == 84.0 / 117.0
    homedata['Points'] = 3
    homedata['GP'] = 1
    awaydata['Points'] = 1
    awaydata['GP'] = 1
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 481.0 / 881.0
    assert threshold['draw'] == 710.0 / 881.0
    homedata['Points'] = 1
    homedata['GP'] = 1
    awaydata['Points'] = 3
    awaydata['GP'] = 1
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 433.0 / 957.0
    assert threshold['draw'] == 711.0 / 957.0
Example #2
0
def test_game_modelV1():
    log = Log('test.log')
    model = 'v1'
    homedata = {}
    awaydata = {}
    g = Game()
    threshold = g.modelV1(homedata, awaydata)
    assert threshold['home'] == 972.0
    assert threshold['draw'] == 533.0
    assert threshold['away'] == 450.0
Example #3
0
def test_game_modelV0():
    log = Log('test.log')
    model = 'v0'
    homedata = {}
    awaydata = {}
    g = Game()
    threshold = g.modelV0(homedata, awaydata)
    assert threshold['home'] == 1.0
    assert threshold['draw'] == 1.0
    assert threshold['away'] == 1.0
Example #4
0
def test_game_lookupGamesBySeason():
    log = Log('test.log')
    g = Game()
    g.connectDB()
    g.lookupGamesBySeason(1900, 'mls', '1900-01-01', log)
    assert len(g.games) == 0
    g.lookupGamesBySeason(1996, 'foo', '1996-01-01', log)
    assert len(g.games) == 0
    g.lookupGamesBySeason(1996, 'mls', '1996-01-01', log)
    assert len(g.games) == 160
Example #5
0
def test_game_init():
    g = Game()
    # object types
    assert isinstance(g, Game)
    assert isinstance(g.data, dict)
    # Default values
    assert g.data['ID'] == 0
Example #6
0
def test_game_calculateThreshold():
    log = Log('test.log')
    model = 'v0'
    homedata = {}
    awaydata = {}
    g = Game()
    # This tests the ability to get back different models based on a passed
    # parameter.
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 1.0 / 3.0
    model = 'v1'
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 972.0 / 1955.0
    model = 'v2'
    homedata['Points'] = 1
    homedata['GP'] = 1
    awaydata['Points'] = 1
    awaydata['GP'] = 1
    threshold = g.calculateThreshold(model, homedata, awaydata)
    assert threshold['home'] == 58.0 / 117.0
Example #7
0
    def simulateSeason(self, games, model, log):

        self.initSeason()

        for i in range(games.game_count):
            log.message(str(games.games[i]))
            homeAbbv = games.games[i]['Home']
            awayAbbv = games.games[i]['Away']

            # For now, we pretend the home team always wins
            game = Game()
            result = game.simulateResult(games.games[i],
                                         self.standings[homeAbbv],
                                         self.standings[awayAbbv],
                                         model)
            log.message(str(result))

            # Update standings based on result
            self.updateStandings(homeAbbv, awayAbbv, result)

        return self
Example #8
0
def test_game_connection():
    g = Game()
    assert hasattr(g, 'db') is False
    g.connectDB()
    assert hasattr(g, 'db')
    g.disconnectDB()
    assert hasattr(g, 'db') is False
Example #9
0
def test_league_simulateSeason():
    log = Log('test.log')
    model = 'v0'
    g = Game()
    g.connectDB()
    g.lookupGamesBySeason(1996, 'mls', '1996-01-01', log)
    l = League()
    l.connectDB()
    l.lookupTeamsBySeason(1996, 'mls', '1996-01-01', log)
    l.simulateSeason(g, model, log)
    assert l.standings['CLB']['GP'] == 32
Example #10
0
def test_game_simulateResult():
    log = Log('test.log')
    model = 'v0'
    g = Game()
    context = {}
    homedata = {}
    awaydata = {}
    # This is hacky, but should generally protect against unexpected results
    # being returned. I'm not so concerned here about testing randomness, or
    # specific distributions of results. That is numpy's problem.
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
    assert g.simulateResult(context, homedata, awaydata,
                            model) in ['home', 'draw', 'away']
Example #11
0
def main(mode, competition, model, batch, season, start):
    settings = Settings(mode, competition, model, batch, season, start)

    # Reflect back the configuration being used
    click.echo(settings.output())

    # Initialize tooling
    click.echo('Initializing tooling...')
    filename = date.today().strftime("%y%m%d") + '-' + mode + '-' + model
    log = Log('logs/' + filename + '.log')
    log.message('Started')
    output = Log('output/' + filename + '.csv')
    db = Database()
    db.connect()
    log.message('Database connected')

    # Initialize data
    click.echo('Initializing data...')

    league = League()
    league.connectDB()
    league.lookupTeamsBySeason(settings.values['season'],
                               settings.values['competition'],
                               settings.values['start'], log)
    game = Game()
    game.connectDB()
    game.lookupGamesBySeason(settings.values['season'],
                             settings.values['competition'],
                             settings.values['start'], log)

    # Output the starting point for simulation, including the standings.
    click.echo(league.printStandings())
    log.message(league.printStandings())

    # Write top lines of output/labels once
    output.message(league.outputLine('Conference', league.teams))
    output.message(league.outputLine('Abbv', league.teams))

    # Iterate over games
    for i in range(settings.values['batch']):
        log.message("Season " + str(i))
        league.simulateSeason(game, settings.values['model'], log)
        output.message(league.outputLine('Points', league.standings))

    # Teardown
    click.echo('Finishing...')
    db.disconnect()
    log.message('Database disconnected')
    log.end()