Skip to content
Snippets Groups Projects
Commit 7e7c960f authored by Lennard Geese's avatar Lennard Geese
Browse files

Integrate new weather fetching from wikipedia into active weather analysis

parent 4143b8eb
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import string ...@@ -4,6 +4,7 @@ import string
import pandas as pandas import pandas as pandas
from fastf1.core import Session, Lap, Laps, DataNotLoadedError from fastf1.core import Session, Lap, Laps, DataNotLoadedError
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from fastf1.events import Event
from DataHandler import DataHandler from DataHandler import DataHandler
...@@ -145,7 +146,27 @@ class DataAnalyser(DataHandler): ...@@ -145,7 +146,27 @@ class DataAnalyser(DataHandler):
raise KeyError("No weather entry found") raise KeyError("No weather entry found")
def filterForRainSessions(self, sessions: list[Session]): def filterForWetEvents(self, events: list[Event]):
"""
Filter out & return only those events from input list that had wet conditions in any notable capacity according
to their respective wikipedia pages.
:param sessions: List of sessions from which to pick out sessions with rain.
:return: List of sessions which had rain falling during the session for any amount of time.
"""
from DataImporter import DataImporter # here due to circular dependency
wetEvents: list[Event] = []
importer = DataImporter()
for event in events:
print(f"Filtering {event.year} {event["EventName"]}")
weatherDescription: string = importer.fetchWeather(event)
if self.isWet(weatherDescription): wetEvents.append(event)
return wetEvents
# TODO: Rename (other than legacy)
@DeprecationWarning
def filterForRainSessionsLEGACY(self, sessions: list[Session]):
""" """
Filter out & return only those sessions from input list that had rain falling at any point during the session. Filter out & return only those sessions from input list that had rain falling at any point during the session.
......
...@@ -100,18 +100,20 @@ class DataImporter(DataHandler, ABC): ...@@ -100,18 +100,20 @@ class DataImporter(DataHandler, ABC):
session.load(laps = laps, telemetry = telemetry, weather = weather, messages = messages) session.load(laps = laps, telemetry = telemetry, weather = weather, messages = messages)
return session return session
def getRainRacesSince(self, firstYear: int): def getWetEventsSince(self, firstYear: int):
currentYear = datetime.now().year currentYear = datetime.now().year
if firstYear > currentYear: if firstYear > currentYear:
raise ValueError("Cannot get race data from the future :)") raise ValueError("Cannot get race data from the future :)")
rainRaces: list[Session] = [] rainRaces: list[Session] = []
for firstYear in range(firstYear, currentYear): # FIXME: Handle exception after new years, when no events have run in current year yet for firstYear in range(firstYear, currentYear): # FIXME: Handle exception after new years, when no events have run in current year yet
wetWeatherRacesInYear: list[Session] = self.getRainRacesIn(firstYear) wetWeatherRacesInYear: list[Session] = self.getWetEventsIn(firstYear)
for wetWeatherRace in wetWeatherRacesInYear: for wetWeatherRace in wetWeatherRacesInYear:
rainRaces.append(wetWeatherRace) rainRaces.append(wetWeatherRace)
return rainRaces return rainRaces
# TODO: Rename (other than legacy)
@DeprecationWarning
def getRainRacesIn(self, year: int): def getRainRacesIn(self, year: int):
events: list[Event] = self.importAllEventsFromYear(year) events: list[Event] = self.importAllEventsFromYear(year)
races: list[Session] = [] races: list[Session] = []
...@@ -120,10 +122,20 @@ class DataImporter(DataHandler, ABC): ...@@ -120,10 +122,20 @@ class DataImporter(DataHandler, ABC):
try: try:
raceSession: Session = self.importSessionWeather(raceIdentifier) raceSession: Session = self.importSessionWeather(raceIdentifier)
races.append(raceSession) races.append(raceSession)
except ValueError: # Needed as long as weather data for races before 2018 hasn't been supplemented except ValueError: # Needed as long as weather data for races before 2018 hasn't been supplemented
print(f"Weather for {event["EventName"]} {event.year} could not be determined") print(f"Weather for {event["EventName"]} {event.year} could not be determined")
analyser: DataAnalyser = DataAnalyser() analyser: DataAnalyser = DataAnalyser()
rainRaces: list[Session] = analyser.filterForRainSessions(races) rainRaces: list[Session] = analyser.filterForRainSessionsLEGACY(races)
return rainRaces
def getWetEventsIn(self, year: int):
print(f"Fetching wet events in {year}")
events: list[Event] = self.importAllEventsFromYear(year)
analyser: DataAnalyser = DataAnalyser()
rainRaces: list[Session] = analyser.filterForWetEvents(events)
return rainRaces
return rainRaces
\ No newline at end of file
...@@ -31,9 +31,9 @@ class Main: ...@@ -31,9 +31,9 @@ class Main:
#dataHandler = Main.DataHandlingPackage() #dataHandler = Main.DataHandlingPackage()
#dataHandler.plotter.plotBackgroundPaintTest() #dataHandler.plotter.plotBackgroundPaintTest()
self.testNewRainRaceFetching() #self.testNewRainRaceFetching()
#self.printRainRaces() self.printWetEvents()
#self.overtakeAnalysis(racesToAnalyse) #self.overtakeAnalysis(racesToAnalyse)
...@@ -45,13 +45,14 @@ class Main: ...@@ -45,13 +45,14 @@ class Main:
isWet: bool = dataHandler.analyser.isWet(w) isWet: bool = dataHandler.analyser.isWet(w)
print(isWet) print(isWet)
def printRainRaces(self): def printWetEvents(self):
dataHandler: Main.DataHandlingPackage = Main.DataHandlingPackage() dataHandler: Main.DataHandlingPackage = Main.DataHandlingPackage()
year: int = 2015 year: int = 2015
rainRaces: list[Session] = dataHandler.importer.getRainRacesSince(year) wetEvents: list[Event] = dataHandler.importer.getWetEventsSince(year)
print(f"Rain races since {max(year, 2018)}") print(f"Wet races since {year} (according to wikipedia):")
for rainRace in rainRaces: for wetEvent in wetEvents:
print(rainRace) print(f"{wetEvent.year} {wetEvent["EventName"]}")
def overtakeAnalysis(self, raceSessionIdentifiers: list[SessionIdentifier]): def overtakeAnalysis(self, raceSessionIdentifiers: list[SessionIdentifier]):
dataHandler: Main.DataHandlingPackage = self.DataHandlingPackage() dataHandler: Main.DataHandlingPackage = self.DataHandlingPackage()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment