Adding win and if the team is home to the database, calculations for win and lose streak, and returning the training data. Fixing sunrise sunset times
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
import ephem
|
||||
import requests
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
from suntime import Sun, SunTimeException
|
||||
from datetime import datetime
|
||||
from dateutil import tz
|
||||
from timezonefinder import TimezoneFinder
|
||||
|
||||
def get_sun_and_moon_phase(lat, long, date_str):
|
||||
curr_date = datetime.strptime(date_str, "%Y%m%d")
|
||||
date_str = f"{date_str[:4]}/{date_str[4:6]}/{date_str[6:8]}"
|
||||
|
||||
observer = ephem.Observer()
|
||||
observer.lat = str(lat)
|
||||
observer.lon = str(long)
|
||||
observer.date = date_str
|
||||
|
||||
sun = ephem.Sun()
|
||||
sun.compute(observer)
|
||||
|
||||
sunrise_time = int(observer.next_rising(sun).datetime().strftime('%Y%m%d'))
|
||||
sunset_time = int(observer.next_setting(sun).datetime().strftime('%Y%m%d'))
|
||||
my_tz = get_timezone(lat, long)
|
||||
sun = Sun(lat, long)
|
||||
sunrise_time = sun.get_sunrise_time(curr_date, tz.gettz(my_tz)).strftime("%H:%M:%S")
|
||||
sunset_time = sun.get_sunset_time(curr_date, tz.gettz(my_tz)).strftime("%H:%M:%S")
|
||||
|
||||
date = ephem.Date(date_str)
|
||||
moon = ephem.Moon()
|
||||
|
||||
189
data/get_data.py
189
data/get_data.py
@@ -211,7 +211,7 @@ def pull_training_data(database: Database, game_date: str, game_number: int, par
|
||||
- starting_9_position
|
||||
|
||||
- Output
|
||||
- 0 home team wins, 1 visiting team wins
|
||||
- 1 home team wins, 0 visiting team wins
|
||||
"""
|
||||
|
||||
select_upcoming_game = """
|
||||
@@ -257,6 +257,7 @@ def pull_training_data(database: Database, game_date: str, game_number: int, par
|
||||
|
||||
select_teams = """
|
||||
SELECT
|
||||
win, team, home,
|
||||
game_num, manager_id, starting_pitcher_id,
|
||||
starting_1_id, starting_1_position,
|
||||
starting_2_id, starting_2_position,
|
||||
@@ -267,76 +268,138 @@ def pull_training_data(database: Database, game_date: str, game_number: int, par
|
||||
starting_7_id, starting_7_position,
|
||||
starting_8_id, starting_8_position,
|
||||
starting_9_id, starting_9_position
|
||||
FROM
|
||||
team_game
|
||||
WHERE
|
||||
game = ?
|
||||
"""
|
||||
|
||||
curr_team_game = database.selectall(select_teams, [curr_game[0]])
|
||||
|
||||
if curr_game.len() != 2:
|
||||
print(f"Got the wrong number of games {curr_game.len()}")
|
||||
if len(curr_team_game) != 2:
|
||||
print(f"Got the wrong number of games {len(curr_team_game)}")
|
||||
return None
|
||||
|
||||
training_result = (curr_team_game[0][0] == 1 and curr_team_game[0][2] == 1)
|
||||
|
||||
select_previous_games = """
|
||||
SELECT
|
||||
- win_streak
|
||||
- loss_streak
|
||||
- game_date
|
||||
- day_of_weeki
|
||||
- length_in_outs
|
||||
- day_night
|
||||
- completion_info
|
||||
- forfeit
|
||||
- protest
|
||||
- park_id
|
||||
- attendence
|
||||
- length_in_minutes
|
||||
- hits
|
||||
- doubles
|
||||
- triples
|
||||
- homeruns
|
||||
- rbis
|
||||
- sacrifice_hits
|
||||
- sacrifice_flies
|
||||
- hit_by_pitch
|
||||
- walks
|
||||
- intentional_walks
|
||||
- strikeouts
|
||||
- stolen_bases
|
||||
- caught_stealing
|
||||
- grounded_double
|
||||
- interference
|
||||
- left_on_base
|
||||
- pitchers_used
|
||||
- individual_earned_runs
|
||||
- earned_runs
|
||||
- wild_pitches
|
||||
- balks
|
||||
- putouts
|
||||
- assists
|
||||
- errors
|
||||
- passed
|
||||
- double_play
|
||||
- triple_play
|
||||
- game_num
|
||||
- manager_id
|
||||
- starting_pitcher_id
|
||||
- starting_1_id
|
||||
- starting_1_position
|
||||
- starting_2_id
|
||||
- starting_2_position
|
||||
- starting_3_id
|
||||
- starting_3_position
|
||||
- starting_4_id
|
||||
- starting_4_position
|
||||
- starting_5_id
|
||||
- starting_5_position
|
||||
- starting_6_id
|
||||
- starting_6_position
|
||||
- starting_7_id
|
||||
- starting_7_position
|
||||
- starting_8_id
|
||||
- starting_8_position
|
||||
- starting_9_id
|
||||
- starting_9_position
|
||||
games.id,
|
||||
|
||||
games.game_date, games.game_number,
|
||||
games.day_of_week, games.day_night,
|
||||
games.length_in_outs, games.completion_info,
|
||||
games.forfeit, games.protest,
|
||||
games.attendence, games.length_in_minutes,
|
||||
|
||||
games.home_plate_ump_id,
|
||||
games.b1_ump_id, games.b2_ump_id, games.b3_ump_id,
|
||||
games.lf_ump_id, games.rf_ump_id,
|
||||
|
||||
team_game.game_num, team_game.score,
|
||||
team_game.line_score, team_game.win,
|
||||
team_game.hits, team_game.doubles, team_game.triples,
|
||||
team_game.homeruns, team_game.rbis,
|
||||
team_game.sacrifice_hits, team_game.sacrifice_flies,
|
||||
team_game.hit_by_pitch, team_game.walks, team_game.intentional_walks,
|
||||
team_game.strikeouts, team_game.stolen_bases,
|
||||
team_game.caught_stealing, team_game.grounded_double,
|
||||
team_game.interference, team_game.left_on_base,
|
||||
team_game.pitchers_used,
|
||||
team_game.individual_earned_runs, team_game.earned_runs,
|
||||
team_game.wild_pitches, team_game.balks,
|
||||
team_game.putouts, team_game.assists,
|
||||
team_game.errors, team_game.passed,
|
||||
team_game.double_play, team_game.triple_play,
|
||||
team_game.manager_id, team_game.starting_pitcher_id,
|
||||
team_game.starting_1_id, team_game.starting_1_position,
|
||||
team_game.starting_2_id, team_game.starting_2_position,
|
||||
team_game.starting_3_id, team_game.starting_3_position,
|
||||
team_game.starting_4_id, team_game.starting_4_position,
|
||||
team_game.starting_5_id, team_game.starting_5_position,
|
||||
team_game.starting_6_id, team_game.starting_6_position,
|
||||
team_game.starting_7_id, team_game.starting_7_position,
|
||||
team_game.starting_8_id, team_game.starting_8_position,
|
||||
team_game.starting_9_id, team_game.starting_9_position,
|
||||
|
||||
parks.park_id,
|
||||
|
||||
weather.temperature, weather.humidity, weather.dew_point,
|
||||
weather.apparent_temperature, weather.air_pressure,
|
||||
weather.precipitation, weather.rain, weather.snowfall,
|
||||
weather.cloud_cover,
|
||||
weather.wind_speed, weather.wind_direction, weather.wind_gusts,
|
||||
weather.sun_rise, weather.sun_set, weather.moon_phase
|
||||
|
||||
FROM
|
||||
games
|
||||
|
||||
LEFT JOIN parks ON parks.park_id = games.park_id
|
||||
LEFT JOIN team_game ON team_game.game = games.id
|
||||
LEFT JOIN weather ON weather.game_id = games.id
|
||||
|
||||
WHERE
|
||||
games.game_date > ? AND
|
||||
games.game_date < ? AND
|
||||
team_game.team = ?
|
||||
|
||||
ORDER BY team_game.game_num ASC
|
||||
"""
|
||||
|
||||
first_of_the_year = f"{game_date[:4]}0101"
|
||||
select_previous_games_data_0 = [first_of_the_year, game_date, curr_team_game[0][1]]
|
||||
select_previous_games_data_1 = [first_of_the_year, game_date, curr_team_game[1][1]]
|
||||
|
||||
training_data = [*curr_game[1:]]
|
||||
|
||||
training_data = [*training_data, *curr_team_game[0][1:]]
|
||||
prev_games = database.selectall(select_previous_games, select_previous_games_data_0)
|
||||
prev_game_data = [0] * 61
|
||||
if prev_games is not None:
|
||||
prev_win_streak = 0
|
||||
index = len(prev_games) - 1
|
||||
while index > 0 and prev_games[index][20] == 1:
|
||||
prev_win_streak += 1
|
||||
index -= 1
|
||||
|
||||
prev_loss_streak = 0
|
||||
index = len(prev_games) - 1
|
||||
while index > 0 and prev_games[index][20] == 0:
|
||||
prev_loss_streak += 1
|
||||
index -= 1
|
||||
|
||||
index = len(prev_games) - 1
|
||||
prev_game_data = [
|
||||
prev_win_streak,
|
||||
prev_loss_streak,
|
||||
*prev_games[index][1:]
|
||||
]
|
||||
training_data = [*training_data, *prev_game_data]
|
||||
|
||||
training_data = [*training_data, *curr_team_game[1][1:]]
|
||||
prev_games = database.selectall(select_previous_games, select_previous_games_data_1)
|
||||
prev_game_data = [0] * 61
|
||||
if prev_games is not None:
|
||||
prev_win_streak = 0
|
||||
index = len(prev_games) - 1
|
||||
while index > 0 and prev_games[index][20] == 1:
|
||||
prev_win_streak += 1
|
||||
index -= 1
|
||||
|
||||
prev_loss_streak = 0
|
||||
index = len(prev_games) - 1
|
||||
while index > 0 and prev_games[index][20] == 0:
|
||||
prev_loss_streak += 1
|
||||
index -= 1
|
||||
|
||||
index = len(prev_games) - 1
|
||||
prev_game_data = [
|
||||
prev_win_streak,
|
||||
prev_loss_streak,
|
||||
*prev_games[index][1:]
|
||||
]
|
||||
training_data = [*training_data, *prev_game_data]
|
||||
|
||||
print(f"{training_result}\n{training_data}")
|
||||
|
||||
return (training_result, training_data)
|
||||
|
||||
@@ -2,7 +2,7 @@ CREATE TABLE IF NOT EXISTS parks (
|
||||
park_id CHAR(5) NOT NULL,
|
||||
park_name VARCHAR,
|
||||
lf_fence_distance SMALLINT UNSIGNED,
|
||||
lf_fence_height, SMALLINT UNSIGNED,
|
||||
lf_fence_height SMALLINT UNSIGNED,
|
||||
ct_fence_distance SMALLINT UNSIGNED,
|
||||
ct_fence_height SMALLINT UNSIGNED,
|
||||
rf_fence_distance SMALLINT UNSIGNED,
|
||||
@@ -59,6 +59,8 @@ CREATE TABLE IF NOT EXISTS team_game (
|
||||
game_num TINYINT UNSIGNED,
|
||||
score TINYINT UNSIGNED,
|
||||
line_score CHAR(9),
|
||||
win BIT,
|
||||
home BIT,
|
||||
|
||||
at_bats TINYINT UNSIGNED,
|
||||
hits TINYINT UNSIGNED,
|
||||
|
||||
@@ -107,7 +107,7 @@ class Importer:
|
||||
INSERT INTO team_game
|
||||
(
|
||||
game, team, game_num,
|
||||
score, line_score, at_bats,
|
||||
score, line_score, win, home, at_bats,
|
||||
hits, doubles, triples,
|
||||
homeruns, rbis, sacrifice_hits,
|
||||
sacrifice_flies, hit_by_pitch, walks,
|
||||
@@ -132,7 +132,7 @@ class Importer:
|
||||
VALUES
|
||||
(
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
@@ -205,9 +205,10 @@ class Importer:
|
||||
|
||||
game_id = self.database.insert(insert_game, game_data)
|
||||
|
||||
visiting_win_loss = int(game_stats["visiting-score"]) > int(game_stats["home-score"])
|
||||
visiting_team_data = [
|
||||
game_id, game_stats["visiting-team"], game_stats["visiting-game-num"],
|
||||
game_stats["visiting-score"], game_stats["visiting-line-scores"], game_stats["visiting-at-bats"],
|
||||
game_stats["visiting-score"], game_stats["visiting-line-scores"], visiting_win_loss, 0, game_stats["visiting-at-bats"],
|
||||
game_stats["visiting-hits"], game_stats["visiting-doubles"], game_stats["visiting-triples"],
|
||||
game_stats["visiting-homeruns"], game_stats["visiting-rbi"], game_stats["visiting-sacrifice-hits"],
|
||||
game_stats["visiting-sacrifice-flies"], game_stats["visiting-hit-by-pitch"], game_stats["visiting-walks"],
|
||||
@@ -230,9 +231,10 @@ class Importer:
|
||||
game_stats["visiting-starting-9-position"]
|
||||
]
|
||||
|
||||
home_win_loss = int(game_stats["home-score"]) > int(game_stats["visiting-score"])
|
||||
home_team_data = [
|
||||
game_id, game_stats["home-team"], game_stats["home-game-num"],
|
||||
game_stats["home-score"], game_stats["home-line-scores"], game_stats["home-at-bats"],
|
||||
game_stats["home-score"], game_stats["home-line-scores"], home_win_loss, 1, game_stats["home-at-bats"],
|
||||
game_stats["home-hits"], game_stats["home-doubles"], game_stats["home-triples"],
|
||||
game_stats["home-homeruns"], game_stats["home-rbi"], game_stats["home-sacrifice-hits"],
|
||||
game_stats["home-sacrifice-flies"], game_stats["home-hit-by-pitch"], game_stats["home-walks"],
|
||||
|
||||
Reference in New Issue
Block a user